blob: c635741ab95a8048bd48e2447badc264bde61aae [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
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100270 configuration file, on the default syslog server if it is configured and on
271 the stderr if it is allowed.
272
Bertrand Jacquin874a35c2018-09-10 21:26:07 +0100273 :param integer loglevel: Is the log level associated with the message. It is a
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +0200274 number between 0 and 7.
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100275 :param string msg: The log content.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +0100276 :see: :js:attr:`core.emerg`, :js:attr:`core.alert`, :js:attr:`core.crit`,
277 :js:attr:`core.err`, :js:attr:`core.warning`, :js:attr:`core.notice`,
278 :js:attr:`core.info`, :js:attr:`core.debug` (log level definitions)
279 :see: :js:func:`core.Debug`
280 :see: :js:func:`core.Info`
281 :see: :js:func:`core.Warning`
282 :see: :js:func:`core.Alert`
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100283
284.. js:function:: core.Debug(msg)
285
286 **context**: body, init, task, action, sample-fetch, converter
287
288 :param string msg: The log content.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +0100289 :see: :js:func:`core.log`
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100290
291 Does the same job than:
292
293.. code-block:: lua
294
Thierry FOURNIERdc595002015-12-21 11:13:52 +0100295 function Debug(msg)
296 core.log(core.debug, msg)
297 end
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100298..
299
300.. js:function:: core.Info(msg)
301
302 **context**: body, init, task, action, sample-fetch, converter
303
304 :param string msg: The log content.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +0100305 :see: :js:func:`core.log`
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100306
307.. code-block:: lua
308
Thierry FOURNIERdc595002015-12-21 11:13:52 +0100309 function Info(msg)
310 core.log(core.info, msg)
311 end
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100312..
313
314.. js:function:: core.Warning(msg)
315
316 **context**: body, init, task, action, sample-fetch, converter
317
318 :param string msg: The log content.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +0100319 :see: :js:func:`core.log`
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100320
321.. code-block:: lua
322
Thierry FOURNIERdc595002015-12-21 11:13:52 +0100323 function Warning(msg)
324 core.log(core.warning, msg)
325 end
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100326..
327
328.. js:function:: core.Alert(msg)
329
330 **context**: body, init, task, action, sample-fetch, converter
331
332 :param string msg: The log content.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +0100333 :see: :js:func:`core.log`
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100334
335.. code-block:: lua
336
Thierry FOURNIERdc595002015-12-21 11:13:52 +0100337 function Alert(msg)
338 core.log(core.alert, msg)
339 end
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100340..
341
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100342.. js:function:: core.add_acl(filename, key)
343
344 **context**: init, task, action, sample-fetch, converter
345
346 Add the ACL *key* in the ACLs list referenced by the file *filename*.
347
348 :param string filename: the filename that reference the ACL entries.
349 :param string key: the key which will be added.
350
351.. js:function:: core.del_acl(filename, key)
352
353 **context**: init, task, action, sample-fetch, converter
354
355 Delete the ACL entry referenced by the key *key* in the list of ACLs
356 referenced by *filename*.
357
358 :param string filename: the filename that reference the ACL entries.
359 :param string key: the key which will be deleted.
360
361.. js:function:: core.del_map(filename, key)
362
363 **context**: init, task, action, sample-fetch, converter
364
365 Delete the map entry indexed with the specified key in the list of maps
366 referenced by his filename.
367
368 :param string filename: the filename that reference the map entries.
369 :param string key: the key which will be deleted.
370
Thierry Fourniereea77c02016-03-18 08:47:13 +0100371.. js:function:: core.get_info()
372
373 **context**: body, init, task, action, sample-fetch, converter
374
Aurelien DARRAGON53901f42022-10-13 19:49:42 +0200375 Returns HAProxy core information. We can find information like the uptime,
Thierry Fourniereea77c02016-03-18 08:47:13 +0100376 the pid, memory pool usage, tasks number, ...
377
Ilya Shipitsin5fa29b82022-12-07 09:46:19 +0500378 This information is also returned by the management socket via the command
Bertrand Jacquin874a35c2018-09-10 21:26:07 +0100379 "show info". See the management socket documentation for more information
Thierry Fourniereea77c02016-03-18 08:47:13 +0100380 about the content of these variables.
381
382 :returns: an array of values.
383
Thierry Fournierb1f46562016-01-21 09:46:15 +0100384.. js:function:: core.now()
385
386 **context**: body, init, task, action
387
388 This function returns the current time. The time returned is fixed by the
Bertrand Jacquin874a35c2018-09-10 21:26:07 +0100389 HAProxy core and assures than the hour will be monotonic and that the system
Thierry Fournierb1f46562016-01-21 09:46:15 +0100390 call 'gettimeofday' will not be called too. The time is refreshed between each
391 Lua execution or resume, so two consecutive call to the function "now" will
392 probably returns the same result.
393
Patrick Hemmerc6a1d712018-05-01 21:30:41 -0400394 :returns: a table which contains two entries "sec" and "usec". "sec"
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +0200395 contains the current at the epoch format, and "usec" contains the
396 current microseconds.
Thierry Fournierb1f46562016-01-21 09:46:15 +0100397
Thierry FOURNIERa78f0372016-12-14 19:04:41 +0100398.. js:function:: core.http_date(date)
399
400 **context**: body, init, task, action
401
Bertrand Jacquin874a35c2018-09-10 21:26:07 +0100402 This function take a string representing http date, and returns an integer
Thierry FOURNIERa78f0372016-12-14 19:04:41 +0100403 containing the corresponding date with a epoch format. A valid http date
404 me respect the format IMF, RFC850 or ASCTIME.
405
406 :param string date: a date http-date formatted
407 :returns: integer containing epoch date
408 :see: :js:func:`core.imf_date`.
409 :see: :js:func:`core.rfc850_date`.
410 :see: :js:func:`core.asctime_date`.
411 :see: https://tools.ietf.org/html/rfc7231#section-7.1.1.1
412
413.. js:function:: core.imf_date(date)
414
415 **context**: body, init, task, action
416
Bertrand Jacquin874a35c2018-09-10 21:26:07 +0100417 This function take a string representing IMF date, and returns an integer
Thierry FOURNIERa78f0372016-12-14 19:04:41 +0100418 containing the corresponding date with a epoch format.
419
420 :param string date: a date IMF formatted
421 :returns: integer containing epoch date
422 :see: https://tools.ietf.org/html/rfc7231#section-7.1.1.1
423
424 The IMF format is like this:
425
426.. code-block:: text
427
428 Sun, 06 Nov 1994 08:49:37 GMT
429..
430
431.. js:function:: core.rfc850_date(date)
432
433 **context**: body, init, task, action
434
Bertrand Jacquin874a35c2018-09-10 21:26:07 +0100435 This function take a string representing RFC850 date, and returns an integer
Thierry FOURNIERa78f0372016-12-14 19:04:41 +0100436 containing the corresponding date with a epoch format.
437
438 :param string date: a date RFC859 formatted
439 :returns: integer containing epoch date
440 :see: https://tools.ietf.org/html/rfc7231#section-7.1.1.1
441
442 The RFC850 format is like this:
443
444.. code-block:: text
445
446 Sunday, 06-Nov-94 08:49:37 GMT
447..
448
449.. js:function:: core.asctime_date(date)
450
451 **context**: body, init, task, action
452
Bertrand Jacquin874a35c2018-09-10 21:26:07 +0100453 This function take a string representing ASCTIME date, and returns an integer
Thierry FOURNIERa78f0372016-12-14 19:04:41 +0100454 containing the corresponding date with a epoch format.
455
456 :param string date: a date ASCTIME formatted
457 :returns: integer containing epoch date
458 :see: https://tools.ietf.org/html/rfc7231#section-7.1.1.1
459
460 The ASCTIME format is like this:
461
462.. code-block:: text
463
464 Sun Nov 6 08:49:37 1994
465..
466
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100467.. js:function:: core.msleep(milliseconds)
468
469 **context**: body, init, task, action
470
471 The `core.msleep()` stops the Lua execution between specified milliseconds.
472
473 :param integer milliseconds: the required milliseconds.
474
Thierry FOURNIERc5d11c62018-02-12 14:46:54 +0100475.. js:function:: core.register_action(name, actions, func [, nb_args])
Thierry FOURNIER8255a752015-09-23 21:03:35 +0200476
477 **context**: body
478
David Carlier61fdf8b2015-10-02 11:59:38 +0100479 Register a Lua function executed as action. All the registered action can be
Thierry FOURNIER8255a752015-09-23 21:03:35 +0200480 used in HAProxy with the prefix "lua.". An action gets a TXN object class as
481 input.
482
483 :param string name: is the name of the converter.
Willy Tarreau61add3c2015-09-28 15:39:10 +0200484 :param table actions: is a table of string describing the HAProxy actions who
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +0200485 want to register to. The expected actions are 'tcp-req', 'tcp-res', 'http-req'
486 or 'http-res'.
Aurelien DARRAGON53901f42022-10-13 19:49:42 +0200487 :param function func: is the Lua function called to work as converter.
Thierry FOURNIERc5d11c62018-02-12 14:46:54 +0100488 :param integer nb_args: is the expected number of argument for the action.
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +0200489 By default the value is 0.
Thierry FOURNIER8255a752015-09-23 21:03:35 +0200490
491 The prototype of the Lua function used as argument is:
492
493.. code-block:: lua
494
Thierry FOURNIERc5d11c62018-02-12 14:46:54 +0100495 function(txn [, arg1 [, arg2]])
Thierry FOURNIER8255a752015-09-23 21:03:35 +0200496..
497
Thierry FOURNIERdc595002015-12-21 11:13:52 +0100498 * **txn** (:ref:`txn_class`): this is a TXN object used for manipulating the
Thierry FOURNIER8255a752015-09-23 21:03:35 +0200499 current request or TCP stream.
500
Bertrand Jacquin874a35c2018-09-10 21:26:07 +0100501 * **argX**: this is argument provided through the HAProxy configuration file.
Thierry FOURNIERc5d11c62018-02-12 14:46:54 +0100502
Bertrand Jacquin874a35c2018-09-10 21:26:07 +0100503 Here, an example of action registration. The action just send an 'Hello world'
Thierry FOURNIER8255a752015-09-23 21:03:35 +0200504 in the logs.
505
506.. code-block:: lua
507
508 core.register_action("hello-world", { "tcp-req", "http-req" }, function(txn)
509 txn:Info("Hello world")
510 end)
511..
512
Willy Tarreau714f3452021-05-09 06:47:26 +0200513 This example code is used in HAProxy configuration like this:
Thierry FOURNIER8255a752015-09-23 21:03:35 +0200514
515::
516
517 frontend tcp_frt
518 mode tcp
519 tcp-request content lua.hello-world
520
521 frontend http_frt
522 mode http
523 http-request lua.hello-world
Aurelien DARRAGONd5c80d72023-03-13 19:36:13 +0100524
Thierry FOURNIERc5d11c62018-02-12 14:46:54 +0100525..
526
Bertrand Jacquin874a35c2018-09-10 21:26:07 +0100527 A second example using arguments
Thierry FOURNIERc5d11c62018-02-12 14:46:54 +0100528
529.. code-block:: lua
530
531 function hello_world(txn, arg)
532 txn:Info("Hello world for " .. arg)
533 end
534 core.register_action("hello-world", { "tcp-req", "http-req" }, hello_world, 2)
Aurelien DARRAGONd5c80d72023-03-13 19:36:13 +0100535
Thierry FOURNIERc5d11c62018-02-12 14:46:54 +0100536..
Thierry FOURNIER8255a752015-09-23 21:03:35 +0200537
Willy Tarreau714f3452021-05-09 06:47:26 +0200538 This example code is used in HAProxy configuration like this:
Thierry FOURNIERc5d11c62018-02-12 14:46:54 +0100539
540::
541
542 frontend tcp_frt
543 mode tcp
544 tcp-request content lua.hello-world everybody
Aurelien DARRAGONd5c80d72023-03-13 19:36:13 +0100545
Thierry FOURNIERc5d11c62018-02-12 14:46:54 +0100546..
Aurelien DARRAGON53901f42022-10-13 19:49:42 +0200547
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100548.. js:function:: core.register_converters(name, func)
549
550 **context**: body
551
David Carlier61fdf8b2015-10-02 11:59:38 +0100552 Register a Lua function executed as converter. All the registered converters
Aurelien DARRAGON53901f42022-10-13 19:49:42 +0200553 can be used in HAProxy with the prefix "lua.". A converter gets a string as
554 input and returns a string as output. The registered function can take up to 9
555 values as parameter. All the values are strings.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100556
557 :param string name: is the name of the converter.
558 :param function func: is the Lua function called to work as converter.
559
560 The prototype of the Lua function used as argument is:
561
562.. code-block:: lua
563
564 function(str, [p1 [, p2 [, ... [, p5]]]])
565..
566
567 * **str** (*string*): this is the input value automatically converted in
568 string.
569 * **p1** .. **p5** (*string*): this is a list of string arguments declared in
Bertrand Jacquin874a35c2018-09-10 21:26:07 +0100570 the HAProxy configuration file. The number of arguments doesn't exceed 5.
Aurelien DARRAGON53901f42022-10-13 19:49:42 +0200571 The order and the nature of these is conventionally chosen by the
Bertrand Jacquin874a35c2018-09-10 21:26:07 +0100572 developer.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100573
574.. js:function:: core.register_fetches(name, func)
575
576 **context**: body
577
David Carlier61fdf8b2015-10-02 11:59:38 +0100578 Register a Lua function executed as sample fetch. All the registered sample
Bertrand Jacquin874a35c2018-09-10 21:26:07 +0100579 fetch can be used in HAProxy with the prefix "lua.". A Lua sample fetch
Aurelien DARRAGON53901f42022-10-13 19:49:42 +0200580 returns a string as output. The registered function can take up to 9 values as
581 parameter. All the values are strings.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100582
Aurelien DARRAGON53901f42022-10-13 19:49:42 +0200583 :param string name: is the name of the sample fetch.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100584 :param function func: is the Lua function called to work as sample fetch.
585
586 The prototype of the Lua function used as argument is:
587
588.. code-block:: lua
589
590 string function(txn, [p1 [, p2 [, ... [, p5]]]])
591..
592
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +0200593 * **txn** (:ref:`txn_class`): this is the txn object associated with the
594 current request.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100595 * **p1** .. **p5** (*string*): this is a list of string arguments declared in
Bertrand Jacquin874a35c2018-09-10 21:26:07 +0100596 the HAProxy configuration file. The number of arguments doesn't exceed 5.
Aurelien DARRAGON53901f42022-10-13 19:49:42 +0200597 The order and the nature of these is conventionally chosen by the
Bertrand Jacquin874a35c2018-09-10 21:26:07 +0100598 developer.
599 * **Returns**: A string containing some data, or nil if the value cannot be
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100600 returned now.
601
602 lua example code:
603
604.. code-block:: lua
605
606 core.register_fetches("hello", function(txn)
607 return "hello"
608 end)
609..
610
611 HAProxy example configuration:
612
613::
614
615 frontend example
616 http-request redirect location /%[lua.hello]
617
Christopher Faulet5a2c6612021-08-15 20:35:25 +0200618.. js:function:: core.register_filter(name, Flt, func)
619
620 **context**: body
621
622 Register a Lua function used to declare a filter. All the registered filters
623 can by used in HAProxy with the prefix "lua.".
624
625 :param string name: is the name of the filter.
626 :param table Flt: is a Lua class containing the filter definition (id, flags,
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +0200627 callbacks).
Christopher Faulet5a2c6612021-08-15 20:35:25 +0200628 :param function func: is the Lua function called to create the Lua filter.
629
630 The prototype of the Lua function used as argument is:
631
632.. code-block:: lua
633
634 function(flt, args)
635..
636
637 * **flt** : Is a filter object based on the class provided in
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +0200638 :js:func:`core.register_filter()` function.
Christopher Faulet5a2c6612021-08-15 20:35:25 +0200639
640 * **args**: Is a table of strings containing all arguments provided through
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +0200641 the HAProxy configuration file, on the filter line.
Christopher Faulet5a2c6612021-08-15 20:35:25 +0200642
643 It must return the filter to use or nil to ignore it. Here, an example of
644 filter registration.
645
646.. code-block:: lua
647
648 core.register_filter("my-filter", MyFilter, function(flt, args)
649 flt.args = args -- Save arguments
650 return flt
651 end)
652..
653
654 This example code is used in HAProxy configuration like this:
655
656::
657
658 frontend http
659 mode http
660 filter lua.my-filter arg1 arg2 arg3
Aurelien DARRAGONd5c80d72023-03-13 19:36:13 +0100661
Christopher Faulet5a2c6612021-08-15 20:35:25 +0200662..
663
664 :see: :js:class:`Filter`
665
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +0200666.. js:function:: core.register_service(name, mode, func)
667
668 **context**: body
669
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +0200670 Register a Lua function executed as a service. All the registered services
671 can be used in HAProxy with the prefix "lua.". A service gets an object class
672 as input according with the required mode.
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +0200673
Aurelien DARRAGON53901f42022-10-13 19:49:42 +0200674 :param string name: is the name of the service.
Willy Tarreau61add3c2015-09-28 15:39:10 +0200675 :param string mode: is string describing the required mode. Only 'tcp' or
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +0200676 'http' are allowed.
Aurelien DARRAGON53901f42022-10-13 19:49:42 +0200677 :param function func: is the Lua function called to work as service.
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +0200678
679 The prototype of the Lua function used as argument is:
680
681.. code-block:: lua
682
683 function(applet)
684..
685
Thierry FOURNIERdc595002015-12-21 11:13:52 +0100686 * **applet** *applet* will be a :ref:`applettcp_class` or a
687 :ref:`applethttp_class`. It depends the type of registered applet. An applet
688 registered with the 'http' value for the *mode* parameter will gets a
689 :ref:`applethttp_class`. If the *mode* value is 'tcp', the applet will gets
690 a :ref:`applettcp_class`.
691
Christopher Faulet1e9b1b62021-08-11 10:14:30 +0200692 .. warning::
693 Applets of type 'http' cannot be called from 'tcp-*' rulesets. Only the
694 'http-*' rulesets are authorized, this means that is not possible to call
Aurelien DARRAGON53901f42022-10-13 19:49:42 +0200695 a HTTP applet from a proxy in tcp mode. Applets of type 'tcp' can be
Christopher Faulet1e9b1b62021-08-11 10:14:30 +0200696 called from anywhere.
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +0200697
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +0200698 Here, an example of service registration. The service just send an
699 'Hello world' as an http response.
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +0200700
701.. code-block:: lua
702
Pieter Baauw4d7f7662015-11-08 16:38:08 +0100703 core.register_service("hello-world", "http", function(applet)
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +0200704 local response = "Hello World !"
705 applet:set_status(200)
Pieter Baauw2dcb9bc2015-10-01 22:47:12 +0200706 applet:add_header("content-length", string.len(response))
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +0200707 applet:add_header("content-type", "text/plain")
Pieter Baauw2dcb9bc2015-10-01 22:47:12 +0200708 applet:start_response()
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +0200709 applet:send(response)
710 end)
711..
712
Willy Tarreau714f3452021-05-09 06:47:26 +0200713 This example code is used in HAProxy configuration like this:
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +0200714
715::
716
717 frontend example
718 http-request use-service lua.hello-world
719
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100720.. js:function:: core.register_init(func)
721
722 **context**: body
723
724 Register a function executed after the configuration parsing. This is useful
725 to check any parameters.
726
Pieter Baauw4d7f7662015-11-08 16:38:08 +0100727 :param function func: is the Lua function called to work as initializer.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100728
729 The prototype of the Lua function used as argument is:
730
731.. code-block:: lua
732
733 function()
734..
735
736 It takes no input, and no output is expected.
737
Aurelien DARRAGONb8038992023-03-09 16:48:30 +0100738.. js:function:: core.register_task(func[, arg1[, arg2[, ...[, arg4]]]])
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100739
Aurelien DARRAGONc84899c2023-02-20 18:18:59 +0100740 **context**: body, init, task, action, sample-fetch, converter, event
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100741
742 Register and start independent task. The task is started when the HAProxy
743 main scheduler starts. For example this type of tasks can be executed to
Thierry FOURNIER2e4893c2015-03-18 13:37:27 +0100744 perform complex health checks.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100745
Aurelien DARRAGONb8038992023-03-09 16:48:30 +0100746 :param function func: is the Lua function called to work as an async task.
747
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +0200748 Up to 4 optional arguments (all types supported) may be passed to the
749 function. (They will be passed as-is to the task function)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100750
751 The prototype of the Lua function used as argument is:
752
753.. code-block:: lua
754
Aurelien DARRAGONb8038992023-03-09 16:48:30 +0100755 function([arg1[, arg2[, ...[, arg4]]]])
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100756..
757
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +0200758 It takes up to 4 optional arguments (provided when registering), and no
759 output is expected.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100760
Aurelien DARRAGON86fb22c2023-05-03 17:03:09 +0200761 See also :js:func:`core.queue` to dynamically pass data between main context
762 and tasks or even between tasks.
763
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +0100764.. js:function:: core.register_cli([path], usage, func)
765
766 **context**: body
767
Aurelien DARRAGON53901f42022-10-13 19:49:42 +0200768 Register a custom cli that will be available from haproxy stats socket.
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +0100769
770 :param array path: is the sequence of word for which the cli execute the Lua
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +0200771 binding.
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +0100772 :param string usage: is the usage message displayed in the help.
773 :param function func: is the Lua function called to handle the CLI commands.
774
775 The prototype of the Lua function used as argument is:
776
777.. code-block:: lua
778
779 function(AppletTCP, [arg1, [arg2, [...]]])
780..
781
782 I/O are managed with the :ref:`applettcp_class` object. Args are given as
Bertrand Jacquin874a35c2018-09-10 21:26:07 +0100783 parameter. The args embed the registered path. If the path is declared like
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +0100784 this:
785
786.. code-block:: lua
787
788 core.register_cli({"show", "ssl", "stats"}, "Display SSL stats..", function(applet, arg1, arg2, arg3, arg4, arg5)
789 end)
790..
791
792 And we execute this in the prompt:
793
794.. code-block:: text
795
796 > prompt
797 > show ssl stats all
798..
799
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +0200800 Then, arg1, arg2 and arg3 will contains respectively "show", "ssl" and
801 "stats".
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +0100802 arg4 will contain "all". arg5 contains nil.
803
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100804.. js:function:: core.set_nice(nice)
805
806 **context**: task, action, sample-fetch, converter
807
808 Change the nice of the current task or current session.
Thierry FOURNIER2e4893c2015-03-18 13:37:27 +0100809
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100810 :param integer nice: the nice value, it must be between -1024 and 1024.
811
812.. js:function:: core.set_map(filename, key, value)
813
814 **context**: init, task, action, sample-fetch, converter
815
Bertrand Jacquin874a35c2018-09-10 21:26:07 +0100816 Set the value *value* associated to the key *key* in the map referenced by
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100817 *filename*.
818
Thierry FOURNIERdc595002015-12-21 11:13:52 +0100819 :param string filename: the Map reference
820 :param string key: the key to set or replace
821 :param string value: the associated value
822
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100823.. js:function:: core.sleep(int seconds)
824
825 **context**: body, init, task, action
826
827 The `core.sleep()` functions stop the Lua execution between specified seconds.
828
829 :param integer seconds: the required seconds.
830
831.. js:function:: core.tcp()
832
833 **context**: init, task, action
834
835 This function returns a new object of a *socket* class.
836
Thierry FOURNIERdc595002015-12-21 11:13:52 +0100837 :returns: A :ref:`socket_class` object.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100838
William Lallemand00a15022021-11-19 16:02:44 +0100839.. js:function:: core.httpclient()
840
841 **context**: init, task, action
842
843 This function returns a new object of a *httpclient* class.
844
845 :returns: A :ref:`httpclient_class` object.
846
Thierry Fournier1de16592016-01-27 09:49:07 +0100847.. js:function:: core.concat()
848
849 **context**: body, init, task, action, sample-fetch, converter
850
Bertrand Jacquin874a35c2018-09-10 21:26:07 +0100851 This function returns a new concat object.
Thierry Fournier1de16592016-01-27 09:49:07 +0100852
853 :returns: A :ref:`concat_class` object.
854
Aurelien DARRAGON86fb22c2023-05-03 17:03:09 +0200855.. js:function:: core.queue()
856
857 **context**: body, init, task, event, action, sample-fetch, converter
858
859 This function returns a new queue object.
860
861 :returns: A :ref:`queue_class` object.
862
Thierry FOURNIER0a99b892015-08-26 00:14:17 +0200863.. js:function:: core.done(data)
864
865 **context**: body, init, task, action, sample-fetch, converter
866
867 :param any data: Return some data for the caller. It is useful with
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +0200868 sample-fetches and sample-converters.
Thierry FOURNIER0a99b892015-08-26 00:14:17 +0200869
870 Immediately stops the current Lua execution and returns to the caller which
871 may be a sample fetch, a converter or an action and returns the specified
Thierry Fournier4234dbd2020-11-28 13:18:23 +0100872 value (ignored for actions and init). It is used when the LUA process finishes
873 its work and wants to give back the control to HAProxy without executing the
Thierry FOURNIER0a99b892015-08-26 00:14:17 +0200874 remaining code. It can be seen as a multi-level "return".
875
Thierry FOURNIER486f5a02015-03-16 15:13:03 +0100876.. js:function:: core.yield()
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100877
878 **context**: task, action, sample-fetch, converter
879
880 Give back the hand at the HAProxy scheduler. It is used when the LUA
881 processing consumes a lot of processing time.
882
Thierry FOURNIER / OZON.IO62fec752016-11-10 20:38:11 +0100883.. js:function:: core.parse_addr(address)
884
885 **context**: body, init, task, action, sample-fetch, converter
886
887 :param network: is a string describing an ipv4 or ipv6 address and optionally
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +0200888 its network length, like this: "127.0.0.1/8" or "aaaa::1234/32".
Thierry FOURNIER / OZON.IO62fec752016-11-10 20:38:11 +0100889 :returns: a userdata containing network or nil if an error occurs.
890
Bertrand Jacquin874a35c2018-09-10 21:26:07 +0100891 Parse ipv4 or ipv6 addresses and its facultative associated network.
Thierry FOURNIER / OZON.IO62fec752016-11-10 20:38:11 +0100892
893.. js:function:: core.match_addr(addr1, addr2)
894
895 **context**: body, init, task, action, sample-fetch, converter
896
897 :param addr1: is an address created with "core.parse_addr".
898 :param addr2: is an address created with "core.parse_addr".
Bertrand Jacquin874a35c2018-09-10 21:26:07 +0100899 :returns: boolean, true if the network of the addresses match, else returns
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +0200900 false.
Thierry FOURNIER / OZON.IO62fec752016-11-10 20:38:11 +0100901
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +0200902 Match two networks. For example "127.0.0.1/32" matches "127.0.0.0/8". The
903 order of network is not important.
Thierry FOURNIER / OZON.IO62fec752016-11-10 20:38:11 +0100904
Thierry FOURNIER / OZON.IO8a1027a2016-11-24 20:48:38 +0100905.. js:function:: core.tokenize(str, separators [, noblank])
906
907 **context**: body, init, task, action, sample-fetch, converter
908
909 This function is useful for tokenizing an entry, or splitting some messages.
910 :param string str: The string which will be split.
911 :param string separators: A string containing a list of separators.
912 :param boolean noblank: Ignore empty entries.
913 :returns: an array of string.
914
915 For example:
916
917.. code-block:: lua
918
919 local array = core.tokenize("This function is useful, for tokenizing an entry.", "., ", true)
920 print_r(array)
921..
922
923 Returns this array:
924
925.. code-block:: text
926
927 (table) table: 0x21c01e0 [
928 1: (string) "This"
929 2: (string) "function"
930 3: (string) "is"
931 4: (string) "useful"
932 5: (string) "for"
933 6: (string) "tokenizing"
934 7: (string) "an"
935 8: (string) "entry"
936 ]
937..
938
Aurelien DARRAGONc84899c2023-02-20 18:18:59 +0100939.. js:function:: core.event_sub(event_types, func)
940
941 **context**: body, init, task, action, sample-fetch, converter
942
943 Register a function that will be called on specific system events.
944
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +0200945 :param array event_types: array of string containing the event types you want
946 to subscribe to
947 :param function func: is the Lua function called when one of the subscribed
948 events occur.
Aurelien DARRAGONc84899c2023-02-20 18:18:59 +0100949 :returns: A :ref:`event_sub_class` object.
Aurelien DARRAGON223770d2023-03-10 15:34:35 +0100950 :see: :js:func:`Server.event_sub()`.
Aurelien DARRAGONc84899c2023-02-20 18:18:59 +0100951
952 List of available event types :
953
954 **SERVER** Family:
955
956 * **SERVER_ADD**: when a server is added
957 * **SERVER_DEL**: when a server is removed
958 * **SERVER_DOWN**: when a server state goes from UP to DOWN
959 * **SERVER_UP**: when a server state goes from DOWN to UP
Aurelien DARRAGONc99f3ad2023-04-12 15:47:16 +0200960 * **SERVER_STATE**: when a server state changes
Aurelien DARRAGON948dd3d2023-04-26 11:27:09 +0200961 * **SERVER_ADMIN**: when a server administrative state changes
Aurelien DARRAGON0bd53b22023-03-30 15:53:33 +0200962 * **SERVER_CHECK**: when a server's check status change is reported.
963 Be careful when subscribing to this type since many events might be
964 generated.
Aurelien DARRAGONc84899c2023-02-20 18:18:59 +0100965
966 .. Note::
Aurelien DARRAGONc99f3ad2023-04-12 15:47:16 +0200967 Use **SERVER** in **event_types** to subscribe to all server events types
968 at once. Note that this should only be used for testing purposes since a
969 single event source could result in multiple events types being generated.
970 (e.g.: SERVER_STATE will always be generated for each SERVER_DOWN or
971 SERVER_UP)
Aurelien DARRAGONc84899c2023-02-20 18:18:59 +0100972
973 The prototype of the Lua function used as argument is:
974
975.. code-block:: lua
976
Aurelien DARRAGON096b3832023-04-20 11:32:46 +0200977 function(event, event_data, sub, when)
Aurelien DARRAGONc84899c2023-02-20 18:18:59 +0100978..
979
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +0200980 * **event** (*string*): the event type (one of the **event_types** specified
981 when subscribing)
982 * **event_data**: specific to each event family (For **SERVER** family,
983 a :ref:`server_event_class` object)
984 * **sub**: class to manage the subscription from within the event
985 (a :ref:`event_sub_class` object)
Aurelien DARRAGON096b3832023-04-20 11:32:46 +0200986 * **when**: timestamp corresponding to the date when the event was generated.
987 It is an integer representing the number of seconds elapsed since Epoch.
988 It may be provided as optional argument to `os.date()` lua function to
989 convert it to a string according to a given format string.
Aurelien DARRAGONc84899c2023-02-20 18:18:59 +0100990
991 .. Warning::
992 The callback function will only be scheduled on the very same thread that
993 performed the subscription.
994
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +0200995 Moreover, each thread treats events sequentially. It means that if you
996 have, let's say SERVER_UP followed by a SERVER_DOWN in a short timelapse,
997 then the cb function will first be called with SERVER_UP, and once it's
998 done handling the event, the cb function will be called again with
999 SERVER_DOWN.
Aurelien DARRAGONc84899c2023-02-20 18:18:59 +01001000
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02001001 This is to ensure event consistency when it comes to logging / triggering
1002 logic from lua.
Aurelien DARRAGONc84899c2023-02-20 18:18:59 +01001003
1004 Your lua cb function may yield if needed, but you're pleased to process the
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02001005 event as fast as possible to prevent the event queue from growing up,
1006 depending on the event flow that is expected for the given subscription.
Aurelien DARRAGONc84899c2023-02-20 18:18:59 +01001007
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02001008 To prevent abuses, if the event queue for the current subscription goes
1009 over a certain amount of unconsumed events, the subscription will pause
1010 itself automatically for as long as it takes for your handler to catch up.
1011 This would lead to events being missed, so an error will be reported in the
1012 logs to warn you about that.
1013 This is not something you want to let happen too often, it may indicate
1014 that you subscribed to an event that is occurring too frequently or/and
1015 that your callback function is too slow to keep up the pace and you should
1016 review it.
Aurelien DARRAGONc84899c2023-02-20 18:18:59 +01001017
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02001018 If you want to do some parallel processing because your callback functions
1019 are slow: you might want to create subtasks from lua using
1020 :js:func:`core.register_task()` from within your callback function to
1021 perform the heavy job in a dedicated task and allow remaining events to be
1022 processed more quickly.
Aurelien DARRAGONc84899c2023-02-20 18:18:59 +01001023
Aurelien DARRAGON5bed48f2023-04-21 17:32:46 +02001024.. js:function:: core.disable_legacy_mailers()
1025
1026 **LEGACY**
1027
1028 **context**: body, init
1029
1030 Disable the sending of email alerts through the legacy email sending
1031 function when mailers are used in the configuration.
1032
1033 Use this when sending email alerts directly from lua.
1034
Aurelien DARRAGON717a38d2023-04-26 19:02:43 +02001035 :see: :js:func:`Proxy.get_mailers()`
1036
Thierry Fournierf61aa632016-02-19 20:56:00 +01001037.. _proxy_class:
1038
1039Proxy class
1040============
1041
1042.. js:class:: Proxy
1043
1044 This class provides a way for manipulating proxy and retrieving information
1045 like statistics.
1046
Thierry FOURNIER817e7592017-07-24 14:35:04 +02001047.. js:attribute:: Proxy.name
1048
1049 Contain the name of the proxy.
1050
Aurelien DARRAGONc4b24372023-03-02 12:00:06 +01001051 .. warning::
1052 This attribute is now deprecated and will eventually be removed.
1053 Please use :js:func:`Proxy.get_name()` function instead.
1054
Thierry Fournierb0467732022-10-07 12:07:24 +02001055.. js:function:: Proxy.get_name()
1056
1057 Returns the name of the proxy.
1058
Baptiste Assmann46c72552017-10-26 21:51:58 +02001059.. js:attribute:: Proxy.uuid
1060
1061 Contain the unique identifier of the proxy.
1062
Aurelien DARRAGONc4b24372023-03-02 12:00:06 +01001063 .. warning::
1064 This attribute is now deprecated and will eventually be removed.
1065 Please use :js:func:`Proxy.get_uuid()` function instead.
1066
Thierry Fournierb0467732022-10-07 12:07:24 +02001067.. js:function:: Proxy.get_uuid()
1068
1069 Returns the unique identifier of the proxy.
1070
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001071.. js:attribute:: Proxy.servers
1072
Patrick Hemmerc6a1d712018-05-01 21:30:41 -04001073 Contain a table with the attached servers. The table is indexed by server
1074 name, and each server entry is an object of type :ref:`server_class`.
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001075
Adis Nezirovic8878f8e2018-07-13 12:18:33 +02001076.. js:attribute:: Proxy.stktable
1077
1078 Contains a stick table object attached to the proxy.
1079
Thierry Fournierff480422016-02-25 08:36:46 +01001080.. js:attribute:: Proxy.listeners
1081
Patrick Hemmerc6a1d712018-05-01 21:30:41 -04001082 Contain a table with the attached listeners. The table is indexed by listener
1083 name, and each each listeners entry is an object of type
1084 :ref:`listener_class`.
Thierry Fournierff480422016-02-25 08:36:46 +01001085
Thierry Fournierf61aa632016-02-19 20:56:00 +01001086.. js:function:: Proxy.pause(px)
1087
1088 Pause the proxy. See the management socket documentation for more information.
1089
1090 :param class_proxy px: A :ref:`proxy_class` which indicates the manipulated
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02001091 proxy.
Thierry Fournierf61aa632016-02-19 20:56:00 +01001092
1093.. js:function:: Proxy.resume(px)
1094
1095 Resume the proxy. See the management socket documentation for more
1096 information.
1097
1098 :param class_proxy px: A :ref:`proxy_class` which indicates the manipulated
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02001099 proxy.
Thierry Fournierf61aa632016-02-19 20:56:00 +01001100
1101.. js:function:: Proxy.stop(px)
1102
1103 Stop the proxy. See the management socket documentation for more information.
1104
1105 :param class_proxy px: A :ref:`proxy_class` which indicates the manipulated
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02001106 proxy.
Thierry Fournierf61aa632016-02-19 20:56:00 +01001107
1108.. js:function:: Proxy.shut_bcksess(px)
1109
1110 Kill the session attached to a backup server. See the management socket
1111 documentation for more information.
1112
1113 :param class_proxy px: A :ref:`proxy_class` which indicates the manipulated
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02001114 proxy.
Thierry Fournierf61aa632016-02-19 20:56:00 +01001115
1116.. js:function:: Proxy.get_cap(px)
1117
1118 Returns a string describing the capabilities of the proxy.
1119
1120 :param class_proxy px: A :ref:`proxy_class` which indicates the manipulated
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02001121 proxy.
Thierry Fournierf61aa632016-02-19 20:56:00 +01001122 :returns: a string "frontend", "backend", "proxy" or "ruleset".
1123
1124.. js:function:: Proxy.get_mode(px)
1125
1126 Returns a string describing the mode of the current proxy.
1127
1128 :param class_proxy px: A :ref:`proxy_class` which indicates the manipulated
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02001129 proxy.
Thierry Fournierf61aa632016-02-19 20:56:00 +01001130 :returns: a string "tcp", "http", "health" or "unknown"
1131
Aurelien DARRAGONfc845532023-04-03 11:00:18 +02001132.. js:function:: Proxy.get_srv_act(px)
1133
1134 Returns the number of current active servers for the current proxy that are
1135 eligible for LB.
1136
1137 :param class_proxy px: A :ref:`proxy_class` which indicates the manipulated
1138 proxy.
1139 :returns: an integer
1140
1141.. js:function:: Proxy.get_srv_bck(px)
1142
1143 Returns the number backup servers for the current proxy that are eligible
1144 for LB.
1145
1146 :param class_proxy px: A :ref:`proxy_class` which indicates the manipulated
1147 proxy.
1148 :returns: an integer
1149
Thierry Fournierf61aa632016-02-19 20:56:00 +01001150.. js:function:: Proxy.get_stats(px)
1151
Bertrand Jacquin874a35c2018-09-10 21:26:07 +01001152 Returns a table containing the proxy statistics. The statistics returned are
Thierry Fournierf61aa632016-02-19 20:56:00 +01001153 not the same if the proxy is frontend or a backend.
1154
1155 :param class_proxy px: A :ref:`proxy_class` which indicates the manipulated
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02001156 proxy.
Patrick Hemmerc6a1d712018-05-01 21:30:41 -04001157 :returns: a key/value table containing stats
Thierry Fournierf61aa632016-02-19 20:56:00 +01001158
Aurelien DARRAGON717a38d2023-04-26 19:02:43 +02001159.. js:function:: Proxy.get_mailers(px)
1160
1161 **LEGACY**
1162
1163 Returns a table containing mailers config for the current proxy or nil
1164 if mailers are not available for the proxy.
1165
1166 :param class_proxy px: A :ref:`proxy_class` which indicates the manipulated
1167 proxy.
1168 :returns: a :ref:`proxy_mailers_class` containing proxy mailers config
1169
1170.. _proxy_mailers_class:
1171
1172ProxyMailers class
1173==================
1174
1175**LEGACY**
1176
1177.. js:class:: ProxyMailers
1178
1179 This class provides mailers config for a given proxy.
1180
1181 If sending emails directly from lua, please consider
1182 :js:func:`core.disable_legacy_mailers()` to disable the email sending from
1183 haproxy. (Or email alerts will be sent twice...)
1184
1185.. js:attribute:: ProxyMailers.track_server_health
1186
1187 Boolean set to true if the option "log-health-checks" is configured on
1188 the proxy, meaning that all server checks event should trigger email alerts.
1189
1190.. js:attribute:: ProxyMailers.log_level
1191
1192 An integer, the maximum log level that triggers email alerts. It is a number
1193 between 0 and 7 as defined by option "email-alert level".
1194
1195.. js:attribute:: ProxyMailers.mailservers
1196
1197 An array containing the list of mail servers that should receive email alerts.
1198 Each array entry is a name:desc pair where desc represents the full server
1199 address (including port) as described in haproxy's configuration file.
1200
1201.. js:attribute:: ProxyMailers.smtp_hostname
1202
1203 A string containing the hostname to use for the SMTP transaction.
1204 (option "email-alert myhostname")
1205
1206.. js:attribute:: ProxyMailers.smtp_from
1207
1208 A string containing the "MAIL FROM" address to use for the SMTP transaction.
1209 (option "email-alert from")
1210
1211.. js:attribute:: ProxyMailers.smtp_to
1212
1213 A string containing the "RCPT TO" address to use for the SMTP transaction.
1214 (option "email-alert to")
1215
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001216.. _server_class:
1217
1218Server class
1219============
1220
Patrick Hemmerc6a1d712018-05-01 21:30:41 -04001221.. js:class:: Server
1222
1223 This class provides a way for manipulating servers and retrieving information.
1224
Patrick Hemmera62ae7e2018-04-29 14:23:48 -04001225.. js:attribute:: Server.name
1226
1227 Contain the name of the server.
1228
Aurelien DARRAGONc4b24372023-03-02 12:00:06 +01001229 .. warning::
1230 This attribute is now deprecated and will eventually be removed.
1231 Please use :js:func:`Server.get_name()` function instead.
1232
Thierry Fournierb0467732022-10-07 12:07:24 +02001233.. js:function:: Server.get_name(sv)
1234
1235 Returns the name of the server.
1236
Patrick Hemmera62ae7e2018-04-29 14:23:48 -04001237.. js:attribute:: Server.puid
1238
1239 Contain the proxy unique identifier of the server.
1240
Aurelien DARRAGONc4b24372023-03-02 12:00:06 +01001241 .. warning::
1242 This attribute is now deprecated and will eventually be removed.
1243 Please use :js:func:`Server.get_puid()` function instead.
1244
Thierry Fournierb0467732022-10-07 12:07:24 +02001245.. js:function:: Server.get_puid(sv)
1246
1247 Returns the proxy unique identifier of the server.
1248
Aurelien DARRAGON94ee6632023-03-10 15:11:27 +01001249.. js:function:: Server.get_rid(sv)
1250
1251 Returns the rid (revision ID) of the server.
1252 It is an unsigned integer that is set upon server creation. Value is derived
1253 from a global counter that starts at 0 and is incremented each time one or
1254 multiple server deletions are followed by a server addition (meaning that
1255 old name/id reuse could occur).
1256
1257 Combining server name/id with server rid yields a process-wide unique
1258 identifier.
1259
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001260.. js:function:: Server.is_draining(sv)
1261
Patrick Hemmerc6a1d712018-05-01 21:30:41 -04001262 Return true if the server is currently draining sticky connections.
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001263
1264 :param class_server sv: A :ref:`server_class` which indicates the manipulated
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02001265 server.
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001266 :returns: a boolean
1267
Aurelien DARRAGONc72051d2023-03-29 10:44:38 +02001268.. js:function:: Server.is_backup(sv)
1269
1270 Return true if the server is a backup server
1271
1272 :param class_server sv: A :ref:`server_class` which indicates the manipulated
1273 server.
1274 :returns: a boolean
1275
Aurelien DARRAGON7a03dee2023-03-29 10:49:30 +02001276.. js:function:: Server.is_dynamic(sv)
1277
1278 Return true if the server was instantiated at runtime (e.g.: from the cli)
1279
1280 :param class_server sv: A :ref:`server_class` which indicates the manipulated
1281 server.
1282 :returns: a boolean
1283
Aurelien DARRAGONfc759b42023-04-03 10:43:17 +02001284.. js:function:: Server.get_cur_sess(sv)
1285
1286 Return the number of currently active sessions on the server
1287
1288 :param class_server sv: A :ref:`server_class` which indicates the manipulated
1289 server.
1290 :returns: an integer
1291
1292.. js:function:: Server.get_pend_conn(sv)
1293
1294 Return the number of pending connections to the server
1295
1296 :param class_server sv: A :ref:`server_class` which indicates the manipulated
1297 server.
1298 :returns: an integer
1299
Patrick Hemmer32d539f2018-04-29 14:25:46 -04001300.. js:function:: Server.set_maxconn(sv, weight)
1301
1302 Dynamically change the maximum connections of the server. See the management
1303 socket documentation for more information about the format of the string.
1304
1305 :param class_server sv: A :ref:`server_class` which indicates the manipulated
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02001306 server.
Patrick Hemmer32d539f2018-04-29 14:25:46 -04001307 :param string maxconn: A string describing the server maximum connections.
1308
1309.. js:function:: Server.get_maxconn(sv, weight)
1310
1311 This function returns an integer representing the server maximum connections.
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 :returns: an integer.
1316
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001317.. js:function:: Server.set_weight(sv, weight)
1318
Patrick Hemmerc6a1d712018-05-01 21:30:41 -04001319 Dynamically change the weight of the server. See the management socket
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001320 documentation for more information about the format of the string.
1321
1322 :param class_server sv: A :ref:`server_class` which indicates the manipulated
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02001323 server.
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001324 :param string weight: A string describing the server weight.
1325
1326.. js:function:: Server.get_weight(sv)
1327
Patrick Hemmerc6a1d712018-05-01 21:30:41 -04001328 This function returns an integer representing the server weight.
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001329
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 :returns: an integer.
1333
Joseph C. Sible49bbf522020-05-04 22:20:32 -04001334.. js:function:: Server.set_addr(sv, addr[, port])
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001335
Patrick Hemmerc6a1d712018-05-01 21:30:41 -04001336 Dynamically change the address of the server. See the management socket
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001337 documentation for more information about the format of the string.
1338
1339 :param class_server sv: A :ref:`server_class` which indicates the manipulated
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02001340 server.
Patrick Hemmerc6a1d712018-05-01 21:30:41 -04001341 :param string addr: A string describing the server address.
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001342
1343.. js:function:: Server.get_addr(sv)
1344
Patrick Hemmerc6a1d712018-05-01 21:30:41 -04001345 Returns a string describing the address of the server.
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001346
1347 :param class_server sv: A :ref:`server_class` which indicates the manipulated
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02001348 server.
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001349 :returns: A string
1350
1351.. js:function:: Server.get_stats(sv)
1352
1353 Returns server statistics.
1354
1355 :param class_server sv: A :ref:`server_class` which indicates the manipulated
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02001356 server.
Patrick Hemmerc6a1d712018-05-01 21:30:41 -04001357 :returns: a key/value table containing stats
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001358
Aurelien DARRAGON3889efa2023-04-03 14:00:58 +02001359.. js:function:: Server.get_proxy(sv)
1360
1361 Returns the parent proxy to which the server belongs.
1362
1363 :param class_server sv: A :ref:`server_class` which indicates the manipulated
1364 server.
1365 :returns: a :ref:`proxy_class` or nil if not available
1366
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001367.. js:function:: Server.shut_sess(sv)
1368
1369 Shutdown all the sessions attached to the server. See the management socket
1370 documentation for more information about this function.
1371
1372 :param class_server sv: A :ref:`server_class` which indicates the manipulated
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02001373 server.
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001374
1375.. js:function:: Server.set_drain(sv)
1376
1377 Drain sticky sessions. See the management socket documentation for more
1378 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_maint(sv)
1384
1385 Set maintenance mode. 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_ready(sv)
1392
1393 Set normal mode. See the management socket documentation for more information
1394 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.check_enable(sv)
1400
1401 Enable health checks. See the management socket documentation for more
1402 information about this function.
1403
1404 :param class_server sv: A :ref:`server_class` which indicates the manipulated
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02001405 server.
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001406
1407.. js:function:: Server.check_disable(sv)
1408
1409 Disable 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_force_up(sv)
1416
1417 Force health-check up. 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_nolb(sv)
1424
1425 Force health-check nolb mode. 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_down(sv)
1432
1433 Force health-check down. 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.agent_enable(sv)
1440
1441 Enable agent check. 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_disable(sv)
1448
1449 Disable 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_force_up(sv)
1456
1457 Force agent check up. 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_down(sv)
1464
1465 Force agent check down. 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
Aurelien DARRAGON406511a2023-03-29 11:30:36 +02001471.. js:function:: Server.tracking(sv)
1472
1473 Check if the current server is tracking another server.
1474
1475 :param class_server sv: A :ref:`server_class` which indicates the manipulated
1476 server.
1477 :returns: A :ref:`server_class` which indicates the tracked server or nil if
1478 the server doesn't track another one.
1479
Aurelien DARRAGON4be36a12023-03-29 14:02:39 +02001480.. js:function:: Server.get_trackers(sv)
1481
1482 Check if the current server is being tracked by other servers.
1483
1484 :param class_server sv: A :ref:`server_class` which indicates the manipulated
1485 server.
1486 :returns: An array of :ref:`server_class` which indicates the tracking
1487 servers (might be empty)
1488
Aurelien DARRAGON223770d2023-03-10 15:34:35 +01001489.. js:function:: Server.event_sub(sv, event_types, func)
1490
1491 Register a function that will be called on specific server events.
1492 It works exactly like :js:func:`core.event_sub()` except that the subscription
1493 will be performed within the server dedicated subscription list instead of the
1494 global one.
1495 (Your callback function will only be called for server events affecting sv)
1496
1497 See :js:func:`core.event_sub()` for function usage.
1498
1499 A key advantage to using :js:func:`Server.event_sub()` over
1500 :js:func:`core.event_sub()` for servers is that :js:func:`Server.event_sub()`
1501 allows you to be notified for servers events of a single server only.
1502 It removes the needs for extra filtering in your callback function if you only
1503 care about a single server, and also prevents useless wakeups.
1504
1505 For instance, if you want to be notified for UP/DOWN events on a given set of
Ilya Shipitsinccf80122023-04-22 20:20:39 +02001506 servers, it is recommended to perform multiple per-server subscriptions since
Aurelien DARRAGON223770d2023-03-10 15:34:35 +01001507 it will be more efficient that doing a single global subscription that will
1508 filter the received events.
1509 Unless you really want to be notified for servers events of ALL servers of
1510 course, which could make sense given you setup but should be avoided if you
1511 have an important number of servers as it will add a significant load on your
1512 haproxy process in case of multiple servers state change in a short amount of
1513 time.
1514
1515 .. Note::
1516 You may also combine :js:func:`core.event_sub()` with
1517 :js:func:`Server.event_sub()`.
1518
1519 Also, don't forget that you can use :js:func:`core.register_task()` from
1520 your callback function if needed. (ie: parallel work)
1521
1522 Here is a working example combining :js:func:`core.event_sub()` with
1523 :js:func:`Server.event_sub()` and :js:func:`core.register_task()`
1524 (This only serves as a demo, this is not necessarily useful to do so)
1525
1526.. code-block:: lua
1527
1528 core.event_sub({"SERVER_ADD"}, function(event, data, sub)
1529 -- in the global event handler
1530 if data["reference"] ~= nil then
1531 print("Tracking new server: ", data["name"])
1532 data["reference"]:event_sub({"SERVER_UP", "SERVER_DOWN"}, function(event, data, sub)
1533 -- in the per-server event handler
1534 if data["reference"] ~= nil then
1535 core.register_task(function(server)
1536 -- subtask to perform some async work (e.g.: HTTP API calls, sending emails...)
1537 print("ASYNC: SERVER ", server:get_name(), " is ", event == "SERVER_UP" and "UP" or "DOWN")
1538 end, data["reference"])
1539 end
1540 end)
1541 end
1542 end)
1543
1544..
1545
1546 In this example, we will first track global server addition events.
1547 For each newly added server ("add server" on the cli), we will register a
1548 UP/DOWN server subscription.
1549 Then, the callback function will schedule the event handling in an async
1550 subtask which will receive the server reference as an argument.
1551
Thierry Fournierff480422016-02-25 08:36:46 +01001552.. _listener_class:
1553
1554Listener class
1555==============
1556
1557.. js:function:: Listener.get_stats(ls)
1558
1559 Returns server statistics.
1560
1561 :param class_listener ls: A :ref:`listener_class` which indicates the
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02001562 manipulated listener.
Patrick Hemmerc6a1d712018-05-01 21:30:41 -04001563 :returns: a key/value table containing stats
Thierry Fournierff480422016-02-25 08:36:46 +01001564
Aurelien DARRAGONc84899c2023-02-20 18:18:59 +01001565.. _event_sub_class:
1566
1567EventSub class
1568==============
1569
1570.. js:function:: EventSub.unsub()
1571
1572 End the subscription, the callback function will not be called again.
1573
1574.. _server_event_class:
1575
1576ServerEvent class
1577=================
1578
Aurelien DARRAGONc4ae8902023-04-17 17:24:48 +02001579.. js:class:: ServerEvent
1580
1581This class is provided with every **SERVER** events.
1582
1583See :js:func:`core.event_sub()` for more info.
1584
Aurelien DARRAGONc84899c2023-02-20 18:18:59 +01001585.. js:attribute:: ServerEvent.name
1586
1587 Contains the name of the server.
1588
1589.. js:attribute:: ServerEvent.puid
1590
1591 Contains the proxy-unique uid of the server
1592
1593.. js:attribute:: ServerEvent.rid
1594
1595 Contains the revision ID of the server
1596
1597.. js:attribute:: ServerEvent.proxy_name
1598
1599 Contains the name of the proxy to which the server belongs
1600
Aurelien DARRAGON55f84c72023-03-22 17:49:04 +01001601.. js:attribute:: ServerEvent.proxy_uuid
1602
1603 Contains the uuid of the proxy to which the server belongs
1604
Aurelien DARRAGONc84899c2023-02-20 18:18:59 +01001605.. js:attribute:: ServerEvent.reference
1606
1607 Reference to the live server (A :ref:`server_class`).
1608
1609 .. Warning::
1610 Not available if the server was removed in the meantime.
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02001611 (Will never be set for SERVER_DEL event since the server does not exist
1612 anymore)
Aurelien DARRAGONc84899c2023-02-20 18:18:59 +01001613
Aurelien DARRAGONc99f3ad2023-04-12 15:47:16 +02001614.. js:attribute:: ServerEvent.state
1615
1616 A :ref:`server_event_state_class`
1617
1618 .. Note::
1619 Only available for SERVER_STATE event
1620
Aurelien DARRAGON948dd3d2023-04-26 11:27:09 +02001621.. js:attribute:: ServerEvent.admin
1622
1623 A :ref:`server_event_admin_class`
1624
1625 .. Note::
1626 Only available for SERVER_ADMIN event
1627
Aurelien DARRAGON0bd53b22023-03-30 15:53:33 +02001628.. js:attribute:: ServerEvent.check
1629
1630 A :ref:`server_event_checkres_class`
1631
1632 .. Note::
1633 Only available for SERVER_CHECK event
1634
Aurelien DARRAGONc99f3ad2023-04-12 15:47:16 +02001635.. _server_event_checkres_class:
1636
1637ServerEventCheckRes class
1638=========================
1639
1640.. js:class:: ServerEventCheckRes
1641
1642This class describes the result of a server's check.
1643
1644.. js:attribute:: ServerEventCheckRes.result
1645
1646 Effective check result.
1647
1648 Check result is a string and will be set to one of the following values:
1649 - "FAILED": the check failed
1650 - "PASSED": the check succeeded
1651 - "CONDPASS": the check conditionally passed
1652
1653.. js:attribute:: ServerEventCheckRes.agent
1654
1655 Boolean set to true if the check is an agent check.
1656 Else it is a health check.
1657
1658.. js:attribute:: ServerEventCheckRes.duration
1659
1660 Check's duration in milliseconds
1661
1662.. js:attribute:: ServerEventCheckRes.reason
1663
1664 Check's status. An array containing three fields:
1665 - **short**: a string representing check status short name
1666 - **desc**: a string representing check status description
1667 - **code**: an integer, this extra information is provided for checks
1668 that went through the data analysis stage (>= layer 5)
1669
1670.. js:attribute:: ServerEventCheckRes.health
1671
1672 An array containing values about check's health (integers):
1673 - **cur**: current health counter:
1674 - 0 to (**rise** - 1) = BAD
1675 - **rise** to (**rise** + **fall** - 1) = GOOD
1676 - **rise**: server will be considered as operational after **rise**
1677 consecutive successful checks
1678 - **fall**: server will be considered as dead after **fall** consecutive
1679 unsuccessful checks
1680
1681.. _server_event_state_class:
1682
1683ServerEventState class
1684======================
1685
1686.. js:class:: ServerEventState
1687
1688This class contains additional info related to **SERVER_STATE** event.
1689
1690.. js:attribute:: ServerEventState.admin
1691
1692 Boolean set to true if the server state change is due to an administrative
1693 change. Else it is an operational change.
1694
1695.. js:attribute:: ServerEventState.check
1696
1697 A :ref:`server_event_checkres_class`, provided if the state change is
1698 due to a server check (must be an operational change).
1699
1700.. js:attribute:: ServerEventState.cause
1701
1702 Printable state change cause. Might be empty.
1703
1704.. js:attribute:: ServerEventState.new_state
1705
1706 New server state due to operational or admin change.
1707
1708 It is a string that can be any of the following values:
1709 - "STOPPED": The server is down
1710 - "STOPPING": The server is up but soft-stopping
1711 - "STARTING": The server is warming up
1712 - "RUNNING": The server is fully up
1713
1714.. js:attribute:: ServerEventState.old_state
1715
1716 Previous server state prior to the operational or admin change.
1717
1718 Can be any value described in **new_state**, but they should differ.
1719
1720.. js:attribute:: ServerEventState.requeued
1721
1722 Number of connections that were requeued due to the server state change.
1723
1724 For a server going DOWN: it is the number of pending server connections
1725 that are requeued to the backend (such connections will be redispatched
1726 to any server that is suitable according to the configured load balancing
1727 algorithm).
1728
1729 For a server doing UP: it is the number of pending connections on the
1730 backend that may be redispatched to the server according to the load
1731 balancing algorithm that is in use.
1732
Aurelien DARRAGON948dd3d2023-04-26 11:27:09 +02001733.. _server_event_admin_class:
1734
1735ServerEventAdmin class
1736======================
1737
1738.. js:class:: ServerEventAdmin
1739
1740This class contains additional info related to **SERVER_ADMIN** event.
1741
1742.. js:attribute:: ServerEventAdmin.cause
1743
1744 Printable admin state change cause. Might be empty.
1745
1746.. js:attribute:: ServerEventAdmin.new_admin
1747
1748 New server admin state due to the admin change.
1749
1750 It is an array of string containing a composition of following values:
1751 - "**MAINT**": server is in maintenance mode
1752 - "FMAINT": server is in forced maintenance mode (MAINT is also set)
1753 - "IMAINT": server is in inherited maintenance mode (MAINT is also set)
1754 - "RMAINT": server is in resolve maintenance mode (MAINT is also set)
1755 - "CMAINT": server is in config maintenance mode (MAINT is also set)
1756 - "**DRAIN**": server is in drain mode
1757 - "FDRAIN": server is in forced drain mode (DRAIN is also set)
1758 - "IDRAIN": server is in inherited drain mode (DRAIN is also set)
1759
1760.. js:attribute:: ServerEventAdmin.old_admin
1761
1762 Previous server admin state prior to the admin change.
1763
1764 Values are presented as in **new_admin**, but they should differ.
1765 (Comparing old and new helps to find out the change(s))
1766
1767.. js:attribute:: ServerEventAdmin.requeued
1768
1769 Same as :js:attr:`ServerEventState.requeued` but when the requeue is due to
1770 the server administrative state change.
1771
Aurelien DARRAGON86fb22c2023-05-03 17:03:09 +02001772.. _queue_class:
1773
1774Queue class
1775===========
1776
1777.. js:class:: Queue
1778
1779 This class provides a generic FIFO storage mechanism that may be shared
1780 between multiple lua contexts to easily pass data between them, as stock
1781 Lua doesn't provide easy methods for passing data between multiple coroutines.
1782
1783 inter-task example:
1784
1785.. code-block:: lua
1786
1787 -- script wide shared queue
1788 local queue = core.queue()
1789
1790 -- master task
1791 core.register_task(function()
1792 -- send the date every second
1793 while true do
1794 queue:push(os.date("%c", core.now().sec))
1795 core.sleep(1)
1796 end
1797 end)
1798
1799 -- worker task
1800 core.register_task(function()
1801 while true do
1802 -- print the date sent by master
1803 print(queue:pop_wait())
1804 end
1805 end)
1806..
1807
1808 Of course, queue may also be used as a local storage mechanism.
1809
1810 Use :js:func:`core.queue` to get a new Queue object.
1811
1812.. js:function:: Queue.size(queue)
1813
1814 This function returns the number of items within the Queue.
1815
1816 :param class_queue queue: A :ref:`queue_class` to the current queue
1817
1818.. js:function:: Queue.push(queue, item)
1819
1820 This function pushes the item (may be of any type) to the queue.
1821 Pushed item cannot be nil or invalid, or an error will be thrown.
1822
1823 :param class_queue queue: A :ref:`queue_class` to the current queue
1824 :returns: boolean true for success and false for error
1825
1826.. js:function:: Queue.pop(queue)
1827
1828 This function immediately tries to pop an item from the queue.
1829 It returns nil of no item is available at the time of the call.
1830
1831 :param class_queue queue: A :ref:`queue_class` to the current queue
1832 :returns: the item at the top of the stack (any type) or nil if no items
1833
1834.. js:function:: Queue.pop_wait(queue)
1835
1836 **context**: task
1837
1838 This is an alternative to pop() that may be used within task contexts.
1839
1840 The call waits for data if no item is currently available. This may be
1841 useful when used in a while loop to prevent cpu waste.
1842
1843 Note that this requires yielding, thus it is only available within contexts
1844 that support yielding (mainly task context).
1845
1846 :param class_queue queue: A :ref:`queue_class` to the current queue
1847 :returns: the item at the top of the stack (any type) or nil in case of error
1848
Thierry Fournier1de16592016-01-27 09:49:07 +01001849.. _concat_class:
1850
1851Concat class
1852============
1853
1854.. js:class:: Concat
1855
1856 This class provides a fast way for string concatenation. The way using native
1857 Lua concatenation like the code below is slow for some reasons.
1858
1859.. code-block:: lua
1860
1861 str = "string1"
1862 str = str .. ", string2"
1863 str = str .. ", string3"
1864..
1865
1866 For each concatenation, Lua:
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02001867 - allocates memory for the result,
1868 - catenates the two string copying the strings in the new memory block,
1869 - frees the old memory block containing the string which is no longer used.
1870
Thierry Fournier1de16592016-01-27 09:49:07 +01001871 This process does many memory move, allocation and free. In addition, the
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02001872 memory is not really freed, it is just marked as unused and waits for the
Thierry Fournier1de16592016-01-27 09:49:07 +01001873 garbage collector.
1874
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02001875 The Concat class provides an alternative way to concatenate strings. It uses
Thierry Fournier1de16592016-01-27 09:49:07 +01001876 the internal Lua mechanism (it does not allocate memory), but it doesn't copy
1877 the data more than once.
1878
1879 On my computer, the following loops spends 0.2s for the Concat method and
1880 18.5s for the pure Lua implementation. So, the Concat class is about 1000x
1881 faster than the embedded solution.
1882
1883.. code-block:: lua
1884
1885 for j = 1, 100 do
1886 c = core.concat()
1887 for i = 1, 20000 do
1888 c:add("#####")
1889 end
1890 end
1891..
1892
1893.. code-block:: lua
1894
1895 for j = 1, 100 do
1896 c = ""
1897 for i = 1, 20000 do
1898 c = c .. "#####"
1899 end
1900 end
1901..
1902
1903.. js:function:: Concat.add(concat, string)
1904
1905 This function adds a string to the current concatenated string.
1906
1907 :param class_concat concat: A :ref:`concat_class` which contains the currently
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02001908 built string.
Bertrand Jacquin874a35c2018-09-10 21:26:07 +01001909 :param string string: A new string to concatenate to the current built
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02001910 string.
Thierry Fournier1de16592016-01-27 09:49:07 +01001911
1912.. js:function:: Concat.dump(concat)
1913
Bertrand Jacquin874a35c2018-09-10 21:26:07 +01001914 This function returns the concatenated string.
Thierry Fournier1de16592016-01-27 09:49:07 +01001915
1916 :param class_concat concat: A :ref:`concat_class` which contains the currently
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02001917 built string.
Thierry Fournier1de16592016-01-27 09:49:07 +01001918 :returns: the concatenated string
1919
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001920.. _fetches_class:
1921
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001922Fetches class
1923=============
1924
1925.. js:class:: Fetches
1926
1927 This class contains a lot of internal HAProxy sample fetches. See the
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02001928 HAProxy "configuration.txt" documentation for more information.
1929 (chapters 7.3.2 to 7.3.6)
Thierry FOURNIER2e4893c2015-03-18 13:37:27 +01001930
Christopher Faulet1e9b1b62021-08-11 10:14:30 +02001931 .. warning::
1932 some sample fetches are not available in some context. These limitations
1933 are specified in this documentation when they're useful.
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001934
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01001935 :see: :js:attr:`TXN.f`
1936 :see: :js:attr:`TXN.sf`
Thierry FOURNIER2e4893c2015-03-18 13:37:27 +01001937
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02001938 Fetches are useful to:
Thierry FOURNIER2e4893c2015-03-18 13:37:27 +01001939
1940 * get system time,
1941 * get environment variable,
1942 * get random numbers,
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02001943 * know backend status like the number of users in queue or the number of
Thierry FOURNIER2e4893c2015-03-18 13:37:27 +01001944 connections established,
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02001945 * get client information like ip source or destination,
Thierry FOURNIER2e4893c2015-03-18 13:37:27 +01001946 * deal with stick tables,
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02001947 * fetch established SSL information,
1948 * fetch HTTP information like headers or method.
Thierry FOURNIER2e4893c2015-03-18 13:37:27 +01001949
1950.. code-block:: lua
1951
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001952 function action(txn)
1953 -- Get source IP
1954 local clientip = txn.f:src()
1955 end
Thierry FOURNIER2e4893c2015-03-18 13:37:27 +01001956..
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001957
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001958.. _converters_class:
1959
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001960Converters class
1961================
1962
1963.. js:class:: Converters
1964
1965 This class contains a lot of internal HAProxy sample converters. See the
Thierry FOURNIER2e4893c2015-03-18 13:37:27 +01001966 HAProxy documentation "configuration.txt" for more information about her
1967 usage. Its the chapter 7.3.1.
1968
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01001969 :see: :js:attr:`TXN.c`
1970 :see: :js:attr:`TXN.sc`
Thierry FOURNIER2e4893c2015-03-18 13:37:27 +01001971
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02001972 Converters provides stateful transformation. They are useful to:
Thierry FOURNIER2e4893c2015-03-18 13:37:27 +01001973
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02001974 * convert input to base64,
1975 * apply hash on input string (djb2, crc32, sdbm, wt6),
Thierry FOURNIER2e4893c2015-03-18 13:37:27 +01001976 * format date,
1977 * json escape,
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02001978 * extract preferred language comparing two lists,
Thierry FOURNIER2e4893c2015-03-18 13:37:27 +01001979 * turn to lower or upper chars,
1980 * deal with stick tables.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001981
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001982.. _channel_class:
1983
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001984Channel class
1985=============
1986
1987.. js:class:: Channel
1988
Christopher Faulet5a2c6612021-08-15 20:35:25 +02001989 **context**: action, sample-fetch, convert, filter
1990
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001991 HAProxy uses two buffers for the processing of the requests. The first one is
1992 used with the request data (from the client to the server) and the second is
1993 used for the response data (from the server to the client).
1994
1995 Each buffer contains two types of data. The first type is the incoming data
1996 waiting for a processing. The second part is the outgoing data already
1997 processed. Usually, the incoming data is processed, after it is tagged as
1998 outgoing data, and finally it is sent. The following functions provides tools
1999 for manipulating these data in a buffer.
2000
2001 The following diagram shows where the channel class function are applied.
2002
Christopher Faulet6a79fc12021-08-06 16:02:36 +02002003 .. image:: _static/channel.png
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002004
Christopher Faulet6a79fc12021-08-06 16:02:36 +02002005 .. warning::
2006 It is not possible to read from the response in request action, and it is
Boyang Li60cfe8b2022-05-10 18:11:00 +00002007 not possible to read from the request channel in response action.
Christopher Faulet09530392021-06-14 11:43:18 +02002008
Christopher Faulet6a79fc12021-08-06 16:02:36 +02002009 .. warning::
2010 It is forbidden to alter the Channels buffer from HTTP contexts. So only
2011 :js:func:`Channel.input`, :js:func:`Channel.output`,
2012 :js:func:`Channel.may_recv`, :js:func:`Channel.is_full` and
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02002013 :js:func:`Channel.is_resp` can be called from a HTTP context.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002014
Christopher Faulet5a2c6612021-08-15 20:35:25 +02002015 All the functions provided by this class are available in the
2016 **sample-fetches**, **actions** and **filters** contexts. For **filters**,
2017 incoming data (offset and length) are relative to the filter. Some functions
Boyang Li60cfe8b2022-05-10 18:11:00 +00002018 may yield, but only for **actions**. Yield is not possible for
Christopher Faulet5a2c6612021-08-15 20:35:25 +02002019 **sample-fetches**, **converters** and **filters**.
Christopher Faulet6a79fc12021-08-06 16:02:36 +02002020
2021.. js:function:: Channel.append(channel, string)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002022
Christopher Faulet6a79fc12021-08-06 16:02:36 +02002023 This function copies the string **string** at the end of incoming data of the
2024 channel buffer. The function returns the copied length on success or -1 if
2025 data cannot be copied.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002026
Christopher Faulet6a79fc12021-08-06 16:02:36 +02002027 Same that :js:func:`Channel.insert(channel, string, channel:input())`.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002028
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002029 :param class_channel channel: The manipulated Channel.
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02002030 :param string string: The data to copy at the end of incoming data.
Christopher Faulet6a79fc12021-08-06 16:02:36 +02002031 :returns: an integer containing the amount of bytes copied or -1.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002032
Christopher Faulet6a79fc12021-08-06 16:02:36 +02002033.. js:function:: Channel.data(channel [, offset [, length]])
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002034
Christopher Faulet6a79fc12021-08-06 16:02:36 +02002035 This function returns **length** bytes of incoming data from the channel
2036 buffer, starting at the offset **offset**. The data are not removed from the
2037 buffer.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002038
Christopher Faulet6a79fc12021-08-06 16:02:36 +02002039 By default, if no length is provided, all incoming data found, starting at the
2040 given offset, are returned. If **length** is set to -1, the function tries to
Christopher Faulet5a2c6612021-08-15 20:35:25 +02002041 retrieve a maximum of data and, if called by an action, it yields if
2042 necessary. It also waits for more data if the requested length exceeds the
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02002043 available amount of incoming data. Not providing an offset is the same as
Ilya Shipitsinff0f2782021-08-22 22:18:07 +05002044 setting it to 0. A positive offset is relative to the beginning of incoming
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02002045 data of the channel buffer while negative offset is relative to the end.
Christopher Faulet6a79fc12021-08-06 16:02:36 +02002046
2047 If there is no incoming data and the channel can't receive more data, a 'nil'
2048 value is returned.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002049
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002050 :param class_channel channel: The manipulated Channel.
Christopher Faulet6a79fc12021-08-06 16:02:36 +02002051 :param integer offset: *optional* The offset in incoming data to start to get
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02002052 data. 0 by default. May be negative to be relative to the end of incoming
2053 data.
Christopher Faulet6a79fc12021-08-06 16:02:36 +02002054 :param integer length: *optional* The expected length of data to retrieve. All
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02002055 incoming data by default. May be set to -1 to get a maximum of data.
Christopher Faulet6a79fc12021-08-06 16:02:36 +02002056 :returns: a string containing the data found or nil.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002057
Christopher Faulet6a79fc12021-08-06 16:02:36 +02002058.. js:function:: Channel.forward(channel, length)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002059
Christopher Faulet6a79fc12021-08-06 16:02:36 +02002060 This function forwards **length** bytes of data from the channel buffer. If
Christopher Faulet5a2c6612021-08-15 20:35:25 +02002061 the requested length exceeds the available amount of incoming data, and if
2062 called by an action, the function yields, waiting for more data to forward. It
2063 returns the amount of data forwarded.
Christopher Faulet6a79fc12021-08-06 16:02:36 +02002064
2065 :param class_channel channel: The manipulated Channel.
2066 :param integer int: The amount of data to forward.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002067
Christopher Faulet6a79fc12021-08-06 16:02:36 +02002068.. js:function:: Channel.input(channel)
2069
Christopher Faulet5a2c6612021-08-15 20:35:25 +02002070 This function returns the length of incoming data in the channel buffer. When
2071 called by a filter, this value is relative to the filter.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002072
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002073 :param class_channel channel: The manipulated Channel.
Christopher Faulet6a79fc12021-08-06 16:02:36 +02002074 :returns: an integer containing the amount of available bytes.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002075
Christopher Faulet6a79fc12021-08-06 16:02:36 +02002076.. js:function:: Channel.insert(channel, string [, offset])
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002077
Christopher Faulet6a79fc12021-08-06 16:02:36 +02002078 This function copies the string **string** at the offset **offset** in
2079 incoming data of the channel buffer. The function returns the copied length on
2080 success or -1 if data cannot be copied.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002081
Christopher Faulet6a79fc12021-08-06 16:02:36 +02002082 By default, if no offset is provided, the string is copied in front of
Ilya Shipitsinff0f2782021-08-22 22:18:07 +05002083 incoming data. A positive offset is relative to the beginning of incoming data
Christopher Faulet6a79fc12021-08-06 16:02:36 +02002084 of the channel buffer while negative offset is relative to their end.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002085
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002086 :param class_channel channel: The manipulated Channel.
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02002087 :param string string: The data to copy into incoming data.
2088 :param integer offset: *optional* The offset in incoming data where to copy
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02002089 data. 0 by default. May be negative to be relative to the end of incoming
2090 data.
Pieter Baauw4d7f7662015-11-08 16:38:08 +01002091 :returns: an integer containing the amount of bytes copied or -1.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002092
Christopher Faulet6a79fc12021-08-06 16:02:36 +02002093.. js:function:: Channel.is_full(channel)
2094
2095 This function returns true if the channel buffer is full.
2096
Christopher Faulet5a2c6612021-08-15 20:35:25 +02002097 :param class_channel channel: The manipulated Channel.
Christopher Faulet6a79fc12021-08-06 16:02:36 +02002098 :returns: a boolean
2099
2100.. js:function:: Channel.is_resp(channel)
2101
2102 This function returns true if the channel is the response one.
2103
Christopher Faulet5a2c6612021-08-15 20:35:25 +02002104 :param class_channel channel: The manipulated Channel.
Christopher Faulet6a79fc12021-08-06 16:02:36 +02002105 :returns: a boolean
2106
2107.. js:function:: Channel.line(channel [, offset [, length]])
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002108
Christopher Faulet6a79fc12021-08-06 16:02:36 +02002109 This function parses **length** bytes of incoming data of the channel buffer,
2110 starting at offset **offset**, and returns the first line found, including the
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02002111 '\\n'. The data are not removed from the buffer. If no line is found, all
2112 data are returned.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002113
Christopher Faulet6a79fc12021-08-06 16:02:36 +02002114 By default, if no length is provided, all incoming data, starting at the given
2115 offset, are evaluated. If **length** is set to -1, the function tries to
Christopher Faulet5a2c6612021-08-15 20:35:25 +02002116 retrieve a maximum of data and, if called by an action, yields if
2117 necessary. It also waits for more data if the requested length exceeds the
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02002118 available amount of incoming data. Not providing an offset is the same as
Ilya Shipitsinff0f2782021-08-22 22:18:07 +05002119 setting it to 0. A positive offset is relative to the beginning of incoming
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02002120 data of the channel buffer while negative offset is relative to the end.
Christopher Faulet6a79fc12021-08-06 16:02:36 +02002121
2122 If there is no incoming data and the channel can't receive more data, a 'nil'
2123 value is returned.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002124
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002125 :param class_channel channel: The manipulated Channel.
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02002126 :param integer offset: *optional* The offset in incoming data to start to
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02002127 parse data. 0 by default. May be negative to be relative to the end of
2128 incoming data.
Christopher Faulet6a79fc12021-08-06 16:02:36 +02002129 :param integer length: *optional* The length of data to parse. All incoming
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02002130 data by default. May be set to -1 to get a maximum of data.
Christopher Faulet6a79fc12021-08-06 16:02:36 +02002131 :returns: a string containing the line found or nil.
2132
2133.. js:function:: Channel.may_recv(channel)
2134
2135 This function returns true if the channel may still receive data.
2136
Christopher Faulet5a2c6612021-08-15 20:35:25 +02002137 :param class_channel channel: The manipulated Channel.
Christopher Faulet6a79fc12021-08-06 16:02:36 +02002138 :returns: a boolean
2139
2140.. js:function:: Channel.output(channel)
2141
Christopher Faulet5a2c6612021-08-15 20:35:25 +02002142 This function returns the length of outgoing data of the channel buffer. When
2143 called by a filter, this value is relative to the filter.
Christopher Faulet6a79fc12021-08-06 16:02:36 +02002144
2145 :param class_channel channel: The manipulated Channel.
2146 :returns: an integer containing the amount of available bytes.
2147
2148.. js:function:: Channel.prepend(channel, string)
2149
2150 This function copies the string **string** in front of incoming data of the
2151 channel buffer. The function returns the copied length on success or -1 if
2152 data cannot be copied.
2153
2154 Same that :js:func:`Channel.insert(channel, string, 0)`.
2155
2156 :param class_channel channel: The manipulated Channel.
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02002157 :param string string: The data to copy in front of incoming data.
Pieter Baauw4d7f7662015-11-08 16:38:08 +01002158 :returns: an integer containing the amount of bytes copied or -1.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002159
Christopher Faulet6a79fc12021-08-06 16:02:36 +02002160.. js:function:: Channel.remove(channel [, offset [, length]])
2161
2162 This function removes **length** bytes of incoming data of the channel buffer,
2163 starting at offset **offset**. This function returns number of bytes removed
2164 on success.
2165
2166 By default, if no length is provided, all incoming data, starting at the given
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02002167 offset, are removed. Not providing an offset is the same as setting it
Ilya Shipitsinff0f2782021-08-22 22:18:07 +05002168 to 0. A positive offset is relative to the beginning of incoming data of the
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02002169 channel buffer while negative offset is relative to the end.
Christopher Faulet6a79fc12021-08-06 16:02:36 +02002170
2171 :param class_channel channel: The manipulated Channel.
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02002172 :param integer offset: *optional* The offset in incoming data where to start
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02002173 to remove data. 0 by default. May be negative to be relative to the end of
2174 incoming data.
Christopher Faulet6a79fc12021-08-06 16:02:36 +02002175 :param integer length: *optional* The length of data to remove. All incoming
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02002176 data by default.
Christopher Faulet6a79fc12021-08-06 16:02:36 +02002177 :returns: an integer containing the amount of bytes removed.
2178
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002179.. js:function:: Channel.send(channel, string)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002180
Christopher Faulet5a2c6612021-08-15 20:35:25 +02002181 This function requires immediate send of the string **string**. It means the
Ilya Shipitsinff0f2782021-08-22 22:18:07 +05002182 string is copied at the beginning of incoming data of the channel buffer and
Christopher Faulet5a2c6612021-08-15 20:35:25 +02002183 immediately forwarded. Unless if the connection is close, and if called by an
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02002184 action, this function yields to copy and forward all the string.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002185
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002186 :param class_channel channel: The manipulated Channel.
Christopher Faulet6a79fc12021-08-06 16:02:36 +02002187 :param string string: The data to send.
Pieter Baauw4d7f7662015-11-08 16:38:08 +01002188 :returns: an integer containing the amount of bytes copied or -1.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002189
Christopher Faulet6a79fc12021-08-06 16:02:36 +02002190.. js:function:: Channel.set(channel, string [, offset [, length]])
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002191
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02002192 This function replaces **length** bytes of incoming data of the channel
2193 buffer, starting at offset **offset**, by the string **string**. The function
2194 returns the copied length on success or -1 if data cannot be copied.
Christopher Faulet6a79fc12021-08-06 16:02:36 +02002195
2196 By default, if no length is provided, all incoming data, starting at the given
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02002197 offset, are replaced. Not providing an offset is the same as setting it
Ilya Shipitsinff0f2782021-08-22 22:18:07 +05002198 to 0. A positive offset is relative to the beginning of incoming data of the
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02002199 channel buffer while negative offset is relative to the end.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002200
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002201 :param class_channel channel: The manipulated Channel.
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02002202 :param string string: The data to copy into incoming data.
2203 :param integer offset: *optional* The offset in incoming data where to start
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02002204 the data replacement. 0 by default. May be negative to be relative to the
2205 end of incoming data.
Christopher Faulet6a79fc12021-08-06 16:02:36 +02002206 :param integer length: *optional* The length of data to replace. All incoming
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02002207 data by default.
Christopher Faulet6a79fc12021-08-06 16:02:36 +02002208 :returns: an integer containing the amount of bytes copied or -1.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002209
Christopher Faulet6a79fc12021-08-06 16:02:36 +02002210.. js:function:: Channel.dup(channel)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002211
Christopher Faulet6a79fc12021-08-06 16:02:36 +02002212 **DEPRECATED**
2213
2214 This function returns all incoming data found in the channel buffer. The data
Boyang Li60cfe8b2022-05-10 18:11:00 +00002215 are not removed from the buffer and can be reprocessed later.
Christopher Faulet6a79fc12021-08-06 16:02:36 +02002216
2217 If there is no incoming data and the channel can't receive more data, a 'nil'
2218 value is returned.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002219
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002220 :param class_channel channel: The manipulated Channel.
Christopher Faulet6a79fc12021-08-06 16:02:36 +02002221 :returns: a string containing all data found or nil.
2222
2223 .. warning::
2224 This function is deprecated. :js:func:`Channel.data()` must be used
2225 instead.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002226
Christopher Faulet6a79fc12021-08-06 16:02:36 +02002227.. js:function:: Channel.get(channel)
2228
2229 **DEPRECATED**
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002230
Christopher Faulet6a79fc12021-08-06 16:02:36 +02002231 This function returns all incoming data found in the channel buffer and remove
2232 them from the buffer.
2233
2234 If there is no incoming data and the channel can't receive more data, a 'nil'
2235 value is returned.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002236
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002237 :param class_channel channel: The manipulated Channel.
Christopher Faulet6a79fc12021-08-06 16:02:36 +02002238 :returns: a string containing all the data found or nil.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002239
Christopher Faulet6a79fc12021-08-06 16:02:36 +02002240 .. warning::
2241 This function is deprecated. :js:func:`Channel.data()` must be used to
2242 retrieve data followed by a call to :js:func:`Channel:remove()` to remove
2243 data.
Thierry FOURNIER / OZON.IO65192f32016-11-07 15:28:40 +01002244
Christopher Faulet6a79fc12021-08-06 16:02:36 +02002245 .. code-block:: lua
Thierry FOURNIER / OZON.IO65192f32016-11-07 15:28:40 +01002246
Christopher Faulet6a79fc12021-08-06 16:02:36 +02002247 local data = chn:data()
2248 chn:remove(0, data:len())
2249
2250 ..
2251
2252.. js:function:: Channel.getline(channel)
2253
2254 **DEPRECATED**
2255
2256 This function returns the first line found in incoming data of the channel
Christopher Faulet5a2c6612021-08-15 20:35:25 +02002257 buffer, including the '\\n'. The returned data are removed from the buffer. If
2258 no line is found, and if called by an action, this function yields to wait for
2259 more data, except if the channel can't receive more data. In this case all
2260 data are returned.
Christopher Faulet6a79fc12021-08-06 16:02:36 +02002261
2262 If there is no incoming data and the channel can't receive more data, a 'nil'
2263 value is returned.
2264
2265 :param class_channel channel: The manipulated Channel.
2266 :returns: a string containing the line found or nil.
2267
2268 .. warning::
Boyang Li60cfe8b2022-05-10 18:11:00 +00002269 This function is deprecated. :js:func:`Channel.line()` must be used to
Christopher Faulet6a79fc12021-08-06 16:02:36 +02002270 retrieve a line followed by a call to :js:func:`Channel:remove()` to remove
2271 data.
2272
2273 .. code-block:: lua
2274
2275 local line = chn:line(0, -1)
2276 chn:remove(0, line:len())
2277
2278 ..
2279
2280.. js:function:: Channel.get_in_len(channel)
2281
Boyang Li60cfe8b2022-05-10 18:11:00 +00002282 **DEPRECATED**
Christopher Faulet6a79fc12021-08-06 16:02:36 +02002283
Christopher Faulet5a2c6612021-08-15 20:35:25 +02002284 This function returns the length of the input part of the buffer. When called
2285 by a filter, this value is relative to the filter.
Christopher Faulet6a79fc12021-08-06 16:02:36 +02002286
2287 :param class_channel channel: The manipulated Channel.
2288 :returns: an integer containing the amount of available bytes.
2289
2290 .. warning::
2291 This function is deprecated. :js:func:`Channel.input()` must be used
2292 instead.
2293
2294.. js:function:: Channel.get_out_len(channel)
2295
Boyang Li60cfe8b2022-05-10 18:11:00 +00002296 **DEPRECATED**
Christopher Faulet6a79fc12021-08-06 16:02:36 +02002297
Christopher Faulet5a2c6612021-08-15 20:35:25 +02002298 This function returns the length of the output part of the buffer. When called
2299 by a filter, this value is relative to the filter.
Christopher Faulet6a79fc12021-08-06 16:02:36 +02002300
2301 :param class_channel channel: The manipulated Channel.
2302 :returns: an integer containing the amount of available bytes.
2303
2304 .. warning::
2305 This function is deprecated. :js:func:`Channel.output()` must be used
2306 instead.
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002307
2308.. _http_class:
Thierry FOURNIER08504f42015-03-16 14:17:08 +01002309
2310HTTP class
2311==========
2312
2313.. js:class:: HTTP
2314
2315 This class contain all the HTTP manipulation functions.
2316
Pieter Baauw386a1272015-08-16 15:26:24 +02002317.. js:function:: HTTP.req_get_headers(http)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01002318
Patrick Hemmerc6a1d712018-05-01 21:30:41 -04002319 Returns a table containing all the request headers.
Thierry FOURNIER08504f42015-03-16 14:17:08 +01002320
2321 :param class_http http: The related http object.
Patrick Hemmerc6a1d712018-05-01 21:30:41 -04002322 :returns: table of headers.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01002323 :see: :js:func:`HTTP.res_get_headers`
Thierry FOURNIER08504f42015-03-16 14:17:08 +01002324
Patrick Hemmerc6a1d712018-05-01 21:30:41 -04002325 This is the form of the returned table:
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002326
2327.. code-block:: lua
2328
2329 HTTP:req_get_headers()['<header-name>'][<header-index>] = "<header-value>"
2330
2331 local hdr = HTTP:req_get_headers()
2332 hdr["host"][0] = "www.test.com"
2333 hdr["accept"][0] = "audio/basic q=1"
2334 hdr["accept"][1] = "audio/*, q=0.2"
2335 hdr["accept"][2] = "*/*, q=0.1"
2336..
2337
Pieter Baauw386a1272015-08-16 15:26:24 +02002338.. js:function:: HTTP.res_get_headers(http)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01002339
Patrick Hemmerc6a1d712018-05-01 21:30:41 -04002340 Returns a table containing all the response headers.
Thierry FOURNIER08504f42015-03-16 14:17:08 +01002341
2342 :param class_http http: The related http object.
Patrick Hemmerc6a1d712018-05-01 21:30:41 -04002343 :returns: table of headers.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01002344 :see: :js:func:`HTTP.req_get_headers`
Thierry FOURNIER08504f42015-03-16 14:17:08 +01002345
Patrick Hemmerc6a1d712018-05-01 21:30:41 -04002346 This is the form of the returned table:
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002347
2348.. code-block:: lua
2349
2350 HTTP:res_get_headers()['<header-name>'][<header-index>] = "<header-value>"
2351
2352 local hdr = HTTP:req_get_headers()
2353 hdr["host"][0] = "www.test.com"
2354 hdr["accept"][0] = "audio/basic q=1"
2355 hdr["accept"][1] = "audio/*, q=0.2"
2356 hdr["accept"][2] = "*.*, q=0.1"
2357..
2358
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002359.. js:function:: HTTP.req_add_header(http, name, value)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01002360
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02002361 Appends a HTTP header field in the request whose name is
Thierry FOURNIER08504f42015-03-16 14:17:08 +01002362 specified in "name" and whose value is defined in "value".
2363
2364 :param class_http http: The related http object.
2365 :param string name: The header name.
2366 :param string value: The header value.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01002367 :see: :js:func:`HTTP.res_add_header`
Thierry FOURNIER08504f42015-03-16 14:17:08 +01002368
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002369.. js:function:: HTTP.res_add_header(http, name, value)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01002370
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02002371 Appends a HTTP header field in the response whose name is
Thierry FOURNIER08504f42015-03-16 14:17:08 +01002372 specified in "name" and whose value is defined in "value".
2373
2374 :param class_http http: The related http object.
2375 :param string name: The header name.
2376 :param string value: The header value.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01002377 :see: :js:func:`HTTP.req_add_header`
Thierry FOURNIER08504f42015-03-16 14:17:08 +01002378
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002379.. js:function:: HTTP.req_del_header(http, name)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01002380
2381 Removes all HTTP header fields in the request whose name is
2382 specified in "name".
2383
2384 :param class_http http: The related http object.
2385 :param string name: The header name.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01002386 :see: :js:func:`HTTP.res_del_header`
Thierry FOURNIER08504f42015-03-16 14:17:08 +01002387
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002388.. js:function:: HTTP.res_del_header(http, name)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01002389
2390 Removes all HTTP header fields in the response whose name is
2391 specified in "name".
2392
2393 :param class_http http: The related http object.
2394 :param string name: The header name.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01002395 :see: :js:func:`HTTP.req_del_header`
Thierry FOURNIER08504f42015-03-16 14:17:08 +01002396
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002397.. js:function:: HTTP.req_set_header(http, name, value)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01002398
Bertrand Jacquin874a35c2018-09-10 21:26:07 +01002399 This variable replace all occurrence of all header "name", by only
Thierry FOURNIER08504f42015-03-16 14:17:08 +01002400 one containing the "value".
2401
2402 :param class_http http: The related http object.
2403 :param string name: The header name.
2404 :param string value: The header value.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01002405 :see: :js:func:`HTTP.res_set_header`
Thierry FOURNIER08504f42015-03-16 14:17:08 +01002406
Bertrand Jacquin874a35c2018-09-10 21:26:07 +01002407 This function does the same work as the following code:
Thierry FOURNIER08504f42015-03-16 14:17:08 +01002408
2409.. code-block:: lua
2410
2411 function fcn(txn)
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002412 TXN.http:req_del_header("header")
2413 TXN.http:req_add_header("header", "value")
Thierry FOURNIER08504f42015-03-16 14:17:08 +01002414 end
2415..
2416
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002417.. js:function:: HTTP.res_set_header(http, name, value)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01002418
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02002419 This function replaces all occurrence of all header "name", by only
Thierry FOURNIER08504f42015-03-16 14:17:08 +01002420 one containing the "value".
2421
2422 :param class_http http: The related http object.
2423 :param string name: The header name.
2424 :param string value: The header value.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01002425 :see: :js:func:`HTTP.req_rep_header()`
Thierry FOURNIER08504f42015-03-16 14:17:08 +01002426
Pieter Baauw386a1272015-08-16 15:26:24 +02002427.. js:function:: HTTP.req_rep_header(http, name, regex, replace)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01002428
2429 Matches the regular expression in all occurrences of header field "name"
2430 according to "regex", and replaces them with the "replace" argument. The
2431 replacement value can contain back references like \1, \2, ... This
2432 function works with the request.
2433
2434 :param class_http http: The related http object.
2435 :param string name: The header name.
2436 :param string regex: The match regular expression.
2437 :param string replace: The replacement value.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01002438 :see: :js:func:`HTTP.res_rep_header()`
Thierry FOURNIER08504f42015-03-16 14:17:08 +01002439
Pieter Baauw386a1272015-08-16 15:26:24 +02002440.. js:function:: HTTP.res_rep_header(http, name, regex, string)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01002441
2442 Matches the regular expression in all occurrences of header field "name"
2443 according to "regex", and replaces them with the "replace" argument. The
2444 replacement value can contain back references like \1, \2, ... This
2445 function works with the request.
2446
2447 :param class_http http: The related http object.
2448 :param string name: The header name.
2449 :param string regex: The match regular expression.
2450 :param string replace: The replacement value.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01002451 :see: :js:func:`HTTP.req_rep_header()`
Thierry FOURNIER08504f42015-03-16 14:17:08 +01002452
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002453.. js:function:: HTTP.req_set_method(http, method)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01002454
2455 Rewrites the request method with the parameter "method".
2456
2457 :param class_http http: The related http object.
2458 :param string method: The new method.
2459
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002460.. js:function:: HTTP.req_set_path(http, path)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01002461
2462 Rewrites the request path with the "path" parameter.
2463
2464 :param class_http http: The related http object.
2465 :param string path: The new path.
2466
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002467.. js:function:: HTTP.req_set_query(http, query)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01002468
2469 Rewrites the request's query string which appears after the first question
2470 mark ("?") with the parameter "query".
2471
2472 :param class_http http: The related http object.
2473 :param string query: The new query.
2474
Thierry FOURNIER0d79cf62015-08-26 14:20:58 +02002475.. js:function:: HTTP.req_set_uri(http, uri)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01002476
2477 Rewrites the request URI with the parameter "uri".
2478
2479 :param class_http http: The related http object.
2480 :param string uri: The new uri.
2481
Robin H. Johnson52f5db22017-01-01 13:10:52 -08002482.. js:function:: HTTP.res_set_status(http, status [, reason])
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +02002483
Robin H. Johnson52f5db22017-01-01 13:10:52 -08002484 Rewrites the response status code with the parameter "code".
2485
2486 If no custom reason is provided, it will be generated from the status.
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +02002487
2488 :param class_http http: The related http object.
2489 :param integer status: The new response status code.
Robin H. Johnson52f5db22017-01-01 13:10:52 -08002490 :param string reason: The new response reason (optional).
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +02002491
William Lallemand00a15022021-11-19 16:02:44 +01002492.. _httpclient_class:
2493
2494HTTPClient class
2495================
2496
2497.. js:class:: HTTPClient
2498
2499 The httpclient class allows issue of outbound HTTP requests through a simple
2500 API without the knowledge of HAProxy internals.
2501
2502.. js:function:: HTTPClient.get(httpclient, request)
2503.. js:function:: HTTPClient.head(httpclient, request)
2504.. js:function:: HTTPClient.put(httpclient, request)
2505.. js:function:: HTTPClient.post(httpclient, request)
2506.. js:function:: HTTPClient.delete(httpclient, request)
2507
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02002508 Send a HTTP request and wait for a response. GET, HEAD PUT, POST and DELETE
2509 methods can be used.
2510 The HTTPClient will send asynchronously the data and is able to send and
2511 receive more than HAProxy bufsize.
William Lallemand00a15022021-11-19 16:02:44 +01002512
William Lallemanda9256192022-10-21 11:48:24 +02002513 The HTTPClient interface is not able to decompress responses, it is not
2514 recommended to send an Accept-Encoding in the request so the response is
2515 received uncompressed.
William Lallemand00a15022021-11-19 16:02:44 +01002516
2517 :param class httpclient: Is the manipulated HTTPClient.
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02002518 :param table request: Is a table containing the parameters of the request
2519 that will be send.
2520 :param string request.url: Is a mandatory parameter for the request that
2521 contains the URL.
2522 :param string request.body: Is an optional parameter for the request that
2523 contains the body to send.
2524 :param table request.headers: Is an optional parameter for the request that
2525 contains the headers to send.
2526 :param string request.dst: Is an optional parameter for the destination in
2527 haproxy address format.
2528 :param integer request.timeout: Optional timeout parameter, set a
2529 "timeout server" on the connections.
William Lallemand00a15022021-11-19 16:02:44 +01002530 :returns: Lua table containing the response
2531
2532
2533.. code-block:: lua
2534
2535 local httpclient = core.httpclient()
William Lallemand4f4f2b72022-02-17 20:00:23 +01002536 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 +01002537
2538..
2539
2540.. code-block:: lua
2541
2542 response = {
2543 status = 400,
2544 reason = "Bad request",
2545 headers = {
2546 ["content-type"] = { "text/html" },
2547 ["cache-control"] = { "no-cache", "no-store" },
2548 },
William Lallemand4f4f2b72022-02-17 20:00:23 +01002549 body = "<html><body><h1>invalid request<h1></body></html>",
William Lallemand00a15022021-11-19 16:02:44 +01002550 }
2551..
2552
2553
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002554.. _txn_class:
2555
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002556TXN class
2557=========
2558
2559.. js:class:: TXN
2560
2561 The txn class contain all the functions relative to the http or tcp
2562 transaction (Note than a tcp stream is the same than a tcp transaction, but
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02002563 a HTTP transaction is not the same than a tcp stream).
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002564
2565 The usage of this class permits to retrieve data from the requests, alter it
2566 and forward it.
2567
2568 All the functions provided by this class are available in the context
Christopher Faulet5a2c6612021-08-15 20:35:25 +02002569 **sample-fetches**, **actions** and **filters**.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002570
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002571.. js:attribute:: TXN.c
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002572
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002573 :returns: An :ref:`converters_class`.
2574
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002575 This attribute contains a Converters class object.
2576
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002577.. js:attribute:: TXN.sc
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002578
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002579 :returns: An :ref:`converters_class`.
2580
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002581 This attribute contains a Converters class object. The functions of
2582 this object returns always a string.
2583
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002584.. js:attribute:: TXN.f
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002585
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002586 :returns: An :ref:`fetches_class`.
2587
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002588 This attribute contains a Fetches class object.
2589
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002590.. js:attribute:: TXN.sf
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002591
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002592 :returns: An :ref:`fetches_class`.
2593
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002594 This attribute contains a Fetches class object. The functions of
2595 this object returns always a string.
2596
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002597.. js:attribute:: TXN.req
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002598
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002599 :returns: An :ref:`channel_class`.
2600
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002601 This attribute contains a channel class object for the request buffer.
2602
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002603.. js:attribute:: TXN.res
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002604
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002605 :returns: An :ref:`channel_class`.
2606
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002607 This attribute contains a channel class object for the response buffer.
2608
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002609.. js:attribute:: TXN.http
Thierry FOURNIER08504f42015-03-16 14:17:08 +01002610
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002611 :returns: An :ref:`http_class`.
2612
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02002613 This attribute contains a HTTP class object. It is available only if the
Thierry FOURNIER08504f42015-03-16 14:17:08 +01002614 proxy has the "mode http" enabled.
2615
Christopher Faulet5a2c6612021-08-15 20:35:25 +02002616.. js:attribute:: TXN.http_req
2617
2618 :returns: An :ref:`httpmessage_class`.
2619
2620 This attribute contains the request HTTPMessage class object. It is available
2621 only if the proxy has the "mode http" enabled and only in the **filters**
2622 context.
2623
2624.. js:attribute:: TXN.http_res
2625
2626 :returns: An :ref:`httpmessage_class`.
2627
2628 This attribute contains the response HTTPMessage class object. It is available
2629 only if the proxy has the "mode http" enabled and only in the **filters**
2630 context.
2631
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01002632.. js:function:: TXN.log(TXN, loglevel, msg)
2633
2634 This function sends a log. The log is sent, according with the HAProxy
2635 configuration file, on the default syslog server if it is configured and on
2636 the stderr if it is allowed.
2637
2638 :param class_txn txn: The class txn object containing the data.
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02002639 :param integer loglevel: Is the log level associated with the message. It is
2640 a number between 0 and 7.
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01002641 :param string msg: The log content.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01002642 :see: :js:attr:`core.emerg`, :js:attr:`core.alert`, :js:attr:`core.crit`,
2643 :js:attr:`core.err`, :js:attr:`core.warning`, :js:attr:`core.notice`,
2644 :js:attr:`core.info`, :js:attr:`core.debug` (log level definitions)
2645 :see: :js:func:`TXN.deflog`
2646 :see: :js:func:`TXN.Debug`
2647 :see: :js:func:`TXN.Info`
2648 :see: :js:func:`TXN.Warning`
2649 :see: :js:func:`TXN.Alert`
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01002650
2651.. js:function:: TXN.deflog(TXN, msg)
2652
Bertrand Jacquin874a35c2018-09-10 21:26:07 +01002653 Sends a log line with the default loglevel for the proxy associated with the
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01002654 transaction.
2655
2656 :param class_txn txn: The class txn object containing the data.
2657 :param string msg: The log content.
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02002658 :see: :js:func:`TXN.log`
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01002659
2660.. js:function:: TXN.Debug(txn, msg)
2661
2662 :param class_txn txn: The class txn object containing the data.
2663 :param string msg: The log content.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01002664 :see: :js:func:`TXN.log`
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01002665
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02002666 Does the same job as:
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01002667
2668.. code-block:: lua
2669
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002670 function Debug(txn, msg)
2671 TXN.log(txn, core.debug, msg)
2672 end
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01002673..
2674
2675.. js:function:: TXN.Info(txn, msg)
2676
2677 :param class_txn txn: The class txn object containing the data.
2678 :param string msg: The log content.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01002679 :see: :js:func:`TXN.log`
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01002680
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02002681 Does the same job as:
2682
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01002683.. code-block:: lua
2684
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02002685 function Info(txn, msg)
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002686 TXN.log(txn, core.info, msg)
2687 end
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01002688..
2689
2690.. js:function:: TXN.Warning(txn, msg)
2691
2692 :param class_txn txn: The class txn object containing the data.
2693 :param string msg: The log content.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01002694 :see: :js:func:`TXN.log`
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01002695
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02002696 Does the same job as:
2697
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01002698.. code-block:: lua
2699
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02002700 function Warning(txn, msg)
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002701 TXN.log(txn, core.warning, msg)
2702 end
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01002703..
2704
2705.. js:function:: TXN.Alert(txn, msg)
2706
2707 :param class_txn txn: The class txn object containing the data.
2708 :param string msg: The log content.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01002709 :see: :js:func:`TXN.log`
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01002710
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02002711 Does the same job as:
2712
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01002713.. code-block:: lua
2714
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02002715 function Alert(txn, msg)
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002716 TXN.log(txn, core.alert, msg)
2717 end
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01002718..
2719
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002720.. js:function:: TXN.get_priv(txn)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002721
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002722 Return Lua data stored in the current transaction (with the `TXN.set_priv()`)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002723 function. If no data are stored, it returns a nil value.
2724
2725 :param class_txn txn: The class txn object containing the data.
Bertrand Jacquin874a35c2018-09-10 21:26:07 +01002726 :returns: the opaque data previously stored, or nil if nothing is
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02002727 available.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002728
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002729.. js:function:: TXN.set_priv(txn, data)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002730
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02002731 Store any data in the current HAProxy transaction. This action replaces the
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002732 old stored data.
2733
2734 :param class_txn txn: The class txn object containing the data.
2735 :param opaque data: The data which is stored in the transaction.
2736
Tim Duesterhus4e172c92020-05-19 13:49:42 +02002737.. js:function:: TXN.set_var(TXN, var, value[, ifexist])
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +02002738
David Carlier61fdf8b2015-10-02 11:59:38 +01002739 Converts a Lua type in a HAProxy type and store it in a variable <var>.
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +02002740
2741 :param class_txn txn: The class txn object containing the data.
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02002742 :param string var: The variable name according with the HAProxy variable
2743 syntax.
2744 :param type value: The value associated to the variable. The type can be
2745 string or integer.
2746 :param boolean ifexist: If this parameter is set to true the variable will
2747 only be set if it was defined elsewhere (i.e. used within the configuration).
2748 For global variables (using the "proc" scope), they will only be updated and
2749 never created. It is highly recommended to always set this to true.
Christopher Faulet85d79c92016-11-09 16:54:56 +01002750
2751.. js:function:: TXN.unset_var(TXN, var)
2752
2753 Unset the variable <var>.
2754
2755 :param class_txn txn: The class txn object containing the data.
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02002756 :param string var: The variable name according with the HAProxy variable
2757 syntax.
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +02002758
2759.. js:function:: TXN.get_var(TXN, var)
2760
2761 Returns data stored in the variable <var> converter in Lua type.
2762
2763 :param class_txn txn: The class txn object containing the data.
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02002764 :param string var: The variable name according with the HAProxy variable
2765 syntax.
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +02002766
Christopher Faulet700d9e82020-01-31 12:21:52 +01002767.. js:function:: TXN.reply([reply])
2768
2769 Return a new reply object
2770
2771 :param table reply: A table containing info to initialize the reply fields.
2772 :returns: A :ref:`reply_class` object.
2773
2774 The table used to initialized the reply object may contain following entries :
2775
2776 * status : The reply status code. the code 200 is used by default.
2777 * reason : The reply reason. The reason corresponding to the status code is
2778 used by default.
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02002779 * headers : A list of headers, indexed by header name. Empty by default. For
Christopher Faulet700d9e82020-01-31 12:21:52 +01002780 a given name, multiple values are possible, stored in an ordered list.
2781 * body : The reply body, empty by default.
2782
2783.. code-block:: lua
2784
2785 local reply = txn:reply{
2786 status = 400,
2787 reason = "Bad request",
2788 headers = {
2789 ["content-type"] = { "text/html" },
2790 ["cache-control"] = {"no-cache", "no-store" }
2791 },
2792 body = "<html><body><h1>invalid request<h1></body></html>"
2793 }
2794..
2795 :see: :js:class:`Reply`
2796
2797.. js:function:: TXN.done(txn[, reply])
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002798
Willy Tarreaubc183a62015-08-28 10:39:11 +02002799 This function terminates processing of the transaction and the associated
Christopher Faulet700d9e82020-01-31 12:21:52 +01002800 session and optionally reply to the client for HTTP sessions.
2801
2802 :param class_txn txn: The class txn object containing the data.
2803 :param class_reply reply: The class reply object to return to the client.
2804
2805 This functions can be used when a critical error is detected or to terminate
Willy Tarreaubc183a62015-08-28 10:39:11 +02002806 processing after some data have been returned to the client (eg: a redirect).
Christopher Faulet700d9e82020-01-31 12:21:52 +01002807 To do so, a reply may be provided. This object is optional and may contain a
2808 status code, a reason, a header list and a body. All these fields are
Christopher Faulet7855b192021-11-09 18:39:51 +01002809 optional. When not provided, the default values are used. By default, with an
2810 empty reply object, an empty HTTP 200 response is returned to the client. If
2811 no reply object is provided, the transaction is terminated without any
2812 reply. If a reply object is provided, it must not exceed the buffer size once
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02002813 converted into the internal HTTP representation. Because for now there is no
Christopher Faulet7855b192021-11-09 18:39:51 +01002814 easy way to be sure it fits, it is probably better to keep it reasonably
2815 small.
Christopher Faulet700d9e82020-01-31 12:21:52 +01002816
2817 The reply object may be fully created in lua or the class Reply may be used to
2818 create it.
2819
2820.. code-block:: lua
2821
2822 local reply = txn:reply()
2823 reply:set_status(400, "Bad request")
2824 reply:add_header("content-type", "text/html")
2825 reply:add_header("cache-control", "no-cache")
2826 reply:add_header("cache-control", "no-store")
2827 reply:set_body("<html><body><h1>invalid request<h1></body></html>")
2828 txn:done(reply)
2829..
2830
2831.. code-block:: lua
2832
2833 txn:done{
2834 status = 400,
2835 reason = "Bad request",
2836 headers = {
2837 ["content-type"] = { "text/html" },
2838 ["cache-control"] = { "no-cache", "no-store" },
2839 },
2840 body = "<html><body><h1>invalid request<h1></body></html>"
2841 }
2842..
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002843
Christopher Faulet1e9b1b62021-08-11 10:14:30 +02002844 .. warning::
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02002845 It does not make sense to call this function from sample-fetches. In this
2846 case the behavior is the same than core.done(): it finishes the Lua
Christopher Faulet1e9b1b62021-08-11 10:14:30 +02002847 execution. The transaction is really aborted only from an action registered
2848 function.
Thierry FOURNIERab00df62016-07-14 11:42:37 +02002849
Christopher Faulet700d9e82020-01-31 12:21:52 +01002850 :see: :js:func:`TXN.reply`, :js:class:`Reply`
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002851
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002852.. js:function:: TXN.set_loglevel(txn, loglevel)
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01002853
2854 Is used to change the log level of the current request. The "loglevel" must
2855 be an integer between 0 and 7.
2856
2857 :param class_txn txn: The class txn object containing the data.
2858 :param integer loglevel: The required log level. This variable can be one of
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01002859 :see: :js:attr:`core.emerg`, :js:attr:`core.alert`, :js:attr:`core.crit`,
2860 :js:attr:`core.err`, :js:attr:`core.warning`, :js:attr:`core.notice`,
2861 :js:attr:`core.info`, :js:attr:`core.debug` (log level definitions)
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01002862
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002863.. js:function:: TXN.set_tos(txn, tos)
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01002864
2865 Is used to set the TOS or DSCP field value of packets sent to the client to
2866 the value passed in "tos" on platforms which support this.
2867
2868 :param class_txn txn: The class txn object containing the data.
2869 :param integer tos: The new TOS os DSCP.
2870
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002871.. js:function:: TXN.set_mark(txn, mark)
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01002872
2873 Is used to set the Netfilter MARK on all packets sent to the client to the
2874 value passed in "mark" on platforms which support it.
2875
2876 :param class_txn txn: The class txn object containing the data.
2877 :param integer mark: The mark value.
2878
Patrick Hemmer268a7072018-05-11 12:52:31 -04002879.. js:function:: TXN.set_priority_class(txn, prio)
2880
2881 This function adjusts the priority class of the transaction. The value should
2882 be within the range -2047..2047. Values outside this range will be
2883 truncated.
2884
2885 See the HAProxy configuration.txt file keyword "http-request" action
2886 "set-priority-class" for details.
2887
2888.. js:function:: TXN.set_priority_offset(txn, prio)
2889
2890 This function adjusts the priority offset of the transaction. The value
2891 should be within the range -524287..524287. Values outside this range will be
2892 truncated.
2893
2894 See the HAProxy configuration.txt file keyword "http-request" action
2895 "set-priority-offset" for details.
2896
Christopher Faulet700d9e82020-01-31 12:21:52 +01002897.. _reply_class:
2898
2899Reply class
2900============
2901
2902.. js:class:: Reply
2903
2904 **context**: action
2905
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02002906 This class represents a HTTP response message. It provides some methods to
Christopher Faulet7855b192021-11-09 18:39:51 +01002907 enrich it. Once converted into the internal HTTP representation, the response
2908 message must not exceed the buffer size. Because for now there is no
2909 easy way to be sure it fits, it is probably better to keep it reasonably
2910 small.
2911
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02002912 See tune.bufsize in the configuration manual for details.
Christopher Faulet700d9e82020-01-31 12:21:52 +01002913
2914.. code-block:: lua
2915
2916 local reply = txn:reply({status = 400}) -- default HTTP 400 reason-phase used
2917 reply:add_header("content-type", "text/html")
2918 reply:add_header("cache-control", "no-cache")
2919 reply:add_header("cache-control", "no-store")
2920 reply:set_body("<html><body><h1>invalid request<h1></body></html>")
2921..
2922
2923 :see: :js:func:`TXN.reply`
2924
2925.. js:attribute:: Reply.status
2926
2927 The reply status code. By default, the status code is set to 200.
2928
2929 :returns: integer
2930
2931.. js:attribute:: Reply.reason
2932
2933 The reason string describing the status code.
2934
2935 :returns: string
2936
2937.. js:attribute:: Reply.headers
2938
2939 A table indexing all reply headers by name. To each name is associated an
2940 ordered list of values.
2941
2942 :returns: Lua table
2943
2944.. code-block:: lua
2945
2946 {
2947 ["content-type"] = { "text/html" },
2948 ["cache-control"] = {"no-cache", "no-store" },
2949 x_header_name = { "value1", "value2", ... }
2950 ...
2951 }
2952..
2953
2954.. js:attribute:: Reply.body
2955
2956 The reply payload.
2957
2958 :returns: string
2959
2960.. js:function:: Reply.set_status(REPLY, status[, reason])
2961
2962 Set the reply status code and optionally the reason-phrase. If the reason is
2963 not provided, the default reason corresponding to the status code is used.
2964
2965 :param class_reply reply: The related Reply object.
2966 :param integer status: The reply status code.
2967 :param string reason: The reply status reason (optional).
2968
2969.. js:function:: Reply.add_header(REPLY, name, value)
2970
2971 Add a header to the reply object. If the header does not already exist, a new
2972 entry is created with its name as index and a one-element list containing its
2973 value as value. Otherwise, the header value is appended to the ordered list of
2974 values associated to the header name.
2975
2976 :param class_reply reply: The related Reply object.
2977 :param string name: The header field name.
2978 :param string value: The header field value.
2979
2980.. js:function:: Reply.del_header(REPLY, name)
2981
2982 Remove all occurrences of a header name from the reply object.
2983
2984 :param class_reply reply: The related Reply object.
2985 :param string name: The header field name.
2986
2987.. js:function:: Reply.set_body(REPLY, body)
2988
2989 Set the reply payload.
2990
2991 :param class_reply reply: The related Reply object.
2992 :param string body: The reply payload.
2993
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002994.. _socket_class:
2995
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002996Socket class
2997============
2998
2999.. js:class:: Socket
3000
3001 This class must be compatible with the Lua Socket class. Only the 'client'
3002 functions are available. See the Lua Socket documentation:
3003
3004 `http://w3.impa.br/~diego/software/luasocket/tcp.html
3005 <http://w3.impa.br/~diego/software/luasocket/tcp.html>`_
3006
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01003007.. js:function:: Socket.close(socket)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01003008
3009 Closes a TCP object. The internal socket used by the object is closed and the
3010 local address to which the object was bound is made available to other
3011 applications. No further operations (except for further calls to the close
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01003012 method) are allowed on a closed Socket.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01003013
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01003014 :param class_socket socket: Is the manipulated Socket.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01003015
3016 Note: It is important to close all used sockets once they are not needed,
3017 since, in many systems, each socket uses a file descriptor, which are limited
3018 system resources. Garbage-collected objects are automatically closed before
3019 destruction, though.
3020
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02003021.. js:function:: Socket.connect(socket, address[, port])
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01003022
3023 Attempts to connect a socket object to a remote host.
3024
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01003025
3026 In case of error, the method returns nil followed by a string describing the
3027 error. In case of success, the method returns 1.
3028
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01003029 :param class_socket socket: Is the manipulated Socket.
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02003030 :param string address: can be an IP address or a host name. See below for more
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02003031 information.
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02003032 :param integer port: must be an integer number in the range [1..64K].
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01003033 :returns: 1 or nil.
3034
Bertrand Jacquin874a35c2018-09-10 21:26:07 +01003035 An address field extension permits to use the connect() function to connect to
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02003036 other stream than TCP. The syntax containing a simpleipv4 or ipv6 address is
3037 the basically expected format. This format requires the port.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01003038
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02003039 Other format accepted are a socket path like "/socket/path", it permits to
Bertrand Jacquin874a35c2018-09-10 21:26:07 +01003040 connect to a socket. Abstract namespaces are supported with the prefix
Joseph Herlant02cedc42018-11-13 19:45:17 -08003041 "abns@", and finally a file descriptor can be passed with the prefix "fd@".
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02003042 The prefix "ipv4@", "ipv6@" and "unix@" are also supported. The port can be
Bertrand Jacquin874a35c2018-09-10 21:26:07 +01003043 passed int the string. The syntax "127.0.0.1:1234" is valid. In this case, the
Tim Duesterhus6edab862018-01-06 19:04:45 +01003044 parameter *port* must not be set.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01003045
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01003046.. js:function:: Socket.connect_ssl(socket, address, port)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01003047
3048 Same behavior than the function socket:connect, but uses SSL.
3049
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01003050 :param class_socket socket: Is the manipulated Socket.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01003051 :returns: 1 or nil.
3052
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01003053.. js:function:: Socket.getpeername(socket)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01003054
3055 Returns information about the remote side of a connected client object.
3056
3057 Returns a string with the IP address of the peer, followed by the port number
3058 that peer is using for the connection. In case of error, the method returns
3059 nil.
3060
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01003061 :param class_socket socket: Is the manipulated Socket.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01003062 :returns: a string containing the server information.
3063
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01003064.. js:function:: Socket.getsockname(socket)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01003065
3066 Returns the local address information associated to the object.
3067
3068 The method returns a string with local IP address and a number with the port.
3069 In case of error, the method returns 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 client information.
3073
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01003074.. js:function:: Socket.receive(socket, [pattern [, prefix]])
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01003075
3076 Reads data from a client object, according to the specified read pattern.
3077 Patterns follow the Lua file I/O format, and the difference in performance
3078 between all patterns is negligible.
3079
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01003080 :param class_socket socket: Is the manipulated Socket.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01003081 :param string|integer pattern: Describe what is required (see below).
3082 :param string prefix: A string which will be prefix the returned data.
3083 :returns: a string containing the required data or nil.
3084
3085 Pattern can be any of the following:
3086
3087 * **`*a`**: reads from the socket until the connection is closed. No
3088 end-of-line translation is performed;
3089
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01003090 * **`*l`**: reads a line of text from the Socket. The line is terminated by a
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01003091 LF character (ASCII 10), optionally preceded by a CR character
3092 (ASCII 13). The CR and LF characters are not included in the
3093 returned line. In fact, all CR characters are ignored by the
3094 pattern. This is the default pattern.
3095
3096 * **number**: causes the method to read a specified number of bytes from the
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01003097 Socket. Prefix is an optional string to be concatenated to the
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01003098 beginning of any received data before return.
3099
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02003100 * **empty**: If the pattern is left empty, the default option is `*l`.
3101
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01003102 If successful, the method returns the received pattern. In case of error, the
3103 method returns nil followed by an error message which can be the string
3104 'closed' in case the connection was closed before the transmission was
3105 completed or the string 'timeout' in case there was a timeout during the
3106 operation. Also, after the error message, the function returns the partial
3107 result of the transmission.
3108
3109 Important note: This function was changed severely. It used to support
3110 multiple patterns (but I have never seen this feature used) and now it
3111 doesn't anymore. Partial results used to be returned in the same way as
3112 successful results. This last feature violated the idea that all functions
3113 should return nil on error. Thus it was changed too.
3114
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01003115.. js:function:: Socket.send(socket, data [, start [, end ]])
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01003116
3117 Sends data through client object.
3118
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01003119 :param class_socket socket: Is the manipulated Socket.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01003120 :param string data: The data that will be sent.
3121 :param integer start: The start position in the buffer of the data which will
3122 be sent.
3123 :param integer end: The end position in the buffer of the data which will
3124 be sent.
3125 :returns: see below.
3126
3127 Data is the string to be sent. The optional arguments i and j work exactly
3128 like the standard string.sub Lua function to allow the selection of a
3129 substring to be sent.
3130
3131 If successful, the method returns the index of the last byte within [start,
3132 end] that has been sent. Notice that, if start is 1 or absent, this is
3133 effectively the total number of bytes sent. In case of error, the method
3134 returns nil, followed by an error message, followed by the index of the last
3135 byte within [start, end] that has been sent. You might want to try again from
3136 the byte following that. The error message can be 'closed' in case the
3137 connection was closed before the transmission was completed or the string
3138 'timeout' in case there was a timeout during the operation.
3139
3140 Note: Output is not buffered. For small strings, it is always better to
3141 concatenate them in Lua (with the '..' operator) and send the result in one
3142 call instead of calling the method several times.
3143
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01003144.. js:function:: Socket.setoption(socket, option [, value])
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01003145
3146 Just implemented for compatibility, this cal does nothing.
3147
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01003148.. js:function:: Socket.settimeout(socket, value [, mode])
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01003149
3150 Changes the timeout values for the object. All I/O operations are blocking.
3151 That is, any call to the methods send, receive, and accept will block
3152 indefinitely, until the operation completes. The settimeout method defines a
3153 limit on the amount of time the I/O methods can block. When a timeout time
3154 has elapsed, the affected methods give up and fail with an error code.
3155
3156 The amount of time to wait is specified as the value parameter, in seconds.
3157
Mark Lakes56cc1252018-03-27 09:48:06 +02003158 The timeout modes are not implemented, the only settable timeout is the
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01003159 inactivity time waiting for complete the internal buffer send or waiting for
3160 receive data.
3161
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01003162 :param class_socket socket: Is the manipulated Socket.
Bertrand Jacquin874a35c2018-09-10 21:26:07 +01003163 :param float value: The timeout value. Use floating point to specify
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02003164 milliseconds.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01003165
Thierry FOURNIER31904272017-10-25 12:59:51 +02003166.. _regex_class:
3167
3168Regex class
3169===========
3170
3171.. js:class:: Regex
3172
3173 This class allows the usage of HAProxy regexes because classic lua doesn't
3174 provides regexes. This class inherits the HAProxy compilation options, so the
3175 regexes can be libc regex, pcre regex or pcre JIT regex.
3176
3177 The expression matching number is limited to 20 per regex. The only available
3178 option is case sensitive.
3179
3180 Because regexes compilation is a heavy process, it is better to define all
3181 your regexes in the **body context** and use it during the runtime.
3182
3183.. code-block:: lua
3184
3185 -- Create the regex
3186 st, regex = Regex.new("needle (..) (...)", true);
3187
3188 -- Check compilation errors
3189 if st == false then
3190 print "error: " .. regex
3191 end
3192
3193 -- Match the regexes
3194 print(regex:exec("Looking for a needle in the haystack")) -- true
3195 print(regex:exec("Lokking for a cat in the haystack")) -- false
3196
3197 -- Extract words
3198 st, list = regex:match("Looking for a needle in the haystack")
3199 print(st) -- true
3200 print(list[1]) -- needle in the
3201 print(list[2]) -- in
3202 print(list[3]) -- the
3203
3204.. js:function:: Regex.new(regex, case_sensitive)
3205
3206 Create and compile a regex.
3207
3208 :param string regex: The regular expression according with the libc or pcre
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02003209 standard
Thierry FOURNIER31904272017-10-25 12:59:51 +02003210 :param boolean case_sensitive: Match is case sensitive or not.
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02003211 :returns: boolean status and :ref:`regex_class` or string containing fail
3212 reason.
Thierry FOURNIER31904272017-10-25 12:59:51 +02003213
3214.. js:function:: Regex.exec(regex, str)
3215
3216 Execute the regex.
3217
3218 :param class_regex regex: A :ref:`regex_class` object.
3219 :param string str: The input string will be compared with the compiled regex.
3220 :returns: a boolean status according with the match result.
3221
3222.. js:function:: Regex.match(regex, str)
3223
3224 Execute the regex and return matched expressions.
3225
3226 :param class_map map: A :ref:`regex_class` object.
3227 :param string str: The input string will be compared with the compiled regex.
3228 :returns: a boolean status according with the match result, and
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02003229 a table containing all the string matched in order of declaration.
Thierry FOURNIER31904272017-10-25 12:59:51 +02003230
Thierry FOURNIERdc595002015-12-21 11:13:52 +01003231.. _map_class:
3232
Thierry FOURNIER3def3932015-04-07 11:27:54 +02003233Map class
3234=========
3235
3236.. js:class:: Map
3237
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02003238 This class permits to do some lookups in HAProxy maps. The declared maps can
Bertrand Jacquin874a35c2018-09-10 21:26:07 +01003239 be modified during the runtime through the HAProxy management socket.
Thierry FOURNIER3def3932015-04-07 11:27:54 +02003240
3241.. code-block:: lua
3242
Thierry FOURNIERdc595002015-12-21 11:13:52 +01003243 default = "usa"
Thierry FOURNIER3def3932015-04-07 11:27:54 +02003244
Thierry FOURNIERdc595002015-12-21 11:13:52 +01003245 -- Create and load map
Thierry FOURNIER4dc71972017-01-28 08:33:08 +01003246 geo = Map.new("geo.map", Map._ip);
Thierry FOURNIER3def3932015-04-07 11:27:54 +02003247
Thierry FOURNIERdc595002015-12-21 11:13:52 +01003248 -- Create new fetch that returns the user country
3249 core.register_fetches("country", function(txn)
3250 local src;
3251 local loc;
Thierry FOURNIER3def3932015-04-07 11:27:54 +02003252
Thierry FOURNIERdc595002015-12-21 11:13:52 +01003253 src = txn.f:fhdr("x-forwarded-for");
3254 if (src == nil) then
3255 src = txn.f:src()
3256 if (src == nil) then
3257 return default;
3258 end
3259 end
Thierry FOURNIER3def3932015-04-07 11:27:54 +02003260
Thierry FOURNIERdc595002015-12-21 11:13:52 +01003261 -- Perform lookup
3262 loc = geo:lookup(src);
Thierry FOURNIER3def3932015-04-07 11:27:54 +02003263
Thierry FOURNIERdc595002015-12-21 11:13:52 +01003264 if (loc == nil) then
3265 return default;
3266 end
Thierry FOURNIER3def3932015-04-07 11:27:54 +02003267
Thierry FOURNIERdc595002015-12-21 11:13:52 +01003268 return loc;
3269 end);
Thierry FOURNIER3def3932015-04-07 11:27:54 +02003270
Thierry FOURNIER4dc71972017-01-28 08:33:08 +01003271.. js:attribute:: Map._int
Thierry FOURNIER3def3932015-04-07 11:27:54 +02003272
3273 See the HAProxy configuration.txt file, chapter "Using ACLs and fetching
Ilya Shipitsin2075ca82020-03-06 23:22:22 +05003274 samples" and subchapter "ACL basics" to understand this pattern matching
Thierry FOURNIER3def3932015-04-07 11:27:54 +02003275 method.
3276
Thierry FOURNIER4dc71972017-01-28 08:33:08 +01003277 Note that :js:attr:`Map.int` is also available for compatibility.
3278
3279.. js:attribute:: Map._ip
Thierry FOURNIER3def3932015-04-07 11:27:54 +02003280
3281 See the HAProxy configuration.txt file, chapter "Using ACLs and fetching
Ilya Shipitsin2075ca82020-03-06 23:22:22 +05003282 samples" and subchapter "ACL basics" to understand this pattern matching
Thierry FOURNIER3def3932015-04-07 11:27:54 +02003283 method.
3284
Thierry FOURNIER4dc71972017-01-28 08:33:08 +01003285 Note that :js:attr:`Map.ip` is also available for compatibility.
3286
3287.. js:attribute:: Map._str
Thierry FOURNIER3def3932015-04-07 11:27:54 +02003288
3289 See the HAProxy configuration.txt file, chapter "Using ACLs and fetching
Ilya Shipitsin2075ca82020-03-06 23:22:22 +05003290 samples" and subchapter "ACL basics" to understand this pattern matching
Thierry FOURNIER3def3932015-04-07 11:27:54 +02003291 method.
3292
Thierry FOURNIER4dc71972017-01-28 08:33:08 +01003293 Note that :js:attr:`Map.str` is also available for compatibility.
3294
3295.. js:attribute:: Map._beg
Thierry FOURNIER3def3932015-04-07 11:27:54 +02003296
3297 See the HAProxy configuration.txt file, chapter "Using ACLs and fetching
Ilya Shipitsin2075ca82020-03-06 23:22:22 +05003298 samples" and subchapter "ACL basics" to understand this pattern matching
Thierry FOURNIER3def3932015-04-07 11:27:54 +02003299 method.
3300
Thierry FOURNIER4dc71972017-01-28 08:33:08 +01003301 Note that :js:attr:`Map.beg` is also available for compatibility.
3302
3303.. js:attribute:: Map._sub
Thierry FOURNIER3def3932015-04-07 11:27:54 +02003304
3305 See the HAProxy configuration.txt file, chapter "Using ACLs and fetching
Ilya Shipitsin2075ca82020-03-06 23:22:22 +05003306 samples" and subchapter "ACL basics" to understand this pattern matching
Thierry FOURNIER3def3932015-04-07 11:27:54 +02003307 method.
3308
Thierry FOURNIER4dc71972017-01-28 08:33:08 +01003309 Note that :js:attr:`Map.sub` is also available for compatibility.
3310
3311.. js:attribute:: Map._dir
Thierry FOURNIER3def3932015-04-07 11:27:54 +02003312
3313 See the HAProxy configuration.txt file, chapter "Using ACLs and fetching
Ilya Shipitsin2075ca82020-03-06 23:22:22 +05003314 samples" and subchapter "ACL basics" to understand this pattern matching
Thierry FOURNIER3def3932015-04-07 11:27:54 +02003315 method.
3316
Thierry FOURNIER4dc71972017-01-28 08:33:08 +01003317 Note that :js:attr:`Map.dir` is also available for compatibility.
3318
3319.. js:attribute:: Map._dom
Thierry FOURNIER3def3932015-04-07 11:27:54 +02003320
3321 See the HAProxy configuration.txt file, chapter "Using ACLs and fetching
Ilya Shipitsin2075ca82020-03-06 23:22:22 +05003322 samples" and subchapter "ACL basics" to understand this pattern matching
Thierry FOURNIER3def3932015-04-07 11:27:54 +02003323 method.
3324
Thierry FOURNIER4dc71972017-01-28 08:33:08 +01003325 Note that :js:attr:`Map.dom` is also available for compatibility.
3326
3327.. js:attribute:: Map._end
Thierry FOURNIER3def3932015-04-07 11:27:54 +02003328
3329 See the HAProxy configuration.txt file, chapter "Using ACLs and fetching
Ilya Shipitsin2075ca82020-03-06 23:22:22 +05003330 samples" and subchapter "ACL basics" to understand this pattern matching
Thierry FOURNIER3def3932015-04-07 11:27:54 +02003331 method.
3332
Thierry FOURNIER4dc71972017-01-28 08:33:08 +01003333.. js:attribute:: Map._reg
Thierry FOURNIER3def3932015-04-07 11:27:54 +02003334
3335 See the HAProxy configuration.txt file, chapter "Using ACLs and fetching
Ilya Shipitsin2075ca82020-03-06 23:22:22 +05003336 samples" and subchapter "ACL basics" to understand this pattern matching
Thierry FOURNIER3def3932015-04-07 11:27:54 +02003337 method.
3338
Thierry FOURNIER4dc71972017-01-28 08:33:08 +01003339 Note that :js:attr:`Map.reg` is also available for compatibility.
3340
Thierry FOURNIER3def3932015-04-07 11:27:54 +02003341
3342.. js:function:: Map.new(file, method)
3343
3344 Creates and load a map.
3345
3346 :param string file: Is the file containing the map.
3347 :param integer method: Is the map pattern matching method. See the attributes
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02003348 of the Map class.
Thierry FOURNIER3def3932015-04-07 11:27:54 +02003349 :returns: a class Map object.
Thierry FOURNIER4dc71972017-01-28 08:33:08 +01003350 :see: The Map attributes: :js:attr:`Map._int`, :js:attr:`Map._ip`,
3351 :js:attr:`Map._str`, :js:attr:`Map._beg`, :js:attr:`Map._sub`,
3352 :js:attr:`Map._dir`, :js:attr:`Map._dom`, :js:attr:`Map._end` and
3353 :js:attr:`Map._reg`.
Thierry FOURNIER3def3932015-04-07 11:27:54 +02003354
3355.. js:function:: Map.lookup(map, str)
3356
3357 Perform a lookup in a map.
3358
3359 :param class_map map: Is the class Map object.
3360 :param string str: Is the string used as key.
3361 :returns: a string containing the result or nil if no match.
3362
3363.. js:function:: Map.slookup(map, str)
3364
3365 Perform a lookup in a map.
3366
3367 :param class_map map: Is the class Map object.
3368 :param string str: Is the string used as key.
3369 :returns: a string containing the result or empty string if no match.
3370
Thierry FOURNIERdc595002015-12-21 11:13:52 +01003371.. _applethttp_class:
3372
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02003373AppletHTTP class
Thierry FOURNIERdc595002015-12-21 11:13:52 +01003374================
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02003375
3376.. js:class:: AppletHTTP
3377
3378 This class is used with applets that requires the 'http' mode. The http applet
3379 can be registered with the *core.register_service()* function. They are used
3380 for processing an http request like a server in back of HAProxy.
3381
3382 This is an hello world sample code:
3383
3384.. code-block:: lua
Thierry FOURNIERdc595002015-12-21 11:13:52 +01003385
Pieter Baauw4d7f7662015-11-08 16:38:08 +01003386 core.register_service("hello-world", "http", function(applet)
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02003387 local response = "Hello World !"
3388 applet:set_status(200)
Pieter Baauw2dcb9bc2015-10-01 22:47:12 +02003389 applet:add_header("content-length", string.len(response))
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02003390 applet:add_header("content-type", "text/plain")
Pieter Baauw2dcb9bc2015-10-01 22:47:12 +02003391 applet:start_response()
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02003392 applet:send(response)
3393 end)
3394
Thierry FOURNIERdc595002015-12-21 11:13:52 +01003395.. js:attribute:: AppletHTTP.c
3396
3397 :returns: A :ref:`converters_class`
3398
3399 This attribute contains a Converters class object.
3400
3401.. js:attribute:: AppletHTTP.sc
3402
3403 :returns: A :ref:`converters_class`
3404
3405 This attribute contains a Converters class object. The
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02003406 functions of this object always return a string.
Thierry FOURNIERdc595002015-12-21 11:13:52 +01003407
3408.. js:attribute:: AppletHTTP.f
3409
3410 :returns: A :ref:`fetches_class`
3411
3412 This attribute contains a Fetches class object. Note that the
3413 applet execution place cannot access to a valid HAProxy core HTTP
Ilya Shipitsin2075ca82020-03-06 23:22:22 +05003414 transaction, so some sample fetches related to the HTTP dependent
Thierry FOURNIERdc595002015-12-21 11:13:52 +01003415 values (hdr, path, ...) are not available.
3416
3417.. js:attribute:: AppletHTTP.sf
3418
3419 :returns: A :ref:`fetches_class`
3420
3421 This attribute contains a Fetches class object. The functions of
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02003422 this object always return a string. Note that the applet
Thierry FOURNIERdc595002015-12-21 11:13:52 +01003423 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
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01003427.. js:attribute:: AppletHTTP.method
Thierry FOURNIERdc595002015-12-21 11:13:52 +01003428
3429 :returns: string
3430
3431 The attribute method returns a string containing the HTTP
3432 method.
3433
3434.. js:attribute:: AppletHTTP.version
3435
3436 :returns: string
3437
3438 The attribute version, returns a string containing the HTTP
3439 request version.
3440
3441.. js:attribute:: AppletHTTP.path
3442
3443 :returns: string
3444
3445 The attribute path returns a string containing the HTTP
3446 request path.
3447
3448.. js:attribute:: AppletHTTP.qs
3449
3450 :returns: string
3451
3452 The attribute qs returns a string containing the HTTP
3453 request query string.
3454
3455.. js:attribute:: AppletHTTP.length
3456
3457 :returns: integer
3458
3459 The attribute length returns an integer containing the HTTP
3460 body length.
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02003461
Thierry FOURNIER841475e2015-12-11 17:10:09 +01003462.. js:attribute:: AppletHTTP.headers
3463
Patrick Hemmerc6a1d712018-05-01 21:30:41 -04003464 :returns: table
Thierry FOURNIERdc595002015-12-21 11:13:52 +01003465
Patrick Hemmerc6a1d712018-05-01 21:30:41 -04003466 The attribute headers returns a table containing the HTTP
Thierry FOURNIERdc595002015-12-21 11:13:52 +01003467 headers. The header names are always in lower case. As the header name can be
3468 encountered more than once in each request, the value is indexed with 0 as
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02003469 first index value. The table has this form:
Thierry FOURNIERdc595002015-12-21 11:13:52 +01003470
3471.. code-block:: lua
3472
3473 AppletHTTP.headers['<header-name>'][<header-index>] = "<header-value>"
3474
3475 AppletHTTP.headers["host"][0] = "www.test.com"
3476 AppletHTTP.headers["accept"][0] = "audio/basic q=1"
3477 AppletHTTP.headers["accept"][1] = "audio/*, q=0.2"
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01003478 AppletHTTP.headers["accept"][2] = "*/*, q=0.1"
Thierry FOURNIERdc595002015-12-21 11:13:52 +01003479..
3480
Robin H. Johnson52f5db22017-01-01 13:10:52 -08003481.. js:function:: AppletHTTP.set_status(applet, code [, reason])
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02003482
3483 This function sets the HTTP status code for the response. The allowed code are
3484 from 100 to 599.
3485
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01003486 :param class_AppletHTTP applet: An :ref:`applethttp_class`
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02003487 :param integer code: the status code returned to the client.
Robin H. Johnson52f5db22017-01-01 13:10:52 -08003488 :param string reason: the status reason returned to the client (optional).
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02003489
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01003490.. js:function:: AppletHTTP.add_header(applet, name, value)
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02003491
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02003492 This function adds a header in the response. Duplicated headers are not
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02003493 collapsed. The special header *content-length* is used to determinate the
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02003494 response length. If it does not exist, a *transfer-encoding: chunked* is set,
3495 and all the write from the function *AppletHTTP:send()* become a chunk.
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02003496
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01003497 :param class_AppletHTTP applet: An :ref:`applethttp_class`
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02003498 :param string name: the header name
3499 :param string value: the header value
3500
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01003501.. js:function:: AppletHTTP.start_response(applet)
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02003502
3503 This function indicates to the HTTP engine that it can process and send the
3504 response headers. After this called we cannot add headers to the response; We
3505 cannot use the *AppletHTTP:send()* function if the
3506 *AppletHTTP:start_response()* is not called.
3507
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01003508 :param class_AppletHTTP applet: An :ref:`applethttp_class`
3509
3510.. js:function:: AppletHTTP.getline(applet)
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02003511
3512 This function returns a string containing one line from the http body. If the
3513 data returned doesn't contains a final '\\n' its assumed than its the last
3514 available data before the end of stream.
3515
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01003516 :param class_AppletHTTP applet: An :ref:`applethttp_class`
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02003517 :returns: a string. The string can be empty if we reach the end of the stream.
3518
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01003519.. js:function:: AppletHTTP.receive(applet, [size])
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02003520
3521 Reads data from the HTTP body, according to the specified read *size*. If the
3522 *size* is missing, the function tries to read all the content of the stream
3523 until the end. If the *size* is bigger than the http body, it returns the
Bertrand Jacquin874a35c2018-09-10 21:26:07 +01003524 amount of data available.
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02003525
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01003526 :param class_AppletHTTP applet: An :ref:`applethttp_class`
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02003527 :param integer size: the required read size.
Ilya Shipitsin11057a32020-06-21 21:18:27 +05003528 :returns: always return a string,the string can be empty is the connection is
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02003529 closed.
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02003530
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01003531.. js:function:: AppletHTTP.send(applet, msg)
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02003532
3533 Send the message *msg* on the http request body.
3534
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01003535 :param class_AppletHTTP applet: An :ref:`applethttp_class`
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02003536 :param string msg: the message to send.
3537
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01003538.. js:function:: AppletHTTP.get_priv(applet)
3539
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01003540 Return Lua data stored in the current transaction. If no data are stored,
3541 it returns a nil value.
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01003542
3543 :param class_AppletHTTP applet: An :ref:`applethttp_class`
Bertrand Jacquin874a35c2018-09-10 21:26:07 +01003544 :returns: the opaque data previously stored, or nil if nothing is
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02003545 available.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01003546 :see: :js:func:`AppletHTTP.set_priv`
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01003547
3548.. js:function:: AppletHTTP.set_priv(applet, data)
3549
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02003550 Store any data in the current HAProxy transaction. This action replaces the
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01003551 old stored data.
3552
3553 :param class_AppletHTTP applet: An :ref:`applethttp_class`
3554 :param opaque data: The data which is stored in the transaction.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01003555 :see: :js:func:`AppletHTTP.get_priv`
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01003556
Tim Duesterhus4e172c92020-05-19 13:49:42 +02003557.. js:function:: AppletHTTP.set_var(applet, var, value[, ifexist])
Thierry FOURNIER / OZON.IOc1edafe2016-12-12 16:25:30 +01003558
3559 Converts a Lua type in a HAProxy type and store it in a variable <var>.
3560
3561 :param class_AppletHTTP applet: An :ref:`applethttp_class`
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02003562 :param string var: The variable name according with the HAProxy variable
3563 syntax.
3564 :param type value: The value associated to the variable. The type ca be string
3565 or integer.
3566 :param boolean ifexist: If this parameter is set to true the variable will
3567 only be set if it was defined elsewhere (i.e. used within the configuration).
3568 For global variables (using the "proc" scope), they will only be updated and
3569 never created. It is highly recommended to always set this to true.
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02003570
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01003571 :see: :js:func:`AppletHTTP.unset_var`
3572 :see: :js:func:`AppletHTTP.get_var`
Thierry FOURNIER / OZON.IOc1edafe2016-12-12 16:25:30 +01003573
3574.. js:function:: AppletHTTP.unset_var(applet, var)
3575
3576 Unset the variable <var>.
3577
3578 :param class_AppletHTTP applet: An :ref:`applethttp_class`
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02003579 :param string var: The variable name according with the HAProxy variable
3580 syntax.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01003581 :see: :js:func:`AppletHTTP.set_var`
3582 :see: :js:func:`AppletHTTP.get_var`
Thierry FOURNIER / OZON.IOc1edafe2016-12-12 16:25:30 +01003583
3584.. js:function:: AppletHTTP.get_var(applet, var)
3585
3586 Returns data stored in the variable <var> converter in Lua type.
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.unset_var`
Thierry FOURNIER / OZON.IOc1edafe2016-12-12 16:25:30 +01003593
Thierry FOURNIERdc595002015-12-21 11:13:52 +01003594.. _applettcp_class:
3595
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02003596AppletTCP class
3597===============
3598
3599.. js:class:: AppletTCP
3600
3601 This class is used with applets that requires the 'tcp' mode. The tcp applet
3602 can be registered with the *core.register_service()* function. They are used
3603 for processing a tcp stream like a server in back of HAProxy.
3604
Thierry FOURNIERdc595002015-12-21 11:13:52 +01003605.. js:attribute:: AppletTCP.c
3606
3607 :returns: A :ref:`converters_class`
3608
3609 This attribute contains a Converters class object.
3610
3611.. js:attribute:: AppletTCP.sc
3612
3613 :returns: A :ref:`converters_class`
3614
3615 This attribute contains a Converters class object. The
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02003616 functions of this object always return a string.
Thierry FOURNIERdc595002015-12-21 11:13:52 +01003617
3618.. js:attribute:: AppletTCP.f
3619
3620 :returns: A :ref:`fetches_class`
3621
3622 This attribute contains a Fetches class object.
3623
3624.. js:attribute:: AppletTCP.sf
3625
3626 :returns: A :ref:`fetches_class`
3627
3628 This attribute contains a Fetches class object.
3629
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01003630.. js:function:: AppletTCP.getline(applet)
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02003631
3632 This function returns a string containing one line from the stream. If the
3633 data returned doesn't contains a final '\\n' its assumed than its the last
3634 available data before the end of stream.
3635
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01003636 :param class_AppletTCP applet: An :ref:`applettcp_class`
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02003637 :returns: a string. The string can be empty if we reach the end of the stream.
3638
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01003639.. js:function:: AppletTCP.receive(applet, [size])
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02003640
3641 Reads data from the TCP stream, according to the specified read *size*. If the
3642 *size* is missing, the function tries to read all the content of the stream
3643 until the end.
3644
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01003645 :param class_AppletTCP applet: An :ref:`applettcp_class`
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02003646 :param integer size: the required read size.
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02003647 :returns: always return a string, the string can be empty if the connection is
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02003648 closed.
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02003649
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01003650.. js:function:: AppletTCP.send(appletmsg)
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02003651
3652 Send the message on the stream.
3653
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01003654 :param class_AppletTCP applet: An :ref:`applettcp_class`
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02003655 :param string msg: the message to send.
3656
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01003657.. js:function:: AppletTCP.get_priv(applet)
3658
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01003659 Return Lua data stored in the current transaction. If no data are stored,
3660 it returns a nil value.
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01003661
3662 :param class_AppletTCP applet: An :ref:`applettcp_class`
Bertrand Jacquin874a35c2018-09-10 21:26:07 +01003663 :returns: the opaque data previously stored, or nil if nothing is
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02003664 available.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01003665 :see: :js:func:`AppletTCP.set_priv`
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01003666
3667.. js:function:: AppletTCP.set_priv(applet, data)
3668
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02003669 Store any data in the current HAProxy transaction. This action replaces the
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01003670 old stored data.
3671
3672 :param class_AppletTCP applet: An :ref:`applettcp_class`
3673 :param opaque data: The data which is stored in the transaction.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01003674 :see: :js:func:`AppletTCP.get_priv`
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01003675
Tim Duesterhus4e172c92020-05-19 13:49:42 +02003676.. js:function:: AppletTCP.set_var(applet, var, value[, ifexist])
Thierry FOURNIER / OZON.IOc1edafe2016-12-12 16:25:30 +01003677
3678 Converts a Lua type in a HAProxy type and stores it in a variable <var>.
3679
3680 :param class_AppletTCP applet: An :ref:`applettcp_class`
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02003681 :param string var: The variable name according with the HAProxy variable
3682 syntax.
3683 :param type value: The value associated to the variable. The type can be
3684 string or integer.
3685 :param boolean ifexist: If this parameter is set to true the variable will
3686 only be set if it was defined elsewhere (i.e. used within the configuration).
3687 For global variables (using the "proc" scope), they will only be updated and
3688 never created. It is highly recommended to always set this to true.
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02003689
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01003690 :see: :js:func:`AppletTCP.unset_var`
3691 :see: :js:func:`AppletTCP.get_var`
Thierry FOURNIER / OZON.IOc1edafe2016-12-12 16:25:30 +01003692
3693.. js:function:: AppletTCP.unset_var(applet, var)
3694
3695 Unsets the variable <var>.
3696
3697 :param class_AppletTCP applet: An :ref:`applettcp_class`
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02003698 :param string var: The variable name according with the HAProxy variable
3699 syntax.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01003700 :see: :js:func:`AppletTCP.unset_var`
3701 :see: :js:func:`AppletTCP.set_var`
Thierry FOURNIER / OZON.IOc1edafe2016-12-12 16:25:30 +01003702
3703.. js:function:: AppletTCP.get_var(applet, var)
3704
3705 Returns data stored in the variable <var> converter in Lua type.
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
Adis Nezirovic8878f8e2018-07-13 12:18:33 +02003713StickTable class
3714================
3715
3716.. js:class:: StickTable
3717
3718 **context**: task, action, sample-fetch
3719
3720 This class can be used to access the HAProxy stick tables from Lua.
3721
3722.. js:function:: StickTable.info()
3723
3724 Returns stick table attributes as a Lua table. See HAProxy documentation for
Ilya Shipitsin2272d8a2020-12-21 01:22:40 +05003725 "stick-table" for canonical info, or check out example below.
Adis Nezirovic8878f8e2018-07-13 12:18:33 +02003726
3727 :returns: Lua table
3728
3729 Assume our table has IPv4 key and gpc0 and conn_rate "columns":
3730
3731.. code-block:: lua
3732
3733 {
3734 expire=<int>, # Value in ms
3735 size=<int>, # Maximum table size
3736 used=<int>, # Actual number of entries in table
3737 data={ # Data columns, with types as key, and periods as values
3738 (-1 if type is not rate counter)
3739 conn_rate=<int>,
3740 gpc0=-1
3741 },
3742 length=<int>, # max string length for string table keys, key length
3743 # otherwise
3744 nopurge=<boolean>, # purge oldest entries when table is full
3745 type="ip" # can be "ip", "ipv6", "integer", "string", "binary"
3746 }
3747
3748.. js:function:: StickTable.lookup(key)
3749
3750 Returns stick table entry for given <key>
3751
3752 :param string key: Stick table key (IP addresses and strings are supported)
3753 :returns: Lua table
3754
3755.. js:function:: StickTable.dump([filter])
3756
3757 Returns all entries in stick table. An optional filter can be used
3758 to extract entries with specific data values. Filter is a table with valid
3759 comparison operators as keys followed by data type name and value pairs.
3760 Check out the HAProxy docs for "show table" for more details. For the
3761 reference, the supported operators are:
Aurelien DARRAGON21f7ebb2023-03-13 19:49:31 +01003762
Adis Nezirovic8878f8e2018-07-13 12:18:33 +02003763 "eq", "ne", "le", "lt", "ge", "gt"
3764
3765 For large tables, execution of this function can take a long time (for
3766 HAProxy standards). That's also true when filter is used, so take care and
3767 measure the impact.
3768
3769 :param table filter: Stick table filter
3770 :returns: Stick table entries (table)
3771
3772 See below for example filter, which contains 4 entries (or comparisons).
3773 (Maximum number of filter entries is 4, defined in the source code)
3774
3775.. code-block:: lua
3776
3777 local filter = {
3778 {"gpc0", "gt", 30}, {"gpc1", "gt", 20}}, {"conn_rate", "le", 10}
3779 }
3780
Christopher Faulet0f3c8902020-01-31 18:57:12 +01003781.. _action_class:
3782
3783Action class
3784=============
3785
3786.. js:class:: Act
3787
3788 **context**: action
3789
3790 This class contains all return codes an action may return. It is the lua
3791 equivalent to HAProxy "ACT_RET_*" code.
3792
3793.. code-block:: lua
3794
3795 core.register_action("deny", { "http-req" }, function (txn)
3796 return act.DENY
3797 end)
3798..
3799.. js:attribute:: act.CONTINUE
3800
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02003801 This attribute is an integer (0). It instructs HAProxy to continue the
3802 current ruleset processing on the message. It is the default return code
3803 for a lua action.
Christopher Faulet0f3c8902020-01-31 18:57:12 +01003804
3805 :returns: integer
3806
3807.. js:attribute:: act.STOP
3808
3809 This attribute is an integer (1). It instructs HAProxy to stop the current
3810 ruleset processing on the message.
3811
3812.. js:attribute:: act.YIELD
3813
3814 This attribute is an integer (2). It instructs HAProxy to temporarily pause
3815 the message processing. It will be resumed later on the same rule. The
3816 corresponding lua script is re-executed for the start.
3817
3818.. js:attribute:: act.ERROR
3819
3820 This attribute is an integer (3). It triggers an internal errors The message
3821 processing is stopped and the transaction is terminated. For HTTP streams, an
3822 HTTP 500 error is returned to the client.
3823
3824 :returns: integer
3825
3826.. js:attribute:: act.DONE
3827
3828 This attribute is an integer (4). It instructs HAProxy to stop the message
3829 processing.
3830
3831 :returns: integer
3832
3833.. js:attribute:: act.DENY
3834
3835 This attribute is an integer (5). It denies the current message. The message
3836 processing is stopped and the transaction is terminated. For HTTP streams, an
3837 HTTP 403 error is returned to the client if the deny is returned during the
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02003838 request analysis. During the response analysis, a HTTP 502 error is returned
Christopher Faulet0f3c8902020-01-31 18:57:12 +01003839 and the server response is discarded.
3840
3841 :returns: integer
3842
3843.. js:attribute:: act.ABORT
3844
3845 This attribute is an integer (6). It aborts the current message. The message
3846 processing is stopped and the transaction is terminated. For HTTP streams,
Willy Tarreau714f3452021-05-09 06:47:26 +02003847 HAProxy assumes a response was already sent to the client. From the Lua
Christopher Faulet0f3c8902020-01-31 18:57:12 +01003848 actions point of view, when this code is used, the transaction is terminated
3849 with no reply.
3850
3851 :returns: integer
3852
3853.. js:attribute:: act.INVALID
3854
3855 This attribute is an integer (7). It triggers an internal errors. The message
3856 processing is stopped and the transaction is terminated. For HTTP streams, an
3857 HTTP 400 error is returned to the client if the error is returned during the
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02003858 request analysis. During the response analysis, a HTTP 502 error is returned
Christopher Faulet0f3c8902020-01-31 18:57:12 +01003859 and the server response is discarded.
3860
3861 :returns: integer
Adis Nezirovic8878f8e2018-07-13 12:18:33 +02003862
Christopher Faulet2c2c2e32020-01-31 19:07:52 +01003863.. js:function:: act:wake_time(milliseconds)
3864
3865 **context**: action
3866
3867 Set the script pause timeout to the specified time, defined in
3868 milliseconds.
3869
3870 :param integer milliseconds: the required milliseconds.
3871
3872 This function may be used when a lua action returns `act.YIELD`, to force its
3873 wake-up at most after the specified number of milliseconds.
3874
Christopher Faulet5a2c6612021-08-15 20:35:25 +02003875.. _filter_class:
3876
3877Filter class
3878=============
3879
3880.. js:class:: filter
3881
3882 **context**: filter
3883
3884 This class contains return codes some filter callback functions may return. It
3885 also contains configuration flags and some helper functions. To understand how
3886 the filter API works, see `doc/internal/filters.txt` documentation.
3887
3888.. js:attribute:: filter.CONTINUE
3889
3890 This attribute is an integer (1). It may be returned by some filter callback
3891 functions to instruct this filtering step is finished for this filter.
3892
3893.. js:attribute:: filter.WAIT
3894
3895 This attribute is an integer (0). It may be returned by some filter callback
3896 functions to instruct the filtering must be paused, waiting for more data or
3897 for an external event depending on this filter.
3898
3899.. js:attribute:: filter.ERROR
3900
3901 This attribute is an integer (-1). It may be returned by some filter callback
3902 functions to trigger an error.
3903
3904.. js:attribute:: filter.FLT_CFG_FL_HTX
3905
3906 This attribute is a flag corresponding to the filter flag FLT_CFG_FL_HTX. When
3907 it is set for a filter, it means the filter is able to filter HTTP streams.
3908
3909.. js:function:: filter.register_data_filter(chn)
3910
3911 **context**: filter
3912
3913 Enable the data filtering on the channel **chn** for the current filter. It
Ilya Shipitsinff0f2782021-08-22 22:18:07 +05003914 may be called at any time from any callback functions proceeding the data
Christopher Faulet5a2c6612021-08-15 20:35:25 +02003915 analysis.
3916
3917 :param class_Channel chn: A :ref:`channel_class`.
3918
3919.. js:function:: filter.unregister_data_filter(chn)
3920
3921 **context**: filter
3922
3923 Disable the data filtering on the channel **chn** for the current filter. It
3924 may be called at any time from any callback functions.
3925
3926 :param class_Channel chn: A :ref:`channel_class`.
3927
3928.. js:function:: filter.wake_time(milliseconds)
3929
3930 **context**: filter
3931
3932 Set the script pause timeout to the specified time, defined in
3933 milliseconds.
3934
3935 :param integer milliseconds: the required milliseconds.
3936
3937 This function may be used from any lua filter callback function to force its
3938 wake-up at most after the specified number of milliseconds. Especially, when
3939 `filter.CONTINUE` is returned.
3940
3941
3942A filters is declared using :js:func:`core.register_filter()` function. The
3943provided class will be used to instantiate filters. It may define following
3944attributes:
3945
3946* id: The filter identifier. It is a string that identifies the filter and is
3947 optional.
3948
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02003949* flags: The filter flags. Only :js:attr:`filter.FLT_CFG_FL_HTX` may be set
3950 for now.
Christopher Faulet5a2c6612021-08-15 20:35:25 +02003951
3952Such filter class must also define all required callback functions in the
3953following list. Note that :js:func:`Filter.new()` must be defined otherwise the
Ilya Shipitsinff0f2782021-08-22 22:18:07 +05003954filter is ignored. Others are optional.
Christopher Faulet5a2c6612021-08-15 20:35:25 +02003955
3956* .. js:function:: FILTER.new()
3957
3958 Called to instantiate a new filter. This function must be defined.
3959
3960 :returns: a Lua object that will be used as filter instance for the current
3961 stream.
3962
3963* .. js:function:: FILTER.start_analyze(flt, txn, chn)
3964
3965 Called when the analysis starts on the channel **chn**.
3966
3967* .. js:function:: FILTER.end_analyze(flt, txn, chn)
3968
3969 Called when the analysis ends on the channel **chn**.
3970
3971* .. js:function:: FILTER.http_headers(flt, txn, http_msg)
3972
3973 Called just before the HTTP payload analysis and after any processing on the
3974 HTTP message **http_msg**. This callback functions is only called for HTTP
3975 streams.
3976
3977* .. js:function:: FILTER.http_payload(flt, txn, http_msg)
3978
3979 Called during the HTTP payload analysis on the HTTP message **http_msg**. This
3980 callback functions is only called for HTTP streams.
3981
3982* .. js:function:: FILTER.http_end(flt, txn, http_msg)
3983
3984 Called after the HTTP payload analysis on the HTTP message **http_msg**. This
3985 callback functions is only called for HTTP streams.
3986
3987* .. js:function:: FILTER.tcp_payload(flt, txn, chn)
3988
3989 Called during the TCP payload analysis on the channel **chn**.
3990
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02003991Here is a full example:
Christopher Faulet5a2c6612021-08-15 20:35:25 +02003992
3993.. code-block:: lua
3994
3995 Trace = {}
3996 Trace.id = "Lua trace filter"
3997 Trace.flags = filter.FLT_CFG_FL_HTX;
3998 Trace.__index = Trace
3999
4000 function Trace:new()
4001 local trace = {}
4002 setmetatable(trace, Trace)
4003 trace.req_len = 0
4004 trace.res_len = 0
4005 return trace
4006 end
4007
4008 function Trace:start_analyze(txn, chn)
4009 if chn:is_resp() then
4010 print("Start response analysis")
4011 else
4012 print("Start request analysis")
4013 end
4014 filter.register_data_filter(self, chn)
4015 end
4016
4017 function Trace:end_analyze(txn, chn)
4018 if chn:is_resp() then
4019 print("End response analysis: "..self.res_len.." bytes filtered")
4020 else
4021 print("End request analysis: "..self.req_len.." bytes filtered")
4022 end
4023 end
4024
4025 function Trace:http_headers(txn, http_msg)
4026 stline = http_msg:get_stline()
4027 if http_msg.channel:is_resp() then
4028 print("response:")
4029 print(stline.version.." "..stline.code.." "..stline.reason)
4030 else
4031 print("request:")
4032 print(stline.method.." "..stline.uri.." "..stline.version)
4033 end
4034
4035 for n, hdrs in pairs(http_msg:get_headers()) do
4036 for i,v in pairs(hdrs) do
4037 print(n..": "..v)
4038 end
4039 end
4040 return filter.CONTINUE
4041 end
4042
4043 function Trace:http_payload(txn, http_msg)
4044 body = http_msg:body(-20000)
4045 if http_msg.channel:is_resp() then
4046 self.res_len = self.res_len + body:len()
4047 else
4048 self.req_len = self.req_len + body:len()
4049 end
4050 end
4051
4052 core.register_filter("trace", Trace, function(trace, args)
4053 return trace
4054 end)
4055
4056..
4057
4058.. _httpmessage_class:
4059
4060HTTPMessage class
4061===================
4062
4063.. js:class:: HTTPMessage
4064
4065 **context**: filter
4066
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02004067 This class contains all functions to manipulate a HTTP message. For now, this
Christopher Faulet5a2c6612021-08-15 20:35:25 +02004068 class is only available from a filter context.
4069
4070.. js:function:: HTTPMessage.add_header(http_msg, name, value)
4071
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02004072 Appends a HTTP header field in the HTTP message **http_msg** whose name is
Christopher Faulet5a2c6612021-08-15 20:35:25 +02004073 specified in **name** and whose value is defined in **value**.
4074
4075 :param class_httpmessage http_msg: The manipulated HTTP message.
4076 :param string name: The header name.
4077 :param string value: The header value.
4078
4079.. js:function:: HTTPMessage.append(http_msg, string)
4080
4081 This function copies the string **string** at the end of incoming data of the
4082 HTTP message **http_msg**. The function returns the copied length on success
4083 or -1 if data cannot be copied.
4084
4085 Same that :js:func:`HTTPMessage.insert(http_msg, string, http_msg:input())`.
4086
4087 :param class_httpmessage http_msg: The manipulated HTTP message.
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02004088 :param string string: The data to copy at the end of incoming data.
Christopher Faulet5a2c6612021-08-15 20:35:25 +02004089 :returns: an integer containing the amount of bytes copied or -1.
4090
4091.. js:function:: HTTPMessage.body(http_msgl[, offset[, length]])
4092
4093 This function returns **length** bytes of incoming data from the HTTP message
4094 **http_msg**, starting at the offset **offset**. The data are not removed from
4095 the buffer.
4096
4097 By default, if no length is provided, all incoming data found, starting at the
4098 given offset, are returned. If **length** is set to -1, the function tries to
4099 retrieve a maximum of data. Because it is called in the filter context, it
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02004100 never yield. Not providing an offset is the same as setting it to 0. A
Ilya Shipitsinff0f2782021-08-22 22:18:07 +05004101 positive offset is relative to the beginning of incoming data of the
Christopher Faulet5a2c6612021-08-15 20:35:25 +02004102 http_message buffer while negative offset is relative to their end.
4103
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02004104 If there is no incoming data and the HTTP message can't receive more data,
4105 a 'nil' value is returned.
Christopher Faulet5a2c6612021-08-15 20:35:25 +02004106
4107 :param class_httpmessage http_msg: The manipulated HTTP message.
4108 :param integer offset: *optional* The offset in incoming data to start to get
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02004109 data. 0 by default. May be negative to be relative to the end of incoming
4110 data.
4111 :param integer length: *optional* The expected length of data to retrieve.
4112 All incoming data by default. May be set to -1 to get a maximum of data.
Christopher Faulet5a2c6612021-08-15 20:35:25 +02004113 :returns: a string containing the data found or nil.
4114
4115.. js:function:: HTTPMessage.eom(http_msg)
4116
4117 This function returns true if the end of message is reached for the HTTP
4118 message **http_msg**.
4119
4120 :param class_httpmessage http_msg: The manipulated HTTP message.
4121 :returns: an integer containing the amount of available bytes.
4122
4123.. js:function:: HTTPMessage.del_header(http_msg, name)
4124
4125 Removes all HTTP header fields in the HTTP message **http_msg** whose name is
4126 specified in **name**.
4127
4128 :param class_httpmessage http_msg: The manipulated http message.
4129 :param string name: The header name.
4130
4131.. js:function:: HTTPMessage.get_headers(http_msg)
4132
4133 Returns a table containing all the headers of the HTTP message **http_msg**.
4134
4135 :param class_httpmessage http_msg: The manipulated http message.
4136 :returns: table of headers.
4137
4138 This is the form of the returned table:
4139
4140.. code-block:: lua
4141
4142 http_msg:get_headers()['<header-name>'][<header-index>] = "<header-value>"
4143
4144 local hdr = http_msg:get_headers()
4145 hdr["host"][0] = "www.test.com"
4146 hdr["accept"][0] = "audio/basic q=1"
4147 hdr["accept"][1] = "audio/*, q=0.2"
4148 hdr["accept"][2] = "*.*, q=0.1"
4149..
4150
4151.. js:function:: HTTPMessage.get_stline(http_msg)
4152
4153 Returns a table containing the start-line of the HTTP message **http_msg**.
4154
4155 :param class_httpmessage http_msg: The manipulated http message.
4156 :returns: the start-line.
4157
4158 This is the form of the returned table:
4159
4160.. code-block:: lua
4161
4162 -- for the request :
4163 {"method" = string, "uri" = string, "version" = string}
4164
4165 -- for the response:
4166 {"version" = string, "code" = string, "reason" = string}
4167..
4168
4169.. js:function:: HTTPMessage.forward(http_msg, length)
4170
4171 This function forwards **length** bytes of data from the HTTP message
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02004172 **http_msg**. Because it is called in the filter context, it never yields. Only
Christopher Faulet5a2c6612021-08-15 20:35:25 +02004173 available incoming data may be forwarded, event if the requested length
4174 exceeds the available amount of incoming data. It returns the amount of data
4175 forwarded.
4176
4177 :param class_httpmessage http_msg: The manipulated HTTP message.
4178 :param integer int: The amount of data to forward.
4179
4180.. js:function:: HTTPMessage.input(http_msg)
4181
4182 This function returns the length of incoming data in the HTTP message
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02004183 **http_msg** from the filter point of view.
Christopher Faulet5a2c6612021-08-15 20:35:25 +02004184
4185 :param class_httpmessage http_msg: The manipulated HTTP message.
4186 :returns: an integer containing the amount of available bytes.
4187
4188.. js:function:: HTTPMessage.insert(http_msg, string[, offset])
4189
4190 This function copies the string **string** at the offset **offset** in
4191 incoming data of the HTTP message **http_msg**. The function returns the
4192 copied length on success or -1 if data cannot be copied.
4193
4194 By default, if no offset is provided, the string is copied in front of
Ilya Shipitsinff0f2782021-08-22 22:18:07 +05004195 incoming data. A positive offset is relative to the beginning of incoming data
Christopher Faulet5a2c6612021-08-15 20:35:25 +02004196 of the HTTP message while negative offset is relative to their end.
4197
4198 :param class_httpmessage http_msg: The manipulated HTTP message.
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02004199 :param string string: The data to copy into incoming data.
4200 :param integer offset: *optional* The offset in incoming data where to copy
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02004201 data. 0 by default. May be negative to be relative to the end of incoming
4202 data.
Christopher Faulet5a2c6612021-08-15 20:35:25 +02004203 :returns: an integer containing the amount of bytes copied or -1.
4204
4205.. js:function:: HTTPMessage.is_full(http_msg)
4206
4207 This function returns true if the HTTP message **http_msg** is full.
4208
4209 :param class_httpmessage http_msg: The manipulated HTTP message.
4210 :returns: a boolean
4211
4212.. js:function:: HTTPMessage.is_resp(http_msg)
4213
4214 This function returns true if the HTTP message **http_msg** is the response
4215 one.
4216
4217 :param class_httpmessage http_msg: The manipulated HTTP message.
4218 :returns: a boolean
4219
4220.. js:function:: HTTPMessage.may_recv(http_msg)
4221
4222 This function returns true if the HTTP message **http_msg** may still receive
4223 data.
4224
4225 :param class_httpmessage http_msg: The manipulated HTTP message.
4226 :returns: a boolean
4227
4228.. js:function:: HTTPMessage.output(http_msg)
4229
4230 This function returns the length of outgoing data of the HTTP message
4231 **http_msg**.
4232
4233 :param class_httpmessage http_msg: The manipulated HTTP message.
4234 :returns: an integer containing the amount of available bytes.
4235
4236.. js:function:: HTTPMessage.prepend(http_msg, string)
4237
4238 This function copies the string **string** in front of incoming data of the
4239 HTTP message **http_msg**. The function returns the copied length on success
4240 or -1 if data cannot be copied.
4241
4242 Same that :js:func:`HTTPMessage.insert(http_msg, string, 0)`.
4243
4244 :param class_httpmessage http_msg: The manipulated HTTP message.
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02004245 :param string string: The data to copy in front of incoming data.
Christopher Faulet5a2c6612021-08-15 20:35:25 +02004246 :returns: an integer containing the amount of bytes copied or -1.
4247
4248.. js:function:: HTTPMessage.remove(http_msg[, offset[, length]])
4249
4250 This function removes **length** bytes of incoming data of the HTTP message
4251 **http_msg**, starting at offset **offset**. This function returns number of
4252 bytes removed on success.
4253
4254 By default, if no length is provided, all incoming data, starting at the given
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02004255 offset, are removed. Not providing an offset is the same that setting it
Ilya Shipitsinff0f2782021-08-22 22:18:07 +05004256 to 0. A positive offset is relative to the beginning of incoming data of the
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02004257 HTTP message while negative offset is relative to the end.
Christopher Faulet5a2c6612021-08-15 20:35:25 +02004258
4259 :param class_httpmessage http_msg: The manipulated HTTP message.
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02004260 :param integer offset: *optional* The offset in incoming data where to start
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02004261 to remove data. 0 by default. May be negative to be relative to the end of
4262 incoming data.
Christopher Faulet5a2c6612021-08-15 20:35:25 +02004263 :param integer length: *optional* The length of data to remove. All incoming
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02004264 data by default.
Christopher Faulet5a2c6612021-08-15 20:35:25 +02004265 :returns: an integer containing the amount of bytes removed.
4266
4267.. js:function:: HTTPMessage.rep_header(http_msg, name, regex, replace)
4268
4269 Matches the regular expression in all occurrences of header field **name**
4270 according to regex **regex**, and replaces them with the string **replace**.
4271 The replacement value can contain back references like \1, \2, ... This
4272 function acts on whole header lines, regardless of the number of values they
4273 may contain.
4274
4275 :param class_httpmessage http_msg: The manipulated HTTP message.
4276 :param string name: The header name.
4277 :param string regex: The match regular expression.
4278 :param string replace: The replacement value.
4279
4280.. js:function:: HTTPMessage.rep_value(http_msg, name, regex, replace)
4281
4282 Matches the regular expression on every comma-delimited value of header field
4283 **name** according to regex **regex**, and replaces them with the string
4284 **replace**. The replacement value can contain back references like \1, \2,
4285 ...
4286
4287 :param class_httpmessage http_msg: The manipulated HTTP message.
4288 :param string name: The header name.
4289 :param string regex: The match regular expression.
4290 :param string replace: The replacement value.
4291
4292.. js:function:: HTTPMessage.send(http_msg, string)
4293
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02004294 This function requires immediate send of the string **string**. It means the
Ilya Shipitsinff0f2782021-08-22 22:18:07 +05004295 string is copied at the beginning of incoming data of the HTTP message
Christopher Faulet5a2c6612021-08-15 20:35:25 +02004296 **http_msg** and immediately forwarded. Because it is called in the filter
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02004297 context, it never yields.
Christopher Faulet5a2c6612021-08-15 20:35:25 +02004298
4299 :param class_httpmessage http_msg: The manipulated HTTP message.
4300 :param string string: The data to send.
4301 :returns: an integer containing the amount of bytes copied or -1.
4302
4303.. js:function:: HTTPMessage.set(http_msg, string[, offset[, length]])
4304
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02004305 This function replaces **length** bytes of incoming data of the HTTP message
Christopher Faulet5a2c6612021-08-15 20:35:25 +02004306 **http_msg**, starting at offset **offset**, by the string **string**. The
4307 function returns the copied length on success or -1 if data cannot be copied.
4308
4309 By default, if no length is provided, all incoming data, starting at the given
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02004310 offset, are replaced. Not providing an offset is the same as setting it
Ilya Shipitsinff0f2782021-08-22 22:18:07 +05004311 to 0. A positive offset is relative to the beginning of incoming data of the
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02004312 HTTP message while negative offset is relative to the end.
Christopher Faulet5a2c6612021-08-15 20:35:25 +02004313
4314 :param class_httpmessage http_msg: The manipulated HTTP message.
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02004315 :param string string: The data to copy into incoming data.
4316 :param integer offset: *optional* The offset in incoming data where to start
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02004317 the data replacement. 0 by default. May be negative to be relative to the
4318 end of incoming data.
Christopher Faulet5a2c6612021-08-15 20:35:25 +02004319 :param integer length: *optional* The length of data to replace. All incoming
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02004320 data by default.
Christopher Faulet5a2c6612021-08-15 20:35:25 +02004321 :returns: an integer containing the amount of bytes copied or -1.
4322
4323.. js:function:: HTTPMessage.set_eom(http_msg)
4324
4325 This function set the end of message for the HTTP message **http_msg**.
4326
4327 :param class_httpmessage http_msg: The manipulated HTTP message.
4328
4329.. js:function:: HTTPMessage.set_header(http_msg, name, value)
4330
4331 This variable replace all occurrence of all header matching the name **name**,
4332 by only one containing the value **value**.
4333
4334 :param class_httpmessage http_msg: The manipulated HTTP message.
4335 :param string name: The header name.
4336 :param string value: The header value.
4337
4338 This function does the same work as the following code:
4339
4340.. code-block:: lua
4341
4342 http_msg:del_header("header")
4343 http_msg:add_header("header", "value")
4344..
4345
4346.. js:function:: HTTPMessage.set_method(http_msg, method)
4347
4348 Rewrites the request method with the string **method**. The HTTP message
4349 **http_msg** must be the request.
4350
4351 :param class_httpmessage http_msg: The manipulated HTTP message.
4352 :param string method: The new method.
4353
4354.. js:function:: HTTPMessage.set_path(http_msg, path)
4355
4356 Rewrites the request path with the string **path**. The HTTP message
4357 **http_msg** must be the request.
4358
4359 :param class_httpmessage http_msg: The manipulated HTTP message.
4360 :param string method: The new method.
4361
4362.. js:function:: HTTPMessage.set_query(http_msg, query)
4363
4364 Rewrites the request's query string which appears after the first question
4365 mark ("?") with the string **query**. The HTTP message **http_msg** must be
4366 the request.
4367
4368 :param class_httpmessage http_msg: The manipulated HTTP message.
4369 :param string query: The new query.
4370
4371.. js:function:: HTTPMessage.set_status(http_msg, status[, reason])
4372
Ilya Shipitsinff0f2782021-08-22 22:18:07 +05004373 Rewrites the response status code with the integer **code** and optional the
Christopher Faulet5a2c6612021-08-15 20:35:25 +02004374 reason **reason**. If no custom reason is provided, it will be generated from
4375 the status. The HTTP message **http_msg** must be the response.
4376
4377 :param class_httpmessage http_msg: The manipulated HTTP message.
4378 :param integer status: The new response status code.
4379 :param string reason: The new response reason (optional).
4380
4381.. js:function:: HTTPMessage.set_uri(http_msg, uri)
4382
4383 Rewrites the request URI with the string **uri**. The HTTP message
4384 **http_msg** must be the request.
4385
4386 :param class_httpmessage http_msg: The manipulated HTTP message.
4387 :param string uri: The new uri.
4388
4389.. js:function:: HTTPMessage.unset_eom(http_msg)
4390
4391 This function remove the end of message for the HTTP message **http_msg**.
4392
4393 :param class_httpmessage http_msg: The manipulated HTTP message.
4394
William Lallemand10cea5c2022-03-30 16:02:43 +02004395.. _CertCache_class:
4396
4397CertCache class
4398================
4399
4400.. js:class:: CertCache
4401
4402 This class allows to update an SSL certificate file in the memory of the
4403 current HAProxy process. It will do the same as "set ssl cert" + "commit ssl
4404 cert" over the HAProxy CLI.
4405
4406.. js:function:: CertCache.set(certificate)
4407
4408 This function updates a certificate in memory.
4409
4410 :param table certificate: A table containing the fields to update.
4411 :param string certificate.filename: The mandatory filename of the certificate
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02004412 to update, it must already exist in memory.
William Lallemand10cea5c2022-03-30 16:02:43 +02004413 :param string certificate.crt: A certificate in the PEM format. It can also
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02004414 contain a private key.
William Lallemand10cea5c2022-03-30 16:02:43 +02004415 :param string certificate.key: A private key in the PEM format.
4416 :param string certificate.ocsp: An OCSP response in base64. (cf management.txt)
4417 :param string certificate.issuer: The certificate of the OCSP issuer.
4418 :param string certificate.sctl: An SCTL file.
4419
4420.. code-block:: lua
4421
4422 CertCache.set{filename="certs/localhost9994.pem.rsa", crt=crt}
4423
4424
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01004425External Lua libraries
4426======================
4427
4428A lot of useful lua libraries can be found here:
4429
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02004430* Lua toolbox has been superseded by
4431 `https://luarocks.org/ <https://luarocks.org/>`_
4432
4433 The old lua toolbox source code is still available here
4434 `https://github.com/catwell/lua-toolbox <https://github.com/catwell/lua-toolbox>`_ (DEPRECATED)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01004435
Ilya Shipitsin2075ca82020-03-06 23:22:22 +05004436Redis client library:
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01004437
4438* `https://github.com/nrk/redis-lua <https://github.com/nrk/redis-lua>`_
4439
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02004440This is an example about the usage of the Redis library within HAProxy.
4441Note that each call to any function of this library can throw an error if
4442the socket connection fails.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01004443
4444.. code-block:: lua
4445
4446 -- load the redis library
4447 local redis = require("redis");
4448
4449 function do_something(txn)
4450
4451 -- create and connect new tcp socket
4452 local tcp = core.tcp();
4453 tcp:settimeout(1);
4454 tcp:connect("127.0.0.1", 6379);
4455
4456 -- use the redis library with this new socket
4457 local client = redis.connect({socket=tcp});
4458 client:ping();
4459
4460 end
4461
4462OpenSSL:
4463
4464* `http://mkottman.github.io/luacrypto/index.html
4465 <http://mkottman.github.io/luacrypto/index.html>`_
4466
4467* `https://github.com/brunoos/luasec/wiki
4468 <https://github.com/brunoos/luasec/wiki>`_