blob: 8a82a1685580031626b1ac3a2535d4b38a6098dd [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
220 **context**: task, action, sample-fetch, converter
221
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
235 **context**: task, action, sample-fetch, converter
236
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
246 **context**: task, action, sample-fetch, converter
247
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
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +0100761.. js:function:: core.register_cli([path], usage, func)
762
763 **context**: body
764
Aurelien DARRAGON53901f42022-10-13 19:49:42 +0200765 Register a custom cli that will be available from haproxy stats socket.
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +0100766
767 :param array path: is the sequence of word for which the cli execute the Lua
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +0200768 binding.
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +0100769 :param string usage: is the usage message displayed in the help.
770 :param function func: is the Lua function called to handle the CLI commands.
771
772 The prototype of the Lua function used as argument is:
773
774.. code-block:: lua
775
776 function(AppletTCP, [arg1, [arg2, [...]]])
777..
778
779 I/O are managed with the :ref:`applettcp_class` object. Args are given as
Bertrand Jacquin874a35c2018-09-10 21:26:07 +0100780 parameter. The args embed the registered path. If the path is declared like
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +0100781 this:
782
783.. code-block:: lua
784
785 core.register_cli({"show", "ssl", "stats"}, "Display SSL stats..", function(applet, arg1, arg2, arg3, arg4, arg5)
786 end)
787..
788
789 And we execute this in the prompt:
790
791.. code-block:: text
792
793 > prompt
794 > show ssl stats all
795..
796
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +0200797 Then, arg1, arg2 and arg3 will contains respectively "show", "ssl" and
798 "stats".
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +0100799 arg4 will contain "all". arg5 contains nil.
800
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100801.. js:function:: core.set_nice(nice)
802
803 **context**: task, action, sample-fetch, converter
804
805 Change the nice of the current task or current session.
Thierry FOURNIER2e4893c2015-03-18 13:37:27 +0100806
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100807 :param integer nice: the nice value, it must be between -1024 and 1024.
808
809.. js:function:: core.set_map(filename, key, value)
810
811 **context**: init, task, action, sample-fetch, converter
812
Bertrand Jacquin874a35c2018-09-10 21:26:07 +0100813 Set the value *value* associated to the key *key* in the map referenced by
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100814 *filename*.
815
Thierry FOURNIERdc595002015-12-21 11:13:52 +0100816 :param string filename: the Map reference
817 :param string key: the key to set or replace
818 :param string value: the associated value
819
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100820.. js:function:: core.sleep(int seconds)
821
822 **context**: body, init, task, action
823
824 The `core.sleep()` functions stop the Lua execution between specified seconds.
825
826 :param integer seconds: the required seconds.
827
828.. js:function:: core.tcp()
829
830 **context**: init, task, action
831
832 This function returns a new object of a *socket* class.
833
Thierry FOURNIERdc595002015-12-21 11:13:52 +0100834 :returns: A :ref:`socket_class` object.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100835
William Lallemand00a15022021-11-19 16:02:44 +0100836.. js:function:: core.httpclient()
837
838 **context**: init, task, action
839
840 This function returns a new object of a *httpclient* class.
841
842 :returns: A :ref:`httpclient_class` object.
843
Thierry Fournier1de16592016-01-27 09:49:07 +0100844.. js:function:: core.concat()
845
846 **context**: body, init, task, action, sample-fetch, converter
847
Bertrand Jacquin874a35c2018-09-10 21:26:07 +0100848 This function returns a new concat object.
Thierry Fournier1de16592016-01-27 09:49:07 +0100849
850 :returns: A :ref:`concat_class` object.
851
Thierry FOURNIER0a99b892015-08-26 00:14:17 +0200852.. js:function:: core.done(data)
853
854 **context**: body, init, task, action, sample-fetch, converter
855
856 :param any data: Return some data for the caller. It is useful with
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +0200857 sample-fetches and sample-converters.
Thierry FOURNIER0a99b892015-08-26 00:14:17 +0200858
859 Immediately stops the current Lua execution and returns to the caller which
860 may be a sample fetch, a converter or an action and returns the specified
Thierry Fournier4234dbd2020-11-28 13:18:23 +0100861 value (ignored for actions and init). It is used when the LUA process finishes
862 its work and wants to give back the control to HAProxy without executing the
Thierry FOURNIER0a99b892015-08-26 00:14:17 +0200863 remaining code. It can be seen as a multi-level "return".
864
Thierry FOURNIER486f5a02015-03-16 15:13:03 +0100865.. js:function:: core.yield()
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100866
867 **context**: task, action, sample-fetch, converter
868
869 Give back the hand at the HAProxy scheduler. It is used when the LUA
870 processing consumes a lot of processing time.
871
Thierry FOURNIER / OZON.IO62fec752016-11-10 20:38:11 +0100872.. js:function:: core.parse_addr(address)
873
874 **context**: body, init, task, action, sample-fetch, converter
875
876 :param network: is a string describing an ipv4 or ipv6 address and optionally
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +0200877 its network length, like this: "127.0.0.1/8" or "aaaa::1234/32".
Thierry FOURNIER / OZON.IO62fec752016-11-10 20:38:11 +0100878 :returns: a userdata containing network or nil if an error occurs.
879
Bertrand Jacquin874a35c2018-09-10 21:26:07 +0100880 Parse ipv4 or ipv6 addresses and its facultative associated network.
Thierry FOURNIER / OZON.IO62fec752016-11-10 20:38:11 +0100881
882.. js:function:: core.match_addr(addr1, addr2)
883
884 **context**: body, init, task, action, sample-fetch, converter
885
886 :param addr1: is an address created with "core.parse_addr".
887 :param addr2: is an address created with "core.parse_addr".
Bertrand Jacquin874a35c2018-09-10 21:26:07 +0100888 :returns: boolean, true if the network of the addresses match, else returns
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +0200889 false.
Thierry FOURNIER / OZON.IO62fec752016-11-10 20:38:11 +0100890
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +0200891 Match two networks. For example "127.0.0.1/32" matches "127.0.0.0/8". The
892 order of network is not important.
Thierry FOURNIER / OZON.IO62fec752016-11-10 20:38:11 +0100893
Thierry FOURNIER / OZON.IO8a1027a2016-11-24 20:48:38 +0100894.. js:function:: core.tokenize(str, separators [, noblank])
895
896 **context**: body, init, task, action, sample-fetch, converter
897
898 This function is useful for tokenizing an entry, or splitting some messages.
899 :param string str: The string which will be split.
900 :param string separators: A string containing a list of separators.
901 :param boolean noblank: Ignore empty entries.
902 :returns: an array of string.
903
904 For example:
905
906.. code-block:: lua
907
908 local array = core.tokenize("This function is useful, for tokenizing an entry.", "., ", true)
909 print_r(array)
910..
911
912 Returns this array:
913
914.. code-block:: text
915
916 (table) table: 0x21c01e0 [
917 1: (string) "This"
918 2: (string) "function"
919 3: (string) "is"
920 4: (string) "useful"
921 5: (string) "for"
922 6: (string) "tokenizing"
923 7: (string) "an"
924 8: (string) "entry"
925 ]
926..
927
Aurelien DARRAGONc84899c2023-02-20 18:18:59 +0100928.. js:function:: core.event_sub(event_types, func)
929
930 **context**: body, init, task, action, sample-fetch, converter
931
932 Register a function that will be called on specific system events.
933
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +0200934 :param array event_types: array of string containing the event types you want
935 to subscribe to
936 :param function func: is the Lua function called when one of the subscribed
937 events occur.
Aurelien DARRAGONc84899c2023-02-20 18:18:59 +0100938 :returns: A :ref:`event_sub_class` object.
Aurelien DARRAGON223770d2023-03-10 15:34:35 +0100939 :see: :js:func:`Server.event_sub()`.
Aurelien DARRAGONc84899c2023-02-20 18:18:59 +0100940
941 List of available event types :
942
943 **SERVER** Family:
944
945 * **SERVER_ADD**: when a server is added
946 * **SERVER_DEL**: when a server is removed
947 * **SERVER_DOWN**: when a server state goes from UP to DOWN
948 * **SERVER_UP**: when a server state goes from DOWN to UP
Aurelien DARRAGONc99f3ad2023-04-12 15:47:16 +0200949 * **SERVER_STATE**: when a server state changes
Aurelien DARRAGON948dd3d2023-04-26 11:27:09 +0200950 * **SERVER_ADMIN**: when a server administrative state changes
Aurelien DARRAGONc84899c2023-02-20 18:18:59 +0100951
952 .. Note::
Aurelien DARRAGONc99f3ad2023-04-12 15:47:16 +0200953 Use **SERVER** in **event_types** to subscribe to all server events types
954 at once. Note that this should only be used for testing purposes since a
955 single event source could result in multiple events types being generated.
956 (e.g.: SERVER_STATE will always be generated for each SERVER_DOWN or
957 SERVER_UP)
Aurelien DARRAGONc84899c2023-02-20 18:18:59 +0100958
959 The prototype of the Lua function used as argument is:
960
961.. code-block:: lua
962
Aurelien DARRAGON096b3832023-04-20 11:32:46 +0200963 function(event, event_data, sub, when)
Aurelien DARRAGONc84899c2023-02-20 18:18:59 +0100964..
965
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +0200966 * **event** (*string*): the event type (one of the **event_types** specified
967 when subscribing)
968 * **event_data**: specific to each event family (For **SERVER** family,
969 a :ref:`server_event_class` object)
970 * **sub**: class to manage the subscription from within the event
971 (a :ref:`event_sub_class` object)
Aurelien DARRAGON096b3832023-04-20 11:32:46 +0200972 * **when**: timestamp corresponding to the date when the event was generated.
973 It is an integer representing the number of seconds elapsed since Epoch.
974 It may be provided as optional argument to `os.date()` lua function to
975 convert it to a string according to a given format string.
Aurelien DARRAGONc84899c2023-02-20 18:18:59 +0100976
977 .. Warning::
978 The callback function will only be scheduled on the very same thread that
979 performed the subscription.
980
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +0200981 Moreover, each thread treats events sequentially. It means that if you
982 have, let's say SERVER_UP followed by a SERVER_DOWN in a short timelapse,
983 then the cb function will first be called with SERVER_UP, and once it's
984 done handling the event, the cb function will be called again with
985 SERVER_DOWN.
Aurelien DARRAGONc84899c2023-02-20 18:18:59 +0100986
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +0200987 This is to ensure event consistency when it comes to logging / triggering
988 logic from lua.
Aurelien DARRAGONc84899c2023-02-20 18:18:59 +0100989
990 Your lua cb function may yield if needed, but you're pleased to process the
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +0200991 event as fast as possible to prevent the event queue from growing up,
992 depending on the event flow that is expected for the given subscription.
Aurelien DARRAGONc84899c2023-02-20 18:18:59 +0100993
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +0200994 To prevent abuses, if the event queue for the current subscription goes
995 over a certain amount of unconsumed events, the subscription will pause
996 itself automatically for as long as it takes for your handler to catch up.
997 This would lead to events being missed, so an error will be reported in the
998 logs to warn you about that.
999 This is not something you want to let happen too often, it may indicate
1000 that you subscribed to an event that is occurring too frequently or/and
1001 that your callback function is too slow to keep up the pace and you should
1002 review it.
Aurelien DARRAGONc84899c2023-02-20 18:18:59 +01001003
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02001004 If you want to do some parallel processing because your callback functions
1005 are slow: you might want to create subtasks from lua using
1006 :js:func:`core.register_task()` from within your callback function to
1007 perform the heavy job in a dedicated task and allow remaining events to be
1008 processed more quickly.
Aurelien DARRAGONc84899c2023-02-20 18:18:59 +01001009
Thierry Fournierf61aa632016-02-19 20:56:00 +01001010.. _proxy_class:
1011
1012Proxy class
1013============
1014
1015.. js:class:: Proxy
1016
1017 This class provides a way for manipulating proxy and retrieving information
1018 like statistics.
1019
Thierry FOURNIER817e7592017-07-24 14:35:04 +02001020.. js:attribute:: Proxy.name
1021
1022 Contain the name of the proxy.
1023
Aurelien DARRAGONc4b24372023-03-02 12:00:06 +01001024 .. warning::
1025 This attribute is now deprecated and will eventually be removed.
1026 Please use :js:func:`Proxy.get_name()` function instead.
1027
Thierry Fournierb0467732022-10-07 12:07:24 +02001028.. js:function:: Proxy.get_name()
1029
1030 Returns the name of the proxy.
1031
Baptiste Assmann46c72552017-10-26 21:51:58 +02001032.. js:attribute:: Proxy.uuid
1033
1034 Contain the unique identifier of the proxy.
1035
Aurelien DARRAGONc4b24372023-03-02 12:00:06 +01001036 .. warning::
1037 This attribute is now deprecated and will eventually be removed.
1038 Please use :js:func:`Proxy.get_uuid()` function instead.
1039
Thierry Fournierb0467732022-10-07 12:07:24 +02001040.. js:function:: Proxy.get_uuid()
1041
1042 Returns the unique identifier of the proxy.
1043
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001044.. js:attribute:: Proxy.servers
1045
Patrick Hemmerc6a1d712018-05-01 21:30:41 -04001046 Contain a table with the attached servers. The table is indexed by server
1047 name, and each server entry is an object of type :ref:`server_class`.
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001048
Adis Nezirovic8878f8e2018-07-13 12:18:33 +02001049.. js:attribute:: Proxy.stktable
1050
1051 Contains a stick table object attached to the proxy.
1052
Thierry Fournierff480422016-02-25 08:36:46 +01001053.. js:attribute:: Proxy.listeners
1054
Patrick Hemmerc6a1d712018-05-01 21:30:41 -04001055 Contain a table with the attached listeners. The table is indexed by listener
1056 name, and each each listeners entry is an object of type
1057 :ref:`listener_class`.
Thierry Fournierff480422016-02-25 08:36:46 +01001058
Thierry Fournierf61aa632016-02-19 20:56:00 +01001059.. js:function:: Proxy.pause(px)
1060
1061 Pause the proxy. See the management socket documentation for more information.
1062
1063 :param class_proxy px: A :ref:`proxy_class` which indicates the manipulated
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02001064 proxy.
Thierry Fournierf61aa632016-02-19 20:56:00 +01001065
1066.. js:function:: Proxy.resume(px)
1067
1068 Resume the proxy. See the management socket documentation for more
1069 information.
1070
1071 :param class_proxy px: A :ref:`proxy_class` which indicates the manipulated
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02001072 proxy.
Thierry Fournierf61aa632016-02-19 20:56:00 +01001073
1074.. js:function:: Proxy.stop(px)
1075
1076 Stop the proxy. See the management socket documentation for more information.
1077
1078 :param class_proxy px: A :ref:`proxy_class` which indicates the manipulated
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02001079 proxy.
Thierry Fournierf61aa632016-02-19 20:56:00 +01001080
1081.. js:function:: Proxy.shut_bcksess(px)
1082
1083 Kill the session attached to a backup server. See the management socket
1084 documentation for more information.
1085
1086 :param class_proxy px: A :ref:`proxy_class` which indicates the manipulated
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02001087 proxy.
Thierry Fournierf61aa632016-02-19 20:56:00 +01001088
1089.. js:function:: Proxy.get_cap(px)
1090
1091 Returns a string describing the capabilities of the proxy.
1092
1093 :param class_proxy px: A :ref:`proxy_class` which indicates the manipulated
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02001094 proxy.
Thierry Fournierf61aa632016-02-19 20:56:00 +01001095 :returns: a string "frontend", "backend", "proxy" or "ruleset".
1096
1097.. js:function:: Proxy.get_mode(px)
1098
1099 Returns a string describing the mode of the current proxy.
1100
1101 :param class_proxy px: A :ref:`proxy_class` which indicates the manipulated
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02001102 proxy.
Thierry Fournierf61aa632016-02-19 20:56:00 +01001103 :returns: a string "tcp", "http", "health" or "unknown"
1104
Aurelien DARRAGONfc845532023-04-03 11:00:18 +02001105.. js:function:: Proxy.get_srv_act(px)
1106
1107 Returns the number of current active servers for the current proxy that are
1108 eligible for LB.
1109
1110 :param class_proxy px: A :ref:`proxy_class` which indicates the manipulated
1111 proxy.
1112 :returns: an integer
1113
1114.. js:function:: Proxy.get_srv_bck(px)
1115
1116 Returns the number backup servers for the current proxy that are eligible
1117 for LB.
1118
1119 :param class_proxy px: A :ref:`proxy_class` which indicates the manipulated
1120 proxy.
1121 :returns: an integer
1122
Thierry Fournierf61aa632016-02-19 20:56:00 +01001123.. js:function:: Proxy.get_stats(px)
1124
Bertrand Jacquin874a35c2018-09-10 21:26:07 +01001125 Returns a table containing the proxy statistics. The statistics returned are
Thierry Fournierf61aa632016-02-19 20:56:00 +01001126 not the same if the proxy is frontend or a backend.
1127
1128 :param class_proxy px: A :ref:`proxy_class` which indicates the manipulated
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02001129 proxy.
Patrick Hemmerc6a1d712018-05-01 21:30:41 -04001130 :returns: a key/value table containing stats
Thierry Fournierf61aa632016-02-19 20:56:00 +01001131
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001132.. _server_class:
1133
1134Server class
1135============
1136
Patrick Hemmerc6a1d712018-05-01 21:30:41 -04001137.. js:class:: Server
1138
1139 This class provides a way for manipulating servers and retrieving information.
1140
Patrick Hemmera62ae7e2018-04-29 14:23:48 -04001141.. js:attribute:: Server.name
1142
1143 Contain the name of the server.
1144
Aurelien DARRAGONc4b24372023-03-02 12:00:06 +01001145 .. warning::
1146 This attribute is now deprecated and will eventually be removed.
1147 Please use :js:func:`Server.get_name()` function instead.
1148
Thierry Fournierb0467732022-10-07 12:07:24 +02001149.. js:function:: Server.get_name(sv)
1150
1151 Returns the name of the server.
1152
Patrick Hemmera62ae7e2018-04-29 14:23:48 -04001153.. js:attribute:: Server.puid
1154
1155 Contain the proxy unique identifier of the server.
1156
Aurelien DARRAGONc4b24372023-03-02 12:00:06 +01001157 .. warning::
1158 This attribute is now deprecated and will eventually be removed.
1159 Please use :js:func:`Server.get_puid()` function instead.
1160
Thierry Fournierb0467732022-10-07 12:07:24 +02001161.. js:function:: Server.get_puid(sv)
1162
1163 Returns the proxy unique identifier of the server.
1164
Aurelien DARRAGON94ee6632023-03-10 15:11:27 +01001165.. js:function:: Server.get_rid(sv)
1166
1167 Returns the rid (revision ID) of the server.
1168 It is an unsigned integer that is set upon server creation. Value is derived
1169 from a global counter that starts at 0 and is incremented each time one or
1170 multiple server deletions are followed by a server addition (meaning that
1171 old name/id reuse could occur).
1172
1173 Combining server name/id with server rid yields a process-wide unique
1174 identifier.
1175
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001176.. js:function:: Server.is_draining(sv)
1177
Patrick Hemmerc6a1d712018-05-01 21:30:41 -04001178 Return true if the server is currently draining sticky connections.
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001179
1180 :param class_server sv: A :ref:`server_class` which indicates the manipulated
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02001181 server.
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001182 :returns: a boolean
1183
Aurelien DARRAGONc72051d2023-03-29 10:44:38 +02001184.. js:function:: Server.is_backup(sv)
1185
1186 Return true if the server is a backup server
1187
1188 :param class_server sv: A :ref:`server_class` which indicates the manipulated
1189 server.
1190 :returns: a boolean
1191
Aurelien DARRAGON7a03dee2023-03-29 10:49:30 +02001192.. js:function:: Server.is_dynamic(sv)
1193
1194 Return true if the server was instantiated at runtime (e.g.: from the cli)
1195
1196 :param class_server sv: A :ref:`server_class` which indicates the manipulated
1197 server.
1198 :returns: a boolean
1199
Aurelien DARRAGONfc759b42023-04-03 10:43:17 +02001200.. js:function:: Server.get_cur_sess(sv)
1201
1202 Return the number of currently active sessions on the server
1203
1204 :param class_server sv: A :ref:`server_class` which indicates the manipulated
1205 server.
1206 :returns: an integer
1207
1208.. js:function:: Server.get_pend_conn(sv)
1209
1210 Return the number of pending connections to the server
1211
1212 :param class_server sv: A :ref:`server_class` which indicates the manipulated
1213 server.
1214 :returns: an integer
1215
Patrick Hemmer32d539f2018-04-29 14:25:46 -04001216.. js:function:: Server.set_maxconn(sv, weight)
1217
1218 Dynamically change the maximum connections of the server. See the management
1219 socket documentation for more information about the format of the string.
1220
1221 :param class_server sv: A :ref:`server_class` which indicates the manipulated
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02001222 server.
Patrick Hemmer32d539f2018-04-29 14:25:46 -04001223 :param string maxconn: A string describing the server maximum connections.
1224
1225.. js:function:: Server.get_maxconn(sv, weight)
1226
1227 This function returns an integer representing the server maximum connections.
1228
1229 :param class_server sv: A :ref:`server_class` which indicates the manipulated
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02001230 server.
Patrick Hemmer32d539f2018-04-29 14:25:46 -04001231 :returns: an integer.
1232
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001233.. js:function:: Server.set_weight(sv, weight)
1234
Patrick Hemmerc6a1d712018-05-01 21:30:41 -04001235 Dynamically change the weight of the server. See the management socket
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001236 documentation for more information about the format of the string.
1237
1238 :param class_server sv: A :ref:`server_class` which indicates the manipulated
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02001239 server.
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001240 :param string weight: A string describing the server weight.
1241
1242.. js:function:: Server.get_weight(sv)
1243
Patrick Hemmerc6a1d712018-05-01 21:30:41 -04001244 This function returns an integer representing the server weight.
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001245
1246 :param class_server sv: A :ref:`server_class` which indicates the manipulated
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02001247 server.
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001248 :returns: an integer.
1249
Joseph C. Sible49bbf522020-05-04 22:20:32 -04001250.. js:function:: Server.set_addr(sv, addr[, port])
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001251
Patrick Hemmerc6a1d712018-05-01 21:30:41 -04001252 Dynamically change the address of the server. See the management socket
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001253 documentation for more information about the format of the string.
1254
1255 :param class_server sv: A :ref:`server_class` which indicates the manipulated
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02001256 server.
Patrick Hemmerc6a1d712018-05-01 21:30:41 -04001257 :param string addr: A string describing the server address.
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001258
1259.. js:function:: Server.get_addr(sv)
1260
Patrick Hemmerc6a1d712018-05-01 21:30:41 -04001261 Returns a string describing the address of the server.
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001262
1263 :param class_server sv: A :ref:`server_class` which indicates the manipulated
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02001264 server.
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001265 :returns: A string
1266
1267.. js:function:: Server.get_stats(sv)
1268
1269 Returns server statistics.
1270
1271 :param class_server sv: A :ref:`server_class` which indicates the manipulated
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02001272 server.
Patrick Hemmerc6a1d712018-05-01 21:30:41 -04001273 :returns: a key/value table containing stats
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001274
Aurelien DARRAGON3889efa2023-04-03 14:00:58 +02001275.. js:function:: Server.get_proxy(sv)
1276
1277 Returns the parent proxy to which the server belongs.
1278
1279 :param class_server sv: A :ref:`server_class` which indicates the manipulated
1280 server.
1281 :returns: a :ref:`proxy_class` or nil if not available
1282
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001283.. js:function:: Server.shut_sess(sv)
1284
1285 Shutdown all the sessions attached to the server. See the management socket
1286 documentation for more information about this function.
1287
1288 :param class_server sv: A :ref:`server_class` which indicates the manipulated
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02001289 server.
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001290
1291.. js:function:: Server.set_drain(sv)
1292
1293 Drain sticky sessions. See the management socket documentation for more
1294 information about this function.
1295
1296 :param class_server sv: A :ref:`server_class` which indicates the manipulated
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02001297 server.
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001298
1299.. js:function:: Server.set_maint(sv)
1300
1301 Set maintenance mode. See the management socket documentation for more
1302 information about this function.
1303
1304 :param class_server sv: A :ref:`server_class` which indicates the manipulated
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02001305 server.
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001306
1307.. js:function:: Server.set_ready(sv)
1308
1309 Set normal mode. See the management socket documentation for more information
1310 about this function.
1311
1312 :param class_server sv: A :ref:`server_class` which indicates the manipulated
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02001313 server.
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001314
1315.. js:function:: Server.check_enable(sv)
1316
1317 Enable health checks. See the management socket documentation for more
1318 information about this function.
1319
1320 :param class_server sv: A :ref:`server_class` which indicates the manipulated
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02001321 server.
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001322
1323.. js:function:: Server.check_disable(sv)
1324
1325 Disable health checks. See the management socket documentation for more
1326 information about this function.
1327
1328 :param class_server sv: A :ref:`server_class` which indicates the manipulated
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02001329 server.
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001330
1331.. js:function:: Server.check_force_up(sv)
1332
1333 Force health-check up. See the management socket documentation for more
1334 information about this function.
1335
1336 :param class_server sv: A :ref:`server_class` which indicates the manipulated
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02001337 server.
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001338
1339.. js:function:: Server.check_force_nolb(sv)
1340
1341 Force health-check nolb mode. See the management socket documentation for more
1342 information about this function.
1343
1344 :param class_server sv: A :ref:`server_class` which indicates the manipulated
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02001345 server.
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001346
1347.. js:function:: Server.check_force_down(sv)
1348
1349 Force health-check down. See the management socket documentation for more
1350 information about this function.
1351
1352 :param class_server sv: A :ref:`server_class` which indicates the manipulated
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02001353 server.
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001354
1355.. js:function:: Server.agent_enable(sv)
1356
1357 Enable agent check. See the management socket documentation for more
1358 information about this function.
1359
1360 :param class_server sv: A :ref:`server_class` which indicates the manipulated
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02001361 server.
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001362
1363.. js:function:: Server.agent_disable(sv)
1364
1365 Disable agent check. See the management socket documentation for more
1366 information about this function.
1367
1368 :param class_server sv: A :ref:`server_class` which indicates the manipulated
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02001369 server.
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001370
1371.. js:function:: Server.agent_force_up(sv)
1372
1373 Force agent check up. See the management socket documentation for more
1374 information about this function.
1375
1376 :param class_server sv: A :ref:`server_class` which indicates the manipulated
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02001377 server.
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001378
1379.. js:function:: Server.agent_force_down(sv)
1380
1381 Force agent check down. See the management socket documentation for more
1382 information about this function.
1383
1384 :param class_server sv: A :ref:`server_class` which indicates the manipulated
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02001385 server.
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001386
Aurelien DARRAGON406511a2023-03-29 11:30:36 +02001387.. js:function:: Server.tracking(sv)
1388
1389 Check if the current server is tracking another server.
1390
1391 :param class_server sv: A :ref:`server_class` which indicates the manipulated
1392 server.
1393 :returns: A :ref:`server_class` which indicates the tracked server or nil if
1394 the server doesn't track another one.
1395
Aurelien DARRAGON4be36a12023-03-29 14:02:39 +02001396.. js:function:: Server.get_trackers(sv)
1397
1398 Check if the current server is being tracked by other servers.
1399
1400 :param class_server sv: A :ref:`server_class` which indicates the manipulated
1401 server.
1402 :returns: An array of :ref:`server_class` which indicates the tracking
1403 servers (might be empty)
1404
Aurelien DARRAGON223770d2023-03-10 15:34:35 +01001405.. js:function:: Server.event_sub(sv, event_types, func)
1406
1407 Register a function that will be called on specific server events.
1408 It works exactly like :js:func:`core.event_sub()` except that the subscription
1409 will be performed within the server dedicated subscription list instead of the
1410 global one.
1411 (Your callback function will only be called for server events affecting sv)
1412
1413 See :js:func:`core.event_sub()` for function usage.
1414
1415 A key advantage to using :js:func:`Server.event_sub()` over
1416 :js:func:`core.event_sub()` for servers is that :js:func:`Server.event_sub()`
1417 allows you to be notified for servers events of a single server only.
1418 It removes the needs for extra filtering in your callback function if you only
1419 care about a single server, and also prevents useless wakeups.
1420
1421 For instance, if you want to be notified for UP/DOWN events on a given set of
Ilya Shipitsinccf80122023-04-22 20:20:39 +02001422 servers, it is recommended to perform multiple per-server subscriptions since
Aurelien DARRAGON223770d2023-03-10 15:34:35 +01001423 it will be more efficient that doing a single global subscription that will
1424 filter the received events.
1425 Unless you really want to be notified for servers events of ALL servers of
1426 course, which could make sense given you setup but should be avoided if you
1427 have an important number of servers as it will add a significant load on your
1428 haproxy process in case of multiple servers state change in a short amount of
1429 time.
1430
1431 .. Note::
1432 You may also combine :js:func:`core.event_sub()` with
1433 :js:func:`Server.event_sub()`.
1434
1435 Also, don't forget that you can use :js:func:`core.register_task()` from
1436 your callback function if needed. (ie: parallel work)
1437
1438 Here is a working example combining :js:func:`core.event_sub()` with
1439 :js:func:`Server.event_sub()` and :js:func:`core.register_task()`
1440 (This only serves as a demo, this is not necessarily useful to do so)
1441
1442.. code-block:: lua
1443
1444 core.event_sub({"SERVER_ADD"}, function(event, data, sub)
1445 -- in the global event handler
1446 if data["reference"] ~= nil then
1447 print("Tracking new server: ", data["name"])
1448 data["reference"]:event_sub({"SERVER_UP", "SERVER_DOWN"}, function(event, data, sub)
1449 -- in the per-server event handler
1450 if data["reference"] ~= nil then
1451 core.register_task(function(server)
1452 -- subtask to perform some async work (e.g.: HTTP API calls, sending emails...)
1453 print("ASYNC: SERVER ", server:get_name(), " is ", event == "SERVER_UP" and "UP" or "DOWN")
1454 end, data["reference"])
1455 end
1456 end)
1457 end
1458 end)
1459
1460..
1461
1462 In this example, we will first track global server addition events.
1463 For each newly added server ("add server" on the cli), we will register a
1464 UP/DOWN server subscription.
1465 Then, the callback function will schedule the event handling in an async
1466 subtask which will receive the server reference as an argument.
1467
Thierry Fournierff480422016-02-25 08:36:46 +01001468.. _listener_class:
1469
1470Listener class
1471==============
1472
1473.. js:function:: Listener.get_stats(ls)
1474
1475 Returns server statistics.
1476
1477 :param class_listener ls: A :ref:`listener_class` which indicates the
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02001478 manipulated listener.
Patrick Hemmerc6a1d712018-05-01 21:30:41 -04001479 :returns: a key/value table containing stats
Thierry Fournierff480422016-02-25 08:36:46 +01001480
Aurelien DARRAGONc84899c2023-02-20 18:18:59 +01001481.. _event_sub_class:
1482
1483EventSub class
1484==============
1485
1486.. js:function:: EventSub.unsub()
1487
1488 End the subscription, the callback function will not be called again.
1489
1490.. _server_event_class:
1491
1492ServerEvent class
1493=================
1494
Aurelien DARRAGONc4ae8902023-04-17 17:24:48 +02001495.. js:class:: ServerEvent
1496
1497This class is provided with every **SERVER** events.
1498
1499See :js:func:`core.event_sub()` for more info.
1500
Aurelien DARRAGONc84899c2023-02-20 18:18:59 +01001501.. js:attribute:: ServerEvent.name
1502
1503 Contains the name of the server.
1504
1505.. js:attribute:: ServerEvent.puid
1506
1507 Contains the proxy-unique uid of the server
1508
1509.. js:attribute:: ServerEvent.rid
1510
1511 Contains the revision ID of the server
1512
1513.. js:attribute:: ServerEvent.proxy_name
1514
1515 Contains the name of the proxy to which the server belongs
1516
Aurelien DARRAGON55f84c72023-03-22 17:49:04 +01001517.. js:attribute:: ServerEvent.proxy_uuid
1518
1519 Contains the uuid of the proxy to which the server belongs
1520
Aurelien DARRAGONc84899c2023-02-20 18:18:59 +01001521.. js:attribute:: ServerEvent.reference
1522
1523 Reference to the live server (A :ref:`server_class`).
1524
1525 .. Warning::
1526 Not available if the server was removed in the meantime.
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02001527 (Will never be set for SERVER_DEL event since the server does not exist
1528 anymore)
Aurelien DARRAGONc84899c2023-02-20 18:18:59 +01001529
Aurelien DARRAGONc99f3ad2023-04-12 15:47:16 +02001530.. js:attribute:: ServerEvent.state
1531
1532 A :ref:`server_event_state_class`
1533
1534 .. Note::
1535 Only available for SERVER_STATE event
1536
Aurelien DARRAGON948dd3d2023-04-26 11:27:09 +02001537.. js:attribute:: ServerEvent.admin
1538
1539 A :ref:`server_event_admin_class`
1540
1541 .. Note::
1542 Only available for SERVER_ADMIN event
1543
Aurelien DARRAGONc99f3ad2023-04-12 15:47:16 +02001544.. _server_event_checkres_class:
1545
1546ServerEventCheckRes class
1547=========================
1548
1549.. js:class:: ServerEventCheckRes
1550
1551This class describes the result of a server's check.
1552
1553.. js:attribute:: ServerEventCheckRes.result
1554
1555 Effective check result.
1556
1557 Check result is a string and will be set to one of the following values:
1558 - "FAILED": the check failed
1559 - "PASSED": the check succeeded
1560 - "CONDPASS": the check conditionally passed
1561
1562.. js:attribute:: ServerEventCheckRes.agent
1563
1564 Boolean set to true if the check is an agent check.
1565 Else it is a health check.
1566
1567.. js:attribute:: ServerEventCheckRes.duration
1568
1569 Check's duration in milliseconds
1570
1571.. js:attribute:: ServerEventCheckRes.reason
1572
1573 Check's status. An array containing three fields:
1574 - **short**: a string representing check status short name
1575 - **desc**: a string representing check status description
1576 - **code**: an integer, this extra information is provided for checks
1577 that went through the data analysis stage (>= layer 5)
1578
1579.. js:attribute:: ServerEventCheckRes.health
1580
1581 An array containing values about check's health (integers):
1582 - **cur**: current health counter:
1583 - 0 to (**rise** - 1) = BAD
1584 - **rise** to (**rise** + **fall** - 1) = GOOD
1585 - **rise**: server will be considered as operational after **rise**
1586 consecutive successful checks
1587 - **fall**: server will be considered as dead after **fall** consecutive
1588 unsuccessful checks
1589
1590.. _server_event_state_class:
1591
1592ServerEventState class
1593======================
1594
1595.. js:class:: ServerEventState
1596
1597This class contains additional info related to **SERVER_STATE** event.
1598
1599.. js:attribute:: ServerEventState.admin
1600
1601 Boolean set to true if the server state change is due to an administrative
1602 change. Else it is an operational change.
1603
1604.. js:attribute:: ServerEventState.check
1605
1606 A :ref:`server_event_checkres_class`, provided if the state change is
1607 due to a server check (must be an operational change).
1608
1609.. js:attribute:: ServerEventState.cause
1610
1611 Printable state change cause. Might be empty.
1612
1613.. js:attribute:: ServerEventState.new_state
1614
1615 New server state due to operational or admin change.
1616
1617 It is a string that can be any of the following values:
1618 - "STOPPED": The server is down
1619 - "STOPPING": The server is up but soft-stopping
1620 - "STARTING": The server is warming up
1621 - "RUNNING": The server is fully up
1622
1623.. js:attribute:: ServerEventState.old_state
1624
1625 Previous server state prior to the operational or admin change.
1626
1627 Can be any value described in **new_state**, but they should differ.
1628
1629.. js:attribute:: ServerEventState.requeued
1630
1631 Number of connections that were requeued due to the server state change.
1632
1633 For a server going DOWN: it is the number of pending server connections
1634 that are requeued to the backend (such connections will be redispatched
1635 to any server that is suitable according to the configured load balancing
1636 algorithm).
1637
1638 For a server doing UP: it is the number of pending connections on the
1639 backend that may be redispatched to the server according to the load
1640 balancing algorithm that is in use.
1641
Aurelien DARRAGON948dd3d2023-04-26 11:27:09 +02001642.. _server_event_admin_class:
1643
1644ServerEventAdmin class
1645======================
1646
1647.. js:class:: ServerEventAdmin
1648
1649This class contains additional info related to **SERVER_ADMIN** event.
1650
1651.. js:attribute:: ServerEventAdmin.cause
1652
1653 Printable admin state change cause. Might be empty.
1654
1655.. js:attribute:: ServerEventAdmin.new_admin
1656
1657 New server admin state due to the admin change.
1658
1659 It is an array of string containing a composition of following values:
1660 - "**MAINT**": server is in maintenance mode
1661 - "FMAINT": server is in forced maintenance mode (MAINT is also set)
1662 - "IMAINT": server is in inherited maintenance mode (MAINT is also set)
1663 - "RMAINT": server is in resolve maintenance mode (MAINT is also set)
1664 - "CMAINT": server is in config maintenance mode (MAINT is also set)
1665 - "**DRAIN**": server is in drain mode
1666 - "FDRAIN": server is in forced drain mode (DRAIN is also set)
1667 - "IDRAIN": server is in inherited drain mode (DRAIN is also set)
1668
1669.. js:attribute:: ServerEventAdmin.old_admin
1670
1671 Previous server admin state prior to the admin change.
1672
1673 Values are presented as in **new_admin**, but they should differ.
1674 (Comparing old and new helps to find out the change(s))
1675
1676.. js:attribute:: ServerEventAdmin.requeued
1677
1678 Same as :js:attr:`ServerEventState.requeued` but when the requeue is due to
1679 the server administrative state change.
1680
Thierry Fournier1de16592016-01-27 09:49:07 +01001681.. _concat_class:
1682
1683Concat class
1684============
1685
1686.. js:class:: Concat
1687
1688 This class provides a fast way for string concatenation. The way using native
1689 Lua concatenation like the code below is slow for some reasons.
1690
1691.. code-block:: lua
1692
1693 str = "string1"
1694 str = str .. ", string2"
1695 str = str .. ", string3"
1696..
1697
1698 For each concatenation, Lua:
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02001699 - allocates memory for the result,
1700 - catenates the two string copying the strings in the new memory block,
1701 - frees the old memory block containing the string which is no longer used.
1702
Thierry Fournier1de16592016-01-27 09:49:07 +01001703 This process does many memory move, allocation and free. In addition, the
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02001704 memory is not really freed, it is just marked as unused and waits for the
Thierry Fournier1de16592016-01-27 09:49:07 +01001705 garbage collector.
1706
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02001707 The Concat class provides an alternative way to concatenate strings. It uses
Thierry Fournier1de16592016-01-27 09:49:07 +01001708 the internal Lua mechanism (it does not allocate memory), but it doesn't copy
1709 the data more than once.
1710
1711 On my computer, the following loops spends 0.2s for the Concat method and
1712 18.5s for the pure Lua implementation. So, the Concat class is about 1000x
1713 faster than the embedded solution.
1714
1715.. code-block:: lua
1716
1717 for j = 1, 100 do
1718 c = core.concat()
1719 for i = 1, 20000 do
1720 c:add("#####")
1721 end
1722 end
1723..
1724
1725.. code-block:: lua
1726
1727 for j = 1, 100 do
1728 c = ""
1729 for i = 1, 20000 do
1730 c = c .. "#####"
1731 end
1732 end
1733..
1734
1735.. js:function:: Concat.add(concat, string)
1736
1737 This function adds a string to the current concatenated string.
1738
1739 :param class_concat concat: A :ref:`concat_class` which contains the currently
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02001740 built string.
Bertrand Jacquin874a35c2018-09-10 21:26:07 +01001741 :param string string: A new string to concatenate to the current built
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02001742 string.
Thierry Fournier1de16592016-01-27 09:49:07 +01001743
1744.. js:function:: Concat.dump(concat)
1745
Bertrand Jacquin874a35c2018-09-10 21:26:07 +01001746 This function returns the concatenated string.
Thierry Fournier1de16592016-01-27 09:49:07 +01001747
1748 :param class_concat concat: A :ref:`concat_class` which contains the currently
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02001749 built string.
Thierry Fournier1de16592016-01-27 09:49:07 +01001750 :returns: the concatenated string
1751
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001752.. _fetches_class:
1753
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001754Fetches class
1755=============
1756
1757.. js:class:: Fetches
1758
1759 This class contains a lot of internal HAProxy sample fetches. See the
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02001760 HAProxy "configuration.txt" documentation for more information.
1761 (chapters 7.3.2 to 7.3.6)
Thierry FOURNIER2e4893c2015-03-18 13:37:27 +01001762
Christopher Faulet1e9b1b62021-08-11 10:14:30 +02001763 .. warning::
1764 some sample fetches are not available in some context. These limitations
1765 are specified in this documentation when they're useful.
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001766
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01001767 :see: :js:attr:`TXN.f`
1768 :see: :js:attr:`TXN.sf`
Thierry FOURNIER2e4893c2015-03-18 13:37:27 +01001769
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02001770 Fetches are useful to:
Thierry FOURNIER2e4893c2015-03-18 13:37:27 +01001771
1772 * get system time,
1773 * get environment variable,
1774 * get random numbers,
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02001775 * know backend status like the number of users in queue or the number of
Thierry FOURNIER2e4893c2015-03-18 13:37:27 +01001776 connections established,
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02001777 * get client information like ip source or destination,
Thierry FOURNIER2e4893c2015-03-18 13:37:27 +01001778 * deal with stick tables,
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02001779 * fetch established SSL information,
1780 * fetch HTTP information like headers or method.
Thierry FOURNIER2e4893c2015-03-18 13:37:27 +01001781
1782.. code-block:: lua
1783
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001784 function action(txn)
1785 -- Get source IP
1786 local clientip = txn.f:src()
1787 end
Thierry FOURNIER2e4893c2015-03-18 13:37:27 +01001788..
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001789
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001790.. _converters_class:
1791
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001792Converters class
1793================
1794
1795.. js:class:: Converters
1796
1797 This class contains a lot of internal HAProxy sample converters. See the
Thierry FOURNIER2e4893c2015-03-18 13:37:27 +01001798 HAProxy documentation "configuration.txt" for more information about her
1799 usage. Its the chapter 7.3.1.
1800
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01001801 :see: :js:attr:`TXN.c`
1802 :see: :js:attr:`TXN.sc`
Thierry FOURNIER2e4893c2015-03-18 13:37:27 +01001803
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02001804 Converters provides stateful transformation. They are useful to:
Thierry FOURNIER2e4893c2015-03-18 13:37:27 +01001805
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02001806 * convert input to base64,
1807 * apply hash on input string (djb2, crc32, sdbm, wt6),
Thierry FOURNIER2e4893c2015-03-18 13:37:27 +01001808 * format date,
1809 * json escape,
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02001810 * extract preferred language comparing two lists,
Thierry FOURNIER2e4893c2015-03-18 13:37:27 +01001811 * turn to lower or upper chars,
1812 * deal with stick tables.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001813
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001814.. _channel_class:
1815
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001816Channel class
1817=============
1818
1819.. js:class:: Channel
1820
Christopher Faulet5a2c6612021-08-15 20:35:25 +02001821 **context**: action, sample-fetch, convert, filter
1822
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001823 HAProxy uses two buffers for the processing of the requests. The first one is
1824 used with the request data (from the client to the server) and the second is
1825 used for the response data (from the server to the client).
1826
1827 Each buffer contains two types of data. The first type is the incoming data
1828 waiting for a processing. The second part is the outgoing data already
1829 processed. Usually, the incoming data is processed, after it is tagged as
1830 outgoing data, and finally it is sent. The following functions provides tools
1831 for manipulating these data in a buffer.
1832
1833 The following diagram shows where the channel class function are applied.
1834
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001835 .. image:: _static/channel.png
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001836
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001837 .. warning::
1838 It is not possible to read from the response in request action, and it is
Boyang Li60cfe8b2022-05-10 18:11:00 +00001839 not possible to read from the request channel in response action.
Christopher Faulet09530392021-06-14 11:43:18 +02001840
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001841 .. warning::
1842 It is forbidden to alter the Channels buffer from HTTP contexts. So only
1843 :js:func:`Channel.input`, :js:func:`Channel.output`,
1844 :js:func:`Channel.may_recv`, :js:func:`Channel.is_full` and
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02001845 :js:func:`Channel.is_resp` can be called from a HTTP context.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001846
Christopher Faulet5a2c6612021-08-15 20:35:25 +02001847 All the functions provided by this class are available in the
1848 **sample-fetches**, **actions** and **filters** contexts. For **filters**,
1849 incoming data (offset and length) are relative to the filter. Some functions
Boyang Li60cfe8b2022-05-10 18:11:00 +00001850 may yield, but only for **actions**. Yield is not possible for
Christopher Faulet5a2c6612021-08-15 20:35:25 +02001851 **sample-fetches**, **converters** and **filters**.
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001852
1853.. js:function:: Channel.append(channel, string)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001854
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001855 This function copies the string **string** at the end of incoming data of the
1856 channel buffer. The function returns the copied length on success or -1 if
1857 data cannot be copied.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001858
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001859 Same that :js:func:`Channel.insert(channel, string, channel:input())`.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001860
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001861 :param class_channel channel: The manipulated Channel.
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02001862 :param string string: The data to copy at the end of incoming data.
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001863 :returns: an integer containing the amount of bytes copied or -1.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001864
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001865.. js:function:: Channel.data(channel [, offset [, length]])
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001866
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001867 This function returns **length** bytes of incoming data from the channel
1868 buffer, starting at the offset **offset**. The data are not removed from the
1869 buffer.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001870
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001871 By default, if no length is provided, all incoming data found, starting at the
1872 given offset, are returned. If **length** is set to -1, the function tries to
Christopher Faulet5a2c6612021-08-15 20:35:25 +02001873 retrieve a maximum of data and, if called by an action, it yields if
1874 necessary. It also waits for more data if the requested length exceeds the
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02001875 available amount of incoming data. Not providing an offset is the same as
Ilya Shipitsinff0f2782021-08-22 22:18:07 +05001876 setting it to 0. A positive offset is relative to the beginning of incoming
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02001877 data of the channel buffer while negative offset is relative to the end.
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001878
1879 If there is no incoming data and the channel can't receive more data, a 'nil'
1880 value is returned.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001881
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001882 :param class_channel channel: The manipulated Channel.
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001883 :param integer offset: *optional* The offset in incoming data to start to get
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02001884 data. 0 by default. May be negative to be relative to the end of incoming
1885 data.
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001886 :param integer length: *optional* The expected length of data to retrieve. All
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02001887 incoming data by default. May be set to -1 to get a maximum of data.
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001888 :returns: a string containing the data found or nil.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001889
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001890.. js:function:: Channel.forward(channel, length)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001891
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001892 This function forwards **length** bytes of data from the channel buffer. If
Christopher Faulet5a2c6612021-08-15 20:35:25 +02001893 the requested length exceeds the available amount of incoming data, and if
1894 called by an action, the function yields, waiting for more data to forward. It
1895 returns the amount of data forwarded.
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001896
1897 :param class_channel channel: The manipulated Channel.
1898 :param integer int: The amount of data to forward.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001899
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001900.. js:function:: Channel.input(channel)
1901
Christopher Faulet5a2c6612021-08-15 20:35:25 +02001902 This function returns the length of incoming data in the channel buffer. When
1903 called by a filter, this value is relative to the filter.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001904
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001905 :param class_channel channel: The manipulated Channel.
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001906 :returns: an integer containing the amount of available bytes.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001907
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001908.. js:function:: Channel.insert(channel, string [, offset])
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001909
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001910 This function copies the string **string** at the offset **offset** in
1911 incoming data of the channel buffer. The function returns the copied length on
1912 success or -1 if data cannot be copied.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001913
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001914 By default, if no offset is provided, the string is copied in front of
Ilya Shipitsinff0f2782021-08-22 22:18:07 +05001915 incoming data. A positive offset is relative to the beginning of incoming data
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001916 of the channel buffer while negative offset is relative to their end.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001917
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001918 :param class_channel channel: The manipulated Channel.
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02001919 :param string string: The data to copy into incoming data.
1920 :param integer offset: *optional* The offset in incoming data where to copy
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02001921 data. 0 by default. May be negative to be relative to the end of incoming
1922 data.
Pieter Baauw4d7f7662015-11-08 16:38:08 +01001923 :returns: an integer containing the amount of bytes copied or -1.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001924
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001925.. js:function:: Channel.is_full(channel)
1926
1927 This function returns true if the channel buffer is full.
1928
Christopher Faulet5a2c6612021-08-15 20:35:25 +02001929 :param class_channel channel: The manipulated Channel.
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001930 :returns: a boolean
1931
1932.. js:function:: Channel.is_resp(channel)
1933
1934 This function returns true if the channel is the response one.
1935
Christopher Faulet5a2c6612021-08-15 20:35:25 +02001936 :param class_channel channel: The manipulated Channel.
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001937 :returns: a boolean
1938
1939.. js:function:: Channel.line(channel [, offset [, length]])
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001940
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001941 This function parses **length** bytes of incoming data of the channel buffer,
1942 starting at offset **offset**, and returns the first line found, including the
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02001943 '\\n'. The data are not removed from the buffer. If no line is found, all
1944 data are returned.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001945
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001946 By default, if no length is provided, all incoming data, starting at the given
1947 offset, are evaluated. If **length** is set to -1, the function tries to
Christopher Faulet5a2c6612021-08-15 20:35:25 +02001948 retrieve a maximum of data and, if called by an action, yields if
1949 necessary. It also waits for more data if the requested length exceeds the
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02001950 available amount of incoming data. Not providing an offset is the same as
Ilya Shipitsinff0f2782021-08-22 22:18:07 +05001951 setting it to 0. A positive offset is relative to the beginning of incoming
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02001952 data of the channel buffer while negative offset is relative to the end.
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001953
1954 If there is no incoming data and the channel can't receive more data, a 'nil'
1955 value is returned.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001956
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001957 :param class_channel channel: The manipulated Channel.
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02001958 :param integer offset: *optional* The offset in incoming data to start to
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02001959 parse data. 0 by default. May be negative to be relative to the end of
1960 incoming data.
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001961 :param integer length: *optional* The length of data to parse. All incoming
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02001962 data by default. May be set to -1 to get a maximum of data.
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001963 :returns: a string containing the line found or nil.
1964
1965.. js:function:: Channel.may_recv(channel)
1966
1967 This function returns true if the channel may still receive data.
1968
Christopher Faulet5a2c6612021-08-15 20:35:25 +02001969 :param class_channel channel: The manipulated Channel.
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001970 :returns: a boolean
1971
1972.. js:function:: Channel.output(channel)
1973
Christopher Faulet5a2c6612021-08-15 20:35:25 +02001974 This function returns the length of outgoing data of the channel buffer. When
1975 called by a filter, this value is relative to the filter.
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001976
1977 :param class_channel channel: The manipulated Channel.
1978 :returns: an integer containing the amount of available bytes.
1979
1980.. js:function:: Channel.prepend(channel, string)
1981
1982 This function copies the string **string** in front of incoming data of the
1983 channel buffer. The function returns the copied length on success or -1 if
1984 data cannot be copied.
1985
1986 Same that :js:func:`Channel.insert(channel, string, 0)`.
1987
1988 :param class_channel channel: The manipulated Channel.
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02001989 :param string string: The data to copy in front of incoming data.
Pieter Baauw4d7f7662015-11-08 16:38:08 +01001990 :returns: an integer containing the amount of bytes copied or -1.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001991
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001992.. js:function:: Channel.remove(channel [, offset [, length]])
1993
1994 This function removes **length** bytes of incoming data of the channel buffer,
1995 starting at offset **offset**. This function returns number of bytes removed
1996 on success.
1997
1998 By default, if no length is provided, all incoming data, starting at the given
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02001999 offset, are removed. Not providing an offset is the same as setting it
Ilya Shipitsinff0f2782021-08-22 22:18:07 +05002000 to 0. A positive offset is relative to the beginning of incoming data of the
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02002001 channel buffer while negative offset is relative to the end.
Christopher Faulet6a79fc12021-08-06 16:02:36 +02002002
2003 :param class_channel channel: The manipulated Channel.
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02002004 :param integer offset: *optional* The offset in incoming data where to start
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02002005 to remove data. 0 by default. May be negative to be relative to the end of
2006 incoming data.
Christopher Faulet6a79fc12021-08-06 16:02:36 +02002007 :param integer length: *optional* The length of data to remove. All incoming
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02002008 data by default.
Christopher Faulet6a79fc12021-08-06 16:02:36 +02002009 :returns: an integer containing the amount of bytes removed.
2010
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002011.. js:function:: Channel.send(channel, string)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002012
Christopher Faulet5a2c6612021-08-15 20:35:25 +02002013 This function requires immediate send of the string **string**. It means the
Ilya Shipitsinff0f2782021-08-22 22:18:07 +05002014 string is copied at the beginning of incoming data of the channel buffer and
Christopher Faulet5a2c6612021-08-15 20:35:25 +02002015 immediately forwarded. Unless if the connection is close, and if called by an
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02002016 action, this function yields to copy and forward all the string.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002017
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002018 :param class_channel channel: The manipulated Channel.
Christopher Faulet6a79fc12021-08-06 16:02:36 +02002019 :param string string: The data to send.
Pieter Baauw4d7f7662015-11-08 16:38:08 +01002020 :returns: an integer containing the amount of bytes copied or -1.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002021
Christopher Faulet6a79fc12021-08-06 16:02:36 +02002022.. js:function:: Channel.set(channel, string [, offset [, length]])
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002023
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02002024 This function replaces **length** bytes of incoming data of the channel
2025 buffer, starting at offset **offset**, by the string **string**. The function
2026 returns the copied length on success or -1 if data cannot be copied.
Christopher Faulet6a79fc12021-08-06 16:02:36 +02002027
2028 By default, if no length is provided, all incoming data, starting at the given
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02002029 offset, are replaced. Not providing an offset is the same as setting it
Ilya Shipitsinff0f2782021-08-22 22:18:07 +05002030 to 0. A positive offset is relative to the beginning of incoming data of the
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02002031 channel buffer while negative offset is relative to the end.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002032
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002033 :param class_channel channel: The manipulated Channel.
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02002034 :param string string: The data to copy into incoming data.
2035 :param integer offset: *optional* The offset in incoming data where to start
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02002036 the data replacement. 0 by default. May be negative to be relative to the
2037 end of incoming data.
Christopher Faulet6a79fc12021-08-06 16:02:36 +02002038 :param integer length: *optional* The length of data to replace. All incoming
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02002039 data by default.
Christopher Faulet6a79fc12021-08-06 16:02:36 +02002040 :returns: an integer containing the amount of bytes copied or -1.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002041
Christopher Faulet6a79fc12021-08-06 16:02:36 +02002042.. js:function:: Channel.dup(channel)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002043
Christopher Faulet6a79fc12021-08-06 16:02:36 +02002044 **DEPRECATED**
2045
2046 This function returns all incoming data found in the channel buffer. The data
Boyang Li60cfe8b2022-05-10 18:11:00 +00002047 are not removed from the buffer and can be reprocessed later.
Christopher Faulet6a79fc12021-08-06 16:02:36 +02002048
2049 If there is no incoming data and the channel can't receive more data, a 'nil'
2050 value is returned.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002051
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002052 :param class_channel channel: The manipulated Channel.
Christopher Faulet6a79fc12021-08-06 16:02:36 +02002053 :returns: a string containing all data found or nil.
2054
2055 .. warning::
2056 This function is deprecated. :js:func:`Channel.data()` must be used
2057 instead.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002058
Christopher Faulet6a79fc12021-08-06 16:02:36 +02002059.. js:function:: Channel.get(channel)
2060
2061 **DEPRECATED**
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002062
Christopher Faulet6a79fc12021-08-06 16:02:36 +02002063 This function returns all incoming data found in the channel buffer and remove
2064 them from the buffer.
2065
2066 If there is no incoming data and the channel can't receive more data, a 'nil'
2067 value is returned.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002068
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002069 :param class_channel channel: The manipulated Channel.
Christopher Faulet6a79fc12021-08-06 16:02:36 +02002070 :returns: a string containing all the data found or nil.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002071
Christopher Faulet6a79fc12021-08-06 16:02:36 +02002072 .. warning::
2073 This function is deprecated. :js:func:`Channel.data()` must be used to
2074 retrieve data followed by a call to :js:func:`Channel:remove()` to remove
2075 data.
Thierry FOURNIER / OZON.IO65192f32016-11-07 15:28:40 +01002076
Christopher Faulet6a79fc12021-08-06 16:02:36 +02002077 .. code-block:: lua
Thierry FOURNIER / OZON.IO65192f32016-11-07 15:28:40 +01002078
Christopher Faulet6a79fc12021-08-06 16:02:36 +02002079 local data = chn:data()
2080 chn:remove(0, data:len())
2081
2082 ..
2083
2084.. js:function:: Channel.getline(channel)
2085
2086 **DEPRECATED**
2087
2088 This function returns the first line found in incoming data of the channel
Christopher Faulet5a2c6612021-08-15 20:35:25 +02002089 buffer, including the '\\n'. The returned data are removed from the buffer. If
2090 no line is found, and if called by an action, this function yields to wait for
2091 more data, except if the channel can't receive more data. In this case all
2092 data are returned.
Christopher Faulet6a79fc12021-08-06 16:02:36 +02002093
2094 If there is no incoming data and the channel can't receive more data, a 'nil'
2095 value is returned.
2096
2097 :param class_channel channel: The manipulated Channel.
2098 :returns: a string containing the line found or nil.
2099
2100 .. warning::
Boyang Li60cfe8b2022-05-10 18:11:00 +00002101 This function is deprecated. :js:func:`Channel.line()` must be used to
Christopher Faulet6a79fc12021-08-06 16:02:36 +02002102 retrieve a line followed by a call to :js:func:`Channel:remove()` to remove
2103 data.
2104
2105 .. code-block:: lua
2106
2107 local line = chn:line(0, -1)
2108 chn:remove(0, line:len())
2109
2110 ..
2111
2112.. js:function:: Channel.get_in_len(channel)
2113
Boyang Li60cfe8b2022-05-10 18:11:00 +00002114 **DEPRECATED**
Christopher Faulet6a79fc12021-08-06 16:02:36 +02002115
Christopher Faulet5a2c6612021-08-15 20:35:25 +02002116 This function returns the length of the input part of the buffer. When called
2117 by a filter, this value is relative to the filter.
Christopher Faulet6a79fc12021-08-06 16:02:36 +02002118
2119 :param class_channel channel: The manipulated Channel.
2120 :returns: an integer containing the amount of available bytes.
2121
2122 .. warning::
2123 This function is deprecated. :js:func:`Channel.input()` must be used
2124 instead.
2125
2126.. js:function:: Channel.get_out_len(channel)
2127
Boyang Li60cfe8b2022-05-10 18:11:00 +00002128 **DEPRECATED**
Christopher Faulet6a79fc12021-08-06 16:02:36 +02002129
Christopher Faulet5a2c6612021-08-15 20:35:25 +02002130 This function returns the length of the output part of the buffer. When called
2131 by a filter, this value is relative to the filter.
Christopher Faulet6a79fc12021-08-06 16:02:36 +02002132
2133 :param class_channel channel: The manipulated Channel.
2134 :returns: an integer containing the amount of available bytes.
2135
2136 .. warning::
2137 This function is deprecated. :js:func:`Channel.output()` must be used
2138 instead.
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002139
2140.. _http_class:
Thierry FOURNIER08504f42015-03-16 14:17:08 +01002141
2142HTTP class
2143==========
2144
2145.. js:class:: HTTP
2146
2147 This class contain all the HTTP manipulation functions.
2148
Pieter Baauw386a1272015-08-16 15:26:24 +02002149.. js:function:: HTTP.req_get_headers(http)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01002150
Patrick Hemmerc6a1d712018-05-01 21:30:41 -04002151 Returns a table containing all the request headers.
Thierry FOURNIER08504f42015-03-16 14:17:08 +01002152
2153 :param class_http http: The related http object.
Patrick Hemmerc6a1d712018-05-01 21:30:41 -04002154 :returns: table of headers.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01002155 :see: :js:func:`HTTP.res_get_headers`
Thierry FOURNIER08504f42015-03-16 14:17:08 +01002156
Patrick Hemmerc6a1d712018-05-01 21:30:41 -04002157 This is the form of the returned table:
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002158
2159.. code-block:: lua
2160
2161 HTTP:req_get_headers()['<header-name>'][<header-index>] = "<header-value>"
2162
2163 local hdr = HTTP:req_get_headers()
2164 hdr["host"][0] = "www.test.com"
2165 hdr["accept"][0] = "audio/basic q=1"
2166 hdr["accept"][1] = "audio/*, q=0.2"
2167 hdr["accept"][2] = "*/*, q=0.1"
2168..
2169
Pieter Baauw386a1272015-08-16 15:26:24 +02002170.. js:function:: HTTP.res_get_headers(http)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01002171
Patrick Hemmerc6a1d712018-05-01 21:30:41 -04002172 Returns a table containing all the response headers.
Thierry FOURNIER08504f42015-03-16 14:17:08 +01002173
2174 :param class_http http: The related http object.
Patrick Hemmerc6a1d712018-05-01 21:30:41 -04002175 :returns: table of headers.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01002176 :see: :js:func:`HTTP.req_get_headers`
Thierry FOURNIER08504f42015-03-16 14:17:08 +01002177
Patrick Hemmerc6a1d712018-05-01 21:30:41 -04002178 This is the form of the returned table:
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002179
2180.. code-block:: lua
2181
2182 HTTP:res_get_headers()['<header-name>'][<header-index>] = "<header-value>"
2183
2184 local hdr = HTTP:req_get_headers()
2185 hdr["host"][0] = "www.test.com"
2186 hdr["accept"][0] = "audio/basic q=1"
2187 hdr["accept"][1] = "audio/*, q=0.2"
2188 hdr["accept"][2] = "*.*, q=0.1"
2189..
2190
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002191.. js:function:: HTTP.req_add_header(http, name, value)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01002192
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02002193 Appends a HTTP header field in the request whose name is
Thierry FOURNIER08504f42015-03-16 14:17:08 +01002194 specified in "name" and whose value is defined in "value".
2195
2196 :param class_http http: The related http object.
2197 :param string name: The header name.
2198 :param string value: The header value.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01002199 :see: :js:func:`HTTP.res_add_header`
Thierry FOURNIER08504f42015-03-16 14:17:08 +01002200
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002201.. js:function:: HTTP.res_add_header(http, name, value)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01002202
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02002203 Appends a HTTP header field in the response whose name is
Thierry FOURNIER08504f42015-03-16 14:17:08 +01002204 specified in "name" and whose value is defined in "value".
2205
2206 :param class_http http: The related http object.
2207 :param string name: The header name.
2208 :param string value: The header value.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01002209 :see: :js:func:`HTTP.req_add_header`
Thierry FOURNIER08504f42015-03-16 14:17:08 +01002210
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002211.. js:function:: HTTP.req_del_header(http, name)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01002212
2213 Removes all HTTP header fields in the request whose name is
2214 specified in "name".
2215
2216 :param class_http http: The related http object.
2217 :param string name: The header name.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01002218 :see: :js:func:`HTTP.res_del_header`
Thierry FOURNIER08504f42015-03-16 14:17:08 +01002219
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002220.. js:function:: HTTP.res_del_header(http, name)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01002221
2222 Removes all HTTP header fields in the response whose name is
2223 specified in "name".
2224
2225 :param class_http http: The related http object.
2226 :param string name: The header name.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01002227 :see: :js:func:`HTTP.req_del_header`
Thierry FOURNIER08504f42015-03-16 14:17:08 +01002228
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002229.. js:function:: HTTP.req_set_header(http, name, value)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01002230
Bertrand Jacquin874a35c2018-09-10 21:26:07 +01002231 This variable replace all occurrence of all header "name", by only
Thierry FOURNIER08504f42015-03-16 14:17:08 +01002232 one containing the "value".
2233
2234 :param class_http http: The related http object.
2235 :param string name: The header name.
2236 :param string value: The header value.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01002237 :see: :js:func:`HTTP.res_set_header`
Thierry FOURNIER08504f42015-03-16 14:17:08 +01002238
Bertrand Jacquin874a35c2018-09-10 21:26:07 +01002239 This function does the same work as the following code:
Thierry FOURNIER08504f42015-03-16 14:17:08 +01002240
2241.. code-block:: lua
2242
2243 function fcn(txn)
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002244 TXN.http:req_del_header("header")
2245 TXN.http:req_add_header("header", "value")
Thierry FOURNIER08504f42015-03-16 14:17:08 +01002246 end
2247..
2248
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002249.. js:function:: HTTP.res_set_header(http, name, value)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01002250
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02002251 This function replaces all occurrence of all header "name", by only
Thierry FOURNIER08504f42015-03-16 14:17:08 +01002252 one containing the "value".
2253
2254 :param class_http http: The related http object.
2255 :param string name: The header name.
2256 :param string value: The header value.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01002257 :see: :js:func:`HTTP.req_rep_header()`
Thierry FOURNIER08504f42015-03-16 14:17:08 +01002258
Pieter Baauw386a1272015-08-16 15:26:24 +02002259.. js:function:: HTTP.req_rep_header(http, name, regex, replace)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01002260
2261 Matches the regular expression in all occurrences of header field "name"
2262 according to "regex", and replaces them with the "replace" argument. The
2263 replacement value can contain back references like \1, \2, ... This
2264 function works with the request.
2265
2266 :param class_http http: The related http object.
2267 :param string name: The header name.
2268 :param string regex: The match regular expression.
2269 :param string replace: The replacement value.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01002270 :see: :js:func:`HTTP.res_rep_header()`
Thierry FOURNIER08504f42015-03-16 14:17:08 +01002271
Pieter Baauw386a1272015-08-16 15:26:24 +02002272.. js:function:: HTTP.res_rep_header(http, name, regex, string)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01002273
2274 Matches the regular expression in all occurrences of header field "name"
2275 according to "regex", and replaces them with the "replace" argument. The
2276 replacement value can contain back references like \1, \2, ... This
2277 function works with the request.
2278
2279 :param class_http http: The related http object.
2280 :param string name: The header name.
2281 :param string regex: The match regular expression.
2282 :param string replace: The replacement value.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01002283 :see: :js:func:`HTTP.req_rep_header()`
Thierry FOURNIER08504f42015-03-16 14:17:08 +01002284
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002285.. js:function:: HTTP.req_set_method(http, method)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01002286
2287 Rewrites the request method with the parameter "method".
2288
2289 :param class_http http: The related http object.
2290 :param string method: The new method.
2291
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002292.. js:function:: HTTP.req_set_path(http, path)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01002293
2294 Rewrites the request path with the "path" parameter.
2295
2296 :param class_http http: The related http object.
2297 :param string path: The new path.
2298
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002299.. js:function:: HTTP.req_set_query(http, query)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01002300
2301 Rewrites the request's query string which appears after the first question
2302 mark ("?") with the parameter "query".
2303
2304 :param class_http http: The related http object.
2305 :param string query: The new query.
2306
Thierry FOURNIER0d79cf62015-08-26 14:20:58 +02002307.. js:function:: HTTP.req_set_uri(http, uri)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01002308
2309 Rewrites the request URI with the parameter "uri".
2310
2311 :param class_http http: The related http object.
2312 :param string uri: The new uri.
2313
Robin H. Johnson52f5db22017-01-01 13:10:52 -08002314.. js:function:: HTTP.res_set_status(http, status [, reason])
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +02002315
Robin H. Johnson52f5db22017-01-01 13:10:52 -08002316 Rewrites the response status code with the parameter "code".
2317
2318 If no custom reason is provided, it will be generated from the status.
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +02002319
2320 :param class_http http: The related http object.
2321 :param integer status: The new response status code.
Robin H. Johnson52f5db22017-01-01 13:10:52 -08002322 :param string reason: The new response reason (optional).
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +02002323
William Lallemand00a15022021-11-19 16:02:44 +01002324.. _httpclient_class:
2325
2326HTTPClient class
2327================
2328
2329.. js:class:: HTTPClient
2330
2331 The httpclient class allows issue of outbound HTTP requests through a simple
2332 API without the knowledge of HAProxy internals.
2333
2334.. js:function:: HTTPClient.get(httpclient, request)
2335.. js:function:: HTTPClient.head(httpclient, request)
2336.. js:function:: HTTPClient.put(httpclient, request)
2337.. js:function:: HTTPClient.post(httpclient, request)
2338.. js:function:: HTTPClient.delete(httpclient, request)
2339
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02002340 Send a HTTP request and wait for a response. GET, HEAD PUT, POST and DELETE
2341 methods can be used.
2342 The HTTPClient will send asynchronously the data and is able to send and
2343 receive more than HAProxy bufsize.
William Lallemand00a15022021-11-19 16:02:44 +01002344
William Lallemanda9256192022-10-21 11:48:24 +02002345 The HTTPClient interface is not able to decompress responses, it is not
2346 recommended to send an Accept-Encoding in the request so the response is
2347 received uncompressed.
William Lallemand00a15022021-11-19 16:02:44 +01002348
2349 :param class httpclient: Is the manipulated HTTPClient.
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02002350 :param table request: Is a table containing the parameters of the request
2351 that will be send.
2352 :param string request.url: Is a mandatory parameter for the request that
2353 contains the URL.
2354 :param string request.body: Is an optional parameter for the request that
2355 contains the body to send.
2356 :param table request.headers: Is an optional parameter for the request that
2357 contains the headers to send.
2358 :param string request.dst: Is an optional parameter for the destination in
2359 haproxy address format.
2360 :param integer request.timeout: Optional timeout parameter, set a
2361 "timeout server" on the connections.
William Lallemand00a15022021-11-19 16:02:44 +01002362 :returns: Lua table containing the response
2363
2364
2365.. code-block:: lua
2366
2367 local httpclient = core.httpclient()
William Lallemand4f4f2b72022-02-17 20:00:23 +01002368 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 +01002369
2370..
2371
2372.. code-block:: lua
2373
2374 response = {
2375 status = 400,
2376 reason = "Bad request",
2377 headers = {
2378 ["content-type"] = { "text/html" },
2379 ["cache-control"] = { "no-cache", "no-store" },
2380 },
William Lallemand4f4f2b72022-02-17 20:00:23 +01002381 body = "<html><body><h1>invalid request<h1></body></html>",
William Lallemand00a15022021-11-19 16:02:44 +01002382 }
2383..
2384
2385
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002386.. _txn_class:
2387
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002388TXN class
2389=========
2390
2391.. js:class:: TXN
2392
2393 The txn class contain all the functions relative to the http or tcp
2394 transaction (Note than a tcp stream is the same than a tcp transaction, but
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02002395 a HTTP transaction is not the same than a tcp stream).
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002396
2397 The usage of this class permits to retrieve data from the requests, alter it
2398 and forward it.
2399
2400 All the functions provided by this class are available in the context
Christopher Faulet5a2c6612021-08-15 20:35:25 +02002401 **sample-fetches**, **actions** and **filters**.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002402
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002403.. js:attribute:: TXN.c
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002404
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002405 :returns: An :ref:`converters_class`.
2406
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002407 This attribute contains a Converters class object.
2408
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002409.. js:attribute:: TXN.sc
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002410
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002411 :returns: An :ref:`converters_class`.
2412
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002413 This attribute contains a Converters class object. The functions of
2414 this object returns always a string.
2415
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002416.. js:attribute:: TXN.f
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002417
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002418 :returns: An :ref:`fetches_class`.
2419
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002420 This attribute contains a Fetches class object.
2421
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002422.. js:attribute:: TXN.sf
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002423
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002424 :returns: An :ref:`fetches_class`.
2425
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002426 This attribute contains a Fetches class object. The functions of
2427 this object returns always a string.
2428
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002429.. js:attribute:: TXN.req
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002430
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002431 :returns: An :ref:`channel_class`.
2432
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002433 This attribute contains a channel class object for the request buffer.
2434
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002435.. js:attribute:: TXN.res
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002436
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002437 :returns: An :ref:`channel_class`.
2438
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002439 This attribute contains a channel class object for the response buffer.
2440
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002441.. js:attribute:: TXN.http
Thierry FOURNIER08504f42015-03-16 14:17:08 +01002442
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002443 :returns: An :ref:`http_class`.
2444
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02002445 This attribute contains a HTTP class object. It is available only if the
Thierry FOURNIER08504f42015-03-16 14:17:08 +01002446 proxy has the "mode http" enabled.
2447
Christopher Faulet5a2c6612021-08-15 20:35:25 +02002448.. js:attribute:: TXN.http_req
2449
2450 :returns: An :ref:`httpmessage_class`.
2451
2452 This attribute contains the request HTTPMessage class object. It is available
2453 only if the proxy has the "mode http" enabled and only in the **filters**
2454 context.
2455
2456.. js:attribute:: TXN.http_res
2457
2458 :returns: An :ref:`httpmessage_class`.
2459
2460 This attribute contains the response HTTPMessage class object. It is available
2461 only if the proxy has the "mode http" enabled and only in the **filters**
2462 context.
2463
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01002464.. js:function:: TXN.log(TXN, loglevel, msg)
2465
2466 This function sends a log. The log is sent, according with the HAProxy
2467 configuration file, on the default syslog server if it is configured and on
2468 the stderr if it is allowed.
2469
2470 :param class_txn txn: The class txn object containing the data.
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02002471 :param integer loglevel: Is the log level associated with the message. It is
2472 a number between 0 and 7.
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01002473 :param string msg: The log content.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01002474 :see: :js:attr:`core.emerg`, :js:attr:`core.alert`, :js:attr:`core.crit`,
2475 :js:attr:`core.err`, :js:attr:`core.warning`, :js:attr:`core.notice`,
2476 :js:attr:`core.info`, :js:attr:`core.debug` (log level definitions)
2477 :see: :js:func:`TXN.deflog`
2478 :see: :js:func:`TXN.Debug`
2479 :see: :js:func:`TXN.Info`
2480 :see: :js:func:`TXN.Warning`
2481 :see: :js:func:`TXN.Alert`
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01002482
2483.. js:function:: TXN.deflog(TXN, msg)
2484
Bertrand Jacquin874a35c2018-09-10 21:26:07 +01002485 Sends a log line with the default loglevel for the proxy associated with the
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01002486 transaction.
2487
2488 :param class_txn txn: The class txn object containing the data.
2489 :param string msg: The log content.
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02002490 :see: :js:func:`TXN.log`
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01002491
2492.. js:function:: TXN.Debug(txn, msg)
2493
2494 :param class_txn txn: The class txn object containing the data.
2495 :param string msg: The log content.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01002496 :see: :js:func:`TXN.log`
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01002497
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02002498 Does the same job as:
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01002499
2500.. code-block:: lua
2501
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002502 function Debug(txn, msg)
2503 TXN.log(txn, core.debug, msg)
2504 end
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01002505..
2506
2507.. js:function:: TXN.Info(txn, msg)
2508
2509 :param class_txn txn: The class txn object containing the data.
2510 :param string msg: The log content.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01002511 :see: :js:func:`TXN.log`
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01002512
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02002513 Does the same job as:
2514
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01002515.. code-block:: lua
2516
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02002517 function Info(txn, msg)
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002518 TXN.log(txn, core.info, msg)
2519 end
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01002520..
2521
2522.. js:function:: TXN.Warning(txn, msg)
2523
2524 :param class_txn txn: The class txn object containing the data.
2525 :param string msg: The log content.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01002526 :see: :js:func:`TXN.log`
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01002527
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02002528 Does the same job as:
2529
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01002530.. code-block:: lua
2531
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02002532 function Warning(txn, msg)
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002533 TXN.log(txn, core.warning, msg)
2534 end
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01002535..
2536
2537.. js:function:: TXN.Alert(txn, msg)
2538
2539 :param class_txn txn: The class txn object containing the data.
2540 :param string msg: The log content.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01002541 :see: :js:func:`TXN.log`
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01002542
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02002543 Does the same job as:
2544
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01002545.. code-block:: lua
2546
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02002547 function Alert(txn, msg)
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002548 TXN.log(txn, core.alert, msg)
2549 end
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01002550..
2551
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002552.. js:function:: TXN.get_priv(txn)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002553
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002554 Return Lua data stored in the current transaction (with the `TXN.set_priv()`)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002555 function. If no data are stored, it returns a nil value.
2556
2557 :param class_txn txn: The class txn object containing the data.
Bertrand Jacquin874a35c2018-09-10 21:26:07 +01002558 :returns: the opaque data previously stored, or nil if nothing is
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02002559 available.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002560
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002561.. js:function:: TXN.set_priv(txn, data)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002562
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02002563 Store any data in the current HAProxy transaction. This action replaces the
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002564 old stored data.
2565
2566 :param class_txn txn: The class txn object containing the data.
2567 :param opaque data: The data which is stored in the transaction.
2568
Tim Duesterhus4e172c92020-05-19 13:49:42 +02002569.. js:function:: TXN.set_var(TXN, var, value[, ifexist])
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +02002570
David Carlier61fdf8b2015-10-02 11:59:38 +01002571 Converts a Lua type in a HAProxy type and store it in a variable <var>.
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +02002572
2573 :param class_txn txn: The class txn object containing the data.
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02002574 :param string var: The variable name according with the HAProxy variable
2575 syntax.
2576 :param type value: The value associated to the variable. The type can be
2577 string or integer.
2578 :param boolean ifexist: If this parameter is set to true the variable will
2579 only be set if it was defined elsewhere (i.e. used within the configuration).
2580 For global variables (using the "proc" scope), they will only be updated and
2581 never created. It is highly recommended to always set this to true.
Christopher Faulet85d79c92016-11-09 16:54:56 +01002582
2583.. js:function:: TXN.unset_var(TXN, var)
2584
2585 Unset the variable <var>.
2586
2587 :param class_txn txn: The class txn object containing the data.
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02002588 :param string var: The variable name according with the HAProxy variable
2589 syntax.
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +02002590
2591.. js:function:: TXN.get_var(TXN, var)
2592
2593 Returns data stored in the variable <var> converter in Lua type.
2594
2595 :param class_txn txn: The class txn object containing the data.
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02002596 :param string var: The variable name according with the HAProxy variable
2597 syntax.
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +02002598
Christopher Faulet700d9e82020-01-31 12:21:52 +01002599.. js:function:: TXN.reply([reply])
2600
2601 Return a new reply object
2602
2603 :param table reply: A table containing info to initialize the reply fields.
2604 :returns: A :ref:`reply_class` object.
2605
2606 The table used to initialized the reply object may contain following entries :
2607
2608 * status : The reply status code. the code 200 is used by default.
2609 * reason : The reply reason. The reason corresponding to the status code is
2610 used by default.
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02002611 * headers : A list of headers, indexed by header name. Empty by default. For
Christopher Faulet700d9e82020-01-31 12:21:52 +01002612 a given name, multiple values are possible, stored in an ordered list.
2613 * body : The reply body, empty by default.
2614
2615.. code-block:: lua
2616
2617 local reply = txn:reply{
2618 status = 400,
2619 reason = "Bad request",
2620 headers = {
2621 ["content-type"] = { "text/html" },
2622 ["cache-control"] = {"no-cache", "no-store" }
2623 },
2624 body = "<html><body><h1>invalid request<h1></body></html>"
2625 }
2626..
2627 :see: :js:class:`Reply`
2628
2629.. js:function:: TXN.done(txn[, reply])
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002630
Willy Tarreaubc183a62015-08-28 10:39:11 +02002631 This function terminates processing of the transaction and the associated
Christopher Faulet700d9e82020-01-31 12:21:52 +01002632 session and optionally reply to the client for HTTP sessions.
2633
2634 :param class_txn txn: The class txn object containing the data.
2635 :param class_reply reply: The class reply object to return to the client.
2636
2637 This functions can be used when a critical error is detected or to terminate
Willy Tarreaubc183a62015-08-28 10:39:11 +02002638 processing after some data have been returned to the client (eg: a redirect).
Christopher Faulet700d9e82020-01-31 12:21:52 +01002639 To do so, a reply may be provided. This object is optional and may contain a
2640 status code, a reason, a header list and a body. All these fields are
Christopher Faulet7855b192021-11-09 18:39:51 +01002641 optional. When not provided, the default values are used. By default, with an
2642 empty reply object, an empty HTTP 200 response is returned to the client. If
2643 no reply object is provided, the transaction is terminated without any
2644 reply. If a reply object is provided, it must not exceed the buffer size once
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02002645 converted into the internal HTTP representation. Because for now there is no
Christopher Faulet7855b192021-11-09 18:39:51 +01002646 easy way to be sure it fits, it is probably better to keep it reasonably
2647 small.
Christopher Faulet700d9e82020-01-31 12:21:52 +01002648
2649 The reply object may be fully created in lua or the class Reply may be used to
2650 create it.
2651
2652.. code-block:: lua
2653
2654 local reply = txn:reply()
2655 reply:set_status(400, "Bad request")
2656 reply:add_header("content-type", "text/html")
2657 reply:add_header("cache-control", "no-cache")
2658 reply:add_header("cache-control", "no-store")
2659 reply:set_body("<html><body><h1>invalid request<h1></body></html>")
2660 txn:done(reply)
2661..
2662
2663.. code-block:: lua
2664
2665 txn:done{
2666 status = 400,
2667 reason = "Bad request",
2668 headers = {
2669 ["content-type"] = { "text/html" },
2670 ["cache-control"] = { "no-cache", "no-store" },
2671 },
2672 body = "<html><body><h1>invalid request<h1></body></html>"
2673 }
2674..
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002675
Christopher Faulet1e9b1b62021-08-11 10:14:30 +02002676 .. warning::
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02002677 It does not make sense to call this function from sample-fetches. In this
2678 case the behavior is the same than core.done(): it finishes the Lua
Christopher Faulet1e9b1b62021-08-11 10:14:30 +02002679 execution. The transaction is really aborted only from an action registered
2680 function.
Thierry FOURNIERab00df62016-07-14 11:42:37 +02002681
Christopher Faulet700d9e82020-01-31 12:21:52 +01002682 :see: :js:func:`TXN.reply`, :js:class:`Reply`
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002683
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002684.. js:function:: TXN.set_loglevel(txn, loglevel)
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01002685
2686 Is used to change the log level of the current request. The "loglevel" must
2687 be an integer between 0 and 7.
2688
2689 :param class_txn txn: The class txn object containing the data.
2690 :param integer loglevel: The required log level. This variable can be one of
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01002691 :see: :js:attr:`core.emerg`, :js:attr:`core.alert`, :js:attr:`core.crit`,
2692 :js:attr:`core.err`, :js:attr:`core.warning`, :js:attr:`core.notice`,
2693 :js:attr:`core.info`, :js:attr:`core.debug` (log level definitions)
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01002694
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002695.. js:function:: TXN.set_tos(txn, tos)
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01002696
2697 Is used to set the TOS or DSCP field value of packets sent to the client to
2698 the value passed in "tos" on platforms which support this.
2699
2700 :param class_txn txn: The class txn object containing the data.
2701 :param integer tos: The new TOS os DSCP.
2702
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002703.. js:function:: TXN.set_mark(txn, mark)
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01002704
2705 Is used to set the Netfilter MARK on all packets sent to the client to the
2706 value passed in "mark" on platforms which support it.
2707
2708 :param class_txn txn: The class txn object containing the data.
2709 :param integer mark: The mark value.
2710
Patrick Hemmer268a7072018-05-11 12:52:31 -04002711.. js:function:: TXN.set_priority_class(txn, prio)
2712
2713 This function adjusts the priority class of the transaction. The value should
2714 be within the range -2047..2047. Values outside this range will be
2715 truncated.
2716
2717 See the HAProxy configuration.txt file keyword "http-request" action
2718 "set-priority-class" for details.
2719
2720.. js:function:: TXN.set_priority_offset(txn, prio)
2721
2722 This function adjusts the priority offset of the transaction. The value
2723 should be within the range -524287..524287. Values outside this range will be
2724 truncated.
2725
2726 See the HAProxy configuration.txt file keyword "http-request" action
2727 "set-priority-offset" for details.
2728
Christopher Faulet700d9e82020-01-31 12:21:52 +01002729.. _reply_class:
2730
2731Reply class
2732============
2733
2734.. js:class:: Reply
2735
2736 **context**: action
2737
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02002738 This class represents a HTTP response message. It provides some methods to
Christopher Faulet7855b192021-11-09 18:39:51 +01002739 enrich it. Once converted into the internal HTTP representation, the response
2740 message must not exceed the buffer size. Because for now there is no
2741 easy way to be sure it fits, it is probably better to keep it reasonably
2742 small.
2743
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02002744 See tune.bufsize in the configuration manual for details.
Christopher Faulet700d9e82020-01-31 12:21:52 +01002745
2746.. code-block:: lua
2747
2748 local reply = txn:reply({status = 400}) -- default HTTP 400 reason-phase used
2749 reply:add_header("content-type", "text/html")
2750 reply:add_header("cache-control", "no-cache")
2751 reply:add_header("cache-control", "no-store")
2752 reply:set_body("<html><body><h1>invalid request<h1></body></html>")
2753..
2754
2755 :see: :js:func:`TXN.reply`
2756
2757.. js:attribute:: Reply.status
2758
2759 The reply status code. By default, the status code is set to 200.
2760
2761 :returns: integer
2762
2763.. js:attribute:: Reply.reason
2764
2765 The reason string describing the status code.
2766
2767 :returns: string
2768
2769.. js:attribute:: Reply.headers
2770
2771 A table indexing all reply headers by name. To each name is associated an
2772 ordered list of values.
2773
2774 :returns: Lua table
2775
2776.. code-block:: lua
2777
2778 {
2779 ["content-type"] = { "text/html" },
2780 ["cache-control"] = {"no-cache", "no-store" },
2781 x_header_name = { "value1", "value2", ... }
2782 ...
2783 }
2784..
2785
2786.. js:attribute:: Reply.body
2787
2788 The reply payload.
2789
2790 :returns: string
2791
2792.. js:function:: Reply.set_status(REPLY, status[, reason])
2793
2794 Set the reply status code and optionally the reason-phrase. If the reason is
2795 not provided, the default reason corresponding to the status code is used.
2796
2797 :param class_reply reply: The related Reply object.
2798 :param integer status: The reply status code.
2799 :param string reason: The reply status reason (optional).
2800
2801.. js:function:: Reply.add_header(REPLY, name, value)
2802
2803 Add a header to the reply object. If the header does not already exist, a new
2804 entry is created with its name as index and a one-element list containing its
2805 value as value. Otherwise, the header value is appended to the ordered list of
2806 values associated to the header name.
2807
2808 :param class_reply reply: The related Reply object.
2809 :param string name: The header field name.
2810 :param string value: The header field value.
2811
2812.. js:function:: Reply.del_header(REPLY, name)
2813
2814 Remove all occurrences of a header name from the reply object.
2815
2816 :param class_reply reply: The related Reply object.
2817 :param string name: The header field name.
2818
2819.. js:function:: Reply.set_body(REPLY, body)
2820
2821 Set the reply payload.
2822
2823 :param class_reply reply: The related Reply object.
2824 :param string body: The reply payload.
2825
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002826.. _socket_class:
2827
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002828Socket class
2829============
2830
2831.. js:class:: Socket
2832
2833 This class must be compatible with the Lua Socket class. Only the 'client'
2834 functions are available. See the Lua Socket documentation:
2835
2836 `http://w3.impa.br/~diego/software/luasocket/tcp.html
2837 <http://w3.impa.br/~diego/software/luasocket/tcp.html>`_
2838
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002839.. js:function:: Socket.close(socket)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002840
2841 Closes a TCP object. The internal socket used by the object is closed and the
2842 local address to which the object was bound is made available to other
2843 applications. No further operations (except for further calls to the close
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002844 method) are allowed on a closed Socket.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002845
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002846 :param class_socket socket: Is the manipulated Socket.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002847
2848 Note: It is important to close all used sockets once they are not needed,
2849 since, in many systems, each socket uses a file descriptor, which are limited
2850 system resources. Garbage-collected objects are automatically closed before
2851 destruction, though.
2852
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002853.. js:function:: Socket.connect(socket, address[, port])
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002854
2855 Attempts to connect a socket object to a remote host.
2856
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002857
2858 In case of error, the method returns nil followed by a string describing the
2859 error. In case of success, the method returns 1.
2860
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002861 :param class_socket socket: Is the manipulated Socket.
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002862 :param string address: can be an IP address or a host name. See below for more
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02002863 information.
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002864 :param integer port: must be an integer number in the range [1..64K].
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002865 :returns: 1 or nil.
2866
Bertrand Jacquin874a35c2018-09-10 21:26:07 +01002867 An address field extension permits to use the connect() function to connect to
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002868 other stream than TCP. The syntax containing a simpleipv4 or ipv6 address is
2869 the basically expected format. This format requires the port.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002870
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002871 Other format accepted are a socket path like "/socket/path", it permits to
Bertrand Jacquin874a35c2018-09-10 21:26:07 +01002872 connect to a socket. Abstract namespaces are supported with the prefix
Joseph Herlant02cedc42018-11-13 19:45:17 -08002873 "abns@", and finally a file descriptor can be passed with the prefix "fd@".
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002874 The prefix "ipv4@", "ipv6@" and "unix@" are also supported. The port can be
Bertrand Jacquin874a35c2018-09-10 21:26:07 +01002875 passed int the string. The syntax "127.0.0.1:1234" is valid. In this case, the
Tim Duesterhus6edab862018-01-06 19:04:45 +01002876 parameter *port* must not be set.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002877
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002878.. js:function:: Socket.connect_ssl(socket, address, port)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002879
2880 Same behavior than the function socket:connect, but uses SSL.
2881
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002882 :param class_socket socket: Is the manipulated Socket.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002883 :returns: 1 or nil.
2884
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002885.. js:function:: Socket.getpeername(socket)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002886
2887 Returns information about the remote side of a connected client object.
2888
2889 Returns a string with the IP address of the peer, followed by the port number
2890 that peer is using for the connection. In case of error, the method returns
2891 nil.
2892
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002893 :param class_socket socket: Is the manipulated Socket.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002894 :returns: a string containing the server information.
2895
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002896.. js:function:: Socket.getsockname(socket)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002897
2898 Returns the local address information associated to the object.
2899
2900 The method returns a string with local IP address and a number with the port.
2901 In case of error, the method returns nil.
2902
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002903 :param class_socket socket: Is the manipulated Socket.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002904 :returns: a string containing the client information.
2905
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002906.. js:function:: Socket.receive(socket, [pattern [, prefix]])
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002907
2908 Reads data from a client object, according to the specified read pattern.
2909 Patterns follow the Lua file I/O format, and the difference in performance
2910 between all patterns is negligible.
2911
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002912 :param class_socket socket: Is the manipulated Socket.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002913 :param string|integer pattern: Describe what is required (see below).
2914 :param string prefix: A string which will be prefix the returned data.
2915 :returns: a string containing the required data or nil.
2916
2917 Pattern can be any of the following:
2918
2919 * **`*a`**: reads from the socket until the connection is closed. No
2920 end-of-line translation is performed;
2921
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002922 * **`*l`**: reads a line of text from the Socket. The line is terminated by a
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002923 LF character (ASCII 10), optionally preceded by a CR character
2924 (ASCII 13). The CR and LF characters are not included in the
2925 returned line. In fact, all CR characters are ignored by the
2926 pattern. This is the default pattern.
2927
2928 * **number**: causes the method to read a specified number of bytes from the
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002929 Socket. Prefix is an optional string to be concatenated to the
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002930 beginning of any received data before return.
2931
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002932 * **empty**: If the pattern is left empty, the default option is `*l`.
2933
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002934 If successful, the method returns the received pattern. In case of error, the
2935 method returns nil followed by an error message which can be the string
2936 'closed' in case the connection was closed before the transmission was
2937 completed or the string 'timeout' in case there was a timeout during the
2938 operation. Also, after the error message, the function returns the partial
2939 result of the transmission.
2940
2941 Important note: This function was changed severely. It used to support
2942 multiple patterns (but I have never seen this feature used) and now it
2943 doesn't anymore. Partial results used to be returned in the same way as
2944 successful results. This last feature violated the idea that all functions
2945 should return nil on error. Thus it was changed too.
2946
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002947.. js:function:: Socket.send(socket, data [, start [, end ]])
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002948
2949 Sends data through client object.
2950
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002951 :param class_socket socket: Is the manipulated Socket.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002952 :param string data: The data that will be sent.
2953 :param integer start: The start position in the buffer of the data which will
2954 be sent.
2955 :param integer end: The end position in the buffer of the data which will
2956 be sent.
2957 :returns: see below.
2958
2959 Data is the string to be sent. The optional arguments i and j work exactly
2960 like the standard string.sub Lua function to allow the selection of a
2961 substring to be sent.
2962
2963 If successful, the method returns the index of the last byte within [start,
2964 end] that has been sent. Notice that, if start is 1 or absent, this is
2965 effectively the total number of bytes sent. In case of error, the method
2966 returns nil, followed by an error message, followed by the index of the last
2967 byte within [start, end] that has been sent. You might want to try again from
2968 the byte following that. The error message can be 'closed' in case the
2969 connection was closed before the transmission was completed or the string
2970 'timeout' in case there was a timeout during the operation.
2971
2972 Note: Output is not buffered. For small strings, it is always better to
2973 concatenate them in Lua (with the '..' operator) and send the result in one
2974 call instead of calling the method several times.
2975
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002976.. js:function:: Socket.setoption(socket, option [, value])
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002977
2978 Just implemented for compatibility, this cal does nothing.
2979
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002980.. js:function:: Socket.settimeout(socket, value [, mode])
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002981
2982 Changes the timeout values for the object. All I/O operations are blocking.
2983 That is, any call to the methods send, receive, and accept will block
2984 indefinitely, until the operation completes. The settimeout method defines a
2985 limit on the amount of time the I/O methods can block. When a timeout time
2986 has elapsed, the affected methods give up and fail with an error code.
2987
2988 The amount of time to wait is specified as the value parameter, in seconds.
2989
Mark Lakes56cc1252018-03-27 09:48:06 +02002990 The timeout modes are not implemented, the only settable timeout is the
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002991 inactivity time waiting for complete the internal buffer send or waiting for
2992 receive data.
2993
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002994 :param class_socket socket: Is the manipulated Socket.
Bertrand Jacquin874a35c2018-09-10 21:26:07 +01002995 :param float value: The timeout value. Use floating point to specify
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02002996 milliseconds.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002997
Thierry FOURNIER31904272017-10-25 12:59:51 +02002998.. _regex_class:
2999
3000Regex class
3001===========
3002
3003.. js:class:: Regex
3004
3005 This class allows the usage of HAProxy regexes because classic lua doesn't
3006 provides regexes. This class inherits the HAProxy compilation options, so the
3007 regexes can be libc regex, pcre regex or pcre JIT regex.
3008
3009 The expression matching number is limited to 20 per regex. The only available
3010 option is case sensitive.
3011
3012 Because regexes compilation is a heavy process, it is better to define all
3013 your regexes in the **body context** and use it during the runtime.
3014
3015.. code-block:: lua
3016
3017 -- Create the regex
3018 st, regex = Regex.new("needle (..) (...)", true);
3019
3020 -- Check compilation errors
3021 if st == false then
3022 print "error: " .. regex
3023 end
3024
3025 -- Match the regexes
3026 print(regex:exec("Looking for a needle in the haystack")) -- true
3027 print(regex:exec("Lokking for a cat in the haystack")) -- false
3028
3029 -- Extract words
3030 st, list = regex:match("Looking for a needle in the haystack")
3031 print(st) -- true
3032 print(list[1]) -- needle in the
3033 print(list[2]) -- in
3034 print(list[3]) -- the
3035
3036.. js:function:: Regex.new(regex, case_sensitive)
3037
3038 Create and compile a regex.
3039
3040 :param string regex: The regular expression according with the libc or pcre
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02003041 standard
Thierry FOURNIER31904272017-10-25 12:59:51 +02003042 :param boolean case_sensitive: Match is case sensitive or not.
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02003043 :returns: boolean status and :ref:`regex_class` or string containing fail
3044 reason.
Thierry FOURNIER31904272017-10-25 12:59:51 +02003045
3046.. js:function:: Regex.exec(regex, str)
3047
3048 Execute the regex.
3049
3050 :param class_regex regex: A :ref:`regex_class` object.
3051 :param string str: The input string will be compared with the compiled regex.
3052 :returns: a boolean status according with the match result.
3053
3054.. js:function:: Regex.match(regex, str)
3055
3056 Execute the regex and return matched expressions.
3057
3058 :param class_map map: A :ref:`regex_class` object.
3059 :param string str: The input string will be compared with the compiled regex.
3060 :returns: a boolean status according with the match result, and
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02003061 a table containing all the string matched in order of declaration.
Thierry FOURNIER31904272017-10-25 12:59:51 +02003062
Thierry FOURNIERdc595002015-12-21 11:13:52 +01003063.. _map_class:
3064
Thierry FOURNIER3def3932015-04-07 11:27:54 +02003065Map class
3066=========
3067
3068.. js:class:: Map
3069
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02003070 This class permits to do some lookups in HAProxy maps. The declared maps can
Bertrand Jacquin874a35c2018-09-10 21:26:07 +01003071 be modified during the runtime through the HAProxy management socket.
Thierry FOURNIER3def3932015-04-07 11:27:54 +02003072
3073.. code-block:: lua
3074
Thierry FOURNIERdc595002015-12-21 11:13:52 +01003075 default = "usa"
Thierry FOURNIER3def3932015-04-07 11:27:54 +02003076
Thierry FOURNIERdc595002015-12-21 11:13:52 +01003077 -- Create and load map
Thierry FOURNIER4dc71972017-01-28 08:33:08 +01003078 geo = Map.new("geo.map", Map._ip);
Thierry FOURNIER3def3932015-04-07 11:27:54 +02003079
Thierry FOURNIERdc595002015-12-21 11:13:52 +01003080 -- Create new fetch that returns the user country
3081 core.register_fetches("country", function(txn)
3082 local src;
3083 local loc;
Thierry FOURNIER3def3932015-04-07 11:27:54 +02003084
Thierry FOURNIERdc595002015-12-21 11:13:52 +01003085 src = txn.f:fhdr("x-forwarded-for");
3086 if (src == nil) then
3087 src = txn.f:src()
3088 if (src == nil) then
3089 return default;
3090 end
3091 end
Thierry FOURNIER3def3932015-04-07 11:27:54 +02003092
Thierry FOURNIERdc595002015-12-21 11:13:52 +01003093 -- Perform lookup
3094 loc = geo:lookup(src);
Thierry FOURNIER3def3932015-04-07 11:27:54 +02003095
Thierry FOURNIERdc595002015-12-21 11:13:52 +01003096 if (loc == nil) then
3097 return default;
3098 end
Thierry FOURNIER3def3932015-04-07 11:27:54 +02003099
Thierry FOURNIERdc595002015-12-21 11:13:52 +01003100 return loc;
3101 end);
Thierry FOURNIER3def3932015-04-07 11:27:54 +02003102
Thierry FOURNIER4dc71972017-01-28 08:33:08 +01003103.. js:attribute:: Map._int
Thierry FOURNIER3def3932015-04-07 11:27:54 +02003104
3105 See the HAProxy configuration.txt file, chapter "Using ACLs and fetching
Ilya Shipitsin2075ca82020-03-06 23:22:22 +05003106 samples" and subchapter "ACL basics" to understand this pattern matching
Thierry FOURNIER3def3932015-04-07 11:27:54 +02003107 method.
3108
Thierry FOURNIER4dc71972017-01-28 08:33:08 +01003109 Note that :js:attr:`Map.int` is also available for compatibility.
3110
3111.. js:attribute:: Map._ip
Thierry FOURNIER3def3932015-04-07 11:27:54 +02003112
3113 See the HAProxy configuration.txt file, chapter "Using ACLs and fetching
Ilya Shipitsin2075ca82020-03-06 23:22:22 +05003114 samples" and subchapter "ACL basics" to understand this pattern matching
Thierry FOURNIER3def3932015-04-07 11:27:54 +02003115 method.
3116
Thierry FOURNIER4dc71972017-01-28 08:33:08 +01003117 Note that :js:attr:`Map.ip` is also available for compatibility.
3118
3119.. js:attribute:: Map._str
Thierry FOURNIER3def3932015-04-07 11:27:54 +02003120
3121 See the HAProxy configuration.txt file, chapter "Using ACLs and fetching
Ilya Shipitsin2075ca82020-03-06 23:22:22 +05003122 samples" and subchapter "ACL basics" to understand this pattern matching
Thierry FOURNIER3def3932015-04-07 11:27:54 +02003123 method.
3124
Thierry FOURNIER4dc71972017-01-28 08:33:08 +01003125 Note that :js:attr:`Map.str` is also available for compatibility.
3126
3127.. js:attribute:: Map._beg
Thierry FOURNIER3def3932015-04-07 11:27:54 +02003128
3129 See the HAProxy configuration.txt file, chapter "Using ACLs and fetching
Ilya Shipitsin2075ca82020-03-06 23:22:22 +05003130 samples" and subchapter "ACL basics" to understand this pattern matching
Thierry FOURNIER3def3932015-04-07 11:27:54 +02003131 method.
3132
Thierry FOURNIER4dc71972017-01-28 08:33:08 +01003133 Note that :js:attr:`Map.beg` is also available for compatibility.
3134
3135.. js:attribute:: Map._sub
Thierry FOURNIER3def3932015-04-07 11:27:54 +02003136
3137 See the HAProxy configuration.txt file, chapter "Using ACLs and fetching
Ilya Shipitsin2075ca82020-03-06 23:22:22 +05003138 samples" and subchapter "ACL basics" to understand this pattern matching
Thierry FOURNIER3def3932015-04-07 11:27:54 +02003139 method.
3140
Thierry FOURNIER4dc71972017-01-28 08:33:08 +01003141 Note that :js:attr:`Map.sub` is also available for compatibility.
3142
3143.. js:attribute:: Map._dir
Thierry FOURNIER3def3932015-04-07 11:27:54 +02003144
3145 See the HAProxy configuration.txt file, chapter "Using ACLs and fetching
Ilya Shipitsin2075ca82020-03-06 23:22:22 +05003146 samples" and subchapter "ACL basics" to understand this pattern matching
Thierry FOURNIER3def3932015-04-07 11:27:54 +02003147 method.
3148
Thierry FOURNIER4dc71972017-01-28 08:33:08 +01003149 Note that :js:attr:`Map.dir` is also available for compatibility.
3150
3151.. js:attribute:: Map._dom
Thierry FOURNIER3def3932015-04-07 11:27:54 +02003152
3153 See the HAProxy configuration.txt file, chapter "Using ACLs and fetching
Ilya Shipitsin2075ca82020-03-06 23:22:22 +05003154 samples" and subchapter "ACL basics" to understand this pattern matching
Thierry FOURNIER3def3932015-04-07 11:27:54 +02003155 method.
3156
Thierry FOURNIER4dc71972017-01-28 08:33:08 +01003157 Note that :js:attr:`Map.dom` is also available for compatibility.
3158
3159.. js:attribute:: Map._end
Thierry FOURNIER3def3932015-04-07 11:27:54 +02003160
3161 See the HAProxy configuration.txt file, chapter "Using ACLs and fetching
Ilya Shipitsin2075ca82020-03-06 23:22:22 +05003162 samples" and subchapter "ACL basics" to understand this pattern matching
Thierry FOURNIER3def3932015-04-07 11:27:54 +02003163 method.
3164
Thierry FOURNIER4dc71972017-01-28 08:33:08 +01003165.. js:attribute:: Map._reg
Thierry FOURNIER3def3932015-04-07 11:27:54 +02003166
3167 See the HAProxy configuration.txt file, chapter "Using ACLs and fetching
Ilya Shipitsin2075ca82020-03-06 23:22:22 +05003168 samples" and subchapter "ACL basics" to understand this pattern matching
Thierry FOURNIER3def3932015-04-07 11:27:54 +02003169 method.
3170
Thierry FOURNIER4dc71972017-01-28 08:33:08 +01003171 Note that :js:attr:`Map.reg` is also available for compatibility.
3172
Thierry FOURNIER3def3932015-04-07 11:27:54 +02003173
3174.. js:function:: Map.new(file, method)
3175
3176 Creates and load a map.
3177
3178 :param string file: Is the file containing the map.
3179 :param integer method: Is the map pattern matching method. See the attributes
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02003180 of the Map class.
Thierry FOURNIER3def3932015-04-07 11:27:54 +02003181 :returns: a class Map object.
Thierry FOURNIER4dc71972017-01-28 08:33:08 +01003182 :see: The Map attributes: :js:attr:`Map._int`, :js:attr:`Map._ip`,
3183 :js:attr:`Map._str`, :js:attr:`Map._beg`, :js:attr:`Map._sub`,
3184 :js:attr:`Map._dir`, :js:attr:`Map._dom`, :js:attr:`Map._end` and
3185 :js:attr:`Map._reg`.
Thierry FOURNIER3def3932015-04-07 11:27:54 +02003186
3187.. js:function:: Map.lookup(map, str)
3188
3189 Perform a lookup in a map.
3190
3191 :param class_map map: Is the class Map object.
3192 :param string str: Is the string used as key.
3193 :returns: a string containing the result or nil if no match.
3194
3195.. js:function:: Map.slookup(map, str)
3196
3197 Perform a lookup in a map.
3198
3199 :param class_map map: Is the class Map object.
3200 :param string str: Is the string used as key.
3201 :returns: a string containing the result or empty string if no match.
3202
Thierry FOURNIERdc595002015-12-21 11:13:52 +01003203.. _applethttp_class:
3204
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02003205AppletHTTP class
Thierry FOURNIERdc595002015-12-21 11:13:52 +01003206================
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02003207
3208.. js:class:: AppletHTTP
3209
3210 This class is used with applets that requires the 'http' mode. The http applet
3211 can be registered with the *core.register_service()* function. They are used
3212 for processing an http request like a server in back of HAProxy.
3213
3214 This is an hello world sample code:
3215
3216.. code-block:: lua
Thierry FOURNIERdc595002015-12-21 11:13:52 +01003217
Pieter Baauw4d7f7662015-11-08 16:38:08 +01003218 core.register_service("hello-world", "http", function(applet)
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02003219 local response = "Hello World !"
3220 applet:set_status(200)
Pieter Baauw2dcb9bc2015-10-01 22:47:12 +02003221 applet:add_header("content-length", string.len(response))
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02003222 applet:add_header("content-type", "text/plain")
Pieter Baauw2dcb9bc2015-10-01 22:47:12 +02003223 applet:start_response()
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02003224 applet:send(response)
3225 end)
3226
Thierry FOURNIERdc595002015-12-21 11:13:52 +01003227.. js:attribute:: AppletHTTP.c
3228
3229 :returns: A :ref:`converters_class`
3230
3231 This attribute contains a Converters class object.
3232
3233.. js:attribute:: AppletHTTP.sc
3234
3235 :returns: A :ref:`converters_class`
3236
3237 This attribute contains a Converters class object. The
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02003238 functions of this object always return a string.
Thierry FOURNIERdc595002015-12-21 11:13:52 +01003239
3240.. js:attribute:: AppletHTTP.f
3241
3242 :returns: A :ref:`fetches_class`
3243
3244 This attribute contains a Fetches class object. Note that the
3245 applet execution place cannot access to a valid HAProxy core HTTP
Ilya Shipitsin2075ca82020-03-06 23:22:22 +05003246 transaction, so some sample fetches related to the HTTP dependent
Thierry FOURNIERdc595002015-12-21 11:13:52 +01003247 values (hdr, path, ...) are not available.
3248
3249.. js:attribute:: AppletHTTP.sf
3250
3251 :returns: A :ref:`fetches_class`
3252
3253 This attribute contains a Fetches class object. The functions of
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02003254 this object always return a string. Note that the applet
Thierry FOURNIERdc595002015-12-21 11:13:52 +01003255 execution place cannot access to a valid HAProxy core HTTP
Ilya Shipitsin2075ca82020-03-06 23:22:22 +05003256 transaction, so some sample fetches related to the HTTP dependent
Thierry FOURNIERdc595002015-12-21 11:13:52 +01003257 values (hdr, path, ...) are not available.
3258
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01003259.. js:attribute:: AppletHTTP.method
Thierry FOURNIERdc595002015-12-21 11:13:52 +01003260
3261 :returns: string
3262
3263 The attribute method returns a string containing the HTTP
3264 method.
3265
3266.. js:attribute:: AppletHTTP.version
3267
3268 :returns: string
3269
3270 The attribute version, returns a string containing the HTTP
3271 request version.
3272
3273.. js:attribute:: AppletHTTP.path
3274
3275 :returns: string
3276
3277 The attribute path returns a string containing the HTTP
3278 request path.
3279
3280.. js:attribute:: AppletHTTP.qs
3281
3282 :returns: string
3283
3284 The attribute qs returns a string containing the HTTP
3285 request query string.
3286
3287.. js:attribute:: AppletHTTP.length
3288
3289 :returns: integer
3290
3291 The attribute length returns an integer containing the HTTP
3292 body length.
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02003293
Thierry FOURNIER841475e2015-12-11 17:10:09 +01003294.. js:attribute:: AppletHTTP.headers
3295
Patrick Hemmerc6a1d712018-05-01 21:30:41 -04003296 :returns: table
Thierry FOURNIERdc595002015-12-21 11:13:52 +01003297
Patrick Hemmerc6a1d712018-05-01 21:30:41 -04003298 The attribute headers returns a table containing the HTTP
Thierry FOURNIERdc595002015-12-21 11:13:52 +01003299 headers. The header names are always in lower case. As the header name can be
3300 encountered more than once in each request, the value is indexed with 0 as
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02003301 first index value. The table has this form:
Thierry FOURNIERdc595002015-12-21 11:13:52 +01003302
3303.. code-block:: lua
3304
3305 AppletHTTP.headers['<header-name>'][<header-index>] = "<header-value>"
3306
3307 AppletHTTP.headers["host"][0] = "www.test.com"
3308 AppletHTTP.headers["accept"][0] = "audio/basic q=1"
3309 AppletHTTP.headers["accept"][1] = "audio/*, q=0.2"
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01003310 AppletHTTP.headers["accept"][2] = "*/*, q=0.1"
Thierry FOURNIERdc595002015-12-21 11:13:52 +01003311..
3312
Robin H. Johnson52f5db22017-01-01 13:10:52 -08003313.. js:function:: AppletHTTP.set_status(applet, code [, reason])
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02003314
3315 This function sets the HTTP status code for the response. The allowed code are
3316 from 100 to 599.
3317
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01003318 :param class_AppletHTTP applet: An :ref:`applethttp_class`
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02003319 :param integer code: the status code returned to the client.
Robin H. Johnson52f5db22017-01-01 13:10:52 -08003320 :param string reason: the status reason returned to the client (optional).
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02003321
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01003322.. js:function:: AppletHTTP.add_header(applet, name, value)
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02003323
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02003324 This function adds a header in the response. Duplicated headers are not
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02003325 collapsed. The special header *content-length* is used to determinate the
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02003326 response length. If it does not exist, a *transfer-encoding: chunked* is set,
3327 and all the write from the function *AppletHTTP:send()* become a chunk.
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02003328
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01003329 :param class_AppletHTTP applet: An :ref:`applethttp_class`
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02003330 :param string name: the header name
3331 :param string value: the header value
3332
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01003333.. js:function:: AppletHTTP.start_response(applet)
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02003334
3335 This function indicates to the HTTP engine that it can process and send the
3336 response headers. After this called we cannot add headers to the response; We
3337 cannot use the *AppletHTTP:send()* function if the
3338 *AppletHTTP:start_response()* is not called.
3339
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01003340 :param class_AppletHTTP applet: An :ref:`applethttp_class`
3341
3342.. js:function:: AppletHTTP.getline(applet)
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02003343
3344 This function returns a string containing one line from the http body. If the
3345 data returned doesn't contains a final '\\n' its assumed than its the last
3346 available data before the end of stream.
3347
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01003348 :param class_AppletHTTP applet: An :ref:`applethttp_class`
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02003349 :returns: a string. The string can be empty if we reach the end of the stream.
3350
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01003351.. js:function:: AppletHTTP.receive(applet, [size])
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02003352
3353 Reads data from the HTTP body, according to the specified read *size*. If the
3354 *size* is missing, the function tries to read all the content of the stream
3355 until the end. If the *size* is bigger than the http body, it returns the
Bertrand Jacquin874a35c2018-09-10 21:26:07 +01003356 amount of data available.
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02003357
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01003358 :param class_AppletHTTP applet: An :ref:`applethttp_class`
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02003359 :param integer size: the required read size.
Ilya Shipitsin11057a32020-06-21 21:18:27 +05003360 :returns: always return a string,the string can be empty is the connection is
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02003361 closed.
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02003362
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01003363.. js:function:: AppletHTTP.send(applet, msg)
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02003364
3365 Send the message *msg* on the http request body.
3366
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01003367 :param class_AppletHTTP applet: An :ref:`applethttp_class`
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02003368 :param string msg: the message to send.
3369
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01003370.. js:function:: AppletHTTP.get_priv(applet)
3371
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01003372 Return Lua data stored in the current transaction. If no data are stored,
3373 it returns a nil value.
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01003374
3375 :param class_AppletHTTP applet: An :ref:`applethttp_class`
Bertrand Jacquin874a35c2018-09-10 21:26:07 +01003376 :returns: the opaque data previously stored, or nil if nothing is
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02003377 available.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01003378 :see: :js:func:`AppletHTTP.set_priv`
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01003379
3380.. js:function:: AppletHTTP.set_priv(applet, data)
3381
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02003382 Store any data in the current HAProxy transaction. This action replaces the
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01003383 old stored data.
3384
3385 :param class_AppletHTTP applet: An :ref:`applethttp_class`
3386 :param opaque data: The data which is stored in the transaction.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01003387 :see: :js:func:`AppletHTTP.get_priv`
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01003388
Tim Duesterhus4e172c92020-05-19 13:49:42 +02003389.. js:function:: AppletHTTP.set_var(applet, var, value[, ifexist])
Thierry FOURNIER / OZON.IOc1edafe2016-12-12 16:25:30 +01003390
3391 Converts a Lua type in a HAProxy type and store it in a variable <var>.
3392
3393 :param class_AppletHTTP applet: An :ref:`applethttp_class`
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02003394 :param string var: The variable name according with the HAProxy variable
3395 syntax.
3396 :param type value: The value associated to the variable. The type ca be string
3397 or integer.
3398 :param boolean ifexist: If this parameter is set to true the variable will
3399 only be set if it was defined elsewhere (i.e. used within the configuration).
3400 For global variables (using the "proc" scope), they will only be updated and
3401 never created. It is highly recommended to always set this to true.
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02003402
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01003403 :see: :js:func:`AppletHTTP.unset_var`
3404 :see: :js:func:`AppletHTTP.get_var`
Thierry FOURNIER / OZON.IOc1edafe2016-12-12 16:25:30 +01003405
3406.. js:function:: AppletHTTP.unset_var(applet, var)
3407
3408 Unset the variable <var>.
3409
3410 :param class_AppletHTTP applet: An :ref:`applethttp_class`
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02003411 :param string var: The variable name according with the HAProxy variable
3412 syntax.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01003413 :see: :js:func:`AppletHTTP.set_var`
3414 :see: :js:func:`AppletHTTP.get_var`
Thierry FOURNIER / OZON.IOc1edafe2016-12-12 16:25:30 +01003415
3416.. js:function:: AppletHTTP.get_var(applet, var)
3417
3418 Returns data stored in the variable <var> converter in Lua type.
3419
3420 :param class_AppletHTTP applet: An :ref:`applethttp_class`
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02003421 :param string var: The variable name according with the HAProxy variable
3422 syntax.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01003423 :see: :js:func:`AppletHTTP.set_var`
3424 :see: :js:func:`AppletHTTP.unset_var`
Thierry FOURNIER / OZON.IOc1edafe2016-12-12 16:25:30 +01003425
Thierry FOURNIERdc595002015-12-21 11:13:52 +01003426.. _applettcp_class:
3427
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02003428AppletTCP class
3429===============
3430
3431.. js:class:: AppletTCP
3432
3433 This class is used with applets that requires the 'tcp' mode. The tcp applet
3434 can be registered with the *core.register_service()* function. They are used
3435 for processing a tcp stream like a server in back of HAProxy.
3436
Thierry FOURNIERdc595002015-12-21 11:13:52 +01003437.. js:attribute:: AppletTCP.c
3438
3439 :returns: A :ref:`converters_class`
3440
3441 This attribute contains a Converters class object.
3442
3443.. js:attribute:: AppletTCP.sc
3444
3445 :returns: A :ref:`converters_class`
3446
3447 This attribute contains a Converters class object. The
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02003448 functions of this object always return a string.
Thierry FOURNIERdc595002015-12-21 11:13:52 +01003449
3450.. js:attribute:: AppletTCP.f
3451
3452 :returns: A :ref:`fetches_class`
3453
3454 This attribute contains a Fetches class object.
3455
3456.. js:attribute:: AppletTCP.sf
3457
3458 :returns: A :ref:`fetches_class`
3459
3460 This attribute contains a Fetches class object.
3461
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01003462.. js:function:: AppletTCP.getline(applet)
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02003463
3464 This function returns a string containing one line from the stream. If the
3465 data returned doesn't contains a final '\\n' its assumed than its the last
3466 available data before the end of stream.
3467
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01003468 :param class_AppletTCP applet: An :ref:`applettcp_class`
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02003469 :returns: a string. The string can be empty if we reach the end of the stream.
3470
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01003471.. js:function:: AppletTCP.receive(applet, [size])
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02003472
3473 Reads data from the TCP stream, according to the specified read *size*. If the
3474 *size* is missing, the function tries to read all the content of the stream
3475 until the end.
3476
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01003477 :param class_AppletTCP applet: An :ref:`applettcp_class`
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02003478 :param integer size: the required read size.
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02003479 :returns: always return a string, the string can be empty if the connection is
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02003480 closed.
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02003481
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01003482.. js:function:: AppletTCP.send(appletmsg)
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02003483
3484 Send the message on the stream.
3485
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01003486 :param class_AppletTCP applet: An :ref:`applettcp_class`
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02003487 :param string msg: the message to send.
3488
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01003489.. js:function:: AppletTCP.get_priv(applet)
3490
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01003491 Return Lua data stored in the current transaction. If no data are stored,
3492 it returns a nil value.
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01003493
3494 :param class_AppletTCP applet: An :ref:`applettcp_class`
Bertrand Jacquin874a35c2018-09-10 21:26:07 +01003495 :returns: the opaque data previously stored, or nil if nothing is
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02003496 available.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01003497 :see: :js:func:`AppletTCP.set_priv`
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01003498
3499.. js:function:: AppletTCP.set_priv(applet, data)
3500
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02003501 Store any data in the current HAProxy transaction. This action replaces the
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01003502 old stored data.
3503
3504 :param class_AppletTCP applet: An :ref:`applettcp_class`
3505 :param opaque data: The data which is stored in the transaction.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01003506 :see: :js:func:`AppletTCP.get_priv`
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01003507
Tim Duesterhus4e172c92020-05-19 13:49:42 +02003508.. js:function:: AppletTCP.set_var(applet, var, value[, ifexist])
Thierry FOURNIER / OZON.IOc1edafe2016-12-12 16:25:30 +01003509
3510 Converts a Lua type in a HAProxy type and stores it in a variable <var>.
3511
3512 :param class_AppletTCP applet: An :ref:`applettcp_class`
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02003513 :param string var: The variable name according with the HAProxy variable
3514 syntax.
3515 :param type value: The value associated to the variable. The type can be
3516 string or integer.
3517 :param boolean ifexist: If this parameter is set to true the variable will
3518 only be set if it was defined elsewhere (i.e. used within the configuration).
3519 For global variables (using the "proc" scope), they will only be updated and
3520 never created. It is highly recommended to always set this to true.
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02003521
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01003522 :see: :js:func:`AppletTCP.unset_var`
3523 :see: :js:func:`AppletTCP.get_var`
Thierry FOURNIER / OZON.IOc1edafe2016-12-12 16:25:30 +01003524
3525.. js:function:: AppletTCP.unset_var(applet, var)
3526
3527 Unsets the variable <var>.
3528
3529 :param class_AppletTCP applet: An :ref:`applettcp_class`
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02003530 :param string var: The variable name according with the HAProxy variable
3531 syntax.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01003532 :see: :js:func:`AppletTCP.unset_var`
3533 :see: :js:func:`AppletTCP.set_var`
Thierry FOURNIER / OZON.IOc1edafe2016-12-12 16:25:30 +01003534
3535.. js:function:: AppletTCP.get_var(applet, var)
3536
3537 Returns data stored in the variable <var> converter in Lua type.
3538
3539 :param class_AppletTCP applet: An :ref:`applettcp_class`
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02003540 :param string var: The variable name according with the HAProxy variable
3541 syntax.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01003542 :see: :js:func:`AppletTCP.unset_var`
3543 :see: :js:func:`AppletTCP.set_var`
Thierry FOURNIER / OZON.IOc1edafe2016-12-12 16:25:30 +01003544
Adis Nezirovic8878f8e2018-07-13 12:18:33 +02003545StickTable class
3546================
3547
3548.. js:class:: StickTable
3549
3550 **context**: task, action, sample-fetch
3551
3552 This class can be used to access the HAProxy stick tables from Lua.
3553
3554.. js:function:: StickTable.info()
3555
3556 Returns stick table attributes as a Lua table. See HAProxy documentation for
Ilya Shipitsin2272d8a2020-12-21 01:22:40 +05003557 "stick-table" for canonical info, or check out example below.
Adis Nezirovic8878f8e2018-07-13 12:18:33 +02003558
3559 :returns: Lua table
3560
3561 Assume our table has IPv4 key and gpc0 and conn_rate "columns":
3562
3563.. code-block:: lua
3564
3565 {
3566 expire=<int>, # Value in ms
3567 size=<int>, # Maximum table size
3568 used=<int>, # Actual number of entries in table
3569 data={ # Data columns, with types as key, and periods as values
3570 (-1 if type is not rate counter)
3571 conn_rate=<int>,
3572 gpc0=-1
3573 },
3574 length=<int>, # max string length for string table keys, key length
3575 # otherwise
3576 nopurge=<boolean>, # purge oldest entries when table is full
3577 type="ip" # can be "ip", "ipv6", "integer", "string", "binary"
3578 }
3579
3580.. js:function:: StickTable.lookup(key)
3581
3582 Returns stick table entry for given <key>
3583
3584 :param string key: Stick table key (IP addresses and strings are supported)
3585 :returns: Lua table
3586
3587.. js:function:: StickTable.dump([filter])
3588
3589 Returns all entries in stick table. An optional filter can be used
3590 to extract entries with specific data values. Filter is a table with valid
3591 comparison operators as keys followed by data type name and value pairs.
3592 Check out the HAProxy docs for "show table" for more details. For the
3593 reference, the supported operators are:
Aurelien DARRAGON21f7ebb2023-03-13 19:49:31 +01003594
Adis Nezirovic8878f8e2018-07-13 12:18:33 +02003595 "eq", "ne", "le", "lt", "ge", "gt"
3596
3597 For large tables, execution of this function can take a long time (for
3598 HAProxy standards). That's also true when filter is used, so take care and
3599 measure the impact.
3600
3601 :param table filter: Stick table filter
3602 :returns: Stick table entries (table)
3603
3604 See below for example filter, which contains 4 entries (or comparisons).
3605 (Maximum number of filter entries is 4, defined in the source code)
3606
3607.. code-block:: lua
3608
3609 local filter = {
3610 {"gpc0", "gt", 30}, {"gpc1", "gt", 20}}, {"conn_rate", "le", 10}
3611 }
3612
Christopher Faulet0f3c8902020-01-31 18:57:12 +01003613.. _action_class:
3614
3615Action class
3616=============
3617
3618.. js:class:: Act
3619
3620 **context**: action
3621
3622 This class contains all return codes an action may return. It is the lua
3623 equivalent to HAProxy "ACT_RET_*" code.
3624
3625.. code-block:: lua
3626
3627 core.register_action("deny", { "http-req" }, function (txn)
3628 return act.DENY
3629 end)
3630..
3631.. js:attribute:: act.CONTINUE
3632
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02003633 This attribute is an integer (0). It instructs HAProxy to continue the
3634 current ruleset processing on the message. It is the default return code
3635 for a lua action.
Christopher Faulet0f3c8902020-01-31 18:57:12 +01003636
3637 :returns: integer
3638
3639.. js:attribute:: act.STOP
3640
3641 This attribute is an integer (1). It instructs HAProxy to stop the current
3642 ruleset processing on the message.
3643
3644.. js:attribute:: act.YIELD
3645
3646 This attribute is an integer (2). It instructs HAProxy to temporarily pause
3647 the message processing. It will be resumed later on the same rule. The
3648 corresponding lua script is re-executed for the start.
3649
3650.. js:attribute:: act.ERROR
3651
3652 This attribute is an integer (3). It triggers an internal errors The message
3653 processing is stopped and the transaction is terminated. For HTTP streams, an
3654 HTTP 500 error is returned to the client.
3655
3656 :returns: integer
3657
3658.. js:attribute:: act.DONE
3659
3660 This attribute is an integer (4). It instructs HAProxy to stop the message
3661 processing.
3662
3663 :returns: integer
3664
3665.. js:attribute:: act.DENY
3666
3667 This attribute is an integer (5). It denies the current message. The message
3668 processing is stopped and the transaction is terminated. For HTTP streams, an
3669 HTTP 403 error is returned to the client if the deny is returned during the
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02003670 request analysis. During the response analysis, a HTTP 502 error is returned
Christopher Faulet0f3c8902020-01-31 18:57:12 +01003671 and the server response is discarded.
3672
3673 :returns: integer
3674
3675.. js:attribute:: act.ABORT
3676
3677 This attribute is an integer (6). It aborts the current message. The message
3678 processing is stopped and the transaction is terminated. For HTTP streams,
Willy Tarreau714f3452021-05-09 06:47:26 +02003679 HAProxy assumes a response was already sent to the client. From the Lua
Christopher Faulet0f3c8902020-01-31 18:57:12 +01003680 actions point of view, when this code is used, the transaction is terminated
3681 with no reply.
3682
3683 :returns: integer
3684
3685.. js:attribute:: act.INVALID
3686
3687 This attribute is an integer (7). It triggers an internal errors. The message
3688 processing is stopped and the transaction is terminated. For HTTP streams, an
3689 HTTP 400 error is returned to the client if the error is returned during the
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02003690 request analysis. During the response analysis, a HTTP 502 error is returned
Christopher Faulet0f3c8902020-01-31 18:57:12 +01003691 and the server response is discarded.
3692
3693 :returns: integer
Adis Nezirovic8878f8e2018-07-13 12:18:33 +02003694
Christopher Faulet2c2c2e32020-01-31 19:07:52 +01003695.. js:function:: act:wake_time(milliseconds)
3696
3697 **context**: action
3698
3699 Set the script pause timeout to the specified time, defined in
3700 milliseconds.
3701
3702 :param integer milliseconds: the required milliseconds.
3703
3704 This function may be used when a lua action returns `act.YIELD`, to force its
3705 wake-up at most after the specified number of milliseconds.
3706
Christopher Faulet5a2c6612021-08-15 20:35:25 +02003707.. _filter_class:
3708
3709Filter class
3710=============
3711
3712.. js:class:: filter
3713
3714 **context**: filter
3715
3716 This class contains return codes some filter callback functions may return. It
3717 also contains configuration flags and some helper functions. To understand how
3718 the filter API works, see `doc/internal/filters.txt` documentation.
3719
3720.. js:attribute:: filter.CONTINUE
3721
3722 This attribute is an integer (1). It may be returned by some filter callback
3723 functions to instruct this filtering step is finished for this filter.
3724
3725.. js:attribute:: filter.WAIT
3726
3727 This attribute is an integer (0). It may be returned by some filter callback
3728 functions to instruct the filtering must be paused, waiting for more data or
3729 for an external event depending on this filter.
3730
3731.. js:attribute:: filter.ERROR
3732
3733 This attribute is an integer (-1). It may be returned by some filter callback
3734 functions to trigger an error.
3735
3736.. js:attribute:: filter.FLT_CFG_FL_HTX
3737
3738 This attribute is a flag corresponding to the filter flag FLT_CFG_FL_HTX. When
3739 it is set for a filter, it means the filter is able to filter HTTP streams.
3740
3741.. js:function:: filter.register_data_filter(chn)
3742
3743 **context**: filter
3744
3745 Enable the data filtering on the channel **chn** for the current filter. It
Ilya Shipitsinff0f2782021-08-22 22:18:07 +05003746 may be called at any time from any callback functions proceeding the data
Christopher Faulet5a2c6612021-08-15 20:35:25 +02003747 analysis.
3748
3749 :param class_Channel chn: A :ref:`channel_class`.
3750
3751.. js:function:: filter.unregister_data_filter(chn)
3752
3753 **context**: filter
3754
3755 Disable the data filtering on the channel **chn** for the current filter. It
3756 may be called at any time from any callback functions.
3757
3758 :param class_Channel chn: A :ref:`channel_class`.
3759
3760.. js:function:: filter.wake_time(milliseconds)
3761
3762 **context**: filter
3763
3764 Set the script pause timeout to the specified time, defined in
3765 milliseconds.
3766
3767 :param integer milliseconds: the required milliseconds.
3768
3769 This function may be used from any lua filter callback function to force its
3770 wake-up at most after the specified number of milliseconds. Especially, when
3771 `filter.CONTINUE` is returned.
3772
3773
3774A filters is declared using :js:func:`core.register_filter()` function. The
3775provided class will be used to instantiate filters. It may define following
3776attributes:
3777
3778* id: The filter identifier. It is a string that identifies the filter and is
3779 optional.
3780
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02003781* flags: The filter flags. Only :js:attr:`filter.FLT_CFG_FL_HTX` may be set
3782 for now.
Christopher Faulet5a2c6612021-08-15 20:35:25 +02003783
3784Such filter class must also define all required callback functions in the
3785following list. Note that :js:func:`Filter.new()` must be defined otherwise the
Ilya Shipitsinff0f2782021-08-22 22:18:07 +05003786filter is ignored. Others are optional.
Christopher Faulet5a2c6612021-08-15 20:35:25 +02003787
3788* .. js:function:: FILTER.new()
3789
3790 Called to instantiate a new filter. This function must be defined.
3791
3792 :returns: a Lua object that will be used as filter instance for the current
3793 stream.
3794
3795* .. js:function:: FILTER.start_analyze(flt, txn, chn)
3796
3797 Called when the analysis starts on the channel **chn**.
3798
3799* .. js:function:: FILTER.end_analyze(flt, txn, chn)
3800
3801 Called when the analysis ends on the channel **chn**.
3802
3803* .. js:function:: FILTER.http_headers(flt, txn, http_msg)
3804
3805 Called just before the HTTP payload analysis and after any processing on the
3806 HTTP message **http_msg**. This callback functions is only called for HTTP
3807 streams.
3808
3809* .. js:function:: FILTER.http_payload(flt, txn, http_msg)
3810
3811 Called during the HTTP payload analysis on the HTTP message **http_msg**. This
3812 callback functions is only called for HTTP streams.
3813
3814* .. js:function:: FILTER.http_end(flt, txn, http_msg)
3815
3816 Called after the HTTP payload analysis on the HTTP message **http_msg**. This
3817 callback functions is only called for HTTP streams.
3818
3819* .. js:function:: FILTER.tcp_payload(flt, txn, chn)
3820
3821 Called during the TCP payload analysis on the channel **chn**.
3822
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02003823Here is a full example:
Christopher Faulet5a2c6612021-08-15 20:35:25 +02003824
3825.. code-block:: lua
3826
3827 Trace = {}
3828 Trace.id = "Lua trace filter"
3829 Trace.flags = filter.FLT_CFG_FL_HTX;
3830 Trace.__index = Trace
3831
3832 function Trace:new()
3833 local trace = {}
3834 setmetatable(trace, Trace)
3835 trace.req_len = 0
3836 trace.res_len = 0
3837 return trace
3838 end
3839
3840 function Trace:start_analyze(txn, chn)
3841 if chn:is_resp() then
3842 print("Start response analysis")
3843 else
3844 print("Start request analysis")
3845 end
3846 filter.register_data_filter(self, chn)
3847 end
3848
3849 function Trace:end_analyze(txn, chn)
3850 if chn:is_resp() then
3851 print("End response analysis: "..self.res_len.." bytes filtered")
3852 else
3853 print("End request analysis: "..self.req_len.." bytes filtered")
3854 end
3855 end
3856
3857 function Trace:http_headers(txn, http_msg)
3858 stline = http_msg:get_stline()
3859 if http_msg.channel:is_resp() then
3860 print("response:")
3861 print(stline.version.." "..stline.code.." "..stline.reason)
3862 else
3863 print("request:")
3864 print(stline.method.." "..stline.uri.." "..stline.version)
3865 end
3866
3867 for n, hdrs in pairs(http_msg:get_headers()) do
3868 for i,v in pairs(hdrs) do
3869 print(n..": "..v)
3870 end
3871 end
3872 return filter.CONTINUE
3873 end
3874
3875 function Trace:http_payload(txn, http_msg)
3876 body = http_msg:body(-20000)
3877 if http_msg.channel:is_resp() then
3878 self.res_len = self.res_len + body:len()
3879 else
3880 self.req_len = self.req_len + body:len()
3881 end
3882 end
3883
3884 core.register_filter("trace", Trace, function(trace, args)
3885 return trace
3886 end)
3887
3888..
3889
3890.. _httpmessage_class:
3891
3892HTTPMessage class
3893===================
3894
3895.. js:class:: HTTPMessage
3896
3897 **context**: filter
3898
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02003899 This class contains all functions to manipulate a HTTP message. For now, this
Christopher Faulet5a2c6612021-08-15 20:35:25 +02003900 class is only available from a filter context.
3901
3902.. js:function:: HTTPMessage.add_header(http_msg, name, value)
3903
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02003904 Appends a HTTP header field in the HTTP message **http_msg** whose name is
Christopher Faulet5a2c6612021-08-15 20:35:25 +02003905 specified in **name** and whose value is defined in **value**.
3906
3907 :param class_httpmessage http_msg: The manipulated HTTP message.
3908 :param string name: The header name.
3909 :param string value: The header value.
3910
3911.. js:function:: HTTPMessage.append(http_msg, string)
3912
3913 This function copies the string **string** at the end of incoming data of the
3914 HTTP message **http_msg**. The function returns the copied length on success
3915 or -1 if data cannot be copied.
3916
3917 Same that :js:func:`HTTPMessage.insert(http_msg, string, http_msg:input())`.
3918
3919 :param class_httpmessage http_msg: The manipulated HTTP message.
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02003920 :param string string: The data to copy at the end of incoming data.
Christopher Faulet5a2c6612021-08-15 20:35:25 +02003921 :returns: an integer containing the amount of bytes copied or -1.
3922
3923.. js:function:: HTTPMessage.body(http_msgl[, offset[, length]])
3924
3925 This function returns **length** bytes of incoming data from the HTTP message
3926 **http_msg**, starting at the offset **offset**. The data are not removed from
3927 the buffer.
3928
3929 By default, if no length is provided, all incoming data found, starting at the
3930 given offset, are returned. If **length** is set to -1, the function tries to
3931 retrieve a maximum of data. Because it is called in the filter context, it
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02003932 never yield. Not providing an offset is the same as setting it to 0. A
Ilya Shipitsinff0f2782021-08-22 22:18:07 +05003933 positive offset is relative to the beginning of incoming data of the
Christopher Faulet5a2c6612021-08-15 20:35:25 +02003934 http_message buffer while negative offset is relative to their end.
3935
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02003936 If there is no incoming data and the HTTP message can't receive more data,
3937 a 'nil' value is returned.
Christopher Faulet5a2c6612021-08-15 20:35:25 +02003938
3939 :param class_httpmessage http_msg: The manipulated HTTP message.
3940 :param integer offset: *optional* The offset in incoming data to start to get
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02003941 data. 0 by default. May be negative to be relative to the end of incoming
3942 data.
3943 :param integer length: *optional* The expected length of data to retrieve.
3944 All incoming data by default. May be set to -1 to get a maximum of data.
Christopher Faulet5a2c6612021-08-15 20:35:25 +02003945 :returns: a string containing the data found or nil.
3946
3947.. js:function:: HTTPMessage.eom(http_msg)
3948
3949 This function returns true if the end of message is reached for the HTTP
3950 message **http_msg**.
3951
3952 :param class_httpmessage http_msg: The manipulated HTTP message.
3953 :returns: an integer containing the amount of available bytes.
3954
3955.. js:function:: HTTPMessage.del_header(http_msg, name)
3956
3957 Removes all HTTP header fields in the HTTP message **http_msg** whose name is
3958 specified in **name**.
3959
3960 :param class_httpmessage http_msg: The manipulated http message.
3961 :param string name: The header name.
3962
3963.. js:function:: HTTPMessage.get_headers(http_msg)
3964
3965 Returns a table containing all the headers of the HTTP message **http_msg**.
3966
3967 :param class_httpmessage http_msg: The manipulated http message.
3968 :returns: table of headers.
3969
3970 This is the form of the returned table:
3971
3972.. code-block:: lua
3973
3974 http_msg:get_headers()['<header-name>'][<header-index>] = "<header-value>"
3975
3976 local hdr = http_msg:get_headers()
3977 hdr["host"][0] = "www.test.com"
3978 hdr["accept"][0] = "audio/basic q=1"
3979 hdr["accept"][1] = "audio/*, q=0.2"
3980 hdr["accept"][2] = "*.*, q=0.1"
3981..
3982
3983.. js:function:: HTTPMessage.get_stline(http_msg)
3984
3985 Returns a table containing the start-line of the HTTP message **http_msg**.
3986
3987 :param class_httpmessage http_msg: The manipulated http message.
3988 :returns: the start-line.
3989
3990 This is the form of the returned table:
3991
3992.. code-block:: lua
3993
3994 -- for the request :
3995 {"method" = string, "uri" = string, "version" = string}
3996
3997 -- for the response:
3998 {"version" = string, "code" = string, "reason" = string}
3999..
4000
4001.. js:function:: HTTPMessage.forward(http_msg, length)
4002
4003 This function forwards **length** bytes of data from the HTTP message
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02004004 **http_msg**. Because it is called in the filter context, it never yields. Only
Christopher Faulet5a2c6612021-08-15 20:35:25 +02004005 available incoming data may be forwarded, event if the requested length
4006 exceeds the available amount of incoming data. It returns the amount of data
4007 forwarded.
4008
4009 :param class_httpmessage http_msg: The manipulated HTTP message.
4010 :param integer int: The amount of data to forward.
4011
4012.. js:function:: HTTPMessage.input(http_msg)
4013
4014 This function returns the length of incoming data in the HTTP message
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02004015 **http_msg** from the filter point of view.
Christopher Faulet5a2c6612021-08-15 20:35:25 +02004016
4017 :param class_httpmessage http_msg: The manipulated HTTP message.
4018 :returns: an integer containing the amount of available bytes.
4019
4020.. js:function:: HTTPMessage.insert(http_msg, string[, offset])
4021
4022 This function copies the string **string** at the offset **offset** in
4023 incoming data of the HTTP message **http_msg**. The function returns the
4024 copied length on success or -1 if data cannot be copied.
4025
4026 By default, if no offset is provided, the string is copied in front of
Ilya Shipitsinff0f2782021-08-22 22:18:07 +05004027 incoming data. A positive offset is relative to the beginning of incoming data
Christopher Faulet5a2c6612021-08-15 20:35:25 +02004028 of the HTTP message while negative offset is relative to their end.
4029
4030 :param class_httpmessage http_msg: The manipulated HTTP message.
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02004031 :param string string: The data to copy into incoming data.
4032 :param integer offset: *optional* The offset in incoming data where to copy
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02004033 data. 0 by default. May be negative to be relative to the end of incoming
4034 data.
Christopher Faulet5a2c6612021-08-15 20:35:25 +02004035 :returns: an integer containing the amount of bytes copied or -1.
4036
4037.. js:function:: HTTPMessage.is_full(http_msg)
4038
4039 This function returns true if the HTTP message **http_msg** is full.
4040
4041 :param class_httpmessage http_msg: The manipulated HTTP message.
4042 :returns: a boolean
4043
4044.. js:function:: HTTPMessage.is_resp(http_msg)
4045
4046 This function returns true if the HTTP message **http_msg** is the response
4047 one.
4048
4049 :param class_httpmessage http_msg: The manipulated HTTP message.
4050 :returns: a boolean
4051
4052.. js:function:: HTTPMessage.may_recv(http_msg)
4053
4054 This function returns true if the HTTP message **http_msg** may still receive
4055 data.
4056
4057 :param class_httpmessage http_msg: The manipulated HTTP message.
4058 :returns: a boolean
4059
4060.. js:function:: HTTPMessage.output(http_msg)
4061
4062 This function returns the length of outgoing data of the HTTP message
4063 **http_msg**.
4064
4065 :param class_httpmessage http_msg: The manipulated HTTP message.
4066 :returns: an integer containing the amount of available bytes.
4067
4068.. js:function:: HTTPMessage.prepend(http_msg, string)
4069
4070 This function copies the string **string** in front of incoming data of the
4071 HTTP message **http_msg**. The function returns the copied length on success
4072 or -1 if data cannot be copied.
4073
4074 Same that :js:func:`HTTPMessage.insert(http_msg, string, 0)`.
4075
4076 :param class_httpmessage http_msg: The manipulated HTTP message.
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02004077 :param string string: The data to copy in front of incoming data.
Christopher Faulet5a2c6612021-08-15 20:35:25 +02004078 :returns: an integer containing the amount of bytes copied or -1.
4079
4080.. js:function:: HTTPMessage.remove(http_msg[, offset[, length]])
4081
4082 This function removes **length** bytes of incoming data of the HTTP message
4083 **http_msg**, starting at offset **offset**. This function returns number of
4084 bytes removed on success.
4085
4086 By default, if no length is provided, all incoming data, starting at the given
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02004087 offset, are removed. Not providing an offset is the same that setting it
Ilya Shipitsinff0f2782021-08-22 22:18:07 +05004088 to 0. A positive offset is relative to the beginning of incoming data of the
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02004089 HTTP message while negative offset is relative to the end.
Christopher Faulet5a2c6612021-08-15 20:35:25 +02004090
4091 :param class_httpmessage http_msg: The manipulated HTTP message.
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02004092 :param integer offset: *optional* The offset in incoming data where to start
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02004093 to remove data. 0 by default. May be negative to be relative to the end of
4094 incoming data.
Christopher Faulet5a2c6612021-08-15 20:35:25 +02004095 :param integer length: *optional* The length of data to remove. All incoming
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02004096 data by default.
Christopher Faulet5a2c6612021-08-15 20:35:25 +02004097 :returns: an integer containing the amount of bytes removed.
4098
4099.. js:function:: HTTPMessage.rep_header(http_msg, name, regex, replace)
4100
4101 Matches the regular expression in all occurrences of header field **name**
4102 according to regex **regex**, and replaces them with the string **replace**.
4103 The replacement value can contain back references like \1, \2, ... This
4104 function acts on whole header lines, regardless of the number of values they
4105 may contain.
4106
4107 :param class_httpmessage http_msg: The manipulated HTTP message.
4108 :param string name: The header name.
4109 :param string regex: The match regular expression.
4110 :param string replace: The replacement value.
4111
4112.. js:function:: HTTPMessage.rep_value(http_msg, name, regex, replace)
4113
4114 Matches the regular expression on every comma-delimited value of header field
4115 **name** according to regex **regex**, and replaces them with the string
4116 **replace**. The replacement value can contain back references like \1, \2,
4117 ...
4118
4119 :param class_httpmessage http_msg: The manipulated HTTP message.
4120 :param string name: The header name.
4121 :param string regex: The match regular expression.
4122 :param string replace: The replacement value.
4123
4124.. js:function:: HTTPMessage.send(http_msg, string)
4125
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02004126 This function requires immediate send of the string **string**. It means the
Ilya Shipitsinff0f2782021-08-22 22:18:07 +05004127 string is copied at the beginning of incoming data of the HTTP message
Christopher Faulet5a2c6612021-08-15 20:35:25 +02004128 **http_msg** and immediately forwarded. Because it is called in the filter
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02004129 context, it never yields.
Christopher Faulet5a2c6612021-08-15 20:35:25 +02004130
4131 :param class_httpmessage http_msg: The manipulated HTTP message.
4132 :param string string: The data to send.
4133 :returns: an integer containing the amount of bytes copied or -1.
4134
4135.. js:function:: HTTPMessage.set(http_msg, string[, offset[, length]])
4136
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02004137 This function replaces **length** bytes of incoming data of the HTTP message
Christopher Faulet5a2c6612021-08-15 20:35:25 +02004138 **http_msg**, starting at offset **offset**, by the string **string**. The
4139 function returns the copied length on success or -1 if data cannot be copied.
4140
4141 By default, if no length is provided, all incoming data, starting at the given
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02004142 offset, are replaced. Not providing an offset is the same as setting it
Ilya Shipitsinff0f2782021-08-22 22:18:07 +05004143 to 0. A positive offset is relative to the beginning of incoming data of the
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02004144 HTTP message while negative offset is relative to the end.
Christopher Faulet5a2c6612021-08-15 20:35:25 +02004145
4146 :param class_httpmessage http_msg: The manipulated HTTP message.
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02004147 :param string string: The data to copy into incoming data.
4148 :param integer offset: *optional* The offset in incoming data where to start
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02004149 the data replacement. 0 by default. May be negative to be relative to the
4150 end of incoming data.
Christopher Faulet5a2c6612021-08-15 20:35:25 +02004151 :param integer length: *optional* The length of data to replace. All incoming
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02004152 data by default.
Christopher Faulet5a2c6612021-08-15 20:35:25 +02004153 :returns: an integer containing the amount of bytes copied or -1.
4154
4155.. js:function:: HTTPMessage.set_eom(http_msg)
4156
4157 This function set the end of message for the HTTP message **http_msg**.
4158
4159 :param class_httpmessage http_msg: The manipulated HTTP message.
4160
4161.. js:function:: HTTPMessage.set_header(http_msg, name, value)
4162
4163 This variable replace all occurrence of all header matching the name **name**,
4164 by only one containing the value **value**.
4165
4166 :param class_httpmessage http_msg: The manipulated HTTP message.
4167 :param string name: The header name.
4168 :param string value: The header value.
4169
4170 This function does the same work as the following code:
4171
4172.. code-block:: lua
4173
4174 http_msg:del_header("header")
4175 http_msg:add_header("header", "value")
4176..
4177
4178.. js:function:: HTTPMessage.set_method(http_msg, method)
4179
4180 Rewrites the request method with the string **method**. The HTTP message
4181 **http_msg** must be the request.
4182
4183 :param class_httpmessage http_msg: The manipulated HTTP message.
4184 :param string method: The new method.
4185
4186.. js:function:: HTTPMessage.set_path(http_msg, path)
4187
4188 Rewrites the request path with the string **path**. The HTTP message
4189 **http_msg** must be the request.
4190
4191 :param class_httpmessage http_msg: The manipulated HTTP message.
4192 :param string method: The new method.
4193
4194.. js:function:: HTTPMessage.set_query(http_msg, query)
4195
4196 Rewrites the request's query string which appears after the first question
4197 mark ("?") with the string **query**. The HTTP message **http_msg** must be
4198 the request.
4199
4200 :param class_httpmessage http_msg: The manipulated HTTP message.
4201 :param string query: The new query.
4202
4203.. js:function:: HTTPMessage.set_status(http_msg, status[, reason])
4204
Ilya Shipitsinff0f2782021-08-22 22:18:07 +05004205 Rewrites the response status code with the integer **code** and optional the
Christopher Faulet5a2c6612021-08-15 20:35:25 +02004206 reason **reason**. If no custom reason is provided, it will be generated from
4207 the status. The HTTP message **http_msg** must be the response.
4208
4209 :param class_httpmessage http_msg: The manipulated HTTP message.
4210 :param integer status: The new response status code.
4211 :param string reason: The new response reason (optional).
4212
4213.. js:function:: HTTPMessage.set_uri(http_msg, uri)
4214
4215 Rewrites the request URI with the string **uri**. The HTTP message
4216 **http_msg** must be the request.
4217
4218 :param class_httpmessage http_msg: The manipulated HTTP message.
4219 :param string uri: The new uri.
4220
4221.. js:function:: HTTPMessage.unset_eom(http_msg)
4222
4223 This function remove the end of message for the HTTP message **http_msg**.
4224
4225 :param class_httpmessage http_msg: The manipulated HTTP message.
4226
William Lallemand10cea5c2022-03-30 16:02:43 +02004227.. _CertCache_class:
4228
4229CertCache class
4230================
4231
4232.. js:class:: CertCache
4233
4234 This class allows to update an SSL certificate file in the memory of the
4235 current HAProxy process. It will do the same as "set ssl cert" + "commit ssl
4236 cert" over the HAProxy CLI.
4237
4238.. js:function:: CertCache.set(certificate)
4239
4240 This function updates a certificate in memory.
4241
4242 :param table certificate: A table containing the fields to update.
4243 :param string certificate.filename: The mandatory filename of the certificate
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02004244 to update, it must already exist in memory.
William Lallemand10cea5c2022-03-30 16:02:43 +02004245 :param string certificate.crt: A certificate in the PEM format. It can also
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02004246 contain a private key.
William Lallemand10cea5c2022-03-30 16:02:43 +02004247 :param string certificate.key: A private key in the PEM format.
4248 :param string certificate.ocsp: An OCSP response in base64. (cf management.txt)
4249 :param string certificate.issuer: The certificate of the OCSP issuer.
4250 :param string certificate.sctl: An SCTL file.
4251
4252.. code-block:: lua
4253
4254 CertCache.set{filename="certs/localhost9994.pem.rsa", crt=crt}
4255
4256
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01004257External Lua libraries
4258======================
4259
4260A lot of useful lua libraries can be found here:
4261
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02004262* Lua toolbox has been superseded by
4263 `https://luarocks.org/ <https://luarocks.org/>`_
4264
4265 The old lua toolbox source code is still available here
4266 `https://github.com/catwell/lua-toolbox <https://github.com/catwell/lua-toolbox>`_ (DEPRECATED)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01004267
Ilya Shipitsin2075ca82020-03-06 23:22:22 +05004268Redis client library:
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01004269
4270* `https://github.com/nrk/redis-lua <https://github.com/nrk/redis-lua>`_
4271
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02004272This is an example about the usage of the Redis library within HAProxy.
4273Note that each call to any function of this library can throw an error if
4274the socket connection fails.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01004275
4276.. code-block:: lua
4277
4278 -- load the redis library
4279 local redis = require("redis");
4280
4281 function do_something(txn)
4282
4283 -- create and connect new tcp socket
4284 local tcp = core.tcp();
4285 tcp:settimeout(1);
4286 tcp:connect("127.0.0.1", 6379);
4287
4288 -- use the redis library with this new socket
4289 local client = redis.connect({socket=tcp});
4290 client:ping();
4291
4292 end
4293
4294OpenSSL:
4295
4296* `http://mkottman.github.io/luacrypto/index.html
4297 <http://mkottman.github.io/luacrypto/index.html>`_
4298
4299* `https://github.com/brunoos/luasec/wiki
4300 <https://github.com/brunoos/luasec/wiki>`_