blob: d2720d22c8a2411c71014b664d87b9be95394a3d [file] [log] [blame]
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001.. toctree::
2 :maxdepth: 2
3
4
5How Lua runs in HAProxy
6=======================
7
8HAProxy Lua running contexts
9----------------------------
10
11The Lua code executed in HAProxy can be processed in 2 main modes. The first one
12is the **initialisation mode**, and the second is the **runtime mode**.
13
14* In the **initialisation mode**, we can perform DNS solves, but we cannot
15 perform socket I/O. In this initialisation mode, HAProxy still blocked during
16 the execution of the Lua program.
17
18* In the **runtime mode**, we cannot perform DNS solves, but we can use sockets.
19 The execution of the Lua code is multiplexed with the requests processing, so
20 the Lua code seems to be run in blocking, but it is not the case.
21
22The Lua code is loaded in one or more files. These files contains main code and
Aurelien DARRAGONc84899c2023-02-20 18:18:59 +010023functions. Lua has 8 execution contexts.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +010024
251. The Lua file **body context**. It is executed during the load of the Lua file
26 in the HAProxy `[global]` section with the directive `lua-load`. It is
27 executed in initialisation mode. This section is use for configuring Lua
28 bindings in HAProxy.
29
David Carlier61fdf8b2015-10-02 11:59:38 +0100302. The Lua **init context**. It is a Lua function executed just after the
Thierry FOURNIER17bd1522015-03-11 20:31:00 +010031 HAProxy configuration parsing. The execution is in initialisation mode. In
32 this context the HAProxy environment are already initialized. It is useful to
33 check configuration, or initializing socket connections or tasks. These
34 functions are declared in the body context with the Lua function
35 `core.register_init()`. The prototype of the function is a simple function
36 without return value and without parameters, like this: `function fcn()`.
37
David Carlier61fdf8b2015-10-02 11:59:38 +0100383. The Lua **task context**. It is a Lua function executed after the start
Thierry FOURNIER17bd1522015-03-11 20:31:00 +010039 of the HAProxy scheduler, and just after the declaration of the task with the
40 Lua function `core.register_task()`. This context can be concurrent with the
41 traffic processing. It is executed in runtime mode. The prototype of the
42 function is a simple function without return value and without parameters,
43 like this: `function fcn()`.
44
David Carlier61fdf8b2015-10-02 11:59:38 +0100454. The **action context**. It is a Lua function conditionally executed. These
Thierry FOURNIERa2d02252015-10-01 15:00:42 +020046 actions are registered by the Lua directives "`core.register_action()`". The
47 prototype of the Lua called function is a function with doesn't returns
48 anything and that take an object of class TXN as entry. `function fcn(txn)`.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +010049
505. The **sample-fetch context**. This function takes a TXN object as entry
51 argument and returns a string. These types of function cannot execute any
52 blocking function. They are useful to aggregate some of original HAProxy
53 sample-fetches and return the result. The prototype of the function is
54 `function string fcn(txn)`. These functions can be registered with the Lua
55 function `core.register_fetches()`. Each declared sample-fetch is prefixed by
56 the string "lua.".
57
Christopher Faulet1e9b1b62021-08-11 10:14:30 +020058 .. note::
59 It is possible that this function cannot found the required data in the
60 original HAProxy sample-fetches, in this case, it cannot return the
61 result. This case is not yet supported
Thierry FOURNIER17bd1522015-03-11 20:31:00 +010062
David Carlier61fdf8b2015-10-02 11:59:38 +0100636. The **converter context**. It is a Lua function that takes a string as input
Thierry FOURNIER17bd1522015-03-11 20:31:00 +010064 and returns another string as output. These types of function are stateless,
65 it cannot access to any context. They don't execute any blocking function.
66 The call prototype is `function string fcn(string)`. This function can be
67 registered with the Lua function `core.register_converters()`. Each declared
68 converter is prefixed by the string "lua.".
69
Christopher Faulet5a2c6612021-08-15 20:35:25 +0200707. The **filter context**: It is a Lua object based on a class defining filter
71 callback functions. Lua filters are registered using
72 `core.register_filter()`. Each declared filter is prefixed by the string
73 "lua.".
74
Aurelien DARRAGONc84899c2023-02-20 18:18:59 +0100758. The **event context**: Inside a function that handles events subscribed
76 through `core.event_sub()` or `Server.event_sub()`.
77
Christopher Faulet5a2c6612021-08-15 20:35:25 +020078
Thierry FOURNIER17bd1522015-03-11 20:31:00 +010079HAProxy Lua Hello world
80-----------------------
81
82HAProxy configuration file (`hello_world.conf`):
83
84::
85
86 global
87 lua-load hello_world.lua
88
89 listen proxy
90 bind 127.0.0.1:10001
Thierry FOURNIERa2d02252015-10-01 15:00:42 +020091 tcp-request inspect-delay 1s
92 tcp-request content use-service lua.hello_world
Thierry FOURNIER17bd1522015-03-11 20:31:00 +010093
94HAProxy Lua file (`hello_world.lua`):
95
96.. code-block:: lua
97
Thierry FOURNIERa2d02252015-10-01 15:00:42 +020098 core.register_service("hello_world", "tcp", function(applet)
99 applet:send("hello world\n")
100 end)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100101
102How to start HAProxy for testing this configuration:
103
104::
105
106 ./haproxy -f hello_world.conf
107
108On other terminal, you can test with telnet:
109
110::
111
112 #:~ telnet 127.0.0.1 10001
113 hello world
114
Thierry Fournierae6b5682022-09-19 09:04:16 +0200115Usage of load parameters
116------------------------
117
Ilya Shipitsin4a689da2022-10-29 09:34:32 +0500118HAProxy lua-load(-per-thread) directives allow a list of parameters after
Thierry Fournierae6b5682022-09-19 09:04:16 +0200119the lua file name. These parameters are accessible through an array of args
120using this code `local args = table.pack(...)` in the body of loaded file.
121
122Below, a new version of the hello world using load parameters
123
124HAProxy configuration file (`hello_world.conf`):
125
126::
127
128 global
129 lua-load hello_world.lua "this is not an hello world"
130
131 listen proxy
132 bind 127.0.0.1:10001
133 tcp-request inspect-delay 1s
134 tcp-request content use-service lua.hello_world
135
136HAProxy Lua file (`hello_world.lua`):
137
138.. code-block:: lua
139
140 local args = table.pack(...)
141
142 core.register_service("hello_world", "tcp", function(applet)
143 applet:send(args[1] .. "\n")
144 end)
145
146
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100147Core class
148==========
149
150.. js:class:: core
151
152 The "core" class contains all the HAProxy core functions. These function are
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +0200153 useful for the controlling of the execution flow, registering hooks,
154 manipulating global maps or ACL, ...
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100155
156 "core" class is basically provided with HAProxy. No `require` line is
157 required to uses these function.
158
David Carlier61fdf8b2015-10-02 11:59:38 +0100159 The "core" class is static, it is not possible to create a new object of this
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100160 type.
161
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100162.. js:attribute:: core.emerg
163
Thierry FOURNIERdc595002015-12-21 11:13:52 +0100164 :returns: integer
165
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +0200166 This attribute is an integer, it contains the value of the loglevel
167 "emergency" (0).
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100168
169.. js:attribute:: core.alert
170
Thierry FOURNIERdc595002015-12-21 11:13:52 +0100171 :returns: integer
172
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +0200173 This attribute is an integer, it contains the value of the loglevel
174 "alert" (1).
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100175
176.. js:attribute:: core.crit
177
Thierry FOURNIERdc595002015-12-21 11:13:52 +0100178 :returns: integer
179
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +0200180 This attribute is an integer, it contains the value of the loglevel
181 "critical" (2).
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100182
183.. js:attribute:: core.err
184
Thierry FOURNIERdc595002015-12-21 11:13:52 +0100185 :returns: integer
186
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +0200187 This attribute is an integer, it contains the value of the loglevel
188 "error" (3).
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100189
190.. js:attribute:: core.warning
191
Thierry FOURNIERdc595002015-12-21 11:13:52 +0100192 :returns: integer
193
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +0200194 This attribute is an integer, it contains the value of the loglevel
195 "warning" (4).
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100196
197.. js:attribute:: core.notice
198
Thierry FOURNIERdc595002015-12-21 11:13:52 +0100199 :returns: integer
200
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +0200201 This attribute is an integer, it contains the value of the loglevel
202 "notice" (5).
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100203
204.. js:attribute:: core.info
205
Thierry FOURNIERdc595002015-12-21 11:13:52 +0100206 :returns: integer
207
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +0200208 This attribute is an integer, it contains the value of the loglevel
209 "info" (6).
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100210
211.. js:attribute:: core.debug
212
Thierry FOURNIERdc595002015-12-21 11:13:52 +0100213 :returns: integer
214
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +0200215 This attribute is an integer, it contains the value of the loglevel
216 "debug" (7).
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100217
Thierry FOURNIER12a865d2016-12-14 19:40:37 +0100218.. js:attribute:: core.proxies
219
Aurelien DARRAGON2a295712023-05-11 17:31:46 +0200220 **context**: init, task, action, sample-fetch, converter
Thierry FOURNIER12a865d2016-12-14 19:40:37 +0100221
Patrick Hemmerc6a1d712018-05-01 21:30:41 -0400222 This attribute is a table of declared proxies (frontend and backends). Each
223 proxy give an access to his list of listeners and servers. The table is
224 indexed by proxy name, and each entry is of type :ref:`proxy_class`.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +0100225
Christopher Faulet1e9b1b62021-08-11 10:14:30 +0200226 .. Warning::
Aurelien DARRAGON53901f42022-10-13 19:49:42 +0200227 if you declared a frontend and backend with the same name, only one of
228 them will be listed.
Thierry FOURNIER9b82a582017-07-24 13:30:43 +0200229
230 :see: :js:attr:`core.backends`
231 :see: :js:attr:`core.frontends`
232
233.. js:attribute:: core.backends
234
Aurelien DARRAGON2a295712023-05-11 17:31:46 +0200235 **context**: init, task, action, sample-fetch, converter
Thierry FOURNIER9b82a582017-07-24 13:30:43 +0200236
Patrick Hemmerc6a1d712018-05-01 21:30:41 -0400237 This attribute is a table of declared proxies with backend capability. Each
238 proxy give an access to his list of listeners and servers. The table is
239 indexed by the backend name, and each entry is of type :ref:`proxy_class`.
Thierry FOURNIER9b82a582017-07-24 13:30:43 +0200240
241 :see: :js:attr:`core.proxies`
242 :see: :js:attr:`core.frontends`
243
244.. js:attribute:: core.frontends
245
Aurelien DARRAGON2a295712023-05-11 17:31:46 +0200246 **context**: init, task, action, sample-fetch, converter
Thierry FOURNIER9b82a582017-07-24 13:30:43 +0200247
Patrick Hemmerc6a1d712018-05-01 21:30:41 -0400248 This attribute is a table of declared proxies with frontend capability. Each
249 proxy give an access to his list of listeners and servers. The table is
250 indexed by the frontend name, and each entry is of type :ref:`proxy_class`.
Thierry FOURNIER9b82a582017-07-24 13:30:43 +0200251
252 :see: :js:attr:`core.proxies`
253 :see: :js:attr:`core.backends`
254
Thierry Fournierecb83c22020-11-28 15:49:44 +0100255.. js:attribute:: core.thread
256
257 **context**: task, action, sample-fetch, converter, applet
258
259 This variable contains the executing thread number starting at 1. 0 is a
260 special case for the common lua context. So, if thread is 0, Lua scope is
261 shared by all threads, otherwise the scope is dedicated to a single thread.
262 A program which needs to execute some parts exactly once regardless of the
263 number of threads can check that core.thread is 0 or 1.
264
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100265.. js:function:: core.log(loglevel, msg)
266
267 **context**: body, init, task, action, sample-fetch, converter
268
David Carlier61fdf8b2015-10-02 11:59:38 +0100269 This function sends a log. The log is sent, according with the HAProxy
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100270 configuration file, on the default syslog server if it is configured and on
271 the stderr if it is allowed.
272
Bertrand Jacquin874a35c2018-09-10 21:26:07 +0100273 :param integer loglevel: Is the log level associated with the message. It is a
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +0200274 number between 0 and 7.
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100275 :param string msg: The log content.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +0100276 :see: :js:attr:`core.emerg`, :js:attr:`core.alert`, :js:attr:`core.crit`,
277 :js:attr:`core.err`, :js:attr:`core.warning`, :js:attr:`core.notice`,
278 :js:attr:`core.info`, :js:attr:`core.debug` (log level definitions)
279 :see: :js:func:`core.Debug`
280 :see: :js:func:`core.Info`
281 :see: :js:func:`core.Warning`
282 :see: :js:func:`core.Alert`
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100283
284.. js:function:: core.Debug(msg)
285
286 **context**: body, init, task, action, sample-fetch, converter
287
288 :param string msg: The log content.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +0100289 :see: :js:func:`core.log`
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100290
291 Does the same job than:
292
293.. code-block:: lua
294
Thierry FOURNIERdc595002015-12-21 11:13:52 +0100295 function Debug(msg)
296 core.log(core.debug, msg)
297 end
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100298..
299
300.. js:function:: core.Info(msg)
301
302 **context**: body, init, task, action, sample-fetch, converter
303
304 :param string msg: The log content.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +0100305 :see: :js:func:`core.log`
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100306
307.. code-block:: lua
308
Thierry FOURNIERdc595002015-12-21 11:13:52 +0100309 function Info(msg)
310 core.log(core.info, msg)
311 end
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100312..
313
314.. js:function:: core.Warning(msg)
315
316 **context**: body, init, task, action, sample-fetch, converter
317
318 :param string msg: The log content.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +0100319 :see: :js:func:`core.log`
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100320
321.. code-block:: lua
322
Thierry FOURNIERdc595002015-12-21 11:13:52 +0100323 function Warning(msg)
324 core.log(core.warning, msg)
325 end
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100326..
327
328.. js:function:: core.Alert(msg)
329
330 **context**: body, init, task, action, sample-fetch, converter
331
332 :param string msg: The log content.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +0100333 :see: :js:func:`core.log`
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100334
335.. code-block:: lua
336
Thierry FOURNIERdc595002015-12-21 11:13:52 +0100337 function Alert(msg)
338 core.log(core.alert, msg)
339 end
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100340..
341
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100342.. js:function:: core.add_acl(filename, key)
343
344 **context**: init, task, action, sample-fetch, converter
345
346 Add the ACL *key* in the ACLs list referenced by the file *filename*.
347
348 :param string filename: the filename that reference the ACL entries.
349 :param string key: the key which will be added.
350
351.. js:function:: core.del_acl(filename, key)
352
353 **context**: init, task, action, sample-fetch, converter
354
355 Delete the ACL entry referenced by the key *key* in the list of ACLs
356 referenced by *filename*.
357
358 :param string filename: the filename that reference the ACL entries.
359 :param string key: the key which will be deleted.
360
361.. js:function:: core.del_map(filename, key)
362
363 **context**: init, task, action, sample-fetch, converter
364
365 Delete the map entry indexed with the specified key in the list of maps
366 referenced by his filename.
367
368 :param string filename: the filename that reference the map entries.
369 :param string key: the key which will be deleted.
370
Thierry Fourniereea77c02016-03-18 08:47:13 +0100371.. js:function:: core.get_info()
372
373 **context**: body, init, task, action, sample-fetch, converter
374
Aurelien DARRAGON53901f42022-10-13 19:49:42 +0200375 Returns HAProxy core information. We can find information like the uptime,
Thierry Fourniereea77c02016-03-18 08:47:13 +0100376 the pid, memory pool usage, tasks number, ...
377
Ilya Shipitsin5fa29b82022-12-07 09:46:19 +0500378 This information is also returned by the management socket via the command
Bertrand Jacquin874a35c2018-09-10 21:26:07 +0100379 "show info". See the management socket documentation for more information
Thierry Fourniereea77c02016-03-18 08:47:13 +0100380 about the content of these variables.
381
382 :returns: an array of values.
383
Thierry Fournierb1f46562016-01-21 09:46:15 +0100384.. js:function:: core.now()
385
386 **context**: body, init, task, action
387
388 This function returns the current time. The time returned is fixed by the
Bertrand Jacquin874a35c2018-09-10 21:26:07 +0100389 HAProxy core and assures than the hour will be monotonic and that the system
Thierry Fournierb1f46562016-01-21 09:46:15 +0100390 call 'gettimeofday' will not be called too. The time is refreshed between each
391 Lua execution or resume, so two consecutive call to the function "now" will
392 probably returns the same result.
393
Patrick Hemmerc6a1d712018-05-01 21:30:41 -0400394 :returns: a table which contains two entries "sec" and "usec". "sec"
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +0200395 contains the current at the epoch format, and "usec" contains the
396 current microseconds.
Thierry Fournierb1f46562016-01-21 09:46:15 +0100397
Thierry FOURNIERa78f0372016-12-14 19:04:41 +0100398.. js:function:: core.http_date(date)
399
400 **context**: body, init, task, action
401
Bertrand Jacquin874a35c2018-09-10 21:26:07 +0100402 This function take a string representing http date, and returns an integer
Thierry FOURNIERa78f0372016-12-14 19:04:41 +0100403 containing the corresponding date with a epoch format. A valid http date
404 me respect the format IMF, RFC850 or ASCTIME.
405
406 :param string date: a date http-date formatted
407 :returns: integer containing epoch date
408 :see: :js:func:`core.imf_date`.
409 :see: :js:func:`core.rfc850_date`.
410 :see: :js:func:`core.asctime_date`.
411 :see: https://tools.ietf.org/html/rfc7231#section-7.1.1.1
412
413.. js:function:: core.imf_date(date)
414
415 **context**: body, init, task, action
416
Bertrand Jacquin874a35c2018-09-10 21:26:07 +0100417 This function take a string representing IMF date, and returns an integer
Thierry FOURNIERa78f0372016-12-14 19:04:41 +0100418 containing the corresponding date with a epoch format.
419
420 :param string date: a date IMF formatted
421 :returns: integer containing epoch date
422 :see: https://tools.ietf.org/html/rfc7231#section-7.1.1.1
423
424 The IMF format is like this:
425
426.. code-block:: text
427
428 Sun, 06 Nov 1994 08:49:37 GMT
429..
430
431.. js:function:: core.rfc850_date(date)
432
433 **context**: body, init, task, action
434
Bertrand Jacquin874a35c2018-09-10 21:26:07 +0100435 This function take a string representing RFC850 date, and returns an integer
Thierry FOURNIERa78f0372016-12-14 19:04:41 +0100436 containing the corresponding date with a epoch format.
437
438 :param string date: a date RFC859 formatted
439 :returns: integer containing epoch date
440 :see: https://tools.ietf.org/html/rfc7231#section-7.1.1.1
441
442 The RFC850 format is like this:
443
444.. code-block:: text
445
446 Sunday, 06-Nov-94 08:49:37 GMT
447..
448
449.. js:function:: core.asctime_date(date)
450
451 **context**: body, init, task, action
452
Bertrand Jacquin874a35c2018-09-10 21:26:07 +0100453 This function take a string representing ASCTIME date, and returns an integer
Thierry FOURNIERa78f0372016-12-14 19:04:41 +0100454 containing the corresponding date with a epoch format.
455
456 :param string date: a date ASCTIME formatted
457 :returns: integer containing epoch date
458 :see: https://tools.ietf.org/html/rfc7231#section-7.1.1.1
459
460 The ASCTIME format is like this:
461
462.. code-block:: text
463
464 Sun Nov 6 08:49:37 1994
465..
466
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100467.. js:function:: core.msleep(milliseconds)
468
469 **context**: body, init, task, action
470
471 The `core.msleep()` stops the Lua execution between specified milliseconds.
472
473 :param integer milliseconds: the required milliseconds.
474
Thierry FOURNIERc5d11c62018-02-12 14:46:54 +0100475.. js:function:: core.register_action(name, actions, func [, nb_args])
Thierry FOURNIER8255a752015-09-23 21:03:35 +0200476
477 **context**: body
478
David Carlier61fdf8b2015-10-02 11:59:38 +0100479 Register a Lua function executed as action. All the registered action can be
Thierry FOURNIER8255a752015-09-23 21:03:35 +0200480 used in HAProxy with the prefix "lua.". An action gets a TXN object class as
481 input.
482
483 :param string name: is the name of the converter.
Willy Tarreau61add3c2015-09-28 15:39:10 +0200484 :param table actions: is a table of string describing the HAProxy actions who
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +0200485 want to register to. The expected actions are 'tcp-req', 'tcp-res', 'http-req'
486 or 'http-res'.
Aurelien DARRAGON53901f42022-10-13 19:49:42 +0200487 :param function func: is the Lua function called to work as converter.
Thierry FOURNIERc5d11c62018-02-12 14:46:54 +0100488 :param integer nb_args: is the expected number of argument for the action.
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +0200489 By default the value is 0.
Thierry FOURNIER8255a752015-09-23 21:03:35 +0200490
491 The prototype of the Lua function used as argument is:
492
493.. code-block:: lua
494
Thierry FOURNIERc5d11c62018-02-12 14:46:54 +0100495 function(txn [, arg1 [, arg2]])
Thierry FOURNIER8255a752015-09-23 21:03:35 +0200496..
497
Thierry FOURNIERdc595002015-12-21 11:13:52 +0100498 * **txn** (:ref:`txn_class`): this is a TXN object used for manipulating the
Thierry FOURNIER8255a752015-09-23 21:03:35 +0200499 current request or TCP stream.
500
Bertrand Jacquin874a35c2018-09-10 21:26:07 +0100501 * **argX**: this is argument provided through the HAProxy configuration file.
Thierry FOURNIERc5d11c62018-02-12 14:46:54 +0100502
Bertrand Jacquin874a35c2018-09-10 21:26:07 +0100503 Here, an example of action registration. The action just send an 'Hello world'
Thierry FOURNIER8255a752015-09-23 21:03:35 +0200504 in the logs.
505
506.. code-block:: lua
507
508 core.register_action("hello-world", { "tcp-req", "http-req" }, function(txn)
509 txn:Info("Hello world")
510 end)
511..
512
Willy Tarreau714f3452021-05-09 06:47:26 +0200513 This example code is used in HAProxy configuration like this:
Thierry FOURNIER8255a752015-09-23 21:03:35 +0200514
515::
516
517 frontend tcp_frt
518 mode tcp
519 tcp-request content lua.hello-world
520
521 frontend http_frt
522 mode http
523 http-request lua.hello-world
Aurelien DARRAGONd5c80d72023-03-13 19:36:13 +0100524
Thierry FOURNIERc5d11c62018-02-12 14:46:54 +0100525..
526
Bertrand Jacquin874a35c2018-09-10 21:26:07 +0100527 A second example using arguments
Thierry FOURNIERc5d11c62018-02-12 14:46:54 +0100528
529.. code-block:: lua
530
531 function hello_world(txn, arg)
532 txn:Info("Hello world for " .. arg)
533 end
534 core.register_action("hello-world", { "tcp-req", "http-req" }, hello_world, 2)
Aurelien DARRAGONd5c80d72023-03-13 19:36:13 +0100535
Thierry FOURNIERc5d11c62018-02-12 14:46:54 +0100536..
Thierry FOURNIER8255a752015-09-23 21:03:35 +0200537
Willy Tarreau714f3452021-05-09 06:47:26 +0200538 This example code is used in HAProxy configuration like this:
Thierry FOURNIERc5d11c62018-02-12 14:46:54 +0100539
540::
541
542 frontend tcp_frt
543 mode tcp
544 tcp-request content lua.hello-world everybody
Aurelien DARRAGONd5c80d72023-03-13 19:36:13 +0100545
Thierry FOURNIERc5d11c62018-02-12 14:46:54 +0100546..
Aurelien DARRAGON53901f42022-10-13 19:49:42 +0200547
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100548.. js:function:: core.register_converters(name, func)
549
550 **context**: body
551
David Carlier61fdf8b2015-10-02 11:59:38 +0100552 Register a Lua function executed as converter. All the registered converters
Aurelien DARRAGON53901f42022-10-13 19:49:42 +0200553 can be used in HAProxy with the prefix "lua.". A converter gets a string as
554 input and returns a string as output. The registered function can take up to 9
555 values as parameter. All the values are strings.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100556
557 :param string name: is the name of the converter.
558 :param function func: is the Lua function called to work as converter.
559
560 The prototype of the Lua function used as argument is:
561
562.. code-block:: lua
563
564 function(str, [p1 [, p2 [, ... [, p5]]]])
565..
566
567 * **str** (*string*): this is the input value automatically converted in
568 string.
569 * **p1** .. **p5** (*string*): this is a list of string arguments declared in
Bertrand Jacquin874a35c2018-09-10 21:26:07 +0100570 the HAProxy configuration file. The number of arguments doesn't exceed 5.
Aurelien DARRAGON53901f42022-10-13 19:49:42 +0200571 The order and the nature of these is conventionally chosen by the
Bertrand Jacquin874a35c2018-09-10 21:26:07 +0100572 developer.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100573
574.. js:function:: core.register_fetches(name, func)
575
576 **context**: body
577
David Carlier61fdf8b2015-10-02 11:59:38 +0100578 Register a Lua function executed as sample fetch. All the registered sample
Bertrand Jacquin874a35c2018-09-10 21:26:07 +0100579 fetch can be used in HAProxy with the prefix "lua.". A Lua sample fetch
Aurelien DARRAGON53901f42022-10-13 19:49:42 +0200580 returns a string as output. The registered function can take up to 9 values as
581 parameter. All the values are strings.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100582
Aurelien DARRAGON53901f42022-10-13 19:49:42 +0200583 :param string name: is the name of the sample fetch.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100584 :param function func: is the Lua function called to work as sample fetch.
585
586 The prototype of the Lua function used as argument is:
587
588.. code-block:: lua
589
590 string function(txn, [p1 [, p2 [, ... [, p5]]]])
591..
592
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +0200593 * **txn** (:ref:`txn_class`): this is the txn object associated with the
594 current request.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100595 * **p1** .. **p5** (*string*): this is a list of string arguments declared in
Bertrand Jacquin874a35c2018-09-10 21:26:07 +0100596 the HAProxy configuration file. The number of arguments doesn't exceed 5.
Aurelien DARRAGON53901f42022-10-13 19:49:42 +0200597 The order and the nature of these is conventionally chosen by the
Bertrand Jacquin874a35c2018-09-10 21:26:07 +0100598 developer.
599 * **Returns**: A string containing some data, or nil if the value cannot be
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100600 returned now.
601
602 lua example code:
603
604.. code-block:: lua
605
606 core.register_fetches("hello", function(txn)
607 return "hello"
608 end)
609..
610
611 HAProxy example configuration:
612
613::
614
615 frontend example
616 http-request redirect location /%[lua.hello]
617
Christopher Faulet5a2c6612021-08-15 20:35:25 +0200618.. js:function:: core.register_filter(name, Flt, func)
619
620 **context**: body
621
622 Register a Lua function used to declare a filter. All the registered filters
623 can by used in HAProxy with the prefix "lua.".
624
625 :param string name: is the name of the filter.
626 :param table Flt: is a Lua class containing the filter definition (id, flags,
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +0200627 callbacks).
Christopher Faulet5a2c6612021-08-15 20:35:25 +0200628 :param function func: is the Lua function called to create the Lua filter.
629
630 The prototype of the Lua function used as argument is:
631
632.. code-block:: lua
633
634 function(flt, args)
635..
636
637 * **flt** : Is a filter object based on the class provided in
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +0200638 :js:func:`core.register_filter()` function.
Christopher Faulet5a2c6612021-08-15 20:35:25 +0200639
640 * **args**: Is a table of strings containing all arguments provided through
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +0200641 the HAProxy configuration file, on the filter line.
Christopher Faulet5a2c6612021-08-15 20:35:25 +0200642
643 It must return the filter to use or nil to ignore it. Here, an example of
644 filter registration.
645
646.. code-block:: lua
647
648 core.register_filter("my-filter", MyFilter, function(flt, args)
649 flt.args = args -- Save arguments
650 return flt
651 end)
652..
653
654 This example code is used in HAProxy configuration like this:
655
656::
657
658 frontend http
659 mode http
660 filter lua.my-filter arg1 arg2 arg3
Aurelien DARRAGONd5c80d72023-03-13 19:36:13 +0100661
Christopher Faulet5a2c6612021-08-15 20:35:25 +0200662..
663
664 :see: :js:class:`Filter`
665
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +0200666.. js:function:: core.register_service(name, mode, func)
667
668 **context**: body
669
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +0200670 Register a Lua function executed as a service. All the registered services
671 can be used in HAProxy with the prefix "lua.". A service gets an object class
672 as input according with the required mode.
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +0200673
Aurelien DARRAGON53901f42022-10-13 19:49:42 +0200674 :param string name: is the name of the service.
Willy Tarreau61add3c2015-09-28 15:39:10 +0200675 :param string mode: is string describing the required mode. Only 'tcp' or
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +0200676 'http' are allowed.
Aurelien DARRAGON53901f42022-10-13 19:49:42 +0200677 :param function func: is the Lua function called to work as service.
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +0200678
679 The prototype of the Lua function used as argument is:
680
681.. code-block:: lua
682
683 function(applet)
684..
685
Thierry FOURNIERdc595002015-12-21 11:13:52 +0100686 * **applet** *applet* will be a :ref:`applettcp_class` or a
687 :ref:`applethttp_class`. It depends the type of registered applet. An applet
688 registered with the 'http' value for the *mode* parameter will gets a
689 :ref:`applethttp_class`. If the *mode* value is 'tcp', the applet will gets
690 a :ref:`applettcp_class`.
691
Christopher Faulet1e9b1b62021-08-11 10:14:30 +0200692 .. warning::
693 Applets of type 'http' cannot be called from 'tcp-*' rulesets. Only the
694 'http-*' rulesets are authorized, this means that is not possible to call
Aurelien DARRAGON53901f42022-10-13 19:49:42 +0200695 a HTTP applet from a proxy in tcp mode. Applets of type 'tcp' can be
Christopher Faulet1e9b1b62021-08-11 10:14:30 +0200696 called from anywhere.
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +0200697
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +0200698 Here, an example of service registration. The service just send an
699 'Hello world' as an http response.
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +0200700
701.. code-block:: lua
702
Pieter Baauw4d7f7662015-11-08 16:38:08 +0100703 core.register_service("hello-world", "http", function(applet)
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +0200704 local response = "Hello World !"
705 applet:set_status(200)
Pieter Baauw2dcb9bc2015-10-01 22:47:12 +0200706 applet:add_header("content-length", string.len(response))
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +0200707 applet:add_header("content-type", "text/plain")
Pieter Baauw2dcb9bc2015-10-01 22:47:12 +0200708 applet:start_response()
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +0200709 applet:send(response)
710 end)
711..
712
Willy Tarreau714f3452021-05-09 06:47:26 +0200713 This example code is used in HAProxy configuration like this:
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +0200714
715::
716
717 frontend example
718 http-request use-service lua.hello-world
719
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100720.. js:function:: core.register_init(func)
721
722 **context**: body
723
724 Register a function executed after the configuration parsing. This is useful
725 to check any parameters.
726
Pieter Baauw4d7f7662015-11-08 16:38:08 +0100727 :param function func: is the Lua function called to work as initializer.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100728
729 The prototype of the Lua function used as argument is:
730
731.. code-block:: lua
732
733 function()
734..
735
736 It takes no input, and no output is expected.
737
Aurelien DARRAGONb8038992023-03-09 16:48:30 +0100738.. js:function:: core.register_task(func[, arg1[, arg2[, ...[, arg4]]]])
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100739
Aurelien DARRAGONc84899c2023-02-20 18:18:59 +0100740 **context**: body, init, task, action, sample-fetch, converter, event
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100741
742 Register and start independent task. The task is started when the HAProxy
743 main scheduler starts. For example this type of tasks can be executed to
Thierry FOURNIER2e4893c2015-03-18 13:37:27 +0100744 perform complex health checks.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100745
Aurelien DARRAGONb8038992023-03-09 16:48:30 +0100746 :param function func: is the Lua function called to work as an async task.
747
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +0200748 Up to 4 optional arguments (all types supported) may be passed to the
749 function. (They will be passed as-is to the task function)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100750
751 The prototype of the Lua function used as argument is:
752
753.. code-block:: lua
754
Aurelien DARRAGONb8038992023-03-09 16:48:30 +0100755 function([arg1[, arg2[, ...[, arg4]]]])
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100756..
757
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +0200758 It takes up to 4 optional arguments (provided when registering), and no
759 output is expected.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100760
Aurelien DARRAGON86fb22c2023-05-03 17:03:09 +0200761 See also :js:func:`core.queue` to dynamically pass data between main context
762 and tasks or even between tasks.
763
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +0100764.. js:function:: core.register_cli([path], usage, func)
765
766 **context**: body
767
Aurelien DARRAGON53901f42022-10-13 19:49:42 +0200768 Register a custom cli that will be available from haproxy stats socket.
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +0100769
770 :param array path: is the sequence of word for which the cli execute the Lua
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +0200771 binding.
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +0100772 :param string usage: is the usage message displayed in the help.
773 :param function func: is the Lua function called to handle the CLI commands.
774
775 The prototype of the Lua function used as argument is:
776
777.. code-block:: lua
778
779 function(AppletTCP, [arg1, [arg2, [...]]])
780..
781
782 I/O are managed with the :ref:`applettcp_class` object. Args are given as
Bertrand Jacquin874a35c2018-09-10 21:26:07 +0100783 parameter. The args embed the registered path. If the path is declared like
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +0100784 this:
785
786.. code-block:: lua
787
788 core.register_cli({"show", "ssl", "stats"}, "Display SSL stats..", function(applet, arg1, arg2, arg3, arg4, arg5)
789 end)
790..
791
792 And we execute this in the prompt:
793
794.. code-block:: text
795
796 > prompt
797 > show ssl stats all
798..
799
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +0200800 Then, arg1, arg2 and arg3 will contains respectively "show", "ssl" and
801 "stats".
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +0100802 arg4 will contain "all". arg5 contains nil.
803
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100804.. js:function:: core.set_nice(nice)
805
806 **context**: task, action, sample-fetch, converter
807
808 Change the nice of the current task or current session.
Thierry FOURNIER2e4893c2015-03-18 13:37:27 +0100809
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100810 :param integer nice: the nice value, it must be between -1024 and 1024.
811
812.. js:function:: core.set_map(filename, key, value)
813
814 **context**: init, task, action, sample-fetch, converter
815
Bertrand Jacquin874a35c2018-09-10 21:26:07 +0100816 Set the value *value* associated to the key *key* in the map referenced by
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100817 *filename*.
818
Thierry FOURNIERdc595002015-12-21 11:13:52 +0100819 :param string filename: the Map reference
820 :param string key: the key to set or replace
821 :param string value: the associated value
822
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100823.. js:function:: core.sleep(int seconds)
824
825 **context**: body, init, task, action
826
827 The `core.sleep()` functions stop the Lua execution between specified seconds.
828
829 :param integer seconds: the required seconds.
830
831.. js:function:: core.tcp()
832
833 **context**: init, task, action
834
835 This function returns a new object of a *socket* class.
836
Thierry FOURNIERdc595002015-12-21 11:13:52 +0100837 :returns: A :ref:`socket_class` object.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100838
William Lallemand00a15022021-11-19 16:02:44 +0100839.. js:function:: core.httpclient()
840
841 **context**: init, task, action
842
843 This function returns a new object of a *httpclient* class.
844
845 :returns: A :ref:`httpclient_class` object.
846
Thierry Fournier1de16592016-01-27 09:49:07 +0100847.. js:function:: core.concat()
848
849 **context**: body, init, task, action, sample-fetch, converter
850
Bertrand Jacquin874a35c2018-09-10 21:26:07 +0100851 This function returns a new concat object.
Thierry Fournier1de16592016-01-27 09:49:07 +0100852
853 :returns: A :ref:`concat_class` object.
854
Aurelien DARRAGON86fb22c2023-05-03 17:03:09 +0200855.. js:function:: core.queue()
856
857 **context**: body, init, task, event, action, sample-fetch, converter
858
859 This function returns a new queue object.
860
861 :returns: A :ref:`queue_class` object.
862
Thierry FOURNIER0a99b892015-08-26 00:14:17 +0200863.. js:function:: core.done(data)
864
865 **context**: body, init, task, action, sample-fetch, converter
866
867 :param any data: Return some data for the caller. It is useful with
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +0200868 sample-fetches and sample-converters.
Thierry FOURNIER0a99b892015-08-26 00:14:17 +0200869
870 Immediately stops the current Lua execution and returns to the caller which
871 may be a sample fetch, a converter or an action and returns the specified
Thierry Fournier4234dbd2020-11-28 13:18:23 +0100872 value (ignored for actions and init). It is used when the LUA process finishes
873 its work and wants to give back the control to HAProxy without executing the
Thierry FOURNIER0a99b892015-08-26 00:14:17 +0200874 remaining code. It can be seen as a multi-level "return".
875
Thierry FOURNIER486f5a02015-03-16 15:13:03 +0100876.. js:function:: core.yield()
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100877
878 **context**: task, action, sample-fetch, converter
879
880 Give back the hand at the HAProxy scheduler. It is used when the LUA
881 processing consumes a lot of processing time.
882
Thierry FOURNIER / OZON.IO62fec752016-11-10 20:38:11 +0100883.. js:function:: core.parse_addr(address)
884
885 **context**: body, init, task, action, sample-fetch, converter
886
887 :param network: is a string describing an ipv4 or ipv6 address and optionally
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +0200888 its network length, like this: "127.0.0.1/8" or "aaaa::1234/32".
Thierry FOURNIER / OZON.IO62fec752016-11-10 20:38:11 +0100889 :returns: a userdata containing network or nil if an error occurs.
890
Bertrand Jacquin874a35c2018-09-10 21:26:07 +0100891 Parse ipv4 or ipv6 addresses and its facultative associated network.
Thierry FOURNIER / OZON.IO62fec752016-11-10 20:38:11 +0100892
893.. js:function:: core.match_addr(addr1, addr2)
894
895 **context**: body, init, task, action, sample-fetch, converter
896
897 :param addr1: is an address created with "core.parse_addr".
898 :param addr2: is an address created with "core.parse_addr".
Bertrand Jacquin874a35c2018-09-10 21:26:07 +0100899 :returns: boolean, true if the network of the addresses match, else returns
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +0200900 false.
Thierry FOURNIER / OZON.IO62fec752016-11-10 20:38:11 +0100901
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +0200902 Match two networks. For example "127.0.0.1/32" matches "127.0.0.0/8". The
903 order of network is not important.
Thierry FOURNIER / OZON.IO62fec752016-11-10 20:38:11 +0100904
Thierry FOURNIER / OZON.IO8a1027a2016-11-24 20:48:38 +0100905.. js:function:: core.tokenize(str, separators [, noblank])
906
907 **context**: body, init, task, action, sample-fetch, converter
908
909 This function is useful for tokenizing an entry, or splitting some messages.
910 :param string str: The string which will be split.
911 :param string separators: A string containing a list of separators.
912 :param boolean noblank: Ignore empty entries.
913 :returns: an array of string.
914
915 For example:
916
917.. code-block:: lua
918
919 local array = core.tokenize("This function is useful, for tokenizing an entry.", "., ", true)
920 print_r(array)
921..
922
923 Returns this array:
924
925.. code-block:: text
926
927 (table) table: 0x21c01e0 [
928 1: (string) "This"
929 2: (string) "function"
930 3: (string) "is"
931 4: (string) "useful"
932 5: (string) "for"
933 6: (string) "tokenizing"
934 7: (string) "an"
935 8: (string) "entry"
936 ]
937..
938
Aurelien DARRAGONc84899c2023-02-20 18:18:59 +0100939.. js:function:: core.event_sub(event_types, func)
940
941 **context**: body, init, task, action, sample-fetch, converter
942
943 Register a function that will be called on specific system events.
944
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +0200945 :param array event_types: array of string containing the event types you want
946 to subscribe to
947 :param function func: is the Lua function called when one of the subscribed
948 events occur.
Aurelien DARRAGONc84899c2023-02-20 18:18:59 +0100949 :returns: A :ref:`event_sub_class` object.
Aurelien DARRAGON223770d2023-03-10 15:34:35 +0100950 :see: :js:func:`Server.event_sub()`.
Aurelien DARRAGONc84899c2023-02-20 18:18:59 +0100951
952 List of available event types :
953
954 **SERVER** Family:
955
956 * **SERVER_ADD**: when a server is added
957 * **SERVER_DEL**: when a server is removed
958 * **SERVER_DOWN**: when a server state goes from UP to DOWN
959 * **SERVER_UP**: when a server state goes from DOWN to UP
Aurelien DARRAGONc99f3ad2023-04-12 15:47:16 +0200960 * **SERVER_STATE**: when a server state changes
Aurelien DARRAGON948dd3d2023-04-26 11:27:09 +0200961 * **SERVER_ADMIN**: when a server administrative state changes
Aurelien DARRAGON0bd53b22023-03-30 15:53:33 +0200962 * **SERVER_CHECK**: when a server's check status change is reported.
963 Be careful when subscribing to this type since many events might be
964 generated.
Aurelien DARRAGONc84899c2023-02-20 18:18:59 +0100965
966 .. Note::
Aurelien DARRAGONc99f3ad2023-04-12 15:47:16 +0200967 Use **SERVER** in **event_types** to subscribe to all server events types
968 at once. Note that this should only be used for testing purposes since a
969 single event source could result in multiple events types being generated.
970 (e.g.: SERVER_STATE will always be generated for each SERVER_DOWN or
971 SERVER_UP)
Aurelien DARRAGONc84899c2023-02-20 18:18:59 +0100972
973 The prototype of the Lua function used as argument is:
974
975.. code-block:: lua
976
Aurelien DARRAGON096b3832023-04-20 11:32:46 +0200977 function(event, event_data, sub, when)
Aurelien DARRAGONc84899c2023-02-20 18:18:59 +0100978..
979
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +0200980 * **event** (*string*): the event type (one of the **event_types** specified
981 when subscribing)
982 * **event_data**: specific to each event family (For **SERVER** family,
983 a :ref:`server_event_class` object)
984 * **sub**: class to manage the subscription from within the event
985 (a :ref:`event_sub_class` object)
Aurelien DARRAGON096b3832023-04-20 11:32:46 +0200986 * **when**: timestamp corresponding to the date when the event was generated.
987 It is an integer representing the number of seconds elapsed since Epoch.
988 It may be provided as optional argument to `os.date()` lua function to
989 convert it to a string according to a given format string.
Aurelien DARRAGONc84899c2023-02-20 18:18:59 +0100990
991 .. Warning::
992 The callback function will only be scheduled on the very same thread that
993 performed the subscription.
994
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +0200995 Moreover, each thread treats events sequentially. It means that if you
996 have, let's say SERVER_UP followed by a SERVER_DOWN in a short timelapse,
997 then the cb function will first be called with SERVER_UP, and once it's
998 done handling the event, the cb function will be called again with
999 SERVER_DOWN.
Aurelien DARRAGONc84899c2023-02-20 18:18:59 +01001000
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02001001 This is to ensure event consistency when it comes to logging / triggering
1002 logic from lua.
Aurelien DARRAGONc84899c2023-02-20 18:18:59 +01001003
1004 Your lua cb function may yield if needed, but you're pleased to process the
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02001005 event as fast as possible to prevent the event queue from growing up,
1006 depending on the event flow that is expected for the given subscription.
Aurelien DARRAGONc84899c2023-02-20 18:18:59 +01001007
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02001008 To prevent abuses, if the event queue for the current subscription goes
1009 over a certain amount of unconsumed events, the subscription will pause
1010 itself automatically for as long as it takes for your handler to catch up.
1011 This would lead to events being missed, so an error will be reported in the
1012 logs to warn you about that.
1013 This is not something you want to let happen too often, it may indicate
1014 that you subscribed to an event that is occurring too frequently or/and
1015 that your callback function is too slow to keep up the pace and you should
1016 review it.
Aurelien DARRAGONc84899c2023-02-20 18:18:59 +01001017
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02001018 If you want to do some parallel processing because your callback functions
1019 are slow: you might want to create subtasks from lua using
1020 :js:func:`core.register_task()` from within your callback function to
1021 perform the heavy job in a dedicated task and allow remaining events to be
1022 processed more quickly.
Aurelien DARRAGONc84899c2023-02-20 18:18:59 +01001023
Aurelien DARRAGON5bed48f2023-04-21 17:32:46 +02001024.. js:function:: core.disable_legacy_mailers()
1025
1026 **LEGACY**
1027
1028 **context**: body, init
1029
1030 Disable the sending of email alerts through the legacy email sending
1031 function when mailers are used in the configuration.
1032
1033 Use this when sending email alerts directly from lua.
1034
Aurelien DARRAGON717a38d2023-04-26 19:02:43 +02001035 :see: :js:func:`Proxy.get_mailers()`
1036
Thierry Fournierf61aa632016-02-19 20:56:00 +01001037.. _proxy_class:
1038
1039Proxy class
1040============
1041
1042.. js:class:: Proxy
1043
1044 This class provides a way for manipulating proxy and retrieving information
1045 like statistics.
1046
Thierry FOURNIER817e7592017-07-24 14:35:04 +02001047.. js:attribute:: Proxy.name
1048
1049 Contain the name of the proxy.
1050
Aurelien DARRAGONc4b24372023-03-02 12:00:06 +01001051 .. warning::
1052 This attribute is now deprecated and will eventually be removed.
1053 Please use :js:func:`Proxy.get_name()` function instead.
1054
Thierry Fournierb0467732022-10-07 12:07:24 +02001055.. js:function:: Proxy.get_name()
1056
1057 Returns the name of the proxy.
1058
Baptiste Assmann46c72552017-10-26 21:51:58 +02001059.. js:attribute:: Proxy.uuid
1060
1061 Contain the unique identifier of the proxy.
1062
Aurelien DARRAGONc4b24372023-03-02 12:00:06 +01001063 .. warning::
1064 This attribute is now deprecated and will eventually be removed.
1065 Please use :js:func:`Proxy.get_uuid()` function instead.
1066
Thierry Fournierb0467732022-10-07 12:07:24 +02001067.. js:function:: Proxy.get_uuid()
1068
1069 Returns the unique identifier of the proxy.
1070
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001071.. js:attribute:: Proxy.servers
1072
Patrick Hemmerc6a1d712018-05-01 21:30:41 -04001073 Contain a table with the attached servers. The table is indexed by server
1074 name, and each server entry is an object of type :ref:`server_class`.
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001075
Adis Nezirovic8878f8e2018-07-13 12:18:33 +02001076.. js:attribute:: Proxy.stktable
1077
1078 Contains a stick table object attached to the proxy.
1079
Thierry Fournierff480422016-02-25 08:36:46 +01001080.. js:attribute:: Proxy.listeners
1081
Patrick Hemmerc6a1d712018-05-01 21:30:41 -04001082 Contain a table with the attached listeners. The table is indexed by listener
1083 name, and each each listeners entry is an object of type
1084 :ref:`listener_class`.
Thierry Fournierff480422016-02-25 08:36:46 +01001085
Thierry Fournierf61aa632016-02-19 20:56:00 +01001086.. js:function:: Proxy.pause(px)
1087
1088 Pause the proxy. See the management socket documentation for more information.
1089
1090 :param class_proxy px: A :ref:`proxy_class` which indicates the manipulated
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02001091 proxy.
Thierry Fournierf61aa632016-02-19 20:56:00 +01001092
1093.. js:function:: Proxy.resume(px)
1094
1095 Resume the proxy. See the management socket documentation for more
1096 information.
1097
1098 :param class_proxy px: A :ref:`proxy_class` which indicates the manipulated
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02001099 proxy.
Thierry Fournierf61aa632016-02-19 20:56:00 +01001100
1101.. js:function:: Proxy.stop(px)
1102
1103 Stop the proxy. See the management socket documentation for more information.
1104
1105 :param class_proxy px: A :ref:`proxy_class` which indicates the manipulated
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02001106 proxy.
Thierry Fournierf61aa632016-02-19 20:56:00 +01001107
1108.. js:function:: Proxy.shut_bcksess(px)
1109
1110 Kill the session attached to a backup server. See the management socket
1111 documentation for more information.
1112
1113 :param class_proxy px: A :ref:`proxy_class` which indicates the manipulated
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02001114 proxy.
Thierry Fournierf61aa632016-02-19 20:56:00 +01001115
1116.. js:function:: Proxy.get_cap(px)
1117
1118 Returns a string describing the capabilities of the proxy.
1119
1120 :param class_proxy px: A :ref:`proxy_class` which indicates the manipulated
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02001121 proxy.
Thierry Fournierf61aa632016-02-19 20:56:00 +01001122 :returns: a string "frontend", "backend", "proxy" or "ruleset".
1123
1124.. js:function:: Proxy.get_mode(px)
1125
1126 Returns a string describing the mode of the current proxy.
1127
1128 :param class_proxy px: A :ref:`proxy_class` which indicates the manipulated
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02001129 proxy.
Thierry Fournierf61aa632016-02-19 20:56:00 +01001130 :returns: a string "tcp", "http", "health" or "unknown"
1131
Aurelien DARRAGONfc845532023-04-03 11:00:18 +02001132.. js:function:: Proxy.get_srv_act(px)
1133
1134 Returns the number of current active servers for the current proxy that are
1135 eligible for LB.
1136
1137 :param class_proxy px: A :ref:`proxy_class` which indicates the manipulated
1138 proxy.
1139 :returns: an integer
1140
1141.. js:function:: Proxy.get_srv_bck(px)
1142
1143 Returns the number backup servers for the current proxy that are eligible
1144 for LB.
1145
1146 :param class_proxy px: A :ref:`proxy_class` which indicates the manipulated
1147 proxy.
1148 :returns: an integer
1149
Thierry Fournierf61aa632016-02-19 20:56:00 +01001150.. js:function:: Proxy.get_stats(px)
1151
Bertrand Jacquin874a35c2018-09-10 21:26:07 +01001152 Returns a table containing the proxy statistics. The statistics returned are
Thierry Fournierf61aa632016-02-19 20:56:00 +01001153 not the same if the proxy is frontend or a backend.
1154
1155 :param class_proxy px: A :ref:`proxy_class` which indicates the manipulated
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02001156 proxy.
Patrick Hemmerc6a1d712018-05-01 21:30:41 -04001157 :returns: a key/value table containing stats
Thierry Fournierf61aa632016-02-19 20:56:00 +01001158
Aurelien DARRAGON717a38d2023-04-26 19:02:43 +02001159.. js:function:: Proxy.get_mailers(px)
1160
1161 **LEGACY**
1162
1163 Returns a table containing mailers config for the current proxy or nil
1164 if mailers are not available for the proxy.
1165
1166 :param class_proxy px: A :ref:`proxy_class` which indicates the manipulated
1167 proxy.
1168 :returns: a :ref:`proxy_mailers_class` containing proxy mailers config
1169
1170.. _proxy_mailers_class:
1171
1172ProxyMailers class
1173==================
1174
1175**LEGACY**
1176
1177.. js:class:: ProxyMailers
1178
1179 This class provides mailers config for a given proxy.
1180
1181 If sending emails directly from lua, please consider
1182 :js:func:`core.disable_legacy_mailers()` to disable the email sending from
1183 haproxy. (Or email alerts will be sent twice...)
1184
1185.. js:attribute:: ProxyMailers.track_server_health
1186
1187 Boolean set to true if the option "log-health-checks" is configured on
1188 the proxy, meaning that all server checks event should trigger email alerts.
1189
1190.. js:attribute:: ProxyMailers.log_level
1191
1192 An integer, the maximum log level that triggers email alerts. It is a number
1193 between 0 and 7 as defined by option "email-alert level".
1194
1195.. js:attribute:: ProxyMailers.mailservers
1196
1197 An array containing the list of mail servers that should receive email alerts.
1198 Each array entry is a name:desc pair where desc represents the full server
1199 address (including port) as described in haproxy's configuration file.
1200
Aurelien DARRAGON2b8f7ab2023-07-07 16:55:43 +02001201.. js:attribute:: ProxyMailers.mailservers_timeout
1202
1203 An integer representing the maximum time in milliseconds to wait for the
1204 email to be sent. See "timeout mail" directive from "mailers" section in
1205 haproxy configuration file.
1206
Aurelien DARRAGON717a38d2023-04-26 19:02:43 +02001207.. js:attribute:: ProxyMailers.smtp_hostname
1208
1209 A string containing the hostname to use for the SMTP transaction.
1210 (option "email-alert myhostname")
1211
1212.. js:attribute:: ProxyMailers.smtp_from
1213
1214 A string containing the "MAIL FROM" address to use for the SMTP transaction.
1215 (option "email-alert from")
1216
1217.. js:attribute:: ProxyMailers.smtp_to
1218
1219 A string containing the "RCPT TO" address to use for the SMTP transaction.
1220 (option "email-alert to")
1221
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001222.. _server_class:
1223
1224Server class
1225============
1226
Patrick Hemmerc6a1d712018-05-01 21:30:41 -04001227.. js:class:: Server
1228
1229 This class provides a way for manipulating servers and retrieving information.
1230
Patrick Hemmera62ae7e2018-04-29 14:23:48 -04001231.. js:attribute:: Server.name
1232
1233 Contain the name of the server.
1234
Aurelien DARRAGONc4b24372023-03-02 12:00:06 +01001235 .. warning::
1236 This attribute is now deprecated and will eventually be removed.
1237 Please use :js:func:`Server.get_name()` function instead.
1238
Thierry Fournierb0467732022-10-07 12:07:24 +02001239.. js:function:: Server.get_name(sv)
1240
1241 Returns the name of the server.
1242
Patrick Hemmera62ae7e2018-04-29 14:23:48 -04001243.. js:attribute:: Server.puid
1244
1245 Contain the proxy unique identifier of the server.
1246
Aurelien DARRAGONc4b24372023-03-02 12:00:06 +01001247 .. warning::
1248 This attribute is now deprecated and will eventually be removed.
1249 Please use :js:func:`Server.get_puid()` function instead.
1250
Thierry Fournierb0467732022-10-07 12:07:24 +02001251.. js:function:: Server.get_puid(sv)
1252
1253 Returns the proxy unique identifier of the server.
1254
Aurelien DARRAGON94ee6632023-03-10 15:11:27 +01001255.. js:function:: Server.get_rid(sv)
1256
1257 Returns the rid (revision ID) of the server.
1258 It is an unsigned integer that is set upon server creation. Value is derived
1259 from a global counter that starts at 0 and is incremented each time one or
1260 multiple server deletions are followed by a server addition (meaning that
1261 old name/id reuse could occur).
1262
1263 Combining server name/id with server rid yields a process-wide unique
1264 identifier.
1265
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001266.. js:function:: Server.is_draining(sv)
1267
Patrick Hemmerc6a1d712018-05-01 21:30:41 -04001268 Return true if the server is currently draining sticky connections.
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001269
1270 :param class_server sv: A :ref:`server_class` which indicates the manipulated
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02001271 server.
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001272 :returns: a boolean
1273
Aurelien DARRAGONc72051d2023-03-29 10:44:38 +02001274.. js:function:: Server.is_backup(sv)
1275
1276 Return true if the server is a backup server
1277
1278 :param class_server sv: A :ref:`server_class` which indicates the manipulated
1279 server.
1280 :returns: a boolean
1281
Aurelien DARRAGON7a03dee2023-03-29 10:49:30 +02001282.. js:function:: Server.is_dynamic(sv)
1283
1284 Return true if the server was instantiated at runtime (e.g.: from the cli)
1285
1286 :param class_server sv: A :ref:`server_class` which indicates the manipulated
1287 server.
1288 :returns: a boolean
1289
Aurelien DARRAGONfc759b42023-04-03 10:43:17 +02001290.. js:function:: Server.get_cur_sess(sv)
1291
1292 Return the number of currently active sessions on the server
1293
1294 :param class_server sv: A :ref:`server_class` which indicates the manipulated
1295 server.
1296 :returns: an integer
1297
1298.. js:function:: Server.get_pend_conn(sv)
1299
1300 Return the number of pending connections to the server
1301
1302 :param class_server sv: A :ref:`server_class` which indicates the manipulated
1303 server.
1304 :returns: an integer
1305
Patrick Hemmer32d539f2018-04-29 14:25:46 -04001306.. js:function:: Server.set_maxconn(sv, weight)
1307
1308 Dynamically change the maximum connections of the server. See the management
1309 socket documentation for more information about the format of the string.
1310
1311 :param class_server sv: A :ref:`server_class` which indicates the manipulated
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02001312 server.
Patrick Hemmer32d539f2018-04-29 14:25:46 -04001313 :param string maxconn: A string describing the server maximum connections.
1314
1315.. js:function:: Server.get_maxconn(sv, weight)
1316
1317 This function returns an integer representing the server maximum connections.
1318
1319 :param class_server sv: A :ref:`server_class` which indicates the manipulated
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02001320 server.
Patrick Hemmer32d539f2018-04-29 14:25:46 -04001321 :returns: an integer.
1322
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001323.. js:function:: Server.set_weight(sv, weight)
1324
Patrick Hemmerc6a1d712018-05-01 21:30:41 -04001325 Dynamically change the weight of the server. See the management socket
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001326 documentation for more information about the format of the string.
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 :param string weight: A string describing the server weight.
1331
1332.. js:function:: Server.get_weight(sv)
1333
Patrick Hemmerc6a1d712018-05-01 21:30:41 -04001334 This function returns an integer representing the server weight.
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001335
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 :returns: an integer.
1339
Joseph C. Sible49bbf522020-05-04 22:20:32 -04001340.. js:function:: Server.set_addr(sv, addr[, port])
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001341
Patrick Hemmerc6a1d712018-05-01 21:30:41 -04001342 Dynamically change the address of the server. See the management socket
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001343 documentation for more information about the format of the string.
1344
1345 :param class_server sv: A :ref:`server_class` which indicates the manipulated
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02001346 server.
Patrick Hemmerc6a1d712018-05-01 21:30:41 -04001347 :param string addr: A string describing the server address.
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001348
1349.. js:function:: Server.get_addr(sv)
1350
Patrick Hemmerc6a1d712018-05-01 21:30:41 -04001351 Returns a string describing the address of the server.
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001352
1353 :param class_server sv: A :ref:`server_class` which indicates the manipulated
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02001354 server.
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001355 :returns: A string
1356
1357.. js:function:: Server.get_stats(sv)
1358
1359 Returns server statistics.
1360
1361 :param class_server sv: A :ref:`server_class` which indicates the manipulated
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02001362 server.
Patrick Hemmerc6a1d712018-05-01 21:30:41 -04001363 :returns: a key/value table containing stats
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001364
Aurelien DARRAGON3889efa2023-04-03 14:00:58 +02001365.. js:function:: Server.get_proxy(sv)
1366
1367 Returns the parent proxy to which the server belongs.
1368
1369 :param class_server sv: A :ref:`server_class` which indicates the manipulated
1370 server.
1371 :returns: a :ref:`proxy_class` or nil if not available
1372
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001373.. js:function:: Server.shut_sess(sv)
1374
1375 Shutdown all the sessions attached to the server. See the management socket
1376 documentation for more information about this function.
1377
1378 :param class_server sv: A :ref:`server_class` which indicates the manipulated
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02001379 server.
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001380
1381.. js:function:: Server.set_drain(sv)
1382
1383 Drain sticky sessions. See the management socket documentation for more
1384 information about this function.
1385
1386 :param class_server sv: A :ref:`server_class` which indicates the manipulated
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02001387 server.
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001388
1389.. js:function:: Server.set_maint(sv)
1390
1391 Set maintenance mode. See the management socket documentation for more
1392 information about this function.
1393
1394 :param class_server sv: A :ref:`server_class` which indicates the manipulated
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02001395 server.
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001396
1397.. js:function:: Server.set_ready(sv)
1398
1399 Set normal mode. See the management socket documentation for more information
1400 about this function.
1401
1402 :param class_server sv: A :ref:`server_class` which indicates the manipulated
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02001403 server.
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001404
1405.. js:function:: Server.check_enable(sv)
1406
1407 Enable health checks. See the management socket documentation for more
1408 information about this function.
1409
1410 :param class_server sv: A :ref:`server_class` which indicates the manipulated
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02001411 server.
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001412
1413.. js:function:: Server.check_disable(sv)
1414
1415 Disable health checks. See the management socket documentation for more
1416 information about this function.
1417
1418 :param class_server sv: A :ref:`server_class` which indicates the manipulated
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02001419 server.
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001420
1421.. js:function:: Server.check_force_up(sv)
1422
1423 Force health-check up. See the management socket documentation for more
1424 information about this function.
1425
1426 :param class_server sv: A :ref:`server_class` which indicates the manipulated
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02001427 server.
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001428
1429.. js:function:: Server.check_force_nolb(sv)
1430
1431 Force health-check nolb mode. See the management socket documentation for more
1432 information about this function.
1433
1434 :param class_server sv: A :ref:`server_class` which indicates the manipulated
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02001435 server.
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001436
1437.. js:function:: Server.check_force_down(sv)
1438
1439 Force health-check down. See the management socket documentation for more
1440 information about this function.
1441
1442 :param class_server sv: A :ref:`server_class` which indicates the manipulated
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02001443 server.
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001444
1445.. js:function:: Server.agent_enable(sv)
1446
1447 Enable agent check. See the management socket documentation for more
1448 information about this function.
1449
1450 :param class_server sv: A :ref:`server_class` which indicates the manipulated
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02001451 server.
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001452
1453.. js:function:: Server.agent_disable(sv)
1454
1455 Disable agent check. See the management socket documentation for more
1456 information about this function.
1457
1458 :param class_server sv: A :ref:`server_class` which indicates the manipulated
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02001459 server.
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001460
1461.. js:function:: Server.agent_force_up(sv)
1462
1463 Force agent check up. See the management socket documentation for more
1464 information about this function.
1465
1466 :param class_server sv: A :ref:`server_class` which indicates the manipulated
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02001467 server.
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001468
1469.. js:function:: Server.agent_force_down(sv)
1470
1471 Force agent check down. See the management socket documentation for more
1472 information about this function.
1473
1474 :param class_server sv: A :ref:`server_class` which indicates the manipulated
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02001475 server.
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001476
Aurelien DARRAGON406511a2023-03-29 11:30:36 +02001477.. js:function:: Server.tracking(sv)
1478
1479 Check if the current server is tracking another server.
1480
1481 :param class_server sv: A :ref:`server_class` which indicates the manipulated
1482 server.
1483 :returns: A :ref:`server_class` which indicates the tracked server or nil if
1484 the server doesn't track another one.
1485
Aurelien DARRAGON4be36a12023-03-29 14:02:39 +02001486.. js:function:: Server.get_trackers(sv)
1487
1488 Check if the current server is being tracked by other servers.
1489
1490 :param class_server sv: A :ref:`server_class` which indicates the manipulated
1491 server.
1492 :returns: An array of :ref:`server_class` which indicates the tracking
1493 servers (might be empty)
1494
Aurelien DARRAGON223770d2023-03-10 15:34:35 +01001495.. js:function:: Server.event_sub(sv, event_types, func)
1496
1497 Register a function that will be called on specific server events.
1498 It works exactly like :js:func:`core.event_sub()` except that the subscription
1499 will be performed within the server dedicated subscription list instead of the
1500 global one.
1501 (Your callback function will only be called for server events affecting sv)
1502
1503 See :js:func:`core.event_sub()` for function usage.
1504
1505 A key advantage to using :js:func:`Server.event_sub()` over
1506 :js:func:`core.event_sub()` for servers is that :js:func:`Server.event_sub()`
1507 allows you to be notified for servers events of a single server only.
1508 It removes the needs for extra filtering in your callback function if you only
1509 care about a single server, and also prevents useless wakeups.
1510
1511 For instance, if you want to be notified for UP/DOWN events on a given set of
Ilya Shipitsinccf80122023-04-22 20:20:39 +02001512 servers, it is recommended to perform multiple per-server subscriptions since
Aurelien DARRAGON223770d2023-03-10 15:34:35 +01001513 it will be more efficient that doing a single global subscription that will
1514 filter the received events.
1515 Unless you really want to be notified for servers events of ALL servers of
1516 course, which could make sense given you setup but should be avoided if you
1517 have an important number of servers as it will add a significant load on your
1518 haproxy process in case of multiple servers state change in a short amount of
1519 time.
1520
1521 .. Note::
1522 You may also combine :js:func:`core.event_sub()` with
1523 :js:func:`Server.event_sub()`.
1524
1525 Also, don't forget that you can use :js:func:`core.register_task()` from
1526 your callback function if needed. (ie: parallel work)
1527
1528 Here is a working example combining :js:func:`core.event_sub()` with
1529 :js:func:`Server.event_sub()` and :js:func:`core.register_task()`
1530 (This only serves as a demo, this is not necessarily useful to do so)
1531
1532.. code-block:: lua
1533
1534 core.event_sub({"SERVER_ADD"}, function(event, data, sub)
1535 -- in the global event handler
1536 if data["reference"] ~= nil then
1537 print("Tracking new server: ", data["name"])
1538 data["reference"]:event_sub({"SERVER_UP", "SERVER_DOWN"}, function(event, data, sub)
1539 -- in the per-server event handler
1540 if data["reference"] ~= nil then
1541 core.register_task(function(server)
1542 -- subtask to perform some async work (e.g.: HTTP API calls, sending emails...)
1543 print("ASYNC: SERVER ", server:get_name(), " is ", event == "SERVER_UP" and "UP" or "DOWN")
1544 end, data["reference"])
1545 end
1546 end)
1547 end
1548 end)
1549
1550..
1551
1552 In this example, we will first track global server addition events.
1553 For each newly added server ("add server" on the cli), we will register a
1554 UP/DOWN server subscription.
1555 Then, the callback function will schedule the event handling in an async
1556 subtask which will receive the server reference as an argument.
1557
Thierry Fournierff480422016-02-25 08:36:46 +01001558.. _listener_class:
1559
1560Listener class
1561==============
1562
1563.. js:function:: Listener.get_stats(ls)
1564
1565 Returns server statistics.
1566
1567 :param class_listener ls: A :ref:`listener_class` which indicates the
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02001568 manipulated listener.
Patrick Hemmerc6a1d712018-05-01 21:30:41 -04001569 :returns: a key/value table containing stats
Thierry Fournierff480422016-02-25 08:36:46 +01001570
Aurelien DARRAGONc84899c2023-02-20 18:18:59 +01001571.. _event_sub_class:
1572
1573EventSub class
1574==============
1575
1576.. js:function:: EventSub.unsub()
1577
1578 End the subscription, the callback function will not be called again.
1579
1580.. _server_event_class:
1581
1582ServerEvent class
1583=================
1584
Aurelien DARRAGONc4ae8902023-04-17 17:24:48 +02001585.. js:class:: ServerEvent
1586
1587This class is provided with every **SERVER** events.
1588
1589See :js:func:`core.event_sub()` for more info.
1590
Aurelien DARRAGONc84899c2023-02-20 18:18:59 +01001591.. js:attribute:: ServerEvent.name
1592
1593 Contains the name of the server.
1594
1595.. js:attribute:: ServerEvent.puid
1596
1597 Contains the proxy-unique uid of the server
1598
1599.. js:attribute:: ServerEvent.rid
1600
1601 Contains the revision ID of the server
1602
1603.. js:attribute:: ServerEvent.proxy_name
1604
1605 Contains the name of the proxy to which the server belongs
1606
Aurelien DARRAGON55f84c72023-03-22 17:49:04 +01001607.. js:attribute:: ServerEvent.proxy_uuid
1608
1609 Contains the uuid of the proxy to which the server belongs
1610
Aurelien DARRAGONc84899c2023-02-20 18:18:59 +01001611.. js:attribute:: ServerEvent.reference
1612
1613 Reference to the live server (A :ref:`server_class`).
1614
1615 .. Warning::
1616 Not available if the server was removed in the meantime.
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02001617 (Will never be set for SERVER_DEL event since the server does not exist
1618 anymore)
Aurelien DARRAGONc84899c2023-02-20 18:18:59 +01001619
Aurelien DARRAGONc99f3ad2023-04-12 15:47:16 +02001620.. js:attribute:: ServerEvent.state
1621
1622 A :ref:`server_event_state_class`
1623
1624 .. Note::
1625 Only available for SERVER_STATE event
1626
Aurelien DARRAGON948dd3d2023-04-26 11:27:09 +02001627.. js:attribute:: ServerEvent.admin
1628
1629 A :ref:`server_event_admin_class`
1630
1631 .. Note::
1632 Only available for SERVER_ADMIN event
1633
Aurelien DARRAGON0bd53b22023-03-30 15:53:33 +02001634.. js:attribute:: ServerEvent.check
1635
1636 A :ref:`server_event_checkres_class`
1637
1638 .. Note::
1639 Only available for SERVER_CHECK event
1640
Aurelien DARRAGONc99f3ad2023-04-12 15:47:16 +02001641.. _server_event_checkres_class:
1642
1643ServerEventCheckRes class
1644=========================
1645
1646.. js:class:: ServerEventCheckRes
1647
1648This class describes the result of a server's check.
1649
1650.. js:attribute:: ServerEventCheckRes.result
1651
1652 Effective check result.
1653
1654 Check result is a string and will be set to one of the following values:
1655 - "FAILED": the check failed
1656 - "PASSED": the check succeeded
1657 - "CONDPASS": the check conditionally passed
1658
1659.. js:attribute:: ServerEventCheckRes.agent
1660
1661 Boolean set to true if the check is an agent check.
1662 Else it is a health check.
1663
1664.. js:attribute:: ServerEventCheckRes.duration
1665
1666 Check's duration in milliseconds
1667
1668.. js:attribute:: ServerEventCheckRes.reason
1669
1670 Check's status. An array containing three fields:
1671 - **short**: a string representing check status short name
1672 - **desc**: a string representing check status description
1673 - **code**: an integer, this extra information is provided for checks
1674 that went through the data analysis stage (>= layer 5)
1675
1676.. js:attribute:: ServerEventCheckRes.health
1677
1678 An array containing values about check's health (integers):
1679 - **cur**: current health counter:
1680 - 0 to (**rise** - 1) = BAD
1681 - **rise** to (**rise** + **fall** - 1) = GOOD
1682 - **rise**: server will be considered as operational after **rise**
1683 consecutive successful checks
1684 - **fall**: server will be considered as dead after **fall** consecutive
1685 unsuccessful checks
1686
1687.. _server_event_state_class:
1688
1689ServerEventState class
1690======================
1691
1692.. js:class:: ServerEventState
1693
1694This class contains additional info related to **SERVER_STATE** event.
1695
1696.. js:attribute:: ServerEventState.admin
1697
1698 Boolean set to true if the server state change is due to an administrative
1699 change. Else it is an operational change.
1700
1701.. js:attribute:: ServerEventState.check
1702
1703 A :ref:`server_event_checkres_class`, provided if the state change is
1704 due to a server check (must be an operational change).
1705
1706.. js:attribute:: ServerEventState.cause
1707
1708 Printable state change cause. Might be empty.
1709
1710.. js:attribute:: ServerEventState.new_state
1711
1712 New server state due to operational or admin change.
1713
1714 It is a string that can be any of the following values:
1715 - "STOPPED": The server is down
1716 - "STOPPING": The server is up but soft-stopping
1717 - "STARTING": The server is warming up
1718 - "RUNNING": The server is fully up
1719
1720.. js:attribute:: ServerEventState.old_state
1721
1722 Previous server state prior to the operational or admin change.
1723
1724 Can be any value described in **new_state**, but they should differ.
1725
1726.. js:attribute:: ServerEventState.requeued
1727
1728 Number of connections that were requeued due to the server state change.
1729
1730 For a server going DOWN: it is the number of pending server connections
1731 that are requeued to the backend (such connections will be redispatched
1732 to any server that is suitable according to the configured load balancing
1733 algorithm).
1734
1735 For a server doing UP: it is the number of pending connections on the
1736 backend that may be redispatched to the server according to the load
1737 balancing algorithm that is in use.
1738
Aurelien DARRAGON948dd3d2023-04-26 11:27:09 +02001739.. _server_event_admin_class:
1740
1741ServerEventAdmin class
1742======================
1743
1744.. js:class:: ServerEventAdmin
1745
1746This class contains additional info related to **SERVER_ADMIN** event.
1747
1748.. js:attribute:: ServerEventAdmin.cause
1749
1750 Printable admin state change cause. Might be empty.
1751
1752.. js:attribute:: ServerEventAdmin.new_admin
1753
1754 New server admin state due to the admin change.
1755
1756 It is an array of string containing a composition of following values:
1757 - "**MAINT**": server is in maintenance mode
1758 - "FMAINT": server is in forced maintenance mode (MAINT is also set)
1759 - "IMAINT": server is in inherited maintenance mode (MAINT is also set)
1760 - "RMAINT": server is in resolve maintenance mode (MAINT is also set)
1761 - "CMAINT": server is in config maintenance mode (MAINT is also set)
1762 - "**DRAIN**": server is in drain mode
1763 - "FDRAIN": server is in forced drain mode (DRAIN is also set)
1764 - "IDRAIN": server is in inherited drain mode (DRAIN is also set)
1765
1766.. js:attribute:: ServerEventAdmin.old_admin
1767
1768 Previous server admin state prior to the admin change.
1769
1770 Values are presented as in **new_admin**, but they should differ.
1771 (Comparing old and new helps to find out the change(s))
1772
1773.. js:attribute:: ServerEventAdmin.requeued
1774
1775 Same as :js:attr:`ServerEventState.requeued` but when the requeue is due to
1776 the server administrative state change.
1777
Aurelien DARRAGON86fb22c2023-05-03 17:03:09 +02001778.. _queue_class:
1779
1780Queue class
1781===========
1782
1783.. js:class:: Queue
1784
1785 This class provides a generic FIFO storage mechanism that may be shared
1786 between multiple lua contexts to easily pass data between them, as stock
1787 Lua doesn't provide easy methods for passing data between multiple coroutines.
1788
1789 inter-task example:
1790
1791.. code-block:: lua
1792
1793 -- script wide shared queue
1794 local queue = core.queue()
1795
1796 -- master task
1797 core.register_task(function()
1798 -- send the date every second
1799 while true do
1800 queue:push(os.date("%c", core.now().sec))
1801 core.sleep(1)
1802 end
1803 end)
1804
1805 -- worker task
1806 core.register_task(function()
1807 while true do
1808 -- print the date sent by master
1809 print(queue:pop_wait())
1810 end
1811 end)
1812..
1813
1814 Of course, queue may also be used as a local storage mechanism.
1815
1816 Use :js:func:`core.queue` to get a new Queue object.
1817
1818.. js:function:: Queue.size(queue)
1819
1820 This function returns the number of items within the Queue.
1821
1822 :param class_queue queue: A :ref:`queue_class` to the current queue
1823
1824.. js:function:: Queue.push(queue, item)
1825
1826 This function pushes the item (may be of any type) to the queue.
1827 Pushed item cannot be nil or invalid, or an error will be thrown.
1828
1829 :param class_queue queue: A :ref:`queue_class` to the current queue
1830 :returns: boolean true for success and false for error
1831
1832.. js:function:: Queue.pop(queue)
1833
1834 This function immediately tries to pop an item from the queue.
1835 It returns nil of no item is available at the time of the call.
1836
1837 :param class_queue queue: A :ref:`queue_class` to the current queue
1838 :returns: the item at the top of the stack (any type) or nil if no items
1839
1840.. js:function:: Queue.pop_wait(queue)
1841
1842 **context**: task
1843
1844 This is an alternative to pop() that may be used within task contexts.
1845
1846 The call waits for data if no item is currently available. This may be
1847 useful when used in a while loop to prevent cpu waste.
1848
1849 Note that this requires yielding, thus it is only available within contexts
1850 that support yielding (mainly task context).
1851
1852 :param class_queue queue: A :ref:`queue_class` to the current queue
1853 :returns: the item at the top of the stack (any type) or nil in case of error
1854
Thierry Fournier1de16592016-01-27 09:49:07 +01001855.. _concat_class:
1856
1857Concat class
1858============
1859
1860.. js:class:: Concat
1861
1862 This class provides a fast way for string concatenation. The way using native
1863 Lua concatenation like the code below is slow for some reasons.
1864
1865.. code-block:: lua
1866
1867 str = "string1"
1868 str = str .. ", string2"
1869 str = str .. ", string3"
1870..
1871
1872 For each concatenation, Lua:
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02001873 - allocates memory for the result,
1874 - catenates the two string copying the strings in the new memory block,
1875 - frees the old memory block containing the string which is no longer used.
1876
Thierry Fournier1de16592016-01-27 09:49:07 +01001877 This process does many memory move, allocation and free. In addition, the
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02001878 memory is not really freed, it is just marked as unused and waits for the
Thierry Fournier1de16592016-01-27 09:49:07 +01001879 garbage collector.
1880
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02001881 The Concat class provides an alternative way to concatenate strings. It uses
Thierry Fournier1de16592016-01-27 09:49:07 +01001882 the internal Lua mechanism (it does not allocate memory), but it doesn't copy
1883 the data more than once.
1884
1885 On my computer, the following loops spends 0.2s for the Concat method and
1886 18.5s for the pure Lua implementation. So, the Concat class is about 1000x
1887 faster than the embedded solution.
1888
1889.. code-block:: lua
1890
1891 for j = 1, 100 do
1892 c = core.concat()
1893 for i = 1, 20000 do
1894 c:add("#####")
1895 end
1896 end
1897..
1898
1899.. code-block:: lua
1900
1901 for j = 1, 100 do
1902 c = ""
1903 for i = 1, 20000 do
1904 c = c .. "#####"
1905 end
1906 end
1907..
1908
1909.. js:function:: Concat.add(concat, string)
1910
1911 This function adds a string to the current concatenated string.
1912
1913 :param class_concat concat: A :ref:`concat_class` which contains the currently
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02001914 built string.
Bertrand Jacquin874a35c2018-09-10 21:26:07 +01001915 :param string string: A new string to concatenate to the current built
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02001916 string.
Thierry Fournier1de16592016-01-27 09:49:07 +01001917
1918.. js:function:: Concat.dump(concat)
1919
Bertrand Jacquin874a35c2018-09-10 21:26:07 +01001920 This function returns the concatenated string.
Thierry Fournier1de16592016-01-27 09:49:07 +01001921
1922 :param class_concat concat: A :ref:`concat_class` which contains the currently
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02001923 built string.
Thierry Fournier1de16592016-01-27 09:49:07 +01001924 :returns: the concatenated string
1925
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001926.. _fetches_class:
1927
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001928Fetches class
1929=============
1930
1931.. js:class:: Fetches
1932
1933 This class contains a lot of internal HAProxy sample fetches. See the
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02001934 HAProxy "configuration.txt" documentation for more information.
1935 (chapters 7.3.2 to 7.3.6)
Thierry FOURNIER2e4893c2015-03-18 13:37:27 +01001936
Christopher Faulet1e9b1b62021-08-11 10:14:30 +02001937 .. warning::
1938 some sample fetches are not available in some context. These limitations
1939 are specified in this documentation when they're useful.
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001940
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01001941 :see: :js:attr:`TXN.f`
1942 :see: :js:attr:`TXN.sf`
Thierry FOURNIER2e4893c2015-03-18 13:37:27 +01001943
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02001944 Fetches are useful to:
Thierry FOURNIER2e4893c2015-03-18 13:37:27 +01001945
1946 * get system time,
1947 * get environment variable,
1948 * get random numbers,
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02001949 * know backend status like the number of users in queue or the number of
Thierry FOURNIER2e4893c2015-03-18 13:37:27 +01001950 connections established,
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02001951 * get client information like ip source or destination,
Thierry FOURNIER2e4893c2015-03-18 13:37:27 +01001952 * deal with stick tables,
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02001953 * fetch established SSL information,
1954 * fetch HTTP information like headers or method.
Thierry FOURNIER2e4893c2015-03-18 13:37:27 +01001955
1956.. code-block:: lua
1957
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001958 function action(txn)
1959 -- Get source IP
1960 local clientip = txn.f:src()
1961 end
Thierry FOURNIER2e4893c2015-03-18 13:37:27 +01001962..
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001963
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001964.. _converters_class:
1965
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001966Converters class
1967================
1968
1969.. js:class:: Converters
1970
1971 This class contains a lot of internal HAProxy sample converters. See the
Thierry FOURNIER2e4893c2015-03-18 13:37:27 +01001972 HAProxy documentation "configuration.txt" for more information about her
1973 usage. Its the chapter 7.3.1.
1974
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01001975 :see: :js:attr:`TXN.c`
1976 :see: :js:attr:`TXN.sc`
Thierry FOURNIER2e4893c2015-03-18 13:37:27 +01001977
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02001978 Converters provides stateful transformation. They are useful to:
Thierry FOURNIER2e4893c2015-03-18 13:37:27 +01001979
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02001980 * convert input to base64,
1981 * apply hash on input string (djb2, crc32, sdbm, wt6),
Thierry FOURNIER2e4893c2015-03-18 13:37:27 +01001982 * format date,
1983 * json escape,
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02001984 * extract preferred language comparing two lists,
Thierry FOURNIER2e4893c2015-03-18 13:37:27 +01001985 * turn to lower or upper chars,
1986 * deal with stick tables.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001987
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001988.. _channel_class:
1989
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001990Channel class
1991=============
1992
1993.. js:class:: Channel
1994
Christopher Faulet5a2c6612021-08-15 20:35:25 +02001995 **context**: action, sample-fetch, convert, filter
1996
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001997 HAProxy uses two buffers for the processing of the requests. The first one is
1998 used with the request data (from the client to the server) and the second is
1999 used for the response data (from the server to the client).
2000
2001 Each buffer contains two types of data. The first type is the incoming data
2002 waiting for a processing. The second part is the outgoing data already
2003 processed. Usually, the incoming data is processed, after it is tagged as
2004 outgoing data, and finally it is sent. The following functions provides tools
2005 for manipulating these data in a buffer.
2006
2007 The following diagram shows where the channel class function are applied.
2008
Christopher Faulet6a79fc12021-08-06 16:02:36 +02002009 .. image:: _static/channel.png
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002010
Christopher Faulet6a79fc12021-08-06 16:02:36 +02002011 .. warning::
2012 It is not possible to read from the response in request action, and it is
Boyang Li60cfe8b2022-05-10 18:11:00 +00002013 not possible to read from the request channel in response action.
Christopher Faulet09530392021-06-14 11:43:18 +02002014
Christopher Faulet6a79fc12021-08-06 16:02:36 +02002015 .. warning::
2016 It is forbidden to alter the Channels buffer from HTTP contexts. So only
2017 :js:func:`Channel.input`, :js:func:`Channel.output`,
2018 :js:func:`Channel.may_recv`, :js:func:`Channel.is_full` and
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02002019 :js:func:`Channel.is_resp` can be called from a HTTP context.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002020
Christopher Faulet5a2c6612021-08-15 20:35:25 +02002021 All the functions provided by this class are available in the
2022 **sample-fetches**, **actions** and **filters** contexts. For **filters**,
2023 incoming data (offset and length) are relative to the filter. Some functions
Boyang Li60cfe8b2022-05-10 18:11:00 +00002024 may yield, but only for **actions**. Yield is not possible for
Christopher Faulet5a2c6612021-08-15 20:35:25 +02002025 **sample-fetches**, **converters** and **filters**.
Christopher Faulet6a79fc12021-08-06 16:02:36 +02002026
2027.. js:function:: Channel.append(channel, string)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002028
Christopher Faulet6a79fc12021-08-06 16:02:36 +02002029 This function copies the string **string** at the end of incoming data of the
2030 channel buffer. The function returns the copied length on success or -1 if
2031 data cannot be copied.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002032
Christopher Faulet6a79fc12021-08-06 16:02:36 +02002033 Same that :js:func:`Channel.insert(channel, string, channel:input())`.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002034
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002035 :param class_channel channel: The manipulated Channel.
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02002036 :param string string: The data to copy at the end of incoming data.
Christopher Faulet6a79fc12021-08-06 16:02:36 +02002037 :returns: an integer containing the amount of bytes copied or -1.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002038
Christopher Faulet6a79fc12021-08-06 16:02:36 +02002039.. js:function:: Channel.data(channel [, offset [, length]])
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002040
Christopher Faulet6a79fc12021-08-06 16:02:36 +02002041 This function returns **length** bytes of incoming data from the channel
2042 buffer, starting at the offset **offset**. The data are not removed from the
2043 buffer.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002044
Christopher Faulet6a79fc12021-08-06 16:02:36 +02002045 By default, if no length is provided, all incoming data found, starting at the
2046 given offset, are returned. If **length** is set to -1, the function tries to
Christopher Faulet5a2c6612021-08-15 20:35:25 +02002047 retrieve a maximum of data and, if called by an action, it yields if
2048 necessary. It also waits for more data if the requested length exceeds the
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02002049 available amount of incoming data. Not providing an offset is the same as
Ilya Shipitsinff0f2782021-08-22 22:18:07 +05002050 setting it to 0. A positive offset is relative to the beginning of incoming
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02002051 data of the channel buffer while negative offset is relative to the end.
Christopher Faulet6a79fc12021-08-06 16:02:36 +02002052
2053 If there is no incoming data and the channel can't receive more data, a 'nil'
2054 value is returned.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002055
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002056 :param class_channel channel: The manipulated Channel.
Christopher Faulet6a79fc12021-08-06 16:02:36 +02002057 :param integer offset: *optional* The offset in incoming data to start to get
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02002058 data. 0 by default. May be negative to be relative to the end of incoming
2059 data.
Christopher Faulet6a79fc12021-08-06 16:02:36 +02002060 :param integer length: *optional* The expected length of data to retrieve. All
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02002061 incoming data by default. May be set to -1 to get a maximum of data.
Christopher Faulet6a79fc12021-08-06 16:02:36 +02002062 :returns: a string containing the data found or nil.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002063
Christopher Faulet6a79fc12021-08-06 16:02:36 +02002064.. js:function:: Channel.forward(channel, length)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002065
Christopher Faulet6a79fc12021-08-06 16:02:36 +02002066 This function forwards **length** bytes of data from the channel buffer. If
Christopher Faulet5a2c6612021-08-15 20:35:25 +02002067 the requested length exceeds the available amount of incoming data, and if
2068 called by an action, the function yields, waiting for more data to forward. It
2069 returns the amount of data forwarded.
Christopher Faulet6a79fc12021-08-06 16:02:36 +02002070
2071 :param class_channel channel: The manipulated Channel.
2072 :param integer int: The amount of data to forward.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002073
Christopher Faulet6a79fc12021-08-06 16:02:36 +02002074.. js:function:: Channel.input(channel)
2075
Christopher Faulet5a2c6612021-08-15 20:35:25 +02002076 This function returns the length of incoming data in the channel buffer. When
2077 called by a filter, this value is relative to the filter.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002078
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002079 :param class_channel channel: The manipulated Channel.
Christopher Faulet6a79fc12021-08-06 16:02:36 +02002080 :returns: an integer containing the amount of available bytes.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002081
Christopher Faulet6a79fc12021-08-06 16:02:36 +02002082.. js:function:: Channel.insert(channel, string [, offset])
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002083
Christopher Faulet6a79fc12021-08-06 16:02:36 +02002084 This function copies the string **string** at the offset **offset** in
2085 incoming data of the channel buffer. The function returns the copied length on
2086 success or -1 if data cannot be copied.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002087
Christopher Faulet6a79fc12021-08-06 16:02:36 +02002088 By default, if no offset is provided, the string is copied in front of
Ilya Shipitsinff0f2782021-08-22 22:18:07 +05002089 incoming data. A positive offset is relative to the beginning of incoming data
Christopher Faulet6a79fc12021-08-06 16:02:36 +02002090 of the channel buffer while negative offset is relative to their end.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002091
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002092 :param class_channel channel: The manipulated Channel.
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02002093 :param string string: The data to copy into incoming data.
2094 :param integer offset: *optional* The offset in incoming data where to copy
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02002095 data. 0 by default. May be negative to be relative to the end of incoming
2096 data.
Pieter Baauw4d7f7662015-11-08 16:38:08 +01002097 :returns: an integer containing the amount of bytes copied or -1.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002098
Christopher Faulet6a79fc12021-08-06 16:02:36 +02002099.. js:function:: Channel.is_full(channel)
2100
2101 This function returns true if the channel buffer is full.
2102
Christopher Faulet5a2c6612021-08-15 20:35:25 +02002103 :param class_channel channel: The manipulated Channel.
Christopher Faulet6a79fc12021-08-06 16:02:36 +02002104 :returns: a boolean
2105
2106.. js:function:: Channel.is_resp(channel)
2107
2108 This function returns true if the channel is the response one.
2109
Christopher Faulet5a2c6612021-08-15 20:35:25 +02002110 :param class_channel channel: The manipulated Channel.
Christopher Faulet6a79fc12021-08-06 16:02:36 +02002111 :returns: a boolean
2112
2113.. js:function:: Channel.line(channel [, offset [, length]])
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002114
Christopher Faulet6a79fc12021-08-06 16:02:36 +02002115 This function parses **length** bytes of incoming data of the channel buffer,
2116 starting at offset **offset**, and returns the first line found, including the
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02002117 '\\n'. The data are not removed from the buffer. If no line is found, all
2118 data are returned.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002119
Christopher Faulet6a79fc12021-08-06 16:02:36 +02002120 By default, if no length is provided, all incoming data, starting at the given
2121 offset, are evaluated. If **length** is set to -1, the function tries to
Christopher Faulet5a2c6612021-08-15 20:35:25 +02002122 retrieve a maximum of data and, if called by an action, yields if
2123 necessary. It also waits for more data if the requested length exceeds the
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02002124 available amount of incoming data. Not providing an offset is the same as
Ilya Shipitsinff0f2782021-08-22 22:18:07 +05002125 setting it to 0. A positive offset is relative to the beginning of incoming
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02002126 data of the channel buffer while negative offset is relative to the end.
Christopher Faulet6a79fc12021-08-06 16:02:36 +02002127
2128 If there is no incoming data and the channel can't receive more data, a 'nil'
2129 value is returned.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002130
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002131 :param class_channel channel: The manipulated Channel.
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02002132 :param integer offset: *optional* The offset in incoming data to start to
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02002133 parse data. 0 by default. May be negative to be relative to the end of
2134 incoming data.
Christopher Faulet6a79fc12021-08-06 16:02:36 +02002135 :param integer length: *optional* The length of data to parse. All incoming
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02002136 data by default. May be set to -1 to get a maximum of data.
Christopher Faulet6a79fc12021-08-06 16:02:36 +02002137 :returns: a string containing the line found or nil.
2138
2139.. js:function:: Channel.may_recv(channel)
2140
2141 This function returns true if the channel may still receive data.
2142
Christopher Faulet5a2c6612021-08-15 20:35:25 +02002143 :param class_channel channel: The manipulated Channel.
Christopher Faulet6a79fc12021-08-06 16:02:36 +02002144 :returns: a boolean
2145
2146.. js:function:: Channel.output(channel)
2147
Christopher Faulet5a2c6612021-08-15 20:35:25 +02002148 This function returns the length of outgoing data of the channel buffer. When
2149 called by a filter, this value is relative to the filter.
Christopher Faulet6a79fc12021-08-06 16:02:36 +02002150
2151 :param class_channel channel: The manipulated Channel.
2152 :returns: an integer containing the amount of available bytes.
2153
2154.. js:function:: Channel.prepend(channel, string)
2155
2156 This function copies the string **string** in front of incoming data of the
2157 channel buffer. The function returns the copied length on success or -1 if
2158 data cannot be copied.
2159
2160 Same that :js:func:`Channel.insert(channel, string, 0)`.
2161
2162 :param class_channel channel: The manipulated Channel.
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02002163 :param string string: The data to copy in front of incoming data.
Pieter Baauw4d7f7662015-11-08 16:38:08 +01002164 :returns: an integer containing the amount of bytes copied or -1.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002165
Christopher Faulet6a79fc12021-08-06 16:02:36 +02002166.. js:function:: Channel.remove(channel [, offset [, length]])
2167
2168 This function removes **length** bytes of incoming data of the channel buffer,
2169 starting at offset **offset**. This function returns number of bytes removed
2170 on success.
2171
2172 By default, if no length is provided, all incoming data, starting at the given
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02002173 offset, are removed. Not providing an offset is the same as setting it
Ilya Shipitsinff0f2782021-08-22 22:18:07 +05002174 to 0. A positive offset is relative to the beginning of incoming data of the
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02002175 channel buffer while negative offset is relative to the end.
Christopher Faulet6a79fc12021-08-06 16:02:36 +02002176
2177 :param class_channel channel: The manipulated Channel.
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02002178 :param integer offset: *optional* The offset in incoming data where to start
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02002179 to remove data. 0 by default. May be negative to be relative to the end of
2180 incoming data.
Christopher Faulet6a79fc12021-08-06 16:02:36 +02002181 :param integer length: *optional* The length of data to remove. All incoming
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02002182 data by default.
Christopher Faulet6a79fc12021-08-06 16:02:36 +02002183 :returns: an integer containing the amount of bytes removed.
2184
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002185.. js:function:: Channel.send(channel, string)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002186
Christopher Faulet5a2c6612021-08-15 20:35:25 +02002187 This function requires immediate send of the string **string**. It means the
Ilya Shipitsinff0f2782021-08-22 22:18:07 +05002188 string is copied at the beginning of incoming data of the channel buffer and
Christopher Faulet5a2c6612021-08-15 20:35:25 +02002189 immediately forwarded. Unless if the connection is close, and if called by an
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02002190 action, this function yields to copy and forward all the string.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002191
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002192 :param class_channel channel: The manipulated Channel.
Christopher Faulet6a79fc12021-08-06 16:02:36 +02002193 :param string string: The data to send.
Pieter Baauw4d7f7662015-11-08 16:38:08 +01002194 :returns: an integer containing the amount of bytes copied or -1.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002195
Christopher Faulet6a79fc12021-08-06 16:02:36 +02002196.. js:function:: Channel.set(channel, string [, offset [, length]])
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002197
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02002198 This function replaces **length** bytes of incoming data of the channel
2199 buffer, starting at offset **offset**, by the string **string**. The function
2200 returns the copied length on success or -1 if data cannot be copied.
Christopher Faulet6a79fc12021-08-06 16:02:36 +02002201
2202 By default, if no length is provided, all incoming data, starting at the given
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02002203 offset, are replaced. Not providing an offset is the same as setting it
Ilya Shipitsinff0f2782021-08-22 22:18:07 +05002204 to 0. A positive offset is relative to the beginning of incoming data of the
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02002205 channel buffer while negative offset is relative to the end.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002206
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002207 :param class_channel channel: The manipulated Channel.
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02002208 :param string string: The data to copy into incoming data.
2209 :param integer offset: *optional* The offset in incoming data where to start
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02002210 the data replacement. 0 by default. May be negative to be relative to the
2211 end of incoming data.
Christopher Faulet6a79fc12021-08-06 16:02:36 +02002212 :param integer length: *optional* The length of data to replace. All incoming
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02002213 data by default.
Christopher Faulet6a79fc12021-08-06 16:02:36 +02002214 :returns: an integer containing the amount of bytes copied or -1.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002215
Christopher Faulet6a79fc12021-08-06 16:02:36 +02002216.. js:function:: Channel.dup(channel)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002217
Christopher Faulet6a79fc12021-08-06 16:02:36 +02002218 **DEPRECATED**
2219
2220 This function returns all incoming data found in the channel buffer. The data
Boyang Li60cfe8b2022-05-10 18:11:00 +00002221 are not removed from the buffer and can be reprocessed later.
Christopher Faulet6a79fc12021-08-06 16:02:36 +02002222
2223 If there is no incoming data and the channel can't receive more data, a 'nil'
2224 value is returned.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002225
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002226 :param class_channel channel: The manipulated Channel.
Christopher Faulet6a79fc12021-08-06 16:02:36 +02002227 :returns: a string containing all data found or nil.
2228
2229 .. warning::
2230 This function is deprecated. :js:func:`Channel.data()` must be used
2231 instead.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002232
Christopher Faulet6a79fc12021-08-06 16:02:36 +02002233.. js:function:: Channel.get(channel)
2234
2235 **DEPRECATED**
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002236
Christopher Faulet6a79fc12021-08-06 16:02:36 +02002237 This function returns all incoming data found in the channel buffer and remove
2238 them from the buffer.
2239
2240 If there is no incoming data and the channel can't receive more data, a 'nil'
2241 value is returned.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002242
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002243 :param class_channel channel: The manipulated Channel.
Christopher Faulet6a79fc12021-08-06 16:02:36 +02002244 :returns: a string containing all the data found or nil.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002245
Christopher Faulet6a79fc12021-08-06 16:02:36 +02002246 .. warning::
2247 This function is deprecated. :js:func:`Channel.data()` must be used to
2248 retrieve data followed by a call to :js:func:`Channel:remove()` to remove
2249 data.
Thierry FOURNIER / OZON.IO65192f32016-11-07 15:28:40 +01002250
Christopher Faulet6a79fc12021-08-06 16:02:36 +02002251 .. code-block:: lua
Thierry FOURNIER / OZON.IO65192f32016-11-07 15:28:40 +01002252
Christopher Faulet6a79fc12021-08-06 16:02:36 +02002253 local data = chn:data()
2254 chn:remove(0, data:len())
2255
2256 ..
2257
2258.. js:function:: Channel.getline(channel)
2259
2260 **DEPRECATED**
2261
2262 This function returns the first line found in incoming data of the channel
Christopher Faulet5a2c6612021-08-15 20:35:25 +02002263 buffer, including the '\\n'. The returned data are removed from the buffer. If
2264 no line is found, and if called by an action, this function yields to wait for
2265 more data, except if the channel can't receive more data. In this case all
2266 data are returned.
Christopher Faulet6a79fc12021-08-06 16:02:36 +02002267
2268 If there is no incoming data and the channel can't receive more data, a 'nil'
2269 value is returned.
2270
2271 :param class_channel channel: The manipulated Channel.
2272 :returns: a string containing the line found or nil.
2273
2274 .. warning::
Boyang Li60cfe8b2022-05-10 18:11:00 +00002275 This function is deprecated. :js:func:`Channel.line()` must be used to
Christopher Faulet6a79fc12021-08-06 16:02:36 +02002276 retrieve a line followed by a call to :js:func:`Channel:remove()` to remove
2277 data.
2278
2279 .. code-block:: lua
2280
2281 local line = chn:line(0, -1)
2282 chn:remove(0, line:len())
2283
2284 ..
2285
2286.. js:function:: Channel.get_in_len(channel)
2287
Boyang Li60cfe8b2022-05-10 18:11:00 +00002288 **DEPRECATED**
Christopher Faulet6a79fc12021-08-06 16:02:36 +02002289
Christopher Faulet5a2c6612021-08-15 20:35:25 +02002290 This function returns the length of the input part of the buffer. When called
2291 by a filter, this value is relative to the filter.
Christopher Faulet6a79fc12021-08-06 16:02:36 +02002292
2293 :param class_channel channel: The manipulated Channel.
2294 :returns: an integer containing the amount of available bytes.
2295
2296 .. warning::
2297 This function is deprecated. :js:func:`Channel.input()` must be used
2298 instead.
2299
2300.. js:function:: Channel.get_out_len(channel)
2301
Boyang Li60cfe8b2022-05-10 18:11:00 +00002302 **DEPRECATED**
Christopher Faulet6a79fc12021-08-06 16:02:36 +02002303
Christopher Faulet5a2c6612021-08-15 20:35:25 +02002304 This function returns the length of the output part of the buffer. When called
2305 by a filter, this value is relative to the filter.
Christopher Faulet6a79fc12021-08-06 16:02:36 +02002306
2307 :param class_channel channel: The manipulated Channel.
2308 :returns: an integer containing the amount of available bytes.
2309
2310 .. warning::
2311 This function is deprecated. :js:func:`Channel.output()` must be used
2312 instead.
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002313
2314.. _http_class:
Thierry FOURNIER08504f42015-03-16 14:17:08 +01002315
2316HTTP class
2317==========
2318
2319.. js:class:: HTTP
2320
2321 This class contain all the HTTP manipulation functions.
2322
Pieter Baauw386a1272015-08-16 15:26:24 +02002323.. js:function:: HTTP.req_get_headers(http)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01002324
Patrick Hemmerc6a1d712018-05-01 21:30:41 -04002325 Returns a table containing all the request headers.
Thierry FOURNIER08504f42015-03-16 14:17:08 +01002326
2327 :param class_http http: The related http object.
Patrick Hemmerc6a1d712018-05-01 21:30:41 -04002328 :returns: table of headers.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01002329 :see: :js:func:`HTTP.res_get_headers`
Thierry FOURNIER08504f42015-03-16 14:17:08 +01002330
Patrick Hemmerc6a1d712018-05-01 21:30:41 -04002331 This is the form of the returned table:
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002332
2333.. code-block:: lua
2334
2335 HTTP:req_get_headers()['<header-name>'][<header-index>] = "<header-value>"
2336
2337 local hdr = HTTP:req_get_headers()
2338 hdr["host"][0] = "www.test.com"
2339 hdr["accept"][0] = "audio/basic q=1"
2340 hdr["accept"][1] = "audio/*, q=0.2"
2341 hdr["accept"][2] = "*/*, q=0.1"
2342..
2343
Pieter Baauw386a1272015-08-16 15:26:24 +02002344.. js:function:: HTTP.res_get_headers(http)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01002345
Patrick Hemmerc6a1d712018-05-01 21:30:41 -04002346 Returns a table containing all the response headers.
Thierry FOURNIER08504f42015-03-16 14:17:08 +01002347
2348 :param class_http http: The related http object.
Patrick Hemmerc6a1d712018-05-01 21:30:41 -04002349 :returns: table of headers.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01002350 :see: :js:func:`HTTP.req_get_headers`
Thierry FOURNIER08504f42015-03-16 14:17:08 +01002351
Patrick Hemmerc6a1d712018-05-01 21:30:41 -04002352 This is the form of the returned table:
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002353
2354.. code-block:: lua
2355
2356 HTTP:res_get_headers()['<header-name>'][<header-index>] = "<header-value>"
2357
2358 local hdr = HTTP:req_get_headers()
2359 hdr["host"][0] = "www.test.com"
2360 hdr["accept"][0] = "audio/basic q=1"
2361 hdr["accept"][1] = "audio/*, q=0.2"
2362 hdr["accept"][2] = "*.*, q=0.1"
2363..
2364
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002365.. js:function:: HTTP.req_add_header(http, name, value)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01002366
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02002367 Appends a HTTP header field in the request whose name is
Thierry FOURNIER08504f42015-03-16 14:17:08 +01002368 specified in "name" and whose value is defined in "value".
2369
2370 :param class_http http: The related http object.
2371 :param string name: The header name.
2372 :param string value: The header value.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01002373 :see: :js:func:`HTTP.res_add_header`
Thierry FOURNIER08504f42015-03-16 14:17:08 +01002374
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002375.. js:function:: HTTP.res_add_header(http, name, value)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01002376
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02002377 Appends a HTTP header field in the response whose name is
Thierry FOURNIER08504f42015-03-16 14:17:08 +01002378 specified in "name" and whose value is defined in "value".
2379
2380 :param class_http http: The related http object.
2381 :param string name: The header name.
2382 :param string value: The header value.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01002383 :see: :js:func:`HTTP.req_add_header`
Thierry FOURNIER08504f42015-03-16 14:17:08 +01002384
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002385.. js:function:: HTTP.req_del_header(http, name)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01002386
2387 Removes all HTTP header fields in the request whose name is
2388 specified in "name".
2389
2390 :param class_http http: The related http object.
2391 :param string name: The header name.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01002392 :see: :js:func:`HTTP.res_del_header`
Thierry FOURNIER08504f42015-03-16 14:17:08 +01002393
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002394.. js:function:: HTTP.res_del_header(http, name)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01002395
2396 Removes all HTTP header fields in the response whose name is
2397 specified in "name".
2398
2399 :param class_http http: The related http object.
2400 :param string name: The header name.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01002401 :see: :js:func:`HTTP.req_del_header`
Thierry FOURNIER08504f42015-03-16 14:17:08 +01002402
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002403.. js:function:: HTTP.req_set_header(http, name, value)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01002404
Bertrand Jacquin874a35c2018-09-10 21:26:07 +01002405 This variable replace all occurrence of all header "name", by only
Thierry FOURNIER08504f42015-03-16 14:17:08 +01002406 one containing the "value".
2407
2408 :param class_http http: The related http object.
2409 :param string name: The header name.
2410 :param string value: The header value.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01002411 :see: :js:func:`HTTP.res_set_header`
Thierry FOURNIER08504f42015-03-16 14:17:08 +01002412
Bertrand Jacquin874a35c2018-09-10 21:26:07 +01002413 This function does the same work as the following code:
Thierry FOURNIER08504f42015-03-16 14:17:08 +01002414
2415.. code-block:: lua
2416
2417 function fcn(txn)
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002418 TXN.http:req_del_header("header")
2419 TXN.http:req_add_header("header", "value")
Thierry FOURNIER08504f42015-03-16 14:17:08 +01002420 end
2421..
2422
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002423.. js:function:: HTTP.res_set_header(http, name, value)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01002424
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02002425 This function replaces all occurrence of all header "name", by only
Thierry FOURNIER08504f42015-03-16 14:17:08 +01002426 one containing the "value".
2427
2428 :param class_http http: The related http object.
2429 :param string name: The header name.
2430 :param string value: The header value.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01002431 :see: :js:func:`HTTP.req_rep_header()`
Thierry FOURNIER08504f42015-03-16 14:17:08 +01002432
Pieter Baauw386a1272015-08-16 15:26:24 +02002433.. js:function:: HTTP.req_rep_header(http, name, regex, replace)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01002434
2435 Matches the regular expression in all occurrences of header field "name"
2436 according to "regex", and replaces them with the "replace" argument. The
2437 replacement value can contain back references like \1, \2, ... This
2438 function works with the request.
2439
2440 :param class_http http: The related http object.
2441 :param string name: The header name.
2442 :param string regex: The match regular expression.
2443 :param string replace: The replacement value.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01002444 :see: :js:func:`HTTP.res_rep_header()`
Thierry FOURNIER08504f42015-03-16 14:17:08 +01002445
Pieter Baauw386a1272015-08-16 15:26:24 +02002446.. js:function:: HTTP.res_rep_header(http, name, regex, string)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01002447
2448 Matches the regular expression in all occurrences of header field "name"
2449 according to "regex", and replaces them with the "replace" argument. The
2450 replacement value can contain back references like \1, \2, ... This
2451 function works with the request.
2452
2453 :param class_http http: The related http object.
2454 :param string name: The header name.
2455 :param string regex: The match regular expression.
2456 :param string replace: The replacement value.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01002457 :see: :js:func:`HTTP.req_rep_header()`
Thierry FOURNIER08504f42015-03-16 14:17:08 +01002458
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002459.. js:function:: HTTP.req_set_method(http, method)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01002460
2461 Rewrites the request method with the parameter "method".
2462
2463 :param class_http http: The related http object.
2464 :param string method: The new method.
2465
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002466.. js:function:: HTTP.req_set_path(http, path)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01002467
2468 Rewrites the request path with the "path" parameter.
2469
2470 :param class_http http: The related http object.
2471 :param string path: The new path.
2472
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002473.. js:function:: HTTP.req_set_query(http, query)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01002474
2475 Rewrites the request's query string which appears after the first question
2476 mark ("?") with the parameter "query".
2477
2478 :param class_http http: The related http object.
2479 :param string query: The new query.
2480
Thierry FOURNIER0d79cf62015-08-26 14:20:58 +02002481.. js:function:: HTTP.req_set_uri(http, uri)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01002482
2483 Rewrites the request URI with the parameter "uri".
2484
2485 :param class_http http: The related http object.
2486 :param string uri: The new uri.
2487
Robin H. Johnson52f5db22017-01-01 13:10:52 -08002488.. js:function:: HTTP.res_set_status(http, status [, reason])
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +02002489
Robin H. Johnson52f5db22017-01-01 13:10:52 -08002490 Rewrites the response status code with the parameter "code".
2491
2492 If no custom reason is provided, it will be generated from the status.
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +02002493
2494 :param class_http http: The related http object.
2495 :param integer status: The new response status code.
Robin H. Johnson52f5db22017-01-01 13:10:52 -08002496 :param string reason: The new response reason (optional).
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +02002497
William Lallemand00a15022021-11-19 16:02:44 +01002498.. _httpclient_class:
2499
2500HTTPClient class
2501================
2502
2503.. js:class:: HTTPClient
2504
2505 The httpclient class allows issue of outbound HTTP requests through a simple
2506 API without the knowledge of HAProxy internals.
2507
2508.. js:function:: HTTPClient.get(httpclient, request)
2509.. js:function:: HTTPClient.head(httpclient, request)
2510.. js:function:: HTTPClient.put(httpclient, request)
2511.. js:function:: HTTPClient.post(httpclient, request)
2512.. js:function:: HTTPClient.delete(httpclient, request)
2513
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02002514 Send a HTTP request and wait for a response. GET, HEAD PUT, POST and DELETE
2515 methods can be used.
2516 The HTTPClient will send asynchronously the data and is able to send and
2517 receive more than HAProxy bufsize.
William Lallemand00a15022021-11-19 16:02:44 +01002518
William Lallemanda9256192022-10-21 11:48:24 +02002519 The HTTPClient interface is not able to decompress responses, it is not
2520 recommended to send an Accept-Encoding in the request so the response is
2521 received uncompressed.
William Lallemand00a15022021-11-19 16:02:44 +01002522
2523 :param class httpclient: Is the manipulated HTTPClient.
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02002524 :param table request: Is a table containing the parameters of the request
2525 that will be send.
2526 :param string request.url: Is a mandatory parameter for the request that
2527 contains the URL.
2528 :param string request.body: Is an optional parameter for the request that
2529 contains the body to send.
2530 :param table request.headers: Is an optional parameter for the request that
2531 contains the headers to send.
2532 :param string request.dst: Is an optional parameter for the destination in
2533 haproxy address format.
2534 :param integer request.timeout: Optional timeout parameter, set a
2535 "timeout server" on the connections.
William Lallemand00a15022021-11-19 16:02:44 +01002536 :returns: Lua table containing the response
2537
2538
2539.. code-block:: lua
2540
2541 local httpclient = core.httpclient()
William Lallemand4f4f2b72022-02-17 20:00:23 +01002542 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 +01002543
2544..
2545
2546.. code-block:: lua
2547
2548 response = {
2549 status = 400,
2550 reason = "Bad request",
2551 headers = {
2552 ["content-type"] = { "text/html" },
2553 ["cache-control"] = { "no-cache", "no-store" },
2554 },
William Lallemand4f4f2b72022-02-17 20:00:23 +01002555 body = "<html><body><h1>invalid request<h1></body></html>",
William Lallemand00a15022021-11-19 16:02:44 +01002556 }
2557..
2558
2559
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002560.. _txn_class:
2561
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002562TXN class
2563=========
2564
2565.. js:class:: TXN
2566
2567 The txn class contain all the functions relative to the http or tcp
2568 transaction (Note than a tcp stream is the same than a tcp transaction, but
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02002569 a HTTP transaction is not the same than a tcp stream).
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002570
2571 The usage of this class permits to retrieve data from the requests, alter it
2572 and forward it.
2573
2574 All the functions provided by this class are available in the context
Christopher Faulet5a2c6612021-08-15 20:35:25 +02002575 **sample-fetches**, **actions** and **filters**.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002576
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002577.. js:attribute:: TXN.c
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002578
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002579 :returns: An :ref:`converters_class`.
2580
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002581 This attribute contains a Converters class object.
2582
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002583.. js:attribute:: TXN.sc
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002584
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002585 :returns: An :ref:`converters_class`.
2586
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002587 This attribute contains a Converters class object. The functions of
2588 this object returns always a string.
2589
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002590.. js:attribute:: TXN.f
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002591
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002592 :returns: An :ref:`fetches_class`.
2593
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002594 This attribute contains a Fetches class object.
2595
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002596.. js:attribute:: TXN.sf
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002597
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002598 :returns: An :ref:`fetches_class`.
2599
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002600 This attribute contains a Fetches class object. The functions of
2601 this object returns always a string.
2602
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002603.. js:attribute:: TXN.req
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002604
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002605 :returns: An :ref:`channel_class`.
2606
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002607 This attribute contains a channel class object for the request buffer.
2608
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002609.. js:attribute:: TXN.res
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002610
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002611 :returns: An :ref:`channel_class`.
2612
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002613 This attribute contains a channel class object for the response buffer.
2614
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002615.. js:attribute:: TXN.http
Thierry FOURNIER08504f42015-03-16 14:17:08 +01002616
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002617 :returns: An :ref:`http_class`.
2618
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02002619 This attribute contains a HTTP class object. It is available only if the
Thierry FOURNIER08504f42015-03-16 14:17:08 +01002620 proxy has the "mode http" enabled.
2621
Christopher Faulet5a2c6612021-08-15 20:35:25 +02002622.. js:attribute:: TXN.http_req
2623
2624 :returns: An :ref:`httpmessage_class`.
2625
2626 This attribute contains the request HTTPMessage class object. It is available
2627 only if the proxy has the "mode http" enabled and only in the **filters**
2628 context.
2629
2630.. js:attribute:: TXN.http_res
2631
2632 :returns: An :ref:`httpmessage_class`.
2633
2634 This attribute contains the response HTTPMessage class object. It is available
2635 only if the proxy has the "mode http" enabled and only in the **filters**
2636 context.
2637
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01002638.. js:function:: TXN.log(TXN, loglevel, msg)
2639
2640 This function sends a log. The log is sent, according with the HAProxy
2641 configuration file, on the default syslog server if it is configured and on
2642 the stderr if it is allowed.
2643
2644 :param class_txn txn: The class txn object containing the data.
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02002645 :param integer loglevel: Is the log level associated with the message. It is
2646 a number between 0 and 7.
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01002647 :param string msg: The log content.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01002648 :see: :js:attr:`core.emerg`, :js:attr:`core.alert`, :js:attr:`core.crit`,
2649 :js:attr:`core.err`, :js:attr:`core.warning`, :js:attr:`core.notice`,
2650 :js:attr:`core.info`, :js:attr:`core.debug` (log level definitions)
2651 :see: :js:func:`TXN.deflog`
2652 :see: :js:func:`TXN.Debug`
2653 :see: :js:func:`TXN.Info`
2654 :see: :js:func:`TXN.Warning`
2655 :see: :js:func:`TXN.Alert`
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01002656
2657.. js:function:: TXN.deflog(TXN, msg)
2658
Bertrand Jacquin874a35c2018-09-10 21:26:07 +01002659 Sends a log line with the default loglevel for the proxy associated with the
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01002660 transaction.
2661
2662 :param class_txn txn: The class txn object containing the data.
2663 :param string msg: The log content.
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02002664 :see: :js:func:`TXN.log`
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01002665
2666.. js:function:: TXN.Debug(txn, msg)
2667
2668 :param class_txn txn: The class txn object containing the data.
2669 :param string msg: The log content.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01002670 :see: :js:func:`TXN.log`
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01002671
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02002672 Does the same job as:
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01002673
2674.. code-block:: lua
2675
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002676 function Debug(txn, msg)
2677 TXN.log(txn, core.debug, msg)
2678 end
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01002679..
2680
2681.. js:function:: TXN.Info(txn, msg)
2682
2683 :param class_txn txn: The class txn object containing the data.
2684 :param string msg: The log content.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01002685 :see: :js:func:`TXN.log`
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01002686
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02002687 Does the same job as:
2688
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01002689.. code-block:: lua
2690
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02002691 function Info(txn, msg)
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002692 TXN.log(txn, core.info, msg)
2693 end
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01002694..
2695
2696.. js:function:: TXN.Warning(txn, msg)
2697
2698 :param class_txn txn: The class txn object containing the data.
2699 :param string msg: The log content.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01002700 :see: :js:func:`TXN.log`
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01002701
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02002702 Does the same job as:
2703
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01002704.. code-block:: lua
2705
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02002706 function Warning(txn, msg)
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002707 TXN.log(txn, core.warning, msg)
2708 end
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01002709..
2710
2711.. js:function:: TXN.Alert(txn, msg)
2712
2713 :param class_txn txn: The class txn object containing the data.
2714 :param string msg: The log content.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01002715 :see: :js:func:`TXN.log`
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01002716
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02002717 Does the same job as:
2718
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01002719.. code-block:: lua
2720
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02002721 function Alert(txn, msg)
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002722 TXN.log(txn, core.alert, msg)
2723 end
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01002724..
2725
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002726.. js:function:: TXN.get_priv(txn)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002727
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002728 Return Lua data stored in the current transaction (with the `TXN.set_priv()`)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002729 function. If no data are stored, it returns a nil value.
2730
2731 :param class_txn txn: The class txn object containing the data.
Bertrand Jacquin874a35c2018-09-10 21:26:07 +01002732 :returns: the opaque data previously stored, or nil if nothing is
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02002733 available.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002734
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002735.. js:function:: TXN.set_priv(txn, data)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002736
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02002737 Store any data in the current HAProxy transaction. This action replaces the
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002738 old stored data.
2739
2740 :param class_txn txn: The class txn object containing the data.
2741 :param opaque data: The data which is stored in the transaction.
2742
Tim Duesterhus4e172c92020-05-19 13:49:42 +02002743.. js:function:: TXN.set_var(TXN, var, value[, ifexist])
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +02002744
David Carlier61fdf8b2015-10-02 11:59:38 +01002745 Converts a Lua type in a HAProxy type and store it in a variable <var>.
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +02002746
2747 :param class_txn txn: The class txn object containing the data.
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02002748 :param string var: The variable name according with the HAProxy variable
2749 syntax.
2750 :param type value: The value associated to the variable. The type can be
2751 string or integer.
2752 :param boolean ifexist: If this parameter is set to true the variable will
2753 only be set if it was defined elsewhere (i.e. used within the configuration).
2754 For global variables (using the "proc" scope), they will only be updated and
2755 never created. It is highly recommended to always set this to true.
Christopher Faulet85d79c92016-11-09 16:54:56 +01002756
2757.. js:function:: TXN.unset_var(TXN, var)
2758
2759 Unset the variable <var>.
2760
2761 :param class_txn txn: The class txn object containing the data.
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02002762 :param string var: The variable name according with the HAProxy variable
2763 syntax.
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +02002764
2765.. js:function:: TXN.get_var(TXN, var)
2766
2767 Returns data stored in the variable <var> converter in Lua type.
2768
2769 :param class_txn txn: The class txn object containing the data.
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02002770 :param string var: The variable name according with the HAProxy variable
2771 syntax.
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +02002772
Christopher Faulet700d9e82020-01-31 12:21:52 +01002773.. js:function:: TXN.reply([reply])
2774
2775 Return a new reply object
2776
2777 :param table reply: A table containing info to initialize the reply fields.
2778 :returns: A :ref:`reply_class` object.
2779
2780 The table used to initialized the reply object may contain following entries :
2781
2782 * status : The reply status code. the code 200 is used by default.
2783 * reason : The reply reason. The reason corresponding to the status code is
2784 used by default.
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02002785 * headers : A list of headers, indexed by header name. Empty by default. For
Christopher Faulet700d9e82020-01-31 12:21:52 +01002786 a given name, multiple values are possible, stored in an ordered list.
2787 * body : The reply body, empty by default.
2788
2789.. code-block:: lua
2790
2791 local reply = txn:reply{
2792 status = 400,
2793 reason = "Bad request",
2794 headers = {
2795 ["content-type"] = { "text/html" },
2796 ["cache-control"] = {"no-cache", "no-store" }
2797 },
2798 body = "<html><body><h1>invalid request<h1></body></html>"
2799 }
2800..
2801 :see: :js:class:`Reply`
2802
2803.. js:function:: TXN.done(txn[, reply])
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002804
Willy Tarreaubc183a62015-08-28 10:39:11 +02002805 This function terminates processing of the transaction and the associated
Christopher Faulet700d9e82020-01-31 12:21:52 +01002806 session and optionally reply to the client for HTTP sessions.
2807
2808 :param class_txn txn: The class txn object containing the data.
2809 :param class_reply reply: The class reply object to return to the client.
2810
2811 This functions can be used when a critical error is detected or to terminate
Willy Tarreaubc183a62015-08-28 10:39:11 +02002812 processing after some data have been returned to the client (eg: a redirect).
Christopher Faulet700d9e82020-01-31 12:21:52 +01002813 To do so, a reply may be provided. This object is optional and may contain a
2814 status code, a reason, a header list and a body. All these fields are
Christopher Faulet7855b192021-11-09 18:39:51 +01002815 optional. When not provided, the default values are used. By default, with an
2816 empty reply object, an empty HTTP 200 response is returned to the client. If
2817 no reply object is provided, the transaction is terminated without any
2818 reply. If a reply object is provided, it must not exceed the buffer size once
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02002819 converted into the internal HTTP representation. Because for now there is no
Christopher Faulet7855b192021-11-09 18:39:51 +01002820 easy way to be sure it fits, it is probably better to keep it reasonably
2821 small.
Christopher Faulet700d9e82020-01-31 12:21:52 +01002822
2823 The reply object may be fully created in lua or the class Reply may be used to
2824 create it.
2825
2826.. code-block:: lua
2827
2828 local reply = txn:reply()
2829 reply:set_status(400, "Bad request")
2830 reply:add_header("content-type", "text/html")
2831 reply:add_header("cache-control", "no-cache")
2832 reply:add_header("cache-control", "no-store")
2833 reply:set_body("<html><body><h1>invalid request<h1></body></html>")
2834 txn:done(reply)
2835..
2836
2837.. code-block:: lua
2838
2839 txn:done{
2840 status = 400,
2841 reason = "Bad request",
2842 headers = {
2843 ["content-type"] = { "text/html" },
2844 ["cache-control"] = { "no-cache", "no-store" },
2845 },
2846 body = "<html><body><h1>invalid request<h1></body></html>"
2847 }
2848..
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002849
Christopher Faulet1e9b1b62021-08-11 10:14:30 +02002850 .. warning::
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02002851 It does not make sense to call this function from sample-fetches. In this
2852 case the behavior is the same than core.done(): it finishes the Lua
Christopher Faulet1e9b1b62021-08-11 10:14:30 +02002853 execution. The transaction is really aborted only from an action registered
2854 function.
Thierry FOURNIERab00df62016-07-14 11:42:37 +02002855
Christopher Faulet700d9e82020-01-31 12:21:52 +01002856 :see: :js:func:`TXN.reply`, :js:class:`Reply`
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002857
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002858.. js:function:: TXN.set_loglevel(txn, loglevel)
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01002859
2860 Is used to change the log level of the current request. The "loglevel" must
2861 be an integer between 0 and 7.
2862
2863 :param class_txn txn: The class txn object containing the data.
2864 :param integer loglevel: The required log level. This variable can be one of
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01002865 :see: :js:attr:`core.emerg`, :js:attr:`core.alert`, :js:attr:`core.crit`,
2866 :js:attr:`core.err`, :js:attr:`core.warning`, :js:attr:`core.notice`,
2867 :js:attr:`core.info`, :js:attr:`core.debug` (log level definitions)
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01002868
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002869.. js:function:: TXN.set_tos(txn, tos)
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01002870
2871 Is used to set the TOS or DSCP field value of packets sent to the client to
2872 the value passed in "tos" on platforms which support this.
2873
2874 :param class_txn txn: The class txn object containing the data.
2875 :param integer tos: The new TOS os DSCP.
2876
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002877.. js:function:: TXN.set_mark(txn, mark)
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01002878
2879 Is used to set the Netfilter MARK on all packets sent to the client to the
2880 value passed in "mark" on platforms which support it.
2881
2882 :param class_txn txn: The class txn object containing the data.
2883 :param integer mark: The mark value.
2884
Patrick Hemmer268a7072018-05-11 12:52:31 -04002885.. js:function:: TXN.set_priority_class(txn, prio)
2886
2887 This function adjusts the priority class of the transaction. The value should
2888 be within the range -2047..2047. Values outside this range will be
2889 truncated.
2890
2891 See the HAProxy configuration.txt file keyword "http-request" action
2892 "set-priority-class" for details.
2893
2894.. js:function:: TXN.set_priority_offset(txn, prio)
2895
2896 This function adjusts the priority offset of the transaction. The value
2897 should be within the range -524287..524287. Values outside this range will be
2898 truncated.
2899
2900 See the HAProxy configuration.txt file keyword "http-request" action
2901 "set-priority-offset" for details.
2902
Christopher Faulet700d9e82020-01-31 12:21:52 +01002903.. _reply_class:
2904
2905Reply class
2906============
2907
2908.. js:class:: Reply
2909
2910 **context**: action
2911
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02002912 This class represents a HTTP response message. It provides some methods to
Christopher Faulet7855b192021-11-09 18:39:51 +01002913 enrich it. Once converted into the internal HTTP representation, the response
2914 message must not exceed the buffer size. Because for now there is no
2915 easy way to be sure it fits, it is probably better to keep it reasonably
2916 small.
2917
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02002918 See tune.bufsize in the configuration manual for details.
Christopher Faulet700d9e82020-01-31 12:21:52 +01002919
2920.. code-block:: lua
2921
2922 local reply = txn:reply({status = 400}) -- default HTTP 400 reason-phase used
2923 reply:add_header("content-type", "text/html")
2924 reply:add_header("cache-control", "no-cache")
2925 reply:add_header("cache-control", "no-store")
2926 reply:set_body("<html><body><h1>invalid request<h1></body></html>")
2927..
2928
2929 :see: :js:func:`TXN.reply`
2930
2931.. js:attribute:: Reply.status
2932
2933 The reply status code. By default, the status code is set to 200.
2934
2935 :returns: integer
2936
2937.. js:attribute:: Reply.reason
2938
2939 The reason string describing the status code.
2940
2941 :returns: string
2942
2943.. js:attribute:: Reply.headers
2944
2945 A table indexing all reply headers by name. To each name is associated an
2946 ordered list of values.
2947
2948 :returns: Lua table
2949
2950.. code-block:: lua
2951
2952 {
2953 ["content-type"] = { "text/html" },
2954 ["cache-control"] = {"no-cache", "no-store" },
2955 x_header_name = { "value1", "value2", ... }
2956 ...
2957 }
2958..
2959
2960.. js:attribute:: Reply.body
2961
2962 The reply payload.
2963
2964 :returns: string
2965
2966.. js:function:: Reply.set_status(REPLY, status[, reason])
2967
2968 Set the reply status code and optionally the reason-phrase. If the reason is
2969 not provided, the default reason corresponding to the status code is used.
2970
2971 :param class_reply reply: The related Reply object.
2972 :param integer status: The reply status code.
2973 :param string reason: The reply status reason (optional).
2974
2975.. js:function:: Reply.add_header(REPLY, name, value)
2976
2977 Add a header to the reply object. If the header does not already exist, a new
2978 entry is created with its name as index and a one-element list containing its
2979 value as value. Otherwise, the header value is appended to the ordered list of
2980 values associated to the header name.
2981
2982 :param class_reply reply: The related Reply object.
2983 :param string name: The header field name.
2984 :param string value: The header field value.
2985
2986.. js:function:: Reply.del_header(REPLY, name)
2987
2988 Remove all occurrences of a header name from the reply object.
2989
2990 :param class_reply reply: The related Reply object.
2991 :param string name: The header field name.
2992
2993.. js:function:: Reply.set_body(REPLY, body)
2994
2995 Set the reply payload.
2996
2997 :param class_reply reply: The related Reply object.
2998 :param string body: The reply payload.
2999
Thierry FOURNIERdc595002015-12-21 11:13:52 +01003000.. _socket_class:
3001
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01003002Socket class
3003============
3004
3005.. js:class:: Socket
3006
3007 This class must be compatible with the Lua Socket class. Only the 'client'
3008 functions are available. See the Lua Socket documentation:
3009
3010 `http://w3.impa.br/~diego/software/luasocket/tcp.html
3011 <http://w3.impa.br/~diego/software/luasocket/tcp.html>`_
3012
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01003013.. js:function:: Socket.close(socket)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01003014
3015 Closes a TCP object. The internal socket used by the object is closed and the
3016 local address to which the object was bound is made available to other
3017 applications. No further operations (except for further calls to the close
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01003018 method) are allowed on a closed Socket.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01003019
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01003020 :param class_socket socket: Is the manipulated Socket.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01003021
3022 Note: It is important to close all used sockets once they are not needed,
3023 since, in many systems, each socket uses a file descriptor, which are limited
3024 system resources. Garbage-collected objects are automatically closed before
3025 destruction, though.
3026
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02003027.. js:function:: Socket.connect(socket, address[, port])
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01003028
3029 Attempts to connect a socket object to a remote host.
3030
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01003031
3032 In case of error, the method returns nil followed by a string describing the
3033 error. In case of success, the method returns 1.
3034
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01003035 :param class_socket socket: Is the manipulated Socket.
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02003036 :param string address: can be an IP address or a host name. See below for more
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02003037 information.
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02003038 :param integer port: must be an integer number in the range [1..64K].
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01003039 :returns: 1 or nil.
3040
Bertrand Jacquin874a35c2018-09-10 21:26:07 +01003041 An address field extension permits to use the connect() function to connect to
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02003042 other stream than TCP. The syntax containing a simpleipv4 or ipv6 address is
3043 the basically expected format. This format requires the port.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01003044
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02003045 Other format accepted are a socket path like "/socket/path", it permits to
Bertrand Jacquin874a35c2018-09-10 21:26:07 +01003046 connect to a socket. Abstract namespaces are supported with the prefix
Joseph Herlant02cedc42018-11-13 19:45:17 -08003047 "abns@", and finally a file descriptor can be passed with the prefix "fd@".
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02003048 The prefix "ipv4@", "ipv6@" and "unix@" are also supported. The port can be
Bertrand Jacquin874a35c2018-09-10 21:26:07 +01003049 passed int the string. The syntax "127.0.0.1:1234" is valid. In this case, the
Tim Duesterhus6edab862018-01-06 19:04:45 +01003050 parameter *port* must not be set.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01003051
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01003052.. js:function:: Socket.connect_ssl(socket, address, port)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01003053
3054 Same behavior than the function socket:connect, but uses SSL.
3055
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01003056 :param class_socket socket: Is the manipulated Socket.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01003057 :returns: 1 or nil.
3058
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01003059.. js:function:: Socket.getpeername(socket)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01003060
3061 Returns information about the remote side of a connected client object.
3062
3063 Returns a string with the IP address of the peer, followed by the port number
3064 that peer is using for the connection. In case of error, the method returns
3065 nil.
3066
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01003067 :param class_socket socket: Is the manipulated Socket.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01003068 :returns: a string containing the server information.
3069
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01003070.. js:function:: Socket.getsockname(socket)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01003071
3072 Returns the local address information associated to the object.
3073
3074 The method returns a string with local IP address and a number with the port.
3075 In case of error, the method returns nil.
3076
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01003077 :param class_socket socket: Is the manipulated Socket.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01003078 :returns: a string containing the client information.
3079
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01003080.. js:function:: Socket.receive(socket, [pattern [, prefix]])
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01003081
3082 Reads data from a client object, according to the specified read pattern.
3083 Patterns follow the Lua file I/O format, and the difference in performance
3084 between all patterns is negligible.
3085
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01003086 :param class_socket socket: Is the manipulated Socket.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01003087 :param string|integer pattern: Describe what is required (see below).
3088 :param string prefix: A string which will be prefix the returned data.
3089 :returns: a string containing the required data or nil.
3090
3091 Pattern can be any of the following:
3092
3093 * **`*a`**: reads from the socket until the connection is closed. No
3094 end-of-line translation is performed;
3095
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01003096 * **`*l`**: reads a line of text from the Socket. The line is terminated by a
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01003097 LF character (ASCII 10), optionally preceded by a CR character
3098 (ASCII 13). The CR and LF characters are not included in the
3099 returned line. In fact, all CR characters are ignored by the
3100 pattern. This is the default pattern.
3101
3102 * **number**: causes the method to read a specified number of bytes from the
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01003103 Socket. Prefix is an optional string to be concatenated to the
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01003104 beginning of any received data before return.
3105
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02003106 * **empty**: If the pattern is left empty, the default option is `*l`.
3107
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01003108 If successful, the method returns the received pattern. In case of error, the
3109 method returns nil followed by an error message which can be the string
3110 'closed' in case the connection was closed before the transmission was
3111 completed or the string 'timeout' in case there was a timeout during the
3112 operation. Also, after the error message, the function returns the partial
3113 result of the transmission.
3114
3115 Important note: This function was changed severely. It used to support
3116 multiple patterns (but I have never seen this feature used) and now it
3117 doesn't anymore. Partial results used to be returned in the same way as
3118 successful results. This last feature violated the idea that all functions
3119 should return nil on error. Thus it was changed too.
3120
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01003121.. js:function:: Socket.send(socket, data [, start [, end ]])
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01003122
3123 Sends data through client object.
3124
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01003125 :param class_socket socket: Is the manipulated Socket.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01003126 :param string data: The data that will be sent.
3127 :param integer start: The start position in the buffer of the data which will
3128 be sent.
3129 :param integer end: The end position in the buffer of the data which will
3130 be sent.
3131 :returns: see below.
3132
3133 Data is the string to be sent. The optional arguments i and j work exactly
3134 like the standard string.sub Lua function to allow the selection of a
3135 substring to be sent.
3136
3137 If successful, the method returns the index of the last byte within [start,
3138 end] that has been sent. Notice that, if start is 1 or absent, this is
3139 effectively the total number of bytes sent. In case of error, the method
3140 returns nil, followed by an error message, followed by the index of the last
3141 byte within [start, end] that has been sent. You might want to try again from
3142 the byte following that. The error message can be 'closed' in case the
3143 connection was closed before the transmission was completed or the string
3144 'timeout' in case there was a timeout during the operation.
3145
3146 Note: Output is not buffered. For small strings, it is always better to
3147 concatenate them in Lua (with the '..' operator) and send the result in one
3148 call instead of calling the method several times.
3149
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01003150.. js:function:: Socket.setoption(socket, option [, value])
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01003151
3152 Just implemented for compatibility, this cal does nothing.
3153
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01003154.. js:function:: Socket.settimeout(socket, value [, mode])
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01003155
3156 Changes the timeout values for the object. All I/O operations are blocking.
3157 That is, any call to the methods send, receive, and accept will block
3158 indefinitely, until the operation completes. The settimeout method defines a
3159 limit on the amount of time the I/O methods can block. When a timeout time
3160 has elapsed, the affected methods give up and fail with an error code.
3161
3162 The amount of time to wait is specified as the value parameter, in seconds.
3163
Mark Lakes56cc1252018-03-27 09:48:06 +02003164 The timeout modes are not implemented, the only settable timeout is the
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01003165 inactivity time waiting for complete the internal buffer send or waiting for
3166 receive data.
3167
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01003168 :param class_socket socket: Is the manipulated Socket.
Bertrand Jacquin874a35c2018-09-10 21:26:07 +01003169 :param float value: The timeout value. Use floating point to specify
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02003170 milliseconds.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01003171
Thierry FOURNIER31904272017-10-25 12:59:51 +02003172.. _regex_class:
3173
3174Regex class
3175===========
3176
3177.. js:class:: Regex
3178
3179 This class allows the usage of HAProxy regexes because classic lua doesn't
3180 provides regexes. This class inherits the HAProxy compilation options, so the
3181 regexes can be libc regex, pcre regex or pcre JIT regex.
3182
3183 The expression matching number is limited to 20 per regex. The only available
3184 option is case sensitive.
3185
3186 Because regexes compilation is a heavy process, it is better to define all
3187 your regexes in the **body context** and use it during the runtime.
3188
3189.. code-block:: lua
3190
3191 -- Create the regex
3192 st, regex = Regex.new("needle (..) (...)", true);
3193
3194 -- Check compilation errors
3195 if st == false then
3196 print "error: " .. regex
3197 end
3198
3199 -- Match the regexes
3200 print(regex:exec("Looking for a needle in the haystack")) -- true
3201 print(regex:exec("Lokking for a cat in the haystack")) -- false
3202
3203 -- Extract words
3204 st, list = regex:match("Looking for a needle in the haystack")
3205 print(st) -- true
3206 print(list[1]) -- needle in the
3207 print(list[2]) -- in
3208 print(list[3]) -- the
3209
3210.. js:function:: Regex.new(regex, case_sensitive)
3211
3212 Create and compile a regex.
3213
3214 :param string regex: The regular expression according with the libc or pcre
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02003215 standard
Thierry FOURNIER31904272017-10-25 12:59:51 +02003216 :param boolean case_sensitive: Match is case sensitive or not.
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02003217 :returns: boolean status and :ref:`regex_class` or string containing fail
3218 reason.
Thierry FOURNIER31904272017-10-25 12:59:51 +02003219
3220.. js:function:: Regex.exec(regex, str)
3221
3222 Execute the regex.
3223
3224 :param class_regex regex: A :ref:`regex_class` object.
3225 :param string str: The input string will be compared with the compiled regex.
3226 :returns: a boolean status according with the match result.
3227
3228.. js:function:: Regex.match(regex, str)
3229
3230 Execute the regex and return matched expressions.
3231
3232 :param class_map map: A :ref:`regex_class` object.
3233 :param string str: The input string will be compared with the compiled regex.
3234 :returns: a boolean status according with the match result, and
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02003235 a table containing all the string matched in order of declaration.
Thierry FOURNIER31904272017-10-25 12:59:51 +02003236
Thierry FOURNIERdc595002015-12-21 11:13:52 +01003237.. _map_class:
3238
Thierry FOURNIER3def3932015-04-07 11:27:54 +02003239Map class
3240=========
3241
3242.. js:class:: Map
3243
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02003244 This class permits to do some lookups in HAProxy maps. The declared maps can
Bertrand Jacquin874a35c2018-09-10 21:26:07 +01003245 be modified during the runtime through the HAProxy management socket.
Thierry FOURNIER3def3932015-04-07 11:27:54 +02003246
3247.. code-block:: lua
3248
Thierry FOURNIERdc595002015-12-21 11:13:52 +01003249 default = "usa"
Thierry FOURNIER3def3932015-04-07 11:27:54 +02003250
Thierry FOURNIERdc595002015-12-21 11:13:52 +01003251 -- Create and load map
Thierry FOURNIER4dc71972017-01-28 08:33:08 +01003252 geo = Map.new("geo.map", Map._ip);
Thierry FOURNIER3def3932015-04-07 11:27:54 +02003253
Thierry FOURNIERdc595002015-12-21 11:13:52 +01003254 -- Create new fetch that returns the user country
3255 core.register_fetches("country", function(txn)
3256 local src;
3257 local loc;
Thierry FOURNIER3def3932015-04-07 11:27:54 +02003258
Thierry FOURNIERdc595002015-12-21 11:13:52 +01003259 src = txn.f:fhdr("x-forwarded-for");
3260 if (src == nil) then
3261 src = txn.f:src()
3262 if (src == nil) then
3263 return default;
3264 end
3265 end
Thierry FOURNIER3def3932015-04-07 11:27:54 +02003266
Thierry FOURNIERdc595002015-12-21 11:13:52 +01003267 -- Perform lookup
3268 loc = geo:lookup(src);
Thierry FOURNIER3def3932015-04-07 11:27:54 +02003269
Thierry FOURNIERdc595002015-12-21 11:13:52 +01003270 if (loc == nil) then
3271 return default;
3272 end
Thierry FOURNIER3def3932015-04-07 11:27:54 +02003273
Thierry FOURNIERdc595002015-12-21 11:13:52 +01003274 return loc;
3275 end);
Thierry FOURNIER3def3932015-04-07 11:27:54 +02003276
Thierry FOURNIER4dc71972017-01-28 08:33:08 +01003277.. js:attribute:: Map._int
Thierry FOURNIER3def3932015-04-07 11:27:54 +02003278
3279 See the HAProxy configuration.txt file, chapter "Using ACLs and fetching
Ilya Shipitsin2075ca82020-03-06 23:22:22 +05003280 samples" and subchapter "ACL basics" to understand this pattern matching
Thierry FOURNIER3def3932015-04-07 11:27:54 +02003281 method.
3282
Thierry FOURNIER4dc71972017-01-28 08:33:08 +01003283 Note that :js:attr:`Map.int` is also available for compatibility.
3284
3285.. js:attribute:: Map._ip
Thierry FOURNIER3def3932015-04-07 11:27:54 +02003286
3287 See the HAProxy configuration.txt file, chapter "Using ACLs and fetching
Ilya Shipitsin2075ca82020-03-06 23:22:22 +05003288 samples" and subchapter "ACL basics" to understand this pattern matching
Thierry FOURNIER3def3932015-04-07 11:27:54 +02003289 method.
3290
Thierry FOURNIER4dc71972017-01-28 08:33:08 +01003291 Note that :js:attr:`Map.ip` is also available for compatibility.
3292
3293.. js:attribute:: Map._str
Thierry FOURNIER3def3932015-04-07 11:27:54 +02003294
3295 See the HAProxy configuration.txt file, chapter "Using ACLs and fetching
Ilya Shipitsin2075ca82020-03-06 23:22:22 +05003296 samples" and subchapter "ACL basics" to understand this pattern matching
Thierry FOURNIER3def3932015-04-07 11:27:54 +02003297 method.
3298
Thierry FOURNIER4dc71972017-01-28 08:33:08 +01003299 Note that :js:attr:`Map.str` is also available for compatibility.
3300
3301.. js:attribute:: Map._beg
Thierry FOURNIER3def3932015-04-07 11:27:54 +02003302
3303 See the HAProxy configuration.txt file, chapter "Using ACLs and fetching
Ilya Shipitsin2075ca82020-03-06 23:22:22 +05003304 samples" and subchapter "ACL basics" to understand this pattern matching
Thierry FOURNIER3def3932015-04-07 11:27:54 +02003305 method.
3306
Thierry FOURNIER4dc71972017-01-28 08:33:08 +01003307 Note that :js:attr:`Map.beg` is also available for compatibility.
3308
3309.. js:attribute:: Map._sub
Thierry FOURNIER3def3932015-04-07 11:27:54 +02003310
3311 See the HAProxy configuration.txt file, chapter "Using ACLs and fetching
Ilya Shipitsin2075ca82020-03-06 23:22:22 +05003312 samples" and subchapter "ACL basics" to understand this pattern matching
Thierry FOURNIER3def3932015-04-07 11:27:54 +02003313 method.
3314
Thierry FOURNIER4dc71972017-01-28 08:33:08 +01003315 Note that :js:attr:`Map.sub` is also available for compatibility.
3316
3317.. js:attribute:: Map._dir
Thierry FOURNIER3def3932015-04-07 11:27:54 +02003318
3319 See the HAProxy configuration.txt file, chapter "Using ACLs and fetching
Ilya Shipitsin2075ca82020-03-06 23:22:22 +05003320 samples" and subchapter "ACL basics" to understand this pattern matching
Thierry FOURNIER3def3932015-04-07 11:27:54 +02003321 method.
3322
Thierry FOURNIER4dc71972017-01-28 08:33:08 +01003323 Note that :js:attr:`Map.dir` is also available for compatibility.
3324
3325.. js:attribute:: Map._dom
Thierry FOURNIER3def3932015-04-07 11:27:54 +02003326
3327 See the HAProxy configuration.txt file, chapter "Using ACLs and fetching
Ilya Shipitsin2075ca82020-03-06 23:22:22 +05003328 samples" and subchapter "ACL basics" to understand this pattern matching
Thierry FOURNIER3def3932015-04-07 11:27:54 +02003329 method.
3330
Thierry FOURNIER4dc71972017-01-28 08:33:08 +01003331 Note that :js:attr:`Map.dom` is also available for compatibility.
3332
3333.. js:attribute:: Map._end
Thierry FOURNIER3def3932015-04-07 11:27:54 +02003334
3335 See the HAProxy configuration.txt file, chapter "Using ACLs and fetching
Ilya Shipitsin2075ca82020-03-06 23:22:22 +05003336 samples" and subchapter "ACL basics" to understand this pattern matching
Thierry FOURNIER3def3932015-04-07 11:27:54 +02003337 method.
3338
Thierry FOURNIER4dc71972017-01-28 08:33:08 +01003339.. js:attribute:: Map._reg
Thierry FOURNIER3def3932015-04-07 11:27:54 +02003340
3341 See the HAProxy configuration.txt file, chapter "Using ACLs and fetching
Ilya Shipitsin2075ca82020-03-06 23:22:22 +05003342 samples" and subchapter "ACL basics" to understand this pattern matching
Thierry FOURNIER3def3932015-04-07 11:27:54 +02003343 method.
3344
Thierry FOURNIER4dc71972017-01-28 08:33:08 +01003345 Note that :js:attr:`Map.reg` is also available for compatibility.
3346
Thierry FOURNIER3def3932015-04-07 11:27:54 +02003347
3348.. js:function:: Map.new(file, method)
3349
3350 Creates and load a map.
3351
3352 :param string file: Is the file containing the map.
3353 :param integer method: Is the map pattern matching method. See the attributes
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02003354 of the Map class.
Thierry FOURNIER3def3932015-04-07 11:27:54 +02003355 :returns: a class Map object.
Thierry FOURNIER4dc71972017-01-28 08:33:08 +01003356 :see: The Map attributes: :js:attr:`Map._int`, :js:attr:`Map._ip`,
3357 :js:attr:`Map._str`, :js:attr:`Map._beg`, :js:attr:`Map._sub`,
3358 :js:attr:`Map._dir`, :js:attr:`Map._dom`, :js:attr:`Map._end` and
3359 :js:attr:`Map._reg`.
Thierry FOURNIER3def3932015-04-07 11:27:54 +02003360
3361.. js:function:: Map.lookup(map, str)
3362
3363 Perform a lookup in a map.
3364
3365 :param class_map map: Is the class Map object.
3366 :param string str: Is the string used as key.
3367 :returns: a string containing the result or nil if no match.
3368
3369.. js:function:: Map.slookup(map, str)
3370
3371 Perform a lookup in a map.
3372
3373 :param class_map map: Is the class Map object.
3374 :param string str: Is the string used as key.
3375 :returns: a string containing the result or empty string if no match.
3376
Thierry FOURNIERdc595002015-12-21 11:13:52 +01003377.. _applethttp_class:
3378
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02003379AppletHTTP class
Thierry FOURNIERdc595002015-12-21 11:13:52 +01003380================
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02003381
3382.. js:class:: AppletHTTP
3383
3384 This class is used with applets that requires the 'http' mode. The http applet
3385 can be registered with the *core.register_service()* function. They are used
3386 for processing an http request like a server in back of HAProxy.
3387
3388 This is an hello world sample code:
3389
3390.. code-block:: lua
Thierry FOURNIERdc595002015-12-21 11:13:52 +01003391
Pieter Baauw4d7f7662015-11-08 16:38:08 +01003392 core.register_service("hello-world", "http", function(applet)
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02003393 local response = "Hello World !"
3394 applet:set_status(200)
Pieter Baauw2dcb9bc2015-10-01 22:47:12 +02003395 applet:add_header("content-length", string.len(response))
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02003396 applet:add_header("content-type", "text/plain")
Pieter Baauw2dcb9bc2015-10-01 22:47:12 +02003397 applet:start_response()
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02003398 applet:send(response)
3399 end)
3400
Thierry FOURNIERdc595002015-12-21 11:13:52 +01003401.. js:attribute:: AppletHTTP.c
3402
3403 :returns: A :ref:`converters_class`
3404
3405 This attribute contains a Converters class object.
3406
3407.. js:attribute:: AppletHTTP.sc
3408
3409 :returns: A :ref:`converters_class`
3410
3411 This attribute contains a Converters class object. The
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02003412 functions of this object always return a string.
Thierry FOURNIERdc595002015-12-21 11:13:52 +01003413
3414.. js:attribute:: AppletHTTP.f
3415
3416 :returns: A :ref:`fetches_class`
3417
3418 This attribute contains a Fetches class object. Note that the
3419 applet execution place cannot access to a valid HAProxy core HTTP
Ilya Shipitsin2075ca82020-03-06 23:22:22 +05003420 transaction, so some sample fetches related to the HTTP dependent
Thierry FOURNIERdc595002015-12-21 11:13:52 +01003421 values (hdr, path, ...) are not available.
3422
3423.. js:attribute:: AppletHTTP.sf
3424
3425 :returns: A :ref:`fetches_class`
3426
3427 This attribute contains a Fetches class object. The functions of
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02003428 this object always return a string. Note that the applet
Thierry FOURNIERdc595002015-12-21 11:13:52 +01003429 execution place cannot access to a valid HAProxy core HTTP
Ilya Shipitsin2075ca82020-03-06 23:22:22 +05003430 transaction, so some sample fetches related to the HTTP dependent
Thierry FOURNIERdc595002015-12-21 11:13:52 +01003431 values (hdr, path, ...) are not available.
3432
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01003433.. js:attribute:: AppletHTTP.method
Thierry FOURNIERdc595002015-12-21 11:13:52 +01003434
3435 :returns: string
3436
3437 The attribute method returns a string containing the HTTP
3438 method.
3439
3440.. js:attribute:: AppletHTTP.version
3441
3442 :returns: string
3443
3444 The attribute version, returns a string containing the HTTP
3445 request version.
3446
3447.. js:attribute:: AppletHTTP.path
3448
3449 :returns: string
3450
3451 The attribute path returns a string containing the HTTP
3452 request path.
3453
3454.. js:attribute:: AppletHTTP.qs
3455
3456 :returns: string
3457
3458 The attribute qs returns a string containing the HTTP
3459 request query string.
3460
3461.. js:attribute:: AppletHTTP.length
3462
3463 :returns: integer
3464
3465 The attribute length returns an integer containing the HTTP
3466 body length.
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02003467
Thierry FOURNIER841475e2015-12-11 17:10:09 +01003468.. js:attribute:: AppletHTTP.headers
3469
Patrick Hemmerc6a1d712018-05-01 21:30:41 -04003470 :returns: table
Thierry FOURNIERdc595002015-12-21 11:13:52 +01003471
Patrick Hemmerc6a1d712018-05-01 21:30:41 -04003472 The attribute headers returns a table containing the HTTP
Thierry FOURNIERdc595002015-12-21 11:13:52 +01003473 headers. The header names are always in lower case. As the header name can be
3474 encountered more than once in each request, the value is indexed with 0 as
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02003475 first index value. The table has this form:
Thierry FOURNIERdc595002015-12-21 11:13:52 +01003476
3477.. code-block:: lua
3478
3479 AppletHTTP.headers['<header-name>'][<header-index>] = "<header-value>"
3480
3481 AppletHTTP.headers["host"][0] = "www.test.com"
3482 AppletHTTP.headers["accept"][0] = "audio/basic q=1"
3483 AppletHTTP.headers["accept"][1] = "audio/*, q=0.2"
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01003484 AppletHTTP.headers["accept"][2] = "*/*, q=0.1"
Thierry FOURNIERdc595002015-12-21 11:13:52 +01003485..
3486
Robin H. Johnson52f5db22017-01-01 13:10:52 -08003487.. js:function:: AppletHTTP.set_status(applet, code [, reason])
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02003488
3489 This function sets the HTTP status code for the response. The allowed code are
3490 from 100 to 599.
3491
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01003492 :param class_AppletHTTP applet: An :ref:`applethttp_class`
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02003493 :param integer code: the status code returned to the client.
Robin H. Johnson52f5db22017-01-01 13:10:52 -08003494 :param string reason: the status reason returned to the client (optional).
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02003495
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01003496.. js:function:: AppletHTTP.add_header(applet, name, value)
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02003497
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02003498 This function adds a header in the response. Duplicated headers are not
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02003499 collapsed. The special header *content-length* is used to determinate the
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02003500 response length. If it does not exist, a *transfer-encoding: chunked* is set,
3501 and all the write from the function *AppletHTTP:send()* become a chunk.
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02003502
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01003503 :param class_AppletHTTP applet: An :ref:`applethttp_class`
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02003504 :param string name: the header name
3505 :param string value: the header value
3506
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01003507.. js:function:: AppletHTTP.start_response(applet)
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02003508
3509 This function indicates to the HTTP engine that it can process and send the
3510 response headers. After this called we cannot add headers to the response; We
3511 cannot use the *AppletHTTP:send()* function if the
3512 *AppletHTTP:start_response()* is not called.
3513
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01003514 :param class_AppletHTTP applet: An :ref:`applethttp_class`
3515
3516.. js:function:: AppletHTTP.getline(applet)
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02003517
3518 This function returns a string containing one line from the http body. If the
3519 data returned doesn't contains a final '\\n' its assumed than its the last
3520 available data before the end of stream.
3521
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01003522 :param class_AppletHTTP applet: An :ref:`applethttp_class`
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02003523 :returns: a string. The string can be empty if we reach the end of the stream.
3524
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01003525.. js:function:: AppletHTTP.receive(applet, [size])
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02003526
3527 Reads data from the HTTP body, according to the specified read *size*. If the
3528 *size* is missing, the function tries to read all the content of the stream
3529 until the end. If the *size* is bigger than the http body, it returns the
Bertrand Jacquin874a35c2018-09-10 21:26:07 +01003530 amount of data available.
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02003531
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01003532 :param class_AppletHTTP applet: An :ref:`applethttp_class`
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02003533 :param integer size: the required read size.
Ilya Shipitsin11057a32020-06-21 21:18:27 +05003534 :returns: always return a string,the string can be empty is the connection is
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02003535 closed.
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02003536
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01003537.. js:function:: AppletHTTP.send(applet, msg)
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02003538
3539 Send the message *msg* on the http request body.
3540
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01003541 :param class_AppletHTTP applet: An :ref:`applethttp_class`
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02003542 :param string msg: the message to send.
3543
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01003544.. js:function:: AppletHTTP.get_priv(applet)
3545
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01003546 Return Lua data stored in the current transaction. If no data are stored,
3547 it returns a nil value.
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01003548
3549 :param class_AppletHTTP applet: An :ref:`applethttp_class`
Bertrand Jacquin874a35c2018-09-10 21:26:07 +01003550 :returns: the opaque data previously stored, or nil if nothing is
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02003551 available.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01003552 :see: :js:func:`AppletHTTP.set_priv`
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01003553
3554.. js:function:: AppletHTTP.set_priv(applet, data)
3555
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02003556 Store any data in the current HAProxy transaction. This action replaces the
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01003557 old stored data.
3558
3559 :param class_AppletHTTP applet: An :ref:`applethttp_class`
3560 :param opaque data: The data which is stored in the transaction.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01003561 :see: :js:func:`AppletHTTP.get_priv`
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01003562
Tim Duesterhus4e172c92020-05-19 13:49:42 +02003563.. js:function:: AppletHTTP.set_var(applet, var, value[, ifexist])
Thierry FOURNIER / OZON.IOc1edafe2016-12-12 16:25:30 +01003564
3565 Converts a Lua type in a HAProxy type and store it in a variable <var>.
3566
3567 :param class_AppletHTTP applet: An :ref:`applethttp_class`
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02003568 :param string var: The variable name according with the HAProxy variable
3569 syntax.
3570 :param type value: The value associated to the variable. The type ca be string
3571 or integer.
3572 :param boolean ifexist: If this parameter is set to true the variable will
3573 only be set if it was defined elsewhere (i.e. used within the configuration).
3574 For global variables (using the "proc" scope), they will only be updated and
3575 never created. It is highly recommended to always set this to true.
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02003576
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01003577 :see: :js:func:`AppletHTTP.unset_var`
3578 :see: :js:func:`AppletHTTP.get_var`
Thierry FOURNIER / OZON.IOc1edafe2016-12-12 16:25:30 +01003579
3580.. js:function:: AppletHTTP.unset_var(applet, var)
3581
3582 Unset the variable <var>.
3583
3584 :param class_AppletHTTP applet: An :ref:`applethttp_class`
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02003585 :param string var: The variable name according with the HAProxy variable
3586 syntax.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01003587 :see: :js:func:`AppletHTTP.set_var`
3588 :see: :js:func:`AppletHTTP.get_var`
Thierry FOURNIER / OZON.IOc1edafe2016-12-12 16:25:30 +01003589
3590.. js:function:: AppletHTTP.get_var(applet, var)
3591
3592 Returns data stored in the variable <var> converter in Lua type.
3593
3594 :param class_AppletHTTP applet: An :ref:`applethttp_class`
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02003595 :param string var: The variable name according with the HAProxy variable
3596 syntax.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01003597 :see: :js:func:`AppletHTTP.set_var`
3598 :see: :js:func:`AppletHTTP.unset_var`
Thierry FOURNIER / OZON.IOc1edafe2016-12-12 16:25:30 +01003599
Thierry FOURNIERdc595002015-12-21 11:13:52 +01003600.. _applettcp_class:
3601
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02003602AppletTCP class
3603===============
3604
3605.. js:class:: AppletTCP
3606
3607 This class is used with applets that requires the 'tcp' mode. The tcp applet
3608 can be registered with the *core.register_service()* function. They are used
3609 for processing a tcp stream like a server in back of HAProxy.
3610
Thierry FOURNIERdc595002015-12-21 11:13:52 +01003611.. js:attribute:: AppletTCP.c
3612
3613 :returns: A :ref:`converters_class`
3614
3615 This attribute contains a Converters class object.
3616
3617.. js:attribute:: AppletTCP.sc
3618
3619 :returns: A :ref:`converters_class`
3620
3621 This attribute contains a Converters class object. The
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02003622 functions of this object always return a string.
Thierry FOURNIERdc595002015-12-21 11:13:52 +01003623
3624.. js:attribute:: AppletTCP.f
3625
3626 :returns: A :ref:`fetches_class`
3627
3628 This attribute contains a Fetches class object.
3629
3630.. js:attribute:: AppletTCP.sf
3631
3632 :returns: A :ref:`fetches_class`
3633
3634 This attribute contains a Fetches class object.
3635
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01003636.. js:function:: AppletTCP.getline(applet)
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02003637
3638 This function returns a string containing one line from the stream. If the
3639 data returned doesn't contains a final '\\n' its assumed than its the last
3640 available data before the end of stream.
3641
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01003642 :param class_AppletTCP applet: An :ref:`applettcp_class`
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02003643 :returns: a string. The string can be empty if we reach the end of the stream.
3644
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01003645.. js:function:: AppletTCP.receive(applet, [size])
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02003646
3647 Reads data from the TCP stream, according to the specified read *size*. If the
3648 *size* is missing, the function tries to read all the content of the stream
3649 until the end.
3650
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01003651 :param class_AppletTCP applet: An :ref:`applettcp_class`
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02003652 :param integer size: the required read size.
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02003653 :returns: always return a string, the string can be empty if the connection is
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02003654 closed.
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02003655
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01003656.. js:function:: AppletTCP.send(appletmsg)
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02003657
3658 Send the message on the stream.
3659
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01003660 :param class_AppletTCP applet: An :ref:`applettcp_class`
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02003661 :param string msg: the message to send.
3662
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01003663.. js:function:: AppletTCP.get_priv(applet)
3664
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01003665 Return Lua data stored in the current transaction. If no data are stored,
3666 it returns a nil value.
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01003667
3668 :param class_AppletTCP applet: An :ref:`applettcp_class`
Bertrand Jacquin874a35c2018-09-10 21:26:07 +01003669 :returns: the opaque data previously stored, or nil if nothing is
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02003670 available.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01003671 :see: :js:func:`AppletTCP.set_priv`
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01003672
3673.. js:function:: AppletTCP.set_priv(applet, data)
3674
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02003675 Store any data in the current HAProxy transaction. This action replaces the
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01003676 old stored data.
3677
3678 :param class_AppletTCP applet: An :ref:`applettcp_class`
3679 :param opaque data: The data which is stored in the transaction.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01003680 :see: :js:func:`AppletTCP.get_priv`
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01003681
Tim Duesterhus4e172c92020-05-19 13:49:42 +02003682.. js:function:: AppletTCP.set_var(applet, var, value[, ifexist])
Thierry FOURNIER / OZON.IOc1edafe2016-12-12 16:25:30 +01003683
3684 Converts a Lua type in a HAProxy type and stores it in a variable <var>.
3685
3686 :param class_AppletTCP applet: An :ref:`applettcp_class`
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02003687 :param string var: The variable name according with the HAProxy variable
3688 syntax.
3689 :param type value: The value associated to the variable. The type can be
3690 string or integer.
3691 :param boolean ifexist: If this parameter is set to true the variable will
3692 only be set if it was defined elsewhere (i.e. used within the configuration).
3693 For global variables (using the "proc" scope), they will only be updated and
3694 never created. It is highly recommended to always set this to true.
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02003695
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01003696 :see: :js:func:`AppletTCP.unset_var`
3697 :see: :js:func:`AppletTCP.get_var`
Thierry FOURNIER / OZON.IOc1edafe2016-12-12 16:25:30 +01003698
3699.. js:function:: AppletTCP.unset_var(applet, var)
3700
3701 Unsets the variable <var>.
3702
3703 :param class_AppletTCP applet: An :ref:`applettcp_class`
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02003704 :param string var: The variable name according with the HAProxy variable
3705 syntax.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01003706 :see: :js:func:`AppletTCP.unset_var`
3707 :see: :js:func:`AppletTCP.set_var`
Thierry FOURNIER / OZON.IOc1edafe2016-12-12 16:25:30 +01003708
3709.. js:function:: AppletTCP.get_var(applet, var)
3710
3711 Returns data stored in the variable <var> converter in Lua type.
3712
3713 :param class_AppletTCP applet: An :ref:`applettcp_class`
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02003714 :param string var: The variable name according with the HAProxy variable
3715 syntax.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01003716 :see: :js:func:`AppletTCP.unset_var`
3717 :see: :js:func:`AppletTCP.set_var`
Thierry FOURNIER / OZON.IOc1edafe2016-12-12 16:25:30 +01003718
Adis Nezirovic8878f8e2018-07-13 12:18:33 +02003719StickTable class
3720================
3721
3722.. js:class:: StickTable
3723
3724 **context**: task, action, sample-fetch
3725
3726 This class can be used to access the HAProxy stick tables from Lua.
3727
3728.. js:function:: StickTable.info()
3729
3730 Returns stick table attributes as a Lua table. See HAProxy documentation for
Ilya Shipitsin2272d8a2020-12-21 01:22:40 +05003731 "stick-table" for canonical info, or check out example below.
Adis Nezirovic8878f8e2018-07-13 12:18:33 +02003732
3733 :returns: Lua table
3734
3735 Assume our table has IPv4 key and gpc0 and conn_rate "columns":
3736
3737.. code-block:: lua
3738
3739 {
3740 expire=<int>, # Value in ms
3741 size=<int>, # Maximum table size
3742 used=<int>, # Actual number of entries in table
3743 data={ # Data columns, with types as key, and periods as values
3744 (-1 if type is not rate counter)
3745 conn_rate=<int>,
3746 gpc0=-1
3747 },
3748 length=<int>, # max string length for string table keys, key length
3749 # otherwise
3750 nopurge=<boolean>, # purge oldest entries when table is full
3751 type="ip" # can be "ip", "ipv6", "integer", "string", "binary"
3752 }
3753
3754.. js:function:: StickTable.lookup(key)
3755
3756 Returns stick table entry for given <key>
3757
3758 :param string key: Stick table key (IP addresses and strings are supported)
3759 :returns: Lua table
3760
3761.. js:function:: StickTable.dump([filter])
3762
3763 Returns all entries in stick table. An optional filter can be used
3764 to extract entries with specific data values. Filter is a table with valid
3765 comparison operators as keys followed by data type name and value pairs.
3766 Check out the HAProxy docs for "show table" for more details. For the
3767 reference, the supported operators are:
Aurelien DARRAGON21f7ebb2023-03-13 19:49:31 +01003768
Adis Nezirovic8878f8e2018-07-13 12:18:33 +02003769 "eq", "ne", "le", "lt", "ge", "gt"
3770
3771 For large tables, execution of this function can take a long time (for
3772 HAProxy standards). That's also true when filter is used, so take care and
3773 measure the impact.
3774
3775 :param table filter: Stick table filter
3776 :returns: Stick table entries (table)
3777
3778 See below for example filter, which contains 4 entries (or comparisons).
3779 (Maximum number of filter entries is 4, defined in the source code)
3780
3781.. code-block:: lua
3782
3783 local filter = {
3784 {"gpc0", "gt", 30}, {"gpc1", "gt", 20}}, {"conn_rate", "le", 10}
3785 }
3786
Christopher Faulet0f3c8902020-01-31 18:57:12 +01003787.. _action_class:
3788
3789Action class
3790=============
3791
3792.. js:class:: Act
3793
3794 **context**: action
3795
3796 This class contains all return codes an action may return. It is the lua
3797 equivalent to HAProxy "ACT_RET_*" code.
3798
3799.. code-block:: lua
3800
3801 core.register_action("deny", { "http-req" }, function (txn)
3802 return act.DENY
3803 end)
3804..
3805.. js:attribute:: act.CONTINUE
3806
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02003807 This attribute is an integer (0). It instructs HAProxy to continue the
3808 current ruleset processing on the message. It is the default return code
3809 for a lua action.
Christopher Faulet0f3c8902020-01-31 18:57:12 +01003810
3811 :returns: integer
3812
3813.. js:attribute:: act.STOP
3814
3815 This attribute is an integer (1). It instructs HAProxy to stop the current
3816 ruleset processing on the message.
3817
3818.. js:attribute:: act.YIELD
3819
3820 This attribute is an integer (2). It instructs HAProxy to temporarily pause
3821 the message processing. It will be resumed later on the same rule. The
3822 corresponding lua script is re-executed for the start.
3823
3824.. js:attribute:: act.ERROR
3825
3826 This attribute is an integer (3). It triggers an internal errors The message
3827 processing is stopped and the transaction is terminated. For HTTP streams, an
3828 HTTP 500 error is returned to the client.
3829
3830 :returns: integer
3831
3832.. js:attribute:: act.DONE
3833
3834 This attribute is an integer (4). It instructs HAProxy to stop the message
3835 processing.
3836
3837 :returns: integer
3838
3839.. js:attribute:: act.DENY
3840
3841 This attribute is an integer (5). It denies the current message. The message
3842 processing is stopped and the transaction is terminated. For HTTP streams, an
3843 HTTP 403 error is returned to the client if the deny is returned during the
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02003844 request analysis. During the response analysis, a HTTP 502 error is returned
Christopher Faulet0f3c8902020-01-31 18:57:12 +01003845 and the server response is discarded.
3846
3847 :returns: integer
3848
3849.. js:attribute:: act.ABORT
3850
3851 This attribute is an integer (6). It aborts the current message. The message
3852 processing is stopped and the transaction is terminated. For HTTP streams,
Willy Tarreau714f3452021-05-09 06:47:26 +02003853 HAProxy assumes a response was already sent to the client. From the Lua
Christopher Faulet0f3c8902020-01-31 18:57:12 +01003854 actions point of view, when this code is used, the transaction is terminated
3855 with no reply.
3856
3857 :returns: integer
3858
3859.. js:attribute:: act.INVALID
3860
3861 This attribute is an integer (7). It triggers an internal errors. The message
3862 processing is stopped and the transaction is terminated. For HTTP streams, an
3863 HTTP 400 error is returned to the client if the error is returned during the
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02003864 request analysis. During the response analysis, a HTTP 502 error is returned
Christopher Faulet0f3c8902020-01-31 18:57:12 +01003865 and the server response is discarded.
3866
3867 :returns: integer
Adis Nezirovic8878f8e2018-07-13 12:18:33 +02003868
Christopher Faulet2c2c2e32020-01-31 19:07:52 +01003869.. js:function:: act:wake_time(milliseconds)
3870
3871 **context**: action
3872
3873 Set the script pause timeout to the specified time, defined in
3874 milliseconds.
3875
3876 :param integer milliseconds: the required milliseconds.
3877
3878 This function may be used when a lua action returns `act.YIELD`, to force its
3879 wake-up at most after the specified number of milliseconds.
3880
Christopher Faulet5a2c6612021-08-15 20:35:25 +02003881.. _filter_class:
3882
3883Filter class
3884=============
3885
3886.. js:class:: filter
3887
3888 **context**: filter
3889
3890 This class contains return codes some filter callback functions may return. It
3891 also contains configuration flags and some helper functions. To understand how
3892 the filter API works, see `doc/internal/filters.txt` documentation.
3893
3894.. js:attribute:: filter.CONTINUE
3895
3896 This attribute is an integer (1). It may be returned by some filter callback
3897 functions to instruct this filtering step is finished for this filter.
3898
3899.. js:attribute:: filter.WAIT
3900
3901 This attribute is an integer (0). It may be returned by some filter callback
3902 functions to instruct the filtering must be paused, waiting for more data or
3903 for an external event depending on this filter.
3904
3905.. js:attribute:: filter.ERROR
3906
3907 This attribute is an integer (-1). It may be returned by some filter callback
3908 functions to trigger an error.
3909
3910.. js:attribute:: filter.FLT_CFG_FL_HTX
3911
3912 This attribute is a flag corresponding to the filter flag FLT_CFG_FL_HTX. When
3913 it is set for a filter, it means the filter is able to filter HTTP streams.
3914
3915.. js:function:: filter.register_data_filter(chn)
3916
3917 **context**: filter
3918
3919 Enable the data filtering on the channel **chn** for the current filter. It
Ilya Shipitsinff0f2782021-08-22 22:18:07 +05003920 may be called at any time from any callback functions proceeding the data
Christopher Faulet5a2c6612021-08-15 20:35:25 +02003921 analysis.
3922
3923 :param class_Channel chn: A :ref:`channel_class`.
3924
3925.. js:function:: filter.unregister_data_filter(chn)
3926
3927 **context**: filter
3928
3929 Disable the data filtering on the channel **chn** for the current filter. It
3930 may be called at any time from any callback functions.
3931
3932 :param class_Channel chn: A :ref:`channel_class`.
3933
3934.. js:function:: filter.wake_time(milliseconds)
3935
3936 **context**: filter
3937
3938 Set the script pause timeout to the specified time, defined in
3939 milliseconds.
3940
3941 :param integer milliseconds: the required milliseconds.
3942
3943 This function may be used from any lua filter callback function to force its
3944 wake-up at most after the specified number of milliseconds. Especially, when
3945 `filter.CONTINUE` is returned.
3946
3947
3948A filters is declared using :js:func:`core.register_filter()` function. The
3949provided class will be used to instantiate filters. It may define following
3950attributes:
3951
3952* id: The filter identifier. It is a string that identifies the filter and is
3953 optional.
3954
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02003955* flags: The filter flags. Only :js:attr:`filter.FLT_CFG_FL_HTX` may be set
3956 for now.
Christopher Faulet5a2c6612021-08-15 20:35:25 +02003957
3958Such filter class must also define all required callback functions in the
3959following list. Note that :js:func:`Filter.new()` must be defined otherwise the
Ilya Shipitsinff0f2782021-08-22 22:18:07 +05003960filter is ignored. Others are optional.
Christopher Faulet5a2c6612021-08-15 20:35:25 +02003961
3962* .. js:function:: FILTER.new()
3963
3964 Called to instantiate a new filter. This function must be defined.
3965
3966 :returns: a Lua object that will be used as filter instance for the current
3967 stream.
3968
3969* .. js:function:: FILTER.start_analyze(flt, txn, chn)
3970
3971 Called when the analysis starts on the channel **chn**.
3972
3973* .. js:function:: FILTER.end_analyze(flt, txn, chn)
3974
3975 Called when the analysis ends on the channel **chn**.
3976
3977* .. js:function:: FILTER.http_headers(flt, txn, http_msg)
3978
3979 Called just before the HTTP payload analysis and after any processing on the
3980 HTTP message **http_msg**. This callback functions is only called for HTTP
3981 streams.
3982
3983* .. js:function:: FILTER.http_payload(flt, txn, http_msg)
3984
3985 Called during the HTTP payload analysis on the HTTP message **http_msg**. This
3986 callback functions is only called for HTTP streams.
3987
3988* .. js:function:: FILTER.http_end(flt, txn, http_msg)
3989
3990 Called after the HTTP payload analysis on the HTTP message **http_msg**. This
3991 callback functions is only called for HTTP streams.
3992
3993* .. js:function:: FILTER.tcp_payload(flt, txn, chn)
3994
3995 Called during the TCP payload analysis on the channel **chn**.
3996
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02003997Here is a full example:
Christopher Faulet5a2c6612021-08-15 20:35:25 +02003998
3999.. code-block:: lua
4000
4001 Trace = {}
4002 Trace.id = "Lua trace filter"
4003 Trace.flags = filter.FLT_CFG_FL_HTX;
4004 Trace.__index = Trace
4005
4006 function Trace:new()
4007 local trace = {}
4008 setmetatable(trace, Trace)
4009 trace.req_len = 0
4010 trace.res_len = 0
4011 return trace
4012 end
4013
4014 function Trace:start_analyze(txn, chn)
4015 if chn:is_resp() then
4016 print("Start response analysis")
4017 else
4018 print("Start request analysis")
4019 end
4020 filter.register_data_filter(self, chn)
4021 end
4022
4023 function Trace:end_analyze(txn, chn)
4024 if chn:is_resp() then
4025 print("End response analysis: "..self.res_len.." bytes filtered")
4026 else
4027 print("End request analysis: "..self.req_len.." bytes filtered")
4028 end
4029 end
4030
4031 function Trace:http_headers(txn, http_msg)
4032 stline = http_msg:get_stline()
4033 if http_msg.channel:is_resp() then
4034 print("response:")
4035 print(stline.version.." "..stline.code.." "..stline.reason)
4036 else
4037 print("request:")
4038 print(stline.method.." "..stline.uri.." "..stline.version)
4039 end
4040
4041 for n, hdrs in pairs(http_msg:get_headers()) do
4042 for i,v in pairs(hdrs) do
4043 print(n..": "..v)
4044 end
4045 end
4046 return filter.CONTINUE
4047 end
4048
4049 function Trace:http_payload(txn, http_msg)
4050 body = http_msg:body(-20000)
4051 if http_msg.channel:is_resp() then
4052 self.res_len = self.res_len + body:len()
4053 else
4054 self.req_len = self.req_len + body:len()
4055 end
4056 end
4057
4058 core.register_filter("trace", Trace, function(trace, args)
4059 return trace
4060 end)
4061
4062..
4063
4064.. _httpmessage_class:
4065
4066HTTPMessage class
4067===================
4068
4069.. js:class:: HTTPMessage
4070
4071 **context**: filter
4072
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02004073 This class contains all functions to manipulate a HTTP message. For now, this
Christopher Faulet5a2c6612021-08-15 20:35:25 +02004074 class is only available from a filter context.
4075
4076.. js:function:: HTTPMessage.add_header(http_msg, name, value)
4077
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02004078 Appends a HTTP header field in the HTTP message **http_msg** whose name is
Christopher Faulet5a2c6612021-08-15 20:35:25 +02004079 specified in **name** and whose value is defined in **value**.
4080
4081 :param class_httpmessage http_msg: The manipulated HTTP message.
4082 :param string name: The header name.
4083 :param string value: The header value.
4084
4085.. js:function:: HTTPMessage.append(http_msg, string)
4086
4087 This function copies the string **string** at the end of incoming data of the
4088 HTTP message **http_msg**. The function returns the copied length on success
4089 or -1 if data cannot be copied.
4090
4091 Same that :js:func:`HTTPMessage.insert(http_msg, string, http_msg:input())`.
4092
4093 :param class_httpmessage http_msg: The manipulated HTTP message.
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02004094 :param string string: The data to copy at the end of incoming data.
Christopher Faulet5a2c6612021-08-15 20:35:25 +02004095 :returns: an integer containing the amount of bytes copied or -1.
4096
4097.. js:function:: HTTPMessage.body(http_msgl[, offset[, length]])
4098
4099 This function returns **length** bytes of incoming data from the HTTP message
4100 **http_msg**, starting at the offset **offset**. The data are not removed from
4101 the buffer.
4102
4103 By default, if no length is provided, all incoming data found, starting at the
4104 given offset, are returned. If **length** is set to -1, the function tries to
4105 retrieve a maximum of data. Because it is called in the filter context, it
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02004106 never yield. Not providing an offset is the same as setting it to 0. A
Ilya Shipitsinff0f2782021-08-22 22:18:07 +05004107 positive offset is relative to the beginning of incoming data of the
Christopher Faulet5a2c6612021-08-15 20:35:25 +02004108 http_message buffer while negative offset is relative to their end.
4109
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02004110 If there is no incoming data and the HTTP message can't receive more data,
4111 a 'nil' value is returned.
Christopher Faulet5a2c6612021-08-15 20:35:25 +02004112
4113 :param class_httpmessage http_msg: The manipulated HTTP message.
4114 :param integer offset: *optional* The offset in incoming data to start to get
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02004115 data. 0 by default. May be negative to be relative to the end of incoming
4116 data.
4117 :param integer length: *optional* The expected length of data to retrieve.
4118 All incoming data by default. May be set to -1 to get a maximum of data.
Christopher Faulet5a2c6612021-08-15 20:35:25 +02004119 :returns: a string containing the data found or nil.
4120
4121.. js:function:: HTTPMessage.eom(http_msg)
4122
4123 This function returns true if the end of message is reached for the HTTP
4124 message **http_msg**.
4125
4126 :param class_httpmessage http_msg: The manipulated HTTP message.
4127 :returns: an integer containing the amount of available bytes.
4128
4129.. js:function:: HTTPMessage.del_header(http_msg, name)
4130
4131 Removes all HTTP header fields in the HTTP message **http_msg** whose name is
4132 specified in **name**.
4133
4134 :param class_httpmessage http_msg: The manipulated http message.
4135 :param string name: The header name.
4136
4137.. js:function:: HTTPMessage.get_headers(http_msg)
4138
4139 Returns a table containing all the headers of the HTTP message **http_msg**.
4140
4141 :param class_httpmessage http_msg: The manipulated http message.
4142 :returns: table of headers.
4143
4144 This is the form of the returned table:
4145
4146.. code-block:: lua
4147
4148 http_msg:get_headers()['<header-name>'][<header-index>] = "<header-value>"
4149
4150 local hdr = http_msg:get_headers()
4151 hdr["host"][0] = "www.test.com"
4152 hdr["accept"][0] = "audio/basic q=1"
4153 hdr["accept"][1] = "audio/*, q=0.2"
4154 hdr["accept"][2] = "*.*, q=0.1"
4155..
4156
4157.. js:function:: HTTPMessage.get_stline(http_msg)
4158
4159 Returns a table containing the start-line of the HTTP message **http_msg**.
4160
4161 :param class_httpmessage http_msg: The manipulated http message.
4162 :returns: the start-line.
4163
4164 This is the form of the returned table:
4165
4166.. code-block:: lua
4167
4168 -- for the request :
4169 {"method" = string, "uri" = string, "version" = string}
4170
4171 -- for the response:
4172 {"version" = string, "code" = string, "reason" = string}
4173..
4174
4175.. js:function:: HTTPMessage.forward(http_msg, length)
4176
4177 This function forwards **length** bytes of data from the HTTP message
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02004178 **http_msg**. Because it is called in the filter context, it never yields. Only
Christopher Faulet5a2c6612021-08-15 20:35:25 +02004179 available incoming data may be forwarded, event if the requested length
4180 exceeds the available amount of incoming data. It returns the amount of data
4181 forwarded.
4182
4183 :param class_httpmessage http_msg: The manipulated HTTP message.
4184 :param integer int: The amount of data to forward.
4185
4186.. js:function:: HTTPMessage.input(http_msg)
4187
4188 This function returns the length of incoming data in the HTTP message
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02004189 **http_msg** from the filter point of view.
Christopher Faulet5a2c6612021-08-15 20:35:25 +02004190
4191 :param class_httpmessage http_msg: The manipulated HTTP message.
4192 :returns: an integer containing the amount of available bytes.
4193
4194.. js:function:: HTTPMessage.insert(http_msg, string[, offset])
4195
4196 This function copies the string **string** at the offset **offset** in
4197 incoming data of the HTTP message **http_msg**. The function returns the
4198 copied length on success or -1 if data cannot be copied.
4199
4200 By default, if no offset is provided, the string is copied in front of
Ilya Shipitsinff0f2782021-08-22 22:18:07 +05004201 incoming data. A positive offset is relative to the beginning of incoming data
Christopher Faulet5a2c6612021-08-15 20:35:25 +02004202 of the HTTP message while negative offset is relative to their end.
4203
4204 :param class_httpmessage http_msg: The manipulated HTTP message.
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02004205 :param string string: The data to copy into incoming data.
4206 :param integer offset: *optional* The offset in incoming data where to copy
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02004207 data. 0 by default. May be negative to be relative to the end of incoming
4208 data.
Christopher Faulet5a2c6612021-08-15 20:35:25 +02004209 :returns: an integer containing the amount of bytes copied or -1.
4210
4211.. js:function:: HTTPMessage.is_full(http_msg)
4212
4213 This function returns true if the HTTP message **http_msg** is full.
4214
4215 :param class_httpmessage http_msg: The manipulated HTTP message.
4216 :returns: a boolean
4217
4218.. js:function:: HTTPMessage.is_resp(http_msg)
4219
4220 This function returns true if the HTTP message **http_msg** is the response
4221 one.
4222
4223 :param class_httpmessage http_msg: The manipulated HTTP message.
4224 :returns: a boolean
4225
4226.. js:function:: HTTPMessage.may_recv(http_msg)
4227
4228 This function returns true if the HTTP message **http_msg** may still receive
4229 data.
4230
4231 :param class_httpmessage http_msg: The manipulated HTTP message.
4232 :returns: a boolean
4233
4234.. js:function:: HTTPMessage.output(http_msg)
4235
4236 This function returns the length of outgoing data of the HTTP message
4237 **http_msg**.
4238
4239 :param class_httpmessage http_msg: The manipulated HTTP message.
4240 :returns: an integer containing the amount of available bytes.
4241
4242.. js:function:: HTTPMessage.prepend(http_msg, string)
4243
4244 This function copies the string **string** in front of incoming data of the
4245 HTTP message **http_msg**. The function returns the copied length on success
4246 or -1 if data cannot be copied.
4247
4248 Same that :js:func:`HTTPMessage.insert(http_msg, string, 0)`.
4249
4250 :param class_httpmessage http_msg: The manipulated HTTP message.
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02004251 :param string string: The data to copy in front of incoming data.
Christopher Faulet5a2c6612021-08-15 20:35:25 +02004252 :returns: an integer containing the amount of bytes copied or -1.
4253
4254.. js:function:: HTTPMessage.remove(http_msg[, offset[, length]])
4255
4256 This function removes **length** bytes of incoming data of the HTTP message
4257 **http_msg**, starting at offset **offset**. This function returns number of
4258 bytes removed on success.
4259
4260 By default, if no length is provided, all incoming data, starting at the given
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02004261 offset, are removed. Not providing an offset is the same that setting it
Ilya Shipitsinff0f2782021-08-22 22:18:07 +05004262 to 0. A positive offset is relative to the beginning of incoming data of the
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02004263 HTTP message while negative offset is relative to the end.
Christopher Faulet5a2c6612021-08-15 20:35:25 +02004264
4265 :param class_httpmessage http_msg: The manipulated HTTP message.
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02004266 :param integer offset: *optional* The offset in incoming data where to start
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02004267 to remove data. 0 by default. May be negative to be relative to the end of
4268 incoming data.
Christopher Faulet5a2c6612021-08-15 20:35:25 +02004269 :param integer length: *optional* The length of data to remove. All incoming
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02004270 data by default.
Christopher Faulet5a2c6612021-08-15 20:35:25 +02004271 :returns: an integer containing the amount of bytes removed.
4272
4273.. js:function:: HTTPMessage.rep_header(http_msg, name, regex, replace)
4274
4275 Matches the regular expression in all occurrences of header field **name**
4276 according to regex **regex**, and replaces them with the string **replace**.
4277 The replacement value can contain back references like \1, \2, ... This
4278 function acts on whole header lines, regardless of the number of values they
4279 may contain.
4280
4281 :param class_httpmessage http_msg: The manipulated HTTP message.
4282 :param string name: The header name.
4283 :param string regex: The match regular expression.
4284 :param string replace: The replacement value.
4285
4286.. js:function:: HTTPMessage.rep_value(http_msg, name, regex, replace)
4287
4288 Matches the regular expression on every comma-delimited value of header field
4289 **name** according to regex **regex**, and replaces them with the string
4290 **replace**. The replacement value can contain back references like \1, \2,
4291 ...
4292
4293 :param class_httpmessage http_msg: The manipulated HTTP message.
4294 :param string name: The header name.
4295 :param string regex: The match regular expression.
4296 :param string replace: The replacement value.
4297
4298.. js:function:: HTTPMessage.send(http_msg, string)
4299
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02004300 This function requires immediate send of the string **string**. It means the
Ilya Shipitsinff0f2782021-08-22 22:18:07 +05004301 string is copied at the beginning of incoming data of the HTTP message
Christopher Faulet5a2c6612021-08-15 20:35:25 +02004302 **http_msg** and immediately forwarded. Because it is called in the filter
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02004303 context, it never yields.
Christopher Faulet5a2c6612021-08-15 20:35:25 +02004304
4305 :param class_httpmessage http_msg: The manipulated HTTP message.
4306 :param string string: The data to send.
4307 :returns: an integer containing the amount of bytes copied or -1.
4308
4309.. js:function:: HTTPMessage.set(http_msg, string[, offset[, length]])
4310
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02004311 This function replaces **length** bytes of incoming data of the HTTP message
Christopher Faulet5a2c6612021-08-15 20:35:25 +02004312 **http_msg**, starting at offset **offset**, by the string **string**. The
4313 function returns the copied length on success or -1 if data cannot be copied.
4314
4315 By default, if no length is provided, all incoming data, starting at the given
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02004316 offset, are replaced. Not providing an offset is the same as setting it
Ilya Shipitsinff0f2782021-08-22 22:18:07 +05004317 to 0. A positive offset is relative to the beginning of incoming data of the
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02004318 HTTP message while negative offset is relative to the end.
Christopher Faulet5a2c6612021-08-15 20:35:25 +02004319
4320 :param class_httpmessage http_msg: The manipulated HTTP message.
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02004321 :param string string: The data to copy into incoming data.
4322 :param integer offset: *optional* The offset in incoming data where to start
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02004323 the data replacement. 0 by default. May be negative to be relative to the
4324 end of incoming data.
Christopher Faulet5a2c6612021-08-15 20:35:25 +02004325 :param integer length: *optional* The length of data to replace. All incoming
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02004326 data by default.
Christopher Faulet5a2c6612021-08-15 20:35:25 +02004327 :returns: an integer containing the amount of bytes copied or -1.
4328
4329.. js:function:: HTTPMessage.set_eom(http_msg)
4330
4331 This function set the end of message for the HTTP message **http_msg**.
4332
4333 :param class_httpmessage http_msg: The manipulated HTTP message.
4334
4335.. js:function:: HTTPMessage.set_header(http_msg, name, value)
4336
4337 This variable replace all occurrence of all header matching the name **name**,
4338 by only one containing the value **value**.
4339
4340 :param class_httpmessage http_msg: The manipulated HTTP message.
4341 :param string name: The header name.
4342 :param string value: The header value.
4343
4344 This function does the same work as the following code:
4345
4346.. code-block:: lua
4347
4348 http_msg:del_header("header")
4349 http_msg:add_header("header", "value")
4350..
4351
4352.. js:function:: HTTPMessage.set_method(http_msg, method)
4353
4354 Rewrites the request method with the string **method**. The HTTP message
4355 **http_msg** must be the request.
4356
4357 :param class_httpmessage http_msg: The manipulated HTTP message.
4358 :param string method: The new method.
4359
4360.. js:function:: HTTPMessage.set_path(http_msg, path)
4361
4362 Rewrites the request path with the string **path**. The HTTP message
4363 **http_msg** must be the request.
4364
4365 :param class_httpmessage http_msg: The manipulated HTTP message.
4366 :param string method: The new method.
4367
4368.. js:function:: HTTPMessage.set_query(http_msg, query)
4369
4370 Rewrites the request's query string which appears after the first question
4371 mark ("?") with the string **query**. The HTTP message **http_msg** must be
4372 the request.
4373
4374 :param class_httpmessage http_msg: The manipulated HTTP message.
4375 :param string query: The new query.
4376
4377.. js:function:: HTTPMessage.set_status(http_msg, status[, reason])
4378
Ilya Shipitsinff0f2782021-08-22 22:18:07 +05004379 Rewrites the response status code with the integer **code** and optional the
Christopher Faulet5a2c6612021-08-15 20:35:25 +02004380 reason **reason**. If no custom reason is provided, it will be generated from
4381 the status. The HTTP message **http_msg** must be the response.
4382
4383 :param class_httpmessage http_msg: The manipulated HTTP message.
4384 :param integer status: The new response status code.
4385 :param string reason: The new response reason (optional).
4386
4387.. js:function:: HTTPMessage.set_uri(http_msg, uri)
4388
4389 Rewrites the request URI with the string **uri**. The HTTP message
4390 **http_msg** must be the request.
4391
4392 :param class_httpmessage http_msg: The manipulated HTTP message.
4393 :param string uri: The new uri.
4394
4395.. js:function:: HTTPMessage.unset_eom(http_msg)
4396
4397 This function remove the end of message for the HTTP message **http_msg**.
4398
4399 :param class_httpmessage http_msg: The manipulated HTTP message.
4400
William Lallemand10cea5c2022-03-30 16:02:43 +02004401.. _CertCache_class:
4402
4403CertCache class
4404================
4405
4406.. js:class:: CertCache
4407
4408 This class allows to update an SSL certificate file in the memory of the
4409 current HAProxy process. It will do the same as "set ssl cert" + "commit ssl
4410 cert" over the HAProxy CLI.
4411
4412.. js:function:: CertCache.set(certificate)
4413
4414 This function updates a certificate in memory.
4415
4416 :param table certificate: A table containing the fields to update.
4417 :param string certificate.filename: The mandatory filename of the certificate
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02004418 to update, it must already exist in memory.
William Lallemand10cea5c2022-03-30 16:02:43 +02004419 :param string certificate.crt: A certificate in the PEM format. It can also
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02004420 contain a private key.
William Lallemand10cea5c2022-03-30 16:02:43 +02004421 :param string certificate.key: A private key in the PEM format.
4422 :param string certificate.ocsp: An OCSP response in base64. (cf management.txt)
4423 :param string certificate.issuer: The certificate of the OCSP issuer.
4424 :param string certificate.sctl: An SCTL file.
4425
4426.. code-block:: lua
4427
4428 CertCache.set{filename="certs/localhost9994.pem.rsa", crt=crt}
4429
4430
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01004431External Lua libraries
4432======================
4433
4434A lot of useful lua libraries can be found here:
4435
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02004436* Lua toolbox has been superseded by
4437 `https://luarocks.org/ <https://luarocks.org/>`_
4438
4439 The old lua toolbox source code is still available here
4440 `https://github.com/catwell/lua-toolbox <https://github.com/catwell/lua-toolbox>`_ (DEPRECATED)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01004441
Ilya Shipitsin2075ca82020-03-06 23:22:22 +05004442Redis client library:
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01004443
4444* `https://github.com/nrk/redis-lua <https://github.com/nrk/redis-lua>`_
4445
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02004446This is an example about the usage of the Redis library within HAProxy.
4447Note that each call to any function of this library can throw an error if
4448the socket connection fails.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01004449
4450.. code-block:: lua
4451
4452 -- load the redis library
4453 local redis = require("redis");
4454
4455 function do_something(txn)
4456
4457 -- create and connect new tcp socket
4458 local tcp = core.tcp();
4459 tcp:settimeout(1);
4460 tcp:connect("127.0.0.1", 6379);
4461
4462 -- use the redis library with this new socket
4463 local client = redis.connect({socket=tcp});
4464 client:ping();
4465
4466 end
4467
4468OpenSSL:
4469
4470* `http://mkottman.github.io/luacrypto/index.html
4471 <http://mkottman.github.io/luacrypto/index.html>`_
4472
4473* `https://github.com/brunoos/luasec/wiki
4474 <https://github.com/brunoos/luasec/wiki>`_