blob: fd3d47c77f8e5d4b2adb6fbcf72d601c4d39f23d [file] [log] [blame]
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001.. toctree::
2 :maxdepth: 2
3
4
5How Lua runs in HAProxy
6=======================
7
8HAProxy Lua running contexts
9----------------------------
10
11The Lua code executed in HAProxy can be processed in 2 main modes. The first one
12is the **initialisation mode**, and the second is the **runtime mode**.
13
14* In the **initialisation mode**, we can perform DNS solves, but we cannot
15 perform socket I/O. In this initialisation mode, HAProxy still blocked during
16 the execution of the Lua program.
17
18* In the **runtime mode**, we cannot perform DNS solves, but we can use sockets.
19 The execution of the Lua code is multiplexed with the requests processing, so
20 the Lua code seems to be run in blocking, but it is not the case.
21
22The Lua code is loaded in one or more files. These files contains main code and
Aurelien DARRAGONc84899c2023-02-20 18:18:59 +010023functions. Lua has 8 execution contexts.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +010024
251. The Lua file **body context**. It is executed during the load of the Lua file
26 in the HAProxy `[global]` section with the directive `lua-load`. It is
27 executed in initialisation mode. This section is use for configuring Lua
28 bindings in HAProxy.
29
David Carlier61fdf8b2015-10-02 11:59:38 +0100302. The Lua **init context**. It is a Lua function executed just after the
Thierry FOURNIER17bd1522015-03-11 20:31:00 +010031 HAProxy configuration parsing. The execution is in initialisation mode. In
32 this context the HAProxy environment are already initialized. It is useful to
33 check configuration, or initializing socket connections or tasks. These
34 functions are declared in the body context with the Lua function
35 `core.register_init()`. The prototype of the function is a simple function
36 without return value and without parameters, like this: `function fcn()`.
37
David Carlier61fdf8b2015-10-02 11:59:38 +0100383. The Lua **task context**. It is a Lua function executed after the start
Thierry FOURNIER17bd1522015-03-11 20:31:00 +010039 of the HAProxy scheduler, and just after the declaration of the task with the
40 Lua function `core.register_task()`. This context can be concurrent with the
41 traffic processing. It is executed in runtime mode. The prototype of the
42 function is a simple function without return value and without parameters,
43 like this: `function fcn()`.
44
David Carlier61fdf8b2015-10-02 11:59:38 +0100454. The **action context**. It is a Lua function conditionally executed. These
Thierry FOURNIERa2d02252015-10-01 15:00:42 +020046 actions are registered by the Lua directives "`core.register_action()`". The
47 prototype of the Lua called function is a function with doesn't returns
48 anything and that take an object of class TXN as entry. `function fcn(txn)`.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +010049
505. The **sample-fetch context**. This function takes a TXN object as entry
51 argument and returns a string. These types of function cannot execute any
52 blocking function. They are useful to aggregate some of original HAProxy
53 sample-fetches and return the result. The prototype of the function is
54 `function string fcn(txn)`. These functions can be registered with the Lua
55 function `core.register_fetches()`. Each declared sample-fetch is prefixed by
56 the string "lua.".
57
Christopher Faulet1e9b1b62021-08-11 10:14:30 +020058 .. note::
59 It is possible that this function cannot found the required data in the
60 original HAProxy sample-fetches, in this case, it cannot return the
61 result. This case is not yet supported
Thierry FOURNIER17bd1522015-03-11 20:31:00 +010062
David Carlier61fdf8b2015-10-02 11:59:38 +0100636. The **converter context**. It is a Lua function that takes a string as input
Thierry FOURNIER17bd1522015-03-11 20:31:00 +010064 and returns another string as output. These types of function are stateless,
65 it cannot access to any context. They don't execute any blocking function.
66 The call prototype is `function string fcn(string)`. This function can be
67 registered with the Lua function `core.register_converters()`. Each declared
68 converter is prefixed by the string "lua.".
69
Christopher Faulet5a2c6612021-08-15 20:35:25 +0200707. The **filter context**: It is a Lua object based on a class defining filter
71 callback functions. Lua filters are registered using
72 `core.register_filter()`. Each declared filter is prefixed by the string
73 "lua.".
74
Aurelien DARRAGONc84899c2023-02-20 18:18:59 +0100758. The **event context**: Inside a function that handles events subscribed
76 through `core.event_sub()` or `Server.event_sub()`.
77
Christopher Faulet5a2c6612021-08-15 20:35:25 +020078
Thierry FOURNIER17bd1522015-03-11 20:31:00 +010079HAProxy Lua Hello world
80-----------------------
81
82HAProxy configuration file (`hello_world.conf`):
83
84::
85
86 global
87 lua-load hello_world.lua
88
89 listen proxy
90 bind 127.0.0.1:10001
Thierry FOURNIERa2d02252015-10-01 15:00:42 +020091 tcp-request inspect-delay 1s
92 tcp-request content use-service lua.hello_world
Thierry FOURNIER17bd1522015-03-11 20:31:00 +010093
94HAProxy Lua file (`hello_world.lua`):
95
96.. code-block:: lua
97
Thierry FOURNIERa2d02252015-10-01 15:00:42 +020098 core.register_service("hello_world", "tcp", function(applet)
99 applet:send("hello world\n")
100 end)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100101
102How to start HAProxy for testing this configuration:
103
104::
105
106 ./haproxy -f hello_world.conf
107
108On other terminal, you can test with telnet:
109
110::
111
112 #:~ telnet 127.0.0.1 10001
113 hello world
114
Thierry Fournierae6b5682022-09-19 09:04:16 +0200115Usage of load parameters
116------------------------
117
Ilya Shipitsin4a689da2022-10-29 09:34:32 +0500118HAProxy lua-load(-per-thread) directives allow a list of parameters after
Thierry Fournierae6b5682022-09-19 09:04:16 +0200119the lua file name. These parameters are accessible through an array of args
120using this code `local args = table.pack(...)` in the body of loaded file.
121
122Below, a new version of the hello world using load parameters
123
124HAProxy configuration file (`hello_world.conf`):
125
126::
127
128 global
129 lua-load hello_world.lua "this is not an hello world"
130
131 listen proxy
132 bind 127.0.0.1:10001
133 tcp-request inspect-delay 1s
134 tcp-request content use-service lua.hello_world
135
136HAProxy Lua file (`hello_world.lua`):
137
138.. code-block:: lua
139
140 local args = table.pack(...)
141
142 core.register_service("hello_world", "tcp", function(applet)
143 applet:send(args[1] .. "\n")
144 end)
145
146
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100147Core class
148==========
149
150.. js:class:: core
151
152 The "core" class contains all the HAProxy core functions. These function are
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +0200153 useful for the controlling of the execution flow, registering hooks,
154 manipulating global maps or ACL, ...
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100155
156 "core" class is basically provided with HAProxy. No `require` line is
157 required to uses these function.
158
David Carlier61fdf8b2015-10-02 11:59:38 +0100159 The "core" class is static, it is not possible to create a new object of this
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100160 type.
161
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100162.. js:attribute:: core.emerg
163
Thierry FOURNIERdc595002015-12-21 11:13:52 +0100164 :returns: integer
165
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +0200166 This attribute is an integer, it contains the value of the loglevel
167 "emergency" (0).
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100168
169.. js:attribute:: core.alert
170
Thierry FOURNIERdc595002015-12-21 11:13:52 +0100171 :returns: integer
172
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +0200173 This attribute is an integer, it contains the value of the loglevel
174 "alert" (1).
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100175
176.. js:attribute:: core.crit
177
Thierry FOURNIERdc595002015-12-21 11:13:52 +0100178 :returns: integer
179
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +0200180 This attribute is an integer, it contains the value of the loglevel
181 "critical" (2).
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100182
183.. js:attribute:: core.err
184
Thierry FOURNIERdc595002015-12-21 11:13:52 +0100185 :returns: integer
186
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +0200187 This attribute is an integer, it contains the value of the loglevel
188 "error" (3).
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100189
190.. js:attribute:: core.warning
191
Thierry FOURNIERdc595002015-12-21 11:13:52 +0100192 :returns: integer
193
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +0200194 This attribute is an integer, it contains the value of the loglevel
195 "warning" (4).
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100196
197.. js:attribute:: core.notice
198
Thierry FOURNIERdc595002015-12-21 11:13:52 +0100199 :returns: integer
200
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +0200201 This attribute is an integer, it contains the value of the loglevel
202 "notice" (5).
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100203
204.. js:attribute:: core.info
205
Thierry FOURNIERdc595002015-12-21 11:13:52 +0100206 :returns: integer
207
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +0200208 This attribute is an integer, it contains the value of the loglevel
209 "info" (6).
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100210
211.. js:attribute:: core.debug
212
Thierry FOURNIERdc595002015-12-21 11:13:52 +0100213 :returns: integer
214
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +0200215 This attribute is an integer, it contains the value of the loglevel
216 "debug" (7).
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100217
Thierry FOURNIER12a865d2016-12-14 19:40:37 +0100218.. js:attribute:: core.proxies
219
220 **context**: task, action, sample-fetch, converter
221
Patrick Hemmerc6a1d712018-05-01 21:30:41 -0400222 This attribute is a table of declared proxies (frontend and backends). Each
223 proxy give an access to his list of listeners and servers. The table is
224 indexed by proxy name, and each entry is of type :ref:`proxy_class`.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +0100225
Christopher Faulet1e9b1b62021-08-11 10:14:30 +0200226 .. Warning::
Aurelien DARRAGON53901f42022-10-13 19:49:42 +0200227 if you declared a frontend and backend with the same name, only one of
228 them will be listed.
Thierry FOURNIER9b82a582017-07-24 13:30:43 +0200229
230 :see: :js:attr:`core.backends`
231 :see: :js:attr:`core.frontends`
232
233.. js:attribute:: core.backends
234
235 **context**: task, action, sample-fetch, converter
236
Patrick Hemmerc6a1d712018-05-01 21:30:41 -0400237 This attribute is a table of declared proxies with backend capability. Each
238 proxy give an access to his list of listeners and servers. The table is
239 indexed by the backend name, and each entry is of type :ref:`proxy_class`.
Thierry FOURNIER9b82a582017-07-24 13:30:43 +0200240
241 :see: :js:attr:`core.proxies`
242 :see: :js:attr:`core.frontends`
243
244.. js:attribute:: core.frontends
245
246 **context**: task, action, sample-fetch, converter
247
Patrick Hemmerc6a1d712018-05-01 21:30:41 -0400248 This attribute is a table of declared proxies with frontend capability. Each
249 proxy give an access to his list of listeners and servers. The table is
250 indexed by the frontend name, and each entry is of type :ref:`proxy_class`.
Thierry FOURNIER9b82a582017-07-24 13:30:43 +0200251
252 :see: :js:attr:`core.proxies`
253 :see: :js:attr:`core.backends`
254
Thierry Fournierecb83c22020-11-28 15:49:44 +0100255.. js:attribute:: core.thread
256
257 **context**: task, action, sample-fetch, converter, applet
258
259 This variable contains the executing thread number starting at 1. 0 is a
260 special case for the common lua context. So, if thread is 0, Lua scope is
261 shared by all threads, otherwise the scope is dedicated to a single thread.
262 A program which needs to execute some parts exactly once regardless of the
263 number of threads can check that core.thread is 0 or 1.
264
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100265.. js:function:: core.log(loglevel, msg)
266
267 **context**: body, init, task, action, sample-fetch, converter
268
David Carlier61fdf8b2015-10-02 11:59:38 +0100269 This function sends a log. The log is sent, according with the HAProxy
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100270 configuration file, on the default syslog server if it is configured and on
271 the stderr if it is allowed.
272
Bertrand Jacquin874a35c2018-09-10 21:26:07 +0100273 :param integer loglevel: Is the log level associated with the message. It is a
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +0200274 number between 0 and 7.
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100275 :param string msg: The log content.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +0100276 :see: :js:attr:`core.emerg`, :js:attr:`core.alert`, :js:attr:`core.crit`,
277 :js:attr:`core.err`, :js:attr:`core.warning`, :js:attr:`core.notice`,
278 :js:attr:`core.info`, :js:attr:`core.debug` (log level definitions)
279 :see: :js:func:`core.Debug`
280 :see: :js:func:`core.Info`
281 :see: :js:func:`core.Warning`
282 :see: :js:func:`core.Alert`
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100283
284.. js:function:: core.Debug(msg)
285
286 **context**: body, init, task, action, sample-fetch, converter
287
288 :param string msg: The log content.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +0100289 :see: :js:func:`core.log`
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100290
291 Does the same job than:
292
293.. code-block:: lua
294
Thierry FOURNIERdc595002015-12-21 11:13:52 +0100295 function Debug(msg)
296 core.log(core.debug, msg)
297 end
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100298..
299
300.. js:function:: core.Info(msg)
301
302 **context**: body, init, task, action, sample-fetch, converter
303
304 :param string msg: The log content.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +0100305 :see: :js:func:`core.log`
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100306
307.. code-block:: lua
308
Thierry FOURNIERdc595002015-12-21 11:13:52 +0100309 function Info(msg)
310 core.log(core.info, msg)
311 end
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100312..
313
314.. js:function:: core.Warning(msg)
315
316 **context**: body, init, task, action, sample-fetch, converter
317
318 :param string msg: The log content.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +0100319 :see: :js:func:`core.log`
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100320
321.. code-block:: lua
322
Thierry FOURNIERdc595002015-12-21 11:13:52 +0100323 function Warning(msg)
324 core.log(core.warning, msg)
325 end
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100326..
327
328.. js:function:: core.Alert(msg)
329
330 **context**: body, init, task, action, sample-fetch, converter
331
332 :param string msg: The log content.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +0100333 :see: :js:func:`core.log`
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100334
335.. code-block:: lua
336
Thierry FOURNIERdc595002015-12-21 11:13:52 +0100337 function Alert(msg)
338 core.log(core.alert, msg)
339 end
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100340..
341
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100342.. js:function:: core.add_acl(filename, key)
343
344 **context**: init, task, action, sample-fetch, converter
345
346 Add the ACL *key* in the ACLs list referenced by the file *filename*.
347
348 :param string filename: the filename that reference the ACL entries.
349 :param string key: the key which will be added.
350
351.. js:function:: core.del_acl(filename, key)
352
353 **context**: init, task, action, sample-fetch, converter
354
355 Delete the ACL entry referenced by the key *key* in the list of ACLs
356 referenced by *filename*.
357
358 :param string filename: the filename that reference the ACL entries.
359 :param string key: the key which will be deleted.
360
361.. js:function:: core.del_map(filename, key)
362
363 **context**: init, task, action, sample-fetch, converter
364
365 Delete the map entry indexed with the specified key in the list of maps
366 referenced by his filename.
367
368 :param string filename: the filename that reference the map entries.
369 :param string key: the key which will be deleted.
370
Thierry Fourniereea77c02016-03-18 08:47:13 +0100371.. js:function:: core.get_info()
372
373 **context**: body, init, task, action, sample-fetch, converter
374
Aurelien DARRAGON53901f42022-10-13 19:49:42 +0200375 Returns HAProxy core information. We can find information like the uptime,
Thierry Fourniereea77c02016-03-18 08:47:13 +0100376 the pid, memory pool usage, tasks number, ...
377
Ilya Shipitsin5fa29b82022-12-07 09:46:19 +0500378 This information is also returned by the management socket via the command
Bertrand Jacquin874a35c2018-09-10 21:26:07 +0100379 "show info". See the management socket documentation for more information
Thierry Fourniereea77c02016-03-18 08:47:13 +0100380 about the content of these variables.
381
382 :returns: an array of values.
383
Thierry Fournierb1f46562016-01-21 09:46:15 +0100384.. js:function:: core.now()
385
386 **context**: body, init, task, action
387
388 This function returns the current time. The time returned is fixed by the
Bertrand Jacquin874a35c2018-09-10 21:26:07 +0100389 HAProxy core and assures than the hour will be monotonic and that the system
Thierry Fournierb1f46562016-01-21 09:46:15 +0100390 call 'gettimeofday' will not be called too. The time is refreshed between each
391 Lua execution or resume, so two consecutive call to the function "now" will
392 probably returns the same result.
393
Patrick Hemmerc6a1d712018-05-01 21:30:41 -0400394 :returns: a table which contains two entries "sec" and "usec". "sec"
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +0200395 contains the current at the epoch format, and "usec" contains the
396 current microseconds.
Thierry Fournierb1f46562016-01-21 09:46:15 +0100397
Thierry FOURNIERa78f0372016-12-14 19:04:41 +0100398.. js:function:: core.http_date(date)
399
400 **context**: body, init, task, action
401
Bertrand Jacquin874a35c2018-09-10 21:26:07 +0100402 This function take a string representing http date, and returns an integer
Thierry FOURNIERa78f0372016-12-14 19:04:41 +0100403 containing the corresponding date with a epoch format. A valid http date
404 me respect the format IMF, RFC850 or ASCTIME.
405
406 :param string date: a date http-date formatted
407 :returns: integer containing epoch date
408 :see: :js:func:`core.imf_date`.
409 :see: :js:func:`core.rfc850_date`.
410 :see: :js:func:`core.asctime_date`.
411 :see: https://tools.ietf.org/html/rfc7231#section-7.1.1.1
412
413.. js:function:: core.imf_date(date)
414
415 **context**: body, init, task, action
416
Bertrand Jacquin874a35c2018-09-10 21:26:07 +0100417 This function take a string representing IMF date, and returns an integer
Thierry FOURNIERa78f0372016-12-14 19:04:41 +0100418 containing the corresponding date with a epoch format.
419
420 :param string date: a date IMF formatted
421 :returns: integer containing epoch date
422 :see: https://tools.ietf.org/html/rfc7231#section-7.1.1.1
423
424 The IMF format is like this:
425
426.. code-block:: text
427
428 Sun, 06 Nov 1994 08:49:37 GMT
429..
430
431.. js:function:: core.rfc850_date(date)
432
433 **context**: body, init, task, action
434
Bertrand Jacquin874a35c2018-09-10 21:26:07 +0100435 This function take a string representing RFC850 date, and returns an integer
Thierry FOURNIERa78f0372016-12-14 19:04:41 +0100436 containing the corresponding date with a epoch format.
437
438 :param string date: a date RFC859 formatted
439 :returns: integer containing epoch date
440 :see: https://tools.ietf.org/html/rfc7231#section-7.1.1.1
441
442 The RFC850 format is like this:
443
444.. code-block:: text
445
446 Sunday, 06-Nov-94 08:49:37 GMT
447..
448
449.. js:function:: core.asctime_date(date)
450
451 **context**: body, init, task, action
452
Bertrand Jacquin874a35c2018-09-10 21:26:07 +0100453 This function take a string representing ASCTIME date, and returns an integer
Thierry FOURNIERa78f0372016-12-14 19:04:41 +0100454 containing the corresponding date with a epoch format.
455
456 :param string date: a date ASCTIME formatted
457 :returns: integer containing epoch date
458 :see: https://tools.ietf.org/html/rfc7231#section-7.1.1.1
459
460 The ASCTIME format is like this:
461
462.. code-block:: text
463
464 Sun Nov 6 08:49:37 1994
465..
466
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100467.. js:function:: core.msleep(milliseconds)
468
469 **context**: body, init, task, action
470
471 The `core.msleep()` stops the Lua execution between specified milliseconds.
472
473 :param integer milliseconds: the required milliseconds.
474
Thierry FOURNIERc5d11c62018-02-12 14:46:54 +0100475.. js:function:: core.register_action(name, actions, func [, nb_args])
Thierry FOURNIER8255a752015-09-23 21:03:35 +0200476
477 **context**: body
478
David Carlier61fdf8b2015-10-02 11:59:38 +0100479 Register a Lua function executed as action. All the registered action can be
Thierry FOURNIER8255a752015-09-23 21:03:35 +0200480 used in HAProxy with the prefix "lua.". An action gets a TXN object class as
481 input.
482
483 :param string name: is the name of the converter.
Willy Tarreau61add3c2015-09-28 15:39:10 +0200484 :param table actions: is a table of string describing the HAProxy actions who
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +0200485 want to register to. The expected actions are 'tcp-req', 'tcp-res', 'http-req'
486 or 'http-res'.
Aurelien DARRAGON53901f42022-10-13 19:49:42 +0200487 :param function func: is the Lua function called to work as converter.
Thierry FOURNIERc5d11c62018-02-12 14:46:54 +0100488 :param integer nb_args: is the expected number of argument for the action.
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +0200489 By default the value is 0.
Thierry FOURNIER8255a752015-09-23 21:03:35 +0200490
491 The prototype of the Lua function used as argument is:
492
493.. code-block:: lua
494
Thierry FOURNIERc5d11c62018-02-12 14:46:54 +0100495 function(txn [, arg1 [, arg2]])
Thierry FOURNIER8255a752015-09-23 21:03:35 +0200496..
497
Thierry FOURNIERdc595002015-12-21 11:13:52 +0100498 * **txn** (:ref:`txn_class`): this is a TXN object used for manipulating the
Thierry FOURNIER8255a752015-09-23 21:03:35 +0200499 current request or TCP stream.
500
Bertrand Jacquin874a35c2018-09-10 21:26:07 +0100501 * **argX**: this is argument provided through the HAProxy configuration file.
Thierry FOURNIERc5d11c62018-02-12 14:46:54 +0100502
Bertrand Jacquin874a35c2018-09-10 21:26:07 +0100503 Here, an example of action registration. The action just send an 'Hello world'
Thierry FOURNIER8255a752015-09-23 21:03:35 +0200504 in the logs.
505
506.. code-block:: lua
507
508 core.register_action("hello-world", { "tcp-req", "http-req" }, function(txn)
509 txn:Info("Hello world")
510 end)
511..
512
Willy Tarreau714f3452021-05-09 06:47:26 +0200513 This example code is used in HAProxy configuration like this:
Thierry FOURNIER8255a752015-09-23 21:03:35 +0200514
515::
516
517 frontend tcp_frt
518 mode tcp
519 tcp-request content lua.hello-world
520
521 frontend http_frt
522 mode http
523 http-request lua.hello-world
Aurelien DARRAGONd5c80d72023-03-13 19:36:13 +0100524
Thierry FOURNIERc5d11c62018-02-12 14:46:54 +0100525..
526
Bertrand Jacquin874a35c2018-09-10 21:26:07 +0100527 A second example using arguments
Thierry FOURNIERc5d11c62018-02-12 14:46:54 +0100528
529.. code-block:: lua
530
531 function hello_world(txn, arg)
532 txn:Info("Hello world for " .. arg)
533 end
534 core.register_action("hello-world", { "tcp-req", "http-req" }, hello_world, 2)
Aurelien DARRAGONd5c80d72023-03-13 19:36:13 +0100535
Thierry FOURNIERc5d11c62018-02-12 14:46:54 +0100536..
Thierry FOURNIER8255a752015-09-23 21:03:35 +0200537
Willy Tarreau714f3452021-05-09 06:47:26 +0200538 This example code is used in HAProxy configuration like this:
Thierry FOURNIERc5d11c62018-02-12 14:46:54 +0100539
540::
541
542 frontend tcp_frt
543 mode tcp
544 tcp-request content lua.hello-world everybody
Aurelien DARRAGONd5c80d72023-03-13 19:36:13 +0100545
Thierry FOURNIERc5d11c62018-02-12 14:46:54 +0100546..
Aurelien DARRAGON53901f42022-10-13 19:49:42 +0200547
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100548.. js:function:: core.register_converters(name, func)
549
550 **context**: body
551
David Carlier61fdf8b2015-10-02 11:59:38 +0100552 Register a Lua function executed as converter. All the registered converters
Aurelien DARRAGON53901f42022-10-13 19:49:42 +0200553 can be used in HAProxy with the prefix "lua.". A converter gets a string as
554 input and returns a string as output. The registered function can take up to 9
555 values as parameter. All the values are strings.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100556
557 :param string name: is the name of the converter.
558 :param function func: is the Lua function called to work as converter.
559
560 The prototype of the Lua function used as argument is:
561
562.. code-block:: lua
563
564 function(str, [p1 [, p2 [, ... [, p5]]]])
565..
566
567 * **str** (*string*): this is the input value automatically converted in
568 string.
569 * **p1** .. **p5** (*string*): this is a list of string arguments declared in
Bertrand Jacquin874a35c2018-09-10 21:26:07 +0100570 the HAProxy configuration file. The number of arguments doesn't exceed 5.
Aurelien DARRAGON53901f42022-10-13 19:49:42 +0200571 The order and the nature of these is conventionally chosen by the
Bertrand Jacquin874a35c2018-09-10 21:26:07 +0100572 developer.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100573
574.. js:function:: core.register_fetches(name, func)
575
576 **context**: body
577
David Carlier61fdf8b2015-10-02 11:59:38 +0100578 Register a Lua function executed as sample fetch. All the registered sample
Bertrand Jacquin874a35c2018-09-10 21:26:07 +0100579 fetch can be used in HAProxy with the prefix "lua.". A Lua sample fetch
Aurelien DARRAGON53901f42022-10-13 19:49:42 +0200580 returns a string as output. The registered function can take up to 9 values as
581 parameter. All the values are strings.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100582
Aurelien DARRAGON53901f42022-10-13 19:49:42 +0200583 :param string name: is the name of the sample fetch.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100584 :param function func: is the Lua function called to work as sample fetch.
585
586 The prototype of the Lua function used as argument is:
587
588.. code-block:: lua
589
590 string function(txn, [p1 [, p2 [, ... [, p5]]]])
591..
592
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +0200593 * **txn** (:ref:`txn_class`): this is the txn object associated with the
594 current request.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100595 * **p1** .. **p5** (*string*): this is a list of string arguments declared in
Bertrand Jacquin874a35c2018-09-10 21:26:07 +0100596 the HAProxy configuration file. The number of arguments doesn't exceed 5.
Aurelien DARRAGON53901f42022-10-13 19:49:42 +0200597 The order and the nature of these is conventionally chosen by the
Bertrand Jacquin874a35c2018-09-10 21:26:07 +0100598 developer.
599 * **Returns**: A string containing some data, or nil if the value cannot be
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100600 returned now.
601
602 lua example code:
603
604.. code-block:: lua
605
606 core.register_fetches("hello", function(txn)
607 return "hello"
608 end)
609..
610
611 HAProxy example configuration:
612
613::
614
615 frontend example
616 http-request redirect location /%[lua.hello]
617
Christopher Faulet5a2c6612021-08-15 20:35:25 +0200618.. js:function:: core.register_filter(name, Flt, func)
619
620 **context**: body
621
622 Register a Lua function used to declare a filter. All the registered filters
623 can by used in HAProxy with the prefix "lua.".
624
625 :param string name: is the name of the filter.
626 :param table Flt: is a Lua class containing the filter definition (id, flags,
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +0200627 callbacks).
Christopher Faulet5a2c6612021-08-15 20:35:25 +0200628 :param function func: is the Lua function called to create the Lua filter.
629
630 The prototype of the Lua function used as argument is:
631
632.. code-block:: lua
633
634 function(flt, args)
635..
636
637 * **flt** : Is a filter object based on the class provided in
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +0200638 :js:func:`core.register_filter()` function.
Christopher Faulet5a2c6612021-08-15 20:35:25 +0200639
640 * **args**: Is a table of strings containing all arguments provided through
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +0200641 the HAProxy configuration file, on the filter line.
Christopher Faulet5a2c6612021-08-15 20:35:25 +0200642
643 It must return the filter to use or nil to ignore it. Here, an example of
644 filter registration.
645
646.. code-block:: lua
647
648 core.register_filter("my-filter", MyFilter, function(flt, args)
649 flt.args = args -- Save arguments
650 return flt
651 end)
652..
653
654 This example code is used in HAProxy configuration like this:
655
656::
657
658 frontend http
659 mode http
660 filter lua.my-filter arg1 arg2 arg3
Aurelien DARRAGONd5c80d72023-03-13 19:36:13 +0100661
Christopher Faulet5a2c6612021-08-15 20:35:25 +0200662..
663
664 :see: :js:class:`Filter`
665
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +0200666.. js:function:: core.register_service(name, mode, func)
667
668 **context**: body
669
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +0200670 Register a Lua function executed as a service. All the registered services
671 can be used in HAProxy with the prefix "lua.". A service gets an object class
672 as input according with the required mode.
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +0200673
Aurelien DARRAGON53901f42022-10-13 19:49:42 +0200674 :param string name: is the name of the service.
Willy Tarreau61add3c2015-09-28 15:39:10 +0200675 :param string mode: is string describing the required mode. Only 'tcp' or
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +0200676 'http' are allowed.
Aurelien DARRAGON53901f42022-10-13 19:49:42 +0200677 :param function func: is the Lua function called to work as service.
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +0200678
679 The prototype of the Lua function used as argument is:
680
681.. code-block:: lua
682
683 function(applet)
684..
685
Thierry FOURNIERdc595002015-12-21 11:13:52 +0100686 * **applet** *applet* will be a :ref:`applettcp_class` or a
687 :ref:`applethttp_class`. It depends the type of registered applet. An applet
688 registered with the 'http' value for the *mode* parameter will gets a
689 :ref:`applethttp_class`. If the *mode* value is 'tcp', the applet will gets
690 a :ref:`applettcp_class`.
691
Christopher Faulet1e9b1b62021-08-11 10:14:30 +0200692 .. warning::
693 Applets of type 'http' cannot be called from 'tcp-*' rulesets. Only the
694 'http-*' rulesets are authorized, this means that is not possible to call
Aurelien DARRAGON53901f42022-10-13 19:49:42 +0200695 a HTTP applet from a proxy in tcp mode. Applets of type 'tcp' can be
Christopher Faulet1e9b1b62021-08-11 10:14:30 +0200696 called from anywhere.
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +0200697
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +0200698 Here, an example of service registration. The service just send an
699 'Hello world' as an http response.
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +0200700
701.. code-block:: lua
702
Pieter Baauw4d7f7662015-11-08 16:38:08 +0100703 core.register_service("hello-world", "http", function(applet)
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +0200704 local response = "Hello World !"
705 applet:set_status(200)
Pieter Baauw2dcb9bc2015-10-01 22:47:12 +0200706 applet:add_header("content-length", string.len(response))
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +0200707 applet:add_header("content-type", "text/plain")
Pieter Baauw2dcb9bc2015-10-01 22:47:12 +0200708 applet:start_response()
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +0200709 applet:send(response)
710 end)
711..
712
Willy Tarreau714f3452021-05-09 06:47:26 +0200713 This example code is used in HAProxy configuration like this:
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +0200714
715::
716
717 frontend example
718 http-request use-service lua.hello-world
719
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100720.. js:function:: core.register_init(func)
721
722 **context**: body
723
724 Register a function executed after the configuration parsing. This is useful
725 to check any parameters.
726
Pieter Baauw4d7f7662015-11-08 16:38:08 +0100727 :param function func: is the Lua function called to work as initializer.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100728
729 The prototype of the Lua function used as argument is:
730
731.. code-block:: lua
732
733 function()
734..
735
736 It takes no input, and no output is expected.
737
Aurelien DARRAGONb8038992023-03-09 16:48:30 +0100738.. js:function:: core.register_task(func[, arg1[, arg2[, ...[, arg4]]]])
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100739
Aurelien DARRAGONc84899c2023-02-20 18:18:59 +0100740 **context**: body, init, task, action, sample-fetch, converter, event
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100741
742 Register and start independent task. The task is started when the HAProxy
743 main scheduler starts. For example this type of tasks can be executed to
Thierry FOURNIER2e4893c2015-03-18 13:37:27 +0100744 perform complex health checks.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100745
Aurelien DARRAGONb8038992023-03-09 16:48:30 +0100746 :param function func: is the Lua function called to work as an async task.
747
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +0200748 Up to 4 optional arguments (all types supported) may be passed to the
749 function. (They will be passed as-is to the task function)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100750
751 The prototype of the Lua function used as argument is:
752
753.. code-block:: lua
754
Aurelien DARRAGONb8038992023-03-09 16:48:30 +0100755 function([arg1[, arg2[, ...[, arg4]]]])
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100756..
757
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +0200758 It takes up to 4 optional arguments (provided when registering), and no
759 output is expected.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100760
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +0100761.. js:function:: core.register_cli([path], usage, func)
762
763 **context**: body
764
Aurelien DARRAGON53901f42022-10-13 19:49:42 +0200765 Register a custom cli that will be available from haproxy stats socket.
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +0100766
767 :param array path: is the sequence of word for which the cli execute the Lua
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +0200768 binding.
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +0100769 :param string usage: is the usage message displayed in the help.
770 :param function func: is the Lua function called to handle the CLI commands.
771
772 The prototype of the Lua function used as argument is:
773
774.. code-block:: lua
775
776 function(AppletTCP, [arg1, [arg2, [...]]])
777..
778
779 I/O are managed with the :ref:`applettcp_class` object. Args are given as
Bertrand Jacquin874a35c2018-09-10 21:26:07 +0100780 parameter. The args embed the registered path. If the path is declared like
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +0100781 this:
782
783.. code-block:: lua
784
785 core.register_cli({"show", "ssl", "stats"}, "Display SSL stats..", function(applet, arg1, arg2, arg3, arg4, arg5)
786 end)
787..
788
789 And we execute this in the prompt:
790
791.. code-block:: text
792
793 > prompt
794 > show ssl stats all
795..
796
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +0200797 Then, arg1, arg2 and arg3 will contains respectively "show", "ssl" and
798 "stats".
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +0100799 arg4 will contain "all". arg5 contains nil.
800
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100801.. js:function:: core.set_nice(nice)
802
803 **context**: task, action, sample-fetch, converter
804
805 Change the nice of the current task or current session.
Thierry FOURNIER2e4893c2015-03-18 13:37:27 +0100806
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100807 :param integer nice: the nice value, it must be between -1024 and 1024.
808
809.. js:function:: core.set_map(filename, key, value)
810
811 **context**: init, task, action, sample-fetch, converter
812
Bertrand Jacquin874a35c2018-09-10 21:26:07 +0100813 Set the value *value* associated to the key *key* in the map referenced by
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100814 *filename*.
815
Thierry FOURNIERdc595002015-12-21 11:13:52 +0100816 :param string filename: the Map reference
817 :param string key: the key to set or replace
818 :param string value: the associated value
819
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100820.. js:function:: core.sleep(int seconds)
821
822 **context**: body, init, task, action
823
824 The `core.sleep()` functions stop the Lua execution between specified seconds.
825
826 :param integer seconds: the required seconds.
827
828.. js:function:: core.tcp()
829
830 **context**: init, task, action
831
832 This function returns a new object of a *socket* class.
833
Thierry FOURNIERdc595002015-12-21 11:13:52 +0100834 :returns: A :ref:`socket_class` object.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100835
William Lallemand00a15022021-11-19 16:02:44 +0100836.. js:function:: core.httpclient()
837
838 **context**: init, task, action
839
840 This function returns a new object of a *httpclient* class.
841
842 :returns: A :ref:`httpclient_class` object.
843
Thierry Fournier1de16592016-01-27 09:49:07 +0100844.. js:function:: core.concat()
845
846 **context**: body, init, task, action, sample-fetch, converter
847
Bertrand Jacquin874a35c2018-09-10 21:26:07 +0100848 This function returns a new concat object.
Thierry Fournier1de16592016-01-27 09:49:07 +0100849
850 :returns: A :ref:`concat_class` object.
851
Thierry FOURNIER0a99b892015-08-26 00:14:17 +0200852.. js:function:: core.done(data)
853
854 **context**: body, init, task, action, sample-fetch, converter
855
856 :param any data: Return some data for the caller. It is useful with
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +0200857 sample-fetches and sample-converters.
Thierry FOURNIER0a99b892015-08-26 00:14:17 +0200858
859 Immediately stops the current Lua execution and returns to the caller which
860 may be a sample fetch, a converter or an action and returns the specified
Thierry Fournier4234dbd2020-11-28 13:18:23 +0100861 value (ignored for actions and init). It is used when the LUA process finishes
862 its work and wants to give back the control to HAProxy without executing the
Thierry FOURNIER0a99b892015-08-26 00:14:17 +0200863 remaining code. It can be seen as a multi-level "return".
864
Thierry FOURNIER486f5a02015-03-16 15:13:03 +0100865.. js:function:: core.yield()
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100866
867 **context**: task, action, sample-fetch, converter
868
869 Give back the hand at the HAProxy scheduler. It is used when the LUA
870 processing consumes a lot of processing time.
871
Thierry FOURNIER / OZON.IO62fec752016-11-10 20:38:11 +0100872.. js:function:: core.parse_addr(address)
873
874 **context**: body, init, task, action, sample-fetch, converter
875
876 :param network: is a string describing an ipv4 or ipv6 address and optionally
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +0200877 its network length, like this: "127.0.0.1/8" or "aaaa::1234/32".
Thierry FOURNIER / OZON.IO62fec752016-11-10 20:38:11 +0100878 :returns: a userdata containing network or nil if an error occurs.
879
Bertrand Jacquin874a35c2018-09-10 21:26:07 +0100880 Parse ipv4 or ipv6 addresses and its facultative associated network.
Thierry FOURNIER / OZON.IO62fec752016-11-10 20:38:11 +0100881
882.. js:function:: core.match_addr(addr1, addr2)
883
884 **context**: body, init, task, action, sample-fetch, converter
885
886 :param addr1: is an address created with "core.parse_addr".
887 :param addr2: is an address created with "core.parse_addr".
Bertrand Jacquin874a35c2018-09-10 21:26:07 +0100888 :returns: boolean, true if the network of the addresses match, else returns
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +0200889 false.
Thierry FOURNIER / OZON.IO62fec752016-11-10 20:38:11 +0100890
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +0200891 Match two networks. For example "127.0.0.1/32" matches "127.0.0.0/8". The
892 order of network is not important.
Thierry FOURNIER / OZON.IO62fec752016-11-10 20:38:11 +0100893
Thierry FOURNIER / OZON.IO8a1027a2016-11-24 20:48:38 +0100894.. js:function:: core.tokenize(str, separators [, noblank])
895
896 **context**: body, init, task, action, sample-fetch, converter
897
898 This function is useful for tokenizing an entry, or splitting some messages.
899 :param string str: The string which will be split.
900 :param string separators: A string containing a list of separators.
901 :param boolean noblank: Ignore empty entries.
902 :returns: an array of string.
903
904 For example:
905
906.. code-block:: lua
907
908 local array = core.tokenize("This function is useful, for tokenizing an entry.", "., ", true)
909 print_r(array)
910..
911
912 Returns this array:
913
914.. code-block:: text
915
916 (table) table: 0x21c01e0 [
917 1: (string) "This"
918 2: (string) "function"
919 3: (string) "is"
920 4: (string) "useful"
921 5: (string) "for"
922 6: (string) "tokenizing"
923 7: (string) "an"
924 8: (string) "entry"
925 ]
926..
927
Aurelien DARRAGONc84899c2023-02-20 18:18:59 +0100928.. js:function:: core.event_sub(event_types, func)
929
930 **context**: body, init, task, action, sample-fetch, converter
931
932 Register a function that will be called on specific system events.
933
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +0200934 :param array event_types: array of string containing the event types you want
935 to subscribe to
936 :param function func: is the Lua function called when one of the subscribed
937 events occur.
Aurelien DARRAGONc84899c2023-02-20 18:18:59 +0100938 :returns: A :ref:`event_sub_class` object.
Aurelien DARRAGON223770d2023-03-10 15:34:35 +0100939 :see: :js:func:`Server.event_sub()`.
Aurelien DARRAGONc84899c2023-02-20 18:18:59 +0100940
941 List of available event types :
942
943 **SERVER** Family:
944
945 * **SERVER_ADD**: when a server is added
946 * **SERVER_DEL**: when a server is removed
947 * **SERVER_DOWN**: when a server state goes from UP to DOWN
948 * **SERVER_UP**: when a server state goes from DOWN to UP
949
950 .. Note::
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +0200951 You may also use **SERVER** in **event_types** to subscribe to all server
952 events types at once.
Aurelien DARRAGONc84899c2023-02-20 18:18:59 +0100953
954 The prototype of the Lua function used as argument is:
955
956.. code-block:: lua
957
Aurelien DARRAGON096b3832023-04-20 11:32:46 +0200958 function(event, event_data, sub, when)
Aurelien DARRAGONc84899c2023-02-20 18:18:59 +0100959..
960
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +0200961 * **event** (*string*): the event type (one of the **event_types** specified
962 when subscribing)
963 * **event_data**: specific to each event family (For **SERVER** family,
964 a :ref:`server_event_class` object)
965 * **sub**: class to manage the subscription from within the event
966 (a :ref:`event_sub_class` object)
Aurelien DARRAGON096b3832023-04-20 11:32:46 +0200967 * **when**: timestamp corresponding to the date when the event was generated.
968 It is an integer representing the number of seconds elapsed since Epoch.
969 It may be provided as optional argument to `os.date()` lua function to
970 convert it to a string according to a given format string.
Aurelien DARRAGONc84899c2023-02-20 18:18:59 +0100971
972 .. Warning::
973 The callback function will only be scheduled on the very same thread that
974 performed the subscription.
975
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +0200976 Moreover, each thread treats events sequentially. It means that if you
977 have, let's say SERVER_UP followed by a SERVER_DOWN in a short timelapse,
978 then the cb function will first be called with SERVER_UP, and once it's
979 done handling the event, the cb function will be called again with
980 SERVER_DOWN.
Aurelien DARRAGONc84899c2023-02-20 18:18:59 +0100981
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +0200982 This is to ensure event consistency when it comes to logging / triggering
983 logic from lua.
Aurelien DARRAGONc84899c2023-02-20 18:18:59 +0100984
985 Your lua cb function may yield if needed, but you're pleased to process the
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +0200986 event as fast as possible to prevent the event queue from growing up,
987 depending on the event flow that is expected for the given subscription.
Aurelien DARRAGONc84899c2023-02-20 18:18:59 +0100988
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +0200989 To prevent abuses, if the event queue for the current subscription goes
990 over a certain amount of unconsumed events, the subscription will pause
991 itself automatically for as long as it takes for your handler to catch up.
992 This would lead to events being missed, so an error will be reported in the
993 logs to warn you about that.
994 This is not something you want to let happen too often, it may indicate
995 that you subscribed to an event that is occurring too frequently or/and
996 that your callback function is too slow to keep up the pace and you should
997 review it.
Aurelien DARRAGONc84899c2023-02-20 18:18:59 +0100998
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +0200999 If you want to do some parallel processing because your callback functions
1000 are slow: you might want to create subtasks from lua using
1001 :js:func:`core.register_task()` from within your callback function to
1002 perform the heavy job in a dedicated task and allow remaining events to be
1003 processed more quickly.
Aurelien DARRAGONc84899c2023-02-20 18:18:59 +01001004
Thierry Fournierf61aa632016-02-19 20:56:00 +01001005.. _proxy_class:
1006
1007Proxy class
1008============
1009
1010.. js:class:: Proxy
1011
1012 This class provides a way for manipulating proxy and retrieving information
1013 like statistics.
1014
Thierry FOURNIER817e7592017-07-24 14:35:04 +02001015.. js:attribute:: Proxy.name
1016
1017 Contain the name of the proxy.
1018
Aurelien DARRAGONc4b24372023-03-02 12:00:06 +01001019 .. warning::
1020 This attribute is now deprecated and will eventually be removed.
1021 Please use :js:func:`Proxy.get_name()` function instead.
1022
Thierry Fournierb0467732022-10-07 12:07:24 +02001023.. js:function:: Proxy.get_name()
1024
1025 Returns the name of the proxy.
1026
Baptiste Assmann46c72552017-10-26 21:51:58 +02001027.. js:attribute:: Proxy.uuid
1028
1029 Contain the unique identifier of the proxy.
1030
Aurelien DARRAGONc4b24372023-03-02 12:00:06 +01001031 .. warning::
1032 This attribute is now deprecated and will eventually be removed.
1033 Please use :js:func:`Proxy.get_uuid()` function instead.
1034
Thierry Fournierb0467732022-10-07 12:07:24 +02001035.. js:function:: Proxy.get_uuid()
1036
1037 Returns the unique identifier of the proxy.
1038
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001039.. js:attribute:: Proxy.servers
1040
Patrick Hemmerc6a1d712018-05-01 21:30:41 -04001041 Contain a table with the attached servers. The table is indexed by server
1042 name, and each server entry is an object of type :ref:`server_class`.
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001043
Adis Nezirovic8878f8e2018-07-13 12:18:33 +02001044.. js:attribute:: Proxy.stktable
1045
1046 Contains a stick table object attached to the proxy.
1047
Thierry Fournierff480422016-02-25 08:36:46 +01001048.. js:attribute:: Proxy.listeners
1049
Patrick Hemmerc6a1d712018-05-01 21:30:41 -04001050 Contain a table with the attached listeners. The table is indexed by listener
1051 name, and each each listeners entry is an object of type
1052 :ref:`listener_class`.
Thierry Fournierff480422016-02-25 08:36:46 +01001053
Thierry Fournierf61aa632016-02-19 20:56:00 +01001054.. js:function:: Proxy.pause(px)
1055
1056 Pause the proxy. See the management socket documentation for more information.
1057
1058 :param class_proxy px: A :ref:`proxy_class` which indicates the manipulated
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02001059 proxy.
Thierry Fournierf61aa632016-02-19 20:56:00 +01001060
1061.. js:function:: Proxy.resume(px)
1062
1063 Resume the proxy. See the management socket documentation for more
1064 information.
1065
1066 :param class_proxy px: A :ref:`proxy_class` which indicates the manipulated
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02001067 proxy.
Thierry Fournierf61aa632016-02-19 20:56:00 +01001068
1069.. js:function:: Proxy.stop(px)
1070
1071 Stop the proxy. See the management socket documentation for more information.
1072
1073 :param class_proxy px: A :ref:`proxy_class` which indicates the manipulated
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02001074 proxy.
Thierry Fournierf61aa632016-02-19 20:56:00 +01001075
1076.. js:function:: Proxy.shut_bcksess(px)
1077
1078 Kill the session attached to a backup server. See the management socket
1079 documentation for more information.
1080
1081 :param class_proxy px: A :ref:`proxy_class` which indicates the manipulated
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02001082 proxy.
Thierry Fournierf61aa632016-02-19 20:56:00 +01001083
1084.. js:function:: Proxy.get_cap(px)
1085
1086 Returns a string describing the capabilities of the proxy.
1087
1088 :param class_proxy px: A :ref:`proxy_class` which indicates the manipulated
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02001089 proxy.
Thierry Fournierf61aa632016-02-19 20:56:00 +01001090 :returns: a string "frontend", "backend", "proxy" or "ruleset".
1091
1092.. js:function:: Proxy.get_mode(px)
1093
1094 Returns a string describing the mode of the current proxy.
1095
1096 :param class_proxy px: A :ref:`proxy_class` which indicates the manipulated
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02001097 proxy.
Thierry Fournierf61aa632016-02-19 20:56:00 +01001098 :returns: a string "tcp", "http", "health" or "unknown"
1099
1100.. js:function:: Proxy.get_stats(px)
1101
Bertrand Jacquin874a35c2018-09-10 21:26:07 +01001102 Returns a table containing the proxy statistics. The statistics returned are
Thierry Fournierf61aa632016-02-19 20:56:00 +01001103 not the same if the proxy is frontend or a backend.
1104
1105 :param class_proxy px: A :ref:`proxy_class` which indicates the manipulated
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02001106 proxy.
Patrick Hemmerc6a1d712018-05-01 21:30:41 -04001107 :returns: a key/value table containing stats
Thierry Fournierf61aa632016-02-19 20:56:00 +01001108
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001109.. _server_class:
1110
1111Server class
1112============
1113
Patrick Hemmerc6a1d712018-05-01 21:30:41 -04001114.. js:class:: Server
1115
1116 This class provides a way for manipulating servers and retrieving information.
1117
Patrick Hemmera62ae7e2018-04-29 14:23:48 -04001118.. js:attribute:: Server.name
1119
1120 Contain the name of the server.
1121
Aurelien DARRAGONc4b24372023-03-02 12:00:06 +01001122 .. warning::
1123 This attribute is now deprecated and will eventually be removed.
1124 Please use :js:func:`Server.get_name()` function instead.
1125
Thierry Fournierb0467732022-10-07 12:07:24 +02001126.. js:function:: Server.get_name(sv)
1127
1128 Returns the name of the server.
1129
Patrick Hemmera62ae7e2018-04-29 14:23:48 -04001130.. js:attribute:: Server.puid
1131
1132 Contain the proxy unique identifier of the server.
1133
Aurelien DARRAGONc4b24372023-03-02 12:00:06 +01001134 .. warning::
1135 This attribute is now deprecated and will eventually be removed.
1136 Please use :js:func:`Server.get_puid()` function instead.
1137
Thierry Fournierb0467732022-10-07 12:07:24 +02001138.. js:function:: Server.get_puid(sv)
1139
1140 Returns the proxy unique identifier of the server.
1141
Aurelien DARRAGON94ee6632023-03-10 15:11:27 +01001142.. js:function:: Server.get_rid(sv)
1143
1144 Returns the rid (revision ID) of the server.
1145 It is an unsigned integer that is set upon server creation. Value is derived
1146 from a global counter that starts at 0 and is incremented each time one or
1147 multiple server deletions are followed by a server addition (meaning that
1148 old name/id reuse could occur).
1149
1150 Combining server name/id with server rid yields a process-wide unique
1151 identifier.
1152
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001153.. js:function:: Server.is_draining(sv)
1154
Patrick Hemmerc6a1d712018-05-01 21:30:41 -04001155 Return true if the server is currently draining sticky connections.
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001156
1157 :param class_server sv: A :ref:`server_class` which indicates the manipulated
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02001158 server.
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001159 :returns: a boolean
1160
Aurelien DARRAGONc72051d2023-03-29 10:44:38 +02001161.. js:function:: Server.is_backup(sv)
1162
1163 Return true if the server is a backup server
1164
1165 :param class_server sv: A :ref:`server_class` which indicates the manipulated
1166 server.
1167 :returns: a boolean
1168
Aurelien DARRAGON7a03dee2023-03-29 10:49:30 +02001169.. js:function:: Server.is_dynamic(sv)
1170
1171 Return true if the server was instantiated at runtime (e.g.: from the cli)
1172
1173 :param class_server sv: A :ref:`server_class` which indicates the manipulated
1174 server.
1175 :returns: a boolean
1176
Patrick Hemmer32d539f2018-04-29 14:25:46 -04001177.. js:function:: Server.set_maxconn(sv, weight)
1178
1179 Dynamically change the maximum connections of the server. See the management
1180 socket documentation for more information about the format of the string.
1181
1182 :param class_server sv: A :ref:`server_class` which indicates the manipulated
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02001183 server.
Patrick Hemmer32d539f2018-04-29 14:25:46 -04001184 :param string maxconn: A string describing the server maximum connections.
1185
1186.. js:function:: Server.get_maxconn(sv, weight)
1187
1188 This function returns an integer representing the server maximum connections.
1189
1190 :param class_server sv: A :ref:`server_class` which indicates the manipulated
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02001191 server.
Patrick Hemmer32d539f2018-04-29 14:25:46 -04001192 :returns: an integer.
1193
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001194.. js:function:: Server.set_weight(sv, weight)
1195
Patrick Hemmerc6a1d712018-05-01 21:30:41 -04001196 Dynamically change the weight of the server. See the management socket
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001197 documentation for more information about the format of the string.
1198
1199 :param class_server sv: A :ref:`server_class` which indicates the manipulated
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02001200 server.
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001201 :param string weight: A string describing the server weight.
1202
1203.. js:function:: Server.get_weight(sv)
1204
Patrick Hemmerc6a1d712018-05-01 21:30:41 -04001205 This function returns an integer representing the server weight.
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001206
1207 :param class_server sv: A :ref:`server_class` which indicates the manipulated
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02001208 server.
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001209 :returns: an integer.
1210
Joseph C. Sible49bbf522020-05-04 22:20:32 -04001211.. js:function:: Server.set_addr(sv, addr[, port])
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001212
Patrick Hemmerc6a1d712018-05-01 21:30:41 -04001213 Dynamically change the address of the server. See the management socket
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001214 documentation for more information about the format of the string.
1215
1216 :param class_server sv: A :ref:`server_class` which indicates the manipulated
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02001217 server.
Patrick Hemmerc6a1d712018-05-01 21:30:41 -04001218 :param string addr: A string describing the server address.
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001219
1220.. js:function:: Server.get_addr(sv)
1221
Patrick Hemmerc6a1d712018-05-01 21:30:41 -04001222 Returns a string describing the address of the server.
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001223
1224 :param class_server sv: A :ref:`server_class` which indicates the manipulated
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02001225 server.
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001226 :returns: A string
1227
1228.. js:function:: Server.get_stats(sv)
1229
1230 Returns server statistics.
1231
1232 :param class_server sv: A :ref:`server_class` which indicates the manipulated
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02001233 server.
Patrick Hemmerc6a1d712018-05-01 21:30:41 -04001234 :returns: a key/value table containing stats
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001235
Aurelien DARRAGON3889efa2023-04-03 14:00:58 +02001236.. js:function:: Server.get_proxy(sv)
1237
1238 Returns the parent proxy to which the server belongs.
1239
1240 :param class_server sv: A :ref:`server_class` which indicates the manipulated
1241 server.
1242 :returns: a :ref:`proxy_class` or nil if not available
1243
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001244.. js:function:: Server.shut_sess(sv)
1245
1246 Shutdown all the sessions attached to the server. See the management socket
1247 documentation for more information about this function.
1248
1249 :param class_server sv: A :ref:`server_class` which indicates the manipulated
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02001250 server.
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001251
1252.. js:function:: Server.set_drain(sv)
1253
1254 Drain sticky sessions. See the management socket documentation for more
1255 information about this function.
1256
1257 :param class_server sv: A :ref:`server_class` which indicates the manipulated
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02001258 server.
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001259
1260.. js:function:: Server.set_maint(sv)
1261
1262 Set maintenance mode. See the management socket documentation for more
1263 information about this function.
1264
1265 :param class_server sv: A :ref:`server_class` which indicates the manipulated
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02001266 server.
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001267
1268.. js:function:: Server.set_ready(sv)
1269
1270 Set normal mode. See the management socket documentation for more information
1271 about this function.
1272
1273 :param class_server sv: A :ref:`server_class` which indicates the manipulated
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02001274 server.
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001275
1276.. js:function:: Server.check_enable(sv)
1277
1278 Enable health checks. See the management socket documentation for more
1279 information about this function.
1280
1281 :param class_server sv: A :ref:`server_class` which indicates the manipulated
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02001282 server.
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001283
1284.. js:function:: Server.check_disable(sv)
1285
1286 Disable health checks. See the management socket documentation for more
1287 information about this function.
1288
1289 :param class_server sv: A :ref:`server_class` which indicates the manipulated
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02001290 server.
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001291
1292.. js:function:: Server.check_force_up(sv)
1293
1294 Force health-check up. See the management socket documentation for more
1295 information about this function.
1296
1297 :param class_server sv: A :ref:`server_class` which indicates the manipulated
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02001298 server.
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001299
1300.. js:function:: Server.check_force_nolb(sv)
1301
1302 Force health-check nolb mode. See the management socket documentation for more
1303 information about this function.
1304
1305 :param class_server sv: A :ref:`server_class` which indicates the manipulated
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02001306 server.
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001307
1308.. js:function:: Server.check_force_down(sv)
1309
1310 Force health-check down. See the management socket documentation for more
1311 information about this function.
1312
1313 :param class_server sv: A :ref:`server_class` which indicates the manipulated
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02001314 server.
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001315
1316.. js:function:: Server.agent_enable(sv)
1317
1318 Enable agent check. See the management socket documentation for more
1319 information about this function.
1320
1321 :param class_server sv: A :ref:`server_class` which indicates the manipulated
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02001322 server.
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001323
1324.. js:function:: Server.agent_disable(sv)
1325
1326 Disable agent check. See the management socket documentation for more
1327 information about this function.
1328
1329 :param class_server sv: A :ref:`server_class` which indicates the manipulated
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02001330 server.
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001331
1332.. js:function:: Server.agent_force_up(sv)
1333
1334 Force agent check up. See the management socket documentation for more
1335 information about this function.
1336
1337 :param class_server sv: A :ref:`server_class` which indicates the manipulated
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02001338 server.
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001339
1340.. js:function:: Server.agent_force_down(sv)
1341
1342 Force agent check down. See the management socket documentation for more
1343 information about this function.
1344
1345 :param class_server sv: A :ref:`server_class` which indicates the manipulated
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02001346 server.
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001347
Aurelien DARRAGON406511a2023-03-29 11:30:36 +02001348.. js:function:: Server.tracking(sv)
1349
1350 Check if the current server is tracking another server.
1351
1352 :param class_server sv: A :ref:`server_class` which indicates the manipulated
1353 server.
1354 :returns: A :ref:`server_class` which indicates the tracked server or nil if
1355 the server doesn't track another one.
1356
Aurelien DARRAGON4be36a12023-03-29 14:02:39 +02001357.. js:function:: Server.get_trackers(sv)
1358
1359 Check if the current server is being tracked by other servers.
1360
1361 :param class_server sv: A :ref:`server_class` which indicates the manipulated
1362 server.
1363 :returns: An array of :ref:`server_class` which indicates the tracking
1364 servers (might be empty)
1365
Aurelien DARRAGON223770d2023-03-10 15:34:35 +01001366.. js:function:: Server.event_sub(sv, event_types, func)
1367
1368 Register a function that will be called on specific server events.
1369 It works exactly like :js:func:`core.event_sub()` except that the subscription
1370 will be performed within the server dedicated subscription list instead of the
1371 global one.
1372 (Your callback function will only be called for server events affecting sv)
1373
1374 See :js:func:`core.event_sub()` for function usage.
1375
1376 A key advantage to using :js:func:`Server.event_sub()` over
1377 :js:func:`core.event_sub()` for servers is that :js:func:`Server.event_sub()`
1378 allows you to be notified for servers events of a single server only.
1379 It removes the needs for extra filtering in your callback function if you only
1380 care about a single server, and also prevents useless wakeups.
1381
1382 For instance, if you want to be notified for UP/DOWN events on a given set of
Ilya Shipitsinccf80122023-04-22 20:20:39 +02001383 servers, it is recommended to perform multiple per-server subscriptions since
Aurelien DARRAGON223770d2023-03-10 15:34:35 +01001384 it will be more efficient that doing a single global subscription that will
1385 filter the received events.
1386 Unless you really want to be notified for servers events of ALL servers of
1387 course, which could make sense given you setup but should be avoided if you
1388 have an important number of servers as it will add a significant load on your
1389 haproxy process in case of multiple servers state change in a short amount of
1390 time.
1391
1392 .. Note::
1393 You may also combine :js:func:`core.event_sub()` with
1394 :js:func:`Server.event_sub()`.
1395
1396 Also, don't forget that you can use :js:func:`core.register_task()` from
1397 your callback function if needed. (ie: parallel work)
1398
1399 Here is a working example combining :js:func:`core.event_sub()` with
1400 :js:func:`Server.event_sub()` and :js:func:`core.register_task()`
1401 (This only serves as a demo, this is not necessarily useful to do so)
1402
1403.. code-block:: lua
1404
1405 core.event_sub({"SERVER_ADD"}, function(event, data, sub)
1406 -- in the global event handler
1407 if data["reference"] ~= nil then
1408 print("Tracking new server: ", data["name"])
1409 data["reference"]:event_sub({"SERVER_UP", "SERVER_DOWN"}, function(event, data, sub)
1410 -- in the per-server event handler
1411 if data["reference"] ~= nil then
1412 core.register_task(function(server)
1413 -- subtask to perform some async work (e.g.: HTTP API calls, sending emails...)
1414 print("ASYNC: SERVER ", server:get_name(), " is ", event == "SERVER_UP" and "UP" or "DOWN")
1415 end, data["reference"])
1416 end
1417 end)
1418 end
1419 end)
1420
1421..
1422
1423 In this example, we will first track global server addition events.
1424 For each newly added server ("add server" on the cli), we will register a
1425 UP/DOWN server subscription.
1426 Then, the callback function will schedule the event handling in an async
1427 subtask which will receive the server reference as an argument.
1428
Thierry Fournierff480422016-02-25 08:36:46 +01001429.. _listener_class:
1430
1431Listener class
1432==============
1433
1434.. js:function:: Listener.get_stats(ls)
1435
1436 Returns server statistics.
1437
1438 :param class_listener ls: A :ref:`listener_class` which indicates the
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02001439 manipulated listener.
Patrick Hemmerc6a1d712018-05-01 21:30:41 -04001440 :returns: a key/value table containing stats
Thierry Fournierff480422016-02-25 08:36:46 +01001441
Aurelien DARRAGONc84899c2023-02-20 18:18:59 +01001442.. _event_sub_class:
1443
1444EventSub class
1445==============
1446
1447.. js:function:: EventSub.unsub()
1448
1449 End the subscription, the callback function will not be called again.
1450
1451.. _server_event_class:
1452
1453ServerEvent class
1454=================
1455
1456.. js:attribute:: ServerEvent.name
1457
1458 Contains the name of the server.
1459
1460.. js:attribute:: ServerEvent.puid
1461
1462 Contains the proxy-unique uid of the server
1463
1464.. js:attribute:: ServerEvent.rid
1465
1466 Contains the revision ID of the server
1467
1468.. js:attribute:: ServerEvent.proxy_name
1469
1470 Contains the name of the proxy to which the server belongs
1471
Aurelien DARRAGON55f84c72023-03-22 17:49:04 +01001472.. js:attribute:: ServerEvent.proxy_uuid
1473
1474 Contains the uuid of the proxy to which the server belongs
1475
Aurelien DARRAGONc84899c2023-02-20 18:18:59 +01001476.. js:attribute:: ServerEvent.reference
1477
1478 Reference to the live server (A :ref:`server_class`).
1479
1480 .. Warning::
1481 Not available if the server was removed in the meantime.
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02001482 (Will never be set for SERVER_DEL event since the server does not exist
1483 anymore)
Aurelien DARRAGONc84899c2023-02-20 18:18:59 +01001484
Thierry Fournier1de16592016-01-27 09:49:07 +01001485.. _concat_class:
1486
1487Concat class
1488============
1489
1490.. js:class:: Concat
1491
1492 This class provides a fast way for string concatenation. The way using native
1493 Lua concatenation like the code below is slow for some reasons.
1494
1495.. code-block:: lua
1496
1497 str = "string1"
1498 str = str .. ", string2"
1499 str = str .. ", string3"
1500..
1501
1502 For each concatenation, Lua:
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02001503 - allocates memory for the result,
1504 - catenates the two string copying the strings in the new memory block,
1505 - frees the old memory block containing the string which is no longer used.
1506
Thierry Fournier1de16592016-01-27 09:49:07 +01001507 This process does many memory move, allocation and free. In addition, the
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02001508 memory is not really freed, it is just marked as unused and waits for the
Thierry Fournier1de16592016-01-27 09:49:07 +01001509 garbage collector.
1510
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02001511 The Concat class provides an alternative way to concatenate strings. It uses
Thierry Fournier1de16592016-01-27 09:49:07 +01001512 the internal Lua mechanism (it does not allocate memory), but it doesn't copy
1513 the data more than once.
1514
1515 On my computer, the following loops spends 0.2s for the Concat method and
1516 18.5s for the pure Lua implementation. So, the Concat class is about 1000x
1517 faster than the embedded solution.
1518
1519.. code-block:: lua
1520
1521 for j = 1, 100 do
1522 c = core.concat()
1523 for i = 1, 20000 do
1524 c:add("#####")
1525 end
1526 end
1527..
1528
1529.. code-block:: lua
1530
1531 for j = 1, 100 do
1532 c = ""
1533 for i = 1, 20000 do
1534 c = c .. "#####"
1535 end
1536 end
1537..
1538
1539.. js:function:: Concat.add(concat, string)
1540
1541 This function adds a string to the current concatenated string.
1542
1543 :param class_concat concat: A :ref:`concat_class` which contains the currently
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02001544 built string.
Bertrand Jacquin874a35c2018-09-10 21:26:07 +01001545 :param string string: A new string to concatenate to the current built
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02001546 string.
Thierry Fournier1de16592016-01-27 09:49:07 +01001547
1548.. js:function:: Concat.dump(concat)
1549
Bertrand Jacquin874a35c2018-09-10 21:26:07 +01001550 This function returns the concatenated string.
Thierry Fournier1de16592016-01-27 09:49:07 +01001551
1552 :param class_concat concat: A :ref:`concat_class` which contains the currently
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02001553 built string.
Thierry Fournier1de16592016-01-27 09:49:07 +01001554 :returns: the concatenated string
1555
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001556.. _fetches_class:
1557
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001558Fetches class
1559=============
1560
1561.. js:class:: Fetches
1562
1563 This class contains a lot of internal HAProxy sample fetches. See the
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02001564 HAProxy "configuration.txt" documentation for more information.
1565 (chapters 7.3.2 to 7.3.6)
Thierry FOURNIER2e4893c2015-03-18 13:37:27 +01001566
Christopher Faulet1e9b1b62021-08-11 10:14:30 +02001567 .. warning::
1568 some sample fetches are not available in some context. These limitations
1569 are specified in this documentation when they're useful.
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001570
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01001571 :see: :js:attr:`TXN.f`
1572 :see: :js:attr:`TXN.sf`
Thierry FOURNIER2e4893c2015-03-18 13:37:27 +01001573
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02001574 Fetches are useful to:
Thierry FOURNIER2e4893c2015-03-18 13:37:27 +01001575
1576 * get system time,
1577 * get environment variable,
1578 * get random numbers,
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02001579 * know backend status like the number of users in queue or the number of
Thierry FOURNIER2e4893c2015-03-18 13:37:27 +01001580 connections established,
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02001581 * get client information like ip source or destination,
Thierry FOURNIER2e4893c2015-03-18 13:37:27 +01001582 * deal with stick tables,
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02001583 * fetch established SSL information,
1584 * fetch HTTP information like headers or method.
Thierry FOURNIER2e4893c2015-03-18 13:37:27 +01001585
1586.. code-block:: lua
1587
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001588 function action(txn)
1589 -- Get source IP
1590 local clientip = txn.f:src()
1591 end
Thierry FOURNIER2e4893c2015-03-18 13:37:27 +01001592..
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001593
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001594.. _converters_class:
1595
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001596Converters class
1597================
1598
1599.. js:class:: Converters
1600
1601 This class contains a lot of internal HAProxy sample converters. See the
Thierry FOURNIER2e4893c2015-03-18 13:37:27 +01001602 HAProxy documentation "configuration.txt" for more information about her
1603 usage. Its the chapter 7.3.1.
1604
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01001605 :see: :js:attr:`TXN.c`
1606 :see: :js:attr:`TXN.sc`
Thierry FOURNIER2e4893c2015-03-18 13:37:27 +01001607
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02001608 Converters provides stateful transformation. They are useful to:
Thierry FOURNIER2e4893c2015-03-18 13:37:27 +01001609
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02001610 * convert input to base64,
1611 * apply hash on input string (djb2, crc32, sdbm, wt6),
Thierry FOURNIER2e4893c2015-03-18 13:37:27 +01001612 * format date,
1613 * json escape,
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02001614 * extract preferred language comparing two lists,
Thierry FOURNIER2e4893c2015-03-18 13:37:27 +01001615 * turn to lower or upper chars,
1616 * deal with stick tables.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001617
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001618.. _channel_class:
1619
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001620Channel class
1621=============
1622
1623.. js:class:: Channel
1624
Christopher Faulet5a2c6612021-08-15 20:35:25 +02001625 **context**: action, sample-fetch, convert, filter
1626
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001627 HAProxy uses two buffers for the processing of the requests. The first one is
1628 used with the request data (from the client to the server) and the second is
1629 used for the response data (from the server to the client).
1630
1631 Each buffer contains two types of data. The first type is the incoming data
1632 waiting for a processing. The second part is the outgoing data already
1633 processed. Usually, the incoming data is processed, after it is tagged as
1634 outgoing data, and finally it is sent. The following functions provides tools
1635 for manipulating these data in a buffer.
1636
1637 The following diagram shows where the channel class function are applied.
1638
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001639 .. image:: _static/channel.png
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001640
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001641 .. warning::
1642 It is not possible to read from the response in request action, and it is
Boyang Li60cfe8b2022-05-10 18:11:00 +00001643 not possible to read from the request channel in response action.
Christopher Faulet09530392021-06-14 11:43:18 +02001644
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001645 .. warning::
1646 It is forbidden to alter the Channels buffer from HTTP contexts. So only
1647 :js:func:`Channel.input`, :js:func:`Channel.output`,
1648 :js:func:`Channel.may_recv`, :js:func:`Channel.is_full` and
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02001649 :js:func:`Channel.is_resp` can be called from a HTTP context.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001650
Christopher Faulet5a2c6612021-08-15 20:35:25 +02001651 All the functions provided by this class are available in the
1652 **sample-fetches**, **actions** and **filters** contexts. For **filters**,
1653 incoming data (offset and length) are relative to the filter. Some functions
Boyang Li60cfe8b2022-05-10 18:11:00 +00001654 may yield, but only for **actions**. Yield is not possible for
Christopher Faulet5a2c6612021-08-15 20:35:25 +02001655 **sample-fetches**, **converters** and **filters**.
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001656
1657.. js:function:: Channel.append(channel, string)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001658
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001659 This function copies the string **string** at the end of incoming data of the
1660 channel buffer. The function returns the copied length on success or -1 if
1661 data cannot be copied.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001662
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001663 Same that :js:func:`Channel.insert(channel, string, channel:input())`.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001664
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001665 :param class_channel channel: The manipulated Channel.
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02001666 :param string string: The data to copy at the end of incoming data.
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001667 :returns: an integer containing the amount of bytes copied or -1.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001668
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001669.. js:function:: Channel.data(channel [, offset [, length]])
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001670
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001671 This function returns **length** bytes of incoming data from the channel
1672 buffer, starting at the offset **offset**. The data are not removed from the
1673 buffer.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001674
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001675 By default, if no length is provided, all incoming data found, starting at the
1676 given offset, are returned. If **length** is set to -1, the function tries to
Christopher Faulet5a2c6612021-08-15 20:35:25 +02001677 retrieve a maximum of data and, if called by an action, it yields if
1678 necessary. It also waits for more data if the requested length exceeds the
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02001679 available amount of incoming data. Not providing an offset is the same as
Ilya Shipitsinff0f2782021-08-22 22:18:07 +05001680 setting it to 0. A positive offset is relative to the beginning of incoming
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02001681 data of the channel buffer while negative offset is relative to the end.
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001682
1683 If there is no incoming data and the channel can't receive more data, a 'nil'
1684 value is returned.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001685
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001686 :param class_channel channel: The manipulated Channel.
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001687 :param integer offset: *optional* The offset in incoming data to start to get
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02001688 data. 0 by default. May be negative to be relative to the end of incoming
1689 data.
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001690 :param integer length: *optional* The expected length of data to retrieve. All
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02001691 incoming data by default. May be set to -1 to get a maximum of data.
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001692 :returns: a string containing the data found or nil.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001693
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001694.. js:function:: Channel.forward(channel, length)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001695
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001696 This function forwards **length** bytes of data from the channel buffer. If
Christopher Faulet5a2c6612021-08-15 20:35:25 +02001697 the requested length exceeds the available amount of incoming data, and if
1698 called by an action, the function yields, waiting for more data to forward. It
1699 returns the amount of data forwarded.
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001700
1701 :param class_channel channel: The manipulated Channel.
1702 :param integer int: The amount of data to forward.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001703
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001704.. js:function:: Channel.input(channel)
1705
Christopher Faulet5a2c6612021-08-15 20:35:25 +02001706 This function returns the length of incoming data in the channel buffer. When
1707 called by a filter, this value is relative to the filter.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001708
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001709 :param class_channel channel: The manipulated Channel.
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001710 :returns: an integer containing the amount of available bytes.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001711
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001712.. js:function:: Channel.insert(channel, string [, offset])
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001713
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001714 This function copies the string **string** at the offset **offset** in
1715 incoming data of the channel buffer. The function returns the copied length on
1716 success or -1 if data cannot be copied.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001717
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001718 By default, if no offset is provided, the string is copied in front of
Ilya Shipitsinff0f2782021-08-22 22:18:07 +05001719 incoming data. A positive offset is relative to the beginning of incoming data
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001720 of the channel buffer while negative offset is relative to their end.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001721
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001722 :param class_channel channel: The manipulated Channel.
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02001723 :param string string: The data to copy into incoming data.
1724 :param integer offset: *optional* The offset in incoming data where to copy
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02001725 data. 0 by default. May be negative to be relative to the end of incoming
1726 data.
Pieter Baauw4d7f7662015-11-08 16:38:08 +01001727 :returns: an integer containing the amount of bytes copied or -1.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001728
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001729.. js:function:: Channel.is_full(channel)
1730
1731 This function returns true if the channel buffer is full.
1732
Christopher Faulet5a2c6612021-08-15 20:35:25 +02001733 :param class_channel channel: The manipulated Channel.
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001734 :returns: a boolean
1735
1736.. js:function:: Channel.is_resp(channel)
1737
1738 This function returns true if the channel is the response one.
1739
Christopher Faulet5a2c6612021-08-15 20:35:25 +02001740 :param class_channel channel: The manipulated Channel.
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001741 :returns: a boolean
1742
1743.. js:function:: Channel.line(channel [, offset [, length]])
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001744
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001745 This function parses **length** bytes of incoming data of the channel buffer,
1746 starting at offset **offset**, and returns the first line found, including the
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02001747 '\\n'. The data are not removed from the buffer. If no line is found, all
1748 data are returned.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001749
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001750 By default, if no length is provided, all incoming data, starting at the given
1751 offset, are evaluated. If **length** is set to -1, the function tries to
Christopher Faulet5a2c6612021-08-15 20:35:25 +02001752 retrieve a maximum of data and, if called by an action, yields if
1753 necessary. It also waits for more data if the requested length exceeds the
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02001754 available amount of incoming data. Not providing an offset is the same as
Ilya Shipitsinff0f2782021-08-22 22:18:07 +05001755 setting it to 0. A positive offset is relative to the beginning of incoming
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02001756 data of the channel buffer while negative offset is relative to the end.
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001757
1758 If there is no incoming data and the channel can't receive more data, a 'nil'
1759 value is returned.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001760
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001761 :param class_channel channel: The manipulated Channel.
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02001762 :param integer offset: *optional* The offset in incoming data to start to
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02001763 parse data. 0 by default. May be negative to be relative to the end of
1764 incoming data.
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001765 :param integer length: *optional* The length of data to parse. All incoming
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02001766 data by default. May be set to -1 to get a maximum of data.
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001767 :returns: a string containing the line found or nil.
1768
1769.. js:function:: Channel.may_recv(channel)
1770
1771 This function returns true if the channel may still receive data.
1772
Christopher Faulet5a2c6612021-08-15 20:35:25 +02001773 :param class_channel channel: The manipulated Channel.
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001774 :returns: a boolean
1775
1776.. js:function:: Channel.output(channel)
1777
Christopher Faulet5a2c6612021-08-15 20:35:25 +02001778 This function returns the length of outgoing data of the channel buffer. When
1779 called by a filter, this value is relative to the filter.
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001780
1781 :param class_channel channel: The manipulated Channel.
1782 :returns: an integer containing the amount of available bytes.
1783
1784.. js:function:: Channel.prepend(channel, string)
1785
1786 This function copies the string **string** in front of incoming data of the
1787 channel buffer. The function returns the copied length on success or -1 if
1788 data cannot be copied.
1789
1790 Same that :js:func:`Channel.insert(channel, string, 0)`.
1791
1792 :param class_channel channel: The manipulated Channel.
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02001793 :param string string: The data to copy in front of incoming data.
Pieter Baauw4d7f7662015-11-08 16:38:08 +01001794 :returns: an integer containing the amount of bytes copied or -1.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001795
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001796.. js:function:: Channel.remove(channel [, offset [, length]])
1797
1798 This function removes **length** bytes of incoming data of the channel buffer,
1799 starting at offset **offset**. This function returns number of bytes removed
1800 on success.
1801
1802 By default, if no length is provided, all incoming data, starting at the given
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02001803 offset, are removed. Not providing an offset is the same as setting it
Ilya Shipitsinff0f2782021-08-22 22:18:07 +05001804 to 0. A positive offset is relative to the beginning of incoming data of the
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02001805 channel buffer while negative offset is relative to the end.
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001806
1807 :param class_channel channel: The manipulated Channel.
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02001808 :param integer offset: *optional* The offset in incoming data where to start
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02001809 to remove data. 0 by default. May be negative to be relative to the end of
1810 incoming data.
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001811 :param integer length: *optional* The length of data to remove. All incoming
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02001812 data by default.
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001813 :returns: an integer containing the amount of bytes removed.
1814
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001815.. js:function:: Channel.send(channel, string)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001816
Christopher Faulet5a2c6612021-08-15 20:35:25 +02001817 This function requires immediate send of the string **string**. It means the
Ilya Shipitsinff0f2782021-08-22 22:18:07 +05001818 string is copied at the beginning of incoming data of the channel buffer and
Christopher Faulet5a2c6612021-08-15 20:35:25 +02001819 immediately forwarded. Unless if the connection is close, and if called by an
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02001820 action, this function yields to copy and forward all the string.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001821
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001822 :param class_channel channel: The manipulated Channel.
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001823 :param string string: The data to send.
Pieter Baauw4d7f7662015-11-08 16:38:08 +01001824 :returns: an integer containing the amount of bytes copied or -1.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001825
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001826.. js:function:: Channel.set(channel, string [, offset [, length]])
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001827
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02001828 This function replaces **length** bytes of incoming data of the channel
1829 buffer, starting at offset **offset**, by the string **string**. The function
1830 returns the copied length on success or -1 if data cannot be copied.
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001831
1832 By default, if no length is provided, all incoming data, starting at the given
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02001833 offset, are replaced. Not providing an offset is the same as setting it
Ilya Shipitsinff0f2782021-08-22 22:18:07 +05001834 to 0. A positive offset is relative to the beginning of incoming data of the
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02001835 channel buffer while negative offset is relative to the end.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001836
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001837 :param class_channel channel: The manipulated Channel.
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02001838 :param string string: The data to copy into incoming data.
1839 :param integer offset: *optional* The offset in incoming data where to start
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02001840 the data replacement. 0 by default. May be negative to be relative to the
1841 end of incoming data.
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001842 :param integer length: *optional* The length of data to replace. All incoming
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02001843 data by default.
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001844 :returns: an integer containing the amount of bytes copied or -1.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001845
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001846.. js:function:: Channel.dup(channel)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001847
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001848 **DEPRECATED**
1849
1850 This function returns all incoming data found in the channel buffer. The data
Boyang Li60cfe8b2022-05-10 18:11:00 +00001851 are not removed from the buffer and can be reprocessed later.
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001852
1853 If there is no incoming data and the channel can't receive more data, a 'nil'
1854 value is returned.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001855
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001856 :param class_channel channel: The manipulated Channel.
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001857 :returns: a string containing all data found or nil.
1858
1859 .. warning::
1860 This function is deprecated. :js:func:`Channel.data()` must be used
1861 instead.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001862
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001863.. js:function:: Channel.get(channel)
1864
1865 **DEPRECATED**
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001866
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001867 This function returns all incoming data found in the channel buffer and remove
1868 them from the buffer.
1869
1870 If there is no incoming data and the channel can't receive more data, a 'nil'
1871 value is returned.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001872
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001873 :param class_channel channel: The manipulated Channel.
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001874 :returns: a string containing all the data found or nil.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001875
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001876 .. warning::
1877 This function is deprecated. :js:func:`Channel.data()` must be used to
1878 retrieve data followed by a call to :js:func:`Channel:remove()` to remove
1879 data.
Thierry FOURNIER / OZON.IO65192f32016-11-07 15:28:40 +01001880
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001881 .. code-block:: lua
Thierry FOURNIER / OZON.IO65192f32016-11-07 15:28:40 +01001882
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001883 local data = chn:data()
1884 chn:remove(0, data:len())
1885
1886 ..
1887
1888.. js:function:: Channel.getline(channel)
1889
1890 **DEPRECATED**
1891
1892 This function returns the first line found in incoming data of the channel
Christopher Faulet5a2c6612021-08-15 20:35:25 +02001893 buffer, including the '\\n'. The returned data are removed from the buffer. If
1894 no line is found, and if called by an action, this function yields to wait for
1895 more data, except if the channel can't receive more data. In this case all
1896 data are returned.
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001897
1898 If there is no incoming data and the channel can't receive more data, a 'nil'
1899 value is returned.
1900
1901 :param class_channel channel: The manipulated Channel.
1902 :returns: a string containing the line found or nil.
1903
1904 .. warning::
Boyang Li60cfe8b2022-05-10 18:11:00 +00001905 This function is deprecated. :js:func:`Channel.line()` must be used to
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001906 retrieve a line followed by a call to :js:func:`Channel:remove()` to remove
1907 data.
1908
1909 .. code-block:: lua
1910
1911 local line = chn:line(0, -1)
1912 chn:remove(0, line:len())
1913
1914 ..
1915
1916.. js:function:: Channel.get_in_len(channel)
1917
Boyang Li60cfe8b2022-05-10 18:11:00 +00001918 **DEPRECATED**
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001919
Christopher Faulet5a2c6612021-08-15 20:35:25 +02001920 This function returns the length of the input part of the buffer. When called
1921 by a filter, this value is relative to the filter.
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001922
1923 :param class_channel channel: The manipulated Channel.
1924 :returns: an integer containing the amount of available bytes.
1925
1926 .. warning::
1927 This function is deprecated. :js:func:`Channel.input()` must be used
1928 instead.
1929
1930.. js:function:: Channel.get_out_len(channel)
1931
Boyang Li60cfe8b2022-05-10 18:11:00 +00001932 **DEPRECATED**
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001933
Christopher Faulet5a2c6612021-08-15 20:35:25 +02001934 This function returns the length of the output part of the buffer. When called
1935 by a filter, this value is relative to the filter.
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001936
1937 :param class_channel channel: The manipulated Channel.
1938 :returns: an integer containing the amount of available bytes.
1939
1940 .. warning::
1941 This function is deprecated. :js:func:`Channel.output()` must be used
1942 instead.
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001943
1944.. _http_class:
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001945
1946HTTP class
1947==========
1948
1949.. js:class:: HTTP
1950
1951 This class contain all the HTTP manipulation functions.
1952
Pieter Baauw386a1272015-08-16 15:26:24 +02001953.. js:function:: HTTP.req_get_headers(http)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001954
Patrick Hemmerc6a1d712018-05-01 21:30:41 -04001955 Returns a table containing all the request headers.
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001956
1957 :param class_http http: The related http object.
Patrick Hemmerc6a1d712018-05-01 21:30:41 -04001958 :returns: table of headers.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01001959 :see: :js:func:`HTTP.res_get_headers`
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001960
Patrick Hemmerc6a1d712018-05-01 21:30:41 -04001961 This is the form of the returned table:
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001962
1963.. code-block:: lua
1964
1965 HTTP:req_get_headers()['<header-name>'][<header-index>] = "<header-value>"
1966
1967 local hdr = HTTP:req_get_headers()
1968 hdr["host"][0] = "www.test.com"
1969 hdr["accept"][0] = "audio/basic q=1"
1970 hdr["accept"][1] = "audio/*, q=0.2"
1971 hdr["accept"][2] = "*/*, q=0.1"
1972..
1973
Pieter Baauw386a1272015-08-16 15:26:24 +02001974.. js:function:: HTTP.res_get_headers(http)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001975
Patrick Hemmerc6a1d712018-05-01 21:30:41 -04001976 Returns a table containing all the response headers.
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001977
1978 :param class_http http: The related http object.
Patrick Hemmerc6a1d712018-05-01 21:30:41 -04001979 :returns: table of headers.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01001980 :see: :js:func:`HTTP.req_get_headers`
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001981
Patrick Hemmerc6a1d712018-05-01 21:30:41 -04001982 This is the form of the returned table:
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001983
1984.. code-block:: lua
1985
1986 HTTP:res_get_headers()['<header-name>'][<header-index>] = "<header-value>"
1987
1988 local hdr = HTTP:req_get_headers()
1989 hdr["host"][0] = "www.test.com"
1990 hdr["accept"][0] = "audio/basic q=1"
1991 hdr["accept"][1] = "audio/*, q=0.2"
1992 hdr["accept"][2] = "*.*, q=0.1"
1993..
1994
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001995.. js:function:: HTTP.req_add_header(http, name, value)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001996
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02001997 Appends a HTTP header field in the request whose name is
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001998 specified in "name" and whose value is defined in "value".
1999
2000 :param class_http http: The related http object.
2001 :param string name: The header name.
2002 :param string value: The header value.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01002003 :see: :js:func:`HTTP.res_add_header`
Thierry FOURNIER08504f42015-03-16 14:17:08 +01002004
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002005.. js:function:: HTTP.res_add_header(http, name, value)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01002006
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02002007 Appends a HTTP header field in the response whose name is
Thierry FOURNIER08504f42015-03-16 14:17:08 +01002008 specified in "name" and whose value is defined in "value".
2009
2010 :param class_http http: The related http object.
2011 :param string name: The header name.
2012 :param string value: The header value.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01002013 :see: :js:func:`HTTP.req_add_header`
Thierry FOURNIER08504f42015-03-16 14:17:08 +01002014
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002015.. js:function:: HTTP.req_del_header(http, name)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01002016
2017 Removes all HTTP header fields in the request whose name is
2018 specified in "name".
2019
2020 :param class_http http: The related http object.
2021 :param string name: The header name.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01002022 :see: :js:func:`HTTP.res_del_header`
Thierry FOURNIER08504f42015-03-16 14:17:08 +01002023
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002024.. js:function:: HTTP.res_del_header(http, name)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01002025
2026 Removes all HTTP header fields in the response whose name is
2027 specified in "name".
2028
2029 :param class_http http: The related http object.
2030 :param string name: The header name.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01002031 :see: :js:func:`HTTP.req_del_header`
Thierry FOURNIER08504f42015-03-16 14:17:08 +01002032
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002033.. js:function:: HTTP.req_set_header(http, name, value)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01002034
Bertrand Jacquin874a35c2018-09-10 21:26:07 +01002035 This variable replace all occurrence of all header "name", by only
Thierry FOURNIER08504f42015-03-16 14:17:08 +01002036 one containing the "value".
2037
2038 :param class_http http: The related http object.
2039 :param string name: The header name.
2040 :param string value: The header value.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01002041 :see: :js:func:`HTTP.res_set_header`
Thierry FOURNIER08504f42015-03-16 14:17:08 +01002042
Bertrand Jacquin874a35c2018-09-10 21:26:07 +01002043 This function does the same work as the following code:
Thierry FOURNIER08504f42015-03-16 14:17:08 +01002044
2045.. code-block:: lua
2046
2047 function fcn(txn)
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002048 TXN.http:req_del_header("header")
2049 TXN.http:req_add_header("header", "value")
Thierry FOURNIER08504f42015-03-16 14:17:08 +01002050 end
2051..
2052
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002053.. js:function:: HTTP.res_set_header(http, name, value)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01002054
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02002055 This function replaces all occurrence of all header "name", by only
Thierry FOURNIER08504f42015-03-16 14:17:08 +01002056 one containing the "value".
2057
2058 :param class_http http: The related http object.
2059 :param string name: The header name.
2060 :param string value: The header value.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01002061 :see: :js:func:`HTTP.req_rep_header()`
Thierry FOURNIER08504f42015-03-16 14:17:08 +01002062
Pieter Baauw386a1272015-08-16 15:26:24 +02002063.. js:function:: HTTP.req_rep_header(http, name, regex, replace)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01002064
2065 Matches the regular expression in all occurrences of header field "name"
2066 according to "regex", and replaces them with the "replace" argument. The
2067 replacement value can contain back references like \1, \2, ... This
2068 function works with the request.
2069
2070 :param class_http http: The related http object.
2071 :param string name: The header name.
2072 :param string regex: The match regular expression.
2073 :param string replace: The replacement value.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01002074 :see: :js:func:`HTTP.res_rep_header()`
Thierry FOURNIER08504f42015-03-16 14:17:08 +01002075
Pieter Baauw386a1272015-08-16 15:26:24 +02002076.. js:function:: HTTP.res_rep_header(http, name, regex, string)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01002077
2078 Matches the regular expression in all occurrences of header field "name"
2079 according to "regex", and replaces them with the "replace" argument. The
2080 replacement value can contain back references like \1, \2, ... This
2081 function works with the request.
2082
2083 :param class_http http: The related http object.
2084 :param string name: The header name.
2085 :param string regex: The match regular expression.
2086 :param string replace: The replacement value.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01002087 :see: :js:func:`HTTP.req_rep_header()`
Thierry FOURNIER08504f42015-03-16 14:17:08 +01002088
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002089.. js:function:: HTTP.req_set_method(http, method)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01002090
2091 Rewrites the request method with the parameter "method".
2092
2093 :param class_http http: The related http object.
2094 :param string method: The new method.
2095
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002096.. js:function:: HTTP.req_set_path(http, path)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01002097
2098 Rewrites the request path with the "path" parameter.
2099
2100 :param class_http http: The related http object.
2101 :param string path: The new path.
2102
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002103.. js:function:: HTTP.req_set_query(http, query)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01002104
2105 Rewrites the request's query string which appears after the first question
2106 mark ("?") with the parameter "query".
2107
2108 :param class_http http: The related http object.
2109 :param string query: The new query.
2110
Thierry FOURNIER0d79cf62015-08-26 14:20:58 +02002111.. js:function:: HTTP.req_set_uri(http, uri)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01002112
2113 Rewrites the request URI with the parameter "uri".
2114
2115 :param class_http http: The related http object.
2116 :param string uri: The new uri.
2117
Robin H. Johnson52f5db22017-01-01 13:10:52 -08002118.. js:function:: HTTP.res_set_status(http, status [, reason])
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +02002119
Robin H. Johnson52f5db22017-01-01 13:10:52 -08002120 Rewrites the response status code with the parameter "code".
2121
2122 If no custom reason is provided, it will be generated from the status.
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +02002123
2124 :param class_http http: The related http object.
2125 :param integer status: The new response status code.
Robin H. Johnson52f5db22017-01-01 13:10:52 -08002126 :param string reason: The new response reason (optional).
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +02002127
William Lallemand00a15022021-11-19 16:02:44 +01002128.. _httpclient_class:
2129
2130HTTPClient class
2131================
2132
2133.. js:class:: HTTPClient
2134
2135 The httpclient class allows issue of outbound HTTP requests through a simple
2136 API without the knowledge of HAProxy internals.
2137
2138.. js:function:: HTTPClient.get(httpclient, request)
2139.. js:function:: HTTPClient.head(httpclient, request)
2140.. js:function:: HTTPClient.put(httpclient, request)
2141.. js:function:: HTTPClient.post(httpclient, request)
2142.. js:function:: HTTPClient.delete(httpclient, request)
2143
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02002144 Send a HTTP request and wait for a response. GET, HEAD PUT, POST and DELETE
2145 methods can be used.
2146 The HTTPClient will send asynchronously the data and is able to send and
2147 receive more than HAProxy bufsize.
William Lallemand00a15022021-11-19 16:02:44 +01002148
William Lallemanda9256192022-10-21 11:48:24 +02002149 The HTTPClient interface is not able to decompress responses, it is not
2150 recommended to send an Accept-Encoding in the request so the response is
2151 received uncompressed.
William Lallemand00a15022021-11-19 16:02:44 +01002152
2153 :param class httpclient: Is the manipulated HTTPClient.
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02002154 :param table request: Is a table containing the parameters of the request
2155 that will be send.
2156 :param string request.url: Is a mandatory parameter for the request that
2157 contains the URL.
2158 :param string request.body: Is an optional parameter for the request that
2159 contains the body to send.
2160 :param table request.headers: Is an optional parameter for the request that
2161 contains the headers to send.
2162 :param string request.dst: Is an optional parameter for the destination in
2163 haproxy address format.
2164 :param integer request.timeout: Optional timeout parameter, set a
2165 "timeout server" on the connections.
William Lallemand00a15022021-11-19 16:02:44 +01002166 :returns: Lua table containing the response
2167
2168
2169.. code-block:: lua
2170
2171 local httpclient = core.httpclient()
William Lallemand4f4f2b72022-02-17 20:00:23 +01002172 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 +01002173
2174..
2175
2176.. code-block:: lua
2177
2178 response = {
2179 status = 400,
2180 reason = "Bad request",
2181 headers = {
2182 ["content-type"] = { "text/html" },
2183 ["cache-control"] = { "no-cache", "no-store" },
2184 },
William Lallemand4f4f2b72022-02-17 20:00:23 +01002185 body = "<html><body><h1>invalid request<h1></body></html>",
William Lallemand00a15022021-11-19 16:02:44 +01002186 }
2187..
2188
2189
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002190.. _txn_class:
2191
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002192TXN class
2193=========
2194
2195.. js:class:: TXN
2196
2197 The txn class contain all the functions relative to the http or tcp
2198 transaction (Note than a tcp stream is the same than a tcp transaction, but
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02002199 a HTTP transaction is not the same than a tcp stream).
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002200
2201 The usage of this class permits to retrieve data from the requests, alter it
2202 and forward it.
2203
2204 All the functions provided by this class are available in the context
Christopher Faulet5a2c6612021-08-15 20:35:25 +02002205 **sample-fetches**, **actions** and **filters**.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002206
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002207.. js:attribute:: TXN.c
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002208
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002209 :returns: An :ref:`converters_class`.
2210
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002211 This attribute contains a Converters class object.
2212
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002213.. js:attribute:: TXN.sc
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002214
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002215 :returns: An :ref:`converters_class`.
2216
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002217 This attribute contains a Converters class object. The functions of
2218 this object returns always a string.
2219
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002220.. js:attribute:: TXN.f
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002221
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002222 :returns: An :ref:`fetches_class`.
2223
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002224 This attribute contains a Fetches class object.
2225
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002226.. js:attribute:: TXN.sf
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002227
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002228 :returns: An :ref:`fetches_class`.
2229
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002230 This attribute contains a Fetches class object. The functions of
2231 this object returns always a string.
2232
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002233.. js:attribute:: TXN.req
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002234
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002235 :returns: An :ref:`channel_class`.
2236
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002237 This attribute contains a channel class object for the request buffer.
2238
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002239.. js:attribute:: TXN.res
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002240
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002241 :returns: An :ref:`channel_class`.
2242
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002243 This attribute contains a channel class object for the response buffer.
2244
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002245.. js:attribute:: TXN.http
Thierry FOURNIER08504f42015-03-16 14:17:08 +01002246
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002247 :returns: An :ref:`http_class`.
2248
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02002249 This attribute contains a HTTP class object. It is available only if the
Thierry FOURNIER08504f42015-03-16 14:17:08 +01002250 proxy has the "mode http" enabled.
2251
Christopher Faulet5a2c6612021-08-15 20:35:25 +02002252.. js:attribute:: TXN.http_req
2253
2254 :returns: An :ref:`httpmessage_class`.
2255
2256 This attribute contains the request HTTPMessage class object. It is available
2257 only if the proxy has the "mode http" enabled and only in the **filters**
2258 context.
2259
2260.. js:attribute:: TXN.http_res
2261
2262 :returns: An :ref:`httpmessage_class`.
2263
2264 This attribute contains the response HTTPMessage class object. It is available
2265 only if the proxy has the "mode http" enabled and only in the **filters**
2266 context.
2267
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01002268.. js:function:: TXN.log(TXN, loglevel, msg)
2269
2270 This function sends a log. The log is sent, according with the HAProxy
2271 configuration file, on the default syslog server if it is configured and on
2272 the stderr if it is allowed.
2273
2274 :param class_txn txn: The class txn object containing the data.
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02002275 :param integer loglevel: Is the log level associated with the message. It is
2276 a number between 0 and 7.
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01002277 :param string msg: The log content.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01002278 :see: :js:attr:`core.emerg`, :js:attr:`core.alert`, :js:attr:`core.crit`,
2279 :js:attr:`core.err`, :js:attr:`core.warning`, :js:attr:`core.notice`,
2280 :js:attr:`core.info`, :js:attr:`core.debug` (log level definitions)
2281 :see: :js:func:`TXN.deflog`
2282 :see: :js:func:`TXN.Debug`
2283 :see: :js:func:`TXN.Info`
2284 :see: :js:func:`TXN.Warning`
2285 :see: :js:func:`TXN.Alert`
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01002286
2287.. js:function:: TXN.deflog(TXN, msg)
2288
Bertrand Jacquin874a35c2018-09-10 21:26:07 +01002289 Sends a log line with the default loglevel for the proxy associated with the
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01002290 transaction.
2291
2292 :param class_txn txn: The class txn object containing the data.
2293 :param string msg: The log content.
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02002294 :see: :js:func:`TXN.log`
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01002295
2296.. js:function:: TXN.Debug(txn, msg)
2297
2298 :param class_txn txn: The class txn object containing the data.
2299 :param string msg: The log content.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01002300 :see: :js:func:`TXN.log`
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01002301
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02002302 Does the same job as:
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01002303
2304.. code-block:: lua
2305
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002306 function Debug(txn, msg)
2307 TXN.log(txn, core.debug, msg)
2308 end
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01002309..
2310
2311.. js:function:: TXN.Info(txn, msg)
2312
2313 :param class_txn txn: The class txn object containing the data.
2314 :param string msg: The log content.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01002315 :see: :js:func:`TXN.log`
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01002316
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02002317 Does the same job as:
2318
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01002319.. code-block:: lua
2320
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02002321 function Info(txn, msg)
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002322 TXN.log(txn, core.info, msg)
2323 end
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01002324..
2325
2326.. js:function:: TXN.Warning(txn, msg)
2327
2328 :param class_txn txn: The class txn object containing the data.
2329 :param string msg: The log content.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01002330 :see: :js:func:`TXN.log`
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01002331
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02002332 Does the same job as:
2333
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01002334.. code-block:: lua
2335
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02002336 function Warning(txn, msg)
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002337 TXN.log(txn, core.warning, msg)
2338 end
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01002339..
2340
2341.. js:function:: TXN.Alert(txn, msg)
2342
2343 :param class_txn txn: The class txn object containing the data.
2344 :param string msg: The log content.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01002345 :see: :js:func:`TXN.log`
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01002346
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02002347 Does the same job as:
2348
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01002349.. code-block:: lua
2350
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02002351 function Alert(txn, msg)
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002352 TXN.log(txn, core.alert, msg)
2353 end
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01002354..
2355
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002356.. js:function:: TXN.get_priv(txn)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002357
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002358 Return Lua data stored in the current transaction (with the `TXN.set_priv()`)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002359 function. If no data are stored, it returns a nil value.
2360
2361 :param class_txn txn: The class txn object containing the data.
Bertrand Jacquin874a35c2018-09-10 21:26:07 +01002362 :returns: the opaque data previously stored, or nil if nothing is
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02002363 available.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002364
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002365.. js:function:: TXN.set_priv(txn, data)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002366
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02002367 Store any data in the current HAProxy transaction. This action replaces the
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002368 old stored data.
2369
2370 :param class_txn txn: The class txn object containing the data.
2371 :param opaque data: The data which is stored in the transaction.
2372
Tim Duesterhus4e172c92020-05-19 13:49:42 +02002373.. js:function:: TXN.set_var(TXN, var, value[, ifexist])
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +02002374
David Carlier61fdf8b2015-10-02 11:59:38 +01002375 Converts a Lua type in a HAProxy type and store it in a variable <var>.
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +02002376
2377 :param class_txn txn: The class txn object containing the data.
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02002378 :param string var: The variable name according with the HAProxy variable
2379 syntax.
2380 :param type value: The value associated to the variable. The type can be
2381 string or integer.
2382 :param boolean ifexist: If this parameter is set to true the variable will
2383 only be set if it was defined elsewhere (i.e. used within the configuration).
2384 For global variables (using the "proc" scope), they will only be updated and
2385 never created. It is highly recommended to always set this to true.
Christopher Faulet85d79c92016-11-09 16:54:56 +01002386
2387.. js:function:: TXN.unset_var(TXN, var)
2388
2389 Unset the variable <var>.
2390
2391 :param class_txn txn: The class txn object containing the data.
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02002392 :param string var: The variable name according with the HAProxy variable
2393 syntax.
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +02002394
2395.. js:function:: TXN.get_var(TXN, var)
2396
2397 Returns data stored in the variable <var> converter in Lua type.
2398
2399 :param class_txn txn: The class txn object containing the data.
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02002400 :param string var: The variable name according with the HAProxy variable
2401 syntax.
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +02002402
Christopher Faulet700d9e82020-01-31 12:21:52 +01002403.. js:function:: TXN.reply([reply])
2404
2405 Return a new reply object
2406
2407 :param table reply: A table containing info to initialize the reply fields.
2408 :returns: A :ref:`reply_class` object.
2409
2410 The table used to initialized the reply object may contain following entries :
2411
2412 * status : The reply status code. the code 200 is used by default.
2413 * reason : The reply reason. The reason corresponding to the status code is
2414 used by default.
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02002415 * headers : A list of headers, indexed by header name. Empty by default. For
Christopher Faulet700d9e82020-01-31 12:21:52 +01002416 a given name, multiple values are possible, stored in an ordered list.
2417 * body : The reply body, empty by default.
2418
2419.. code-block:: lua
2420
2421 local reply = txn:reply{
2422 status = 400,
2423 reason = "Bad request",
2424 headers = {
2425 ["content-type"] = { "text/html" },
2426 ["cache-control"] = {"no-cache", "no-store" }
2427 },
2428 body = "<html><body><h1>invalid request<h1></body></html>"
2429 }
2430..
2431 :see: :js:class:`Reply`
2432
2433.. js:function:: TXN.done(txn[, reply])
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002434
Willy Tarreaubc183a62015-08-28 10:39:11 +02002435 This function terminates processing of the transaction and the associated
Christopher Faulet700d9e82020-01-31 12:21:52 +01002436 session and optionally reply to the client for HTTP sessions.
2437
2438 :param class_txn txn: The class txn object containing the data.
2439 :param class_reply reply: The class reply object to return to the client.
2440
2441 This functions can be used when a critical error is detected or to terminate
Willy Tarreaubc183a62015-08-28 10:39:11 +02002442 processing after some data have been returned to the client (eg: a redirect).
Christopher Faulet700d9e82020-01-31 12:21:52 +01002443 To do so, a reply may be provided. This object is optional and may contain a
2444 status code, a reason, a header list and a body. All these fields are
Christopher Faulet7855b192021-11-09 18:39:51 +01002445 optional. When not provided, the default values are used. By default, with an
2446 empty reply object, an empty HTTP 200 response is returned to the client. If
2447 no reply object is provided, the transaction is terminated without any
2448 reply. If a reply object is provided, it must not exceed the buffer size once
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02002449 converted into the internal HTTP representation. Because for now there is no
Christopher Faulet7855b192021-11-09 18:39:51 +01002450 easy way to be sure it fits, it is probably better to keep it reasonably
2451 small.
Christopher Faulet700d9e82020-01-31 12:21:52 +01002452
2453 The reply object may be fully created in lua or the class Reply may be used to
2454 create it.
2455
2456.. code-block:: lua
2457
2458 local reply = txn:reply()
2459 reply:set_status(400, "Bad request")
2460 reply:add_header("content-type", "text/html")
2461 reply:add_header("cache-control", "no-cache")
2462 reply:add_header("cache-control", "no-store")
2463 reply:set_body("<html><body><h1>invalid request<h1></body></html>")
2464 txn:done(reply)
2465..
2466
2467.. code-block:: lua
2468
2469 txn:done{
2470 status = 400,
2471 reason = "Bad request",
2472 headers = {
2473 ["content-type"] = { "text/html" },
2474 ["cache-control"] = { "no-cache", "no-store" },
2475 },
2476 body = "<html><body><h1>invalid request<h1></body></html>"
2477 }
2478..
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002479
Christopher Faulet1e9b1b62021-08-11 10:14:30 +02002480 .. warning::
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02002481 It does not make sense to call this function from sample-fetches. In this
2482 case the behavior is the same than core.done(): it finishes the Lua
Christopher Faulet1e9b1b62021-08-11 10:14:30 +02002483 execution. The transaction is really aborted only from an action registered
2484 function.
Thierry FOURNIERab00df62016-07-14 11:42:37 +02002485
Christopher Faulet700d9e82020-01-31 12:21:52 +01002486 :see: :js:func:`TXN.reply`, :js:class:`Reply`
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002487
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002488.. js:function:: TXN.set_loglevel(txn, loglevel)
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01002489
2490 Is used to change the log level of the current request. The "loglevel" must
2491 be an integer between 0 and 7.
2492
2493 :param class_txn txn: The class txn object containing the data.
2494 :param integer loglevel: The required log level. This variable can be one of
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01002495 :see: :js:attr:`core.emerg`, :js:attr:`core.alert`, :js:attr:`core.crit`,
2496 :js:attr:`core.err`, :js:attr:`core.warning`, :js:attr:`core.notice`,
2497 :js:attr:`core.info`, :js:attr:`core.debug` (log level definitions)
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01002498
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002499.. js:function:: TXN.set_tos(txn, tos)
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01002500
2501 Is used to set the TOS or DSCP field value of packets sent to the client to
2502 the value passed in "tos" on platforms which support this.
2503
2504 :param class_txn txn: The class txn object containing the data.
2505 :param integer tos: The new TOS os DSCP.
2506
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002507.. js:function:: TXN.set_mark(txn, mark)
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01002508
2509 Is used to set the Netfilter MARK on all packets sent to the client to the
2510 value passed in "mark" on platforms which support it.
2511
2512 :param class_txn txn: The class txn object containing the data.
2513 :param integer mark: The mark value.
2514
Patrick Hemmer268a7072018-05-11 12:52:31 -04002515.. js:function:: TXN.set_priority_class(txn, prio)
2516
2517 This function adjusts the priority class of the transaction. The value should
2518 be within the range -2047..2047. Values outside this range will be
2519 truncated.
2520
2521 See the HAProxy configuration.txt file keyword "http-request" action
2522 "set-priority-class" for details.
2523
2524.. js:function:: TXN.set_priority_offset(txn, prio)
2525
2526 This function adjusts the priority offset of the transaction. The value
2527 should be within the range -524287..524287. Values outside this range will be
2528 truncated.
2529
2530 See the HAProxy configuration.txt file keyword "http-request" action
2531 "set-priority-offset" for details.
2532
Christopher Faulet700d9e82020-01-31 12:21:52 +01002533.. _reply_class:
2534
2535Reply class
2536============
2537
2538.. js:class:: Reply
2539
2540 **context**: action
2541
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02002542 This class represents a HTTP response message. It provides some methods to
Christopher Faulet7855b192021-11-09 18:39:51 +01002543 enrich it. Once converted into the internal HTTP representation, the response
2544 message must not exceed the buffer size. Because for now there is no
2545 easy way to be sure it fits, it is probably better to keep it reasonably
2546 small.
2547
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02002548 See tune.bufsize in the configuration manual for details.
Christopher Faulet700d9e82020-01-31 12:21:52 +01002549
2550.. code-block:: lua
2551
2552 local reply = txn:reply({status = 400}) -- default HTTP 400 reason-phase used
2553 reply:add_header("content-type", "text/html")
2554 reply:add_header("cache-control", "no-cache")
2555 reply:add_header("cache-control", "no-store")
2556 reply:set_body("<html><body><h1>invalid request<h1></body></html>")
2557..
2558
2559 :see: :js:func:`TXN.reply`
2560
2561.. js:attribute:: Reply.status
2562
2563 The reply status code. By default, the status code is set to 200.
2564
2565 :returns: integer
2566
2567.. js:attribute:: Reply.reason
2568
2569 The reason string describing the status code.
2570
2571 :returns: string
2572
2573.. js:attribute:: Reply.headers
2574
2575 A table indexing all reply headers by name. To each name is associated an
2576 ordered list of values.
2577
2578 :returns: Lua table
2579
2580.. code-block:: lua
2581
2582 {
2583 ["content-type"] = { "text/html" },
2584 ["cache-control"] = {"no-cache", "no-store" },
2585 x_header_name = { "value1", "value2", ... }
2586 ...
2587 }
2588..
2589
2590.. js:attribute:: Reply.body
2591
2592 The reply payload.
2593
2594 :returns: string
2595
2596.. js:function:: Reply.set_status(REPLY, status[, reason])
2597
2598 Set the reply status code and optionally the reason-phrase. If the reason is
2599 not provided, the default reason corresponding to the status code is used.
2600
2601 :param class_reply reply: The related Reply object.
2602 :param integer status: The reply status code.
2603 :param string reason: The reply status reason (optional).
2604
2605.. js:function:: Reply.add_header(REPLY, name, value)
2606
2607 Add a header to the reply object. If the header does not already exist, a new
2608 entry is created with its name as index and a one-element list containing its
2609 value as value. Otherwise, the header value is appended to the ordered list of
2610 values associated to the header name.
2611
2612 :param class_reply reply: The related Reply object.
2613 :param string name: The header field name.
2614 :param string value: The header field value.
2615
2616.. js:function:: Reply.del_header(REPLY, name)
2617
2618 Remove all occurrences of a header name from the reply object.
2619
2620 :param class_reply reply: The related Reply object.
2621 :param string name: The header field name.
2622
2623.. js:function:: Reply.set_body(REPLY, body)
2624
2625 Set the reply payload.
2626
2627 :param class_reply reply: The related Reply object.
2628 :param string body: The reply payload.
2629
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002630.. _socket_class:
2631
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002632Socket class
2633============
2634
2635.. js:class:: Socket
2636
2637 This class must be compatible with the Lua Socket class. Only the 'client'
2638 functions are available. See the Lua Socket documentation:
2639
2640 `http://w3.impa.br/~diego/software/luasocket/tcp.html
2641 <http://w3.impa.br/~diego/software/luasocket/tcp.html>`_
2642
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002643.. js:function:: Socket.close(socket)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002644
2645 Closes a TCP object. The internal socket used by the object is closed and the
2646 local address to which the object was bound is made available to other
2647 applications. No further operations (except for further calls to the close
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002648 method) are allowed on a closed Socket.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002649
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002650 :param class_socket socket: Is the manipulated Socket.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002651
2652 Note: It is important to close all used sockets once they are not needed,
2653 since, in many systems, each socket uses a file descriptor, which are limited
2654 system resources. Garbage-collected objects are automatically closed before
2655 destruction, though.
2656
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002657.. js:function:: Socket.connect(socket, address[, port])
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002658
2659 Attempts to connect a socket object to a remote host.
2660
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002661
2662 In case of error, the method returns nil followed by a string describing the
2663 error. In case of success, the method returns 1.
2664
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002665 :param class_socket socket: Is the manipulated Socket.
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002666 :param string address: can be an IP address or a host name. See below for more
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02002667 information.
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002668 :param integer port: must be an integer number in the range [1..64K].
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002669 :returns: 1 or nil.
2670
Bertrand Jacquin874a35c2018-09-10 21:26:07 +01002671 An address field extension permits to use the connect() function to connect to
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002672 other stream than TCP. The syntax containing a simpleipv4 or ipv6 address is
2673 the basically expected format. This format requires the port.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002674
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002675 Other format accepted are a socket path like "/socket/path", it permits to
Bertrand Jacquin874a35c2018-09-10 21:26:07 +01002676 connect to a socket. Abstract namespaces are supported with the prefix
Joseph Herlant02cedc42018-11-13 19:45:17 -08002677 "abns@", and finally a file descriptor can be passed with the prefix "fd@".
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002678 The prefix "ipv4@", "ipv6@" and "unix@" are also supported. The port can be
Bertrand Jacquin874a35c2018-09-10 21:26:07 +01002679 passed int the string. The syntax "127.0.0.1:1234" is valid. In this case, the
Tim Duesterhus6edab862018-01-06 19:04:45 +01002680 parameter *port* must not be set.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002681
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002682.. js:function:: Socket.connect_ssl(socket, address, port)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002683
2684 Same behavior than the function socket:connect, but uses SSL.
2685
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002686 :param class_socket socket: Is the manipulated Socket.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002687 :returns: 1 or nil.
2688
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002689.. js:function:: Socket.getpeername(socket)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002690
2691 Returns information about the remote side of a connected client object.
2692
2693 Returns a string with the IP address of the peer, followed by the port number
2694 that peer is using for the connection. In case of error, the method returns
2695 nil.
2696
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002697 :param class_socket socket: Is the manipulated Socket.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002698 :returns: a string containing the server information.
2699
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002700.. js:function:: Socket.getsockname(socket)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002701
2702 Returns the local address information associated to the object.
2703
2704 The method returns a string with local IP address and a number with the port.
2705 In case of error, the method returns nil.
2706
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002707 :param class_socket socket: Is the manipulated Socket.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002708 :returns: a string containing the client information.
2709
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002710.. js:function:: Socket.receive(socket, [pattern [, prefix]])
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002711
2712 Reads data from a client object, according to the specified read pattern.
2713 Patterns follow the Lua file I/O format, and the difference in performance
2714 between all patterns is negligible.
2715
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002716 :param class_socket socket: Is the manipulated Socket.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002717 :param string|integer pattern: Describe what is required (see below).
2718 :param string prefix: A string which will be prefix the returned data.
2719 :returns: a string containing the required data or nil.
2720
2721 Pattern can be any of the following:
2722
2723 * **`*a`**: reads from the socket until the connection is closed. No
2724 end-of-line translation is performed;
2725
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002726 * **`*l`**: reads a line of text from the Socket. The line is terminated by a
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002727 LF character (ASCII 10), optionally preceded by a CR character
2728 (ASCII 13). The CR and LF characters are not included in the
2729 returned line. In fact, all CR characters are ignored by the
2730 pattern. This is the default pattern.
2731
2732 * **number**: causes the method to read a specified number of bytes from the
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002733 Socket. Prefix is an optional string to be concatenated to the
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002734 beginning of any received data before return.
2735
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002736 * **empty**: If the pattern is left empty, the default option is `*l`.
2737
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002738 If successful, the method returns the received pattern. In case of error, the
2739 method returns nil followed by an error message which can be the string
2740 'closed' in case the connection was closed before the transmission was
2741 completed or the string 'timeout' in case there was a timeout during the
2742 operation. Also, after the error message, the function returns the partial
2743 result of the transmission.
2744
2745 Important note: This function was changed severely. It used to support
2746 multiple patterns (but I have never seen this feature used) and now it
2747 doesn't anymore. Partial results used to be returned in the same way as
2748 successful results. This last feature violated the idea that all functions
2749 should return nil on error. Thus it was changed too.
2750
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002751.. js:function:: Socket.send(socket, data [, start [, end ]])
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002752
2753 Sends data through client object.
2754
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002755 :param class_socket socket: Is the manipulated Socket.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002756 :param string data: The data that will be sent.
2757 :param integer start: The start position in the buffer of the data which will
2758 be sent.
2759 :param integer end: The end position in the buffer of the data which will
2760 be sent.
2761 :returns: see below.
2762
2763 Data is the string to be sent. The optional arguments i and j work exactly
2764 like the standard string.sub Lua function to allow the selection of a
2765 substring to be sent.
2766
2767 If successful, the method returns the index of the last byte within [start,
2768 end] that has been sent. Notice that, if start is 1 or absent, this is
2769 effectively the total number of bytes sent. In case of error, the method
2770 returns nil, followed by an error message, followed by the index of the last
2771 byte within [start, end] that has been sent. You might want to try again from
2772 the byte following that. The error message can be 'closed' in case the
2773 connection was closed before the transmission was completed or the string
2774 'timeout' in case there was a timeout during the operation.
2775
2776 Note: Output is not buffered. For small strings, it is always better to
2777 concatenate them in Lua (with the '..' operator) and send the result in one
2778 call instead of calling the method several times.
2779
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002780.. js:function:: Socket.setoption(socket, option [, value])
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002781
2782 Just implemented for compatibility, this cal does nothing.
2783
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002784.. js:function:: Socket.settimeout(socket, value [, mode])
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002785
2786 Changes the timeout values for the object. All I/O operations are blocking.
2787 That is, any call to the methods send, receive, and accept will block
2788 indefinitely, until the operation completes. The settimeout method defines a
2789 limit on the amount of time the I/O methods can block. When a timeout time
2790 has elapsed, the affected methods give up and fail with an error code.
2791
2792 The amount of time to wait is specified as the value parameter, in seconds.
2793
Mark Lakes56cc1252018-03-27 09:48:06 +02002794 The timeout modes are not implemented, the only settable timeout is the
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002795 inactivity time waiting for complete the internal buffer send or waiting for
2796 receive data.
2797
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002798 :param class_socket socket: Is the manipulated Socket.
Bertrand Jacquin874a35c2018-09-10 21:26:07 +01002799 :param float value: The timeout value. Use floating point to specify
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02002800 milliseconds.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002801
Thierry FOURNIER31904272017-10-25 12:59:51 +02002802.. _regex_class:
2803
2804Regex class
2805===========
2806
2807.. js:class:: Regex
2808
2809 This class allows the usage of HAProxy regexes because classic lua doesn't
2810 provides regexes. This class inherits the HAProxy compilation options, so the
2811 regexes can be libc regex, pcre regex or pcre JIT regex.
2812
2813 The expression matching number is limited to 20 per regex. The only available
2814 option is case sensitive.
2815
2816 Because regexes compilation is a heavy process, it is better to define all
2817 your regexes in the **body context** and use it during the runtime.
2818
2819.. code-block:: lua
2820
2821 -- Create the regex
2822 st, regex = Regex.new("needle (..) (...)", true);
2823
2824 -- Check compilation errors
2825 if st == false then
2826 print "error: " .. regex
2827 end
2828
2829 -- Match the regexes
2830 print(regex:exec("Looking for a needle in the haystack")) -- true
2831 print(regex:exec("Lokking for a cat in the haystack")) -- false
2832
2833 -- Extract words
2834 st, list = regex:match("Looking for a needle in the haystack")
2835 print(st) -- true
2836 print(list[1]) -- needle in the
2837 print(list[2]) -- in
2838 print(list[3]) -- the
2839
2840.. js:function:: Regex.new(regex, case_sensitive)
2841
2842 Create and compile a regex.
2843
2844 :param string regex: The regular expression according with the libc or pcre
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02002845 standard
Thierry FOURNIER31904272017-10-25 12:59:51 +02002846 :param boolean case_sensitive: Match is case sensitive or not.
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02002847 :returns: boolean status and :ref:`regex_class` or string containing fail
2848 reason.
Thierry FOURNIER31904272017-10-25 12:59:51 +02002849
2850.. js:function:: Regex.exec(regex, str)
2851
2852 Execute the regex.
2853
2854 :param class_regex regex: A :ref:`regex_class` object.
2855 :param string str: The input string will be compared with the compiled regex.
2856 :returns: a boolean status according with the match result.
2857
2858.. js:function:: Regex.match(regex, str)
2859
2860 Execute the regex and return matched expressions.
2861
2862 :param class_map map: A :ref:`regex_class` object.
2863 :param string str: The input string will be compared with the compiled regex.
2864 :returns: a boolean status according with the match result, and
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02002865 a table containing all the string matched in order of declaration.
Thierry FOURNIER31904272017-10-25 12:59:51 +02002866
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002867.. _map_class:
2868
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002869Map class
2870=========
2871
2872.. js:class:: Map
2873
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02002874 This class permits to do some lookups in HAProxy maps. The declared maps can
Bertrand Jacquin874a35c2018-09-10 21:26:07 +01002875 be modified during the runtime through the HAProxy management socket.
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002876
2877.. code-block:: lua
2878
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002879 default = "usa"
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002880
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002881 -- Create and load map
Thierry FOURNIER4dc71972017-01-28 08:33:08 +01002882 geo = Map.new("geo.map", Map._ip);
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002883
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002884 -- Create new fetch that returns the user country
2885 core.register_fetches("country", function(txn)
2886 local src;
2887 local loc;
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002888
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002889 src = txn.f:fhdr("x-forwarded-for");
2890 if (src == nil) then
2891 src = txn.f:src()
2892 if (src == nil) then
2893 return default;
2894 end
2895 end
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002896
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002897 -- Perform lookup
2898 loc = geo:lookup(src);
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002899
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002900 if (loc == nil) then
2901 return default;
2902 end
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002903
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002904 return loc;
2905 end);
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002906
Thierry FOURNIER4dc71972017-01-28 08:33:08 +01002907.. js:attribute:: Map._int
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002908
2909 See the HAProxy configuration.txt file, chapter "Using ACLs and fetching
Ilya Shipitsin2075ca82020-03-06 23:22:22 +05002910 samples" and subchapter "ACL basics" to understand this pattern matching
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002911 method.
2912
Thierry FOURNIER4dc71972017-01-28 08:33:08 +01002913 Note that :js:attr:`Map.int` is also available for compatibility.
2914
2915.. js:attribute:: Map._ip
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002916
2917 See the HAProxy configuration.txt file, chapter "Using ACLs and fetching
Ilya Shipitsin2075ca82020-03-06 23:22:22 +05002918 samples" and subchapter "ACL basics" to understand this pattern matching
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002919 method.
2920
Thierry FOURNIER4dc71972017-01-28 08:33:08 +01002921 Note that :js:attr:`Map.ip` is also available for compatibility.
2922
2923.. js:attribute:: Map._str
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002924
2925 See the HAProxy configuration.txt file, chapter "Using ACLs and fetching
Ilya Shipitsin2075ca82020-03-06 23:22:22 +05002926 samples" and subchapter "ACL basics" to understand this pattern matching
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002927 method.
2928
Thierry FOURNIER4dc71972017-01-28 08:33:08 +01002929 Note that :js:attr:`Map.str` is also available for compatibility.
2930
2931.. js:attribute:: Map._beg
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002932
2933 See the HAProxy configuration.txt file, chapter "Using ACLs and fetching
Ilya Shipitsin2075ca82020-03-06 23:22:22 +05002934 samples" and subchapter "ACL basics" to understand this pattern matching
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002935 method.
2936
Thierry FOURNIER4dc71972017-01-28 08:33:08 +01002937 Note that :js:attr:`Map.beg` is also available for compatibility.
2938
2939.. js:attribute:: Map._sub
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002940
2941 See the HAProxy configuration.txt file, chapter "Using ACLs and fetching
Ilya Shipitsin2075ca82020-03-06 23:22:22 +05002942 samples" and subchapter "ACL basics" to understand this pattern matching
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002943 method.
2944
Thierry FOURNIER4dc71972017-01-28 08:33:08 +01002945 Note that :js:attr:`Map.sub` is also available for compatibility.
2946
2947.. js:attribute:: Map._dir
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002948
2949 See the HAProxy configuration.txt file, chapter "Using ACLs and fetching
Ilya Shipitsin2075ca82020-03-06 23:22:22 +05002950 samples" and subchapter "ACL basics" to understand this pattern matching
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002951 method.
2952
Thierry FOURNIER4dc71972017-01-28 08:33:08 +01002953 Note that :js:attr:`Map.dir` is also available for compatibility.
2954
2955.. js:attribute:: Map._dom
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002956
2957 See the HAProxy configuration.txt file, chapter "Using ACLs and fetching
Ilya Shipitsin2075ca82020-03-06 23:22:22 +05002958 samples" and subchapter "ACL basics" to understand this pattern matching
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002959 method.
2960
Thierry FOURNIER4dc71972017-01-28 08:33:08 +01002961 Note that :js:attr:`Map.dom` is also available for compatibility.
2962
2963.. js:attribute:: Map._end
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002964
2965 See the HAProxy configuration.txt file, chapter "Using ACLs and fetching
Ilya Shipitsin2075ca82020-03-06 23:22:22 +05002966 samples" and subchapter "ACL basics" to understand this pattern matching
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002967 method.
2968
Thierry FOURNIER4dc71972017-01-28 08:33:08 +01002969.. js:attribute:: Map._reg
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002970
2971 See the HAProxy configuration.txt file, chapter "Using ACLs and fetching
Ilya Shipitsin2075ca82020-03-06 23:22:22 +05002972 samples" and subchapter "ACL basics" to understand this pattern matching
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002973 method.
2974
Thierry FOURNIER4dc71972017-01-28 08:33:08 +01002975 Note that :js:attr:`Map.reg` is also available for compatibility.
2976
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002977
2978.. js:function:: Map.new(file, method)
2979
2980 Creates and load a map.
2981
2982 :param string file: Is the file containing the map.
2983 :param integer method: Is the map pattern matching method. See the attributes
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02002984 of the Map class.
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002985 :returns: a class Map object.
Thierry FOURNIER4dc71972017-01-28 08:33:08 +01002986 :see: The Map attributes: :js:attr:`Map._int`, :js:attr:`Map._ip`,
2987 :js:attr:`Map._str`, :js:attr:`Map._beg`, :js:attr:`Map._sub`,
2988 :js:attr:`Map._dir`, :js:attr:`Map._dom`, :js:attr:`Map._end` and
2989 :js:attr:`Map._reg`.
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002990
2991.. js:function:: Map.lookup(map, str)
2992
2993 Perform a lookup in a map.
2994
2995 :param class_map map: Is the class Map object.
2996 :param string str: Is the string used as key.
2997 :returns: a string containing the result or nil if no match.
2998
2999.. js:function:: Map.slookup(map, str)
3000
3001 Perform a lookup in a map.
3002
3003 :param class_map map: Is the class Map object.
3004 :param string str: Is the string used as key.
3005 :returns: a string containing the result or empty string if no match.
3006
Thierry FOURNIERdc595002015-12-21 11:13:52 +01003007.. _applethttp_class:
3008
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02003009AppletHTTP class
Thierry FOURNIERdc595002015-12-21 11:13:52 +01003010================
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02003011
3012.. js:class:: AppletHTTP
3013
3014 This class is used with applets that requires the 'http' mode. The http applet
3015 can be registered with the *core.register_service()* function. They are used
3016 for processing an http request like a server in back of HAProxy.
3017
3018 This is an hello world sample code:
3019
3020.. code-block:: lua
Thierry FOURNIERdc595002015-12-21 11:13:52 +01003021
Pieter Baauw4d7f7662015-11-08 16:38:08 +01003022 core.register_service("hello-world", "http", function(applet)
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02003023 local response = "Hello World !"
3024 applet:set_status(200)
Pieter Baauw2dcb9bc2015-10-01 22:47:12 +02003025 applet:add_header("content-length", string.len(response))
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02003026 applet:add_header("content-type", "text/plain")
Pieter Baauw2dcb9bc2015-10-01 22:47:12 +02003027 applet:start_response()
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02003028 applet:send(response)
3029 end)
3030
Thierry FOURNIERdc595002015-12-21 11:13:52 +01003031.. js:attribute:: AppletHTTP.c
3032
3033 :returns: A :ref:`converters_class`
3034
3035 This attribute contains a Converters class object.
3036
3037.. js:attribute:: AppletHTTP.sc
3038
3039 :returns: A :ref:`converters_class`
3040
3041 This attribute contains a Converters class object. The
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02003042 functions of this object always return a string.
Thierry FOURNIERdc595002015-12-21 11:13:52 +01003043
3044.. js:attribute:: AppletHTTP.f
3045
3046 :returns: A :ref:`fetches_class`
3047
3048 This attribute contains a Fetches class object. Note that the
3049 applet execution place cannot access to a valid HAProxy core HTTP
Ilya Shipitsin2075ca82020-03-06 23:22:22 +05003050 transaction, so some sample fetches related to the HTTP dependent
Thierry FOURNIERdc595002015-12-21 11:13:52 +01003051 values (hdr, path, ...) are not available.
3052
3053.. js:attribute:: AppletHTTP.sf
3054
3055 :returns: A :ref:`fetches_class`
3056
3057 This attribute contains a Fetches class object. The functions of
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02003058 this object always return a string. Note that the applet
Thierry FOURNIERdc595002015-12-21 11:13:52 +01003059 execution place cannot access to a valid HAProxy core HTTP
Ilya Shipitsin2075ca82020-03-06 23:22:22 +05003060 transaction, so some sample fetches related to the HTTP dependent
Thierry FOURNIERdc595002015-12-21 11:13:52 +01003061 values (hdr, path, ...) are not available.
3062
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01003063.. js:attribute:: AppletHTTP.method
Thierry FOURNIERdc595002015-12-21 11:13:52 +01003064
3065 :returns: string
3066
3067 The attribute method returns a string containing the HTTP
3068 method.
3069
3070.. js:attribute:: AppletHTTP.version
3071
3072 :returns: string
3073
3074 The attribute version, returns a string containing the HTTP
3075 request version.
3076
3077.. js:attribute:: AppletHTTP.path
3078
3079 :returns: string
3080
3081 The attribute path returns a string containing the HTTP
3082 request path.
3083
3084.. js:attribute:: AppletHTTP.qs
3085
3086 :returns: string
3087
3088 The attribute qs returns a string containing the HTTP
3089 request query string.
3090
3091.. js:attribute:: AppletHTTP.length
3092
3093 :returns: integer
3094
3095 The attribute length returns an integer containing the HTTP
3096 body length.
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02003097
Thierry FOURNIER841475e2015-12-11 17:10:09 +01003098.. js:attribute:: AppletHTTP.headers
3099
Patrick Hemmerc6a1d712018-05-01 21:30:41 -04003100 :returns: table
Thierry FOURNIERdc595002015-12-21 11:13:52 +01003101
Patrick Hemmerc6a1d712018-05-01 21:30:41 -04003102 The attribute headers returns a table containing the HTTP
Thierry FOURNIERdc595002015-12-21 11:13:52 +01003103 headers. The header names are always in lower case. As the header name can be
3104 encountered more than once in each request, the value is indexed with 0 as
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02003105 first index value. The table has this form:
Thierry FOURNIERdc595002015-12-21 11:13:52 +01003106
3107.. code-block:: lua
3108
3109 AppletHTTP.headers['<header-name>'][<header-index>] = "<header-value>"
3110
3111 AppletHTTP.headers["host"][0] = "www.test.com"
3112 AppletHTTP.headers["accept"][0] = "audio/basic q=1"
3113 AppletHTTP.headers["accept"][1] = "audio/*, q=0.2"
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01003114 AppletHTTP.headers["accept"][2] = "*/*, q=0.1"
Thierry FOURNIERdc595002015-12-21 11:13:52 +01003115..
3116
Robin H. Johnson52f5db22017-01-01 13:10:52 -08003117.. js:function:: AppletHTTP.set_status(applet, code [, reason])
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02003118
3119 This function sets the HTTP status code for the response. The allowed code are
3120 from 100 to 599.
3121
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01003122 :param class_AppletHTTP applet: An :ref:`applethttp_class`
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02003123 :param integer code: the status code returned to the client.
Robin H. Johnson52f5db22017-01-01 13:10:52 -08003124 :param string reason: the status reason returned to the client (optional).
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02003125
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01003126.. js:function:: AppletHTTP.add_header(applet, name, value)
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02003127
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02003128 This function adds a header in the response. Duplicated headers are not
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02003129 collapsed. The special header *content-length* is used to determinate the
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02003130 response length. If it does not exist, a *transfer-encoding: chunked* is set,
3131 and all the write from the function *AppletHTTP:send()* become a chunk.
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02003132
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01003133 :param class_AppletHTTP applet: An :ref:`applethttp_class`
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02003134 :param string name: the header name
3135 :param string value: the header value
3136
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01003137.. js:function:: AppletHTTP.start_response(applet)
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02003138
3139 This function indicates to the HTTP engine that it can process and send the
3140 response headers. After this called we cannot add headers to the response; We
3141 cannot use the *AppletHTTP:send()* function if the
3142 *AppletHTTP:start_response()* is not called.
3143
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01003144 :param class_AppletHTTP applet: An :ref:`applethttp_class`
3145
3146.. js:function:: AppletHTTP.getline(applet)
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02003147
3148 This function returns a string containing one line from the http body. If the
3149 data returned doesn't contains a final '\\n' its assumed than its the last
3150 available data before the end of stream.
3151
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01003152 :param class_AppletHTTP applet: An :ref:`applethttp_class`
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02003153 :returns: a string. The string can be empty if we reach the end of the stream.
3154
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01003155.. js:function:: AppletHTTP.receive(applet, [size])
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02003156
3157 Reads data from the HTTP body, according to the specified read *size*. If the
3158 *size* is missing, the function tries to read all the content of the stream
3159 until the end. If the *size* is bigger than the http body, it returns the
Bertrand Jacquin874a35c2018-09-10 21:26:07 +01003160 amount of data available.
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02003161
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01003162 :param class_AppletHTTP applet: An :ref:`applethttp_class`
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02003163 :param integer size: the required read size.
Ilya Shipitsin11057a32020-06-21 21:18:27 +05003164 :returns: always return a string,the string can be empty is the connection is
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02003165 closed.
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02003166
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01003167.. js:function:: AppletHTTP.send(applet, msg)
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02003168
3169 Send the message *msg* on the http request body.
3170
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01003171 :param class_AppletHTTP applet: An :ref:`applethttp_class`
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02003172 :param string msg: the message to send.
3173
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01003174.. js:function:: AppletHTTP.get_priv(applet)
3175
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01003176 Return Lua data stored in the current transaction. If no data are stored,
3177 it returns a nil value.
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01003178
3179 :param class_AppletHTTP applet: An :ref:`applethttp_class`
Bertrand Jacquin874a35c2018-09-10 21:26:07 +01003180 :returns: the opaque data previously stored, or nil if nothing is
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02003181 available.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01003182 :see: :js:func:`AppletHTTP.set_priv`
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01003183
3184.. js:function:: AppletHTTP.set_priv(applet, data)
3185
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02003186 Store any data in the current HAProxy transaction. This action replaces the
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01003187 old stored data.
3188
3189 :param class_AppletHTTP applet: An :ref:`applethttp_class`
3190 :param opaque data: The data which is stored in the transaction.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01003191 :see: :js:func:`AppletHTTP.get_priv`
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01003192
Tim Duesterhus4e172c92020-05-19 13:49:42 +02003193.. js:function:: AppletHTTP.set_var(applet, var, value[, ifexist])
Thierry FOURNIER / OZON.IOc1edafe2016-12-12 16:25:30 +01003194
3195 Converts a Lua type in a HAProxy type and store it in a variable <var>.
3196
3197 :param class_AppletHTTP applet: An :ref:`applethttp_class`
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02003198 :param string var: The variable name according with the HAProxy variable
3199 syntax.
3200 :param type value: The value associated to the variable. The type ca be string
3201 or integer.
3202 :param boolean ifexist: If this parameter is set to true the variable will
3203 only be set if it was defined elsewhere (i.e. used within the configuration).
3204 For global variables (using the "proc" scope), they will only be updated and
3205 never created. It is highly recommended to always set this to true.
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02003206
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01003207 :see: :js:func:`AppletHTTP.unset_var`
3208 :see: :js:func:`AppletHTTP.get_var`
Thierry FOURNIER / OZON.IOc1edafe2016-12-12 16:25:30 +01003209
3210.. js:function:: AppletHTTP.unset_var(applet, var)
3211
3212 Unset the variable <var>.
3213
3214 :param class_AppletHTTP applet: An :ref:`applethttp_class`
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02003215 :param string var: The variable name according with the HAProxy variable
3216 syntax.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01003217 :see: :js:func:`AppletHTTP.set_var`
3218 :see: :js:func:`AppletHTTP.get_var`
Thierry FOURNIER / OZON.IOc1edafe2016-12-12 16:25:30 +01003219
3220.. js:function:: AppletHTTP.get_var(applet, var)
3221
3222 Returns data stored in the variable <var> converter in Lua type.
3223
3224 :param class_AppletHTTP applet: An :ref:`applethttp_class`
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02003225 :param string var: The variable name according with the HAProxy variable
3226 syntax.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01003227 :see: :js:func:`AppletHTTP.set_var`
3228 :see: :js:func:`AppletHTTP.unset_var`
Thierry FOURNIER / OZON.IOc1edafe2016-12-12 16:25:30 +01003229
Thierry FOURNIERdc595002015-12-21 11:13:52 +01003230.. _applettcp_class:
3231
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02003232AppletTCP class
3233===============
3234
3235.. js:class:: AppletTCP
3236
3237 This class is used with applets that requires the 'tcp' mode. The tcp applet
3238 can be registered with the *core.register_service()* function. They are used
3239 for processing a tcp stream like a server in back of HAProxy.
3240
Thierry FOURNIERdc595002015-12-21 11:13:52 +01003241.. js:attribute:: AppletTCP.c
3242
3243 :returns: A :ref:`converters_class`
3244
3245 This attribute contains a Converters class object.
3246
3247.. js:attribute:: AppletTCP.sc
3248
3249 :returns: A :ref:`converters_class`
3250
3251 This attribute contains a Converters class object. The
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02003252 functions of this object always return a string.
Thierry FOURNIERdc595002015-12-21 11:13:52 +01003253
3254.. js:attribute:: AppletTCP.f
3255
3256 :returns: A :ref:`fetches_class`
3257
3258 This attribute contains a Fetches class object.
3259
3260.. js:attribute:: AppletTCP.sf
3261
3262 :returns: A :ref:`fetches_class`
3263
3264 This attribute contains a Fetches class object.
3265
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01003266.. js:function:: AppletTCP.getline(applet)
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02003267
3268 This function returns a string containing one line from the stream. If the
3269 data returned doesn't contains a final '\\n' its assumed than its the last
3270 available data before the end of stream.
3271
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01003272 :param class_AppletTCP applet: An :ref:`applettcp_class`
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02003273 :returns: a string. The string can be empty if we reach the end of the stream.
3274
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01003275.. js:function:: AppletTCP.receive(applet, [size])
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02003276
3277 Reads data from the TCP stream, according to the specified read *size*. If the
3278 *size* is missing, the function tries to read all the content of the stream
3279 until the end.
3280
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01003281 :param class_AppletTCP applet: An :ref:`applettcp_class`
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02003282 :param integer size: the required read size.
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02003283 :returns: always return a string, the string can be empty if the connection is
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02003284 closed.
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02003285
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01003286.. js:function:: AppletTCP.send(appletmsg)
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02003287
3288 Send the message on the stream.
3289
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01003290 :param class_AppletTCP applet: An :ref:`applettcp_class`
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02003291 :param string msg: the message to send.
3292
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01003293.. js:function:: AppletTCP.get_priv(applet)
3294
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01003295 Return Lua data stored in the current transaction. If no data are stored,
3296 it returns a nil value.
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01003297
3298 :param class_AppletTCP applet: An :ref:`applettcp_class`
Bertrand Jacquin874a35c2018-09-10 21:26:07 +01003299 :returns: the opaque data previously stored, or nil if nothing is
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02003300 available.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01003301 :see: :js:func:`AppletTCP.set_priv`
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01003302
3303.. js:function:: AppletTCP.set_priv(applet, data)
3304
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02003305 Store any data in the current HAProxy transaction. This action replaces the
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01003306 old stored data.
3307
3308 :param class_AppletTCP applet: An :ref:`applettcp_class`
3309 :param opaque data: The data which is stored in the transaction.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01003310 :see: :js:func:`AppletTCP.get_priv`
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01003311
Tim Duesterhus4e172c92020-05-19 13:49:42 +02003312.. js:function:: AppletTCP.set_var(applet, var, value[, ifexist])
Thierry FOURNIER / OZON.IOc1edafe2016-12-12 16:25:30 +01003313
3314 Converts a Lua type in a HAProxy type and stores it in a variable <var>.
3315
3316 :param class_AppletTCP applet: An :ref:`applettcp_class`
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02003317 :param string var: The variable name according with the HAProxy variable
3318 syntax.
3319 :param type value: The value associated to the variable. The type can be
3320 string or integer.
3321 :param boolean ifexist: If this parameter is set to true the variable will
3322 only be set if it was defined elsewhere (i.e. used within the configuration).
3323 For global variables (using the "proc" scope), they will only be updated and
3324 never created. It is highly recommended to always set this to true.
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02003325
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01003326 :see: :js:func:`AppletTCP.unset_var`
3327 :see: :js:func:`AppletTCP.get_var`
Thierry FOURNIER / OZON.IOc1edafe2016-12-12 16:25:30 +01003328
3329.. js:function:: AppletTCP.unset_var(applet, var)
3330
3331 Unsets the variable <var>.
3332
3333 :param class_AppletTCP applet: An :ref:`applettcp_class`
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02003334 :param string var: The variable name according with the HAProxy variable
3335 syntax.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01003336 :see: :js:func:`AppletTCP.unset_var`
3337 :see: :js:func:`AppletTCP.set_var`
Thierry FOURNIER / OZON.IOc1edafe2016-12-12 16:25:30 +01003338
3339.. js:function:: AppletTCP.get_var(applet, var)
3340
3341 Returns data stored in the variable <var> converter in Lua type.
3342
3343 :param class_AppletTCP applet: An :ref:`applettcp_class`
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02003344 :param string var: The variable name according with the HAProxy variable
3345 syntax.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01003346 :see: :js:func:`AppletTCP.unset_var`
3347 :see: :js:func:`AppletTCP.set_var`
Thierry FOURNIER / OZON.IOc1edafe2016-12-12 16:25:30 +01003348
Adis Nezirovic8878f8e2018-07-13 12:18:33 +02003349StickTable class
3350================
3351
3352.. js:class:: StickTable
3353
3354 **context**: task, action, sample-fetch
3355
3356 This class can be used to access the HAProxy stick tables from Lua.
3357
3358.. js:function:: StickTable.info()
3359
3360 Returns stick table attributes as a Lua table. See HAProxy documentation for
Ilya Shipitsin2272d8a2020-12-21 01:22:40 +05003361 "stick-table" for canonical info, or check out example below.
Adis Nezirovic8878f8e2018-07-13 12:18:33 +02003362
3363 :returns: Lua table
3364
3365 Assume our table has IPv4 key and gpc0 and conn_rate "columns":
3366
3367.. code-block:: lua
3368
3369 {
3370 expire=<int>, # Value in ms
3371 size=<int>, # Maximum table size
3372 used=<int>, # Actual number of entries in table
3373 data={ # Data columns, with types as key, and periods as values
3374 (-1 if type is not rate counter)
3375 conn_rate=<int>,
3376 gpc0=-1
3377 },
3378 length=<int>, # max string length for string table keys, key length
3379 # otherwise
3380 nopurge=<boolean>, # purge oldest entries when table is full
3381 type="ip" # can be "ip", "ipv6", "integer", "string", "binary"
3382 }
3383
3384.. js:function:: StickTable.lookup(key)
3385
3386 Returns stick table entry for given <key>
3387
3388 :param string key: Stick table key (IP addresses and strings are supported)
3389 :returns: Lua table
3390
3391.. js:function:: StickTable.dump([filter])
3392
3393 Returns all entries in stick table. An optional filter can be used
3394 to extract entries with specific data values. Filter is a table with valid
3395 comparison operators as keys followed by data type name and value pairs.
3396 Check out the HAProxy docs for "show table" for more details. For the
3397 reference, the supported operators are:
Aurelien DARRAGON21f7ebb2023-03-13 19:49:31 +01003398
Adis Nezirovic8878f8e2018-07-13 12:18:33 +02003399 "eq", "ne", "le", "lt", "ge", "gt"
3400
3401 For large tables, execution of this function can take a long time (for
3402 HAProxy standards). That's also true when filter is used, so take care and
3403 measure the impact.
3404
3405 :param table filter: Stick table filter
3406 :returns: Stick table entries (table)
3407
3408 See below for example filter, which contains 4 entries (or comparisons).
3409 (Maximum number of filter entries is 4, defined in the source code)
3410
3411.. code-block:: lua
3412
3413 local filter = {
3414 {"gpc0", "gt", 30}, {"gpc1", "gt", 20}}, {"conn_rate", "le", 10}
3415 }
3416
Christopher Faulet0f3c8902020-01-31 18:57:12 +01003417.. _action_class:
3418
3419Action class
3420=============
3421
3422.. js:class:: Act
3423
3424 **context**: action
3425
3426 This class contains all return codes an action may return. It is the lua
3427 equivalent to HAProxy "ACT_RET_*" code.
3428
3429.. code-block:: lua
3430
3431 core.register_action("deny", { "http-req" }, function (txn)
3432 return act.DENY
3433 end)
3434..
3435.. js:attribute:: act.CONTINUE
3436
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02003437 This attribute is an integer (0). It instructs HAProxy to continue the
3438 current ruleset processing on the message. It is the default return code
3439 for a lua action.
Christopher Faulet0f3c8902020-01-31 18:57:12 +01003440
3441 :returns: integer
3442
3443.. js:attribute:: act.STOP
3444
3445 This attribute is an integer (1). It instructs HAProxy to stop the current
3446 ruleset processing on the message.
3447
3448.. js:attribute:: act.YIELD
3449
3450 This attribute is an integer (2). It instructs HAProxy to temporarily pause
3451 the message processing. It will be resumed later on the same rule. The
3452 corresponding lua script is re-executed for the start.
3453
3454.. js:attribute:: act.ERROR
3455
3456 This attribute is an integer (3). It triggers an internal errors The message
3457 processing is stopped and the transaction is terminated. For HTTP streams, an
3458 HTTP 500 error is returned to the client.
3459
3460 :returns: integer
3461
3462.. js:attribute:: act.DONE
3463
3464 This attribute is an integer (4). It instructs HAProxy to stop the message
3465 processing.
3466
3467 :returns: integer
3468
3469.. js:attribute:: act.DENY
3470
3471 This attribute is an integer (5). It denies the current message. The message
3472 processing is stopped and the transaction is terminated. For HTTP streams, an
3473 HTTP 403 error is returned to the client if the deny is returned during the
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02003474 request analysis. During the response analysis, a HTTP 502 error is returned
Christopher Faulet0f3c8902020-01-31 18:57:12 +01003475 and the server response is discarded.
3476
3477 :returns: integer
3478
3479.. js:attribute:: act.ABORT
3480
3481 This attribute is an integer (6). It aborts the current message. The message
3482 processing is stopped and the transaction is terminated. For HTTP streams,
Willy Tarreau714f3452021-05-09 06:47:26 +02003483 HAProxy assumes a response was already sent to the client. From the Lua
Christopher Faulet0f3c8902020-01-31 18:57:12 +01003484 actions point of view, when this code is used, the transaction is terminated
3485 with no reply.
3486
3487 :returns: integer
3488
3489.. js:attribute:: act.INVALID
3490
3491 This attribute is an integer (7). It triggers an internal errors. The message
3492 processing is stopped and the transaction is terminated. For HTTP streams, an
3493 HTTP 400 error is returned to the client if the error is returned during the
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02003494 request analysis. During the response analysis, a HTTP 502 error is returned
Christopher Faulet0f3c8902020-01-31 18:57:12 +01003495 and the server response is discarded.
3496
3497 :returns: integer
Adis Nezirovic8878f8e2018-07-13 12:18:33 +02003498
Christopher Faulet2c2c2e32020-01-31 19:07:52 +01003499.. js:function:: act:wake_time(milliseconds)
3500
3501 **context**: action
3502
3503 Set the script pause timeout to the specified time, defined in
3504 milliseconds.
3505
3506 :param integer milliseconds: the required milliseconds.
3507
3508 This function may be used when a lua action returns `act.YIELD`, to force its
3509 wake-up at most after the specified number of milliseconds.
3510
Christopher Faulet5a2c6612021-08-15 20:35:25 +02003511.. _filter_class:
3512
3513Filter class
3514=============
3515
3516.. js:class:: filter
3517
3518 **context**: filter
3519
3520 This class contains return codes some filter callback functions may return. It
3521 also contains configuration flags and some helper functions. To understand how
3522 the filter API works, see `doc/internal/filters.txt` documentation.
3523
3524.. js:attribute:: filter.CONTINUE
3525
3526 This attribute is an integer (1). It may be returned by some filter callback
3527 functions to instruct this filtering step is finished for this filter.
3528
3529.. js:attribute:: filter.WAIT
3530
3531 This attribute is an integer (0). It may be returned by some filter callback
3532 functions to instruct the filtering must be paused, waiting for more data or
3533 for an external event depending on this filter.
3534
3535.. js:attribute:: filter.ERROR
3536
3537 This attribute is an integer (-1). It may be returned by some filter callback
3538 functions to trigger an error.
3539
3540.. js:attribute:: filter.FLT_CFG_FL_HTX
3541
3542 This attribute is a flag corresponding to the filter flag FLT_CFG_FL_HTX. When
3543 it is set for a filter, it means the filter is able to filter HTTP streams.
3544
3545.. js:function:: filter.register_data_filter(chn)
3546
3547 **context**: filter
3548
3549 Enable the data filtering on the channel **chn** for the current filter. It
Ilya Shipitsinff0f2782021-08-22 22:18:07 +05003550 may be called at any time from any callback functions proceeding the data
Christopher Faulet5a2c6612021-08-15 20:35:25 +02003551 analysis.
3552
3553 :param class_Channel chn: A :ref:`channel_class`.
3554
3555.. js:function:: filter.unregister_data_filter(chn)
3556
3557 **context**: filter
3558
3559 Disable the data filtering on the channel **chn** for the current filter. It
3560 may be called at any time from any callback functions.
3561
3562 :param class_Channel chn: A :ref:`channel_class`.
3563
3564.. js:function:: filter.wake_time(milliseconds)
3565
3566 **context**: filter
3567
3568 Set the script pause timeout to the specified time, defined in
3569 milliseconds.
3570
3571 :param integer milliseconds: the required milliseconds.
3572
3573 This function may be used from any lua filter callback function to force its
3574 wake-up at most after the specified number of milliseconds. Especially, when
3575 `filter.CONTINUE` is returned.
3576
3577
3578A filters is declared using :js:func:`core.register_filter()` function. The
3579provided class will be used to instantiate filters. It may define following
3580attributes:
3581
3582* id: The filter identifier. It is a string that identifies the filter and is
3583 optional.
3584
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02003585* flags: The filter flags. Only :js:attr:`filter.FLT_CFG_FL_HTX` may be set
3586 for now.
Christopher Faulet5a2c6612021-08-15 20:35:25 +02003587
3588Such filter class must also define all required callback functions in the
3589following list. Note that :js:func:`Filter.new()` must be defined otherwise the
Ilya Shipitsinff0f2782021-08-22 22:18:07 +05003590filter is ignored. Others are optional.
Christopher Faulet5a2c6612021-08-15 20:35:25 +02003591
3592* .. js:function:: FILTER.new()
3593
3594 Called to instantiate a new filter. This function must be defined.
3595
3596 :returns: a Lua object that will be used as filter instance for the current
3597 stream.
3598
3599* .. js:function:: FILTER.start_analyze(flt, txn, chn)
3600
3601 Called when the analysis starts on the channel **chn**.
3602
3603* .. js:function:: FILTER.end_analyze(flt, txn, chn)
3604
3605 Called when the analysis ends on the channel **chn**.
3606
3607* .. js:function:: FILTER.http_headers(flt, txn, http_msg)
3608
3609 Called just before the HTTP payload analysis and after any processing on the
3610 HTTP message **http_msg**. This callback functions is only called for HTTP
3611 streams.
3612
3613* .. js:function:: FILTER.http_payload(flt, txn, http_msg)
3614
3615 Called during the HTTP payload analysis on the HTTP message **http_msg**. This
3616 callback functions is only called for HTTP streams.
3617
3618* .. js:function:: FILTER.http_end(flt, txn, http_msg)
3619
3620 Called after the HTTP payload analysis on the HTTP message **http_msg**. This
3621 callback functions is only called for HTTP streams.
3622
3623* .. js:function:: FILTER.tcp_payload(flt, txn, chn)
3624
3625 Called during the TCP payload analysis on the channel **chn**.
3626
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02003627Here is a full example:
Christopher Faulet5a2c6612021-08-15 20:35:25 +02003628
3629.. code-block:: lua
3630
3631 Trace = {}
3632 Trace.id = "Lua trace filter"
3633 Trace.flags = filter.FLT_CFG_FL_HTX;
3634 Trace.__index = Trace
3635
3636 function Trace:new()
3637 local trace = {}
3638 setmetatable(trace, Trace)
3639 trace.req_len = 0
3640 trace.res_len = 0
3641 return trace
3642 end
3643
3644 function Trace:start_analyze(txn, chn)
3645 if chn:is_resp() then
3646 print("Start response analysis")
3647 else
3648 print("Start request analysis")
3649 end
3650 filter.register_data_filter(self, chn)
3651 end
3652
3653 function Trace:end_analyze(txn, chn)
3654 if chn:is_resp() then
3655 print("End response analysis: "..self.res_len.." bytes filtered")
3656 else
3657 print("End request analysis: "..self.req_len.." bytes filtered")
3658 end
3659 end
3660
3661 function Trace:http_headers(txn, http_msg)
3662 stline = http_msg:get_stline()
3663 if http_msg.channel:is_resp() then
3664 print("response:")
3665 print(stline.version.." "..stline.code.." "..stline.reason)
3666 else
3667 print("request:")
3668 print(stline.method.." "..stline.uri.." "..stline.version)
3669 end
3670
3671 for n, hdrs in pairs(http_msg:get_headers()) do
3672 for i,v in pairs(hdrs) do
3673 print(n..": "..v)
3674 end
3675 end
3676 return filter.CONTINUE
3677 end
3678
3679 function Trace:http_payload(txn, http_msg)
3680 body = http_msg:body(-20000)
3681 if http_msg.channel:is_resp() then
3682 self.res_len = self.res_len + body:len()
3683 else
3684 self.req_len = self.req_len + body:len()
3685 end
3686 end
3687
3688 core.register_filter("trace", Trace, function(trace, args)
3689 return trace
3690 end)
3691
3692..
3693
3694.. _httpmessage_class:
3695
3696HTTPMessage class
3697===================
3698
3699.. js:class:: HTTPMessage
3700
3701 **context**: filter
3702
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02003703 This class contains all functions to manipulate a HTTP message. For now, this
Christopher Faulet5a2c6612021-08-15 20:35:25 +02003704 class is only available from a filter context.
3705
3706.. js:function:: HTTPMessage.add_header(http_msg, name, value)
3707
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02003708 Appends a HTTP header field in the HTTP message **http_msg** whose name is
Christopher Faulet5a2c6612021-08-15 20:35:25 +02003709 specified in **name** and whose value is defined in **value**.
3710
3711 :param class_httpmessage http_msg: The manipulated HTTP message.
3712 :param string name: The header name.
3713 :param string value: The header value.
3714
3715.. js:function:: HTTPMessage.append(http_msg, string)
3716
3717 This function copies the string **string** at the end of incoming data of the
3718 HTTP message **http_msg**. The function returns the copied length on success
3719 or -1 if data cannot be copied.
3720
3721 Same that :js:func:`HTTPMessage.insert(http_msg, string, http_msg:input())`.
3722
3723 :param class_httpmessage http_msg: The manipulated HTTP message.
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02003724 :param string string: The data to copy at the end of incoming data.
Christopher Faulet5a2c6612021-08-15 20:35:25 +02003725 :returns: an integer containing the amount of bytes copied or -1.
3726
3727.. js:function:: HTTPMessage.body(http_msgl[, offset[, length]])
3728
3729 This function returns **length** bytes of incoming data from the HTTP message
3730 **http_msg**, starting at the offset **offset**. The data are not removed from
3731 the buffer.
3732
3733 By default, if no length is provided, all incoming data found, starting at the
3734 given offset, are returned. If **length** is set to -1, the function tries to
3735 retrieve a maximum of data. Because it is called in the filter context, it
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02003736 never yield. Not providing an offset is the same as setting it to 0. A
Ilya Shipitsinff0f2782021-08-22 22:18:07 +05003737 positive offset is relative to the beginning of incoming data of the
Christopher Faulet5a2c6612021-08-15 20:35:25 +02003738 http_message buffer while negative offset is relative to their end.
3739
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02003740 If there is no incoming data and the HTTP message can't receive more data,
3741 a 'nil' value is returned.
Christopher Faulet5a2c6612021-08-15 20:35:25 +02003742
3743 :param class_httpmessage http_msg: The manipulated HTTP message.
3744 :param integer offset: *optional* The offset in incoming data to start to get
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02003745 data. 0 by default. May be negative to be relative to the end of incoming
3746 data.
3747 :param integer length: *optional* The expected length of data to retrieve.
3748 All incoming data by default. May be set to -1 to get a maximum of data.
Christopher Faulet5a2c6612021-08-15 20:35:25 +02003749 :returns: a string containing the data found or nil.
3750
3751.. js:function:: HTTPMessage.eom(http_msg)
3752
3753 This function returns true if the end of message is reached for the HTTP
3754 message **http_msg**.
3755
3756 :param class_httpmessage http_msg: The manipulated HTTP message.
3757 :returns: an integer containing the amount of available bytes.
3758
3759.. js:function:: HTTPMessage.del_header(http_msg, name)
3760
3761 Removes all HTTP header fields in the HTTP message **http_msg** whose name is
3762 specified in **name**.
3763
3764 :param class_httpmessage http_msg: The manipulated http message.
3765 :param string name: The header name.
3766
3767.. js:function:: HTTPMessage.get_headers(http_msg)
3768
3769 Returns a table containing all the headers of the HTTP message **http_msg**.
3770
3771 :param class_httpmessage http_msg: The manipulated http message.
3772 :returns: table of headers.
3773
3774 This is the form of the returned table:
3775
3776.. code-block:: lua
3777
3778 http_msg:get_headers()['<header-name>'][<header-index>] = "<header-value>"
3779
3780 local hdr = http_msg:get_headers()
3781 hdr["host"][0] = "www.test.com"
3782 hdr["accept"][0] = "audio/basic q=1"
3783 hdr["accept"][1] = "audio/*, q=0.2"
3784 hdr["accept"][2] = "*.*, q=0.1"
3785..
3786
3787.. js:function:: HTTPMessage.get_stline(http_msg)
3788
3789 Returns a table containing the start-line of the HTTP message **http_msg**.
3790
3791 :param class_httpmessage http_msg: The manipulated http message.
3792 :returns: the start-line.
3793
3794 This is the form of the returned table:
3795
3796.. code-block:: lua
3797
3798 -- for the request :
3799 {"method" = string, "uri" = string, "version" = string}
3800
3801 -- for the response:
3802 {"version" = string, "code" = string, "reason" = string}
3803..
3804
3805.. js:function:: HTTPMessage.forward(http_msg, length)
3806
3807 This function forwards **length** bytes of data from the HTTP message
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02003808 **http_msg**. Because it is called in the filter context, it never yields. Only
Christopher Faulet5a2c6612021-08-15 20:35:25 +02003809 available incoming data may be forwarded, event if the requested length
3810 exceeds the available amount of incoming data. It returns the amount of data
3811 forwarded.
3812
3813 :param class_httpmessage http_msg: The manipulated HTTP message.
3814 :param integer int: The amount of data to forward.
3815
3816.. js:function:: HTTPMessage.input(http_msg)
3817
3818 This function returns the length of incoming data in the HTTP message
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02003819 **http_msg** from the filter point of view.
Christopher Faulet5a2c6612021-08-15 20:35:25 +02003820
3821 :param class_httpmessage http_msg: The manipulated HTTP message.
3822 :returns: an integer containing the amount of available bytes.
3823
3824.. js:function:: HTTPMessage.insert(http_msg, string[, offset])
3825
3826 This function copies the string **string** at the offset **offset** in
3827 incoming data of the HTTP message **http_msg**. The function returns the
3828 copied length on success or -1 if data cannot be copied.
3829
3830 By default, if no offset is provided, the string is copied in front of
Ilya Shipitsinff0f2782021-08-22 22:18:07 +05003831 incoming data. A positive offset is relative to the beginning of incoming data
Christopher Faulet5a2c6612021-08-15 20:35:25 +02003832 of the HTTP message while negative offset is relative to their end.
3833
3834 :param class_httpmessage http_msg: The manipulated HTTP message.
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02003835 :param string string: The data to copy into incoming data.
3836 :param integer offset: *optional* The offset in incoming data where to copy
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02003837 data. 0 by default. May be negative to be relative to the end of incoming
3838 data.
Christopher Faulet5a2c6612021-08-15 20:35:25 +02003839 :returns: an integer containing the amount of bytes copied or -1.
3840
3841.. js:function:: HTTPMessage.is_full(http_msg)
3842
3843 This function returns true if the HTTP message **http_msg** is full.
3844
3845 :param class_httpmessage http_msg: The manipulated HTTP message.
3846 :returns: a boolean
3847
3848.. js:function:: HTTPMessage.is_resp(http_msg)
3849
3850 This function returns true if the HTTP message **http_msg** is the response
3851 one.
3852
3853 :param class_httpmessage http_msg: The manipulated HTTP message.
3854 :returns: a boolean
3855
3856.. js:function:: HTTPMessage.may_recv(http_msg)
3857
3858 This function returns true if the HTTP message **http_msg** may still receive
3859 data.
3860
3861 :param class_httpmessage http_msg: The manipulated HTTP message.
3862 :returns: a boolean
3863
3864.. js:function:: HTTPMessage.output(http_msg)
3865
3866 This function returns the length of outgoing data of the HTTP message
3867 **http_msg**.
3868
3869 :param class_httpmessage http_msg: The manipulated HTTP message.
3870 :returns: an integer containing the amount of available bytes.
3871
3872.. js:function:: HTTPMessage.prepend(http_msg, string)
3873
3874 This function copies the string **string** in front of incoming data of the
3875 HTTP message **http_msg**. The function returns the copied length on success
3876 or -1 if data cannot be copied.
3877
3878 Same that :js:func:`HTTPMessage.insert(http_msg, string, 0)`.
3879
3880 :param class_httpmessage http_msg: The manipulated HTTP message.
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02003881 :param string string: The data to copy in front of incoming data.
Christopher Faulet5a2c6612021-08-15 20:35:25 +02003882 :returns: an integer containing the amount of bytes copied or -1.
3883
3884.. js:function:: HTTPMessage.remove(http_msg[, offset[, length]])
3885
3886 This function removes **length** bytes of incoming data of the HTTP message
3887 **http_msg**, starting at offset **offset**. This function returns number of
3888 bytes removed on success.
3889
3890 By default, if no length is provided, all incoming data, starting at the given
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02003891 offset, are removed. Not providing an offset is the same that setting it
Ilya Shipitsinff0f2782021-08-22 22:18:07 +05003892 to 0. A positive offset is relative to the beginning of incoming data of the
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02003893 HTTP message while negative offset is relative to the end.
Christopher Faulet5a2c6612021-08-15 20:35:25 +02003894
3895 :param class_httpmessage http_msg: The manipulated HTTP message.
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02003896 :param integer offset: *optional* The offset in incoming data where to start
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02003897 to remove data. 0 by default. May be negative to be relative to the end of
3898 incoming data.
Christopher Faulet5a2c6612021-08-15 20:35:25 +02003899 :param integer length: *optional* The length of data to remove. All incoming
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02003900 data by default.
Christopher Faulet5a2c6612021-08-15 20:35:25 +02003901 :returns: an integer containing the amount of bytes removed.
3902
3903.. js:function:: HTTPMessage.rep_header(http_msg, name, regex, replace)
3904
3905 Matches the regular expression in all occurrences of header field **name**
3906 according to regex **regex**, and replaces them with the string **replace**.
3907 The replacement value can contain back references like \1, \2, ... This
3908 function acts on whole header lines, regardless of the number of values they
3909 may contain.
3910
3911 :param class_httpmessage http_msg: The manipulated HTTP message.
3912 :param string name: The header name.
3913 :param string regex: The match regular expression.
3914 :param string replace: The replacement value.
3915
3916.. js:function:: HTTPMessage.rep_value(http_msg, name, regex, replace)
3917
3918 Matches the regular expression on every comma-delimited value of header field
3919 **name** according to regex **regex**, and replaces them with the string
3920 **replace**. The replacement value can contain back references like \1, \2,
3921 ...
3922
3923 :param class_httpmessage http_msg: The manipulated HTTP message.
3924 :param string name: The header name.
3925 :param string regex: The match regular expression.
3926 :param string replace: The replacement value.
3927
3928.. js:function:: HTTPMessage.send(http_msg, string)
3929
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02003930 This function requires immediate send of the string **string**. It means the
Ilya Shipitsinff0f2782021-08-22 22:18:07 +05003931 string is copied at the beginning of incoming data of the HTTP message
Christopher Faulet5a2c6612021-08-15 20:35:25 +02003932 **http_msg** and immediately forwarded. Because it is called in the filter
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02003933 context, it never yields.
Christopher Faulet5a2c6612021-08-15 20:35:25 +02003934
3935 :param class_httpmessage http_msg: The manipulated HTTP message.
3936 :param string string: The data to send.
3937 :returns: an integer containing the amount of bytes copied or -1.
3938
3939.. js:function:: HTTPMessage.set(http_msg, string[, offset[, length]])
3940
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02003941 This function replaces **length** bytes of incoming data of the HTTP message
Christopher Faulet5a2c6612021-08-15 20:35:25 +02003942 **http_msg**, starting at offset **offset**, by the string **string**. The
3943 function returns the copied length on success or -1 if data cannot be copied.
3944
3945 By default, if no length is provided, all incoming data, starting at the given
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02003946 offset, are replaced. Not providing an offset is the same as setting it
Ilya Shipitsinff0f2782021-08-22 22:18:07 +05003947 to 0. A positive offset is relative to the beginning of incoming data of the
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02003948 HTTP message while negative offset is relative to the end.
Christopher Faulet5a2c6612021-08-15 20:35:25 +02003949
3950 :param class_httpmessage http_msg: The manipulated HTTP message.
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02003951 :param string string: The data to copy into incoming data.
3952 :param integer offset: *optional* The offset in incoming data where to start
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02003953 the data replacement. 0 by default. May be negative to be relative to the
3954 end of incoming data.
Christopher Faulet5a2c6612021-08-15 20:35:25 +02003955 :param integer length: *optional* The length of data to replace. All incoming
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02003956 data by default.
Christopher Faulet5a2c6612021-08-15 20:35:25 +02003957 :returns: an integer containing the amount of bytes copied or -1.
3958
3959.. js:function:: HTTPMessage.set_eom(http_msg)
3960
3961 This function set the end of message for the HTTP message **http_msg**.
3962
3963 :param class_httpmessage http_msg: The manipulated HTTP message.
3964
3965.. js:function:: HTTPMessage.set_header(http_msg, name, value)
3966
3967 This variable replace all occurrence of all header matching the name **name**,
3968 by only one containing the value **value**.
3969
3970 :param class_httpmessage http_msg: The manipulated HTTP message.
3971 :param string name: The header name.
3972 :param string value: The header value.
3973
3974 This function does the same work as the following code:
3975
3976.. code-block:: lua
3977
3978 http_msg:del_header("header")
3979 http_msg:add_header("header", "value")
3980..
3981
3982.. js:function:: HTTPMessage.set_method(http_msg, method)
3983
3984 Rewrites the request method with the string **method**. The HTTP message
3985 **http_msg** must be the request.
3986
3987 :param class_httpmessage http_msg: The manipulated HTTP message.
3988 :param string method: The new method.
3989
3990.. js:function:: HTTPMessage.set_path(http_msg, path)
3991
3992 Rewrites the request path with the string **path**. The HTTP message
3993 **http_msg** must be the request.
3994
3995 :param class_httpmessage http_msg: The manipulated HTTP message.
3996 :param string method: The new method.
3997
3998.. js:function:: HTTPMessage.set_query(http_msg, query)
3999
4000 Rewrites the request's query string which appears after the first question
4001 mark ("?") with the string **query**. The HTTP message **http_msg** must be
4002 the request.
4003
4004 :param class_httpmessage http_msg: The manipulated HTTP message.
4005 :param string query: The new query.
4006
4007.. js:function:: HTTPMessage.set_status(http_msg, status[, reason])
4008
Ilya Shipitsinff0f2782021-08-22 22:18:07 +05004009 Rewrites the response status code with the integer **code** and optional the
Christopher Faulet5a2c6612021-08-15 20:35:25 +02004010 reason **reason**. If no custom reason is provided, it will be generated from
4011 the status. The HTTP message **http_msg** must be the response.
4012
4013 :param class_httpmessage http_msg: The manipulated HTTP message.
4014 :param integer status: The new response status code.
4015 :param string reason: The new response reason (optional).
4016
4017.. js:function:: HTTPMessage.set_uri(http_msg, uri)
4018
4019 Rewrites the request URI with the string **uri**. The HTTP message
4020 **http_msg** must be the request.
4021
4022 :param class_httpmessage http_msg: The manipulated HTTP message.
4023 :param string uri: The new uri.
4024
4025.. js:function:: HTTPMessage.unset_eom(http_msg)
4026
4027 This function remove the end of message for the HTTP message **http_msg**.
4028
4029 :param class_httpmessage http_msg: The manipulated HTTP message.
4030
William Lallemand10cea5c2022-03-30 16:02:43 +02004031.. _CertCache_class:
4032
4033CertCache class
4034================
4035
4036.. js:class:: CertCache
4037
4038 This class allows to update an SSL certificate file in the memory of the
4039 current HAProxy process. It will do the same as "set ssl cert" + "commit ssl
4040 cert" over the HAProxy CLI.
4041
4042.. js:function:: CertCache.set(certificate)
4043
4044 This function updates a certificate in memory.
4045
4046 :param table certificate: A table containing the fields to update.
4047 :param string certificate.filename: The mandatory filename of the certificate
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02004048 to update, it must already exist in memory.
William Lallemand10cea5c2022-03-30 16:02:43 +02004049 :param string certificate.crt: A certificate in the PEM format. It can also
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02004050 contain a private key.
William Lallemand10cea5c2022-03-30 16:02:43 +02004051 :param string certificate.key: A private key in the PEM format.
4052 :param string certificate.ocsp: An OCSP response in base64. (cf management.txt)
4053 :param string certificate.issuer: The certificate of the OCSP issuer.
4054 :param string certificate.sctl: An SCTL file.
4055
4056.. code-block:: lua
4057
4058 CertCache.set{filename="certs/localhost9994.pem.rsa", crt=crt}
4059
4060
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01004061External Lua libraries
4062======================
4063
4064A lot of useful lua libraries can be found here:
4065
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02004066* Lua toolbox has been superseded by
4067 `https://luarocks.org/ <https://luarocks.org/>`_
4068
4069 The old lua toolbox source code is still available here
4070 `https://github.com/catwell/lua-toolbox <https://github.com/catwell/lua-toolbox>`_ (DEPRECATED)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01004071
Ilya Shipitsin2075ca82020-03-06 23:22:22 +05004072Redis client library:
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01004073
4074* `https://github.com/nrk/redis-lua <https://github.com/nrk/redis-lua>`_
4075
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02004076This is an example about the usage of the Redis library within HAProxy.
4077Note that each call to any function of this library can throw an error if
4078the socket connection fails.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01004079
4080.. code-block:: lua
4081
4082 -- load the redis library
4083 local redis = require("redis");
4084
4085 function do_something(txn)
4086
4087 -- create and connect new tcp socket
4088 local tcp = core.tcp();
4089 tcp:settimeout(1);
4090 tcp:connect("127.0.0.1", 6379);
4091
4092 -- use the redis library with this new socket
4093 local client = redis.connect({socket=tcp});
4094 client:ping();
4095
4096 end
4097
4098OpenSSL:
4099
4100* `http://mkottman.github.io/luacrypto/index.html
4101 <http://mkottman.github.io/luacrypto/index.html>`_
4102
4103* `https://github.com/brunoos/luasec/wiki
4104 <https://github.com/brunoos/luasec/wiki>`_