blob: 5ad22e978a8698a80f6bb27942a1d3551b0609d5 [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
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100162.. js:attribute:: core.emerg
163
Thierry FOURNIERdc595002015-12-21 11:13:52 +0100164 :returns: integer
165
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +0200166 This attribute is an integer, it contains the value of the loglevel
167 "emergency" (0).
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100168
169.. js:attribute:: core.alert
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 "alert" (1).
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100175
176.. js:attribute:: core.crit
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 "critical" (2).
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100182
183.. js:attribute:: core.err
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 "error" (3).
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100189
190.. js:attribute:: core.warning
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 "warning" (4).
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100196
197.. js:attribute:: core.notice
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 "notice" (5).
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100203
204.. js:attribute:: core.info
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 "info" (6).
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100210
211.. js:attribute:: core.debug
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 "debug" (7).
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100217
Thierry FOURNIER12a865d2016-12-14 19:40:37 +0100218.. js:attribute:: core.proxies
219
Aurelien DARRAGON2a295712023-05-11 17:31:46 +0200220 **context**: init, task, action, sample-fetch, converter
Thierry FOURNIER12a865d2016-12-14 19:40:37 +0100221
Patrick Hemmerc6a1d712018-05-01 21:30:41 -0400222 This attribute is a table of declared proxies (frontend and backends). Each
223 proxy give an access to his list of listeners and servers. The table is
224 indexed by proxy name, and each entry is of type :ref:`proxy_class`.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +0100225
Christopher Faulet1e9b1b62021-08-11 10:14:30 +0200226 .. Warning::
Aurelien DARRAGON53901f42022-10-13 19:49:42 +0200227 if you declared a frontend and backend with the same name, only one of
228 them will be listed.
Thierry FOURNIER9b82a582017-07-24 13:30:43 +0200229
230 :see: :js:attr:`core.backends`
231 :see: :js:attr:`core.frontends`
232
233.. js:attribute:: core.backends
234
Aurelien DARRAGON2a295712023-05-11 17:31:46 +0200235 **context**: init, task, action, sample-fetch, converter
Thierry FOURNIER9b82a582017-07-24 13:30:43 +0200236
Patrick Hemmerc6a1d712018-05-01 21:30:41 -0400237 This attribute is a table of declared proxies with backend capability. Each
238 proxy give an access to his list of listeners and servers. The table is
239 indexed by the backend name, and each entry is of type :ref:`proxy_class`.
Thierry FOURNIER9b82a582017-07-24 13:30:43 +0200240
241 :see: :js:attr:`core.proxies`
242 :see: :js:attr:`core.frontends`
243
244.. js:attribute:: core.frontends
245
Aurelien DARRAGON2a295712023-05-11 17:31:46 +0200246 **context**: init, task, action, sample-fetch, converter
Thierry FOURNIER9b82a582017-07-24 13:30:43 +0200247
Patrick Hemmerc6a1d712018-05-01 21:30:41 -0400248 This attribute is a table of declared proxies with frontend capability. Each
249 proxy give an access to his list of listeners and servers. The table is
250 indexed by the frontend name, and each entry is of type :ref:`proxy_class`.
Thierry FOURNIER9b82a582017-07-24 13:30:43 +0200251
252 :see: :js:attr:`core.proxies`
253 :see: :js:attr:`core.backends`
254
Thierry Fournierecb83c22020-11-28 15:49:44 +0100255.. js:attribute:: core.thread
256
257 **context**: task, action, sample-fetch, converter, applet
258
259 This variable contains the executing thread number starting at 1. 0 is a
260 special case for the common lua context. So, if thread is 0, Lua scope is
261 shared by all threads, otherwise the scope is dedicated to a single thread.
262 A program which needs to execute some parts exactly once regardless of the
263 number of threads can check that core.thread is 0 or 1.
264
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100265.. js:function:: core.log(loglevel, msg)
266
267 **context**: body, init, task, action, sample-fetch, converter
268
David Carlier61fdf8b2015-10-02 11:59:38 +0100269 This function sends a log. The log is sent, according with the HAProxy
Tristan2632d042023-10-23 13:07:39 +0100270 configuration file, to the loggers relevant to the current context and
271 to stderr if it is allowed.
272
273 The exact behaviour depends on tune.lua.log.loggers and tune.lua.log.stderr.
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100274
Bertrand Jacquin874a35c2018-09-10 21:26:07 +0100275 :param integer loglevel: Is the log level associated with the message. It is a
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +0200276 number between 0 and 7.
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100277 :param string msg: The log content.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +0100278 :see: :js:attr:`core.emerg`, :js:attr:`core.alert`, :js:attr:`core.crit`,
279 :js:attr:`core.err`, :js:attr:`core.warning`, :js:attr:`core.notice`,
280 :js:attr:`core.info`, :js:attr:`core.debug` (log level definitions)
281 :see: :js:func:`core.Debug`
282 :see: :js:func:`core.Info`
283 :see: :js:func:`core.Warning`
284 :see: :js:func:`core.Alert`
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100285
286.. js:function:: core.Debug(msg)
287
288 **context**: body, init, task, action, sample-fetch, converter
289
290 :param string msg: The log content.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +0100291 :see: :js:func:`core.log`
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100292
293 Does the same job than:
294
295.. code-block:: lua
296
Thierry FOURNIERdc595002015-12-21 11:13:52 +0100297 function Debug(msg)
298 core.log(core.debug, msg)
299 end
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100300..
301
302.. js:function:: core.Info(msg)
303
304 **context**: body, init, task, action, sample-fetch, converter
305
306 :param string msg: The log content.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +0100307 :see: :js:func:`core.log`
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100308
309.. code-block:: lua
310
Thierry FOURNIERdc595002015-12-21 11:13:52 +0100311 function Info(msg)
312 core.log(core.info, msg)
313 end
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100314..
315
316.. js:function:: core.Warning(msg)
317
318 **context**: body, init, task, action, sample-fetch, converter
319
320 :param string msg: The log content.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +0100321 :see: :js:func:`core.log`
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100322
323.. code-block:: lua
324
Thierry FOURNIERdc595002015-12-21 11:13:52 +0100325 function Warning(msg)
326 core.log(core.warning, msg)
327 end
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100328..
329
330.. js:function:: core.Alert(msg)
331
332 **context**: body, init, task, action, sample-fetch, converter
333
334 :param string msg: The log content.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +0100335 :see: :js:func:`core.log`
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100336
337.. code-block:: lua
338
Thierry FOURNIERdc595002015-12-21 11:13:52 +0100339 function Alert(msg)
340 core.log(core.alert, msg)
341 end
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100342..
343
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100344.. js:function:: core.add_acl(filename, key)
345
346 **context**: init, task, action, sample-fetch, converter
347
348 Add the ACL *key* in the ACLs list referenced by the file *filename*.
349
350 :param string filename: the filename that reference the ACL entries.
351 :param string key: the key which will be added.
352
353.. js:function:: core.del_acl(filename, key)
354
355 **context**: init, task, action, sample-fetch, converter
356
357 Delete the ACL entry referenced by the key *key* in the list of ACLs
358 referenced by *filename*.
359
360 :param string filename: the filename that reference the ACL entries.
361 :param string key: the key which will be deleted.
362
363.. js:function:: core.del_map(filename, key)
364
365 **context**: init, task, action, sample-fetch, converter
366
367 Delete the map entry indexed with the specified key in the list of maps
368 referenced by his filename.
369
370 :param string filename: the filename that reference the map entries.
371 :param string key: the key which will be deleted.
372
Thierry Fourniereea77c02016-03-18 08:47:13 +0100373.. js:function:: core.get_info()
374
375 **context**: body, init, task, action, sample-fetch, converter
376
Aurelien DARRAGON53901f42022-10-13 19:49:42 +0200377 Returns HAProxy core information. We can find information like the uptime,
Thierry Fourniereea77c02016-03-18 08:47:13 +0100378 the pid, memory pool usage, tasks number, ...
379
Ilya Shipitsin5fa29b82022-12-07 09:46:19 +0500380 This information is also returned by the management socket via the command
Bertrand Jacquin874a35c2018-09-10 21:26:07 +0100381 "show info". See the management socket documentation for more information
Thierry Fourniereea77c02016-03-18 08:47:13 +0100382 about the content of these variables.
383
384 :returns: an array of values.
385
Thierry Fournierb1f46562016-01-21 09:46:15 +0100386.. js:function:: core.now()
387
388 **context**: body, init, task, action
389
390 This function returns the current time. The time returned is fixed by the
Bertrand Jacquin874a35c2018-09-10 21:26:07 +0100391 HAProxy core and assures than the hour will be monotonic and that the system
Thierry Fournierb1f46562016-01-21 09:46:15 +0100392 call 'gettimeofday' will not be called too. The time is refreshed between each
393 Lua execution or resume, so two consecutive call to the function "now" will
394 probably returns the same result.
395
Patrick Hemmerc6a1d712018-05-01 21:30:41 -0400396 :returns: a table which contains two entries "sec" and "usec". "sec"
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +0200397 contains the current at the epoch format, and "usec" contains the
398 current microseconds.
Thierry Fournierb1f46562016-01-21 09:46:15 +0100399
Thierry FOURNIERa78f0372016-12-14 19:04:41 +0100400.. js:function:: core.http_date(date)
401
402 **context**: body, init, task, action
403
Bertrand Jacquin874a35c2018-09-10 21:26:07 +0100404 This function take a string representing http date, and returns an integer
Thierry FOURNIERa78f0372016-12-14 19:04:41 +0100405 containing the corresponding date with a epoch format. A valid http date
406 me respect the format IMF, RFC850 or ASCTIME.
407
408 :param string date: a date http-date formatted
409 :returns: integer containing epoch date
410 :see: :js:func:`core.imf_date`.
411 :see: :js:func:`core.rfc850_date`.
412 :see: :js:func:`core.asctime_date`.
413 :see: https://tools.ietf.org/html/rfc7231#section-7.1.1.1
414
415.. js:function:: core.imf_date(date)
416
417 **context**: body, init, task, action
418
Bertrand Jacquin874a35c2018-09-10 21:26:07 +0100419 This function take a string representing IMF date, and returns an integer
Thierry FOURNIERa78f0372016-12-14 19:04:41 +0100420 containing the corresponding date with a epoch format.
421
422 :param string date: a date IMF formatted
423 :returns: integer containing epoch date
424 :see: https://tools.ietf.org/html/rfc7231#section-7.1.1.1
425
426 The IMF format is like this:
427
428.. code-block:: text
429
430 Sun, 06 Nov 1994 08:49:37 GMT
431..
432
433.. js:function:: core.rfc850_date(date)
434
435 **context**: body, init, task, action
436
Bertrand Jacquin874a35c2018-09-10 21:26:07 +0100437 This function take a string representing RFC850 date, and returns an integer
Thierry FOURNIERa78f0372016-12-14 19:04:41 +0100438 containing the corresponding date with a epoch format.
439
440 :param string date: a date RFC859 formatted
441 :returns: integer containing epoch date
442 :see: https://tools.ietf.org/html/rfc7231#section-7.1.1.1
443
444 The RFC850 format is like this:
445
446.. code-block:: text
447
448 Sunday, 06-Nov-94 08:49:37 GMT
449..
450
451.. js:function:: core.asctime_date(date)
452
453 **context**: body, init, task, action
454
Bertrand Jacquin874a35c2018-09-10 21:26:07 +0100455 This function take a string representing ASCTIME date, and returns an integer
Thierry FOURNIERa78f0372016-12-14 19:04:41 +0100456 containing the corresponding date with a epoch format.
457
458 :param string date: a date ASCTIME formatted
459 :returns: integer containing epoch date
460 :see: https://tools.ietf.org/html/rfc7231#section-7.1.1.1
461
462 The ASCTIME format is like this:
463
464.. code-block:: text
465
466 Sun Nov 6 08:49:37 1994
467..
468
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100469.. js:function:: core.msleep(milliseconds)
470
471 **context**: body, init, task, action
472
473 The `core.msleep()` stops the Lua execution between specified milliseconds.
474
475 :param integer milliseconds: the required milliseconds.
476
Thierry FOURNIERc5d11c62018-02-12 14:46:54 +0100477.. js:function:: core.register_action(name, actions, func [, nb_args])
Thierry FOURNIER8255a752015-09-23 21:03:35 +0200478
479 **context**: body
480
David Carlier61fdf8b2015-10-02 11:59:38 +0100481 Register a Lua function executed as action. All the registered action can be
Thierry FOURNIER8255a752015-09-23 21:03:35 +0200482 used in HAProxy with the prefix "lua.". An action gets a TXN object class as
483 input.
484
Aurelien DARRAGONe239e702023-08-23 17:38:42 +0200485 :param string name: is the name of the action.
486 :param table actions: is a table of string describing the HAProxy actions
487 facilities where to expose the new action. Expected facilities are:
488 'tcp-req', 'tcp-res', 'http-req' or 'http-res'.
489 :param function func: is the Lua function called to work as an action.
Thierry FOURNIERc5d11c62018-02-12 14:46:54 +0100490 :param integer nb_args: is the expected number of argument for the action.
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +0200491 By default the value is 0.
Thierry FOURNIER8255a752015-09-23 21:03:35 +0200492
493 The prototype of the Lua function used as argument is:
494
495.. code-block:: lua
496
Thierry FOURNIERc5d11c62018-02-12 14:46:54 +0100497 function(txn [, arg1 [, arg2]])
Thierry FOURNIER8255a752015-09-23 21:03:35 +0200498..
499
Thierry FOURNIERdc595002015-12-21 11:13:52 +0100500 * **txn** (:ref:`txn_class`): this is a TXN object used for manipulating the
Thierry FOURNIER8255a752015-09-23 21:03:35 +0200501 current request or TCP stream.
502
Bertrand Jacquin874a35c2018-09-10 21:26:07 +0100503 * **argX**: this is argument provided through the HAProxy configuration file.
Thierry FOURNIERc5d11c62018-02-12 14:46:54 +0100504
Bertrand Jacquin874a35c2018-09-10 21:26:07 +0100505 Here, an example of action registration. The action just send an 'Hello world'
Thierry FOURNIER8255a752015-09-23 21:03:35 +0200506 in the logs.
507
508.. code-block:: lua
509
510 core.register_action("hello-world", { "tcp-req", "http-req" }, function(txn)
511 txn:Info("Hello world")
512 end)
513..
514
Willy Tarreau714f3452021-05-09 06:47:26 +0200515 This example code is used in HAProxy configuration like this:
Thierry FOURNIER8255a752015-09-23 21:03:35 +0200516
517::
518
519 frontend tcp_frt
520 mode tcp
521 tcp-request content lua.hello-world
522
523 frontend http_frt
524 mode http
525 http-request lua.hello-world
Aurelien DARRAGONd5c80d72023-03-13 19:36:13 +0100526
Thierry FOURNIERc5d11c62018-02-12 14:46:54 +0100527..
528
Bertrand Jacquin874a35c2018-09-10 21:26:07 +0100529 A second example using arguments
Thierry FOURNIERc5d11c62018-02-12 14:46:54 +0100530
531.. code-block:: lua
532
533 function hello_world(txn, arg)
534 txn:Info("Hello world for " .. arg)
535 end
536 core.register_action("hello-world", { "tcp-req", "http-req" }, hello_world, 2)
Aurelien DARRAGONd5c80d72023-03-13 19:36:13 +0100537
Thierry FOURNIERc5d11c62018-02-12 14:46:54 +0100538..
Thierry FOURNIER8255a752015-09-23 21:03:35 +0200539
Willy Tarreau714f3452021-05-09 06:47:26 +0200540 This example code is used in HAProxy configuration like this:
Thierry FOURNIERc5d11c62018-02-12 14:46:54 +0100541
542::
543
544 frontend tcp_frt
545 mode tcp
546 tcp-request content lua.hello-world everybody
Aurelien DARRAGONd5c80d72023-03-13 19:36:13 +0100547
Thierry FOURNIERc5d11c62018-02-12 14:46:54 +0100548..
Aurelien DARRAGON53901f42022-10-13 19:49:42 +0200549
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100550.. js:function:: core.register_converters(name, func)
551
552 **context**: body
553
David Carlier61fdf8b2015-10-02 11:59:38 +0100554 Register a Lua function executed as converter. All the registered converters
Aurelien DARRAGON53901f42022-10-13 19:49:42 +0200555 can be used in HAProxy with the prefix "lua.". A converter gets a string as
556 input and returns a string as output. The registered function can take up to 9
557 values as parameter. All the values are strings.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100558
559 :param string name: is the name of the converter.
560 :param function func: is the Lua function called to work as converter.
561
562 The prototype of the Lua function used as argument is:
563
564.. code-block:: lua
565
566 function(str, [p1 [, p2 [, ... [, p5]]]])
567..
568
569 * **str** (*string*): this is the input value automatically converted in
570 string.
571 * **p1** .. **p5** (*string*): this is a list of string arguments declared in
Bertrand Jacquin874a35c2018-09-10 21:26:07 +0100572 the HAProxy configuration file. The number of arguments doesn't exceed 5.
Aurelien DARRAGON53901f42022-10-13 19:49:42 +0200573 The order and the nature of these is conventionally chosen by the
Bertrand Jacquin874a35c2018-09-10 21:26:07 +0100574 developer.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100575
576.. js:function:: core.register_fetches(name, func)
577
578 **context**: body
579
David Carlier61fdf8b2015-10-02 11:59:38 +0100580 Register a Lua function executed as sample fetch. All the registered sample
Bertrand Jacquin874a35c2018-09-10 21:26:07 +0100581 fetch can be used in HAProxy with the prefix "lua.". A Lua sample fetch
Aurelien DARRAGON53901f42022-10-13 19:49:42 +0200582 returns a string as output. The registered function can take up to 9 values as
583 parameter. All the values are strings.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100584
Aurelien DARRAGON53901f42022-10-13 19:49:42 +0200585 :param string name: is the name of the sample fetch.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100586 :param function func: is the Lua function called to work as sample fetch.
587
588 The prototype of the Lua function used as argument is:
589
590.. code-block:: lua
591
592 string function(txn, [p1 [, p2 [, ... [, p5]]]])
593..
594
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +0200595 * **txn** (:ref:`txn_class`): this is the txn object associated with the
596 current request.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100597 * **p1** .. **p5** (*string*): this is a list of string arguments declared in
Bertrand Jacquin874a35c2018-09-10 21:26:07 +0100598 the HAProxy configuration file. The number of arguments doesn't exceed 5.
Aurelien DARRAGON53901f42022-10-13 19:49:42 +0200599 The order and the nature of these is conventionally chosen by the
Bertrand Jacquin874a35c2018-09-10 21:26:07 +0100600 developer.
601 * **Returns**: A string containing some data, or nil if the value cannot be
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100602 returned now.
603
604 lua example code:
605
606.. code-block:: lua
607
608 core.register_fetches("hello", function(txn)
609 return "hello"
610 end)
611..
612
613 HAProxy example configuration:
614
615::
616
617 frontend example
618 http-request redirect location /%[lua.hello]
619
Christopher Faulet5a2c6612021-08-15 20:35:25 +0200620.. js:function:: core.register_filter(name, Flt, func)
621
622 **context**: body
623
624 Register a Lua function used to declare a filter. All the registered filters
625 can by used in HAProxy with the prefix "lua.".
626
627 :param string name: is the name of the filter.
628 :param table Flt: is a Lua class containing the filter definition (id, flags,
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +0200629 callbacks).
Christopher Faulet5a2c6612021-08-15 20:35:25 +0200630 :param function func: is the Lua function called to create the Lua filter.
631
632 The prototype of the Lua function used as argument is:
633
634.. code-block:: lua
635
636 function(flt, args)
637..
638
639 * **flt** : Is a filter object based on the class provided in
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +0200640 :js:func:`core.register_filter()` function.
Christopher Faulet5a2c6612021-08-15 20:35:25 +0200641
642 * **args**: Is a table of strings containing all arguments provided through
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +0200643 the HAProxy configuration file, on the filter line.
Christopher Faulet5a2c6612021-08-15 20:35:25 +0200644
645 It must return the filter to use or nil to ignore it. Here, an example of
646 filter registration.
647
648.. code-block:: lua
649
650 core.register_filter("my-filter", MyFilter, function(flt, args)
651 flt.args = args -- Save arguments
652 return flt
653 end)
654..
655
656 This example code is used in HAProxy configuration like this:
657
658::
659
660 frontend http
661 mode http
662 filter lua.my-filter arg1 arg2 arg3
Aurelien DARRAGONd5c80d72023-03-13 19:36:13 +0100663
Christopher Faulet5a2c6612021-08-15 20:35:25 +0200664..
665
666 :see: :js:class:`Filter`
667
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +0200668.. js:function:: core.register_service(name, mode, func)
669
670 **context**: body
671
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +0200672 Register a Lua function executed as a service. All the registered services
673 can be used in HAProxy with the prefix "lua.". A service gets an object class
674 as input according with the required mode.
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +0200675
Aurelien DARRAGON53901f42022-10-13 19:49:42 +0200676 :param string name: is the name of the service.
Willy Tarreau61add3c2015-09-28 15:39:10 +0200677 :param string mode: is string describing the required mode. Only 'tcp' or
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +0200678 'http' are allowed.
Aurelien DARRAGON53901f42022-10-13 19:49:42 +0200679 :param function func: is the Lua function called to work as service.
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +0200680
681 The prototype of the Lua function used as argument is:
682
683.. code-block:: lua
684
685 function(applet)
686..
687
Thierry FOURNIERdc595002015-12-21 11:13:52 +0100688 * **applet** *applet* will be a :ref:`applettcp_class` or a
689 :ref:`applethttp_class`. It depends the type of registered applet. An applet
690 registered with the 'http' value for the *mode* parameter will gets a
691 :ref:`applethttp_class`. If the *mode* value is 'tcp', the applet will gets
692 a :ref:`applettcp_class`.
693
Christopher Faulet1e9b1b62021-08-11 10:14:30 +0200694 .. warning::
695 Applets of type 'http' cannot be called from 'tcp-*' rulesets. Only the
696 'http-*' rulesets are authorized, this means that is not possible to call
Aurelien DARRAGON53901f42022-10-13 19:49:42 +0200697 a HTTP applet from a proxy in tcp mode. Applets of type 'tcp' can be
Christopher Faulet1e9b1b62021-08-11 10:14:30 +0200698 called from anywhere.
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +0200699
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +0200700 Here, an example of service registration. The service just send an
701 'Hello world' as an http response.
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +0200702
703.. code-block:: lua
704
Pieter Baauw4d7f7662015-11-08 16:38:08 +0100705 core.register_service("hello-world", "http", function(applet)
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +0200706 local response = "Hello World !"
707 applet:set_status(200)
Pieter Baauw2dcb9bc2015-10-01 22:47:12 +0200708 applet:add_header("content-length", string.len(response))
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +0200709 applet:add_header("content-type", "text/plain")
Pieter Baauw2dcb9bc2015-10-01 22:47:12 +0200710 applet:start_response()
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +0200711 applet:send(response)
712 end)
713..
714
Willy Tarreau714f3452021-05-09 06:47:26 +0200715 This example code is used in HAProxy configuration like this:
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +0200716
717::
718
719 frontend example
720 http-request use-service lua.hello-world
721
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100722.. js:function:: core.register_init(func)
723
724 **context**: body
725
726 Register a function executed after the configuration parsing. This is useful
727 to check any parameters.
728
Pieter Baauw4d7f7662015-11-08 16:38:08 +0100729 :param function func: is the Lua function called to work as initializer.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100730
731 The prototype of the Lua function used as argument is:
732
733.. code-block:: lua
734
735 function()
736..
737
738 It takes no input, and no output is expected.
739
Aurelien DARRAGONb8038992023-03-09 16:48:30 +0100740.. js:function:: core.register_task(func[, arg1[, arg2[, ...[, arg4]]]])
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100741
Aurelien DARRAGONc84899c2023-02-20 18:18:59 +0100742 **context**: body, init, task, action, sample-fetch, converter, event
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100743
744 Register and start independent task. The task is started when the HAProxy
745 main scheduler starts. For example this type of tasks can be executed to
Thierry FOURNIER2e4893c2015-03-18 13:37:27 +0100746 perform complex health checks.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100747
Aurelien DARRAGONb8038992023-03-09 16:48:30 +0100748 :param function func: is the Lua function called to work as an async task.
749
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +0200750 Up to 4 optional arguments (all types supported) may be passed to the
751 function. (They will be passed as-is to the task function)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100752
753 The prototype of the Lua function used as argument is:
754
755.. code-block:: lua
756
Aurelien DARRAGONb8038992023-03-09 16:48:30 +0100757 function([arg1[, arg2[, ...[, arg4]]]])
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100758..
759
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +0200760 It takes up to 4 optional arguments (provided when registering), and no
761 output is expected.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100762
Aurelien DARRAGON86fb22c2023-05-03 17:03:09 +0200763 See also :js:func:`core.queue` to dynamically pass data between main context
764 and tasks or even between tasks.
765
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +0100766.. js:function:: core.register_cli([path], usage, func)
767
768 **context**: body
769
Aurelien DARRAGON53901f42022-10-13 19:49:42 +0200770 Register a custom cli that will be available from haproxy stats socket.
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +0100771
772 :param array path: is the sequence of word for which the cli execute the Lua
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +0200773 binding.
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +0100774 :param string usage: is the usage message displayed in the help.
775 :param function func: is the Lua function called to handle the CLI commands.
776
777 The prototype of the Lua function used as argument is:
778
779.. code-block:: lua
780
781 function(AppletTCP, [arg1, [arg2, [...]]])
782..
783
784 I/O are managed with the :ref:`applettcp_class` object. Args are given as
Bertrand Jacquin874a35c2018-09-10 21:26:07 +0100785 parameter. The args embed the registered path. If the path is declared like
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +0100786 this:
787
788.. code-block:: lua
789
790 core.register_cli({"show", "ssl", "stats"}, "Display SSL stats..", function(applet, arg1, arg2, arg3, arg4, arg5)
791 end)
792..
793
794 And we execute this in the prompt:
795
796.. code-block:: text
797
798 > prompt
799 > show ssl stats all
800..
801
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +0200802 Then, arg1, arg2 and arg3 will contains respectively "show", "ssl" and
803 "stats".
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +0100804 arg4 will contain "all". arg5 contains nil.
805
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100806.. js:function:: core.set_nice(nice)
807
808 **context**: task, action, sample-fetch, converter
809
810 Change the nice of the current task or current session.
Thierry FOURNIER2e4893c2015-03-18 13:37:27 +0100811
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100812 :param integer nice: the nice value, it must be between -1024 and 1024.
813
814.. js:function:: core.set_map(filename, key, value)
815
816 **context**: init, task, action, sample-fetch, converter
817
Bertrand Jacquin874a35c2018-09-10 21:26:07 +0100818 Set the value *value* associated to the key *key* in the map referenced by
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100819 *filename*.
820
Thierry FOURNIERdc595002015-12-21 11:13:52 +0100821 :param string filename: the Map reference
822 :param string key: the key to set or replace
823 :param string value: the associated value
824
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100825.. js:function:: core.sleep(int seconds)
826
827 **context**: body, init, task, action
828
829 The `core.sleep()` functions stop the Lua execution between specified seconds.
830
831 :param integer seconds: the required seconds.
832
833.. js:function:: core.tcp()
834
835 **context**: init, task, action
836
837 This function returns a new object of a *socket* class.
838
Thierry FOURNIERdc595002015-12-21 11:13:52 +0100839 :returns: A :ref:`socket_class` object.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100840
William Lallemand00a15022021-11-19 16:02:44 +0100841.. js:function:: core.httpclient()
842
843 **context**: init, task, action
844
845 This function returns a new object of a *httpclient* class.
846
847 :returns: A :ref:`httpclient_class` object.
848
Thierry Fournier1de16592016-01-27 09:49:07 +0100849.. js:function:: core.concat()
850
851 **context**: body, init, task, action, sample-fetch, converter
852
Bertrand Jacquin874a35c2018-09-10 21:26:07 +0100853 This function returns a new concat object.
Thierry Fournier1de16592016-01-27 09:49:07 +0100854
855 :returns: A :ref:`concat_class` object.
856
Aurelien DARRAGON86fb22c2023-05-03 17:03:09 +0200857.. js:function:: core.queue()
858
859 **context**: body, init, task, event, action, sample-fetch, converter
860
861 This function returns a new queue object.
862
863 :returns: A :ref:`queue_class` object.
864
Thierry FOURNIER0a99b892015-08-26 00:14:17 +0200865.. js:function:: core.done(data)
866
867 **context**: body, init, task, action, sample-fetch, converter
868
869 :param any data: Return some data for the caller. It is useful with
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +0200870 sample-fetches and sample-converters.
Thierry FOURNIER0a99b892015-08-26 00:14:17 +0200871
872 Immediately stops the current Lua execution and returns to the caller which
873 may be a sample fetch, a converter or an action and returns the specified
Thierry Fournier4234dbd2020-11-28 13:18:23 +0100874 value (ignored for actions and init). It is used when the LUA process finishes
875 its work and wants to give back the control to HAProxy without executing the
Thierry FOURNIER0a99b892015-08-26 00:14:17 +0200876 remaining code. It can be seen as a multi-level "return".
877
Thierry FOURNIER486f5a02015-03-16 15:13:03 +0100878.. js:function:: core.yield()
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100879
880 **context**: task, action, sample-fetch, converter
881
882 Give back the hand at the HAProxy scheduler. It is used when the LUA
883 processing consumes a lot of processing time.
884
Thierry FOURNIER / OZON.IO62fec752016-11-10 20:38:11 +0100885.. js:function:: core.parse_addr(address)
886
887 **context**: body, init, task, action, sample-fetch, converter
888
889 :param network: is a string describing an ipv4 or ipv6 address and optionally
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +0200890 its network length, like this: "127.0.0.1/8" or "aaaa::1234/32".
Thierry FOURNIER / OZON.IO62fec752016-11-10 20:38:11 +0100891 :returns: a userdata containing network or nil if an error occurs.
892
Bertrand Jacquin874a35c2018-09-10 21:26:07 +0100893 Parse ipv4 or ipv6 addresses and its facultative associated network.
Thierry FOURNIER / OZON.IO62fec752016-11-10 20:38:11 +0100894
895.. js:function:: core.match_addr(addr1, addr2)
896
897 **context**: body, init, task, action, sample-fetch, converter
898
899 :param addr1: is an address created with "core.parse_addr".
900 :param addr2: is an address created with "core.parse_addr".
Bertrand Jacquin874a35c2018-09-10 21:26:07 +0100901 :returns: boolean, true if the network of the addresses match, else returns
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +0200902 false.
Thierry FOURNIER / OZON.IO62fec752016-11-10 20:38:11 +0100903
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +0200904 Match two networks. For example "127.0.0.1/32" matches "127.0.0.0/8". The
905 order of network is not important.
Thierry FOURNIER / OZON.IO62fec752016-11-10 20:38:11 +0100906
Thierry FOURNIER / OZON.IO8a1027a2016-11-24 20:48:38 +0100907.. js:function:: core.tokenize(str, separators [, noblank])
908
909 **context**: body, init, task, action, sample-fetch, converter
910
911 This function is useful for tokenizing an entry, or splitting some messages.
912 :param string str: The string which will be split.
913 :param string separators: A string containing a list of separators.
914 :param boolean noblank: Ignore empty entries.
915 :returns: an array of string.
916
917 For example:
918
919.. code-block:: lua
920
921 local array = core.tokenize("This function is useful, for tokenizing an entry.", "., ", true)
922 print_r(array)
923..
924
925 Returns this array:
926
927.. code-block:: text
928
929 (table) table: 0x21c01e0 [
930 1: (string) "This"
931 2: (string) "function"
932 3: (string) "is"
933 4: (string) "useful"
934 5: (string) "for"
935 6: (string) "tokenizing"
936 7: (string) "an"
937 8: (string) "entry"
938 ]
939..
940
Aurelien DARRAGONc84899c2023-02-20 18:18:59 +0100941.. js:function:: core.event_sub(event_types, func)
942
943 **context**: body, init, task, action, sample-fetch, converter
944
945 Register a function that will be called on specific system events.
946
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +0200947 :param array event_types: array of string containing the event types you want
948 to subscribe to
949 :param function func: is the Lua function called when one of the subscribed
950 events occur.
Aurelien DARRAGONc84899c2023-02-20 18:18:59 +0100951 :returns: A :ref:`event_sub_class` object.
Aurelien DARRAGON223770d2023-03-10 15:34:35 +0100952 :see: :js:func:`Server.event_sub()`.
Aurelien DARRAGONc84899c2023-02-20 18:18:59 +0100953
954 List of available event types :
955
956 **SERVER** Family:
957
958 * **SERVER_ADD**: when a server is added
959 * **SERVER_DEL**: when a server is removed
960 * **SERVER_DOWN**: when a server state goes from UP to DOWN
961 * **SERVER_UP**: when a server state goes from DOWN to UP
Aurelien DARRAGONc99f3ad2023-04-12 15:47:16 +0200962 * **SERVER_STATE**: when a server state changes
Aurelien DARRAGON948dd3d2023-04-26 11:27:09 +0200963 * **SERVER_ADMIN**: when a server administrative state changes
Aurelien DARRAGON0bd53b22023-03-30 15:53:33 +0200964 * **SERVER_CHECK**: when a server's check status change is reported.
965 Be careful when subscribing to this type since many events might be
966 generated.
Aurelien DARRAGONc84899c2023-02-20 18:18:59 +0100967
968 .. Note::
Aurelien DARRAGONc99f3ad2023-04-12 15:47:16 +0200969 Use **SERVER** in **event_types** to subscribe to all server events types
970 at once. Note that this should only be used for testing purposes since a
971 single event source could result in multiple events types being generated.
972 (e.g.: SERVER_STATE will always be generated for each SERVER_DOWN or
973 SERVER_UP)
Aurelien DARRAGONc84899c2023-02-20 18:18:59 +0100974
975 The prototype of the Lua function used as argument is:
976
977.. code-block:: lua
978
Aurelien DARRAGON096b3832023-04-20 11:32:46 +0200979 function(event, event_data, sub, when)
Aurelien DARRAGONc84899c2023-02-20 18:18:59 +0100980..
981
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +0200982 * **event** (*string*): the event type (one of the **event_types** specified
983 when subscribing)
984 * **event_data**: specific to each event family (For **SERVER** family,
985 a :ref:`server_event_class` object)
986 * **sub**: class to manage the subscription from within the event
987 (a :ref:`event_sub_class` object)
Aurelien DARRAGON096b3832023-04-20 11:32:46 +0200988 * **when**: timestamp corresponding to the date when the event was generated.
989 It is an integer representing the number of seconds elapsed since Epoch.
990 It may be provided as optional argument to `os.date()` lua function to
991 convert it to a string according to a given format string.
Aurelien DARRAGONc84899c2023-02-20 18:18:59 +0100992
993 .. Warning::
994 The callback function will only be scheduled on the very same thread that
995 performed the subscription.
996
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +0200997 Moreover, each thread treats events sequentially. It means that if you
998 have, let's say SERVER_UP followed by a SERVER_DOWN in a short timelapse,
999 then the cb function will first be called with SERVER_UP, and once it's
1000 done handling the event, the cb function will be called again with
1001 SERVER_DOWN.
Aurelien DARRAGONc84899c2023-02-20 18:18:59 +01001002
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02001003 This is to ensure event consistency when it comes to logging / triggering
1004 logic from lua.
Aurelien DARRAGONc84899c2023-02-20 18:18:59 +01001005
1006 Your lua cb function may yield if needed, but you're pleased to process the
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02001007 event as fast as possible to prevent the event queue from growing up,
1008 depending on the event flow that is expected for the given subscription.
Aurelien DARRAGONc84899c2023-02-20 18:18:59 +01001009
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02001010 To prevent abuses, if the event queue for the current subscription goes
1011 over a certain amount of unconsumed events, the subscription will pause
1012 itself automatically for as long as it takes for your handler to catch up.
1013 This would lead to events being missed, so an error will be reported in the
1014 logs to warn you about that.
1015 This is not something you want to let happen too often, it may indicate
1016 that you subscribed to an event that is occurring too frequently or/and
1017 that your callback function is too slow to keep up the pace and you should
1018 review it.
Aurelien DARRAGONc84899c2023-02-20 18:18:59 +01001019
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02001020 If you want to do some parallel processing because your callback functions
1021 are slow: you might want to create subtasks from lua using
1022 :js:func:`core.register_task()` from within your callback function to
1023 perform the heavy job in a dedicated task and allow remaining events to be
1024 processed more quickly.
Aurelien DARRAGONc84899c2023-02-20 18:18:59 +01001025
Aurelien DARRAGON5bed48f2023-04-21 17:32:46 +02001026.. js:function:: core.disable_legacy_mailers()
1027
1028 **LEGACY**
1029
1030 **context**: body, init
1031
1032 Disable the sending of email alerts through the legacy email sending
1033 function when mailers are used in the configuration.
1034
1035 Use this when sending email alerts directly from lua.
1036
Aurelien DARRAGON717a38d2023-04-26 19:02:43 +02001037 :see: :js:func:`Proxy.get_mailers()`
1038
Thierry Fournierf61aa632016-02-19 20:56:00 +01001039.. _proxy_class:
1040
1041Proxy class
1042============
1043
1044.. js:class:: Proxy
1045
1046 This class provides a way for manipulating proxy and retrieving information
1047 like statistics.
1048
Thierry FOURNIER817e7592017-07-24 14:35:04 +02001049.. js:attribute:: Proxy.name
1050
1051 Contain the name of the proxy.
1052
Aurelien DARRAGONc4b24372023-03-02 12:00:06 +01001053 .. warning::
1054 This attribute is now deprecated and will eventually be removed.
1055 Please use :js:func:`Proxy.get_name()` function instead.
1056
Thierry Fournierb0467732022-10-07 12:07:24 +02001057.. js:function:: Proxy.get_name()
1058
1059 Returns the name of the proxy.
1060
Baptiste Assmann46c72552017-10-26 21:51:58 +02001061.. js:attribute:: Proxy.uuid
1062
1063 Contain the unique identifier of the proxy.
1064
Aurelien DARRAGONc4b24372023-03-02 12:00:06 +01001065 .. warning::
1066 This attribute is now deprecated and will eventually be removed.
1067 Please use :js:func:`Proxy.get_uuid()` function instead.
1068
Thierry Fournierb0467732022-10-07 12:07:24 +02001069.. js:function:: Proxy.get_uuid()
1070
1071 Returns the unique identifier of the proxy.
1072
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001073.. js:attribute:: Proxy.servers
1074
Patrick Hemmerc6a1d712018-05-01 21:30:41 -04001075 Contain a table with the attached servers. The table is indexed by server
1076 name, and each server entry is an object of type :ref:`server_class`.
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001077
Adis Nezirovic8878f8e2018-07-13 12:18:33 +02001078.. js:attribute:: Proxy.stktable
1079
1080 Contains a stick table object attached to the proxy.
1081
Thierry Fournierff480422016-02-25 08:36:46 +01001082.. js:attribute:: Proxy.listeners
1083
Patrick Hemmerc6a1d712018-05-01 21:30:41 -04001084 Contain a table with the attached listeners. The table is indexed by listener
1085 name, and each each listeners entry is an object of type
1086 :ref:`listener_class`.
Thierry Fournierff480422016-02-25 08:36:46 +01001087
Thierry Fournierf61aa632016-02-19 20:56:00 +01001088.. js:function:: Proxy.pause(px)
1089
1090 Pause the proxy. See the management socket documentation for more information.
1091
1092 :param class_proxy px: A :ref:`proxy_class` which indicates the manipulated
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02001093 proxy.
Thierry Fournierf61aa632016-02-19 20:56:00 +01001094
1095.. js:function:: Proxy.resume(px)
1096
1097 Resume the proxy. See the management socket documentation for more
1098 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.stop(px)
1104
1105 Stop the proxy. See the management socket documentation for more information.
1106
1107 :param class_proxy px: A :ref:`proxy_class` which indicates the manipulated
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02001108 proxy.
Thierry Fournierf61aa632016-02-19 20:56:00 +01001109
1110.. js:function:: Proxy.shut_bcksess(px)
1111
1112 Kill the session attached to a backup server. See the management socket
1113 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.get_cap(px)
1119
1120 Returns a string describing the capabilities of the proxy.
1121
1122 :param class_proxy px: A :ref:`proxy_class` which indicates the manipulated
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02001123 proxy.
Thierry Fournierf61aa632016-02-19 20:56:00 +01001124 :returns: a string "frontend", "backend", "proxy" or "ruleset".
1125
1126.. js:function:: Proxy.get_mode(px)
1127
1128 Returns a string describing the mode of the current 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 "tcp", "http", "health" or "unknown"
1133
Aurelien DARRAGONfc845532023-04-03 11:00:18 +02001134.. js:function:: Proxy.get_srv_act(px)
1135
1136 Returns the number of current active servers for the current proxy that are
1137 eligible for LB.
1138
1139 :param class_proxy px: A :ref:`proxy_class` which indicates the manipulated
1140 proxy.
1141 :returns: an integer
1142
1143.. js:function:: Proxy.get_srv_bck(px)
1144
1145 Returns the number backup servers for the current proxy that are eligible
1146 for LB.
1147
1148 :param class_proxy px: A :ref:`proxy_class` which indicates the manipulated
1149 proxy.
1150 :returns: an integer
1151
Thierry Fournierf61aa632016-02-19 20:56:00 +01001152.. js:function:: Proxy.get_stats(px)
1153
Bertrand Jacquin874a35c2018-09-10 21:26:07 +01001154 Returns a table containing the proxy statistics. The statistics returned are
Thierry Fournierf61aa632016-02-19 20:56:00 +01001155 not the same if the proxy is frontend or a backend.
1156
1157 :param class_proxy px: A :ref:`proxy_class` which indicates the manipulated
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02001158 proxy.
Patrick Hemmerc6a1d712018-05-01 21:30:41 -04001159 :returns: a key/value table containing stats
Thierry Fournierf61aa632016-02-19 20:56:00 +01001160
Aurelien DARRAGON717a38d2023-04-26 19:02:43 +02001161.. js:function:: Proxy.get_mailers(px)
1162
1163 **LEGACY**
1164
1165 Returns a table containing mailers config for the current proxy or nil
1166 if mailers are not available for the proxy.
1167
1168 :param class_proxy px: A :ref:`proxy_class` which indicates the manipulated
1169 proxy.
1170 :returns: a :ref:`proxy_mailers_class` containing proxy mailers config
1171
1172.. _proxy_mailers_class:
1173
1174ProxyMailers class
1175==================
1176
1177**LEGACY**
1178
1179.. js:class:: ProxyMailers
1180
1181 This class provides mailers config for a given proxy.
1182
1183 If sending emails directly from lua, please consider
1184 :js:func:`core.disable_legacy_mailers()` to disable the email sending from
1185 haproxy. (Or email alerts will be sent twice...)
1186
1187.. js:attribute:: ProxyMailers.track_server_health
1188
1189 Boolean set to true if the option "log-health-checks" is configured on
1190 the proxy, meaning that all server checks event should trigger email alerts.
1191
1192.. js:attribute:: ProxyMailers.log_level
1193
1194 An integer, the maximum log level that triggers email alerts. It is a number
1195 between 0 and 7 as defined by option "email-alert level".
1196
1197.. js:attribute:: ProxyMailers.mailservers
1198
1199 An array containing the list of mail servers that should receive email alerts.
1200 Each array entry is a name:desc pair where desc represents the full server
1201 address (including port) as described in haproxy's configuration file.
1202
Aurelien DARRAGON2b8f7ab2023-07-07 16:55:43 +02001203.. js:attribute:: ProxyMailers.mailservers_timeout
1204
1205 An integer representing the maximum time in milliseconds to wait for the
1206 email to be sent. See "timeout mail" directive from "mailers" section in
1207 haproxy configuration file.
1208
Aurelien DARRAGON717a38d2023-04-26 19:02:43 +02001209.. js:attribute:: ProxyMailers.smtp_hostname
1210
1211 A string containing the hostname to use for the SMTP transaction.
1212 (option "email-alert myhostname")
1213
1214.. js:attribute:: ProxyMailers.smtp_from
1215
1216 A string containing the "MAIL FROM" address to use for the SMTP transaction.
1217 (option "email-alert from")
1218
1219.. js:attribute:: ProxyMailers.smtp_to
1220
1221 A string containing the "RCPT TO" address to use for the SMTP transaction.
1222 (option "email-alert to")
1223
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001224.. _server_class:
1225
1226Server class
1227============
1228
Patrick Hemmerc6a1d712018-05-01 21:30:41 -04001229.. js:class:: Server
1230
1231 This class provides a way for manipulating servers and retrieving information.
1232
Patrick Hemmera62ae7e2018-04-29 14:23:48 -04001233.. js:attribute:: Server.name
1234
1235 Contain the name of the server.
1236
Aurelien DARRAGONc4b24372023-03-02 12:00:06 +01001237 .. warning::
1238 This attribute is now deprecated and will eventually be removed.
1239 Please use :js:func:`Server.get_name()` function instead.
1240
Thierry Fournierb0467732022-10-07 12:07:24 +02001241.. js:function:: Server.get_name(sv)
1242
1243 Returns the name of the server.
1244
Patrick Hemmera62ae7e2018-04-29 14:23:48 -04001245.. js:attribute:: Server.puid
1246
1247 Contain the proxy unique identifier of the server.
1248
Aurelien DARRAGONc4b24372023-03-02 12:00:06 +01001249 .. warning::
1250 This attribute is now deprecated and will eventually be removed.
1251 Please use :js:func:`Server.get_puid()` function instead.
1252
Thierry Fournierb0467732022-10-07 12:07:24 +02001253.. js:function:: Server.get_puid(sv)
1254
1255 Returns the proxy unique identifier of the server.
1256
Aurelien DARRAGON94ee6632023-03-10 15:11:27 +01001257.. js:function:: Server.get_rid(sv)
1258
1259 Returns the rid (revision ID) of the server.
1260 It is an unsigned integer that is set upon server creation. Value is derived
1261 from a global counter that starts at 0 and is incremented each time one or
1262 multiple server deletions are followed by a server addition (meaning that
1263 old name/id reuse could occur).
1264
1265 Combining server name/id with server rid yields a process-wide unique
1266 identifier.
1267
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001268.. js:function:: Server.is_draining(sv)
1269
Patrick Hemmerc6a1d712018-05-01 21:30:41 -04001270 Return true if the server is currently draining sticky connections.
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001271
1272 :param class_server sv: A :ref:`server_class` which indicates the manipulated
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02001273 server.
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001274 :returns: a boolean
1275
Aurelien DARRAGONc72051d2023-03-29 10:44:38 +02001276.. js:function:: Server.is_backup(sv)
1277
1278 Return true if the server is a backup server
1279
1280 :param class_server sv: A :ref:`server_class` which indicates the manipulated
1281 server.
1282 :returns: a boolean
1283
Aurelien DARRAGON7a03dee2023-03-29 10:49:30 +02001284.. js:function:: Server.is_dynamic(sv)
1285
1286 Return true if the server was instantiated at runtime (e.g.: from the cli)
1287
1288 :param class_server sv: A :ref:`server_class` which indicates the manipulated
1289 server.
1290 :returns: a boolean
1291
Aurelien DARRAGONfc759b42023-04-03 10:43:17 +02001292.. js:function:: Server.get_cur_sess(sv)
1293
1294 Return the number of currently active sessions on the server
1295
1296 :param class_server sv: A :ref:`server_class` which indicates the manipulated
1297 server.
1298 :returns: an integer
1299
1300.. js:function:: Server.get_pend_conn(sv)
1301
1302 Return the number of pending connections to the server
1303
1304 :param class_server sv: A :ref:`server_class` which indicates the manipulated
1305 server.
1306 :returns: an integer
1307
Patrick Hemmer32d539f2018-04-29 14:25:46 -04001308.. js:function:: Server.set_maxconn(sv, weight)
1309
1310 Dynamically change the maximum connections of the server. See the management
1311 socket documentation for more information about the format of the string.
1312
1313 :param class_server sv: A :ref:`server_class` which indicates the manipulated
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02001314 server.
Patrick Hemmer32d539f2018-04-29 14:25:46 -04001315 :param string maxconn: A string describing the server maximum connections.
1316
1317.. js:function:: Server.get_maxconn(sv, weight)
1318
1319 This function returns an integer representing the server maximum connections.
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 :returns: an integer.
1324
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001325.. js:function:: Server.set_weight(sv, weight)
1326
Patrick Hemmerc6a1d712018-05-01 21:30:41 -04001327 Dynamically change the weight of the server. See the management socket
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001328 documentation for more information about the format of the string.
1329
1330 :param class_server sv: A :ref:`server_class` which indicates the manipulated
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02001331 server.
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001332 :param string weight: A string describing the server weight.
1333
1334.. js:function:: Server.get_weight(sv)
1335
Patrick Hemmerc6a1d712018-05-01 21:30:41 -04001336 This function returns an integer representing the server weight.
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001337
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 :returns: an integer.
1341
Joseph C. Sible49bbf522020-05-04 22:20:32 -04001342.. js:function:: Server.set_addr(sv, addr[, port])
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001343
Patrick Hemmerc6a1d712018-05-01 21:30:41 -04001344 Dynamically change the address of the server. See the management socket
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001345 documentation for more information about the format of the string.
1346
1347 :param class_server sv: A :ref:`server_class` which indicates the manipulated
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02001348 server.
Patrick Hemmerc6a1d712018-05-01 21:30:41 -04001349 :param string addr: A string describing the server address.
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001350
1351.. js:function:: Server.get_addr(sv)
1352
Patrick Hemmerc6a1d712018-05-01 21:30:41 -04001353 Returns a string describing the address of the server.
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001354
1355 :param class_server sv: A :ref:`server_class` which indicates the manipulated
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02001356 server.
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001357 :returns: A string
1358
1359.. js:function:: Server.get_stats(sv)
1360
1361 Returns server statistics.
1362
1363 :param class_server sv: A :ref:`server_class` which indicates the manipulated
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02001364 server.
Patrick Hemmerc6a1d712018-05-01 21:30:41 -04001365 :returns: a key/value table containing stats
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001366
Aurelien DARRAGON3889efa2023-04-03 14:00:58 +02001367.. js:function:: Server.get_proxy(sv)
1368
1369 Returns the parent proxy to which the server belongs.
1370
1371 :param class_server sv: A :ref:`server_class` which indicates the manipulated
1372 server.
1373 :returns: a :ref:`proxy_class` or nil if not available
1374
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001375.. js:function:: Server.shut_sess(sv)
1376
1377 Shutdown all the sessions attached to the server. See the management socket
1378 documentation for more information about this function.
1379
1380 :param class_server sv: A :ref:`server_class` which indicates the manipulated
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02001381 server.
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001382
1383.. js:function:: Server.set_drain(sv)
1384
1385 Drain sticky sessions. See the management socket documentation for more
1386 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_maint(sv)
1392
1393 Set maintenance mode. 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_ready(sv)
1400
1401 Set normal mode. See the management socket documentation for more information
1402 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.check_enable(sv)
1408
1409 Enable health checks. See the management socket documentation for more
1410 information 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_disable(sv)
1416
1417 Disable 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_force_up(sv)
1424
1425 Force health-check up. 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_nolb(sv)
1432
1433 Force health-check nolb mode. 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_down(sv)
1440
1441 Force health-check down. 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.agent_enable(sv)
1448
1449 Enable agent check. 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_disable(sv)
1456
1457 Disable 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_force_up(sv)
1464
1465 Force agent check up. 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_down(sv)
1472
1473 Force agent check down. 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
Aurelien DARRAGON406511a2023-03-29 11:30:36 +02001479.. js:function:: Server.tracking(sv)
1480
1481 Check if the current server is tracking another server.
1482
1483 :param class_server sv: A :ref:`server_class` which indicates the manipulated
1484 server.
1485 :returns: A :ref:`server_class` which indicates the tracked server or nil if
1486 the server doesn't track another one.
1487
Aurelien DARRAGON4be36a12023-03-29 14:02:39 +02001488.. js:function:: Server.get_trackers(sv)
1489
1490 Check if the current server is being tracked by other servers.
1491
1492 :param class_server sv: A :ref:`server_class` which indicates the manipulated
1493 server.
1494 :returns: An array of :ref:`server_class` which indicates the tracking
1495 servers (might be empty)
1496
Aurelien DARRAGON223770d2023-03-10 15:34:35 +01001497.. js:function:: Server.event_sub(sv, event_types, func)
1498
1499 Register a function that will be called on specific server events.
1500 It works exactly like :js:func:`core.event_sub()` except that the subscription
1501 will be performed within the server dedicated subscription list instead of the
1502 global one.
1503 (Your callback function will only be called for server events affecting sv)
1504
1505 See :js:func:`core.event_sub()` for function usage.
1506
1507 A key advantage to using :js:func:`Server.event_sub()` over
1508 :js:func:`core.event_sub()` for servers is that :js:func:`Server.event_sub()`
1509 allows you to be notified for servers events of a single server only.
1510 It removes the needs for extra filtering in your callback function if you only
1511 care about a single server, and also prevents useless wakeups.
1512
1513 For instance, if you want to be notified for UP/DOWN events on a given set of
Ilya Shipitsinccf80122023-04-22 20:20:39 +02001514 servers, it is recommended to perform multiple per-server subscriptions since
Aurelien DARRAGON223770d2023-03-10 15:34:35 +01001515 it will be more efficient that doing a single global subscription that will
1516 filter the received events.
1517 Unless you really want to be notified for servers events of ALL servers of
1518 course, which could make sense given you setup but should be avoided if you
1519 have an important number of servers as it will add a significant load on your
1520 haproxy process in case of multiple servers state change in a short amount of
1521 time.
1522
1523 .. Note::
1524 You may also combine :js:func:`core.event_sub()` with
1525 :js:func:`Server.event_sub()`.
1526
1527 Also, don't forget that you can use :js:func:`core.register_task()` from
1528 your callback function if needed. (ie: parallel work)
1529
1530 Here is a working example combining :js:func:`core.event_sub()` with
1531 :js:func:`Server.event_sub()` and :js:func:`core.register_task()`
1532 (This only serves as a demo, this is not necessarily useful to do so)
1533
1534.. code-block:: lua
1535
1536 core.event_sub({"SERVER_ADD"}, function(event, data, sub)
1537 -- in the global event handler
1538 if data["reference"] ~= nil then
1539 print("Tracking new server: ", data["name"])
1540 data["reference"]:event_sub({"SERVER_UP", "SERVER_DOWN"}, function(event, data, sub)
1541 -- in the per-server event handler
1542 if data["reference"] ~= nil then
1543 core.register_task(function(server)
1544 -- subtask to perform some async work (e.g.: HTTP API calls, sending emails...)
1545 print("ASYNC: SERVER ", server:get_name(), " is ", event == "SERVER_UP" and "UP" or "DOWN")
1546 end, data["reference"])
1547 end
1548 end)
1549 end
1550 end)
1551
1552..
1553
1554 In this example, we will first track global server addition events.
1555 For each newly added server ("add server" on the cli), we will register a
1556 UP/DOWN server subscription.
1557 Then, the callback function will schedule the event handling in an async
1558 subtask which will receive the server reference as an argument.
1559
Thierry Fournierff480422016-02-25 08:36:46 +01001560.. _listener_class:
1561
1562Listener class
1563==============
1564
1565.. js:function:: Listener.get_stats(ls)
1566
1567 Returns server statistics.
1568
1569 :param class_listener ls: A :ref:`listener_class` which indicates the
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02001570 manipulated listener.
Patrick Hemmerc6a1d712018-05-01 21:30:41 -04001571 :returns: a key/value table containing stats
Thierry Fournierff480422016-02-25 08:36:46 +01001572
Aurelien DARRAGONc84899c2023-02-20 18:18:59 +01001573.. _event_sub_class:
1574
1575EventSub class
1576==============
1577
1578.. js:function:: EventSub.unsub()
1579
1580 End the subscription, the callback function will not be called again.
1581
1582.. _server_event_class:
1583
1584ServerEvent class
1585=================
1586
Aurelien DARRAGONc4ae8902023-04-17 17:24:48 +02001587.. js:class:: ServerEvent
1588
1589This class is provided with every **SERVER** events.
1590
1591See :js:func:`core.event_sub()` for more info.
1592
Aurelien DARRAGONc84899c2023-02-20 18:18:59 +01001593.. js:attribute:: ServerEvent.name
1594
1595 Contains the name of the server.
1596
1597.. js:attribute:: ServerEvent.puid
1598
1599 Contains the proxy-unique uid of the server
1600
1601.. js:attribute:: ServerEvent.rid
1602
1603 Contains the revision ID of the server
1604
1605.. js:attribute:: ServerEvent.proxy_name
1606
1607 Contains the name of the proxy to which the server belongs
1608
Aurelien DARRAGON55f84c72023-03-22 17:49:04 +01001609.. js:attribute:: ServerEvent.proxy_uuid
1610
1611 Contains the uuid of the proxy to which the server belongs
1612
Aurelien DARRAGONc84899c2023-02-20 18:18:59 +01001613.. js:attribute:: ServerEvent.reference
1614
1615 Reference to the live server (A :ref:`server_class`).
1616
1617 .. Warning::
1618 Not available if the server was removed in the meantime.
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02001619 (Will never be set for SERVER_DEL event since the server does not exist
1620 anymore)
Aurelien DARRAGONc84899c2023-02-20 18:18:59 +01001621
Aurelien DARRAGONc99f3ad2023-04-12 15:47:16 +02001622.. js:attribute:: ServerEvent.state
1623
1624 A :ref:`server_event_state_class`
1625
1626 .. Note::
1627 Only available for SERVER_STATE event
1628
Aurelien DARRAGON948dd3d2023-04-26 11:27:09 +02001629.. js:attribute:: ServerEvent.admin
1630
1631 A :ref:`server_event_admin_class`
1632
1633 .. Note::
1634 Only available for SERVER_ADMIN event
1635
Aurelien DARRAGON0bd53b22023-03-30 15:53:33 +02001636.. js:attribute:: ServerEvent.check
1637
1638 A :ref:`server_event_checkres_class`
1639
1640 .. Note::
1641 Only available for SERVER_CHECK event
1642
Aurelien DARRAGONc99f3ad2023-04-12 15:47:16 +02001643.. _server_event_checkres_class:
1644
1645ServerEventCheckRes class
1646=========================
1647
1648.. js:class:: ServerEventCheckRes
1649
1650This class describes the result of a server's check.
1651
1652.. js:attribute:: ServerEventCheckRes.result
1653
1654 Effective check result.
1655
1656 Check result is a string and will be set to one of the following values:
1657 - "FAILED": the check failed
1658 - "PASSED": the check succeeded
1659 - "CONDPASS": the check conditionally passed
1660
1661.. js:attribute:: ServerEventCheckRes.agent
1662
1663 Boolean set to true if the check is an agent check.
1664 Else it is a health check.
1665
1666.. js:attribute:: ServerEventCheckRes.duration
1667
1668 Check's duration in milliseconds
1669
1670.. js:attribute:: ServerEventCheckRes.reason
1671
1672 Check's status. An array containing three fields:
1673 - **short**: a string representing check status short name
1674 - **desc**: a string representing check status description
1675 - **code**: an integer, this extra information is provided for checks
1676 that went through the data analysis stage (>= layer 5)
1677
1678.. js:attribute:: ServerEventCheckRes.health
1679
1680 An array containing values about check's health (integers):
1681 - **cur**: current health counter:
1682 - 0 to (**rise** - 1) = BAD
1683 - **rise** to (**rise** + **fall** - 1) = GOOD
1684 - **rise**: server will be considered as operational after **rise**
1685 consecutive successful checks
1686 - **fall**: server will be considered as dead after **fall** consecutive
1687 unsuccessful checks
1688
1689.. _server_event_state_class:
1690
1691ServerEventState class
1692======================
1693
1694.. js:class:: ServerEventState
1695
1696This class contains additional info related to **SERVER_STATE** event.
1697
1698.. js:attribute:: ServerEventState.admin
1699
1700 Boolean set to true if the server state change is due to an administrative
1701 change. Else it is an operational change.
1702
1703.. js:attribute:: ServerEventState.check
1704
1705 A :ref:`server_event_checkres_class`, provided if the state change is
1706 due to a server check (must be an operational change).
1707
1708.. js:attribute:: ServerEventState.cause
1709
1710 Printable state change cause. Might be empty.
1711
1712.. js:attribute:: ServerEventState.new_state
1713
1714 New server state due to operational or admin change.
1715
1716 It is a string that can be any of the following values:
1717 - "STOPPED": The server is down
1718 - "STOPPING": The server is up but soft-stopping
1719 - "STARTING": The server is warming up
1720 - "RUNNING": The server is fully up
1721
1722.. js:attribute:: ServerEventState.old_state
1723
1724 Previous server state prior to the operational or admin change.
1725
1726 Can be any value described in **new_state**, but they should differ.
1727
1728.. js:attribute:: ServerEventState.requeued
1729
1730 Number of connections that were requeued due to the server state change.
1731
1732 For a server going DOWN: it is the number of pending server connections
1733 that are requeued to the backend (such connections will be redispatched
1734 to any server that is suitable according to the configured load balancing
1735 algorithm).
1736
1737 For a server doing UP: it is the number of pending connections on the
1738 backend that may be redispatched to the server according to the load
1739 balancing algorithm that is in use.
1740
Aurelien DARRAGON948dd3d2023-04-26 11:27:09 +02001741.. _server_event_admin_class:
1742
1743ServerEventAdmin class
1744======================
1745
1746.. js:class:: ServerEventAdmin
1747
1748This class contains additional info related to **SERVER_ADMIN** event.
1749
1750.. js:attribute:: ServerEventAdmin.cause
1751
1752 Printable admin state change cause. Might be empty.
1753
1754.. js:attribute:: ServerEventAdmin.new_admin
1755
1756 New server admin state due to the admin change.
1757
1758 It is an array of string containing a composition of following values:
1759 - "**MAINT**": server is in maintenance mode
1760 - "FMAINT": server is in forced maintenance mode (MAINT is also set)
1761 - "IMAINT": server is in inherited maintenance mode (MAINT is also set)
1762 - "RMAINT": server is in resolve maintenance mode (MAINT is also set)
1763 - "CMAINT": server is in config maintenance mode (MAINT is also set)
1764 - "**DRAIN**": server is in drain mode
1765 - "FDRAIN": server is in forced drain mode (DRAIN is also set)
1766 - "IDRAIN": server is in inherited drain mode (DRAIN is also set)
1767
1768.. js:attribute:: ServerEventAdmin.old_admin
1769
1770 Previous server admin state prior to the admin change.
1771
1772 Values are presented as in **new_admin**, but they should differ.
1773 (Comparing old and new helps to find out the change(s))
1774
1775.. js:attribute:: ServerEventAdmin.requeued
1776
1777 Same as :js:attr:`ServerEventState.requeued` but when the requeue is due to
1778 the server administrative state change.
1779
Aurelien DARRAGON86fb22c2023-05-03 17:03:09 +02001780.. _queue_class:
1781
1782Queue class
1783===========
1784
1785.. js:class:: Queue
1786
1787 This class provides a generic FIFO storage mechanism that may be shared
1788 between multiple lua contexts to easily pass data between them, as stock
1789 Lua doesn't provide easy methods for passing data between multiple coroutines.
1790
1791 inter-task example:
1792
1793.. code-block:: lua
1794
1795 -- script wide shared queue
1796 local queue = core.queue()
1797
1798 -- master task
1799 core.register_task(function()
1800 -- send the date every second
1801 while true do
1802 queue:push(os.date("%c", core.now().sec))
1803 core.sleep(1)
1804 end
1805 end)
1806
1807 -- worker task
1808 core.register_task(function()
1809 while true do
1810 -- print the date sent by master
1811 print(queue:pop_wait())
1812 end
1813 end)
1814..
1815
1816 Of course, queue may also be used as a local storage mechanism.
1817
1818 Use :js:func:`core.queue` to get a new Queue object.
1819
1820.. js:function:: Queue.size(queue)
1821
1822 This function returns the number of items within the Queue.
1823
1824 :param class_queue queue: A :ref:`queue_class` to the current queue
1825
1826.. js:function:: Queue.push(queue, item)
1827
1828 This function pushes the item (may be of any type) to the queue.
1829 Pushed item cannot be nil or invalid, or an error will be thrown.
1830
1831 :param class_queue queue: A :ref:`queue_class` to the current queue
1832 :returns: boolean true for success and false for error
1833
1834.. js:function:: Queue.pop(queue)
1835
1836 This function immediately tries to pop an item from the queue.
1837 It returns nil of no item is available at the time of the call.
1838
1839 :param class_queue queue: A :ref:`queue_class` to the current queue
1840 :returns: the item at the top of the stack (any type) or nil if no items
1841
1842.. js:function:: Queue.pop_wait(queue)
1843
1844 **context**: task
1845
1846 This is an alternative to pop() that may be used within task contexts.
1847
1848 The call waits for data if no item is currently available. This may be
1849 useful when used in a while loop to prevent cpu waste.
1850
1851 Note that this requires yielding, thus it is only available within contexts
1852 that support yielding (mainly task context).
1853
1854 :param class_queue queue: A :ref:`queue_class` to the current queue
1855 :returns: the item at the top of the stack (any type) or nil in case of error
1856
Thierry Fournier1de16592016-01-27 09:49:07 +01001857.. _concat_class:
1858
1859Concat class
1860============
1861
1862.. js:class:: Concat
1863
1864 This class provides a fast way for string concatenation. The way using native
1865 Lua concatenation like the code below is slow for some reasons.
1866
1867.. code-block:: lua
1868
1869 str = "string1"
1870 str = str .. ", string2"
1871 str = str .. ", string3"
1872..
1873
1874 For each concatenation, Lua:
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02001875 - allocates memory for the result,
1876 - catenates the two string copying the strings in the new memory block,
1877 - frees the old memory block containing the string which is no longer used.
1878
Thierry Fournier1de16592016-01-27 09:49:07 +01001879 This process does many memory move, allocation and free. In addition, the
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02001880 memory is not really freed, it is just marked as unused and waits for the
Thierry Fournier1de16592016-01-27 09:49:07 +01001881 garbage collector.
1882
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02001883 The Concat class provides an alternative way to concatenate strings. It uses
Thierry Fournier1de16592016-01-27 09:49:07 +01001884 the internal Lua mechanism (it does not allocate memory), but it doesn't copy
1885 the data more than once.
1886
1887 On my computer, the following loops spends 0.2s for the Concat method and
1888 18.5s for the pure Lua implementation. So, the Concat class is about 1000x
1889 faster than the embedded solution.
1890
1891.. code-block:: lua
1892
1893 for j = 1, 100 do
1894 c = core.concat()
1895 for i = 1, 20000 do
1896 c:add("#####")
1897 end
1898 end
1899..
1900
1901.. code-block:: lua
1902
1903 for j = 1, 100 do
1904 c = ""
1905 for i = 1, 20000 do
1906 c = c .. "#####"
1907 end
1908 end
1909..
1910
1911.. js:function:: Concat.add(concat, string)
1912
1913 This function adds a string to the current concatenated string.
1914
1915 :param class_concat concat: A :ref:`concat_class` which contains the currently
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02001916 built string.
Bertrand Jacquin874a35c2018-09-10 21:26:07 +01001917 :param string string: A new string to concatenate to the current built
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02001918 string.
Thierry Fournier1de16592016-01-27 09:49:07 +01001919
1920.. js:function:: Concat.dump(concat)
1921
Bertrand Jacquin874a35c2018-09-10 21:26:07 +01001922 This function returns the concatenated string.
Thierry Fournier1de16592016-01-27 09:49:07 +01001923
1924 :param class_concat concat: A :ref:`concat_class` which contains the currently
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02001925 built string.
Thierry Fournier1de16592016-01-27 09:49:07 +01001926 :returns: the concatenated string
1927
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001928.. _fetches_class:
1929
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001930Fetches class
1931=============
1932
1933.. js:class:: Fetches
1934
1935 This class contains a lot of internal HAProxy sample fetches. See the
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02001936 HAProxy "configuration.txt" documentation for more information.
1937 (chapters 7.3.2 to 7.3.6)
Thierry FOURNIER2e4893c2015-03-18 13:37:27 +01001938
Christopher Faulet1e9b1b62021-08-11 10:14:30 +02001939 .. warning::
1940 some sample fetches are not available in some context. These limitations
1941 are specified in this documentation when they're useful.
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001942
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01001943 :see: :js:attr:`TXN.f`
1944 :see: :js:attr:`TXN.sf`
Thierry FOURNIER2e4893c2015-03-18 13:37:27 +01001945
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02001946 Fetches are useful to:
Thierry FOURNIER2e4893c2015-03-18 13:37:27 +01001947
1948 * get system time,
1949 * get environment variable,
1950 * get random numbers,
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02001951 * know backend status like the number of users in queue or the number of
Thierry FOURNIER2e4893c2015-03-18 13:37:27 +01001952 connections established,
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02001953 * get client information like ip source or destination,
Thierry FOURNIER2e4893c2015-03-18 13:37:27 +01001954 * deal with stick tables,
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02001955 * fetch established SSL information,
1956 * fetch HTTP information like headers or method.
Thierry FOURNIER2e4893c2015-03-18 13:37:27 +01001957
1958.. code-block:: lua
1959
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001960 function action(txn)
1961 -- Get source IP
1962 local clientip = txn.f:src()
1963 end
Thierry FOURNIER2e4893c2015-03-18 13:37:27 +01001964..
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001965
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001966.. _converters_class:
1967
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001968Converters class
1969================
1970
1971.. js:class:: Converters
1972
1973 This class contains a lot of internal HAProxy sample converters. See the
Thierry FOURNIER2e4893c2015-03-18 13:37:27 +01001974 HAProxy documentation "configuration.txt" for more information about her
1975 usage. Its the chapter 7.3.1.
1976
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01001977 :see: :js:attr:`TXN.c`
1978 :see: :js:attr:`TXN.sc`
Thierry FOURNIER2e4893c2015-03-18 13:37:27 +01001979
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02001980 Converters provides stateful transformation. They are useful to:
Thierry FOURNIER2e4893c2015-03-18 13:37:27 +01001981
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02001982 * convert input to base64,
1983 * apply hash on input string (djb2, crc32, sdbm, wt6),
Thierry FOURNIER2e4893c2015-03-18 13:37:27 +01001984 * format date,
1985 * json escape,
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02001986 * extract preferred language comparing two lists,
Thierry FOURNIER2e4893c2015-03-18 13:37:27 +01001987 * turn to lower or upper chars,
1988 * deal with stick tables.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001989
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001990.. _channel_class:
1991
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001992Channel class
1993=============
1994
1995.. js:class:: Channel
1996
Christopher Faulet5a2c6612021-08-15 20:35:25 +02001997 **context**: action, sample-fetch, convert, filter
1998
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001999 HAProxy uses two buffers for the processing of the requests. The first one is
2000 used with the request data (from the client to the server) and the second is
2001 used for the response data (from the server to the client).
2002
2003 Each buffer contains two types of data. The first type is the incoming data
2004 waiting for a processing. The second part is the outgoing data already
2005 processed. Usually, the incoming data is processed, after it is tagged as
2006 outgoing data, and finally it is sent. The following functions provides tools
2007 for manipulating these data in a buffer.
2008
2009 The following diagram shows where the channel class function are applied.
2010
Christopher Faulet6a79fc12021-08-06 16:02:36 +02002011 .. image:: _static/channel.png
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002012
Christopher Faulet6a79fc12021-08-06 16:02:36 +02002013 .. warning::
2014 It is not possible to read from the response in request action, and it is
Boyang Li60cfe8b2022-05-10 18:11:00 +00002015 not possible to read from the request channel in response action.
Christopher Faulet09530392021-06-14 11:43:18 +02002016
Christopher Faulet6a79fc12021-08-06 16:02:36 +02002017 .. warning::
2018 It is forbidden to alter the Channels buffer from HTTP contexts. So only
2019 :js:func:`Channel.input`, :js:func:`Channel.output`,
2020 :js:func:`Channel.may_recv`, :js:func:`Channel.is_full` and
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02002021 :js:func:`Channel.is_resp` can be called from a HTTP context.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002022
Christopher Faulet5a2c6612021-08-15 20:35:25 +02002023 All the functions provided by this class are available in the
2024 **sample-fetches**, **actions** and **filters** contexts. For **filters**,
2025 incoming data (offset and length) are relative to the filter. Some functions
Boyang Li60cfe8b2022-05-10 18:11:00 +00002026 may yield, but only for **actions**. Yield is not possible for
Christopher Faulet5a2c6612021-08-15 20:35:25 +02002027 **sample-fetches**, **converters** and **filters**.
Christopher Faulet6a79fc12021-08-06 16:02:36 +02002028
2029.. js:function:: Channel.append(channel, string)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002030
Christopher Faulet6a79fc12021-08-06 16:02:36 +02002031 This function copies the string **string** at the end of incoming data of the
2032 channel buffer. The function returns the copied length on success or -1 if
2033 data cannot be copied.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002034
Christopher Faulet6a79fc12021-08-06 16:02:36 +02002035 Same that :js:func:`Channel.insert(channel, string, channel:input())`.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002036
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002037 :param class_channel channel: The manipulated Channel.
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02002038 :param string string: The data to copy at the end of incoming data.
Christopher Faulet6a79fc12021-08-06 16:02:36 +02002039 :returns: an integer containing the amount of bytes copied or -1.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002040
Christopher Faulet6a79fc12021-08-06 16:02:36 +02002041.. js:function:: Channel.data(channel [, offset [, length]])
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002042
Christopher Faulet6a79fc12021-08-06 16:02:36 +02002043 This function returns **length** bytes of incoming data from the channel
2044 buffer, starting at the offset **offset**. The data are not removed from the
2045 buffer.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002046
Christopher Faulet6a79fc12021-08-06 16:02:36 +02002047 By default, if no length is provided, all incoming data found, starting at the
2048 given offset, are returned. If **length** is set to -1, the function tries to
Christopher Faulet5a2c6612021-08-15 20:35:25 +02002049 retrieve a maximum of data and, if called by an action, it yields if
2050 necessary. It also waits for more data if the requested length exceeds the
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02002051 available amount of incoming data. Not providing an offset is the same as
Ilya Shipitsinff0f2782021-08-22 22:18:07 +05002052 setting it to 0. A positive offset is relative to the beginning of incoming
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02002053 data of the channel buffer while negative offset is relative to the end.
Christopher Faulet6a79fc12021-08-06 16:02:36 +02002054
2055 If there is no incoming data and the channel can't receive more data, a 'nil'
2056 value is returned.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002057
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002058 :param class_channel channel: The manipulated Channel.
Christopher Faulet6a79fc12021-08-06 16:02:36 +02002059 :param integer offset: *optional* The offset in incoming data to start to get
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02002060 data. 0 by default. May be negative to be relative to the end of incoming
2061 data.
Christopher Faulet6a79fc12021-08-06 16:02:36 +02002062 :param integer length: *optional* The expected length of data to retrieve. All
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02002063 incoming data by default. May be set to -1 to get a maximum of data.
Christopher Faulet6a79fc12021-08-06 16:02:36 +02002064 :returns: a string containing the data found or nil.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002065
Christopher Faulet6a79fc12021-08-06 16:02:36 +02002066.. js:function:: Channel.forward(channel, length)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002067
Christopher Faulet6a79fc12021-08-06 16:02:36 +02002068 This function forwards **length** bytes of data from the channel buffer. If
Christopher Faulet5a2c6612021-08-15 20:35:25 +02002069 the requested length exceeds the available amount of incoming data, and if
2070 called by an action, the function yields, waiting for more data to forward. It
2071 returns the amount of data forwarded.
Christopher Faulet6a79fc12021-08-06 16:02:36 +02002072
2073 :param class_channel channel: The manipulated Channel.
2074 :param integer int: The amount of data to forward.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002075
Christopher Faulet6a79fc12021-08-06 16:02:36 +02002076.. js:function:: Channel.input(channel)
2077
Christopher Faulet5a2c6612021-08-15 20:35:25 +02002078 This function returns the length of incoming data in the channel buffer. When
2079 called by a filter, this value is relative to the filter.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002080
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002081 :param class_channel channel: The manipulated Channel.
Christopher Faulet6a79fc12021-08-06 16:02:36 +02002082 :returns: an integer containing the amount of available bytes.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002083
Christopher Faulet6a79fc12021-08-06 16:02:36 +02002084.. js:function:: Channel.insert(channel, string [, offset])
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002085
Christopher Faulet6a79fc12021-08-06 16:02:36 +02002086 This function copies the string **string** at the offset **offset** in
2087 incoming data of the channel buffer. The function returns the copied length on
2088 success or -1 if data cannot be copied.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002089
Christopher Faulet6a79fc12021-08-06 16:02:36 +02002090 By default, if no offset is provided, the string is copied in front of
Ilya Shipitsinff0f2782021-08-22 22:18:07 +05002091 incoming data. A positive offset is relative to the beginning of incoming data
Christopher Faulet6a79fc12021-08-06 16:02:36 +02002092 of the channel buffer while negative offset is relative to their end.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002093
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002094 :param class_channel channel: The manipulated Channel.
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02002095 :param string string: The data to copy into incoming data.
2096 :param integer offset: *optional* The offset in incoming data where to copy
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02002097 data. 0 by default. May be negative to be relative to the end of incoming
2098 data.
Pieter Baauw4d7f7662015-11-08 16:38:08 +01002099 :returns: an integer containing the amount of bytes copied or -1.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002100
Christopher Faulet6a79fc12021-08-06 16:02:36 +02002101.. js:function:: Channel.is_full(channel)
2102
2103 This function returns true if the channel buffer is full.
2104
Christopher Faulet5a2c6612021-08-15 20:35:25 +02002105 :param class_channel channel: The manipulated Channel.
Christopher Faulet6a79fc12021-08-06 16:02:36 +02002106 :returns: a boolean
2107
2108.. js:function:: Channel.is_resp(channel)
2109
2110 This function returns true if the channel is the response one.
2111
Christopher Faulet5a2c6612021-08-15 20:35:25 +02002112 :param class_channel channel: The manipulated Channel.
Christopher Faulet6a79fc12021-08-06 16:02:36 +02002113 :returns: a boolean
2114
2115.. js:function:: Channel.line(channel [, offset [, length]])
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002116
Christopher Faulet6a79fc12021-08-06 16:02:36 +02002117 This function parses **length** bytes of incoming data of the channel buffer,
2118 starting at offset **offset**, and returns the first line found, including the
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02002119 '\\n'. The data are not removed from the buffer. If no line is found, all
2120 data are returned.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002121
Christopher Faulet6a79fc12021-08-06 16:02:36 +02002122 By default, if no length is provided, all incoming data, starting at the given
2123 offset, are evaluated. If **length** is set to -1, the function tries to
Christopher Faulet5a2c6612021-08-15 20:35:25 +02002124 retrieve a maximum of data and, if called by an action, yields if
2125 necessary. It also waits for more data if the requested length exceeds the
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02002126 available amount of incoming data. Not providing an offset is the same as
Ilya Shipitsinff0f2782021-08-22 22:18:07 +05002127 setting it to 0. A positive offset is relative to the beginning of incoming
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02002128 data of the channel buffer while negative offset is relative to the end.
Christopher Faulet6a79fc12021-08-06 16:02:36 +02002129
2130 If there is no incoming data and the channel can't receive more data, a 'nil'
2131 value is returned.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002132
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002133 :param class_channel channel: The manipulated Channel.
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02002134 :param integer offset: *optional* The offset in incoming data to start to
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02002135 parse data. 0 by default. May be negative to be relative to the end of
2136 incoming data.
Christopher Faulet6a79fc12021-08-06 16:02:36 +02002137 :param integer length: *optional* The length of data to parse. All incoming
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02002138 data by default. May be set to -1 to get a maximum of data.
Christopher Faulet6a79fc12021-08-06 16:02:36 +02002139 :returns: a string containing the line found or nil.
2140
2141.. js:function:: Channel.may_recv(channel)
2142
2143 This function returns true if the channel may still receive data.
2144
Christopher Faulet5a2c6612021-08-15 20:35:25 +02002145 :param class_channel channel: The manipulated Channel.
Christopher Faulet6a79fc12021-08-06 16:02:36 +02002146 :returns: a boolean
2147
2148.. js:function:: Channel.output(channel)
2149
Christopher Faulet5a2c6612021-08-15 20:35:25 +02002150 This function returns the length of outgoing data of the channel buffer. When
2151 called by a filter, this value is relative to the filter.
Christopher Faulet6a79fc12021-08-06 16:02:36 +02002152
2153 :param class_channel channel: The manipulated Channel.
2154 :returns: an integer containing the amount of available bytes.
2155
2156.. js:function:: Channel.prepend(channel, string)
2157
2158 This function copies the string **string** in front of incoming data of the
2159 channel buffer. The function returns the copied length on success or -1 if
2160 data cannot be copied.
2161
2162 Same that :js:func:`Channel.insert(channel, string, 0)`.
2163
2164 :param class_channel channel: The manipulated Channel.
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02002165 :param string string: The data to copy in front of incoming data.
Pieter Baauw4d7f7662015-11-08 16:38:08 +01002166 :returns: an integer containing the amount of bytes copied or -1.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002167
Christopher Faulet6a79fc12021-08-06 16:02:36 +02002168.. js:function:: Channel.remove(channel [, offset [, length]])
2169
2170 This function removes **length** bytes of incoming data of the channel buffer,
2171 starting at offset **offset**. This function returns number of bytes removed
2172 on success.
2173
2174 By default, if no length is provided, all incoming data, starting at the given
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02002175 offset, are removed. Not providing an offset is the same as setting it
Ilya Shipitsinff0f2782021-08-22 22:18:07 +05002176 to 0. A positive offset is relative to the beginning of incoming data of the
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02002177 channel buffer while negative offset is relative to the end.
Christopher Faulet6a79fc12021-08-06 16:02:36 +02002178
2179 :param class_channel channel: The manipulated Channel.
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02002180 :param integer offset: *optional* The offset in incoming data where to start
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02002181 to remove data. 0 by default. May be negative to be relative to the end of
2182 incoming data.
Christopher Faulet6a79fc12021-08-06 16:02:36 +02002183 :param integer length: *optional* The length of data to remove. All incoming
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02002184 data by default.
Christopher Faulet6a79fc12021-08-06 16:02:36 +02002185 :returns: an integer containing the amount of bytes removed.
2186
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002187.. js:function:: Channel.send(channel, string)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002188
Christopher Faulet5a2c6612021-08-15 20:35:25 +02002189 This function requires immediate send of the string **string**. It means the
Ilya Shipitsinff0f2782021-08-22 22:18:07 +05002190 string is copied at the beginning of incoming data of the channel buffer and
Christopher Faulet5a2c6612021-08-15 20:35:25 +02002191 immediately forwarded. Unless if the connection is close, and if called by an
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02002192 action, this function yields to copy and forward all the string.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002193
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002194 :param class_channel channel: The manipulated Channel.
Christopher Faulet6a79fc12021-08-06 16:02:36 +02002195 :param string string: The data to send.
Pieter Baauw4d7f7662015-11-08 16:38:08 +01002196 :returns: an integer containing the amount of bytes copied or -1.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002197
Christopher Faulet6a79fc12021-08-06 16:02:36 +02002198.. js:function:: Channel.set(channel, string [, offset [, length]])
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002199
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02002200 This function replaces **length** bytes of incoming data of the channel
2201 buffer, starting at offset **offset**, by the string **string**. The function
2202 returns the copied length on success or -1 if data cannot be copied.
Christopher Faulet6a79fc12021-08-06 16:02:36 +02002203
2204 By default, if no length is provided, all incoming data, starting at the given
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02002205 offset, are replaced. Not providing an offset is the same as setting it
Ilya Shipitsinff0f2782021-08-22 22:18:07 +05002206 to 0. A positive offset is relative to the beginning of incoming data of the
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02002207 channel buffer while negative offset is relative to the end.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002208
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002209 :param class_channel channel: The manipulated Channel.
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02002210 :param string string: The data to copy into incoming data.
2211 :param integer offset: *optional* The offset in incoming data where to start
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02002212 the data replacement. 0 by default. May be negative to be relative to the
2213 end of incoming data.
Christopher Faulet6a79fc12021-08-06 16:02:36 +02002214 :param integer length: *optional* The length of data to replace. All incoming
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02002215 data by default.
Christopher Faulet6a79fc12021-08-06 16:02:36 +02002216 :returns: an integer containing the amount of bytes copied or -1.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002217
Christopher Faulet6a79fc12021-08-06 16:02:36 +02002218.. js:function:: Channel.dup(channel)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002219
Christopher Faulet6a79fc12021-08-06 16:02:36 +02002220 **DEPRECATED**
2221
2222 This function returns all incoming data found in the channel buffer. The data
Boyang Li60cfe8b2022-05-10 18:11:00 +00002223 are not removed from the buffer and can be reprocessed later.
Christopher Faulet6a79fc12021-08-06 16:02:36 +02002224
2225 If there is no incoming data and the channel can't receive more data, a 'nil'
2226 value is returned.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002227
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002228 :param class_channel channel: The manipulated Channel.
Christopher Faulet6a79fc12021-08-06 16:02:36 +02002229 :returns: a string containing all data found or nil.
2230
2231 .. warning::
2232 This function is deprecated. :js:func:`Channel.data()` must be used
2233 instead.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002234
Christopher Faulet6a79fc12021-08-06 16:02:36 +02002235.. js:function:: Channel.get(channel)
2236
2237 **DEPRECATED**
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002238
Christopher Faulet6a79fc12021-08-06 16:02:36 +02002239 This function returns all incoming data found in the channel buffer and remove
2240 them from the buffer.
2241
2242 If there is no incoming data and the channel can't receive more data, a 'nil'
2243 value is returned.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002244
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002245 :param class_channel channel: The manipulated Channel.
Christopher Faulet6a79fc12021-08-06 16:02:36 +02002246 :returns: a string containing all the data found or nil.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002247
Christopher Faulet6a79fc12021-08-06 16:02:36 +02002248 .. warning::
2249 This function is deprecated. :js:func:`Channel.data()` must be used to
2250 retrieve data followed by a call to :js:func:`Channel:remove()` to remove
2251 data.
Thierry FOURNIER / OZON.IO65192f32016-11-07 15:28:40 +01002252
Christopher Faulet6a79fc12021-08-06 16:02:36 +02002253 .. code-block:: lua
Thierry FOURNIER / OZON.IO65192f32016-11-07 15:28:40 +01002254
Christopher Faulet6a79fc12021-08-06 16:02:36 +02002255 local data = chn:data()
2256 chn:remove(0, data:len())
2257
2258 ..
2259
2260.. js:function:: Channel.getline(channel)
2261
2262 **DEPRECATED**
2263
2264 This function returns the first line found in incoming data of the channel
Christopher Faulet5a2c6612021-08-15 20:35:25 +02002265 buffer, including the '\\n'. The returned data are removed from the buffer. If
2266 no line is found, and if called by an action, this function yields to wait for
2267 more data, except if the channel can't receive more data. In this case all
2268 data are returned.
Christopher Faulet6a79fc12021-08-06 16:02:36 +02002269
2270 If there is no incoming data and the channel can't receive more data, a 'nil'
2271 value is returned.
2272
2273 :param class_channel channel: The manipulated Channel.
2274 :returns: a string containing the line found or nil.
2275
2276 .. warning::
Boyang Li60cfe8b2022-05-10 18:11:00 +00002277 This function is deprecated. :js:func:`Channel.line()` must be used to
Christopher Faulet6a79fc12021-08-06 16:02:36 +02002278 retrieve a line followed by a call to :js:func:`Channel:remove()` to remove
2279 data.
2280
2281 .. code-block:: lua
2282
2283 local line = chn:line(0, -1)
2284 chn:remove(0, line:len())
2285
2286 ..
2287
2288.. js:function:: Channel.get_in_len(channel)
2289
Boyang Li60cfe8b2022-05-10 18:11:00 +00002290 **DEPRECATED**
Christopher Faulet6a79fc12021-08-06 16:02:36 +02002291
Christopher Faulet5a2c6612021-08-15 20:35:25 +02002292 This function returns the length of the input part of the buffer. When called
2293 by a filter, this value is relative to the filter.
Christopher Faulet6a79fc12021-08-06 16:02:36 +02002294
2295 :param class_channel channel: The manipulated Channel.
2296 :returns: an integer containing the amount of available bytes.
2297
2298 .. warning::
2299 This function is deprecated. :js:func:`Channel.input()` must be used
2300 instead.
2301
2302.. js:function:: Channel.get_out_len(channel)
2303
Boyang Li60cfe8b2022-05-10 18:11:00 +00002304 **DEPRECATED**
Christopher Faulet6a79fc12021-08-06 16:02:36 +02002305
Christopher Faulet5a2c6612021-08-15 20:35:25 +02002306 This function returns the length of the output part of the buffer. When called
2307 by a filter, this value is relative to the filter.
Christopher Faulet6a79fc12021-08-06 16:02:36 +02002308
2309 :param class_channel channel: The manipulated Channel.
2310 :returns: an integer containing the amount of available bytes.
2311
2312 .. warning::
2313 This function is deprecated. :js:func:`Channel.output()` must be used
2314 instead.
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002315
2316.. _http_class:
Thierry FOURNIER08504f42015-03-16 14:17:08 +01002317
2318HTTP class
2319==========
2320
2321.. js:class:: HTTP
2322
2323 This class contain all the HTTP manipulation functions.
2324
Pieter Baauw386a1272015-08-16 15:26:24 +02002325.. js:function:: HTTP.req_get_headers(http)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01002326
Patrick Hemmerc6a1d712018-05-01 21:30:41 -04002327 Returns a table containing all the request headers.
Thierry FOURNIER08504f42015-03-16 14:17:08 +01002328
2329 :param class_http http: The related http object.
Patrick Hemmerc6a1d712018-05-01 21:30:41 -04002330 :returns: table of headers.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01002331 :see: :js:func:`HTTP.res_get_headers`
Thierry FOURNIER08504f42015-03-16 14:17:08 +01002332
Patrick Hemmerc6a1d712018-05-01 21:30:41 -04002333 This is the form of the returned table:
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002334
2335.. code-block:: lua
2336
2337 HTTP:req_get_headers()['<header-name>'][<header-index>] = "<header-value>"
2338
2339 local hdr = HTTP:req_get_headers()
2340 hdr["host"][0] = "www.test.com"
2341 hdr["accept"][0] = "audio/basic q=1"
2342 hdr["accept"][1] = "audio/*, q=0.2"
2343 hdr["accept"][2] = "*/*, q=0.1"
2344..
2345
Pieter Baauw386a1272015-08-16 15:26:24 +02002346.. js:function:: HTTP.res_get_headers(http)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01002347
Patrick Hemmerc6a1d712018-05-01 21:30:41 -04002348 Returns a table containing all the response headers.
Thierry FOURNIER08504f42015-03-16 14:17:08 +01002349
2350 :param class_http http: The related http object.
Patrick Hemmerc6a1d712018-05-01 21:30:41 -04002351 :returns: table of headers.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01002352 :see: :js:func:`HTTP.req_get_headers`
Thierry FOURNIER08504f42015-03-16 14:17:08 +01002353
Patrick Hemmerc6a1d712018-05-01 21:30:41 -04002354 This is the form of the returned table:
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002355
2356.. code-block:: lua
2357
2358 HTTP:res_get_headers()['<header-name>'][<header-index>] = "<header-value>"
2359
2360 local hdr = HTTP:req_get_headers()
2361 hdr["host"][0] = "www.test.com"
2362 hdr["accept"][0] = "audio/basic q=1"
2363 hdr["accept"][1] = "audio/*, q=0.2"
2364 hdr["accept"][2] = "*.*, q=0.1"
2365..
2366
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002367.. js:function:: HTTP.req_add_header(http, name, value)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01002368
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02002369 Appends a HTTP header field in the request whose name is
Thierry FOURNIER08504f42015-03-16 14:17:08 +01002370 specified in "name" and whose value is defined in "value".
2371
2372 :param class_http http: The related http object.
2373 :param string name: The header name.
2374 :param string value: The header value.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01002375 :see: :js:func:`HTTP.res_add_header`
Thierry FOURNIER08504f42015-03-16 14:17:08 +01002376
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002377.. js:function:: HTTP.res_add_header(http, name, value)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01002378
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02002379 Appends a HTTP header field in the response whose name is
Thierry FOURNIER08504f42015-03-16 14:17:08 +01002380 specified in "name" and whose value is defined in "value".
2381
2382 :param class_http http: The related http object.
2383 :param string name: The header name.
2384 :param string value: The header value.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01002385 :see: :js:func:`HTTP.req_add_header`
Thierry FOURNIER08504f42015-03-16 14:17:08 +01002386
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002387.. js:function:: HTTP.req_del_header(http, name)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01002388
2389 Removes all HTTP header fields in the request whose name is
2390 specified in "name".
2391
2392 :param class_http http: The related http object.
2393 :param string name: The header name.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01002394 :see: :js:func:`HTTP.res_del_header`
Thierry FOURNIER08504f42015-03-16 14:17:08 +01002395
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002396.. js:function:: HTTP.res_del_header(http, name)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01002397
2398 Removes all HTTP header fields in the response whose name is
2399 specified in "name".
2400
2401 :param class_http http: The related http object.
2402 :param string name: The header name.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01002403 :see: :js:func:`HTTP.req_del_header`
Thierry FOURNIER08504f42015-03-16 14:17:08 +01002404
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002405.. js:function:: HTTP.req_set_header(http, name, value)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01002406
Bertrand Jacquin874a35c2018-09-10 21:26:07 +01002407 This variable replace all occurrence of all header "name", by only
Thierry FOURNIER08504f42015-03-16 14:17:08 +01002408 one containing the "value".
2409
2410 :param class_http http: The related http object.
2411 :param string name: The header name.
2412 :param string value: The header value.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01002413 :see: :js:func:`HTTP.res_set_header`
Thierry FOURNIER08504f42015-03-16 14:17:08 +01002414
Bertrand Jacquin874a35c2018-09-10 21:26:07 +01002415 This function does the same work as the following code:
Thierry FOURNIER08504f42015-03-16 14:17:08 +01002416
2417.. code-block:: lua
2418
2419 function fcn(txn)
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002420 TXN.http:req_del_header("header")
2421 TXN.http:req_add_header("header", "value")
Thierry FOURNIER08504f42015-03-16 14:17:08 +01002422 end
2423..
2424
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002425.. js:function:: HTTP.res_set_header(http, name, value)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01002426
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02002427 This function replaces all occurrence of all header "name", by only
Thierry FOURNIER08504f42015-03-16 14:17:08 +01002428 one containing the "value".
2429
2430 :param class_http http: The related http object.
2431 :param string name: The header name.
2432 :param string value: The header value.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01002433 :see: :js:func:`HTTP.req_rep_header()`
Thierry FOURNIER08504f42015-03-16 14:17:08 +01002434
Pieter Baauw386a1272015-08-16 15:26:24 +02002435.. js:function:: HTTP.req_rep_header(http, name, regex, replace)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01002436
2437 Matches the regular expression in all occurrences of header field "name"
2438 according to "regex", and replaces them with the "replace" argument. The
2439 replacement value can contain back references like \1, \2, ... This
2440 function works with the request.
2441
2442 :param class_http http: The related http object.
2443 :param string name: The header name.
2444 :param string regex: The match regular expression.
2445 :param string replace: The replacement value.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01002446 :see: :js:func:`HTTP.res_rep_header()`
Thierry FOURNIER08504f42015-03-16 14:17:08 +01002447
Pieter Baauw386a1272015-08-16 15:26:24 +02002448.. js:function:: HTTP.res_rep_header(http, name, regex, string)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01002449
2450 Matches the regular expression in all occurrences of header field "name"
2451 according to "regex", and replaces them with the "replace" argument. The
2452 replacement value can contain back references like \1, \2, ... This
2453 function works with the request.
2454
2455 :param class_http http: The related http object.
2456 :param string name: The header name.
2457 :param string regex: The match regular expression.
2458 :param string replace: The replacement value.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01002459 :see: :js:func:`HTTP.req_rep_header()`
Thierry FOURNIER08504f42015-03-16 14:17:08 +01002460
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002461.. js:function:: HTTP.req_set_method(http, method)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01002462
2463 Rewrites the request method with the parameter "method".
2464
2465 :param class_http http: The related http object.
2466 :param string method: The new method.
2467
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002468.. js:function:: HTTP.req_set_path(http, path)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01002469
2470 Rewrites the request path with the "path" parameter.
2471
2472 :param class_http http: The related http object.
2473 :param string path: The new path.
2474
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002475.. js:function:: HTTP.req_set_query(http, query)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01002476
2477 Rewrites the request's query string which appears after the first question
2478 mark ("?") with the parameter "query".
2479
2480 :param class_http http: The related http object.
2481 :param string query: The new query.
2482
Thierry FOURNIER0d79cf62015-08-26 14:20:58 +02002483.. js:function:: HTTP.req_set_uri(http, uri)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01002484
2485 Rewrites the request URI with the parameter "uri".
2486
2487 :param class_http http: The related http object.
2488 :param string uri: The new uri.
2489
Robin H. Johnson52f5db22017-01-01 13:10:52 -08002490.. js:function:: HTTP.res_set_status(http, status [, reason])
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +02002491
Robin H. Johnson52f5db22017-01-01 13:10:52 -08002492 Rewrites the response status code with the parameter "code".
2493
2494 If no custom reason is provided, it will be generated from the status.
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +02002495
2496 :param class_http http: The related http object.
2497 :param integer status: The new response status code.
Robin H. Johnson52f5db22017-01-01 13:10:52 -08002498 :param string reason: The new response reason (optional).
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +02002499
William Lallemand00a15022021-11-19 16:02:44 +01002500.. _httpclient_class:
2501
2502HTTPClient class
2503================
2504
2505.. js:class:: HTTPClient
2506
2507 The httpclient class allows issue of outbound HTTP requests through a simple
2508 API without the knowledge of HAProxy internals.
2509
2510.. js:function:: HTTPClient.get(httpclient, request)
2511.. js:function:: HTTPClient.head(httpclient, request)
2512.. js:function:: HTTPClient.put(httpclient, request)
2513.. js:function:: HTTPClient.post(httpclient, request)
2514.. js:function:: HTTPClient.delete(httpclient, request)
2515
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02002516 Send a HTTP request and wait for a response. GET, HEAD PUT, POST and DELETE
2517 methods can be used.
2518 The HTTPClient will send asynchronously the data and is able to send and
2519 receive more than HAProxy bufsize.
William Lallemand00a15022021-11-19 16:02:44 +01002520
William Lallemanda9256192022-10-21 11:48:24 +02002521 The HTTPClient interface is not able to decompress responses, it is not
2522 recommended to send an Accept-Encoding in the request so the response is
2523 received uncompressed.
William Lallemand00a15022021-11-19 16:02:44 +01002524
2525 :param class httpclient: Is the manipulated HTTPClient.
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02002526 :param table request: Is a table containing the parameters of the request
2527 that will be send.
2528 :param string request.url: Is a mandatory parameter for the request that
2529 contains the URL.
2530 :param string request.body: Is an optional parameter for the request that
2531 contains the body to send.
2532 :param table request.headers: Is an optional parameter for the request that
2533 contains the headers to send.
2534 :param string request.dst: Is an optional parameter for the destination in
2535 haproxy address format.
2536 :param integer request.timeout: Optional timeout parameter, set a
2537 "timeout server" on the connections.
William Lallemand00a15022021-11-19 16:02:44 +01002538 :returns: Lua table containing the response
2539
2540
2541.. code-block:: lua
2542
2543 local httpclient = core.httpclient()
William Lallemand4f4f2b72022-02-17 20:00:23 +01002544 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 +01002545
2546..
2547
2548.. code-block:: lua
2549
2550 response = {
2551 status = 400,
2552 reason = "Bad request",
2553 headers = {
2554 ["content-type"] = { "text/html" },
2555 ["cache-control"] = { "no-cache", "no-store" },
2556 },
William Lallemand4f4f2b72022-02-17 20:00:23 +01002557 body = "<html><body><h1>invalid request<h1></body></html>",
William Lallemand00a15022021-11-19 16:02:44 +01002558 }
2559..
2560
2561
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002562.. _txn_class:
2563
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002564TXN class
2565=========
2566
2567.. js:class:: TXN
2568
2569 The txn class contain all the functions relative to the http or tcp
2570 transaction (Note than a tcp stream is the same than a tcp transaction, but
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02002571 a HTTP transaction is not the same than a tcp stream).
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002572
2573 The usage of this class permits to retrieve data from the requests, alter it
2574 and forward it.
2575
2576 All the functions provided by this class are available in the context
Christopher Faulet5a2c6612021-08-15 20:35:25 +02002577 **sample-fetches**, **actions** and **filters**.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002578
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002579.. js:attribute:: TXN.c
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002580
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002581 :returns: An :ref:`converters_class`.
2582
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002583 This attribute contains a Converters class object.
2584
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002585.. js:attribute:: TXN.sc
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002586
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002587 :returns: An :ref:`converters_class`.
2588
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002589 This attribute contains a Converters class object. The functions of
2590 this object returns always a string.
2591
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002592.. js:attribute:: TXN.f
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002593
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002594 :returns: An :ref:`fetches_class`.
2595
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002596 This attribute contains a Fetches class object.
2597
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002598.. js:attribute:: TXN.sf
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002599
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002600 :returns: An :ref:`fetches_class`.
2601
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002602 This attribute contains a Fetches class object. The functions of
2603 this object returns always a string.
2604
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002605.. js:attribute:: TXN.req
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002606
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002607 :returns: An :ref:`channel_class`.
2608
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002609 This attribute contains a channel class object for the request buffer.
2610
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002611.. js:attribute:: TXN.res
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002612
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002613 :returns: An :ref:`channel_class`.
2614
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002615 This attribute contains a channel class object for the response buffer.
2616
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002617.. js:attribute:: TXN.http
Thierry FOURNIER08504f42015-03-16 14:17:08 +01002618
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002619 :returns: An :ref:`http_class`.
2620
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02002621 This attribute contains a HTTP class object. It is available only if the
Thierry FOURNIER08504f42015-03-16 14:17:08 +01002622 proxy has the "mode http" enabled.
2623
Christopher Faulet5a2c6612021-08-15 20:35:25 +02002624.. js:attribute:: TXN.http_req
2625
2626 :returns: An :ref:`httpmessage_class`.
2627
2628 This attribute contains the request HTTPMessage class object. It is available
2629 only if the proxy has the "mode http" enabled and only in the **filters**
2630 context.
2631
2632.. js:attribute:: TXN.http_res
2633
2634 :returns: An :ref:`httpmessage_class`.
2635
2636 This attribute contains the response HTTPMessage class object. It is available
2637 only if the proxy has the "mode http" enabled and only in the **filters**
2638 context.
2639
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01002640.. js:function:: TXN.log(TXN, loglevel, msg)
2641
2642 This function sends a log. The log is sent, according with the HAProxy
Tristan2632d042023-10-23 13:07:39 +01002643 configuration file, to the loggers relevant to the current context and
2644 to stderr if it is allowed.
2645
2646 The exact behaviour depends on tune.lua.log.loggers and tune.lua.log.stderr.
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01002647
2648 :param class_txn txn: The class txn object containing the data.
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02002649 :param integer loglevel: Is the log level associated with the message. It is
2650 a number between 0 and 7.
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01002651 :param string msg: The log content.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01002652 :see: :js:attr:`core.emerg`, :js:attr:`core.alert`, :js:attr:`core.crit`,
2653 :js:attr:`core.err`, :js:attr:`core.warning`, :js:attr:`core.notice`,
2654 :js:attr:`core.info`, :js:attr:`core.debug` (log level definitions)
2655 :see: :js:func:`TXN.deflog`
2656 :see: :js:func:`TXN.Debug`
2657 :see: :js:func:`TXN.Info`
2658 :see: :js:func:`TXN.Warning`
2659 :see: :js:func:`TXN.Alert`
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01002660
2661.. js:function:: TXN.deflog(TXN, msg)
2662
Bertrand Jacquin874a35c2018-09-10 21:26:07 +01002663 Sends a log line with the default loglevel for the proxy associated with the
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01002664 transaction.
2665
2666 :param class_txn txn: The class txn object containing the data.
2667 :param string msg: The log content.
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02002668 :see: :js:func:`TXN.log`
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01002669
2670.. js:function:: TXN.Debug(txn, msg)
2671
2672 :param class_txn txn: The class txn object containing the data.
2673 :param string msg: The log content.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01002674 :see: :js:func:`TXN.log`
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01002675
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02002676 Does the same job as:
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01002677
2678.. code-block:: lua
2679
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002680 function Debug(txn, msg)
2681 TXN.log(txn, core.debug, msg)
2682 end
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01002683..
2684
2685.. js:function:: TXN.Info(txn, msg)
2686
2687 :param class_txn txn: The class txn object containing the data.
2688 :param string msg: The log content.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01002689 :see: :js:func:`TXN.log`
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01002690
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02002691 Does the same job as:
2692
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01002693.. code-block:: lua
2694
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02002695 function Info(txn, msg)
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002696 TXN.log(txn, core.info, msg)
2697 end
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01002698..
2699
2700.. js:function:: TXN.Warning(txn, msg)
2701
2702 :param class_txn txn: The class txn object containing the data.
2703 :param string msg: The log content.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01002704 :see: :js:func:`TXN.log`
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01002705
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02002706 Does the same job as:
2707
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01002708.. code-block:: lua
2709
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02002710 function Warning(txn, msg)
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002711 TXN.log(txn, core.warning, msg)
2712 end
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01002713..
2714
2715.. js:function:: TXN.Alert(txn, msg)
2716
2717 :param class_txn txn: The class txn object containing the data.
2718 :param string msg: The log content.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01002719 :see: :js:func:`TXN.log`
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01002720
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02002721 Does the same job as:
2722
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01002723.. code-block:: lua
2724
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02002725 function Alert(txn, msg)
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002726 TXN.log(txn, core.alert, msg)
2727 end
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01002728..
2729
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002730.. js:function:: TXN.get_priv(txn)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002731
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002732 Return Lua data stored in the current transaction (with the `TXN.set_priv()`)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002733 function. If no data are stored, it returns a nil value.
2734
2735 :param class_txn txn: The class txn object containing the data.
Bertrand Jacquin874a35c2018-09-10 21:26:07 +01002736 :returns: the opaque data previously stored, or nil if nothing is
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02002737 available.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002738
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002739.. js:function:: TXN.set_priv(txn, data)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002740
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02002741 Store any data in the current HAProxy transaction. This action replaces the
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002742 old stored data.
2743
2744 :param class_txn txn: The class txn object containing the data.
2745 :param opaque data: The data which is stored in the transaction.
2746
Tim Duesterhus4e172c92020-05-19 13:49:42 +02002747.. js:function:: TXN.set_var(TXN, var, value[, ifexist])
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +02002748
David Carlier61fdf8b2015-10-02 11:59:38 +01002749 Converts a Lua type in a HAProxy type and store it in a variable <var>.
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +02002750
2751 :param class_txn txn: The class txn object containing the data.
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02002752 :param string var: The variable name according with the HAProxy variable
2753 syntax.
2754 :param type value: The value associated to the variable. The type can be
2755 string or integer.
2756 :param boolean ifexist: If this parameter is set to true the variable will
2757 only be set if it was defined elsewhere (i.e. used within the configuration).
2758 For global variables (using the "proc" scope), they will only be updated and
2759 never created. It is highly recommended to always set this to true.
Christopher Faulet85d79c92016-11-09 16:54:56 +01002760
2761.. js:function:: TXN.unset_var(TXN, var)
2762
2763 Unset the variable <var>.
2764
2765 :param class_txn txn: The class txn object containing the data.
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02002766 :param string var: The variable name according with the HAProxy variable
2767 syntax.
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +02002768
2769.. js:function:: TXN.get_var(TXN, var)
2770
2771 Returns data stored in the variable <var> converter in Lua type.
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
Christopher Faulet700d9e82020-01-31 12:21:52 +01002777.. js:function:: TXN.reply([reply])
2778
2779 Return a new reply object
2780
2781 :param table reply: A table containing info to initialize the reply fields.
2782 :returns: A :ref:`reply_class` object.
2783
2784 The table used to initialized the reply object may contain following entries :
2785
2786 * status : The reply status code. the code 200 is used by default.
2787 * reason : The reply reason. The reason corresponding to the status code is
2788 used by default.
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02002789 * headers : A list of headers, indexed by header name. Empty by default. For
Christopher Faulet700d9e82020-01-31 12:21:52 +01002790 a given name, multiple values are possible, stored in an ordered list.
2791 * body : The reply body, empty by default.
2792
2793.. code-block:: lua
2794
2795 local reply = txn:reply{
2796 status = 400,
2797 reason = "Bad request",
2798 headers = {
2799 ["content-type"] = { "text/html" },
2800 ["cache-control"] = {"no-cache", "no-store" }
2801 },
2802 body = "<html><body><h1>invalid request<h1></body></html>"
2803 }
2804..
2805 :see: :js:class:`Reply`
2806
2807.. js:function:: TXN.done(txn[, reply])
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002808
Willy Tarreaubc183a62015-08-28 10:39:11 +02002809 This function terminates processing of the transaction and the associated
Christopher Faulet700d9e82020-01-31 12:21:52 +01002810 session and optionally reply to the client for HTTP sessions.
2811
2812 :param class_txn txn: The class txn object containing the data.
2813 :param class_reply reply: The class reply object to return to the client.
2814
2815 This functions can be used when a critical error is detected or to terminate
Willy Tarreaubc183a62015-08-28 10:39:11 +02002816 processing after some data have been returned to the client (eg: a redirect).
Christopher Faulet700d9e82020-01-31 12:21:52 +01002817 To do so, a reply may be provided. This object is optional and may contain a
2818 status code, a reason, a header list and a body. All these fields are
Christopher Faulet7855b192021-11-09 18:39:51 +01002819 optional. When not provided, the default values are used. By default, with an
2820 empty reply object, an empty HTTP 200 response is returned to the client. If
2821 no reply object is provided, the transaction is terminated without any
2822 reply. If a reply object is provided, it must not exceed the buffer size once
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02002823 converted into the internal HTTP representation. Because for now there is no
Christopher Faulet7855b192021-11-09 18:39:51 +01002824 easy way to be sure it fits, it is probably better to keep it reasonably
2825 small.
Christopher Faulet700d9e82020-01-31 12:21:52 +01002826
2827 The reply object may be fully created in lua or the class Reply may be used to
2828 create it.
2829
2830.. code-block:: lua
2831
2832 local reply = txn:reply()
2833 reply:set_status(400, "Bad request")
2834 reply:add_header("content-type", "text/html")
2835 reply:add_header("cache-control", "no-cache")
2836 reply:add_header("cache-control", "no-store")
2837 reply:set_body("<html><body><h1>invalid request<h1></body></html>")
2838 txn:done(reply)
2839..
2840
2841.. code-block:: lua
2842
2843 txn:done{
2844 status = 400,
2845 reason = "Bad request",
2846 headers = {
2847 ["content-type"] = { "text/html" },
2848 ["cache-control"] = { "no-cache", "no-store" },
2849 },
2850 body = "<html><body><h1>invalid request<h1></body></html>"
2851 }
2852..
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002853
Christopher Faulet1e9b1b62021-08-11 10:14:30 +02002854 .. warning::
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02002855 It does not make sense to call this function from sample-fetches. In this
2856 case the behavior is the same than core.done(): it finishes the Lua
Christopher Faulet1e9b1b62021-08-11 10:14:30 +02002857 execution. The transaction is really aborted only from an action registered
2858 function.
Thierry FOURNIERab00df62016-07-14 11:42:37 +02002859
Christopher Faulet700d9e82020-01-31 12:21:52 +01002860 :see: :js:func:`TXN.reply`, :js:class:`Reply`
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002861
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002862.. js:function:: TXN.set_loglevel(txn, loglevel)
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01002863
2864 Is used to change the log level of the current request. The "loglevel" must
2865 be an integer between 0 and 7.
2866
2867 :param class_txn txn: The class txn object containing the data.
2868 :param integer loglevel: The required log level. This variable can be one of
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01002869 :see: :js:attr:`core.emerg`, :js:attr:`core.alert`, :js:attr:`core.crit`,
2870 :js:attr:`core.err`, :js:attr:`core.warning`, :js:attr:`core.notice`,
2871 :js:attr:`core.info`, :js:attr:`core.debug` (log level definitions)
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01002872
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002873.. js:function:: TXN.set_tos(txn, tos)
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01002874
2875 Is used to set the TOS or DSCP field value of packets sent to the client to
2876 the value passed in "tos" on platforms which support this.
2877
2878 :param class_txn txn: The class txn object containing the data.
2879 :param integer tos: The new TOS os DSCP.
2880
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002881.. js:function:: TXN.set_mark(txn, mark)
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01002882
2883 Is used to set the Netfilter MARK on all packets sent to the client to the
2884 value passed in "mark" on platforms which support it.
2885
2886 :param class_txn txn: The class txn object containing the data.
2887 :param integer mark: The mark value.
2888
Patrick Hemmer268a7072018-05-11 12:52:31 -04002889.. js:function:: TXN.set_priority_class(txn, prio)
2890
2891 This function adjusts the priority class of the transaction. The value should
2892 be within the range -2047..2047. Values outside this range will be
2893 truncated.
2894
2895 See the HAProxy configuration.txt file keyword "http-request" action
2896 "set-priority-class" for details.
2897
2898.. js:function:: TXN.set_priority_offset(txn, prio)
2899
2900 This function adjusts the priority offset of the transaction. The value
2901 should be within the range -524287..524287. Values outside this range will be
2902 truncated.
2903
2904 See the HAProxy configuration.txt file keyword "http-request" action
2905 "set-priority-offset" for details.
2906
Christopher Faulet700d9e82020-01-31 12:21:52 +01002907.. _reply_class:
2908
2909Reply class
2910============
2911
2912.. js:class:: Reply
2913
2914 **context**: action
2915
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02002916 This class represents a HTTP response message. It provides some methods to
Christopher Faulet7855b192021-11-09 18:39:51 +01002917 enrich it. Once converted into the internal HTTP representation, the response
2918 message must not exceed the buffer size. Because for now there is no
2919 easy way to be sure it fits, it is probably better to keep it reasonably
2920 small.
2921
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02002922 See tune.bufsize in the configuration manual for details.
Christopher Faulet700d9e82020-01-31 12:21:52 +01002923
2924.. code-block:: lua
2925
2926 local reply = txn:reply({status = 400}) -- default HTTP 400 reason-phase used
2927 reply:add_header("content-type", "text/html")
2928 reply:add_header("cache-control", "no-cache")
2929 reply:add_header("cache-control", "no-store")
2930 reply:set_body("<html><body><h1>invalid request<h1></body></html>")
2931..
2932
2933 :see: :js:func:`TXN.reply`
2934
2935.. js:attribute:: Reply.status
2936
2937 The reply status code. By default, the status code is set to 200.
2938
2939 :returns: integer
2940
2941.. js:attribute:: Reply.reason
2942
2943 The reason string describing the status code.
2944
2945 :returns: string
2946
2947.. js:attribute:: Reply.headers
2948
2949 A table indexing all reply headers by name. To each name is associated an
2950 ordered list of values.
2951
2952 :returns: Lua table
2953
2954.. code-block:: lua
2955
2956 {
2957 ["content-type"] = { "text/html" },
2958 ["cache-control"] = {"no-cache", "no-store" },
2959 x_header_name = { "value1", "value2", ... }
2960 ...
2961 }
2962..
2963
2964.. js:attribute:: Reply.body
2965
2966 The reply payload.
2967
2968 :returns: string
2969
2970.. js:function:: Reply.set_status(REPLY, status[, reason])
2971
2972 Set the reply status code and optionally the reason-phrase. If the reason is
2973 not provided, the default reason corresponding to the status code is used.
2974
2975 :param class_reply reply: The related Reply object.
2976 :param integer status: The reply status code.
2977 :param string reason: The reply status reason (optional).
2978
2979.. js:function:: Reply.add_header(REPLY, name, value)
2980
2981 Add a header to the reply object. If the header does not already exist, a new
2982 entry is created with its name as index and a one-element list containing its
2983 value as value. Otherwise, the header value is appended to the ordered list of
2984 values associated to the header name.
2985
2986 :param class_reply reply: The related Reply object.
2987 :param string name: The header field name.
2988 :param string value: The header field value.
2989
2990.. js:function:: Reply.del_header(REPLY, name)
2991
2992 Remove all occurrences of a header name from the reply object.
2993
2994 :param class_reply reply: The related Reply object.
2995 :param string name: The header field name.
2996
2997.. js:function:: Reply.set_body(REPLY, body)
2998
2999 Set the reply payload.
3000
3001 :param class_reply reply: The related Reply object.
3002 :param string body: The reply payload.
3003
Thierry FOURNIERdc595002015-12-21 11:13:52 +01003004.. _socket_class:
3005
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01003006Socket class
3007============
3008
3009.. js:class:: Socket
3010
3011 This class must be compatible with the Lua Socket class. Only the 'client'
3012 functions are available. See the Lua Socket documentation:
3013
3014 `http://w3.impa.br/~diego/software/luasocket/tcp.html
3015 <http://w3.impa.br/~diego/software/luasocket/tcp.html>`_
3016
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01003017.. js:function:: Socket.close(socket)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01003018
3019 Closes a TCP object. The internal socket used by the object is closed and the
3020 local address to which the object was bound is made available to other
3021 applications. No further operations (except for further calls to the close
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01003022 method) are allowed on a closed Socket.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01003023
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01003024 :param class_socket socket: Is the manipulated Socket.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01003025
3026 Note: It is important to close all used sockets once they are not needed,
3027 since, in many systems, each socket uses a file descriptor, which are limited
3028 system resources. Garbage-collected objects are automatically closed before
3029 destruction, though.
3030
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02003031.. js:function:: Socket.connect(socket, address[, port])
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01003032
3033 Attempts to connect a socket object to a remote host.
3034
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01003035
3036 In case of error, the method returns nil followed by a string describing the
3037 error. In case of success, the method returns 1.
3038
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01003039 :param class_socket socket: Is the manipulated Socket.
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02003040 :param string address: can be an IP address or a host name. See below for more
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02003041 information.
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02003042 :param integer port: must be an integer number in the range [1..64K].
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01003043 :returns: 1 or nil.
3044
Bertrand Jacquin874a35c2018-09-10 21:26:07 +01003045 An address field extension permits to use the connect() function to connect to
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02003046 other stream than TCP. The syntax containing a simpleipv4 or ipv6 address is
3047 the basically expected format. This format requires the port.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01003048
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02003049 Other format accepted are a socket path like "/socket/path", it permits to
Bertrand Jacquin874a35c2018-09-10 21:26:07 +01003050 connect to a socket. Abstract namespaces are supported with the prefix
Joseph Herlant02cedc42018-11-13 19:45:17 -08003051 "abns@", and finally a file descriptor can be passed with the prefix "fd@".
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02003052 The prefix "ipv4@", "ipv6@" and "unix@" are also supported. The port can be
Bertrand Jacquin874a35c2018-09-10 21:26:07 +01003053 passed int the string. The syntax "127.0.0.1:1234" is valid. In this case, the
Tim Duesterhus6edab862018-01-06 19:04:45 +01003054 parameter *port* must not be set.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01003055
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01003056.. js:function:: Socket.connect_ssl(socket, address, port)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01003057
3058 Same behavior than the function socket:connect, but uses SSL.
3059
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01003060 :param class_socket socket: Is the manipulated Socket.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01003061 :returns: 1 or nil.
3062
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01003063.. js:function:: Socket.getpeername(socket)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01003064
3065 Returns information about the remote side of a connected client object.
3066
3067 Returns a string with the IP address of the peer, followed by the port number
3068 that peer is using for the connection. In case of error, the method returns
3069 nil.
3070
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01003071 :param class_socket socket: Is the manipulated Socket.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01003072 :returns: a string containing the server information.
3073
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01003074.. js:function:: Socket.getsockname(socket)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01003075
3076 Returns the local address information associated to the object.
3077
3078 The method returns a string with local IP address and a number with the port.
3079 In case of error, the method returns nil.
3080
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01003081 :param class_socket socket: Is the manipulated Socket.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01003082 :returns: a string containing the client information.
3083
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01003084.. js:function:: Socket.receive(socket, [pattern [, prefix]])
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01003085
3086 Reads data from a client object, according to the specified read pattern.
3087 Patterns follow the Lua file I/O format, and the difference in performance
3088 between all patterns is negligible.
3089
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01003090 :param class_socket socket: Is the manipulated Socket.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01003091 :param string|integer pattern: Describe what is required (see below).
3092 :param string prefix: A string which will be prefix the returned data.
3093 :returns: a string containing the required data or nil.
3094
3095 Pattern can be any of the following:
3096
3097 * **`*a`**: reads from the socket until the connection is closed. No
3098 end-of-line translation is performed;
3099
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01003100 * **`*l`**: reads a line of text from the Socket. The line is terminated by a
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01003101 LF character (ASCII 10), optionally preceded by a CR character
3102 (ASCII 13). The CR and LF characters are not included in the
3103 returned line. In fact, all CR characters are ignored by the
3104 pattern. This is the default pattern.
3105
3106 * **number**: causes the method to read a specified number of bytes from the
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01003107 Socket. Prefix is an optional string to be concatenated to the
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01003108 beginning of any received data before return.
3109
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02003110 * **empty**: If the pattern is left empty, the default option is `*l`.
3111
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01003112 If successful, the method returns the received pattern. In case of error, the
3113 method returns nil followed by an error message which can be the string
3114 'closed' in case the connection was closed before the transmission was
3115 completed or the string 'timeout' in case there was a timeout during the
3116 operation. Also, after the error message, the function returns the partial
3117 result of the transmission.
3118
3119 Important note: This function was changed severely. It used to support
3120 multiple patterns (but I have never seen this feature used) and now it
3121 doesn't anymore. Partial results used to be returned in the same way as
3122 successful results. This last feature violated the idea that all functions
3123 should return nil on error. Thus it was changed too.
3124
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01003125.. js:function:: Socket.send(socket, data [, start [, end ]])
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01003126
3127 Sends data through client object.
3128
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01003129 :param class_socket socket: Is the manipulated Socket.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01003130 :param string data: The data that will be sent.
3131 :param integer start: The start position in the buffer of the data which will
3132 be sent.
3133 :param integer end: The end position in the buffer of the data which will
3134 be sent.
3135 :returns: see below.
3136
3137 Data is the string to be sent. The optional arguments i and j work exactly
3138 like the standard string.sub Lua function to allow the selection of a
3139 substring to be sent.
3140
3141 If successful, the method returns the index of the last byte within [start,
3142 end] that has been sent. Notice that, if start is 1 or absent, this is
3143 effectively the total number of bytes sent. In case of error, the method
3144 returns nil, followed by an error message, followed by the index of the last
3145 byte within [start, end] that has been sent. You might want to try again from
3146 the byte following that. The error message can be 'closed' in case the
3147 connection was closed before the transmission was completed or the string
3148 'timeout' in case there was a timeout during the operation.
3149
3150 Note: Output is not buffered. For small strings, it is always better to
3151 concatenate them in Lua (with the '..' operator) and send the result in one
3152 call instead of calling the method several times.
3153
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01003154.. js:function:: Socket.setoption(socket, option [, value])
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01003155
3156 Just implemented for compatibility, this cal does nothing.
3157
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01003158.. js:function:: Socket.settimeout(socket, value [, mode])
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01003159
3160 Changes the timeout values for the object. All I/O operations are blocking.
3161 That is, any call to the methods send, receive, and accept will block
3162 indefinitely, until the operation completes. The settimeout method defines a
3163 limit on the amount of time the I/O methods can block. When a timeout time
3164 has elapsed, the affected methods give up and fail with an error code.
3165
3166 The amount of time to wait is specified as the value parameter, in seconds.
3167
Mark Lakes56cc1252018-03-27 09:48:06 +02003168 The timeout modes are not implemented, the only settable timeout is the
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01003169 inactivity time waiting for complete the internal buffer send or waiting for
3170 receive data.
3171
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01003172 :param class_socket socket: Is the manipulated Socket.
Bertrand Jacquin874a35c2018-09-10 21:26:07 +01003173 :param float value: The timeout value. Use floating point to specify
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02003174 milliseconds.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01003175
Thierry FOURNIER31904272017-10-25 12:59:51 +02003176.. _regex_class:
3177
3178Regex class
3179===========
3180
3181.. js:class:: Regex
3182
3183 This class allows the usage of HAProxy regexes because classic lua doesn't
3184 provides regexes. This class inherits the HAProxy compilation options, so the
3185 regexes can be libc regex, pcre regex or pcre JIT regex.
3186
3187 The expression matching number is limited to 20 per regex. The only available
3188 option is case sensitive.
3189
3190 Because regexes compilation is a heavy process, it is better to define all
3191 your regexes in the **body context** and use it during the runtime.
3192
3193.. code-block:: lua
3194
3195 -- Create the regex
3196 st, regex = Regex.new("needle (..) (...)", true);
3197
3198 -- Check compilation errors
3199 if st == false then
3200 print "error: " .. regex
3201 end
3202
3203 -- Match the regexes
3204 print(regex:exec("Looking for a needle in the haystack")) -- true
3205 print(regex:exec("Lokking for a cat in the haystack")) -- false
3206
3207 -- Extract words
3208 st, list = regex:match("Looking for a needle in the haystack")
3209 print(st) -- true
3210 print(list[1]) -- needle in the
3211 print(list[2]) -- in
3212 print(list[3]) -- the
3213
3214.. js:function:: Regex.new(regex, case_sensitive)
3215
3216 Create and compile a regex.
3217
3218 :param string regex: The regular expression according with the libc or pcre
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02003219 standard
Thierry FOURNIER31904272017-10-25 12:59:51 +02003220 :param boolean case_sensitive: Match is case sensitive or not.
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02003221 :returns: boolean status and :ref:`regex_class` or string containing fail
3222 reason.
Thierry FOURNIER31904272017-10-25 12:59:51 +02003223
3224.. js:function:: Regex.exec(regex, str)
3225
3226 Execute the regex.
3227
3228 :param class_regex regex: A :ref:`regex_class` object.
3229 :param string str: The input string will be compared with the compiled regex.
3230 :returns: a boolean status according with the match result.
3231
3232.. js:function:: Regex.match(regex, str)
3233
3234 Execute the regex and return matched expressions.
3235
3236 :param class_map map: 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, and
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02003239 a table containing all the string matched in order of declaration.
Thierry FOURNIER31904272017-10-25 12:59:51 +02003240
Thierry FOURNIERdc595002015-12-21 11:13:52 +01003241.. _map_class:
3242
Thierry FOURNIER3def3932015-04-07 11:27:54 +02003243Map class
3244=========
3245
3246.. js:class:: Map
3247
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02003248 This class permits to do some lookups in HAProxy maps. The declared maps can
Bertrand Jacquin874a35c2018-09-10 21:26:07 +01003249 be modified during the runtime through the HAProxy management socket.
Thierry FOURNIER3def3932015-04-07 11:27:54 +02003250
3251.. code-block:: lua
3252
Thierry FOURNIERdc595002015-12-21 11:13:52 +01003253 default = "usa"
Thierry FOURNIER3def3932015-04-07 11:27:54 +02003254
Thierry FOURNIERdc595002015-12-21 11:13:52 +01003255 -- Create and load map
Thierry FOURNIER4dc71972017-01-28 08:33:08 +01003256 geo = Map.new("geo.map", Map._ip);
Thierry FOURNIER3def3932015-04-07 11:27:54 +02003257
Thierry FOURNIERdc595002015-12-21 11:13:52 +01003258 -- Create new fetch that returns the user country
3259 core.register_fetches("country", function(txn)
3260 local src;
3261 local loc;
Thierry FOURNIER3def3932015-04-07 11:27:54 +02003262
Thierry FOURNIERdc595002015-12-21 11:13:52 +01003263 src = txn.f:fhdr("x-forwarded-for");
3264 if (src == nil) then
3265 src = txn.f:src()
3266 if (src == nil) then
3267 return default;
3268 end
3269 end
Thierry FOURNIER3def3932015-04-07 11:27:54 +02003270
Thierry FOURNIERdc595002015-12-21 11:13:52 +01003271 -- Perform lookup
3272 loc = geo:lookup(src);
Thierry FOURNIER3def3932015-04-07 11:27:54 +02003273
Thierry FOURNIERdc595002015-12-21 11:13:52 +01003274 if (loc == nil) then
3275 return default;
3276 end
Thierry FOURNIER3def3932015-04-07 11:27:54 +02003277
Thierry FOURNIERdc595002015-12-21 11:13:52 +01003278 return loc;
3279 end);
Thierry FOURNIER3def3932015-04-07 11:27:54 +02003280
Thierry FOURNIER4dc71972017-01-28 08:33:08 +01003281.. js:attribute:: Map._int
Thierry FOURNIER3def3932015-04-07 11:27:54 +02003282
3283 See the HAProxy configuration.txt file, chapter "Using ACLs and fetching
Ilya Shipitsin2075ca82020-03-06 23:22:22 +05003284 samples" and subchapter "ACL basics" to understand this pattern matching
Thierry FOURNIER3def3932015-04-07 11:27:54 +02003285 method.
3286
Thierry FOURNIER4dc71972017-01-28 08:33:08 +01003287 Note that :js:attr:`Map.int` is also available for compatibility.
3288
3289.. js:attribute:: Map._ip
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.ip` is also available for compatibility.
3296
3297.. js:attribute:: Map._str
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.str` is also available for compatibility.
3304
3305.. js:attribute:: Map._beg
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.beg` is also available for compatibility.
3312
3313.. js:attribute:: Map._sub
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.sub` is also available for compatibility.
3320
3321.. js:attribute:: Map._dir
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.dir` is also available for compatibility.
3328
3329.. js:attribute:: Map._dom
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.dom` is also available for compatibility.
3336
3337.. js:attribute:: Map._end
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.. js:attribute:: Map._reg
Thierry FOURNIER3def3932015-04-07 11:27:54 +02003344
3345 See the HAProxy configuration.txt file, chapter "Using ACLs and fetching
Ilya Shipitsin2075ca82020-03-06 23:22:22 +05003346 samples" and subchapter "ACL basics" to understand this pattern matching
Thierry FOURNIER3def3932015-04-07 11:27:54 +02003347 method.
3348
Thierry FOURNIER4dc71972017-01-28 08:33:08 +01003349 Note that :js:attr:`Map.reg` is also available for compatibility.
3350
Thierry FOURNIER3def3932015-04-07 11:27:54 +02003351
3352.. js:function:: Map.new(file, method)
3353
3354 Creates and load a map.
3355
3356 :param string file: Is the file containing the map.
3357 :param integer method: Is the map pattern matching method. See the attributes
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02003358 of the Map class.
Thierry FOURNIER3def3932015-04-07 11:27:54 +02003359 :returns: a class Map object.
Thierry FOURNIER4dc71972017-01-28 08:33:08 +01003360 :see: The Map attributes: :js:attr:`Map._int`, :js:attr:`Map._ip`,
3361 :js:attr:`Map._str`, :js:attr:`Map._beg`, :js:attr:`Map._sub`,
3362 :js:attr:`Map._dir`, :js:attr:`Map._dom`, :js:attr:`Map._end` and
3363 :js:attr:`Map._reg`.
Thierry FOURNIER3def3932015-04-07 11:27:54 +02003364
3365.. js:function:: Map.lookup(map, str)
3366
3367 Perform a lookup in a map.
3368
3369 :param class_map map: Is the class Map object.
3370 :param string str: Is the string used as key.
3371 :returns: a string containing the result or nil if no match.
3372
3373.. js:function:: Map.slookup(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 empty string if no match.
3380
Thierry FOURNIERdc595002015-12-21 11:13:52 +01003381.. _applethttp_class:
3382
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02003383AppletHTTP class
Thierry FOURNIERdc595002015-12-21 11:13:52 +01003384================
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02003385
3386.. js:class:: AppletHTTP
3387
3388 This class is used with applets that requires the 'http' mode. The http applet
3389 can be registered with the *core.register_service()* function. They are used
3390 for processing an http request like a server in back of HAProxy.
3391
3392 This is an hello world sample code:
3393
3394.. code-block:: lua
Thierry FOURNIERdc595002015-12-21 11:13:52 +01003395
Pieter Baauw4d7f7662015-11-08 16:38:08 +01003396 core.register_service("hello-world", "http", function(applet)
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02003397 local response = "Hello World !"
3398 applet:set_status(200)
Pieter Baauw2dcb9bc2015-10-01 22:47:12 +02003399 applet:add_header("content-length", string.len(response))
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02003400 applet:add_header("content-type", "text/plain")
Pieter Baauw2dcb9bc2015-10-01 22:47:12 +02003401 applet:start_response()
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02003402 applet:send(response)
3403 end)
3404
Thierry FOURNIERdc595002015-12-21 11:13:52 +01003405.. js:attribute:: AppletHTTP.c
3406
3407 :returns: A :ref:`converters_class`
3408
3409 This attribute contains a Converters class object.
3410
3411.. js:attribute:: AppletHTTP.sc
3412
3413 :returns: A :ref:`converters_class`
3414
3415 This attribute contains a Converters class object. The
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02003416 functions of this object always return a string.
Thierry FOURNIERdc595002015-12-21 11:13:52 +01003417
3418.. js:attribute:: AppletHTTP.f
3419
3420 :returns: A :ref:`fetches_class`
3421
3422 This attribute contains a Fetches class object. Note that the
3423 applet execution place cannot access to a valid HAProxy core HTTP
Ilya Shipitsin2075ca82020-03-06 23:22:22 +05003424 transaction, so some sample fetches related to the HTTP dependent
Thierry FOURNIERdc595002015-12-21 11:13:52 +01003425 values (hdr, path, ...) are not available.
3426
3427.. js:attribute:: AppletHTTP.sf
3428
3429 :returns: A :ref:`fetches_class`
3430
3431 This attribute contains a Fetches class object. The functions of
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02003432 this object always return a string. Note that the applet
Thierry FOURNIERdc595002015-12-21 11:13:52 +01003433 execution place cannot access to a valid HAProxy core HTTP
Ilya Shipitsin2075ca82020-03-06 23:22:22 +05003434 transaction, so some sample fetches related to the HTTP dependent
Thierry FOURNIERdc595002015-12-21 11:13:52 +01003435 values (hdr, path, ...) are not available.
3436
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01003437.. js:attribute:: AppletHTTP.method
Thierry FOURNIERdc595002015-12-21 11:13:52 +01003438
3439 :returns: string
3440
3441 The attribute method returns a string containing the HTTP
3442 method.
3443
3444.. js:attribute:: AppletHTTP.version
3445
3446 :returns: string
3447
3448 The attribute version, returns a string containing the HTTP
3449 request version.
3450
3451.. js:attribute:: AppletHTTP.path
3452
3453 :returns: string
3454
3455 The attribute path returns a string containing the HTTP
3456 request path.
3457
3458.. js:attribute:: AppletHTTP.qs
3459
3460 :returns: string
3461
3462 The attribute qs returns a string containing the HTTP
3463 request query string.
3464
3465.. js:attribute:: AppletHTTP.length
3466
3467 :returns: integer
3468
3469 The attribute length returns an integer containing the HTTP
3470 body length.
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02003471
Thierry FOURNIER841475e2015-12-11 17:10:09 +01003472.. js:attribute:: AppletHTTP.headers
3473
Patrick Hemmerc6a1d712018-05-01 21:30:41 -04003474 :returns: table
Thierry FOURNIERdc595002015-12-21 11:13:52 +01003475
Patrick Hemmerc6a1d712018-05-01 21:30:41 -04003476 The attribute headers returns a table containing the HTTP
Thierry FOURNIERdc595002015-12-21 11:13:52 +01003477 headers. The header names are always in lower case. As the header name can be
3478 encountered more than once in each request, the value is indexed with 0 as
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02003479 first index value. The table has this form:
Thierry FOURNIERdc595002015-12-21 11:13:52 +01003480
3481.. code-block:: lua
3482
3483 AppletHTTP.headers['<header-name>'][<header-index>] = "<header-value>"
3484
3485 AppletHTTP.headers["host"][0] = "www.test.com"
3486 AppletHTTP.headers["accept"][0] = "audio/basic q=1"
3487 AppletHTTP.headers["accept"][1] = "audio/*, q=0.2"
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01003488 AppletHTTP.headers["accept"][2] = "*/*, q=0.1"
Thierry FOURNIERdc595002015-12-21 11:13:52 +01003489..
3490
Robin H. Johnson52f5db22017-01-01 13:10:52 -08003491.. js:function:: AppletHTTP.set_status(applet, code [, reason])
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02003492
3493 This function sets the HTTP status code for the response. The allowed code are
3494 from 100 to 599.
3495
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01003496 :param class_AppletHTTP applet: An :ref:`applethttp_class`
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02003497 :param integer code: the status code returned to the client.
Robin H. Johnson52f5db22017-01-01 13:10:52 -08003498 :param string reason: the status reason returned to the client (optional).
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02003499
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01003500.. js:function:: AppletHTTP.add_header(applet, name, value)
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02003501
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02003502 This function adds a header in the response. Duplicated headers are not
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02003503 collapsed. The special header *content-length* is used to determinate the
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02003504 response length. If it does not exist, a *transfer-encoding: chunked* is set,
3505 and all the write from the function *AppletHTTP:send()* become a chunk.
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02003506
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01003507 :param class_AppletHTTP applet: An :ref:`applethttp_class`
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02003508 :param string name: the header name
3509 :param string value: the header value
3510
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01003511.. js:function:: AppletHTTP.start_response(applet)
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02003512
3513 This function indicates to the HTTP engine that it can process and send the
3514 response headers. After this called we cannot add headers to the response; We
3515 cannot use the *AppletHTTP:send()* function if the
3516 *AppletHTTP:start_response()* is not called.
3517
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01003518 :param class_AppletHTTP applet: An :ref:`applethttp_class`
3519
3520.. js:function:: AppletHTTP.getline(applet)
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02003521
3522 This function returns a string containing one line from the http body. If the
3523 data returned doesn't contains a final '\\n' its assumed than its the last
3524 available data before the end of stream.
3525
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01003526 :param class_AppletHTTP applet: An :ref:`applethttp_class`
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02003527 :returns: a string. The string can be empty if we reach the end of the stream.
3528
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01003529.. js:function:: AppletHTTP.receive(applet, [size])
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02003530
3531 Reads data from the HTTP body, according to the specified read *size*. If the
3532 *size* is missing, the function tries to read all the content of the stream
3533 until the end. If the *size* is bigger than the http body, it returns the
Bertrand Jacquin874a35c2018-09-10 21:26:07 +01003534 amount of data available.
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02003535
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01003536 :param class_AppletHTTP applet: An :ref:`applethttp_class`
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02003537 :param integer size: the required read size.
Ilya Shipitsin11057a32020-06-21 21:18:27 +05003538 :returns: always return a string,the string can be empty is the connection is
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02003539 closed.
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02003540
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01003541.. js:function:: AppletHTTP.send(applet, msg)
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02003542
3543 Send the message *msg* on the http request body.
3544
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01003545 :param class_AppletHTTP applet: An :ref:`applethttp_class`
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02003546 :param string msg: the message to send.
3547
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01003548.. js:function:: AppletHTTP.get_priv(applet)
3549
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01003550 Return Lua data stored in the current transaction. If no data are stored,
3551 it returns a nil value.
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01003552
3553 :param class_AppletHTTP applet: An :ref:`applethttp_class`
Bertrand Jacquin874a35c2018-09-10 21:26:07 +01003554 :returns: the opaque data previously stored, or nil if nothing is
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02003555 available.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01003556 :see: :js:func:`AppletHTTP.set_priv`
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01003557
3558.. js:function:: AppletHTTP.set_priv(applet, data)
3559
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02003560 Store any data in the current HAProxy transaction. This action replaces the
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01003561 old stored data.
3562
3563 :param class_AppletHTTP applet: An :ref:`applethttp_class`
3564 :param opaque data: The data which is stored in the transaction.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01003565 :see: :js:func:`AppletHTTP.get_priv`
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01003566
Tim Duesterhus4e172c92020-05-19 13:49:42 +02003567.. js:function:: AppletHTTP.set_var(applet, var, value[, ifexist])
Thierry FOURNIER / OZON.IOc1edafe2016-12-12 16:25:30 +01003568
3569 Converts a Lua type in a HAProxy type and store it in a variable <var>.
3570
3571 :param class_AppletHTTP applet: An :ref:`applethttp_class`
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02003572 :param string var: The variable name according with the HAProxy variable
3573 syntax.
3574 :param type value: The value associated to the variable. The type ca be string
3575 or integer.
3576 :param boolean ifexist: If this parameter is set to true the variable will
3577 only be set if it was defined elsewhere (i.e. used within the configuration).
3578 For global variables (using the "proc" scope), they will only be updated and
3579 never created. It is highly recommended to always set this to true.
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02003580
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01003581 :see: :js:func:`AppletHTTP.unset_var`
3582 :see: :js:func:`AppletHTTP.get_var`
Thierry FOURNIER / OZON.IOc1edafe2016-12-12 16:25:30 +01003583
3584.. js:function:: AppletHTTP.unset_var(applet, var)
3585
3586 Unset the variable <var>.
3587
3588 :param class_AppletHTTP applet: An :ref:`applethttp_class`
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02003589 :param string var: The variable name according with the HAProxy variable
3590 syntax.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01003591 :see: :js:func:`AppletHTTP.set_var`
3592 :see: :js:func:`AppletHTTP.get_var`
Thierry FOURNIER / OZON.IOc1edafe2016-12-12 16:25:30 +01003593
3594.. js:function:: AppletHTTP.get_var(applet, var)
3595
3596 Returns data stored in the variable <var> converter in Lua type.
3597
3598 :param class_AppletHTTP applet: An :ref:`applethttp_class`
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02003599 :param string var: The variable name according with the HAProxy variable
3600 syntax.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01003601 :see: :js:func:`AppletHTTP.set_var`
3602 :see: :js:func:`AppletHTTP.unset_var`
Thierry FOURNIER / OZON.IOc1edafe2016-12-12 16:25:30 +01003603
Thierry FOURNIERdc595002015-12-21 11:13:52 +01003604.. _applettcp_class:
3605
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02003606AppletTCP class
3607===============
3608
3609.. js:class:: AppletTCP
3610
3611 This class is used with applets that requires the 'tcp' mode. The tcp applet
3612 can be registered with the *core.register_service()* function. They are used
3613 for processing a tcp stream like a server in back of HAProxy.
3614
Thierry FOURNIERdc595002015-12-21 11:13:52 +01003615.. js:attribute:: AppletTCP.c
3616
3617 :returns: A :ref:`converters_class`
3618
3619 This attribute contains a Converters class object.
3620
3621.. js:attribute:: AppletTCP.sc
3622
3623 :returns: A :ref:`converters_class`
3624
3625 This attribute contains a Converters class object. The
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02003626 functions of this object always return a string.
Thierry FOURNIERdc595002015-12-21 11:13:52 +01003627
3628.. js:attribute:: AppletTCP.f
3629
3630 :returns: A :ref:`fetches_class`
3631
3632 This attribute contains a Fetches class object.
3633
3634.. js:attribute:: AppletTCP.sf
3635
3636 :returns: A :ref:`fetches_class`
3637
3638 This attribute contains a Fetches class object.
3639
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01003640.. js:function:: AppletTCP.getline(applet)
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02003641
3642 This function returns a string containing one line from the stream. If the
3643 data returned doesn't contains a final '\\n' its assumed than its the last
3644 available data before the end of stream.
3645
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01003646 :param class_AppletTCP applet: An :ref:`applettcp_class`
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02003647 :returns: a string. The string can be empty if we reach the end of the stream.
3648
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01003649.. js:function:: AppletTCP.receive(applet, [size])
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02003650
3651 Reads data from the TCP stream, according to the specified read *size*. If the
3652 *size* is missing, the function tries to read all the content of the stream
3653 until the end.
3654
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01003655 :param class_AppletTCP applet: An :ref:`applettcp_class`
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02003656 :param integer size: the required read size.
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02003657 :returns: always return a string, the string can be empty if the connection is
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02003658 closed.
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02003659
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01003660.. js:function:: AppletTCP.send(appletmsg)
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02003661
3662 Send the message on the stream.
3663
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01003664 :param class_AppletTCP applet: An :ref:`applettcp_class`
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02003665 :param string msg: the message to send.
3666
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01003667.. js:function:: AppletTCP.get_priv(applet)
3668
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01003669 Return Lua data stored in the current transaction. If no data are stored,
3670 it returns a nil value.
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01003671
3672 :param class_AppletTCP applet: An :ref:`applettcp_class`
Bertrand Jacquin874a35c2018-09-10 21:26:07 +01003673 :returns: the opaque data previously stored, or nil if nothing is
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02003674 available.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01003675 :see: :js:func:`AppletTCP.set_priv`
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01003676
3677.. js:function:: AppletTCP.set_priv(applet, data)
3678
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02003679 Store any data in the current HAProxy transaction. This action replaces the
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01003680 old stored data.
3681
3682 :param class_AppletTCP applet: An :ref:`applettcp_class`
3683 :param opaque data: The data which is stored in the transaction.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01003684 :see: :js:func:`AppletTCP.get_priv`
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01003685
Tim Duesterhus4e172c92020-05-19 13:49:42 +02003686.. js:function:: AppletTCP.set_var(applet, var, value[, ifexist])
Thierry FOURNIER / OZON.IOc1edafe2016-12-12 16:25:30 +01003687
3688 Converts a Lua type in a HAProxy type and stores it in a variable <var>.
3689
3690 :param class_AppletTCP applet: An :ref:`applettcp_class`
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02003691 :param string var: The variable name according with the HAProxy variable
3692 syntax.
3693 :param type value: The value associated to the variable. The type can be
3694 string or integer.
3695 :param boolean ifexist: If this parameter is set to true the variable will
3696 only be set if it was defined elsewhere (i.e. used within the configuration).
3697 For global variables (using the "proc" scope), they will only be updated and
3698 never created. It is highly recommended to always set this to true.
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02003699
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01003700 :see: :js:func:`AppletTCP.unset_var`
3701 :see: :js:func:`AppletTCP.get_var`
Thierry FOURNIER / OZON.IOc1edafe2016-12-12 16:25:30 +01003702
3703.. js:function:: AppletTCP.unset_var(applet, var)
3704
3705 Unsets the variable <var>.
3706
3707 :param class_AppletTCP applet: An :ref:`applettcp_class`
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02003708 :param string var: The variable name according with the HAProxy variable
3709 syntax.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01003710 :see: :js:func:`AppletTCP.unset_var`
3711 :see: :js:func:`AppletTCP.set_var`
Thierry FOURNIER / OZON.IOc1edafe2016-12-12 16:25:30 +01003712
3713.. js:function:: AppletTCP.get_var(applet, var)
3714
3715 Returns data stored in the variable <var> converter in Lua type.
3716
3717 :param class_AppletTCP applet: An :ref:`applettcp_class`
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02003718 :param string var: The variable name according with the HAProxy variable
3719 syntax.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01003720 :see: :js:func:`AppletTCP.unset_var`
3721 :see: :js:func:`AppletTCP.set_var`
Thierry FOURNIER / OZON.IOc1edafe2016-12-12 16:25:30 +01003722
Adis Nezirovic8878f8e2018-07-13 12:18:33 +02003723StickTable class
3724================
3725
3726.. js:class:: StickTable
3727
3728 **context**: task, action, sample-fetch
3729
3730 This class can be used to access the HAProxy stick tables from Lua.
3731
3732.. js:function:: StickTable.info()
3733
3734 Returns stick table attributes as a Lua table. See HAProxy documentation for
Ilya Shipitsin2272d8a2020-12-21 01:22:40 +05003735 "stick-table" for canonical info, or check out example below.
Adis Nezirovic8878f8e2018-07-13 12:18:33 +02003736
3737 :returns: Lua table
3738
3739 Assume our table has IPv4 key and gpc0 and conn_rate "columns":
3740
3741.. code-block:: lua
3742
3743 {
3744 expire=<int>, # Value in ms
3745 size=<int>, # Maximum table size
3746 used=<int>, # Actual number of entries in table
3747 data={ # Data columns, with types as key, and periods as values
3748 (-1 if type is not rate counter)
3749 conn_rate=<int>,
3750 gpc0=-1
3751 },
3752 length=<int>, # max string length for string table keys, key length
3753 # otherwise
3754 nopurge=<boolean>, # purge oldest entries when table is full
3755 type="ip" # can be "ip", "ipv6", "integer", "string", "binary"
3756 }
3757
3758.. js:function:: StickTable.lookup(key)
3759
3760 Returns stick table entry for given <key>
3761
3762 :param string key: Stick table key (IP addresses and strings are supported)
3763 :returns: Lua table
3764
3765.. js:function:: StickTable.dump([filter])
3766
3767 Returns all entries in stick table. An optional filter can be used
3768 to extract entries with specific data values. Filter is a table with valid
3769 comparison operators as keys followed by data type name and value pairs.
3770 Check out the HAProxy docs for "show table" for more details. For the
3771 reference, the supported operators are:
Aurelien DARRAGON21f7ebb2023-03-13 19:49:31 +01003772
Adis Nezirovic8878f8e2018-07-13 12:18:33 +02003773 "eq", "ne", "le", "lt", "ge", "gt"
3774
3775 For large tables, execution of this function can take a long time (for
3776 HAProxy standards). That's also true when filter is used, so take care and
3777 measure the impact.
3778
3779 :param table filter: Stick table filter
3780 :returns: Stick table entries (table)
3781
3782 See below for example filter, which contains 4 entries (or comparisons).
3783 (Maximum number of filter entries is 4, defined in the source code)
3784
3785.. code-block:: lua
3786
3787 local filter = {
3788 {"gpc0", "gt", 30}, {"gpc1", "gt", 20}}, {"conn_rate", "le", 10}
3789 }
3790
Christopher Faulet0f3c8902020-01-31 18:57:12 +01003791.. _action_class:
3792
3793Action class
3794=============
3795
3796.. js:class:: Act
3797
3798 **context**: action
3799
3800 This class contains all return codes an action may return. It is the lua
3801 equivalent to HAProxy "ACT_RET_*" code.
3802
3803.. code-block:: lua
3804
3805 core.register_action("deny", { "http-req" }, function (txn)
3806 return act.DENY
3807 end)
3808..
3809.. js:attribute:: act.CONTINUE
3810
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02003811 This attribute is an integer (0). It instructs HAProxy to continue the
3812 current ruleset processing on the message. It is the default return code
3813 for a lua action.
Christopher Faulet0f3c8902020-01-31 18:57:12 +01003814
3815 :returns: integer
3816
3817.. js:attribute:: act.STOP
3818
3819 This attribute is an integer (1). It instructs HAProxy to stop the current
3820 ruleset processing on the message.
3821
3822.. js:attribute:: act.YIELD
3823
3824 This attribute is an integer (2). It instructs HAProxy to temporarily pause
3825 the message processing. It will be resumed later on the same rule. The
3826 corresponding lua script is re-executed for the start.
3827
3828.. js:attribute:: act.ERROR
3829
3830 This attribute is an integer (3). It triggers an internal errors The message
3831 processing is stopped and the transaction is terminated. For HTTP streams, an
3832 HTTP 500 error is returned to the client.
3833
3834 :returns: integer
3835
3836.. js:attribute:: act.DONE
3837
3838 This attribute is an integer (4). It instructs HAProxy to stop the message
3839 processing.
3840
3841 :returns: integer
3842
3843.. js:attribute:: act.DENY
3844
3845 This attribute is an integer (5). It denies the current message. The message
3846 processing is stopped and the transaction is terminated. For HTTP streams, an
3847 HTTP 403 error is returned to the client if the deny is returned during the
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02003848 request analysis. During the response analysis, a HTTP 502 error is returned
Christopher Faulet0f3c8902020-01-31 18:57:12 +01003849 and the server response is discarded.
3850
3851 :returns: integer
3852
3853.. js:attribute:: act.ABORT
3854
3855 This attribute is an integer (6). It aborts the current message. The message
3856 processing is stopped and the transaction is terminated. For HTTP streams,
Willy Tarreau714f3452021-05-09 06:47:26 +02003857 HAProxy assumes a response was already sent to the client. From the Lua
Christopher Faulet0f3c8902020-01-31 18:57:12 +01003858 actions point of view, when this code is used, the transaction is terminated
3859 with no reply.
3860
3861 :returns: integer
3862
3863.. js:attribute:: act.INVALID
3864
3865 This attribute is an integer (7). It triggers an internal errors. The message
3866 processing is stopped and the transaction is terminated. For HTTP streams, an
3867 HTTP 400 error is returned to the client if the error is returned during the
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02003868 request analysis. During the response analysis, a HTTP 502 error is returned
Christopher Faulet0f3c8902020-01-31 18:57:12 +01003869 and the server response is discarded.
3870
3871 :returns: integer
Adis Nezirovic8878f8e2018-07-13 12:18:33 +02003872
Christopher Faulet2c2c2e32020-01-31 19:07:52 +01003873.. js:function:: act:wake_time(milliseconds)
3874
3875 **context**: action
3876
3877 Set the script pause timeout to the specified time, defined in
3878 milliseconds.
3879
3880 :param integer milliseconds: the required milliseconds.
3881
3882 This function may be used when a lua action returns `act.YIELD`, to force its
3883 wake-up at most after the specified number of milliseconds.
3884
Christopher Faulet5a2c6612021-08-15 20:35:25 +02003885.. _filter_class:
3886
3887Filter class
3888=============
3889
3890.. js:class:: filter
3891
3892 **context**: filter
3893
3894 This class contains return codes some filter callback functions may return. It
3895 also contains configuration flags and some helper functions. To understand how
3896 the filter API works, see `doc/internal/filters.txt` documentation.
3897
3898.. js:attribute:: filter.CONTINUE
3899
3900 This attribute is an integer (1). It may be returned by some filter callback
3901 functions to instruct this filtering step is finished for this filter.
3902
3903.. js:attribute:: filter.WAIT
3904
3905 This attribute is an integer (0). It may be returned by some filter callback
3906 functions to instruct the filtering must be paused, waiting for more data or
3907 for an external event depending on this filter.
3908
3909.. js:attribute:: filter.ERROR
3910
3911 This attribute is an integer (-1). It may be returned by some filter callback
3912 functions to trigger an error.
3913
3914.. js:attribute:: filter.FLT_CFG_FL_HTX
3915
3916 This attribute is a flag corresponding to the filter flag FLT_CFG_FL_HTX. When
3917 it is set for a filter, it means the filter is able to filter HTTP streams.
3918
3919.. js:function:: filter.register_data_filter(chn)
3920
3921 **context**: filter
3922
3923 Enable the data filtering on the channel **chn** for the current filter. It
Ilya Shipitsinff0f2782021-08-22 22:18:07 +05003924 may be called at any time from any callback functions proceeding the data
Christopher Faulet5a2c6612021-08-15 20:35:25 +02003925 analysis.
3926
3927 :param class_Channel chn: A :ref:`channel_class`.
3928
3929.. js:function:: filter.unregister_data_filter(chn)
3930
3931 **context**: filter
3932
3933 Disable the data filtering on the channel **chn** for the current filter. It
3934 may be called at any time from any callback functions.
3935
3936 :param class_Channel chn: A :ref:`channel_class`.
3937
3938.. js:function:: filter.wake_time(milliseconds)
3939
3940 **context**: filter
3941
3942 Set the script pause timeout to the specified time, defined in
3943 milliseconds.
3944
3945 :param integer milliseconds: the required milliseconds.
3946
3947 This function may be used from any lua filter callback function to force its
3948 wake-up at most after the specified number of milliseconds. Especially, when
3949 `filter.CONTINUE` is returned.
3950
3951
3952A filters is declared using :js:func:`core.register_filter()` function. The
3953provided class will be used to instantiate filters. It may define following
3954attributes:
3955
3956* id: The filter identifier. It is a string that identifies the filter and is
3957 optional.
3958
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02003959* flags: The filter flags. Only :js:attr:`filter.FLT_CFG_FL_HTX` may be set
3960 for now.
Christopher Faulet5a2c6612021-08-15 20:35:25 +02003961
3962Such filter class must also define all required callback functions in the
3963following list. Note that :js:func:`Filter.new()` must be defined otherwise the
Ilya Shipitsinff0f2782021-08-22 22:18:07 +05003964filter is ignored. Others are optional.
Christopher Faulet5a2c6612021-08-15 20:35:25 +02003965
3966* .. js:function:: FILTER.new()
3967
3968 Called to instantiate a new filter. This function must be defined.
3969
3970 :returns: a Lua object that will be used as filter instance for the current
3971 stream.
3972
3973* .. js:function:: FILTER.start_analyze(flt, txn, chn)
3974
3975 Called when the analysis starts on the channel **chn**.
3976
3977* .. js:function:: FILTER.end_analyze(flt, txn, chn)
3978
3979 Called when the analysis ends on the channel **chn**.
3980
3981* .. js:function:: FILTER.http_headers(flt, txn, http_msg)
3982
3983 Called just before the HTTP payload analysis and after any processing on the
3984 HTTP message **http_msg**. This callback functions is only called for HTTP
3985 streams.
3986
3987* .. js:function:: FILTER.http_payload(flt, txn, http_msg)
3988
3989 Called during the HTTP payload analysis on the HTTP message **http_msg**. This
3990 callback functions is only called for HTTP streams.
3991
3992* .. js:function:: FILTER.http_end(flt, txn, http_msg)
3993
3994 Called after the HTTP payload analysis on the HTTP message **http_msg**. This
3995 callback functions is only called for HTTP streams.
3996
3997* .. js:function:: FILTER.tcp_payload(flt, txn, chn)
3998
3999 Called during the TCP payload analysis on the channel **chn**.
4000
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02004001Here is a full example:
Christopher Faulet5a2c6612021-08-15 20:35:25 +02004002
4003.. code-block:: lua
4004
4005 Trace = {}
4006 Trace.id = "Lua trace filter"
4007 Trace.flags = filter.FLT_CFG_FL_HTX;
4008 Trace.__index = Trace
4009
4010 function Trace:new()
4011 local trace = {}
4012 setmetatable(trace, Trace)
4013 trace.req_len = 0
4014 trace.res_len = 0
4015 return trace
4016 end
4017
4018 function Trace:start_analyze(txn, chn)
4019 if chn:is_resp() then
4020 print("Start response analysis")
4021 else
4022 print("Start request analysis")
4023 end
4024 filter.register_data_filter(self, chn)
4025 end
4026
4027 function Trace:end_analyze(txn, chn)
4028 if chn:is_resp() then
4029 print("End response analysis: "..self.res_len.." bytes filtered")
4030 else
4031 print("End request analysis: "..self.req_len.." bytes filtered")
4032 end
4033 end
4034
4035 function Trace:http_headers(txn, http_msg)
4036 stline = http_msg:get_stline()
4037 if http_msg.channel:is_resp() then
4038 print("response:")
4039 print(stline.version.." "..stline.code.." "..stline.reason)
4040 else
4041 print("request:")
4042 print(stline.method.." "..stline.uri.." "..stline.version)
4043 end
4044
4045 for n, hdrs in pairs(http_msg:get_headers()) do
4046 for i,v in pairs(hdrs) do
4047 print(n..": "..v)
4048 end
4049 end
4050 return filter.CONTINUE
4051 end
4052
4053 function Trace:http_payload(txn, http_msg)
4054 body = http_msg:body(-20000)
4055 if http_msg.channel:is_resp() then
4056 self.res_len = self.res_len + body:len()
4057 else
4058 self.req_len = self.req_len + body:len()
4059 end
4060 end
4061
4062 core.register_filter("trace", Trace, function(trace, args)
4063 return trace
4064 end)
4065
4066..
4067
4068.. _httpmessage_class:
4069
4070HTTPMessage class
4071===================
4072
4073.. js:class:: HTTPMessage
4074
4075 **context**: filter
4076
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02004077 This class contains all functions to manipulate a HTTP message. For now, this
Christopher Faulet5a2c6612021-08-15 20:35:25 +02004078 class is only available from a filter context.
4079
4080.. js:function:: HTTPMessage.add_header(http_msg, name, value)
4081
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02004082 Appends a HTTP header field in the HTTP message **http_msg** whose name is
Christopher Faulet5a2c6612021-08-15 20:35:25 +02004083 specified in **name** and whose value is defined in **value**.
4084
4085 :param class_httpmessage http_msg: The manipulated HTTP message.
4086 :param string name: The header name.
4087 :param string value: The header value.
4088
4089.. js:function:: HTTPMessage.append(http_msg, string)
4090
4091 This function copies the string **string** at the end of incoming data of the
4092 HTTP message **http_msg**. The function returns the copied length on success
4093 or -1 if data cannot be copied.
4094
4095 Same that :js:func:`HTTPMessage.insert(http_msg, string, http_msg:input())`.
4096
4097 :param class_httpmessage http_msg: The manipulated HTTP message.
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02004098 :param string string: The data to copy at the end of incoming data.
Christopher Faulet5a2c6612021-08-15 20:35:25 +02004099 :returns: an integer containing the amount of bytes copied or -1.
4100
4101.. js:function:: HTTPMessage.body(http_msgl[, offset[, length]])
4102
4103 This function returns **length** bytes of incoming data from the HTTP message
4104 **http_msg**, starting at the offset **offset**. The data are not removed from
4105 the buffer.
4106
4107 By default, if no length is provided, all incoming data found, starting at the
4108 given offset, are returned. If **length** is set to -1, the function tries to
4109 retrieve a maximum of data. Because it is called in the filter context, it
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02004110 never yield. Not providing an offset is the same as setting it to 0. A
Ilya Shipitsinff0f2782021-08-22 22:18:07 +05004111 positive offset is relative to the beginning of incoming data of the
Christopher Faulet5a2c6612021-08-15 20:35:25 +02004112 http_message buffer while negative offset is relative to their end.
4113
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02004114 If there is no incoming data and the HTTP message can't receive more data,
4115 a 'nil' value is returned.
Christopher Faulet5a2c6612021-08-15 20:35:25 +02004116
4117 :param class_httpmessage http_msg: The manipulated HTTP message.
4118 :param integer offset: *optional* The offset in incoming data to start to get
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02004119 data. 0 by default. May be negative to be relative to the end of incoming
4120 data.
4121 :param integer length: *optional* The expected length of data to retrieve.
4122 All incoming data by default. May be set to -1 to get a maximum of data.
Christopher Faulet5a2c6612021-08-15 20:35:25 +02004123 :returns: a string containing the data found or nil.
4124
4125.. js:function:: HTTPMessage.eom(http_msg)
4126
4127 This function returns true if the end of message is reached for the HTTP
4128 message **http_msg**.
4129
4130 :param class_httpmessage http_msg: The manipulated HTTP message.
4131 :returns: an integer containing the amount of available bytes.
4132
4133.. js:function:: HTTPMessage.del_header(http_msg, name)
4134
4135 Removes all HTTP header fields in the HTTP message **http_msg** whose name is
4136 specified in **name**.
4137
4138 :param class_httpmessage http_msg: The manipulated http message.
4139 :param string name: The header name.
4140
4141.. js:function:: HTTPMessage.get_headers(http_msg)
4142
4143 Returns a table containing all the headers of the HTTP message **http_msg**.
4144
4145 :param class_httpmessage http_msg: The manipulated http message.
4146 :returns: table of headers.
4147
4148 This is the form of the returned table:
4149
4150.. code-block:: lua
4151
4152 http_msg:get_headers()['<header-name>'][<header-index>] = "<header-value>"
4153
4154 local hdr = http_msg:get_headers()
4155 hdr["host"][0] = "www.test.com"
4156 hdr["accept"][0] = "audio/basic q=1"
4157 hdr["accept"][1] = "audio/*, q=0.2"
4158 hdr["accept"][2] = "*.*, q=0.1"
4159..
4160
4161.. js:function:: HTTPMessage.get_stline(http_msg)
4162
4163 Returns a table containing the start-line of the HTTP message **http_msg**.
4164
4165 :param class_httpmessage http_msg: The manipulated http message.
4166 :returns: the start-line.
4167
4168 This is the form of the returned table:
4169
4170.. code-block:: lua
4171
4172 -- for the request :
4173 {"method" = string, "uri" = string, "version" = string}
4174
4175 -- for the response:
4176 {"version" = string, "code" = string, "reason" = string}
4177..
4178
4179.. js:function:: HTTPMessage.forward(http_msg, length)
4180
4181 This function forwards **length** bytes of data from the HTTP message
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02004182 **http_msg**. Because it is called in the filter context, it never yields. Only
Christopher Faulet5a2c6612021-08-15 20:35:25 +02004183 available incoming data may be forwarded, event if the requested length
4184 exceeds the available amount of incoming data. It returns the amount of data
4185 forwarded.
4186
4187 :param class_httpmessage http_msg: The manipulated HTTP message.
4188 :param integer int: The amount of data to forward.
4189
4190.. js:function:: HTTPMessage.input(http_msg)
4191
4192 This function returns the length of incoming data in the HTTP message
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02004193 **http_msg** from the filter point of view.
Christopher Faulet5a2c6612021-08-15 20:35:25 +02004194
4195 :param class_httpmessage http_msg: The manipulated HTTP message.
4196 :returns: an integer containing the amount of available bytes.
4197
4198.. js:function:: HTTPMessage.insert(http_msg, string[, offset])
4199
4200 This function copies the string **string** at the offset **offset** in
4201 incoming data of the HTTP message **http_msg**. The function returns the
4202 copied length on success or -1 if data cannot be copied.
4203
4204 By default, if no offset is provided, the string is copied in front of
Ilya Shipitsinff0f2782021-08-22 22:18:07 +05004205 incoming data. A positive offset is relative to the beginning of incoming data
Christopher Faulet5a2c6612021-08-15 20:35:25 +02004206 of the HTTP message while negative offset is relative to their end.
4207
4208 :param class_httpmessage http_msg: The manipulated HTTP message.
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02004209 :param string string: The data to copy into incoming data.
4210 :param integer offset: *optional* The offset in incoming data where to copy
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02004211 data. 0 by default. May be negative to be relative to the end of incoming
4212 data.
Christopher Faulet5a2c6612021-08-15 20:35:25 +02004213 :returns: an integer containing the amount of bytes copied or -1.
4214
4215.. js:function:: HTTPMessage.is_full(http_msg)
4216
4217 This function returns true if the HTTP message **http_msg** is full.
4218
4219 :param class_httpmessage http_msg: The manipulated HTTP message.
4220 :returns: a boolean
4221
4222.. js:function:: HTTPMessage.is_resp(http_msg)
4223
4224 This function returns true if the HTTP message **http_msg** is the response
4225 one.
4226
4227 :param class_httpmessage http_msg: The manipulated HTTP message.
4228 :returns: a boolean
4229
4230.. js:function:: HTTPMessage.may_recv(http_msg)
4231
4232 This function returns true if the HTTP message **http_msg** may still receive
4233 data.
4234
4235 :param class_httpmessage http_msg: The manipulated HTTP message.
4236 :returns: a boolean
4237
4238.. js:function:: HTTPMessage.output(http_msg)
4239
4240 This function returns the length of outgoing data of the HTTP message
4241 **http_msg**.
4242
4243 :param class_httpmessage http_msg: The manipulated HTTP message.
4244 :returns: an integer containing the amount of available bytes.
4245
4246.. js:function:: HTTPMessage.prepend(http_msg, string)
4247
4248 This function copies the string **string** in front of incoming data of the
4249 HTTP message **http_msg**. The function returns the copied length on success
4250 or -1 if data cannot be copied.
4251
4252 Same that :js:func:`HTTPMessage.insert(http_msg, string, 0)`.
4253
4254 :param class_httpmessage http_msg: The manipulated HTTP message.
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02004255 :param string string: The data to copy in front of incoming data.
Christopher Faulet5a2c6612021-08-15 20:35:25 +02004256 :returns: an integer containing the amount of bytes copied or -1.
4257
4258.. js:function:: HTTPMessage.remove(http_msg[, offset[, length]])
4259
4260 This function removes **length** bytes of incoming data of the HTTP message
4261 **http_msg**, starting at offset **offset**. This function returns number of
4262 bytes removed on success.
4263
4264 By default, if no length is provided, all incoming data, starting at the given
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02004265 offset, are removed. Not providing an offset is the same that setting it
Ilya Shipitsinff0f2782021-08-22 22:18:07 +05004266 to 0. A positive offset is relative to the beginning of incoming data of the
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02004267 HTTP message while negative offset is relative to the end.
Christopher Faulet5a2c6612021-08-15 20:35:25 +02004268
4269 :param class_httpmessage http_msg: The manipulated HTTP message.
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02004270 :param integer offset: *optional* The offset in incoming data where to start
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02004271 to remove data. 0 by default. May be negative to be relative to the end of
4272 incoming data.
Christopher Faulet5a2c6612021-08-15 20:35:25 +02004273 :param integer length: *optional* The length of data to remove. All incoming
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02004274 data by default.
Christopher Faulet5a2c6612021-08-15 20:35:25 +02004275 :returns: an integer containing the amount of bytes removed.
4276
4277.. js:function:: HTTPMessage.rep_header(http_msg, name, regex, replace)
4278
4279 Matches the regular expression in all occurrences of header field **name**
4280 according to regex **regex**, and replaces them with the string **replace**.
4281 The replacement value can contain back references like \1, \2, ... This
4282 function acts on whole header lines, regardless of the number of values they
4283 may contain.
4284
4285 :param class_httpmessage http_msg: The manipulated HTTP message.
4286 :param string name: The header name.
4287 :param string regex: The match regular expression.
4288 :param string replace: The replacement value.
4289
4290.. js:function:: HTTPMessage.rep_value(http_msg, name, regex, replace)
4291
4292 Matches the regular expression on every comma-delimited value of header field
4293 **name** according to regex **regex**, and replaces them with the string
4294 **replace**. The replacement value can contain back references like \1, \2,
4295 ...
4296
4297 :param class_httpmessage http_msg: The manipulated HTTP message.
4298 :param string name: The header name.
4299 :param string regex: The match regular expression.
4300 :param string replace: The replacement value.
4301
4302.. js:function:: HTTPMessage.send(http_msg, string)
4303
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02004304 This function requires immediate send of the string **string**. It means the
Ilya Shipitsinff0f2782021-08-22 22:18:07 +05004305 string is copied at the beginning of incoming data of the HTTP message
Christopher Faulet5a2c6612021-08-15 20:35:25 +02004306 **http_msg** and immediately forwarded. Because it is called in the filter
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02004307 context, it never yields.
Christopher Faulet5a2c6612021-08-15 20:35:25 +02004308
4309 :param class_httpmessage http_msg: The manipulated HTTP message.
4310 :param string string: The data to send.
4311 :returns: an integer containing the amount of bytes copied or -1.
4312
4313.. js:function:: HTTPMessage.set(http_msg, string[, offset[, length]])
4314
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02004315 This function replaces **length** bytes of incoming data of the HTTP message
Christopher Faulet5a2c6612021-08-15 20:35:25 +02004316 **http_msg**, starting at offset **offset**, by the string **string**. The
4317 function returns the copied length on success or -1 if data cannot be copied.
4318
4319 By default, if no length is provided, all incoming data, starting at the given
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02004320 offset, are replaced. Not providing an offset is the same as setting it
Ilya Shipitsinff0f2782021-08-22 22:18:07 +05004321 to 0. A positive offset is relative to the beginning of incoming data of the
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02004322 HTTP message while negative offset is relative to the end.
Christopher Faulet5a2c6612021-08-15 20:35:25 +02004323
4324 :param class_httpmessage http_msg: The manipulated HTTP message.
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02004325 :param string string: The data to copy into incoming data.
4326 :param integer offset: *optional* The offset in incoming data where to start
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02004327 the data replacement. 0 by default. May be negative to be relative to the
4328 end of incoming data.
Christopher Faulet5a2c6612021-08-15 20:35:25 +02004329 :param integer length: *optional* The length of data to replace. All incoming
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02004330 data by default.
Christopher Faulet5a2c6612021-08-15 20:35:25 +02004331 :returns: an integer containing the amount of bytes copied or -1.
4332
4333.. js:function:: HTTPMessage.set_eom(http_msg)
4334
4335 This function set the end of message for the HTTP message **http_msg**.
4336
4337 :param class_httpmessage http_msg: The manipulated HTTP message.
4338
4339.. js:function:: HTTPMessage.set_header(http_msg, name, value)
4340
4341 This variable replace all occurrence of all header matching the name **name**,
4342 by only one containing the value **value**.
4343
4344 :param class_httpmessage http_msg: The manipulated HTTP message.
4345 :param string name: The header name.
4346 :param string value: The header value.
4347
4348 This function does the same work as the following code:
4349
4350.. code-block:: lua
4351
4352 http_msg:del_header("header")
4353 http_msg:add_header("header", "value")
4354..
4355
4356.. js:function:: HTTPMessage.set_method(http_msg, method)
4357
4358 Rewrites the request method with the string **method**. The HTTP message
4359 **http_msg** must be the request.
4360
4361 :param class_httpmessage http_msg: The manipulated HTTP message.
4362 :param string method: The new method.
4363
4364.. js:function:: HTTPMessage.set_path(http_msg, path)
4365
4366 Rewrites the request path with the string **path**. The HTTP message
4367 **http_msg** must be the request.
4368
4369 :param class_httpmessage http_msg: The manipulated HTTP message.
4370 :param string method: The new method.
4371
4372.. js:function:: HTTPMessage.set_query(http_msg, query)
4373
4374 Rewrites the request's query string which appears after the first question
4375 mark ("?") with the string **query**. The HTTP message **http_msg** must be
4376 the request.
4377
4378 :param class_httpmessage http_msg: The manipulated HTTP message.
4379 :param string query: The new query.
4380
4381.. js:function:: HTTPMessage.set_status(http_msg, status[, reason])
4382
Ilya Shipitsinff0f2782021-08-22 22:18:07 +05004383 Rewrites the response status code with the integer **code** and optional the
Christopher Faulet5a2c6612021-08-15 20:35:25 +02004384 reason **reason**. If no custom reason is provided, it will be generated from
4385 the status. The HTTP message **http_msg** must be the response.
4386
4387 :param class_httpmessage http_msg: The manipulated HTTP message.
4388 :param integer status: The new response status code.
4389 :param string reason: The new response reason (optional).
4390
4391.. js:function:: HTTPMessage.set_uri(http_msg, uri)
4392
4393 Rewrites the request URI with the string **uri**. The HTTP message
4394 **http_msg** must be the request.
4395
4396 :param class_httpmessage http_msg: The manipulated HTTP message.
4397 :param string uri: The new uri.
4398
4399.. js:function:: HTTPMessage.unset_eom(http_msg)
4400
4401 This function remove the end of message for the HTTP message **http_msg**.
4402
4403 :param class_httpmessage http_msg: The manipulated HTTP message.
4404
William Lallemand10cea5c2022-03-30 16:02:43 +02004405.. _CertCache_class:
4406
4407CertCache class
4408================
4409
4410.. js:class:: CertCache
4411
4412 This class allows to update an SSL certificate file in the memory of the
4413 current HAProxy process. It will do the same as "set ssl cert" + "commit ssl
4414 cert" over the HAProxy CLI.
4415
4416.. js:function:: CertCache.set(certificate)
4417
4418 This function updates a certificate in memory.
4419
4420 :param table certificate: A table containing the fields to update.
4421 :param string certificate.filename: The mandatory filename of the certificate
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02004422 to update, it must already exist in memory.
William Lallemand10cea5c2022-03-30 16:02:43 +02004423 :param string certificate.crt: A certificate in the PEM format. It can also
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02004424 contain a private key.
William Lallemand10cea5c2022-03-30 16:02:43 +02004425 :param string certificate.key: A private key in the PEM format.
4426 :param string certificate.ocsp: An OCSP response in base64. (cf management.txt)
4427 :param string certificate.issuer: The certificate of the OCSP issuer.
4428 :param string certificate.sctl: An SCTL file.
4429
4430.. code-block:: lua
4431
4432 CertCache.set{filename="certs/localhost9994.pem.rsa", crt=crt}
4433
4434
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01004435External Lua libraries
4436======================
4437
4438A lot of useful lua libraries can be found here:
4439
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02004440* Lua toolbox has been superseded by
4441 `https://luarocks.org/ <https://luarocks.org/>`_
4442
4443 The old lua toolbox source code is still available here
4444 `https://github.com/catwell/lua-toolbox <https://github.com/catwell/lua-toolbox>`_ (DEPRECATED)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01004445
Ilya Shipitsin2075ca82020-03-06 23:22:22 +05004446Redis client library:
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01004447
4448* `https://github.com/nrk/redis-lua <https://github.com/nrk/redis-lua>`_
4449
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02004450This is an example about the usage of the Redis library within HAProxy.
4451Note that each call to any function of this library can throw an error if
4452the socket connection fails.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01004453
4454.. code-block:: lua
4455
4456 -- load the redis library
4457 local redis = require("redis");
4458
4459 function do_something(txn)
4460
4461 -- create and connect new tcp socket
4462 local tcp = core.tcp();
4463 tcp:settimeout(1);
4464 tcp:connect("127.0.0.1", 6379);
4465
4466 -- use the redis library with this new socket
4467 local client = redis.connect({socket=tcp});
4468 client:ping();
4469
4470 end
4471
4472OpenSSL:
4473
4474* `http://mkottman.github.io/luacrypto/index.html
4475 <http://mkottman.github.io/luacrypto/index.html>`_
4476
4477* `https://github.com/brunoos/luasec/wiki
4478 <https://github.com/brunoos/luasec/wiki>`_