blob: d790c6b6d990357c83907f027c89041b4788cd77 [file] [log] [blame]
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001.. toctree::
2 :maxdepth: 2
3
4
5How Lua runs in HAProxy
6=======================
7
8HAProxy Lua running contexts
9----------------------------
10
11The Lua code executed in HAProxy can be processed in 2 main modes. The first one
12is the **initialisation mode**, and the second is the **runtime mode**.
13
14* In the **initialisation mode**, we can perform DNS solves, but we cannot
15 perform socket I/O. In this initialisation mode, HAProxy still blocked during
16 the execution of the Lua program.
17
18* In the **runtime mode**, we cannot perform DNS solves, but we can use sockets.
19 The execution of the Lua code is multiplexed with the requests processing, so
20 the Lua code seems to be run in blocking, but it is not the case.
21
22The Lua code is loaded in one or more files. These files contains main code and
Aurelien DARRAGONc84899c2023-02-20 18:18:59 +010023functions. Lua has 8 execution contexts.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +010024
251. The Lua file **body context**. It is executed during the load of the Lua file
26 in the HAProxy `[global]` section with the directive `lua-load`. It is
27 executed in initialisation mode. This section is use for configuring Lua
28 bindings in HAProxy.
29
David Carlier61fdf8b2015-10-02 11:59:38 +0100302. The Lua **init context**. It is a Lua function executed just after the
Thierry FOURNIER17bd1522015-03-11 20:31:00 +010031 HAProxy configuration parsing. The execution is in initialisation mode. In
32 this context the HAProxy environment are already initialized. It is useful to
33 check configuration, or initializing socket connections or tasks. These
34 functions are declared in the body context with the Lua function
35 `core.register_init()`. The prototype of the function is a simple function
36 without return value and without parameters, like this: `function fcn()`.
37
David Carlier61fdf8b2015-10-02 11:59:38 +0100383. The Lua **task context**. It is a Lua function executed after the start
Thierry FOURNIER17bd1522015-03-11 20:31:00 +010039 of the HAProxy scheduler, and just after the declaration of the task with the
40 Lua function `core.register_task()`. This context can be concurrent with the
41 traffic processing. It is executed in runtime mode. The prototype of the
42 function is a simple function without return value and without parameters,
43 like this: `function fcn()`.
44
David Carlier61fdf8b2015-10-02 11:59:38 +0100454. The **action context**. It is a Lua function conditionally executed. These
Thierry FOURNIERa2d02252015-10-01 15:00:42 +020046 actions are registered by the Lua directives "`core.register_action()`". The
47 prototype of the Lua called function is a function with doesn't returns
48 anything and that take an object of class TXN as entry. `function fcn(txn)`.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +010049
505. The **sample-fetch context**. This function takes a TXN object as entry
51 argument and returns a string. These types of function cannot execute any
52 blocking function. They are useful to aggregate some of original HAProxy
53 sample-fetches and return the result. The prototype of the function is
54 `function string fcn(txn)`. These functions can be registered with the Lua
55 function `core.register_fetches()`. Each declared sample-fetch is prefixed by
56 the string "lua.".
57
Christopher Faulet1e9b1b62021-08-11 10:14:30 +020058 .. note::
59 It is possible that this function cannot found the required data in the
60 original HAProxy sample-fetches, in this case, it cannot return the
61 result. This case is not yet supported
Thierry FOURNIER17bd1522015-03-11 20:31:00 +010062
David Carlier61fdf8b2015-10-02 11:59:38 +0100636. The **converter context**. It is a Lua function that takes a string as input
Thierry FOURNIER17bd1522015-03-11 20:31:00 +010064 and returns another string as output. These types of function are stateless,
65 it cannot access to any context. They don't execute any blocking function.
66 The call prototype is `function string fcn(string)`. This function can be
67 registered with the Lua function `core.register_converters()`. Each declared
68 converter is prefixed by the string "lua.".
69
Christopher Faulet5a2c6612021-08-15 20:35:25 +0200707. The **filter context**: It is a Lua object based on a class defining filter
71 callback functions. Lua filters are registered using
72 `core.register_filter()`. Each declared filter is prefixed by the string
73 "lua.".
74
Aurelien DARRAGONc84899c2023-02-20 18:18:59 +0100758. The **event context**: Inside a function that handles events subscribed
76 through `core.event_sub()` or `Server.event_sub()`.
77
Christopher Faulet5a2c6612021-08-15 20:35:25 +020078
Thierry FOURNIER17bd1522015-03-11 20:31:00 +010079HAProxy Lua Hello world
80-----------------------
81
82HAProxy configuration file (`hello_world.conf`):
83
84::
85
86 global
87 lua-load hello_world.lua
88
89 listen proxy
90 bind 127.0.0.1:10001
Thierry FOURNIERa2d02252015-10-01 15:00:42 +020091 tcp-request inspect-delay 1s
92 tcp-request content use-service lua.hello_world
Thierry FOURNIER17bd1522015-03-11 20:31:00 +010093
94HAProxy Lua file (`hello_world.lua`):
95
96.. code-block:: lua
97
Thierry FOURNIERa2d02252015-10-01 15:00:42 +020098 core.register_service("hello_world", "tcp", function(applet)
99 applet:send("hello world\n")
100 end)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100101
102How to start HAProxy for testing this configuration:
103
104::
105
106 ./haproxy -f hello_world.conf
107
108On other terminal, you can test with telnet:
109
110::
111
112 #:~ telnet 127.0.0.1 10001
113 hello world
114
Thierry Fournierae6b5682022-09-19 09:04:16 +0200115Usage of load parameters
116------------------------
117
Ilya Shipitsin4a689da2022-10-29 09:34:32 +0500118HAProxy lua-load(-per-thread) directives allow a list of parameters after
Thierry Fournierae6b5682022-09-19 09:04:16 +0200119the lua file name. These parameters are accessible through an array of args
120using this code `local args = table.pack(...)` in the body of loaded file.
121
122Below, a new version of the hello world using load parameters
123
124HAProxy configuration file (`hello_world.conf`):
125
126::
127
128 global
129 lua-load hello_world.lua "this is not an hello world"
130
131 listen proxy
132 bind 127.0.0.1:10001
133 tcp-request inspect-delay 1s
134 tcp-request content use-service lua.hello_world
135
136HAProxy Lua file (`hello_world.lua`):
137
138.. code-block:: lua
139
140 local args = table.pack(...)
141
142 core.register_service("hello_world", "tcp", function(applet)
143 applet:send(args[1] .. "\n")
144 end)
145
146
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100147Core class
148==========
149
150.. js:class:: core
151
152 The "core" class contains all the HAProxy core functions. These function are
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +0200153 useful for the controlling of the execution flow, registering hooks,
154 manipulating global maps or ACL, ...
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100155
156 "core" class is basically provided with HAProxy. No `require` line is
157 required to uses these function.
158
David Carlier61fdf8b2015-10-02 11:59:38 +0100159 The "core" class is static, it is not possible to create a new object of this
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100160 type.
161
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100162.. js:attribute:: core.emerg
163
Thierry FOURNIERdc595002015-12-21 11:13:52 +0100164 :returns: integer
165
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +0200166 This attribute is an integer, it contains the value of the loglevel
167 "emergency" (0).
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100168
169.. js:attribute:: core.alert
170
Thierry FOURNIERdc595002015-12-21 11:13:52 +0100171 :returns: integer
172
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +0200173 This attribute is an integer, it contains the value of the loglevel
174 "alert" (1).
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100175
176.. js:attribute:: core.crit
177
Thierry FOURNIERdc595002015-12-21 11:13:52 +0100178 :returns: integer
179
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +0200180 This attribute is an integer, it contains the value of the loglevel
181 "critical" (2).
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100182
183.. js:attribute:: core.err
184
Thierry FOURNIERdc595002015-12-21 11:13:52 +0100185 :returns: integer
186
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +0200187 This attribute is an integer, it contains the value of the loglevel
188 "error" (3).
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100189
190.. js:attribute:: core.warning
191
Thierry FOURNIERdc595002015-12-21 11:13:52 +0100192 :returns: integer
193
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +0200194 This attribute is an integer, it contains the value of the loglevel
195 "warning" (4).
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100196
197.. js:attribute:: core.notice
198
Thierry FOURNIERdc595002015-12-21 11:13:52 +0100199 :returns: integer
200
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +0200201 This attribute is an integer, it contains the value of the loglevel
202 "notice" (5).
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100203
204.. js:attribute:: core.info
205
Thierry FOURNIERdc595002015-12-21 11:13:52 +0100206 :returns: integer
207
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +0200208 This attribute is an integer, it contains the value of the loglevel
209 "info" (6).
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100210
211.. js:attribute:: core.debug
212
Thierry FOURNIERdc595002015-12-21 11:13:52 +0100213 :returns: integer
214
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +0200215 This attribute is an integer, it contains the value of the loglevel
216 "debug" (7).
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100217
Thierry FOURNIER12a865d2016-12-14 19:40:37 +0100218.. js:attribute:: core.proxies
219
220 **context**: task, action, sample-fetch, converter
221
Patrick Hemmerc6a1d712018-05-01 21:30:41 -0400222 This attribute is a table of declared proxies (frontend and backends). Each
223 proxy give an access to his list of listeners and servers. The table is
224 indexed by proxy name, and each entry is of type :ref:`proxy_class`.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +0100225
Christopher Faulet1e9b1b62021-08-11 10:14:30 +0200226 .. Warning::
Aurelien DARRAGON53901f42022-10-13 19:49:42 +0200227 if you declared a frontend and backend with the same name, only one of
228 them will be listed.
Thierry FOURNIER9b82a582017-07-24 13:30:43 +0200229
230 :see: :js:attr:`core.backends`
231 :see: :js:attr:`core.frontends`
232
233.. js:attribute:: core.backends
234
235 **context**: task, action, sample-fetch, converter
236
Patrick Hemmerc6a1d712018-05-01 21:30:41 -0400237 This attribute is a table of declared proxies with backend capability. Each
238 proxy give an access to his list of listeners and servers. The table is
239 indexed by the backend name, and each entry is of type :ref:`proxy_class`.
Thierry FOURNIER9b82a582017-07-24 13:30:43 +0200240
241 :see: :js:attr:`core.proxies`
242 :see: :js:attr:`core.frontends`
243
244.. js:attribute:: core.frontends
245
246 **context**: task, action, sample-fetch, converter
247
Patrick Hemmerc6a1d712018-05-01 21:30:41 -0400248 This attribute is a table of declared proxies with frontend capability. Each
249 proxy give an access to his list of listeners and servers. The table is
250 indexed by the frontend name, and each entry is of type :ref:`proxy_class`.
Thierry FOURNIER9b82a582017-07-24 13:30:43 +0200251
252 :see: :js:attr:`core.proxies`
253 :see: :js:attr:`core.backends`
254
Thierry Fournierecb83c22020-11-28 15:49:44 +0100255.. js:attribute:: core.thread
256
257 **context**: task, action, sample-fetch, converter, applet
258
259 This variable contains the executing thread number starting at 1. 0 is a
260 special case for the common lua context. So, if thread is 0, Lua scope is
261 shared by all threads, otherwise the scope is dedicated to a single thread.
262 A program which needs to execute some parts exactly once regardless of the
263 number of threads can check that core.thread is 0 or 1.
264
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100265.. js:function:: core.log(loglevel, msg)
266
267 **context**: body, init, task, action, sample-fetch, converter
268
David Carlier61fdf8b2015-10-02 11:59:38 +0100269 This function sends a log. The log is sent, according with the HAProxy
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100270 configuration file, on the default syslog server if it is configured and on
271 the stderr if it is allowed.
272
Bertrand Jacquin874a35c2018-09-10 21:26:07 +0100273 :param integer loglevel: Is the log level associated with the message. It is a
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +0200274 number between 0 and 7.
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100275 :param string msg: The log content.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +0100276 :see: :js:attr:`core.emerg`, :js:attr:`core.alert`, :js:attr:`core.crit`,
277 :js:attr:`core.err`, :js:attr:`core.warning`, :js:attr:`core.notice`,
278 :js:attr:`core.info`, :js:attr:`core.debug` (log level definitions)
279 :see: :js:func:`core.Debug`
280 :see: :js:func:`core.Info`
281 :see: :js:func:`core.Warning`
282 :see: :js:func:`core.Alert`
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100283
284.. js:function:: core.Debug(msg)
285
286 **context**: body, init, task, action, sample-fetch, converter
287
288 :param string msg: The log content.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +0100289 :see: :js:func:`core.log`
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100290
291 Does the same job than:
292
293.. code-block:: lua
294
Thierry FOURNIERdc595002015-12-21 11:13:52 +0100295 function Debug(msg)
296 core.log(core.debug, msg)
297 end
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100298..
299
300.. js:function:: core.Info(msg)
301
302 **context**: body, init, task, action, sample-fetch, converter
303
304 :param string msg: The log content.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +0100305 :see: :js:func:`core.log`
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100306
307.. code-block:: lua
308
Thierry FOURNIERdc595002015-12-21 11:13:52 +0100309 function Info(msg)
310 core.log(core.info, msg)
311 end
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100312..
313
314.. js:function:: core.Warning(msg)
315
316 **context**: body, init, task, action, sample-fetch, converter
317
318 :param string msg: The log content.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +0100319 :see: :js:func:`core.log`
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100320
321.. code-block:: lua
322
Thierry FOURNIERdc595002015-12-21 11:13:52 +0100323 function Warning(msg)
324 core.log(core.warning, msg)
325 end
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100326..
327
328.. js:function:: core.Alert(msg)
329
330 **context**: body, init, task, action, sample-fetch, converter
331
332 :param string msg: The log content.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +0100333 :see: :js:func:`core.log`
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100334
335.. code-block:: lua
336
Thierry FOURNIERdc595002015-12-21 11:13:52 +0100337 function Alert(msg)
338 core.log(core.alert, msg)
339 end
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100340..
341
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100342.. js:function:: core.add_acl(filename, key)
343
344 **context**: init, task, action, sample-fetch, converter
345
346 Add the ACL *key* in the ACLs list referenced by the file *filename*.
347
348 :param string filename: the filename that reference the ACL entries.
349 :param string key: the key which will be added.
350
351.. js:function:: core.del_acl(filename, key)
352
353 **context**: init, task, action, sample-fetch, converter
354
355 Delete the ACL entry referenced by the key *key* in the list of ACLs
356 referenced by *filename*.
357
358 :param string filename: the filename that reference the ACL entries.
359 :param string key: the key which will be deleted.
360
361.. js:function:: core.del_map(filename, key)
362
363 **context**: init, task, action, sample-fetch, converter
364
365 Delete the map entry indexed with the specified key in the list of maps
366 referenced by his filename.
367
368 :param string filename: the filename that reference the map entries.
369 :param string key: the key which will be deleted.
370
Thierry Fourniereea77c02016-03-18 08:47:13 +0100371.. js:function:: core.get_info()
372
373 **context**: body, init, task, action, sample-fetch, converter
374
Aurelien DARRAGON53901f42022-10-13 19:49:42 +0200375 Returns HAProxy core information. We can find information like the uptime,
Thierry Fourniereea77c02016-03-18 08:47:13 +0100376 the pid, memory pool usage, tasks number, ...
377
Ilya Shipitsin5fa29b82022-12-07 09:46:19 +0500378 This information is also returned by the management socket via the command
Bertrand Jacquin874a35c2018-09-10 21:26:07 +0100379 "show info". See the management socket documentation for more information
Thierry Fourniereea77c02016-03-18 08:47:13 +0100380 about the content of these variables.
381
382 :returns: an array of values.
383
Thierry Fournierb1f46562016-01-21 09:46:15 +0100384.. js:function:: core.now()
385
386 **context**: body, init, task, action
387
388 This function returns the current time. The time returned is fixed by the
Bertrand Jacquin874a35c2018-09-10 21:26:07 +0100389 HAProxy core and assures than the hour will be monotonic and that the system
Thierry Fournierb1f46562016-01-21 09:46:15 +0100390 call 'gettimeofday' will not be called too. The time is refreshed between each
391 Lua execution or resume, so two consecutive call to the function "now" will
392 probably returns the same result.
393
Patrick Hemmerc6a1d712018-05-01 21:30:41 -0400394 :returns: a table which contains two entries "sec" and "usec". "sec"
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +0200395 contains the current at the epoch format, and "usec" contains the
396 current microseconds.
Thierry Fournierb1f46562016-01-21 09:46:15 +0100397
Thierry FOURNIERa78f0372016-12-14 19:04:41 +0100398.. js:function:: core.http_date(date)
399
400 **context**: body, init, task, action
401
Bertrand Jacquin874a35c2018-09-10 21:26:07 +0100402 This function take a string representing http date, and returns an integer
Thierry FOURNIERa78f0372016-12-14 19:04:41 +0100403 containing the corresponding date with a epoch format. A valid http date
404 me respect the format IMF, RFC850 or ASCTIME.
405
406 :param string date: a date http-date formatted
407 :returns: integer containing epoch date
408 :see: :js:func:`core.imf_date`.
409 :see: :js:func:`core.rfc850_date`.
410 :see: :js:func:`core.asctime_date`.
411 :see: https://tools.ietf.org/html/rfc7231#section-7.1.1.1
412
413.. js:function:: core.imf_date(date)
414
415 **context**: body, init, task, action
416
Bertrand Jacquin874a35c2018-09-10 21:26:07 +0100417 This function take a string representing IMF date, and returns an integer
Thierry FOURNIERa78f0372016-12-14 19:04:41 +0100418 containing the corresponding date with a epoch format.
419
420 :param string date: a date IMF formatted
421 :returns: integer containing epoch date
422 :see: https://tools.ietf.org/html/rfc7231#section-7.1.1.1
423
424 The IMF format is like this:
425
426.. code-block:: text
427
428 Sun, 06 Nov 1994 08:49:37 GMT
429..
430
431.. js:function:: core.rfc850_date(date)
432
433 **context**: body, init, task, action
434
Bertrand Jacquin874a35c2018-09-10 21:26:07 +0100435 This function take a string representing RFC850 date, and returns an integer
Thierry FOURNIERa78f0372016-12-14 19:04:41 +0100436 containing the corresponding date with a epoch format.
437
438 :param string date: a date RFC859 formatted
439 :returns: integer containing epoch date
440 :see: https://tools.ietf.org/html/rfc7231#section-7.1.1.1
441
442 The RFC850 format is like this:
443
444.. code-block:: text
445
446 Sunday, 06-Nov-94 08:49:37 GMT
447..
448
449.. js:function:: core.asctime_date(date)
450
451 **context**: body, init, task, action
452
Bertrand Jacquin874a35c2018-09-10 21:26:07 +0100453 This function take a string representing ASCTIME date, and returns an integer
Thierry FOURNIERa78f0372016-12-14 19:04:41 +0100454 containing the corresponding date with a epoch format.
455
456 :param string date: a date ASCTIME formatted
457 :returns: integer containing epoch date
458 :see: https://tools.ietf.org/html/rfc7231#section-7.1.1.1
459
460 The ASCTIME format is like this:
461
462.. code-block:: text
463
464 Sun Nov 6 08:49:37 1994
465..
466
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100467.. js:function:: core.msleep(milliseconds)
468
469 **context**: body, init, task, action
470
471 The `core.msleep()` stops the Lua execution between specified milliseconds.
472
473 :param integer milliseconds: the required milliseconds.
474
Thierry FOURNIERc5d11c62018-02-12 14:46:54 +0100475.. js:function:: core.register_action(name, actions, func [, nb_args])
Thierry FOURNIER8255a752015-09-23 21:03:35 +0200476
477 **context**: body
478
David Carlier61fdf8b2015-10-02 11:59:38 +0100479 Register a Lua function executed as action. All the registered action can be
Thierry FOURNIER8255a752015-09-23 21:03:35 +0200480 used in HAProxy with the prefix "lua.". An action gets a TXN object class as
481 input.
482
483 :param string name: is the name of the converter.
Willy Tarreau61add3c2015-09-28 15:39:10 +0200484 :param table actions: is a table of string describing the HAProxy actions who
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +0200485 want to register to. The expected actions are 'tcp-req', 'tcp-res', 'http-req'
486 or 'http-res'.
Aurelien DARRAGON53901f42022-10-13 19:49:42 +0200487 :param function func: is the Lua function called to work as converter.
Thierry FOURNIERc5d11c62018-02-12 14:46:54 +0100488 :param integer nb_args: is the expected number of argument for the action.
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +0200489 By default the value is 0.
Thierry FOURNIER8255a752015-09-23 21:03:35 +0200490
491 The prototype of the Lua function used as argument is:
492
493.. code-block:: lua
494
Thierry FOURNIERc5d11c62018-02-12 14:46:54 +0100495 function(txn [, arg1 [, arg2]])
Thierry FOURNIER8255a752015-09-23 21:03:35 +0200496..
497
Thierry FOURNIERdc595002015-12-21 11:13:52 +0100498 * **txn** (:ref:`txn_class`): this is a TXN object used for manipulating the
Thierry FOURNIER8255a752015-09-23 21:03:35 +0200499 current request or TCP stream.
500
Bertrand Jacquin874a35c2018-09-10 21:26:07 +0100501 * **argX**: this is argument provided through the HAProxy configuration file.
Thierry FOURNIERc5d11c62018-02-12 14:46:54 +0100502
Bertrand Jacquin874a35c2018-09-10 21:26:07 +0100503 Here, an example of action registration. The action just send an 'Hello world'
Thierry FOURNIER8255a752015-09-23 21:03:35 +0200504 in the logs.
505
506.. code-block:: lua
507
508 core.register_action("hello-world", { "tcp-req", "http-req" }, function(txn)
509 txn:Info("Hello world")
510 end)
511..
512
Willy Tarreau714f3452021-05-09 06:47:26 +0200513 This example code is used in HAProxy configuration like this:
Thierry FOURNIER8255a752015-09-23 21:03:35 +0200514
515::
516
517 frontend tcp_frt
518 mode tcp
519 tcp-request content lua.hello-world
520
521 frontend http_frt
522 mode http
523 http-request lua.hello-world
Aurelien DARRAGONd5c80d72023-03-13 19:36:13 +0100524
Thierry FOURNIERc5d11c62018-02-12 14:46:54 +0100525..
526
Bertrand Jacquin874a35c2018-09-10 21:26:07 +0100527 A second example using arguments
Thierry FOURNIERc5d11c62018-02-12 14:46:54 +0100528
529.. code-block:: lua
530
531 function hello_world(txn, arg)
532 txn:Info("Hello world for " .. arg)
533 end
534 core.register_action("hello-world", { "tcp-req", "http-req" }, hello_world, 2)
Aurelien DARRAGONd5c80d72023-03-13 19:36:13 +0100535
Thierry FOURNIERc5d11c62018-02-12 14:46:54 +0100536..
Thierry FOURNIER8255a752015-09-23 21:03:35 +0200537
Willy Tarreau714f3452021-05-09 06:47:26 +0200538 This example code is used in HAProxy configuration like this:
Thierry FOURNIERc5d11c62018-02-12 14:46:54 +0100539
540::
541
542 frontend tcp_frt
543 mode tcp
544 tcp-request content lua.hello-world everybody
Aurelien DARRAGONd5c80d72023-03-13 19:36:13 +0100545
Thierry FOURNIERc5d11c62018-02-12 14:46:54 +0100546..
Aurelien DARRAGON53901f42022-10-13 19:49:42 +0200547
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100548.. js:function:: core.register_converters(name, func)
549
550 **context**: body
551
David Carlier61fdf8b2015-10-02 11:59:38 +0100552 Register a Lua function executed as converter. All the registered converters
Aurelien DARRAGON53901f42022-10-13 19:49:42 +0200553 can be used in HAProxy with the prefix "lua.". A converter gets a string as
554 input and returns a string as output. The registered function can take up to 9
555 values as parameter. All the values are strings.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100556
557 :param string name: is the name of the converter.
558 :param function func: is the Lua function called to work as converter.
559
560 The prototype of the Lua function used as argument is:
561
562.. code-block:: lua
563
564 function(str, [p1 [, p2 [, ... [, p5]]]])
565..
566
567 * **str** (*string*): this is the input value automatically converted in
568 string.
569 * **p1** .. **p5** (*string*): this is a list of string arguments declared in
Bertrand Jacquin874a35c2018-09-10 21:26:07 +0100570 the HAProxy configuration file. The number of arguments doesn't exceed 5.
Aurelien DARRAGON53901f42022-10-13 19:49:42 +0200571 The order and the nature of these is conventionally chosen by the
Bertrand Jacquin874a35c2018-09-10 21:26:07 +0100572 developer.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100573
574.. js:function:: core.register_fetches(name, func)
575
576 **context**: body
577
David Carlier61fdf8b2015-10-02 11:59:38 +0100578 Register a Lua function executed as sample fetch. All the registered sample
Bertrand Jacquin874a35c2018-09-10 21:26:07 +0100579 fetch can be used in HAProxy with the prefix "lua.". A Lua sample fetch
Aurelien DARRAGON53901f42022-10-13 19:49:42 +0200580 returns a string as output. The registered function can take up to 9 values as
581 parameter. All the values are strings.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100582
Aurelien DARRAGON53901f42022-10-13 19:49:42 +0200583 :param string name: is the name of the sample fetch.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100584 :param function func: is the Lua function called to work as sample fetch.
585
586 The prototype of the Lua function used as argument is:
587
588.. code-block:: lua
589
590 string function(txn, [p1 [, p2 [, ... [, p5]]]])
591..
592
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +0200593 * **txn** (:ref:`txn_class`): this is the txn object associated with the
594 current request.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100595 * **p1** .. **p5** (*string*): this is a list of string arguments declared in
Bertrand Jacquin874a35c2018-09-10 21:26:07 +0100596 the HAProxy configuration file. The number of arguments doesn't exceed 5.
Aurelien DARRAGON53901f42022-10-13 19:49:42 +0200597 The order and the nature of these is conventionally chosen by the
Bertrand Jacquin874a35c2018-09-10 21:26:07 +0100598 developer.
599 * **Returns**: A string containing some data, or nil if the value cannot be
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100600 returned now.
601
602 lua example code:
603
604.. code-block:: lua
605
606 core.register_fetches("hello", function(txn)
607 return "hello"
608 end)
609..
610
611 HAProxy example configuration:
612
613::
614
615 frontend example
616 http-request redirect location /%[lua.hello]
617
Christopher Faulet5a2c6612021-08-15 20:35:25 +0200618.. js:function:: core.register_filter(name, Flt, func)
619
620 **context**: body
621
622 Register a Lua function used to declare a filter. All the registered filters
623 can by used in HAProxy with the prefix "lua.".
624
625 :param string name: is the name of the filter.
626 :param table Flt: is a Lua class containing the filter definition (id, flags,
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +0200627 callbacks).
Christopher Faulet5a2c6612021-08-15 20:35:25 +0200628 :param function func: is the Lua function called to create the Lua filter.
629
630 The prototype of the Lua function used as argument is:
631
632.. code-block:: lua
633
634 function(flt, args)
635..
636
637 * **flt** : Is a filter object based on the class provided in
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +0200638 :js:func:`core.register_filter()` function.
Christopher Faulet5a2c6612021-08-15 20:35:25 +0200639
640 * **args**: Is a table of strings containing all arguments provided through
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +0200641 the HAProxy configuration file, on the filter line.
Christopher Faulet5a2c6612021-08-15 20:35:25 +0200642
643 It must return the filter to use or nil to ignore it. Here, an example of
644 filter registration.
645
646.. code-block:: lua
647
648 core.register_filter("my-filter", MyFilter, function(flt, args)
649 flt.args = args -- Save arguments
650 return flt
651 end)
652..
653
654 This example code is used in HAProxy configuration like this:
655
656::
657
658 frontend http
659 mode http
660 filter lua.my-filter arg1 arg2 arg3
Aurelien DARRAGONd5c80d72023-03-13 19:36:13 +0100661
Christopher Faulet5a2c6612021-08-15 20:35:25 +0200662..
663
664 :see: :js:class:`Filter`
665
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +0200666.. js:function:: core.register_service(name, mode, func)
667
668 **context**: body
669
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +0200670 Register a Lua function executed as a service. All the registered services
671 can be used in HAProxy with the prefix "lua.". A service gets an object class
672 as input according with the required mode.
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +0200673
Aurelien DARRAGON53901f42022-10-13 19:49:42 +0200674 :param string name: is the name of the service.
Willy Tarreau61add3c2015-09-28 15:39:10 +0200675 :param string mode: is string describing the required mode. Only 'tcp' or
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +0200676 'http' are allowed.
Aurelien DARRAGON53901f42022-10-13 19:49:42 +0200677 :param function func: is the Lua function called to work as service.
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +0200678
679 The prototype of the Lua function used as argument is:
680
681.. code-block:: lua
682
683 function(applet)
684..
685
Thierry FOURNIERdc595002015-12-21 11:13:52 +0100686 * **applet** *applet* will be a :ref:`applettcp_class` or a
687 :ref:`applethttp_class`. It depends the type of registered applet. An applet
688 registered with the 'http' value for the *mode* parameter will gets a
689 :ref:`applethttp_class`. If the *mode* value is 'tcp', the applet will gets
690 a :ref:`applettcp_class`.
691
Christopher Faulet1e9b1b62021-08-11 10:14:30 +0200692 .. warning::
693 Applets of type 'http' cannot be called from 'tcp-*' rulesets. Only the
694 'http-*' rulesets are authorized, this means that is not possible to call
Aurelien DARRAGON53901f42022-10-13 19:49:42 +0200695 a HTTP applet from a proxy in tcp mode. Applets of type 'tcp' can be
Christopher Faulet1e9b1b62021-08-11 10:14:30 +0200696 called from anywhere.
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +0200697
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +0200698 Here, an example of service registration. The service just send an
699 'Hello world' as an http response.
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +0200700
701.. code-block:: lua
702
Pieter Baauw4d7f7662015-11-08 16:38:08 +0100703 core.register_service("hello-world", "http", function(applet)
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +0200704 local response = "Hello World !"
705 applet:set_status(200)
Pieter Baauw2dcb9bc2015-10-01 22:47:12 +0200706 applet:add_header("content-length", string.len(response))
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +0200707 applet:add_header("content-type", "text/plain")
Pieter Baauw2dcb9bc2015-10-01 22:47:12 +0200708 applet:start_response()
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +0200709 applet:send(response)
710 end)
711..
712
Willy Tarreau714f3452021-05-09 06:47:26 +0200713 This example code is used in HAProxy configuration like this:
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +0200714
715::
716
717 frontend example
718 http-request use-service lua.hello-world
719
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100720.. js:function:: core.register_init(func)
721
722 **context**: body
723
724 Register a function executed after the configuration parsing. This is useful
725 to check any parameters.
726
Pieter Baauw4d7f7662015-11-08 16:38:08 +0100727 :param function func: is the Lua function called to work as initializer.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100728
729 The prototype of the Lua function used as argument is:
730
731.. code-block:: lua
732
733 function()
734..
735
736 It takes no input, and no output is expected.
737
Aurelien DARRAGONb8038992023-03-09 16:48:30 +0100738.. js:function:: core.register_task(func[, arg1[, arg2[, ...[, arg4]]]])
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100739
Aurelien DARRAGONc84899c2023-02-20 18:18:59 +0100740 **context**: body, init, task, action, sample-fetch, converter, event
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100741
742 Register and start independent task. The task is started when the HAProxy
743 main scheduler starts. For example this type of tasks can be executed to
Thierry FOURNIER2e4893c2015-03-18 13:37:27 +0100744 perform complex health checks.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100745
Aurelien DARRAGONb8038992023-03-09 16:48:30 +0100746 :param function func: is the Lua function called to work as an async task.
747
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +0200748 Up to 4 optional arguments (all types supported) may be passed to the
749 function. (They will be passed as-is to the task function)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100750
751 The prototype of the Lua function used as argument is:
752
753.. code-block:: lua
754
Aurelien DARRAGONb8038992023-03-09 16:48:30 +0100755 function([arg1[, arg2[, ...[, arg4]]]])
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100756..
757
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +0200758 It takes up to 4 optional arguments (provided when registering), and no
759 output is expected.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100760
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +0100761.. js:function:: core.register_cli([path], usage, func)
762
763 **context**: body
764
Aurelien DARRAGON53901f42022-10-13 19:49:42 +0200765 Register a custom cli that will be available from haproxy stats socket.
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +0100766
767 :param array path: is the sequence of word for which the cli execute the Lua
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +0200768 binding.
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +0100769 :param string usage: is the usage message displayed in the help.
770 :param function func: is the Lua function called to handle the CLI commands.
771
772 The prototype of the Lua function used as argument is:
773
774.. code-block:: lua
775
776 function(AppletTCP, [arg1, [arg2, [...]]])
777..
778
779 I/O are managed with the :ref:`applettcp_class` object. Args are given as
Bertrand Jacquin874a35c2018-09-10 21:26:07 +0100780 parameter. The args embed the registered path. If the path is declared like
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +0100781 this:
782
783.. code-block:: lua
784
785 core.register_cli({"show", "ssl", "stats"}, "Display SSL stats..", function(applet, arg1, arg2, arg3, arg4, arg5)
786 end)
787..
788
789 And we execute this in the prompt:
790
791.. code-block:: text
792
793 > prompt
794 > show ssl stats all
795..
796
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +0200797 Then, arg1, arg2 and arg3 will contains respectively "show", "ssl" and
798 "stats".
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +0100799 arg4 will contain "all". arg5 contains nil.
800
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100801.. js:function:: core.set_nice(nice)
802
803 **context**: task, action, sample-fetch, converter
804
805 Change the nice of the current task or current session.
Thierry FOURNIER2e4893c2015-03-18 13:37:27 +0100806
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100807 :param integer nice: the nice value, it must be between -1024 and 1024.
808
809.. js:function:: core.set_map(filename, key, value)
810
811 **context**: init, task, action, sample-fetch, converter
812
Bertrand Jacquin874a35c2018-09-10 21:26:07 +0100813 Set the value *value* associated to the key *key* in the map referenced by
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100814 *filename*.
815
Thierry FOURNIERdc595002015-12-21 11:13:52 +0100816 :param string filename: the Map reference
817 :param string key: the key to set or replace
818 :param string value: the associated value
819
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100820.. js:function:: core.sleep(int seconds)
821
822 **context**: body, init, task, action
823
824 The `core.sleep()` functions stop the Lua execution between specified seconds.
825
826 :param integer seconds: the required seconds.
827
828.. js:function:: core.tcp()
829
830 **context**: init, task, action
831
832 This function returns a new object of a *socket* class.
833
Thierry FOURNIERdc595002015-12-21 11:13:52 +0100834 :returns: A :ref:`socket_class` object.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100835
William Lallemand00a15022021-11-19 16:02:44 +0100836.. js:function:: core.httpclient()
837
838 **context**: init, task, action
839
840 This function returns a new object of a *httpclient* class.
841
842 :returns: A :ref:`httpclient_class` object.
843
Thierry Fournier1de16592016-01-27 09:49:07 +0100844.. js:function:: core.concat()
845
846 **context**: body, init, task, action, sample-fetch, converter
847
Bertrand Jacquin874a35c2018-09-10 21:26:07 +0100848 This function returns a new concat object.
Thierry Fournier1de16592016-01-27 09:49:07 +0100849
850 :returns: A :ref:`concat_class` object.
851
Thierry FOURNIER0a99b892015-08-26 00:14:17 +0200852.. js:function:: core.done(data)
853
854 **context**: body, init, task, action, sample-fetch, converter
855
856 :param any data: Return some data for the caller. It is useful with
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +0200857 sample-fetches and sample-converters.
Thierry FOURNIER0a99b892015-08-26 00:14:17 +0200858
859 Immediately stops the current Lua execution and returns to the caller which
860 may be a sample fetch, a converter or an action and returns the specified
Thierry Fournier4234dbd2020-11-28 13:18:23 +0100861 value (ignored for actions and init). It is used when the LUA process finishes
862 its work and wants to give back the control to HAProxy without executing the
Thierry FOURNIER0a99b892015-08-26 00:14:17 +0200863 remaining code. It can be seen as a multi-level "return".
864
Thierry FOURNIER486f5a02015-03-16 15:13:03 +0100865.. js:function:: core.yield()
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100866
867 **context**: task, action, sample-fetch, converter
868
869 Give back the hand at the HAProxy scheduler. It is used when the LUA
870 processing consumes a lot of processing time.
871
Thierry FOURNIER / OZON.IO62fec752016-11-10 20:38:11 +0100872.. js:function:: core.parse_addr(address)
873
874 **context**: body, init, task, action, sample-fetch, converter
875
876 :param network: is a string describing an ipv4 or ipv6 address and optionally
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +0200877 its network length, like this: "127.0.0.1/8" or "aaaa::1234/32".
Thierry FOURNIER / OZON.IO62fec752016-11-10 20:38:11 +0100878 :returns: a userdata containing network or nil if an error occurs.
879
Bertrand Jacquin874a35c2018-09-10 21:26:07 +0100880 Parse ipv4 or ipv6 addresses and its facultative associated network.
Thierry FOURNIER / OZON.IO62fec752016-11-10 20:38:11 +0100881
882.. js:function:: core.match_addr(addr1, addr2)
883
884 **context**: body, init, task, action, sample-fetch, converter
885
886 :param addr1: is an address created with "core.parse_addr".
887 :param addr2: is an address created with "core.parse_addr".
Bertrand Jacquin874a35c2018-09-10 21:26:07 +0100888 :returns: boolean, true if the network of the addresses match, else returns
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +0200889 false.
Thierry FOURNIER / OZON.IO62fec752016-11-10 20:38:11 +0100890
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +0200891 Match two networks. For example "127.0.0.1/32" matches "127.0.0.0/8". The
892 order of network is not important.
Thierry FOURNIER / OZON.IO62fec752016-11-10 20:38:11 +0100893
Thierry FOURNIER / OZON.IO8a1027a2016-11-24 20:48:38 +0100894.. js:function:: core.tokenize(str, separators [, noblank])
895
896 **context**: body, init, task, action, sample-fetch, converter
897
898 This function is useful for tokenizing an entry, or splitting some messages.
899 :param string str: The string which will be split.
900 :param string separators: A string containing a list of separators.
901 :param boolean noblank: Ignore empty entries.
902 :returns: an array of string.
903
904 For example:
905
906.. code-block:: lua
907
908 local array = core.tokenize("This function is useful, for tokenizing an entry.", "., ", true)
909 print_r(array)
910..
911
912 Returns this array:
913
914.. code-block:: text
915
916 (table) table: 0x21c01e0 [
917 1: (string) "This"
918 2: (string) "function"
919 3: (string) "is"
920 4: (string) "useful"
921 5: (string) "for"
922 6: (string) "tokenizing"
923 7: (string) "an"
924 8: (string) "entry"
925 ]
926..
927
Aurelien DARRAGONc84899c2023-02-20 18:18:59 +0100928.. js:function:: core.event_sub(event_types, func)
929
930 **context**: body, init, task, action, sample-fetch, converter
931
932 Register a function that will be called on specific system events.
933
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +0200934 :param array event_types: array of string containing the event types you want
935 to subscribe to
936 :param function func: is the Lua function called when one of the subscribed
937 events occur.
Aurelien DARRAGONc84899c2023-02-20 18:18:59 +0100938 :returns: A :ref:`event_sub_class` object.
Aurelien DARRAGON223770d2023-03-10 15:34:35 +0100939 :see: :js:func:`Server.event_sub()`.
Aurelien DARRAGONc84899c2023-02-20 18:18:59 +0100940
941 List of available event types :
942
943 **SERVER** Family:
944
945 * **SERVER_ADD**: when a server is added
946 * **SERVER_DEL**: when a server is removed
947 * **SERVER_DOWN**: when a server state goes from UP to DOWN
948 * **SERVER_UP**: when a server state goes from DOWN to UP
Aurelien DARRAGONc99f3ad2023-04-12 15:47:16 +0200949 * **SERVER_STATE**: when a server state changes
Aurelien DARRAGON948dd3d2023-04-26 11:27:09 +0200950 * **SERVER_ADMIN**: when a server administrative state changes
Aurelien DARRAGON0bd53b22023-03-30 15:53:33 +0200951 * **SERVER_CHECK**: when a server's check status change is reported.
952 Be careful when subscribing to this type since many events might be
953 generated.
Aurelien DARRAGONc84899c2023-02-20 18:18:59 +0100954
955 .. Note::
Aurelien DARRAGONc99f3ad2023-04-12 15:47:16 +0200956 Use **SERVER** in **event_types** to subscribe to all server events types
957 at once. Note that this should only be used for testing purposes since a
958 single event source could result in multiple events types being generated.
959 (e.g.: SERVER_STATE will always be generated for each SERVER_DOWN or
960 SERVER_UP)
Aurelien DARRAGONc84899c2023-02-20 18:18:59 +0100961
962 The prototype of the Lua function used as argument is:
963
964.. code-block:: lua
965
Aurelien DARRAGON096b3832023-04-20 11:32:46 +0200966 function(event, event_data, sub, when)
Aurelien DARRAGONc84899c2023-02-20 18:18:59 +0100967..
968
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +0200969 * **event** (*string*): the event type (one of the **event_types** specified
970 when subscribing)
971 * **event_data**: specific to each event family (For **SERVER** family,
972 a :ref:`server_event_class` object)
973 * **sub**: class to manage the subscription from within the event
974 (a :ref:`event_sub_class` object)
Aurelien DARRAGON096b3832023-04-20 11:32:46 +0200975 * **when**: timestamp corresponding to the date when the event was generated.
976 It is an integer representing the number of seconds elapsed since Epoch.
977 It may be provided as optional argument to `os.date()` lua function to
978 convert it to a string according to a given format string.
Aurelien DARRAGONc84899c2023-02-20 18:18:59 +0100979
980 .. Warning::
981 The callback function will only be scheduled on the very same thread that
982 performed the subscription.
983
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +0200984 Moreover, each thread treats events sequentially. It means that if you
985 have, let's say SERVER_UP followed by a SERVER_DOWN in a short timelapse,
986 then the cb function will first be called with SERVER_UP, and once it's
987 done handling the event, the cb function will be called again with
988 SERVER_DOWN.
Aurelien DARRAGONc84899c2023-02-20 18:18:59 +0100989
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +0200990 This is to ensure event consistency when it comes to logging / triggering
991 logic from lua.
Aurelien DARRAGONc84899c2023-02-20 18:18:59 +0100992
993 Your lua cb function may yield if needed, but you're pleased to process the
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +0200994 event as fast as possible to prevent the event queue from growing up,
995 depending on the event flow that is expected for the given subscription.
Aurelien DARRAGONc84899c2023-02-20 18:18:59 +0100996
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +0200997 To prevent abuses, if the event queue for the current subscription goes
998 over a certain amount of unconsumed events, the subscription will pause
999 itself automatically for as long as it takes for your handler to catch up.
1000 This would lead to events being missed, so an error will be reported in the
1001 logs to warn you about that.
1002 This is not something you want to let happen too often, it may indicate
1003 that you subscribed to an event that is occurring too frequently or/and
1004 that your callback function is too slow to keep up the pace and you should
1005 review it.
Aurelien DARRAGONc84899c2023-02-20 18:18:59 +01001006
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02001007 If you want to do some parallel processing because your callback functions
1008 are slow: you might want to create subtasks from lua using
1009 :js:func:`core.register_task()` from within your callback function to
1010 perform the heavy job in a dedicated task and allow remaining events to be
1011 processed more quickly.
Aurelien DARRAGONc84899c2023-02-20 18:18:59 +01001012
Thierry Fournierf61aa632016-02-19 20:56:00 +01001013.. _proxy_class:
1014
1015Proxy class
1016============
1017
1018.. js:class:: Proxy
1019
1020 This class provides a way for manipulating proxy and retrieving information
1021 like statistics.
1022
Thierry FOURNIER817e7592017-07-24 14:35:04 +02001023.. js:attribute:: Proxy.name
1024
1025 Contain the name of the proxy.
1026
Aurelien DARRAGONc4b24372023-03-02 12:00:06 +01001027 .. warning::
1028 This attribute is now deprecated and will eventually be removed.
1029 Please use :js:func:`Proxy.get_name()` function instead.
1030
Thierry Fournierb0467732022-10-07 12:07:24 +02001031.. js:function:: Proxy.get_name()
1032
1033 Returns the name of the proxy.
1034
Baptiste Assmann46c72552017-10-26 21:51:58 +02001035.. js:attribute:: Proxy.uuid
1036
1037 Contain the unique identifier of the proxy.
1038
Aurelien DARRAGONc4b24372023-03-02 12:00:06 +01001039 .. warning::
1040 This attribute is now deprecated and will eventually be removed.
1041 Please use :js:func:`Proxy.get_uuid()` function instead.
1042
Thierry Fournierb0467732022-10-07 12:07:24 +02001043.. js:function:: Proxy.get_uuid()
1044
1045 Returns the unique identifier of the proxy.
1046
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001047.. js:attribute:: Proxy.servers
1048
Patrick Hemmerc6a1d712018-05-01 21:30:41 -04001049 Contain a table with the attached servers. The table is indexed by server
1050 name, and each server entry is an object of type :ref:`server_class`.
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001051
Adis Nezirovic8878f8e2018-07-13 12:18:33 +02001052.. js:attribute:: Proxy.stktable
1053
1054 Contains a stick table object attached to the proxy.
1055
Thierry Fournierff480422016-02-25 08:36:46 +01001056.. js:attribute:: Proxy.listeners
1057
Patrick Hemmerc6a1d712018-05-01 21:30:41 -04001058 Contain a table with the attached listeners. The table is indexed by listener
1059 name, and each each listeners entry is an object of type
1060 :ref:`listener_class`.
Thierry Fournierff480422016-02-25 08:36:46 +01001061
Thierry Fournierf61aa632016-02-19 20:56:00 +01001062.. js:function:: Proxy.pause(px)
1063
1064 Pause the proxy. See the management socket documentation for more 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.resume(px)
1070
1071 Resume the proxy. See the management socket documentation for more
1072 information.
1073
1074 :param class_proxy px: A :ref:`proxy_class` which indicates the manipulated
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02001075 proxy.
Thierry Fournierf61aa632016-02-19 20:56:00 +01001076
1077.. js:function:: Proxy.stop(px)
1078
1079 Stop the proxy. See the management socket 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.shut_bcksess(px)
1085
1086 Kill the session attached to a backup server. See the management socket
1087 documentation for more information.
1088
1089 :param class_proxy px: A :ref:`proxy_class` which indicates the manipulated
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02001090 proxy.
Thierry Fournierf61aa632016-02-19 20:56:00 +01001091
1092.. js:function:: Proxy.get_cap(px)
1093
1094 Returns a string describing the capabilities of the 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 "frontend", "backend", "proxy" or "ruleset".
1099
1100.. js:function:: Proxy.get_mode(px)
1101
1102 Returns a string describing the mode of the current proxy.
1103
1104 :param class_proxy px: A :ref:`proxy_class` which indicates the manipulated
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02001105 proxy.
Thierry Fournierf61aa632016-02-19 20:56:00 +01001106 :returns: a string "tcp", "http", "health" or "unknown"
1107
Aurelien DARRAGONfc845532023-04-03 11:00:18 +02001108.. js:function:: Proxy.get_srv_act(px)
1109
1110 Returns the number of current active servers for the current proxy that are
1111 eligible for LB.
1112
1113 :param class_proxy px: A :ref:`proxy_class` which indicates the manipulated
1114 proxy.
1115 :returns: an integer
1116
1117.. js:function:: Proxy.get_srv_bck(px)
1118
1119 Returns the number backup servers for the current proxy that are eligible
1120 for LB.
1121
1122 :param class_proxy px: A :ref:`proxy_class` which indicates the manipulated
1123 proxy.
1124 :returns: an integer
1125
Thierry Fournierf61aa632016-02-19 20:56:00 +01001126.. js:function:: Proxy.get_stats(px)
1127
Bertrand Jacquin874a35c2018-09-10 21:26:07 +01001128 Returns a table containing the proxy statistics. The statistics returned are
Thierry Fournierf61aa632016-02-19 20:56:00 +01001129 not the same if the proxy is frontend or a backend.
1130
1131 :param class_proxy px: A :ref:`proxy_class` which indicates the manipulated
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02001132 proxy.
Patrick Hemmerc6a1d712018-05-01 21:30:41 -04001133 :returns: a key/value table containing stats
Thierry Fournierf61aa632016-02-19 20:56:00 +01001134
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001135.. _server_class:
1136
1137Server class
1138============
1139
Patrick Hemmerc6a1d712018-05-01 21:30:41 -04001140.. js:class:: Server
1141
1142 This class provides a way for manipulating servers and retrieving information.
1143
Patrick Hemmera62ae7e2018-04-29 14:23:48 -04001144.. js:attribute:: Server.name
1145
1146 Contain the name of the server.
1147
Aurelien DARRAGONc4b24372023-03-02 12:00:06 +01001148 .. warning::
1149 This attribute is now deprecated and will eventually be removed.
1150 Please use :js:func:`Server.get_name()` function instead.
1151
Thierry Fournierb0467732022-10-07 12:07:24 +02001152.. js:function:: Server.get_name(sv)
1153
1154 Returns the name of the server.
1155
Patrick Hemmera62ae7e2018-04-29 14:23:48 -04001156.. js:attribute:: Server.puid
1157
1158 Contain the proxy unique identifier of the server.
1159
Aurelien DARRAGONc4b24372023-03-02 12:00:06 +01001160 .. warning::
1161 This attribute is now deprecated and will eventually be removed.
1162 Please use :js:func:`Server.get_puid()` function instead.
1163
Thierry Fournierb0467732022-10-07 12:07:24 +02001164.. js:function:: Server.get_puid(sv)
1165
1166 Returns the proxy unique identifier of the server.
1167
Aurelien DARRAGON94ee6632023-03-10 15:11:27 +01001168.. js:function:: Server.get_rid(sv)
1169
1170 Returns the rid (revision ID) of the server.
1171 It is an unsigned integer that is set upon server creation. Value is derived
1172 from a global counter that starts at 0 and is incremented each time one or
1173 multiple server deletions are followed by a server addition (meaning that
1174 old name/id reuse could occur).
1175
1176 Combining server name/id with server rid yields a process-wide unique
1177 identifier.
1178
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001179.. js:function:: Server.is_draining(sv)
1180
Patrick Hemmerc6a1d712018-05-01 21:30:41 -04001181 Return true if the server is currently draining sticky connections.
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001182
1183 :param class_server sv: A :ref:`server_class` which indicates the manipulated
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02001184 server.
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001185 :returns: a boolean
1186
Aurelien DARRAGONc72051d2023-03-29 10:44:38 +02001187.. js:function:: Server.is_backup(sv)
1188
1189 Return true if the server is a backup server
1190
1191 :param class_server sv: A :ref:`server_class` which indicates the manipulated
1192 server.
1193 :returns: a boolean
1194
Aurelien DARRAGON7a03dee2023-03-29 10:49:30 +02001195.. js:function:: Server.is_dynamic(sv)
1196
1197 Return true if the server was instantiated at runtime (e.g.: from the cli)
1198
1199 :param class_server sv: A :ref:`server_class` which indicates the manipulated
1200 server.
1201 :returns: a boolean
1202
Aurelien DARRAGONfc759b42023-04-03 10:43:17 +02001203.. js:function:: Server.get_cur_sess(sv)
1204
1205 Return the number of currently active sessions on the server
1206
1207 :param class_server sv: A :ref:`server_class` which indicates the manipulated
1208 server.
1209 :returns: an integer
1210
1211.. js:function:: Server.get_pend_conn(sv)
1212
1213 Return the number of pending connections to the server
1214
1215 :param class_server sv: A :ref:`server_class` which indicates the manipulated
1216 server.
1217 :returns: an integer
1218
Patrick Hemmer32d539f2018-04-29 14:25:46 -04001219.. js:function:: Server.set_maxconn(sv, weight)
1220
1221 Dynamically change the maximum connections of the server. See the management
1222 socket documentation for more information about the format of the string.
1223
1224 :param class_server sv: A :ref:`server_class` which indicates the manipulated
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02001225 server.
Patrick Hemmer32d539f2018-04-29 14:25:46 -04001226 :param string maxconn: A string describing the server maximum connections.
1227
1228.. js:function:: Server.get_maxconn(sv, weight)
1229
1230 This function returns an integer representing the server maximum connections.
1231
1232 :param class_server sv: A :ref:`server_class` which indicates the manipulated
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02001233 server.
Patrick Hemmer32d539f2018-04-29 14:25:46 -04001234 :returns: an integer.
1235
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001236.. js:function:: Server.set_weight(sv, weight)
1237
Patrick Hemmerc6a1d712018-05-01 21:30:41 -04001238 Dynamically change the weight of the server. See the management socket
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001239 documentation for more information about the format of the string.
1240
1241 :param class_server sv: A :ref:`server_class` which indicates the manipulated
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02001242 server.
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001243 :param string weight: A string describing the server weight.
1244
1245.. js:function:: Server.get_weight(sv)
1246
Patrick Hemmerc6a1d712018-05-01 21:30:41 -04001247 This function returns an integer representing the server weight.
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001248
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 :returns: an integer.
1252
Joseph C. Sible49bbf522020-05-04 22:20:32 -04001253.. js:function:: Server.set_addr(sv, addr[, port])
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001254
Patrick Hemmerc6a1d712018-05-01 21:30:41 -04001255 Dynamically change the address of the server. See the management socket
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001256 documentation for more information about the format of the string.
1257
1258 :param class_server sv: A :ref:`server_class` which indicates the manipulated
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02001259 server.
Patrick Hemmerc6a1d712018-05-01 21:30:41 -04001260 :param string addr: A string describing the server address.
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001261
1262.. js:function:: Server.get_addr(sv)
1263
Patrick Hemmerc6a1d712018-05-01 21:30:41 -04001264 Returns a string describing the address of the server.
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001265
1266 :param class_server sv: A :ref:`server_class` which indicates the manipulated
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02001267 server.
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001268 :returns: A string
1269
1270.. js:function:: Server.get_stats(sv)
1271
1272 Returns server statistics.
1273
1274 :param class_server sv: A :ref:`server_class` which indicates the manipulated
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02001275 server.
Patrick Hemmerc6a1d712018-05-01 21:30:41 -04001276 :returns: a key/value table containing stats
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001277
Aurelien DARRAGON3889efa2023-04-03 14:00:58 +02001278.. js:function:: Server.get_proxy(sv)
1279
1280 Returns the parent proxy to which the server belongs.
1281
1282 :param class_server sv: A :ref:`server_class` which indicates the manipulated
1283 server.
1284 :returns: a :ref:`proxy_class` or nil if not available
1285
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001286.. js:function:: Server.shut_sess(sv)
1287
1288 Shutdown all the sessions attached to the server. See the management socket
1289 documentation for more information about this function.
1290
1291 :param class_server sv: A :ref:`server_class` which indicates the manipulated
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02001292 server.
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001293
1294.. js:function:: Server.set_drain(sv)
1295
1296 Drain sticky sessions. See the management socket documentation for more
1297 information about this function.
1298
1299 :param class_server sv: A :ref:`server_class` which indicates the manipulated
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02001300 server.
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001301
1302.. js:function:: Server.set_maint(sv)
1303
1304 Set maintenance mode. See the management socket documentation for more
1305 information about this function.
1306
1307 :param class_server sv: A :ref:`server_class` which indicates the manipulated
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02001308 server.
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001309
1310.. js:function:: Server.set_ready(sv)
1311
1312 Set normal mode. See the management socket documentation for more information
1313 about this function.
1314
1315 :param class_server sv: A :ref:`server_class` which indicates the manipulated
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02001316 server.
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001317
1318.. js:function:: Server.check_enable(sv)
1319
1320 Enable health checks. See the management socket documentation for more
1321 information about this function.
1322
1323 :param class_server sv: A :ref:`server_class` which indicates the manipulated
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02001324 server.
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001325
1326.. js:function:: Server.check_disable(sv)
1327
1328 Disable health checks. See the management socket documentation for more
1329 information about this function.
1330
1331 :param class_server sv: A :ref:`server_class` which indicates the manipulated
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02001332 server.
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001333
1334.. js:function:: Server.check_force_up(sv)
1335
1336 Force health-check up. See the management socket documentation for more
1337 information about this function.
1338
1339 :param class_server sv: A :ref:`server_class` which indicates the manipulated
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02001340 server.
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001341
1342.. js:function:: Server.check_force_nolb(sv)
1343
1344 Force health-check nolb mode. See the management socket documentation for more
1345 information about this function.
1346
1347 :param class_server sv: A :ref:`server_class` which indicates the manipulated
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02001348 server.
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001349
1350.. js:function:: Server.check_force_down(sv)
1351
1352 Force health-check down. See the management socket documentation for more
1353 information about this function.
1354
1355 :param class_server sv: A :ref:`server_class` which indicates the manipulated
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02001356 server.
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001357
1358.. js:function:: Server.agent_enable(sv)
1359
1360 Enable agent check. See the management socket documentation for more
1361 information about this function.
1362
1363 :param class_server sv: A :ref:`server_class` which indicates the manipulated
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02001364 server.
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001365
1366.. js:function:: Server.agent_disable(sv)
1367
1368 Disable agent check. See the management socket documentation for more
1369 information about this function.
1370
1371 :param class_server sv: A :ref:`server_class` which indicates the manipulated
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02001372 server.
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001373
1374.. js:function:: Server.agent_force_up(sv)
1375
1376 Force agent check up. See the management socket documentation for more
1377 information about this function.
1378
1379 :param class_server sv: A :ref:`server_class` which indicates the manipulated
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02001380 server.
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001381
1382.. js:function:: Server.agent_force_down(sv)
1383
1384 Force agent check down. See the management socket documentation for more
1385 information about this function.
1386
1387 :param class_server sv: A :ref:`server_class` which indicates the manipulated
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02001388 server.
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001389
Aurelien DARRAGON406511a2023-03-29 11:30:36 +02001390.. js:function:: Server.tracking(sv)
1391
1392 Check if the current server is tracking another server.
1393
1394 :param class_server sv: A :ref:`server_class` which indicates the manipulated
1395 server.
1396 :returns: A :ref:`server_class` which indicates the tracked server or nil if
1397 the server doesn't track another one.
1398
Aurelien DARRAGON4be36a12023-03-29 14:02:39 +02001399.. js:function:: Server.get_trackers(sv)
1400
1401 Check if the current server is being tracked by other servers.
1402
1403 :param class_server sv: A :ref:`server_class` which indicates the manipulated
1404 server.
1405 :returns: An array of :ref:`server_class` which indicates the tracking
1406 servers (might be empty)
1407
Aurelien DARRAGON223770d2023-03-10 15:34:35 +01001408.. js:function:: Server.event_sub(sv, event_types, func)
1409
1410 Register a function that will be called on specific server events.
1411 It works exactly like :js:func:`core.event_sub()` except that the subscription
1412 will be performed within the server dedicated subscription list instead of the
1413 global one.
1414 (Your callback function will only be called for server events affecting sv)
1415
1416 See :js:func:`core.event_sub()` for function usage.
1417
1418 A key advantage to using :js:func:`Server.event_sub()` over
1419 :js:func:`core.event_sub()` for servers is that :js:func:`Server.event_sub()`
1420 allows you to be notified for servers events of a single server only.
1421 It removes the needs for extra filtering in your callback function if you only
1422 care about a single server, and also prevents useless wakeups.
1423
1424 For instance, if you want to be notified for UP/DOWN events on a given set of
Ilya Shipitsinccf80122023-04-22 20:20:39 +02001425 servers, it is recommended to perform multiple per-server subscriptions since
Aurelien DARRAGON223770d2023-03-10 15:34:35 +01001426 it will be more efficient that doing a single global subscription that will
1427 filter the received events.
1428 Unless you really want to be notified for servers events of ALL servers of
1429 course, which could make sense given you setup but should be avoided if you
1430 have an important number of servers as it will add a significant load on your
1431 haproxy process in case of multiple servers state change in a short amount of
1432 time.
1433
1434 .. Note::
1435 You may also combine :js:func:`core.event_sub()` with
1436 :js:func:`Server.event_sub()`.
1437
1438 Also, don't forget that you can use :js:func:`core.register_task()` from
1439 your callback function if needed. (ie: parallel work)
1440
1441 Here is a working example combining :js:func:`core.event_sub()` with
1442 :js:func:`Server.event_sub()` and :js:func:`core.register_task()`
1443 (This only serves as a demo, this is not necessarily useful to do so)
1444
1445.. code-block:: lua
1446
1447 core.event_sub({"SERVER_ADD"}, function(event, data, sub)
1448 -- in the global event handler
1449 if data["reference"] ~= nil then
1450 print("Tracking new server: ", data["name"])
1451 data["reference"]:event_sub({"SERVER_UP", "SERVER_DOWN"}, function(event, data, sub)
1452 -- in the per-server event handler
1453 if data["reference"] ~= nil then
1454 core.register_task(function(server)
1455 -- subtask to perform some async work (e.g.: HTTP API calls, sending emails...)
1456 print("ASYNC: SERVER ", server:get_name(), " is ", event == "SERVER_UP" and "UP" or "DOWN")
1457 end, data["reference"])
1458 end
1459 end)
1460 end
1461 end)
1462
1463..
1464
1465 In this example, we will first track global server addition events.
1466 For each newly added server ("add server" on the cli), we will register a
1467 UP/DOWN server subscription.
1468 Then, the callback function will schedule the event handling in an async
1469 subtask which will receive the server reference as an argument.
1470
Thierry Fournierff480422016-02-25 08:36:46 +01001471.. _listener_class:
1472
1473Listener class
1474==============
1475
1476.. js:function:: Listener.get_stats(ls)
1477
1478 Returns server statistics.
1479
1480 :param class_listener ls: A :ref:`listener_class` which indicates the
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02001481 manipulated listener.
Patrick Hemmerc6a1d712018-05-01 21:30:41 -04001482 :returns: a key/value table containing stats
Thierry Fournierff480422016-02-25 08:36:46 +01001483
Aurelien DARRAGONc84899c2023-02-20 18:18:59 +01001484.. _event_sub_class:
1485
1486EventSub class
1487==============
1488
1489.. js:function:: EventSub.unsub()
1490
1491 End the subscription, the callback function will not be called again.
1492
1493.. _server_event_class:
1494
1495ServerEvent class
1496=================
1497
Aurelien DARRAGONc4ae8902023-04-17 17:24:48 +02001498.. js:class:: ServerEvent
1499
1500This class is provided with every **SERVER** events.
1501
1502See :js:func:`core.event_sub()` for more info.
1503
Aurelien DARRAGONc84899c2023-02-20 18:18:59 +01001504.. js:attribute:: ServerEvent.name
1505
1506 Contains the name of the server.
1507
1508.. js:attribute:: ServerEvent.puid
1509
1510 Contains the proxy-unique uid of the server
1511
1512.. js:attribute:: ServerEvent.rid
1513
1514 Contains the revision ID of the server
1515
1516.. js:attribute:: ServerEvent.proxy_name
1517
1518 Contains the name of the proxy to which the server belongs
1519
Aurelien DARRAGON55f84c72023-03-22 17:49:04 +01001520.. js:attribute:: ServerEvent.proxy_uuid
1521
1522 Contains the uuid of the proxy to which the server belongs
1523
Aurelien DARRAGONc84899c2023-02-20 18:18:59 +01001524.. js:attribute:: ServerEvent.reference
1525
1526 Reference to the live server (A :ref:`server_class`).
1527
1528 .. Warning::
1529 Not available if the server was removed in the meantime.
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02001530 (Will never be set for SERVER_DEL event since the server does not exist
1531 anymore)
Aurelien DARRAGONc84899c2023-02-20 18:18:59 +01001532
Aurelien DARRAGONc99f3ad2023-04-12 15:47:16 +02001533.. js:attribute:: ServerEvent.state
1534
1535 A :ref:`server_event_state_class`
1536
1537 .. Note::
1538 Only available for SERVER_STATE event
1539
Aurelien DARRAGON948dd3d2023-04-26 11:27:09 +02001540.. js:attribute:: ServerEvent.admin
1541
1542 A :ref:`server_event_admin_class`
1543
1544 .. Note::
1545 Only available for SERVER_ADMIN event
1546
Aurelien DARRAGON0bd53b22023-03-30 15:53:33 +02001547.. js:attribute:: ServerEvent.check
1548
1549 A :ref:`server_event_checkres_class`
1550
1551 .. Note::
1552 Only available for SERVER_CHECK event
1553
Aurelien DARRAGONc99f3ad2023-04-12 15:47:16 +02001554.. _server_event_checkres_class:
1555
1556ServerEventCheckRes class
1557=========================
1558
1559.. js:class:: ServerEventCheckRes
1560
1561This class describes the result of a server's check.
1562
1563.. js:attribute:: ServerEventCheckRes.result
1564
1565 Effective check result.
1566
1567 Check result is a string and will be set to one of the following values:
1568 - "FAILED": the check failed
1569 - "PASSED": the check succeeded
1570 - "CONDPASS": the check conditionally passed
1571
1572.. js:attribute:: ServerEventCheckRes.agent
1573
1574 Boolean set to true if the check is an agent check.
1575 Else it is a health check.
1576
1577.. js:attribute:: ServerEventCheckRes.duration
1578
1579 Check's duration in milliseconds
1580
1581.. js:attribute:: ServerEventCheckRes.reason
1582
1583 Check's status. An array containing three fields:
1584 - **short**: a string representing check status short name
1585 - **desc**: a string representing check status description
1586 - **code**: an integer, this extra information is provided for checks
1587 that went through the data analysis stage (>= layer 5)
1588
1589.. js:attribute:: ServerEventCheckRes.health
1590
1591 An array containing values about check's health (integers):
1592 - **cur**: current health counter:
1593 - 0 to (**rise** - 1) = BAD
1594 - **rise** to (**rise** + **fall** - 1) = GOOD
1595 - **rise**: server will be considered as operational after **rise**
1596 consecutive successful checks
1597 - **fall**: server will be considered as dead after **fall** consecutive
1598 unsuccessful checks
1599
1600.. _server_event_state_class:
1601
1602ServerEventState class
1603======================
1604
1605.. js:class:: ServerEventState
1606
1607This class contains additional info related to **SERVER_STATE** event.
1608
1609.. js:attribute:: ServerEventState.admin
1610
1611 Boolean set to true if the server state change is due to an administrative
1612 change. Else it is an operational change.
1613
1614.. js:attribute:: ServerEventState.check
1615
1616 A :ref:`server_event_checkres_class`, provided if the state change is
1617 due to a server check (must be an operational change).
1618
1619.. js:attribute:: ServerEventState.cause
1620
1621 Printable state change cause. Might be empty.
1622
1623.. js:attribute:: ServerEventState.new_state
1624
1625 New server state due to operational or admin change.
1626
1627 It is a string that can be any of the following values:
1628 - "STOPPED": The server is down
1629 - "STOPPING": The server is up but soft-stopping
1630 - "STARTING": The server is warming up
1631 - "RUNNING": The server is fully up
1632
1633.. js:attribute:: ServerEventState.old_state
1634
1635 Previous server state prior to the operational or admin change.
1636
1637 Can be any value described in **new_state**, but they should differ.
1638
1639.. js:attribute:: ServerEventState.requeued
1640
1641 Number of connections that were requeued due to the server state change.
1642
1643 For a server going DOWN: it is the number of pending server connections
1644 that are requeued to the backend (such connections will be redispatched
1645 to any server that is suitable according to the configured load balancing
1646 algorithm).
1647
1648 For a server doing UP: it is the number of pending connections on the
1649 backend that may be redispatched to the server according to the load
1650 balancing algorithm that is in use.
1651
Aurelien DARRAGON948dd3d2023-04-26 11:27:09 +02001652.. _server_event_admin_class:
1653
1654ServerEventAdmin class
1655======================
1656
1657.. js:class:: ServerEventAdmin
1658
1659This class contains additional info related to **SERVER_ADMIN** event.
1660
1661.. js:attribute:: ServerEventAdmin.cause
1662
1663 Printable admin state change cause. Might be empty.
1664
1665.. js:attribute:: ServerEventAdmin.new_admin
1666
1667 New server admin state due to the admin change.
1668
1669 It is an array of string containing a composition of following values:
1670 - "**MAINT**": server is in maintenance mode
1671 - "FMAINT": server is in forced maintenance mode (MAINT is also set)
1672 - "IMAINT": server is in inherited maintenance mode (MAINT is also set)
1673 - "RMAINT": server is in resolve maintenance mode (MAINT is also set)
1674 - "CMAINT": server is in config maintenance mode (MAINT is also set)
1675 - "**DRAIN**": server is in drain mode
1676 - "FDRAIN": server is in forced drain mode (DRAIN is also set)
1677 - "IDRAIN": server is in inherited drain mode (DRAIN is also set)
1678
1679.. js:attribute:: ServerEventAdmin.old_admin
1680
1681 Previous server admin state prior to the admin change.
1682
1683 Values are presented as in **new_admin**, but they should differ.
1684 (Comparing old and new helps to find out the change(s))
1685
1686.. js:attribute:: ServerEventAdmin.requeued
1687
1688 Same as :js:attr:`ServerEventState.requeued` but when the requeue is due to
1689 the server administrative state change.
1690
Thierry Fournier1de16592016-01-27 09:49:07 +01001691.. _concat_class:
1692
1693Concat class
1694============
1695
1696.. js:class:: Concat
1697
1698 This class provides a fast way for string concatenation. The way using native
1699 Lua concatenation like the code below is slow for some reasons.
1700
1701.. code-block:: lua
1702
1703 str = "string1"
1704 str = str .. ", string2"
1705 str = str .. ", string3"
1706..
1707
1708 For each concatenation, Lua:
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02001709 - allocates memory for the result,
1710 - catenates the two string copying the strings in the new memory block,
1711 - frees the old memory block containing the string which is no longer used.
1712
Thierry Fournier1de16592016-01-27 09:49:07 +01001713 This process does many memory move, allocation and free. In addition, the
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02001714 memory is not really freed, it is just marked as unused and waits for the
Thierry Fournier1de16592016-01-27 09:49:07 +01001715 garbage collector.
1716
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02001717 The Concat class provides an alternative way to concatenate strings. It uses
Thierry Fournier1de16592016-01-27 09:49:07 +01001718 the internal Lua mechanism (it does not allocate memory), but it doesn't copy
1719 the data more than once.
1720
1721 On my computer, the following loops spends 0.2s for the Concat method and
1722 18.5s for the pure Lua implementation. So, the Concat class is about 1000x
1723 faster than the embedded solution.
1724
1725.. code-block:: lua
1726
1727 for j = 1, 100 do
1728 c = core.concat()
1729 for i = 1, 20000 do
1730 c:add("#####")
1731 end
1732 end
1733..
1734
1735.. code-block:: lua
1736
1737 for j = 1, 100 do
1738 c = ""
1739 for i = 1, 20000 do
1740 c = c .. "#####"
1741 end
1742 end
1743..
1744
1745.. js:function:: Concat.add(concat, string)
1746
1747 This function adds a string to the current concatenated string.
1748
1749 :param class_concat concat: A :ref:`concat_class` which contains the currently
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02001750 built string.
Bertrand Jacquin874a35c2018-09-10 21:26:07 +01001751 :param string string: A new string to concatenate to the current built
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02001752 string.
Thierry Fournier1de16592016-01-27 09:49:07 +01001753
1754.. js:function:: Concat.dump(concat)
1755
Bertrand Jacquin874a35c2018-09-10 21:26:07 +01001756 This function returns the concatenated string.
Thierry Fournier1de16592016-01-27 09:49:07 +01001757
1758 :param class_concat concat: A :ref:`concat_class` which contains the currently
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02001759 built string.
Thierry Fournier1de16592016-01-27 09:49:07 +01001760 :returns: the concatenated string
1761
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001762.. _fetches_class:
1763
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001764Fetches class
1765=============
1766
1767.. js:class:: Fetches
1768
1769 This class contains a lot of internal HAProxy sample fetches. See the
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02001770 HAProxy "configuration.txt" documentation for more information.
1771 (chapters 7.3.2 to 7.3.6)
Thierry FOURNIER2e4893c2015-03-18 13:37:27 +01001772
Christopher Faulet1e9b1b62021-08-11 10:14:30 +02001773 .. warning::
1774 some sample fetches are not available in some context. These limitations
1775 are specified in this documentation when they're useful.
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001776
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01001777 :see: :js:attr:`TXN.f`
1778 :see: :js:attr:`TXN.sf`
Thierry FOURNIER2e4893c2015-03-18 13:37:27 +01001779
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02001780 Fetches are useful to:
Thierry FOURNIER2e4893c2015-03-18 13:37:27 +01001781
1782 * get system time,
1783 * get environment variable,
1784 * get random numbers,
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02001785 * know backend status like the number of users in queue or the number of
Thierry FOURNIER2e4893c2015-03-18 13:37:27 +01001786 connections established,
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02001787 * get client information like ip source or destination,
Thierry FOURNIER2e4893c2015-03-18 13:37:27 +01001788 * deal with stick tables,
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02001789 * fetch established SSL information,
1790 * fetch HTTP information like headers or method.
Thierry FOURNIER2e4893c2015-03-18 13:37:27 +01001791
1792.. code-block:: lua
1793
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001794 function action(txn)
1795 -- Get source IP
1796 local clientip = txn.f:src()
1797 end
Thierry FOURNIER2e4893c2015-03-18 13:37:27 +01001798..
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001799
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001800.. _converters_class:
1801
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001802Converters class
1803================
1804
1805.. js:class:: Converters
1806
1807 This class contains a lot of internal HAProxy sample converters. See the
Thierry FOURNIER2e4893c2015-03-18 13:37:27 +01001808 HAProxy documentation "configuration.txt" for more information about her
1809 usage. Its the chapter 7.3.1.
1810
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01001811 :see: :js:attr:`TXN.c`
1812 :see: :js:attr:`TXN.sc`
Thierry FOURNIER2e4893c2015-03-18 13:37:27 +01001813
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02001814 Converters provides stateful transformation. They are useful to:
Thierry FOURNIER2e4893c2015-03-18 13:37:27 +01001815
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02001816 * convert input to base64,
1817 * apply hash on input string (djb2, crc32, sdbm, wt6),
Thierry FOURNIER2e4893c2015-03-18 13:37:27 +01001818 * format date,
1819 * json escape,
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02001820 * extract preferred language comparing two lists,
Thierry FOURNIER2e4893c2015-03-18 13:37:27 +01001821 * turn to lower or upper chars,
1822 * deal with stick tables.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001823
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001824.. _channel_class:
1825
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001826Channel class
1827=============
1828
1829.. js:class:: Channel
1830
Christopher Faulet5a2c6612021-08-15 20:35:25 +02001831 **context**: action, sample-fetch, convert, filter
1832
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001833 HAProxy uses two buffers for the processing of the requests. The first one is
1834 used with the request data (from the client to the server) and the second is
1835 used for the response data (from the server to the client).
1836
1837 Each buffer contains two types of data. The first type is the incoming data
1838 waiting for a processing. The second part is the outgoing data already
1839 processed. Usually, the incoming data is processed, after it is tagged as
1840 outgoing data, and finally it is sent. The following functions provides tools
1841 for manipulating these data in a buffer.
1842
1843 The following diagram shows where the channel class function are applied.
1844
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001845 .. image:: _static/channel.png
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001846
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001847 .. warning::
1848 It is not possible to read from the response in request action, and it is
Boyang Li60cfe8b2022-05-10 18:11:00 +00001849 not possible to read from the request channel in response action.
Christopher Faulet09530392021-06-14 11:43:18 +02001850
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001851 .. warning::
1852 It is forbidden to alter the Channels buffer from HTTP contexts. So only
1853 :js:func:`Channel.input`, :js:func:`Channel.output`,
1854 :js:func:`Channel.may_recv`, :js:func:`Channel.is_full` and
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02001855 :js:func:`Channel.is_resp` can be called from a HTTP context.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001856
Christopher Faulet5a2c6612021-08-15 20:35:25 +02001857 All the functions provided by this class are available in the
1858 **sample-fetches**, **actions** and **filters** contexts. For **filters**,
1859 incoming data (offset and length) are relative to the filter. Some functions
Boyang Li60cfe8b2022-05-10 18:11:00 +00001860 may yield, but only for **actions**. Yield is not possible for
Christopher Faulet5a2c6612021-08-15 20:35:25 +02001861 **sample-fetches**, **converters** and **filters**.
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001862
1863.. js:function:: Channel.append(channel, string)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001864
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001865 This function copies the string **string** at the end of incoming data of the
1866 channel buffer. The function returns the copied length on success or -1 if
1867 data cannot be copied.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001868
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001869 Same that :js:func:`Channel.insert(channel, string, channel:input())`.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001870
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001871 :param class_channel channel: The manipulated Channel.
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02001872 :param string string: The data to copy at the end of incoming data.
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001873 :returns: an integer containing the amount of bytes copied or -1.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001874
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001875.. js:function:: Channel.data(channel [, offset [, length]])
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001876
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001877 This function returns **length** bytes of incoming data from the channel
1878 buffer, starting at the offset **offset**. The data are not removed from the
1879 buffer.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001880
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001881 By default, if no length is provided, all incoming data found, starting at the
1882 given offset, are returned. If **length** is set to -1, the function tries to
Christopher Faulet5a2c6612021-08-15 20:35:25 +02001883 retrieve a maximum of data and, if called by an action, it yields if
1884 necessary. It also waits for more data if the requested length exceeds the
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02001885 available amount of incoming data. Not providing an offset is the same as
Ilya Shipitsinff0f2782021-08-22 22:18:07 +05001886 setting it to 0. A positive offset is relative to the beginning of incoming
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02001887 data of the channel buffer while negative offset is relative to the end.
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001888
1889 If there is no incoming data and the channel can't receive more data, a 'nil'
1890 value is returned.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001891
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001892 :param class_channel channel: The manipulated Channel.
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001893 :param integer offset: *optional* The offset in incoming data to start to get
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02001894 data. 0 by default. May be negative to be relative to the end of incoming
1895 data.
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001896 :param integer length: *optional* The expected length of data to retrieve. All
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02001897 incoming data by default. May be set to -1 to get a maximum of data.
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001898 :returns: a string containing the data found or nil.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001899
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001900.. js:function:: Channel.forward(channel, length)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001901
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001902 This function forwards **length** bytes of data from the channel buffer. If
Christopher Faulet5a2c6612021-08-15 20:35:25 +02001903 the requested length exceeds the available amount of incoming data, and if
1904 called by an action, the function yields, waiting for more data to forward. It
1905 returns the amount of data forwarded.
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001906
1907 :param class_channel channel: The manipulated Channel.
1908 :param integer int: The amount of data to forward.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001909
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001910.. js:function:: Channel.input(channel)
1911
Christopher Faulet5a2c6612021-08-15 20:35:25 +02001912 This function returns the length of incoming data in the channel buffer. When
1913 called by a filter, this value is relative to the filter.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001914
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001915 :param class_channel channel: The manipulated Channel.
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001916 :returns: an integer containing the amount of available bytes.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001917
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001918.. js:function:: Channel.insert(channel, string [, offset])
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001919
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001920 This function copies the string **string** at the offset **offset** in
1921 incoming data of the channel buffer. The function returns the copied length on
1922 success or -1 if data cannot be copied.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001923
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001924 By default, if no offset is provided, the string is copied in front of
Ilya Shipitsinff0f2782021-08-22 22:18:07 +05001925 incoming data. A positive offset is relative to the beginning of incoming data
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001926 of the channel buffer while negative offset is relative to their end.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001927
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001928 :param class_channel channel: The manipulated Channel.
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02001929 :param string string: The data to copy into incoming data.
1930 :param integer offset: *optional* The offset in incoming data where to copy
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02001931 data. 0 by default. May be negative to be relative to the end of incoming
1932 data.
Pieter Baauw4d7f7662015-11-08 16:38:08 +01001933 :returns: an integer containing the amount of bytes copied or -1.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001934
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001935.. js:function:: Channel.is_full(channel)
1936
1937 This function returns true if the channel buffer is full.
1938
Christopher Faulet5a2c6612021-08-15 20:35:25 +02001939 :param class_channel channel: The manipulated Channel.
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001940 :returns: a boolean
1941
1942.. js:function:: Channel.is_resp(channel)
1943
1944 This function returns true if the channel is the response one.
1945
Christopher Faulet5a2c6612021-08-15 20:35:25 +02001946 :param class_channel channel: The manipulated Channel.
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001947 :returns: a boolean
1948
1949.. js:function:: Channel.line(channel [, offset [, length]])
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001950
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001951 This function parses **length** bytes of incoming data of the channel buffer,
1952 starting at offset **offset**, and returns the first line found, including the
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02001953 '\\n'. The data are not removed from the buffer. If no line is found, all
1954 data are returned.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001955
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001956 By default, if no length is provided, all incoming data, starting at the given
1957 offset, are evaluated. If **length** is set to -1, the function tries to
Christopher Faulet5a2c6612021-08-15 20:35:25 +02001958 retrieve a maximum of data and, if called by an action, yields if
1959 necessary. It also waits for more data if the requested length exceeds the
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02001960 available amount of incoming data. Not providing an offset is the same as
Ilya Shipitsinff0f2782021-08-22 22:18:07 +05001961 setting it to 0. A positive offset is relative to the beginning of incoming
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02001962 data of the channel buffer while negative offset is relative to the end.
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001963
1964 If there is no incoming data and the channel can't receive more data, a 'nil'
1965 value is returned.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001966
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001967 :param class_channel channel: The manipulated Channel.
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02001968 :param integer offset: *optional* The offset in incoming data to start to
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02001969 parse data. 0 by default. May be negative to be relative to the end of
1970 incoming data.
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001971 :param integer length: *optional* The length of data to parse. All incoming
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02001972 data by default. May be set to -1 to get a maximum of data.
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001973 :returns: a string containing the line found or nil.
1974
1975.. js:function:: Channel.may_recv(channel)
1976
1977 This function returns true if the channel may still receive data.
1978
Christopher Faulet5a2c6612021-08-15 20:35:25 +02001979 :param class_channel channel: The manipulated Channel.
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001980 :returns: a boolean
1981
1982.. js:function:: Channel.output(channel)
1983
Christopher Faulet5a2c6612021-08-15 20:35:25 +02001984 This function returns the length of outgoing data of the channel buffer. When
1985 called by a filter, this value is relative to the filter.
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001986
1987 :param class_channel channel: The manipulated Channel.
1988 :returns: an integer containing the amount of available bytes.
1989
1990.. js:function:: Channel.prepend(channel, string)
1991
1992 This function copies the string **string** in front of incoming data of the
1993 channel buffer. The function returns the copied length on success or -1 if
1994 data cannot be copied.
1995
1996 Same that :js:func:`Channel.insert(channel, string, 0)`.
1997
1998 :param class_channel channel: The manipulated Channel.
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02001999 :param string string: The data to copy in front of incoming data.
Pieter Baauw4d7f7662015-11-08 16:38:08 +01002000 :returns: an integer containing the amount of bytes copied or -1.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002001
Christopher Faulet6a79fc12021-08-06 16:02:36 +02002002.. js:function:: Channel.remove(channel [, offset [, length]])
2003
2004 This function removes **length** bytes of incoming data of the channel buffer,
2005 starting at offset **offset**. This function returns number of bytes removed
2006 on success.
2007
2008 By default, if no length is provided, all incoming data, starting at the given
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02002009 offset, are removed. Not providing an offset is the same as setting it
Ilya Shipitsinff0f2782021-08-22 22:18:07 +05002010 to 0. A positive offset is relative to the beginning of incoming data of the
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02002011 channel buffer while negative offset is relative to the end.
Christopher Faulet6a79fc12021-08-06 16:02:36 +02002012
2013 :param class_channel channel: The manipulated Channel.
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02002014 :param integer offset: *optional* The offset in incoming data where to start
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02002015 to remove data. 0 by default. May be negative to be relative to the end of
2016 incoming data.
Christopher Faulet6a79fc12021-08-06 16:02:36 +02002017 :param integer length: *optional* The length of data to remove. All incoming
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02002018 data by default.
Christopher Faulet6a79fc12021-08-06 16:02:36 +02002019 :returns: an integer containing the amount of bytes removed.
2020
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002021.. js:function:: Channel.send(channel, string)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002022
Christopher Faulet5a2c6612021-08-15 20:35:25 +02002023 This function requires immediate send of the string **string**. It means the
Ilya Shipitsinff0f2782021-08-22 22:18:07 +05002024 string is copied at the beginning of incoming data of the channel buffer and
Christopher Faulet5a2c6612021-08-15 20:35:25 +02002025 immediately forwarded. Unless if the connection is close, and if called by an
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02002026 action, this function yields to copy and forward all the string.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002027
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002028 :param class_channel channel: The manipulated Channel.
Christopher Faulet6a79fc12021-08-06 16:02:36 +02002029 :param string string: The data to send.
Pieter Baauw4d7f7662015-11-08 16:38:08 +01002030 :returns: an integer containing the amount of bytes copied or -1.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002031
Christopher Faulet6a79fc12021-08-06 16:02:36 +02002032.. js:function:: Channel.set(channel, string [, offset [, length]])
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002033
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02002034 This function replaces **length** bytes of incoming data of the channel
2035 buffer, starting at offset **offset**, by the string **string**. The function
2036 returns the copied length on success or -1 if data cannot be copied.
Christopher Faulet6a79fc12021-08-06 16:02:36 +02002037
2038 By default, if no length is provided, all incoming data, starting at the given
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02002039 offset, are replaced. Not providing an offset is the same as setting it
Ilya Shipitsinff0f2782021-08-22 22:18:07 +05002040 to 0. A positive offset is relative to the beginning of incoming data of the
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02002041 channel buffer while negative offset is relative to the end.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002042
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002043 :param class_channel channel: The manipulated Channel.
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02002044 :param string string: The data to copy into incoming data.
2045 :param integer offset: *optional* The offset in incoming data where to start
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02002046 the data replacement. 0 by default. May be negative to be relative to the
2047 end of incoming data.
Christopher Faulet6a79fc12021-08-06 16:02:36 +02002048 :param integer length: *optional* The length of data to replace. All incoming
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02002049 data by default.
Christopher Faulet6a79fc12021-08-06 16:02:36 +02002050 :returns: an integer containing the amount of bytes copied or -1.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002051
Christopher Faulet6a79fc12021-08-06 16:02:36 +02002052.. js:function:: Channel.dup(channel)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002053
Christopher Faulet6a79fc12021-08-06 16:02:36 +02002054 **DEPRECATED**
2055
2056 This function returns all incoming data found in the channel buffer. The data
Boyang Li60cfe8b2022-05-10 18:11:00 +00002057 are not removed from the buffer and can be reprocessed later.
Christopher Faulet6a79fc12021-08-06 16:02:36 +02002058
2059 If there is no incoming data and the channel can't receive more data, a 'nil'
2060 value is returned.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002061
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002062 :param class_channel channel: The manipulated Channel.
Christopher Faulet6a79fc12021-08-06 16:02:36 +02002063 :returns: a string containing all data found or nil.
2064
2065 .. warning::
2066 This function is deprecated. :js:func:`Channel.data()` must be used
2067 instead.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002068
Christopher Faulet6a79fc12021-08-06 16:02:36 +02002069.. js:function:: Channel.get(channel)
2070
2071 **DEPRECATED**
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002072
Christopher Faulet6a79fc12021-08-06 16:02:36 +02002073 This function returns all incoming data found in the channel buffer and remove
2074 them from the buffer.
2075
2076 If there is no incoming data and the channel can't receive more data, a 'nil'
2077 value is returned.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002078
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002079 :param class_channel channel: The manipulated Channel.
Christopher Faulet6a79fc12021-08-06 16:02:36 +02002080 :returns: a string containing all the data found or nil.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002081
Christopher Faulet6a79fc12021-08-06 16:02:36 +02002082 .. warning::
2083 This function is deprecated. :js:func:`Channel.data()` must be used to
2084 retrieve data followed by a call to :js:func:`Channel:remove()` to remove
2085 data.
Thierry FOURNIER / OZON.IO65192f32016-11-07 15:28:40 +01002086
Christopher Faulet6a79fc12021-08-06 16:02:36 +02002087 .. code-block:: lua
Thierry FOURNIER / OZON.IO65192f32016-11-07 15:28:40 +01002088
Christopher Faulet6a79fc12021-08-06 16:02:36 +02002089 local data = chn:data()
2090 chn:remove(0, data:len())
2091
2092 ..
2093
2094.. js:function:: Channel.getline(channel)
2095
2096 **DEPRECATED**
2097
2098 This function returns the first line found in incoming data of the channel
Christopher Faulet5a2c6612021-08-15 20:35:25 +02002099 buffer, including the '\\n'. The returned data are removed from the buffer. If
2100 no line is found, and if called by an action, this function yields to wait for
2101 more data, except if the channel can't receive more data. In this case all
2102 data are returned.
Christopher Faulet6a79fc12021-08-06 16:02:36 +02002103
2104 If there is no incoming data and the channel can't receive more data, a 'nil'
2105 value is returned.
2106
2107 :param class_channel channel: The manipulated Channel.
2108 :returns: a string containing the line found or nil.
2109
2110 .. warning::
Boyang Li60cfe8b2022-05-10 18:11:00 +00002111 This function is deprecated. :js:func:`Channel.line()` must be used to
Christopher Faulet6a79fc12021-08-06 16:02:36 +02002112 retrieve a line followed by a call to :js:func:`Channel:remove()` to remove
2113 data.
2114
2115 .. code-block:: lua
2116
2117 local line = chn:line(0, -1)
2118 chn:remove(0, line:len())
2119
2120 ..
2121
2122.. js:function:: Channel.get_in_len(channel)
2123
Boyang Li60cfe8b2022-05-10 18:11:00 +00002124 **DEPRECATED**
Christopher Faulet6a79fc12021-08-06 16:02:36 +02002125
Christopher Faulet5a2c6612021-08-15 20:35:25 +02002126 This function returns the length of the input part of the buffer. When called
2127 by a filter, this value is relative to the filter.
Christopher Faulet6a79fc12021-08-06 16:02:36 +02002128
2129 :param class_channel channel: The manipulated Channel.
2130 :returns: an integer containing the amount of available bytes.
2131
2132 .. warning::
2133 This function is deprecated. :js:func:`Channel.input()` must be used
2134 instead.
2135
2136.. js:function:: Channel.get_out_len(channel)
2137
Boyang Li60cfe8b2022-05-10 18:11:00 +00002138 **DEPRECATED**
Christopher Faulet6a79fc12021-08-06 16:02:36 +02002139
Christopher Faulet5a2c6612021-08-15 20:35:25 +02002140 This function returns the length of the output part of the buffer. When called
2141 by a filter, this value is relative to the filter.
Christopher Faulet6a79fc12021-08-06 16:02:36 +02002142
2143 :param class_channel channel: The manipulated Channel.
2144 :returns: an integer containing the amount of available bytes.
2145
2146 .. warning::
2147 This function is deprecated. :js:func:`Channel.output()` must be used
2148 instead.
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002149
2150.. _http_class:
Thierry FOURNIER08504f42015-03-16 14:17:08 +01002151
2152HTTP class
2153==========
2154
2155.. js:class:: HTTP
2156
2157 This class contain all the HTTP manipulation functions.
2158
Pieter Baauw386a1272015-08-16 15:26:24 +02002159.. js:function:: HTTP.req_get_headers(http)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01002160
Patrick Hemmerc6a1d712018-05-01 21:30:41 -04002161 Returns a table containing all the request headers.
Thierry FOURNIER08504f42015-03-16 14:17:08 +01002162
2163 :param class_http http: The related http object.
Patrick Hemmerc6a1d712018-05-01 21:30:41 -04002164 :returns: table of headers.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01002165 :see: :js:func:`HTTP.res_get_headers`
Thierry FOURNIER08504f42015-03-16 14:17:08 +01002166
Patrick Hemmerc6a1d712018-05-01 21:30:41 -04002167 This is the form of the returned table:
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002168
2169.. code-block:: lua
2170
2171 HTTP:req_get_headers()['<header-name>'][<header-index>] = "<header-value>"
2172
2173 local hdr = HTTP:req_get_headers()
2174 hdr["host"][0] = "www.test.com"
2175 hdr["accept"][0] = "audio/basic q=1"
2176 hdr["accept"][1] = "audio/*, q=0.2"
2177 hdr["accept"][2] = "*/*, q=0.1"
2178..
2179
Pieter Baauw386a1272015-08-16 15:26:24 +02002180.. js:function:: HTTP.res_get_headers(http)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01002181
Patrick Hemmerc6a1d712018-05-01 21:30:41 -04002182 Returns a table containing all the response headers.
Thierry FOURNIER08504f42015-03-16 14:17:08 +01002183
2184 :param class_http http: The related http object.
Patrick Hemmerc6a1d712018-05-01 21:30:41 -04002185 :returns: table of headers.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01002186 :see: :js:func:`HTTP.req_get_headers`
Thierry FOURNIER08504f42015-03-16 14:17:08 +01002187
Patrick Hemmerc6a1d712018-05-01 21:30:41 -04002188 This is the form of the returned table:
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002189
2190.. code-block:: lua
2191
2192 HTTP:res_get_headers()['<header-name>'][<header-index>] = "<header-value>"
2193
2194 local hdr = HTTP:req_get_headers()
2195 hdr["host"][0] = "www.test.com"
2196 hdr["accept"][0] = "audio/basic q=1"
2197 hdr["accept"][1] = "audio/*, q=0.2"
2198 hdr["accept"][2] = "*.*, q=0.1"
2199..
2200
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002201.. js:function:: HTTP.req_add_header(http, name, value)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01002202
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02002203 Appends a HTTP header field in the request whose name is
Thierry FOURNIER08504f42015-03-16 14:17:08 +01002204 specified in "name" and whose value is defined in "value".
2205
2206 :param class_http http: The related http object.
2207 :param string name: The header name.
2208 :param string value: The header value.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01002209 :see: :js:func:`HTTP.res_add_header`
Thierry FOURNIER08504f42015-03-16 14:17:08 +01002210
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002211.. js:function:: HTTP.res_add_header(http, name, value)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01002212
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02002213 Appends a HTTP header field in the response whose name is
Thierry FOURNIER08504f42015-03-16 14:17:08 +01002214 specified in "name" and whose value is defined in "value".
2215
2216 :param class_http http: The related http object.
2217 :param string name: The header name.
2218 :param string value: The header value.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01002219 :see: :js:func:`HTTP.req_add_header`
Thierry FOURNIER08504f42015-03-16 14:17:08 +01002220
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002221.. js:function:: HTTP.req_del_header(http, name)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01002222
2223 Removes all HTTP header fields in the request whose name is
2224 specified in "name".
2225
2226 :param class_http http: The related http object.
2227 :param string name: The header name.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01002228 :see: :js:func:`HTTP.res_del_header`
Thierry FOURNIER08504f42015-03-16 14:17:08 +01002229
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002230.. js:function:: HTTP.res_del_header(http, name)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01002231
2232 Removes all HTTP header fields in the response whose name is
2233 specified in "name".
2234
2235 :param class_http http: The related http object.
2236 :param string name: The header name.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01002237 :see: :js:func:`HTTP.req_del_header`
Thierry FOURNIER08504f42015-03-16 14:17:08 +01002238
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002239.. js:function:: HTTP.req_set_header(http, name, value)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01002240
Bertrand Jacquin874a35c2018-09-10 21:26:07 +01002241 This variable replace all occurrence of all header "name", by only
Thierry FOURNIER08504f42015-03-16 14:17:08 +01002242 one containing the "value".
2243
2244 :param class_http http: The related http object.
2245 :param string name: The header name.
2246 :param string value: The header value.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01002247 :see: :js:func:`HTTP.res_set_header`
Thierry FOURNIER08504f42015-03-16 14:17:08 +01002248
Bertrand Jacquin874a35c2018-09-10 21:26:07 +01002249 This function does the same work as the following code:
Thierry FOURNIER08504f42015-03-16 14:17:08 +01002250
2251.. code-block:: lua
2252
2253 function fcn(txn)
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002254 TXN.http:req_del_header("header")
2255 TXN.http:req_add_header("header", "value")
Thierry FOURNIER08504f42015-03-16 14:17:08 +01002256 end
2257..
2258
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002259.. js:function:: HTTP.res_set_header(http, name, value)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01002260
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02002261 This function replaces all occurrence of all header "name", by only
Thierry FOURNIER08504f42015-03-16 14:17:08 +01002262 one containing the "value".
2263
2264 :param class_http http: The related http object.
2265 :param string name: The header name.
2266 :param string value: The header value.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01002267 :see: :js:func:`HTTP.req_rep_header()`
Thierry FOURNIER08504f42015-03-16 14:17:08 +01002268
Pieter Baauw386a1272015-08-16 15:26:24 +02002269.. js:function:: HTTP.req_rep_header(http, name, regex, replace)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01002270
2271 Matches the regular expression in all occurrences of header field "name"
2272 according to "regex", and replaces them with the "replace" argument. The
2273 replacement value can contain back references like \1, \2, ... This
2274 function works with the request.
2275
2276 :param class_http http: The related http object.
2277 :param string name: The header name.
2278 :param string regex: The match regular expression.
2279 :param string replace: The replacement value.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01002280 :see: :js:func:`HTTP.res_rep_header()`
Thierry FOURNIER08504f42015-03-16 14:17:08 +01002281
Pieter Baauw386a1272015-08-16 15:26:24 +02002282.. js:function:: HTTP.res_rep_header(http, name, regex, string)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01002283
2284 Matches the regular expression in all occurrences of header field "name"
2285 according to "regex", and replaces them with the "replace" argument. The
2286 replacement value can contain back references like \1, \2, ... This
2287 function works with the request.
2288
2289 :param class_http http: The related http object.
2290 :param string name: The header name.
2291 :param string regex: The match regular expression.
2292 :param string replace: The replacement value.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01002293 :see: :js:func:`HTTP.req_rep_header()`
Thierry FOURNIER08504f42015-03-16 14:17:08 +01002294
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002295.. js:function:: HTTP.req_set_method(http, method)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01002296
2297 Rewrites the request method with the parameter "method".
2298
2299 :param class_http http: The related http object.
2300 :param string method: The new method.
2301
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002302.. js:function:: HTTP.req_set_path(http, path)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01002303
2304 Rewrites the request path with the "path" parameter.
2305
2306 :param class_http http: The related http object.
2307 :param string path: The new path.
2308
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002309.. js:function:: HTTP.req_set_query(http, query)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01002310
2311 Rewrites the request's query string which appears after the first question
2312 mark ("?") with the parameter "query".
2313
2314 :param class_http http: The related http object.
2315 :param string query: The new query.
2316
Thierry FOURNIER0d79cf62015-08-26 14:20:58 +02002317.. js:function:: HTTP.req_set_uri(http, uri)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01002318
2319 Rewrites the request URI with the parameter "uri".
2320
2321 :param class_http http: The related http object.
2322 :param string uri: The new uri.
2323
Robin H. Johnson52f5db22017-01-01 13:10:52 -08002324.. js:function:: HTTP.res_set_status(http, status [, reason])
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +02002325
Robin H. Johnson52f5db22017-01-01 13:10:52 -08002326 Rewrites the response status code with the parameter "code".
2327
2328 If no custom reason is provided, it will be generated from the status.
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +02002329
2330 :param class_http http: The related http object.
2331 :param integer status: The new response status code.
Robin H. Johnson52f5db22017-01-01 13:10:52 -08002332 :param string reason: The new response reason (optional).
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +02002333
William Lallemand00a15022021-11-19 16:02:44 +01002334.. _httpclient_class:
2335
2336HTTPClient class
2337================
2338
2339.. js:class:: HTTPClient
2340
2341 The httpclient class allows issue of outbound HTTP requests through a simple
2342 API without the knowledge of HAProxy internals.
2343
2344.. js:function:: HTTPClient.get(httpclient, request)
2345.. js:function:: HTTPClient.head(httpclient, request)
2346.. js:function:: HTTPClient.put(httpclient, request)
2347.. js:function:: HTTPClient.post(httpclient, request)
2348.. js:function:: HTTPClient.delete(httpclient, request)
2349
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02002350 Send a HTTP request and wait for a response. GET, HEAD PUT, POST and DELETE
2351 methods can be used.
2352 The HTTPClient will send asynchronously the data and is able to send and
2353 receive more than HAProxy bufsize.
William Lallemand00a15022021-11-19 16:02:44 +01002354
William Lallemanda9256192022-10-21 11:48:24 +02002355 The HTTPClient interface is not able to decompress responses, it is not
2356 recommended to send an Accept-Encoding in the request so the response is
2357 received uncompressed.
William Lallemand00a15022021-11-19 16:02:44 +01002358
2359 :param class httpclient: Is the manipulated HTTPClient.
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02002360 :param table request: Is a table containing the parameters of the request
2361 that will be send.
2362 :param string request.url: Is a mandatory parameter for the request that
2363 contains the URL.
2364 :param string request.body: Is an optional parameter for the request that
2365 contains the body to send.
2366 :param table request.headers: Is an optional parameter for the request that
2367 contains the headers to send.
2368 :param string request.dst: Is an optional parameter for the destination in
2369 haproxy address format.
2370 :param integer request.timeout: Optional timeout parameter, set a
2371 "timeout server" on the connections.
William Lallemand00a15022021-11-19 16:02:44 +01002372 :returns: Lua table containing the response
2373
2374
2375.. code-block:: lua
2376
2377 local httpclient = core.httpclient()
William Lallemand4f4f2b72022-02-17 20:00:23 +01002378 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 +01002379
2380..
2381
2382.. code-block:: lua
2383
2384 response = {
2385 status = 400,
2386 reason = "Bad request",
2387 headers = {
2388 ["content-type"] = { "text/html" },
2389 ["cache-control"] = { "no-cache", "no-store" },
2390 },
William Lallemand4f4f2b72022-02-17 20:00:23 +01002391 body = "<html><body><h1>invalid request<h1></body></html>",
William Lallemand00a15022021-11-19 16:02:44 +01002392 }
2393..
2394
2395
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002396.. _txn_class:
2397
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002398TXN class
2399=========
2400
2401.. js:class:: TXN
2402
2403 The txn class contain all the functions relative to the http or tcp
2404 transaction (Note than a tcp stream is the same than a tcp transaction, but
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02002405 a HTTP transaction is not the same than a tcp stream).
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002406
2407 The usage of this class permits to retrieve data from the requests, alter it
2408 and forward it.
2409
2410 All the functions provided by this class are available in the context
Christopher Faulet5a2c6612021-08-15 20:35:25 +02002411 **sample-fetches**, **actions** and **filters**.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002412
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002413.. js:attribute:: TXN.c
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002414
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002415 :returns: An :ref:`converters_class`.
2416
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002417 This attribute contains a Converters class object.
2418
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002419.. js:attribute:: TXN.sc
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002420
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002421 :returns: An :ref:`converters_class`.
2422
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002423 This attribute contains a Converters class object. The functions of
2424 this object returns always a string.
2425
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002426.. js:attribute:: TXN.f
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002427
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002428 :returns: An :ref:`fetches_class`.
2429
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002430 This attribute contains a Fetches class object.
2431
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002432.. js:attribute:: TXN.sf
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002433
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002434 :returns: An :ref:`fetches_class`.
2435
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002436 This attribute contains a Fetches class object. The functions of
2437 this object returns always a string.
2438
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002439.. js:attribute:: TXN.req
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002440
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002441 :returns: An :ref:`channel_class`.
2442
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002443 This attribute contains a channel class object for the request buffer.
2444
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002445.. js:attribute:: TXN.res
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002446
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002447 :returns: An :ref:`channel_class`.
2448
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002449 This attribute contains a channel class object for the response buffer.
2450
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002451.. js:attribute:: TXN.http
Thierry FOURNIER08504f42015-03-16 14:17:08 +01002452
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002453 :returns: An :ref:`http_class`.
2454
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02002455 This attribute contains a HTTP class object. It is available only if the
Thierry FOURNIER08504f42015-03-16 14:17:08 +01002456 proxy has the "mode http" enabled.
2457
Christopher Faulet5a2c6612021-08-15 20:35:25 +02002458.. js:attribute:: TXN.http_req
2459
2460 :returns: An :ref:`httpmessage_class`.
2461
2462 This attribute contains the request HTTPMessage class object. It is available
2463 only if the proxy has the "mode http" enabled and only in the **filters**
2464 context.
2465
2466.. js:attribute:: TXN.http_res
2467
2468 :returns: An :ref:`httpmessage_class`.
2469
2470 This attribute contains the response HTTPMessage class object. It is available
2471 only if the proxy has the "mode http" enabled and only in the **filters**
2472 context.
2473
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01002474.. js:function:: TXN.log(TXN, loglevel, msg)
2475
2476 This function sends a log. The log is sent, according with the HAProxy
2477 configuration file, on the default syslog server if it is configured and on
2478 the stderr if it is allowed.
2479
2480 :param class_txn txn: The class txn object containing the data.
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02002481 :param integer loglevel: Is the log level associated with the message. It is
2482 a number between 0 and 7.
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01002483 :param string msg: The log content.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01002484 :see: :js:attr:`core.emerg`, :js:attr:`core.alert`, :js:attr:`core.crit`,
2485 :js:attr:`core.err`, :js:attr:`core.warning`, :js:attr:`core.notice`,
2486 :js:attr:`core.info`, :js:attr:`core.debug` (log level definitions)
2487 :see: :js:func:`TXN.deflog`
2488 :see: :js:func:`TXN.Debug`
2489 :see: :js:func:`TXN.Info`
2490 :see: :js:func:`TXN.Warning`
2491 :see: :js:func:`TXN.Alert`
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01002492
2493.. js:function:: TXN.deflog(TXN, msg)
2494
Bertrand Jacquin874a35c2018-09-10 21:26:07 +01002495 Sends a log line with the default loglevel for the proxy associated with the
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01002496 transaction.
2497
2498 :param class_txn txn: The class txn object containing the data.
2499 :param string msg: The log content.
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02002500 :see: :js:func:`TXN.log`
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01002501
2502.. js:function:: TXN.Debug(txn, msg)
2503
2504 :param class_txn txn: The class txn object containing the data.
2505 :param string msg: The log content.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01002506 :see: :js:func:`TXN.log`
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01002507
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02002508 Does the same job as:
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01002509
2510.. code-block:: lua
2511
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002512 function Debug(txn, msg)
2513 TXN.log(txn, core.debug, msg)
2514 end
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01002515..
2516
2517.. js:function:: TXN.Info(txn, msg)
2518
2519 :param class_txn txn: The class txn object containing the data.
2520 :param string msg: The log content.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01002521 :see: :js:func:`TXN.log`
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01002522
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02002523 Does the same job as:
2524
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01002525.. code-block:: lua
2526
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02002527 function Info(txn, msg)
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002528 TXN.log(txn, core.info, msg)
2529 end
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01002530..
2531
2532.. js:function:: TXN.Warning(txn, msg)
2533
2534 :param class_txn txn: The class txn object containing the data.
2535 :param string msg: The log content.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01002536 :see: :js:func:`TXN.log`
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01002537
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02002538 Does the same job as:
2539
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01002540.. code-block:: lua
2541
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02002542 function Warning(txn, msg)
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002543 TXN.log(txn, core.warning, msg)
2544 end
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01002545..
2546
2547.. js:function:: TXN.Alert(txn, msg)
2548
2549 :param class_txn txn: The class txn object containing the data.
2550 :param string msg: The log content.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01002551 :see: :js:func:`TXN.log`
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01002552
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02002553 Does the same job as:
2554
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01002555.. code-block:: lua
2556
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02002557 function Alert(txn, msg)
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002558 TXN.log(txn, core.alert, msg)
2559 end
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01002560..
2561
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002562.. js:function:: TXN.get_priv(txn)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002563
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002564 Return Lua data stored in the current transaction (with the `TXN.set_priv()`)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002565 function. If no data are stored, it returns a nil value.
2566
2567 :param class_txn txn: The class txn object containing the data.
Bertrand Jacquin874a35c2018-09-10 21:26:07 +01002568 :returns: the opaque data previously stored, or nil if nothing is
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02002569 available.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002570
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002571.. js:function:: TXN.set_priv(txn, data)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002572
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02002573 Store any data in the current HAProxy transaction. This action replaces the
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002574 old stored data.
2575
2576 :param class_txn txn: The class txn object containing the data.
2577 :param opaque data: The data which is stored in the transaction.
2578
Tim Duesterhus4e172c92020-05-19 13:49:42 +02002579.. js:function:: TXN.set_var(TXN, var, value[, ifexist])
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +02002580
David Carlier61fdf8b2015-10-02 11:59:38 +01002581 Converts a Lua type in a HAProxy type and store it in a variable <var>.
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +02002582
2583 :param class_txn txn: The class txn object containing the data.
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02002584 :param string var: The variable name according with the HAProxy variable
2585 syntax.
2586 :param type value: The value associated to the variable. The type can be
2587 string or integer.
2588 :param boolean ifexist: If this parameter is set to true the variable will
2589 only be set if it was defined elsewhere (i.e. used within the configuration).
2590 For global variables (using the "proc" scope), they will only be updated and
2591 never created. It is highly recommended to always set this to true.
Christopher Faulet85d79c92016-11-09 16:54:56 +01002592
2593.. js:function:: TXN.unset_var(TXN, var)
2594
2595 Unset the variable <var>.
2596
2597 :param class_txn txn: The class txn object containing the data.
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02002598 :param string var: The variable name according with the HAProxy variable
2599 syntax.
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +02002600
2601.. js:function:: TXN.get_var(TXN, var)
2602
2603 Returns data stored in the variable <var> converter in Lua type.
2604
2605 :param class_txn txn: The class txn object containing the data.
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02002606 :param string var: The variable name according with the HAProxy variable
2607 syntax.
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +02002608
Christopher Faulet700d9e82020-01-31 12:21:52 +01002609.. js:function:: TXN.reply([reply])
2610
2611 Return a new reply object
2612
2613 :param table reply: A table containing info to initialize the reply fields.
2614 :returns: A :ref:`reply_class` object.
2615
2616 The table used to initialized the reply object may contain following entries :
2617
2618 * status : The reply status code. the code 200 is used by default.
2619 * reason : The reply reason. The reason corresponding to the status code is
2620 used by default.
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02002621 * headers : A list of headers, indexed by header name. Empty by default. For
Christopher Faulet700d9e82020-01-31 12:21:52 +01002622 a given name, multiple values are possible, stored in an ordered list.
2623 * body : The reply body, empty by default.
2624
2625.. code-block:: lua
2626
2627 local reply = txn:reply{
2628 status = 400,
2629 reason = "Bad request",
2630 headers = {
2631 ["content-type"] = { "text/html" },
2632 ["cache-control"] = {"no-cache", "no-store" }
2633 },
2634 body = "<html><body><h1>invalid request<h1></body></html>"
2635 }
2636..
2637 :see: :js:class:`Reply`
2638
2639.. js:function:: TXN.done(txn[, reply])
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002640
Willy Tarreaubc183a62015-08-28 10:39:11 +02002641 This function terminates processing of the transaction and the associated
Christopher Faulet700d9e82020-01-31 12:21:52 +01002642 session and optionally reply to the client for HTTP sessions.
2643
2644 :param class_txn txn: The class txn object containing the data.
2645 :param class_reply reply: The class reply object to return to the client.
2646
2647 This functions can be used when a critical error is detected or to terminate
Willy Tarreaubc183a62015-08-28 10:39:11 +02002648 processing after some data have been returned to the client (eg: a redirect).
Christopher Faulet700d9e82020-01-31 12:21:52 +01002649 To do so, a reply may be provided. This object is optional and may contain a
2650 status code, a reason, a header list and a body. All these fields are
Christopher Faulet7855b192021-11-09 18:39:51 +01002651 optional. When not provided, the default values are used. By default, with an
2652 empty reply object, an empty HTTP 200 response is returned to the client. If
2653 no reply object is provided, the transaction is terminated without any
2654 reply. If a reply object is provided, it must not exceed the buffer size once
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02002655 converted into the internal HTTP representation. Because for now there is no
Christopher Faulet7855b192021-11-09 18:39:51 +01002656 easy way to be sure it fits, it is probably better to keep it reasonably
2657 small.
Christopher Faulet700d9e82020-01-31 12:21:52 +01002658
2659 The reply object may be fully created in lua or the class Reply may be used to
2660 create it.
2661
2662.. code-block:: lua
2663
2664 local reply = txn:reply()
2665 reply:set_status(400, "Bad request")
2666 reply:add_header("content-type", "text/html")
2667 reply:add_header("cache-control", "no-cache")
2668 reply:add_header("cache-control", "no-store")
2669 reply:set_body("<html><body><h1>invalid request<h1></body></html>")
2670 txn:done(reply)
2671..
2672
2673.. code-block:: lua
2674
2675 txn:done{
2676 status = 400,
2677 reason = "Bad request",
2678 headers = {
2679 ["content-type"] = { "text/html" },
2680 ["cache-control"] = { "no-cache", "no-store" },
2681 },
2682 body = "<html><body><h1>invalid request<h1></body></html>"
2683 }
2684..
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002685
Christopher Faulet1e9b1b62021-08-11 10:14:30 +02002686 .. warning::
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02002687 It does not make sense to call this function from sample-fetches. In this
2688 case the behavior is the same than core.done(): it finishes the Lua
Christopher Faulet1e9b1b62021-08-11 10:14:30 +02002689 execution. The transaction is really aborted only from an action registered
2690 function.
Thierry FOURNIERab00df62016-07-14 11:42:37 +02002691
Christopher Faulet700d9e82020-01-31 12:21:52 +01002692 :see: :js:func:`TXN.reply`, :js:class:`Reply`
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002693
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002694.. js:function:: TXN.set_loglevel(txn, loglevel)
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01002695
2696 Is used to change the log level of the current request. The "loglevel" must
2697 be an integer between 0 and 7.
2698
2699 :param class_txn txn: The class txn object containing the data.
2700 :param integer loglevel: The required log level. This variable can be one of
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01002701 :see: :js:attr:`core.emerg`, :js:attr:`core.alert`, :js:attr:`core.crit`,
2702 :js:attr:`core.err`, :js:attr:`core.warning`, :js:attr:`core.notice`,
2703 :js:attr:`core.info`, :js:attr:`core.debug` (log level definitions)
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01002704
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002705.. js:function:: TXN.set_tos(txn, tos)
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01002706
2707 Is used to set the TOS or DSCP field value of packets sent to the client to
2708 the value passed in "tos" on platforms which support this.
2709
2710 :param class_txn txn: The class txn object containing the data.
2711 :param integer tos: The new TOS os DSCP.
2712
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002713.. js:function:: TXN.set_mark(txn, mark)
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01002714
2715 Is used to set the Netfilter MARK on all packets sent to the client to the
2716 value passed in "mark" on platforms which support it.
2717
2718 :param class_txn txn: The class txn object containing the data.
2719 :param integer mark: The mark value.
2720
Patrick Hemmer268a7072018-05-11 12:52:31 -04002721.. js:function:: TXN.set_priority_class(txn, prio)
2722
2723 This function adjusts the priority class of the transaction. The value should
2724 be within the range -2047..2047. Values outside this range will be
2725 truncated.
2726
2727 See the HAProxy configuration.txt file keyword "http-request" action
2728 "set-priority-class" for details.
2729
2730.. js:function:: TXN.set_priority_offset(txn, prio)
2731
2732 This function adjusts the priority offset of the transaction. The value
2733 should be within the range -524287..524287. Values outside this range will be
2734 truncated.
2735
2736 See the HAProxy configuration.txt file keyword "http-request" action
2737 "set-priority-offset" for details.
2738
Christopher Faulet700d9e82020-01-31 12:21:52 +01002739.. _reply_class:
2740
2741Reply class
2742============
2743
2744.. js:class:: Reply
2745
2746 **context**: action
2747
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02002748 This class represents a HTTP response message. It provides some methods to
Christopher Faulet7855b192021-11-09 18:39:51 +01002749 enrich it. Once converted into the internal HTTP representation, the response
2750 message must not exceed the buffer size. Because for now there is no
2751 easy way to be sure it fits, it is probably better to keep it reasonably
2752 small.
2753
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02002754 See tune.bufsize in the configuration manual for details.
Christopher Faulet700d9e82020-01-31 12:21:52 +01002755
2756.. code-block:: lua
2757
2758 local reply = txn:reply({status = 400}) -- default HTTP 400 reason-phase used
2759 reply:add_header("content-type", "text/html")
2760 reply:add_header("cache-control", "no-cache")
2761 reply:add_header("cache-control", "no-store")
2762 reply:set_body("<html><body><h1>invalid request<h1></body></html>")
2763..
2764
2765 :see: :js:func:`TXN.reply`
2766
2767.. js:attribute:: Reply.status
2768
2769 The reply status code. By default, the status code is set to 200.
2770
2771 :returns: integer
2772
2773.. js:attribute:: Reply.reason
2774
2775 The reason string describing the status code.
2776
2777 :returns: string
2778
2779.. js:attribute:: Reply.headers
2780
2781 A table indexing all reply headers by name. To each name is associated an
2782 ordered list of values.
2783
2784 :returns: Lua table
2785
2786.. code-block:: lua
2787
2788 {
2789 ["content-type"] = { "text/html" },
2790 ["cache-control"] = {"no-cache", "no-store" },
2791 x_header_name = { "value1", "value2", ... }
2792 ...
2793 }
2794..
2795
2796.. js:attribute:: Reply.body
2797
2798 The reply payload.
2799
2800 :returns: string
2801
2802.. js:function:: Reply.set_status(REPLY, status[, reason])
2803
2804 Set the reply status code and optionally the reason-phrase. If the reason is
2805 not provided, the default reason corresponding to the status code is used.
2806
2807 :param class_reply reply: The related Reply object.
2808 :param integer status: The reply status code.
2809 :param string reason: The reply status reason (optional).
2810
2811.. js:function:: Reply.add_header(REPLY, name, value)
2812
2813 Add a header to the reply object. If the header does not already exist, a new
2814 entry is created with its name as index and a one-element list containing its
2815 value as value. Otherwise, the header value is appended to the ordered list of
2816 values associated to the header name.
2817
2818 :param class_reply reply: The related Reply object.
2819 :param string name: The header field name.
2820 :param string value: The header field value.
2821
2822.. js:function:: Reply.del_header(REPLY, name)
2823
2824 Remove all occurrences of a header name from the reply object.
2825
2826 :param class_reply reply: The related Reply object.
2827 :param string name: The header field name.
2828
2829.. js:function:: Reply.set_body(REPLY, body)
2830
2831 Set the reply payload.
2832
2833 :param class_reply reply: The related Reply object.
2834 :param string body: The reply payload.
2835
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002836.. _socket_class:
2837
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002838Socket class
2839============
2840
2841.. js:class:: Socket
2842
2843 This class must be compatible with the Lua Socket class. Only the 'client'
2844 functions are available. See the Lua Socket documentation:
2845
2846 `http://w3.impa.br/~diego/software/luasocket/tcp.html
2847 <http://w3.impa.br/~diego/software/luasocket/tcp.html>`_
2848
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002849.. js:function:: Socket.close(socket)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002850
2851 Closes a TCP object. The internal socket used by the object is closed and the
2852 local address to which the object was bound is made available to other
2853 applications. No further operations (except for further calls to the close
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002854 method) are allowed on a closed Socket.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002855
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002856 :param class_socket socket: Is the manipulated Socket.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002857
2858 Note: It is important to close all used sockets once they are not needed,
2859 since, in many systems, each socket uses a file descriptor, which are limited
2860 system resources. Garbage-collected objects are automatically closed before
2861 destruction, though.
2862
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002863.. js:function:: Socket.connect(socket, address[, port])
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002864
2865 Attempts to connect a socket object to a remote host.
2866
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002867
2868 In case of error, the method returns nil followed by a string describing the
2869 error. In case of success, the method returns 1.
2870
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002871 :param class_socket socket: Is the manipulated Socket.
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002872 :param string address: can be an IP address or a host name. See below for more
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02002873 information.
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002874 :param integer port: must be an integer number in the range [1..64K].
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002875 :returns: 1 or nil.
2876
Bertrand Jacquin874a35c2018-09-10 21:26:07 +01002877 An address field extension permits to use the connect() function to connect to
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002878 other stream than TCP. The syntax containing a simpleipv4 or ipv6 address is
2879 the basically expected format. This format requires the port.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002880
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002881 Other format accepted are a socket path like "/socket/path", it permits to
Bertrand Jacquin874a35c2018-09-10 21:26:07 +01002882 connect to a socket. Abstract namespaces are supported with the prefix
Joseph Herlant02cedc42018-11-13 19:45:17 -08002883 "abns@", and finally a file descriptor can be passed with the prefix "fd@".
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002884 The prefix "ipv4@", "ipv6@" and "unix@" are also supported. The port can be
Bertrand Jacquin874a35c2018-09-10 21:26:07 +01002885 passed int the string. The syntax "127.0.0.1:1234" is valid. In this case, the
Tim Duesterhus6edab862018-01-06 19:04:45 +01002886 parameter *port* must not be set.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002887
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002888.. js:function:: Socket.connect_ssl(socket, address, port)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002889
2890 Same behavior than the function socket:connect, but uses SSL.
2891
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002892 :param class_socket socket: Is the manipulated Socket.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002893 :returns: 1 or nil.
2894
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002895.. js:function:: Socket.getpeername(socket)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002896
2897 Returns information about the remote side of a connected client object.
2898
2899 Returns a string with the IP address of the peer, followed by the port number
2900 that peer is using for the connection. In case of error, the method returns
2901 nil.
2902
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002903 :param class_socket socket: Is the manipulated Socket.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002904 :returns: a string containing the server information.
2905
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002906.. js:function:: Socket.getsockname(socket)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002907
2908 Returns the local address information associated to the object.
2909
2910 The method returns a string with local IP address and a number with the port.
2911 In case of error, the method returns nil.
2912
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002913 :param class_socket socket: Is the manipulated Socket.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002914 :returns: a string containing the client information.
2915
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002916.. js:function:: Socket.receive(socket, [pattern [, prefix]])
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002917
2918 Reads data from a client object, according to the specified read pattern.
2919 Patterns follow the Lua file I/O format, and the difference in performance
2920 between all patterns is negligible.
2921
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002922 :param class_socket socket: Is the manipulated Socket.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002923 :param string|integer pattern: Describe what is required (see below).
2924 :param string prefix: A string which will be prefix the returned data.
2925 :returns: a string containing the required data or nil.
2926
2927 Pattern can be any of the following:
2928
2929 * **`*a`**: reads from the socket until the connection is closed. No
2930 end-of-line translation is performed;
2931
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002932 * **`*l`**: reads a line of text from the Socket. The line is terminated by a
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002933 LF character (ASCII 10), optionally preceded by a CR character
2934 (ASCII 13). The CR and LF characters are not included in the
2935 returned line. In fact, all CR characters are ignored by the
2936 pattern. This is the default pattern.
2937
2938 * **number**: causes the method to read a specified number of bytes from the
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002939 Socket. Prefix is an optional string to be concatenated to the
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002940 beginning of any received data before return.
2941
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002942 * **empty**: If the pattern is left empty, the default option is `*l`.
2943
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002944 If successful, the method returns the received pattern. In case of error, the
2945 method returns nil followed by an error message which can be the string
2946 'closed' in case the connection was closed before the transmission was
2947 completed or the string 'timeout' in case there was a timeout during the
2948 operation. Also, after the error message, the function returns the partial
2949 result of the transmission.
2950
2951 Important note: This function was changed severely. It used to support
2952 multiple patterns (but I have never seen this feature used) and now it
2953 doesn't anymore. Partial results used to be returned in the same way as
2954 successful results. This last feature violated the idea that all functions
2955 should return nil on error. Thus it was changed too.
2956
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002957.. js:function:: Socket.send(socket, data [, start [, end ]])
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002958
2959 Sends data through client object.
2960
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002961 :param class_socket socket: Is the manipulated Socket.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002962 :param string data: The data that will be sent.
2963 :param integer start: The start position in the buffer of the data which will
2964 be sent.
2965 :param integer end: The end position in the buffer of the data which will
2966 be sent.
2967 :returns: see below.
2968
2969 Data is the string to be sent. The optional arguments i and j work exactly
2970 like the standard string.sub Lua function to allow the selection of a
2971 substring to be sent.
2972
2973 If successful, the method returns the index of the last byte within [start,
2974 end] that has been sent. Notice that, if start is 1 or absent, this is
2975 effectively the total number of bytes sent. In case of error, the method
2976 returns nil, followed by an error message, followed by the index of the last
2977 byte within [start, end] that has been sent. You might want to try again from
2978 the byte following that. The error message can be 'closed' in case the
2979 connection was closed before the transmission was completed or the string
2980 'timeout' in case there was a timeout during the operation.
2981
2982 Note: Output is not buffered. For small strings, it is always better to
2983 concatenate them in Lua (with the '..' operator) and send the result in one
2984 call instead of calling the method several times.
2985
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002986.. js:function:: Socket.setoption(socket, option [, value])
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002987
2988 Just implemented for compatibility, this cal does nothing.
2989
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002990.. js:function:: Socket.settimeout(socket, value [, mode])
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002991
2992 Changes the timeout values for the object. All I/O operations are blocking.
2993 That is, any call to the methods send, receive, and accept will block
2994 indefinitely, until the operation completes. The settimeout method defines a
2995 limit on the amount of time the I/O methods can block. When a timeout time
2996 has elapsed, the affected methods give up and fail with an error code.
2997
2998 The amount of time to wait is specified as the value parameter, in seconds.
2999
Mark Lakes56cc1252018-03-27 09:48:06 +02003000 The timeout modes are not implemented, the only settable timeout is the
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01003001 inactivity time waiting for complete the internal buffer send or waiting for
3002 receive data.
3003
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01003004 :param class_socket socket: Is the manipulated Socket.
Bertrand Jacquin874a35c2018-09-10 21:26:07 +01003005 :param float value: The timeout value. Use floating point to specify
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02003006 milliseconds.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01003007
Thierry FOURNIER31904272017-10-25 12:59:51 +02003008.. _regex_class:
3009
3010Regex class
3011===========
3012
3013.. js:class:: Regex
3014
3015 This class allows the usage of HAProxy regexes because classic lua doesn't
3016 provides regexes. This class inherits the HAProxy compilation options, so the
3017 regexes can be libc regex, pcre regex or pcre JIT regex.
3018
3019 The expression matching number is limited to 20 per regex. The only available
3020 option is case sensitive.
3021
3022 Because regexes compilation is a heavy process, it is better to define all
3023 your regexes in the **body context** and use it during the runtime.
3024
3025.. code-block:: lua
3026
3027 -- Create the regex
3028 st, regex = Regex.new("needle (..) (...)", true);
3029
3030 -- Check compilation errors
3031 if st == false then
3032 print "error: " .. regex
3033 end
3034
3035 -- Match the regexes
3036 print(regex:exec("Looking for a needle in the haystack")) -- true
3037 print(regex:exec("Lokking for a cat in the haystack")) -- false
3038
3039 -- Extract words
3040 st, list = regex:match("Looking for a needle in the haystack")
3041 print(st) -- true
3042 print(list[1]) -- needle in the
3043 print(list[2]) -- in
3044 print(list[3]) -- the
3045
3046.. js:function:: Regex.new(regex, case_sensitive)
3047
3048 Create and compile a regex.
3049
3050 :param string regex: The regular expression according with the libc or pcre
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02003051 standard
Thierry FOURNIER31904272017-10-25 12:59:51 +02003052 :param boolean case_sensitive: Match is case sensitive or not.
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02003053 :returns: boolean status and :ref:`regex_class` or string containing fail
3054 reason.
Thierry FOURNIER31904272017-10-25 12:59:51 +02003055
3056.. js:function:: Regex.exec(regex, str)
3057
3058 Execute the regex.
3059
3060 :param class_regex regex: A :ref:`regex_class` object.
3061 :param string str: The input string will be compared with the compiled regex.
3062 :returns: a boolean status according with the match result.
3063
3064.. js:function:: Regex.match(regex, str)
3065
3066 Execute the regex and return matched expressions.
3067
3068 :param class_map map: A :ref:`regex_class` object.
3069 :param string str: The input string will be compared with the compiled regex.
3070 :returns: a boolean status according with the match result, and
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02003071 a table containing all the string matched in order of declaration.
Thierry FOURNIER31904272017-10-25 12:59:51 +02003072
Thierry FOURNIERdc595002015-12-21 11:13:52 +01003073.. _map_class:
3074
Thierry FOURNIER3def3932015-04-07 11:27:54 +02003075Map class
3076=========
3077
3078.. js:class:: Map
3079
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02003080 This class permits to do some lookups in HAProxy maps. The declared maps can
Bertrand Jacquin874a35c2018-09-10 21:26:07 +01003081 be modified during the runtime through the HAProxy management socket.
Thierry FOURNIER3def3932015-04-07 11:27:54 +02003082
3083.. code-block:: lua
3084
Thierry FOURNIERdc595002015-12-21 11:13:52 +01003085 default = "usa"
Thierry FOURNIER3def3932015-04-07 11:27:54 +02003086
Thierry FOURNIERdc595002015-12-21 11:13:52 +01003087 -- Create and load map
Thierry FOURNIER4dc71972017-01-28 08:33:08 +01003088 geo = Map.new("geo.map", Map._ip);
Thierry FOURNIER3def3932015-04-07 11:27:54 +02003089
Thierry FOURNIERdc595002015-12-21 11:13:52 +01003090 -- Create new fetch that returns the user country
3091 core.register_fetches("country", function(txn)
3092 local src;
3093 local loc;
Thierry FOURNIER3def3932015-04-07 11:27:54 +02003094
Thierry FOURNIERdc595002015-12-21 11:13:52 +01003095 src = txn.f:fhdr("x-forwarded-for");
3096 if (src == nil) then
3097 src = txn.f:src()
3098 if (src == nil) then
3099 return default;
3100 end
3101 end
Thierry FOURNIER3def3932015-04-07 11:27:54 +02003102
Thierry FOURNIERdc595002015-12-21 11:13:52 +01003103 -- Perform lookup
3104 loc = geo:lookup(src);
Thierry FOURNIER3def3932015-04-07 11:27:54 +02003105
Thierry FOURNIERdc595002015-12-21 11:13:52 +01003106 if (loc == nil) then
3107 return default;
3108 end
Thierry FOURNIER3def3932015-04-07 11:27:54 +02003109
Thierry FOURNIERdc595002015-12-21 11:13:52 +01003110 return loc;
3111 end);
Thierry FOURNIER3def3932015-04-07 11:27:54 +02003112
Thierry FOURNIER4dc71972017-01-28 08:33:08 +01003113.. js:attribute:: Map._int
Thierry FOURNIER3def3932015-04-07 11:27:54 +02003114
3115 See the HAProxy configuration.txt file, chapter "Using ACLs and fetching
Ilya Shipitsin2075ca82020-03-06 23:22:22 +05003116 samples" and subchapter "ACL basics" to understand this pattern matching
Thierry FOURNIER3def3932015-04-07 11:27:54 +02003117 method.
3118
Thierry FOURNIER4dc71972017-01-28 08:33:08 +01003119 Note that :js:attr:`Map.int` is also available for compatibility.
3120
3121.. js:attribute:: Map._ip
Thierry FOURNIER3def3932015-04-07 11:27:54 +02003122
3123 See the HAProxy configuration.txt file, chapter "Using ACLs and fetching
Ilya Shipitsin2075ca82020-03-06 23:22:22 +05003124 samples" and subchapter "ACL basics" to understand this pattern matching
Thierry FOURNIER3def3932015-04-07 11:27:54 +02003125 method.
3126
Thierry FOURNIER4dc71972017-01-28 08:33:08 +01003127 Note that :js:attr:`Map.ip` is also available for compatibility.
3128
3129.. js:attribute:: Map._str
Thierry FOURNIER3def3932015-04-07 11:27:54 +02003130
3131 See the HAProxy configuration.txt file, chapter "Using ACLs and fetching
Ilya Shipitsin2075ca82020-03-06 23:22:22 +05003132 samples" and subchapter "ACL basics" to understand this pattern matching
Thierry FOURNIER3def3932015-04-07 11:27:54 +02003133 method.
3134
Thierry FOURNIER4dc71972017-01-28 08:33:08 +01003135 Note that :js:attr:`Map.str` is also available for compatibility.
3136
3137.. js:attribute:: Map._beg
Thierry FOURNIER3def3932015-04-07 11:27:54 +02003138
3139 See the HAProxy configuration.txt file, chapter "Using ACLs and fetching
Ilya Shipitsin2075ca82020-03-06 23:22:22 +05003140 samples" and subchapter "ACL basics" to understand this pattern matching
Thierry FOURNIER3def3932015-04-07 11:27:54 +02003141 method.
3142
Thierry FOURNIER4dc71972017-01-28 08:33:08 +01003143 Note that :js:attr:`Map.beg` is also available for compatibility.
3144
3145.. js:attribute:: Map._sub
Thierry FOURNIER3def3932015-04-07 11:27:54 +02003146
3147 See the HAProxy configuration.txt file, chapter "Using ACLs and fetching
Ilya Shipitsin2075ca82020-03-06 23:22:22 +05003148 samples" and subchapter "ACL basics" to understand this pattern matching
Thierry FOURNIER3def3932015-04-07 11:27:54 +02003149 method.
3150
Thierry FOURNIER4dc71972017-01-28 08:33:08 +01003151 Note that :js:attr:`Map.sub` is also available for compatibility.
3152
3153.. js:attribute:: Map._dir
Thierry FOURNIER3def3932015-04-07 11:27:54 +02003154
3155 See the HAProxy configuration.txt file, chapter "Using ACLs and fetching
Ilya Shipitsin2075ca82020-03-06 23:22:22 +05003156 samples" and subchapter "ACL basics" to understand this pattern matching
Thierry FOURNIER3def3932015-04-07 11:27:54 +02003157 method.
3158
Thierry FOURNIER4dc71972017-01-28 08:33:08 +01003159 Note that :js:attr:`Map.dir` is also available for compatibility.
3160
3161.. js:attribute:: Map._dom
Thierry FOURNIER3def3932015-04-07 11:27:54 +02003162
3163 See the HAProxy configuration.txt file, chapter "Using ACLs and fetching
Ilya Shipitsin2075ca82020-03-06 23:22:22 +05003164 samples" and subchapter "ACL basics" to understand this pattern matching
Thierry FOURNIER3def3932015-04-07 11:27:54 +02003165 method.
3166
Thierry FOURNIER4dc71972017-01-28 08:33:08 +01003167 Note that :js:attr:`Map.dom` is also available for compatibility.
3168
3169.. js:attribute:: Map._end
Thierry FOURNIER3def3932015-04-07 11:27:54 +02003170
3171 See the HAProxy configuration.txt file, chapter "Using ACLs and fetching
Ilya Shipitsin2075ca82020-03-06 23:22:22 +05003172 samples" and subchapter "ACL basics" to understand this pattern matching
Thierry FOURNIER3def3932015-04-07 11:27:54 +02003173 method.
3174
Thierry FOURNIER4dc71972017-01-28 08:33:08 +01003175.. js:attribute:: Map._reg
Thierry FOURNIER3def3932015-04-07 11:27:54 +02003176
3177 See the HAProxy configuration.txt file, chapter "Using ACLs and fetching
Ilya Shipitsin2075ca82020-03-06 23:22:22 +05003178 samples" and subchapter "ACL basics" to understand this pattern matching
Thierry FOURNIER3def3932015-04-07 11:27:54 +02003179 method.
3180
Thierry FOURNIER4dc71972017-01-28 08:33:08 +01003181 Note that :js:attr:`Map.reg` is also available for compatibility.
3182
Thierry FOURNIER3def3932015-04-07 11:27:54 +02003183
3184.. js:function:: Map.new(file, method)
3185
3186 Creates and load a map.
3187
3188 :param string file: Is the file containing the map.
3189 :param integer method: Is the map pattern matching method. See the attributes
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02003190 of the Map class.
Thierry FOURNIER3def3932015-04-07 11:27:54 +02003191 :returns: a class Map object.
Thierry FOURNIER4dc71972017-01-28 08:33:08 +01003192 :see: The Map attributes: :js:attr:`Map._int`, :js:attr:`Map._ip`,
3193 :js:attr:`Map._str`, :js:attr:`Map._beg`, :js:attr:`Map._sub`,
3194 :js:attr:`Map._dir`, :js:attr:`Map._dom`, :js:attr:`Map._end` and
3195 :js:attr:`Map._reg`.
Thierry FOURNIER3def3932015-04-07 11:27:54 +02003196
3197.. js:function:: Map.lookup(map, str)
3198
3199 Perform a lookup in a map.
3200
3201 :param class_map map: Is the class Map object.
3202 :param string str: Is the string used as key.
3203 :returns: a string containing the result or nil if no match.
3204
3205.. js:function:: Map.slookup(map, str)
3206
3207 Perform a lookup in a map.
3208
3209 :param class_map map: Is the class Map object.
3210 :param string str: Is the string used as key.
3211 :returns: a string containing the result or empty string if no match.
3212
Thierry FOURNIERdc595002015-12-21 11:13:52 +01003213.. _applethttp_class:
3214
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02003215AppletHTTP class
Thierry FOURNIERdc595002015-12-21 11:13:52 +01003216================
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02003217
3218.. js:class:: AppletHTTP
3219
3220 This class is used with applets that requires the 'http' mode. The http applet
3221 can be registered with the *core.register_service()* function. They are used
3222 for processing an http request like a server in back of HAProxy.
3223
3224 This is an hello world sample code:
3225
3226.. code-block:: lua
Thierry FOURNIERdc595002015-12-21 11:13:52 +01003227
Pieter Baauw4d7f7662015-11-08 16:38:08 +01003228 core.register_service("hello-world", "http", function(applet)
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02003229 local response = "Hello World !"
3230 applet:set_status(200)
Pieter Baauw2dcb9bc2015-10-01 22:47:12 +02003231 applet:add_header("content-length", string.len(response))
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02003232 applet:add_header("content-type", "text/plain")
Pieter Baauw2dcb9bc2015-10-01 22:47:12 +02003233 applet:start_response()
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02003234 applet:send(response)
3235 end)
3236
Thierry FOURNIERdc595002015-12-21 11:13:52 +01003237.. js:attribute:: AppletHTTP.c
3238
3239 :returns: A :ref:`converters_class`
3240
3241 This attribute contains a Converters class object.
3242
3243.. js:attribute:: AppletHTTP.sc
3244
3245 :returns: A :ref:`converters_class`
3246
3247 This attribute contains a Converters class object. The
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02003248 functions of this object always return a string.
Thierry FOURNIERdc595002015-12-21 11:13:52 +01003249
3250.. js:attribute:: AppletHTTP.f
3251
3252 :returns: A :ref:`fetches_class`
3253
3254 This attribute contains a Fetches class object. Note that the
3255 applet execution place cannot access to a valid HAProxy core HTTP
Ilya Shipitsin2075ca82020-03-06 23:22:22 +05003256 transaction, so some sample fetches related to the HTTP dependent
Thierry FOURNIERdc595002015-12-21 11:13:52 +01003257 values (hdr, path, ...) are not available.
3258
3259.. js:attribute:: AppletHTTP.sf
3260
3261 :returns: A :ref:`fetches_class`
3262
3263 This attribute contains a Fetches class object. The functions of
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02003264 this object always return a string. Note that the applet
Thierry FOURNIERdc595002015-12-21 11:13:52 +01003265 execution place cannot access to a valid HAProxy core HTTP
Ilya Shipitsin2075ca82020-03-06 23:22:22 +05003266 transaction, so some sample fetches related to the HTTP dependent
Thierry FOURNIERdc595002015-12-21 11:13:52 +01003267 values (hdr, path, ...) are not available.
3268
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01003269.. js:attribute:: AppletHTTP.method
Thierry FOURNIERdc595002015-12-21 11:13:52 +01003270
3271 :returns: string
3272
3273 The attribute method returns a string containing the HTTP
3274 method.
3275
3276.. js:attribute:: AppletHTTP.version
3277
3278 :returns: string
3279
3280 The attribute version, returns a string containing the HTTP
3281 request version.
3282
3283.. js:attribute:: AppletHTTP.path
3284
3285 :returns: string
3286
3287 The attribute path returns a string containing the HTTP
3288 request path.
3289
3290.. js:attribute:: AppletHTTP.qs
3291
3292 :returns: string
3293
3294 The attribute qs returns a string containing the HTTP
3295 request query string.
3296
3297.. js:attribute:: AppletHTTP.length
3298
3299 :returns: integer
3300
3301 The attribute length returns an integer containing the HTTP
3302 body length.
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02003303
Thierry FOURNIER841475e2015-12-11 17:10:09 +01003304.. js:attribute:: AppletHTTP.headers
3305
Patrick Hemmerc6a1d712018-05-01 21:30:41 -04003306 :returns: table
Thierry FOURNIERdc595002015-12-21 11:13:52 +01003307
Patrick Hemmerc6a1d712018-05-01 21:30:41 -04003308 The attribute headers returns a table containing the HTTP
Thierry FOURNIERdc595002015-12-21 11:13:52 +01003309 headers. The header names are always in lower case. As the header name can be
3310 encountered more than once in each request, the value is indexed with 0 as
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02003311 first index value. The table has this form:
Thierry FOURNIERdc595002015-12-21 11:13:52 +01003312
3313.. code-block:: lua
3314
3315 AppletHTTP.headers['<header-name>'][<header-index>] = "<header-value>"
3316
3317 AppletHTTP.headers["host"][0] = "www.test.com"
3318 AppletHTTP.headers["accept"][0] = "audio/basic q=1"
3319 AppletHTTP.headers["accept"][1] = "audio/*, q=0.2"
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01003320 AppletHTTP.headers["accept"][2] = "*/*, q=0.1"
Thierry FOURNIERdc595002015-12-21 11:13:52 +01003321..
3322
Robin H. Johnson52f5db22017-01-01 13:10:52 -08003323.. js:function:: AppletHTTP.set_status(applet, code [, reason])
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02003324
3325 This function sets the HTTP status code for the response. The allowed code are
3326 from 100 to 599.
3327
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01003328 :param class_AppletHTTP applet: An :ref:`applethttp_class`
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02003329 :param integer code: the status code returned to the client.
Robin H. Johnson52f5db22017-01-01 13:10:52 -08003330 :param string reason: the status reason returned to the client (optional).
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02003331
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01003332.. js:function:: AppletHTTP.add_header(applet, name, value)
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02003333
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02003334 This function adds a header in the response. Duplicated headers are not
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02003335 collapsed. The special header *content-length* is used to determinate the
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02003336 response length. If it does not exist, a *transfer-encoding: chunked* is set,
3337 and all the write from the function *AppletHTTP:send()* become a chunk.
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02003338
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01003339 :param class_AppletHTTP applet: An :ref:`applethttp_class`
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02003340 :param string name: the header name
3341 :param string value: the header value
3342
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01003343.. js:function:: AppletHTTP.start_response(applet)
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02003344
3345 This function indicates to the HTTP engine that it can process and send the
3346 response headers. After this called we cannot add headers to the response; We
3347 cannot use the *AppletHTTP:send()* function if the
3348 *AppletHTTP:start_response()* is not called.
3349
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01003350 :param class_AppletHTTP applet: An :ref:`applethttp_class`
3351
3352.. js:function:: AppletHTTP.getline(applet)
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02003353
3354 This function returns a string containing one line from the http body. If the
3355 data returned doesn't contains a final '\\n' its assumed than its the last
3356 available data before the end of stream.
3357
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01003358 :param class_AppletHTTP applet: An :ref:`applethttp_class`
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02003359 :returns: a string. The string can be empty if we reach the end of the stream.
3360
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01003361.. js:function:: AppletHTTP.receive(applet, [size])
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02003362
3363 Reads data from the HTTP body, according to the specified read *size*. If the
3364 *size* is missing, the function tries to read all the content of the stream
3365 until the end. If the *size* is bigger than the http body, it returns the
Bertrand Jacquin874a35c2018-09-10 21:26:07 +01003366 amount of data available.
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02003367
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01003368 :param class_AppletHTTP applet: An :ref:`applethttp_class`
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02003369 :param integer size: the required read size.
Ilya Shipitsin11057a32020-06-21 21:18:27 +05003370 :returns: always return a string,the string can be empty is the connection is
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02003371 closed.
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02003372
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01003373.. js:function:: AppletHTTP.send(applet, msg)
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02003374
3375 Send the message *msg* on the http request body.
3376
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01003377 :param class_AppletHTTP applet: An :ref:`applethttp_class`
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02003378 :param string msg: the message to send.
3379
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01003380.. js:function:: AppletHTTP.get_priv(applet)
3381
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01003382 Return Lua data stored in the current transaction. If no data are stored,
3383 it returns a nil value.
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01003384
3385 :param class_AppletHTTP applet: An :ref:`applethttp_class`
Bertrand Jacquin874a35c2018-09-10 21:26:07 +01003386 :returns: the opaque data previously stored, or nil if nothing is
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02003387 available.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01003388 :see: :js:func:`AppletHTTP.set_priv`
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01003389
3390.. js:function:: AppletHTTP.set_priv(applet, data)
3391
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02003392 Store any data in the current HAProxy transaction. This action replaces the
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01003393 old stored data.
3394
3395 :param class_AppletHTTP applet: An :ref:`applethttp_class`
3396 :param opaque data: The data which is stored in the transaction.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01003397 :see: :js:func:`AppletHTTP.get_priv`
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01003398
Tim Duesterhus4e172c92020-05-19 13:49:42 +02003399.. js:function:: AppletHTTP.set_var(applet, var, value[, ifexist])
Thierry FOURNIER / OZON.IOc1edafe2016-12-12 16:25:30 +01003400
3401 Converts a Lua type in a HAProxy type and store it in a variable <var>.
3402
3403 :param class_AppletHTTP applet: An :ref:`applethttp_class`
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02003404 :param string var: The variable name according with the HAProxy variable
3405 syntax.
3406 :param type value: The value associated to the variable. The type ca be string
3407 or integer.
3408 :param boolean ifexist: If this parameter is set to true the variable will
3409 only be set if it was defined elsewhere (i.e. used within the configuration).
3410 For global variables (using the "proc" scope), they will only be updated and
3411 never created. It is highly recommended to always set this to true.
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02003412
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01003413 :see: :js:func:`AppletHTTP.unset_var`
3414 :see: :js:func:`AppletHTTP.get_var`
Thierry FOURNIER / OZON.IOc1edafe2016-12-12 16:25:30 +01003415
3416.. js:function:: AppletHTTP.unset_var(applet, var)
3417
3418 Unset the variable <var>.
3419
3420 :param class_AppletHTTP applet: An :ref:`applethttp_class`
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02003421 :param string var: The variable name according with the HAProxy variable
3422 syntax.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01003423 :see: :js:func:`AppletHTTP.set_var`
3424 :see: :js:func:`AppletHTTP.get_var`
Thierry FOURNIER / OZON.IOc1edafe2016-12-12 16:25:30 +01003425
3426.. js:function:: AppletHTTP.get_var(applet, var)
3427
3428 Returns data stored in the variable <var> converter in Lua type.
3429
3430 :param class_AppletHTTP applet: An :ref:`applethttp_class`
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02003431 :param string var: The variable name according with the HAProxy variable
3432 syntax.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01003433 :see: :js:func:`AppletHTTP.set_var`
3434 :see: :js:func:`AppletHTTP.unset_var`
Thierry FOURNIER / OZON.IOc1edafe2016-12-12 16:25:30 +01003435
Thierry FOURNIERdc595002015-12-21 11:13:52 +01003436.. _applettcp_class:
3437
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02003438AppletTCP class
3439===============
3440
3441.. js:class:: AppletTCP
3442
3443 This class is used with applets that requires the 'tcp' mode. The tcp applet
3444 can be registered with the *core.register_service()* function. They are used
3445 for processing a tcp stream like a server in back of HAProxy.
3446
Thierry FOURNIERdc595002015-12-21 11:13:52 +01003447.. js:attribute:: AppletTCP.c
3448
3449 :returns: A :ref:`converters_class`
3450
3451 This attribute contains a Converters class object.
3452
3453.. js:attribute:: AppletTCP.sc
3454
3455 :returns: A :ref:`converters_class`
3456
3457 This attribute contains a Converters class object. The
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02003458 functions of this object always return a string.
Thierry FOURNIERdc595002015-12-21 11:13:52 +01003459
3460.. js:attribute:: AppletTCP.f
3461
3462 :returns: A :ref:`fetches_class`
3463
3464 This attribute contains a Fetches class object.
3465
3466.. js:attribute:: AppletTCP.sf
3467
3468 :returns: A :ref:`fetches_class`
3469
3470 This attribute contains a Fetches class object.
3471
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01003472.. js:function:: AppletTCP.getline(applet)
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02003473
3474 This function returns a string containing one line from the stream. If the
3475 data returned doesn't contains a final '\\n' its assumed than its the last
3476 available data before the end of stream.
3477
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01003478 :param class_AppletTCP applet: An :ref:`applettcp_class`
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02003479 :returns: a string. The string can be empty if we reach the end of the stream.
3480
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01003481.. js:function:: AppletTCP.receive(applet, [size])
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02003482
3483 Reads data from the TCP stream, according to the specified read *size*. If the
3484 *size* is missing, the function tries to read all the content of the stream
3485 until the end.
3486
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01003487 :param class_AppletTCP applet: An :ref:`applettcp_class`
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02003488 :param integer size: the required read size.
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02003489 :returns: always return a string, the string can be empty if the connection is
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02003490 closed.
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02003491
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01003492.. js:function:: AppletTCP.send(appletmsg)
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02003493
3494 Send the message on the stream.
3495
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01003496 :param class_AppletTCP applet: An :ref:`applettcp_class`
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02003497 :param string msg: the message to send.
3498
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01003499.. js:function:: AppletTCP.get_priv(applet)
3500
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01003501 Return Lua data stored in the current transaction. If no data are stored,
3502 it returns a nil value.
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01003503
3504 :param class_AppletTCP applet: An :ref:`applettcp_class`
Bertrand Jacquin874a35c2018-09-10 21:26:07 +01003505 :returns: the opaque data previously stored, or nil if nothing is
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02003506 available.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01003507 :see: :js:func:`AppletTCP.set_priv`
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01003508
3509.. js:function:: AppletTCP.set_priv(applet, data)
3510
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02003511 Store any data in the current HAProxy transaction. This action replaces the
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01003512 old stored data.
3513
3514 :param class_AppletTCP applet: An :ref:`applettcp_class`
3515 :param opaque data: The data which is stored in the transaction.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01003516 :see: :js:func:`AppletTCP.get_priv`
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01003517
Tim Duesterhus4e172c92020-05-19 13:49:42 +02003518.. js:function:: AppletTCP.set_var(applet, var, value[, ifexist])
Thierry FOURNIER / OZON.IOc1edafe2016-12-12 16:25:30 +01003519
3520 Converts a Lua type in a HAProxy type and stores it in a variable <var>.
3521
3522 :param class_AppletTCP applet: An :ref:`applettcp_class`
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02003523 :param string var: The variable name according with the HAProxy variable
3524 syntax.
3525 :param type value: The value associated to the variable. The type can be
3526 string or integer.
3527 :param boolean ifexist: If this parameter is set to true the variable will
3528 only be set if it was defined elsewhere (i.e. used within the configuration).
3529 For global variables (using the "proc" scope), they will only be updated and
3530 never created. It is highly recommended to always set this to true.
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02003531
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01003532 :see: :js:func:`AppletTCP.unset_var`
3533 :see: :js:func:`AppletTCP.get_var`
Thierry FOURNIER / OZON.IOc1edafe2016-12-12 16:25:30 +01003534
3535.. js:function:: AppletTCP.unset_var(applet, var)
3536
3537 Unsets the variable <var>.
3538
3539 :param class_AppletTCP applet: An :ref:`applettcp_class`
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02003540 :param string var: The variable name according with the HAProxy variable
3541 syntax.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01003542 :see: :js:func:`AppletTCP.unset_var`
3543 :see: :js:func:`AppletTCP.set_var`
Thierry FOURNIER / OZON.IOc1edafe2016-12-12 16:25:30 +01003544
3545.. js:function:: AppletTCP.get_var(applet, var)
3546
3547 Returns data stored in the variable <var> converter in Lua type.
3548
3549 :param class_AppletTCP applet: An :ref:`applettcp_class`
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02003550 :param string var: The variable name according with the HAProxy variable
3551 syntax.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01003552 :see: :js:func:`AppletTCP.unset_var`
3553 :see: :js:func:`AppletTCP.set_var`
Thierry FOURNIER / OZON.IOc1edafe2016-12-12 16:25:30 +01003554
Adis Nezirovic8878f8e2018-07-13 12:18:33 +02003555StickTable class
3556================
3557
3558.. js:class:: StickTable
3559
3560 **context**: task, action, sample-fetch
3561
3562 This class can be used to access the HAProxy stick tables from Lua.
3563
3564.. js:function:: StickTable.info()
3565
3566 Returns stick table attributes as a Lua table. See HAProxy documentation for
Ilya Shipitsin2272d8a2020-12-21 01:22:40 +05003567 "stick-table" for canonical info, or check out example below.
Adis Nezirovic8878f8e2018-07-13 12:18:33 +02003568
3569 :returns: Lua table
3570
3571 Assume our table has IPv4 key and gpc0 and conn_rate "columns":
3572
3573.. code-block:: lua
3574
3575 {
3576 expire=<int>, # Value in ms
3577 size=<int>, # Maximum table size
3578 used=<int>, # Actual number of entries in table
3579 data={ # Data columns, with types as key, and periods as values
3580 (-1 if type is not rate counter)
3581 conn_rate=<int>,
3582 gpc0=-1
3583 },
3584 length=<int>, # max string length for string table keys, key length
3585 # otherwise
3586 nopurge=<boolean>, # purge oldest entries when table is full
3587 type="ip" # can be "ip", "ipv6", "integer", "string", "binary"
3588 }
3589
3590.. js:function:: StickTable.lookup(key)
3591
3592 Returns stick table entry for given <key>
3593
3594 :param string key: Stick table key (IP addresses and strings are supported)
3595 :returns: Lua table
3596
3597.. js:function:: StickTable.dump([filter])
3598
3599 Returns all entries in stick table. An optional filter can be used
3600 to extract entries with specific data values. Filter is a table with valid
3601 comparison operators as keys followed by data type name and value pairs.
3602 Check out the HAProxy docs for "show table" for more details. For the
3603 reference, the supported operators are:
Aurelien DARRAGON21f7ebb2023-03-13 19:49:31 +01003604
Adis Nezirovic8878f8e2018-07-13 12:18:33 +02003605 "eq", "ne", "le", "lt", "ge", "gt"
3606
3607 For large tables, execution of this function can take a long time (for
3608 HAProxy standards). That's also true when filter is used, so take care and
3609 measure the impact.
3610
3611 :param table filter: Stick table filter
3612 :returns: Stick table entries (table)
3613
3614 See below for example filter, which contains 4 entries (or comparisons).
3615 (Maximum number of filter entries is 4, defined in the source code)
3616
3617.. code-block:: lua
3618
3619 local filter = {
3620 {"gpc0", "gt", 30}, {"gpc1", "gt", 20}}, {"conn_rate", "le", 10}
3621 }
3622
Christopher Faulet0f3c8902020-01-31 18:57:12 +01003623.. _action_class:
3624
3625Action class
3626=============
3627
3628.. js:class:: Act
3629
3630 **context**: action
3631
3632 This class contains all return codes an action may return. It is the lua
3633 equivalent to HAProxy "ACT_RET_*" code.
3634
3635.. code-block:: lua
3636
3637 core.register_action("deny", { "http-req" }, function (txn)
3638 return act.DENY
3639 end)
3640..
3641.. js:attribute:: act.CONTINUE
3642
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02003643 This attribute is an integer (0). It instructs HAProxy to continue the
3644 current ruleset processing on the message. It is the default return code
3645 for a lua action.
Christopher Faulet0f3c8902020-01-31 18:57:12 +01003646
3647 :returns: integer
3648
3649.. js:attribute:: act.STOP
3650
3651 This attribute is an integer (1). It instructs HAProxy to stop the current
3652 ruleset processing on the message.
3653
3654.. js:attribute:: act.YIELD
3655
3656 This attribute is an integer (2). It instructs HAProxy to temporarily pause
3657 the message processing. It will be resumed later on the same rule. The
3658 corresponding lua script is re-executed for the start.
3659
3660.. js:attribute:: act.ERROR
3661
3662 This attribute is an integer (3). It triggers an internal errors The message
3663 processing is stopped and the transaction is terminated. For HTTP streams, an
3664 HTTP 500 error is returned to the client.
3665
3666 :returns: integer
3667
3668.. js:attribute:: act.DONE
3669
3670 This attribute is an integer (4). It instructs HAProxy to stop the message
3671 processing.
3672
3673 :returns: integer
3674
3675.. js:attribute:: act.DENY
3676
3677 This attribute is an integer (5). It denies the current message. The message
3678 processing is stopped and the transaction is terminated. For HTTP streams, an
3679 HTTP 403 error is returned to the client if the deny is returned during the
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02003680 request analysis. During the response analysis, a HTTP 502 error is returned
Christopher Faulet0f3c8902020-01-31 18:57:12 +01003681 and the server response is discarded.
3682
3683 :returns: integer
3684
3685.. js:attribute:: act.ABORT
3686
3687 This attribute is an integer (6). It aborts the current message. The message
3688 processing is stopped and the transaction is terminated. For HTTP streams,
Willy Tarreau714f3452021-05-09 06:47:26 +02003689 HAProxy assumes a response was already sent to the client. From the Lua
Christopher Faulet0f3c8902020-01-31 18:57:12 +01003690 actions point of view, when this code is used, the transaction is terminated
3691 with no reply.
3692
3693 :returns: integer
3694
3695.. js:attribute:: act.INVALID
3696
3697 This attribute is an integer (7). It triggers an internal errors. The message
3698 processing is stopped and the transaction is terminated. For HTTP streams, an
3699 HTTP 400 error is returned to the client if the error is returned during the
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02003700 request analysis. During the response analysis, a HTTP 502 error is returned
Christopher Faulet0f3c8902020-01-31 18:57:12 +01003701 and the server response is discarded.
3702
3703 :returns: integer
Adis Nezirovic8878f8e2018-07-13 12:18:33 +02003704
Christopher Faulet2c2c2e32020-01-31 19:07:52 +01003705.. js:function:: act:wake_time(milliseconds)
3706
3707 **context**: action
3708
3709 Set the script pause timeout to the specified time, defined in
3710 milliseconds.
3711
3712 :param integer milliseconds: the required milliseconds.
3713
3714 This function may be used when a lua action returns `act.YIELD`, to force its
3715 wake-up at most after the specified number of milliseconds.
3716
Christopher Faulet5a2c6612021-08-15 20:35:25 +02003717.. _filter_class:
3718
3719Filter class
3720=============
3721
3722.. js:class:: filter
3723
3724 **context**: filter
3725
3726 This class contains return codes some filter callback functions may return. It
3727 also contains configuration flags and some helper functions. To understand how
3728 the filter API works, see `doc/internal/filters.txt` documentation.
3729
3730.. js:attribute:: filter.CONTINUE
3731
3732 This attribute is an integer (1). It may be returned by some filter callback
3733 functions to instruct this filtering step is finished for this filter.
3734
3735.. js:attribute:: filter.WAIT
3736
3737 This attribute is an integer (0). It may be returned by some filter callback
3738 functions to instruct the filtering must be paused, waiting for more data or
3739 for an external event depending on this filter.
3740
3741.. js:attribute:: filter.ERROR
3742
3743 This attribute is an integer (-1). It may be returned by some filter callback
3744 functions to trigger an error.
3745
3746.. js:attribute:: filter.FLT_CFG_FL_HTX
3747
3748 This attribute is a flag corresponding to the filter flag FLT_CFG_FL_HTX. When
3749 it is set for a filter, it means the filter is able to filter HTTP streams.
3750
3751.. js:function:: filter.register_data_filter(chn)
3752
3753 **context**: filter
3754
3755 Enable the data filtering on the channel **chn** for the current filter. It
Ilya Shipitsinff0f2782021-08-22 22:18:07 +05003756 may be called at any time from any callback functions proceeding the data
Christopher Faulet5a2c6612021-08-15 20:35:25 +02003757 analysis.
3758
3759 :param class_Channel chn: A :ref:`channel_class`.
3760
3761.. js:function:: filter.unregister_data_filter(chn)
3762
3763 **context**: filter
3764
3765 Disable the data filtering on the channel **chn** for the current filter. It
3766 may be called at any time from any callback functions.
3767
3768 :param class_Channel chn: A :ref:`channel_class`.
3769
3770.. js:function:: filter.wake_time(milliseconds)
3771
3772 **context**: filter
3773
3774 Set the script pause timeout to the specified time, defined in
3775 milliseconds.
3776
3777 :param integer milliseconds: the required milliseconds.
3778
3779 This function may be used from any lua filter callback function to force its
3780 wake-up at most after the specified number of milliseconds. Especially, when
3781 `filter.CONTINUE` is returned.
3782
3783
3784A filters is declared using :js:func:`core.register_filter()` function. The
3785provided class will be used to instantiate filters. It may define following
3786attributes:
3787
3788* id: The filter identifier. It is a string that identifies the filter and is
3789 optional.
3790
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02003791* flags: The filter flags. Only :js:attr:`filter.FLT_CFG_FL_HTX` may be set
3792 for now.
Christopher Faulet5a2c6612021-08-15 20:35:25 +02003793
3794Such filter class must also define all required callback functions in the
3795following list. Note that :js:func:`Filter.new()` must be defined otherwise the
Ilya Shipitsinff0f2782021-08-22 22:18:07 +05003796filter is ignored. Others are optional.
Christopher Faulet5a2c6612021-08-15 20:35:25 +02003797
3798* .. js:function:: FILTER.new()
3799
3800 Called to instantiate a new filter. This function must be defined.
3801
3802 :returns: a Lua object that will be used as filter instance for the current
3803 stream.
3804
3805* .. js:function:: FILTER.start_analyze(flt, txn, chn)
3806
3807 Called when the analysis starts on the channel **chn**.
3808
3809* .. js:function:: FILTER.end_analyze(flt, txn, chn)
3810
3811 Called when the analysis ends on the channel **chn**.
3812
3813* .. js:function:: FILTER.http_headers(flt, txn, http_msg)
3814
3815 Called just before the HTTP payload analysis and after any processing on the
3816 HTTP message **http_msg**. This callback functions is only called for HTTP
3817 streams.
3818
3819* .. js:function:: FILTER.http_payload(flt, txn, http_msg)
3820
3821 Called during the HTTP payload analysis on the HTTP message **http_msg**. This
3822 callback functions is only called for HTTP streams.
3823
3824* .. js:function:: FILTER.http_end(flt, txn, http_msg)
3825
3826 Called after the HTTP payload analysis on the HTTP message **http_msg**. This
3827 callback functions is only called for HTTP streams.
3828
3829* .. js:function:: FILTER.tcp_payload(flt, txn, chn)
3830
3831 Called during the TCP payload analysis on the channel **chn**.
3832
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02003833Here is a full example:
Christopher Faulet5a2c6612021-08-15 20:35:25 +02003834
3835.. code-block:: lua
3836
3837 Trace = {}
3838 Trace.id = "Lua trace filter"
3839 Trace.flags = filter.FLT_CFG_FL_HTX;
3840 Trace.__index = Trace
3841
3842 function Trace:new()
3843 local trace = {}
3844 setmetatable(trace, Trace)
3845 trace.req_len = 0
3846 trace.res_len = 0
3847 return trace
3848 end
3849
3850 function Trace:start_analyze(txn, chn)
3851 if chn:is_resp() then
3852 print("Start response analysis")
3853 else
3854 print("Start request analysis")
3855 end
3856 filter.register_data_filter(self, chn)
3857 end
3858
3859 function Trace:end_analyze(txn, chn)
3860 if chn:is_resp() then
3861 print("End response analysis: "..self.res_len.." bytes filtered")
3862 else
3863 print("End request analysis: "..self.req_len.." bytes filtered")
3864 end
3865 end
3866
3867 function Trace:http_headers(txn, http_msg)
3868 stline = http_msg:get_stline()
3869 if http_msg.channel:is_resp() then
3870 print("response:")
3871 print(stline.version.." "..stline.code.." "..stline.reason)
3872 else
3873 print("request:")
3874 print(stline.method.." "..stline.uri.." "..stline.version)
3875 end
3876
3877 for n, hdrs in pairs(http_msg:get_headers()) do
3878 for i,v in pairs(hdrs) do
3879 print(n..": "..v)
3880 end
3881 end
3882 return filter.CONTINUE
3883 end
3884
3885 function Trace:http_payload(txn, http_msg)
3886 body = http_msg:body(-20000)
3887 if http_msg.channel:is_resp() then
3888 self.res_len = self.res_len + body:len()
3889 else
3890 self.req_len = self.req_len + body:len()
3891 end
3892 end
3893
3894 core.register_filter("trace", Trace, function(trace, args)
3895 return trace
3896 end)
3897
3898..
3899
3900.. _httpmessage_class:
3901
3902HTTPMessage class
3903===================
3904
3905.. js:class:: HTTPMessage
3906
3907 **context**: filter
3908
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02003909 This class contains all functions to manipulate a HTTP message. For now, this
Christopher Faulet5a2c6612021-08-15 20:35:25 +02003910 class is only available from a filter context.
3911
3912.. js:function:: HTTPMessage.add_header(http_msg, name, value)
3913
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02003914 Appends a HTTP header field in the HTTP message **http_msg** whose name is
Christopher Faulet5a2c6612021-08-15 20:35:25 +02003915 specified in **name** and whose value is defined in **value**.
3916
3917 :param class_httpmessage http_msg: The manipulated HTTP message.
3918 :param string name: The header name.
3919 :param string value: The header value.
3920
3921.. js:function:: HTTPMessage.append(http_msg, string)
3922
3923 This function copies the string **string** at the end of incoming data of the
3924 HTTP message **http_msg**. The function returns the copied length on success
3925 or -1 if data cannot be copied.
3926
3927 Same that :js:func:`HTTPMessage.insert(http_msg, string, http_msg:input())`.
3928
3929 :param class_httpmessage http_msg: The manipulated HTTP message.
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02003930 :param string string: The data to copy at the end of incoming data.
Christopher Faulet5a2c6612021-08-15 20:35:25 +02003931 :returns: an integer containing the amount of bytes copied or -1.
3932
3933.. js:function:: HTTPMessage.body(http_msgl[, offset[, length]])
3934
3935 This function returns **length** bytes of incoming data from the HTTP message
3936 **http_msg**, starting at the offset **offset**. The data are not removed from
3937 the buffer.
3938
3939 By default, if no length is provided, all incoming data found, starting at the
3940 given offset, are returned. If **length** is set to -1, the function tries to
3941 retrieve a maximum of data. Because it is called in the filter context, it
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02003942 never yield. Not providing an offset is the same as setting it to 0. A
Ilya Shipitsinff0f2782021-08-22 22:18:07 +05003943 positive offset is relative to the beginning of incoming data of the
Christopher Faulet5a2c6612021-08-15 20:35:25 +02003944 http_message buffer while negative offset is relative to their end.
3945
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02003946 If there is no incoming data and the HTTP message can't receive more data,
3947 a 'nil' value is returned.
Christopher Faulet5a2c6612021-08-15 20:35:25 +02003948
3949 :param class_httpmessage http_msg: The manipulated HTTP message.
3950 :param integer offset: *optional* The offset in incoming data to start to get
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02003951 data. 0 by default. May be negative to be relative to the end of incoming
3952 data.
3953 :param integer length: *optional* The expected length of data to retrieve.
3954 All incoming data by default. May be set to -1 to get a maximum of data.
Christopher Faulet5a2c6612021-08-15 20:35:25 +02003955 :returns: a string containing the data found or nil.
3956
3957.. js:function:: HTTPMessage.eom(http_msg)
3958
3959 This function returns true if the end of message is reached for the HTTP
3960 message **http_msg**.
3961
3962 :param class_httpmessage http_msg: The manipulated HTTP message.
3963 :returns: an integer containing the amount of available bytes.
3964
3965.. js:function:: HTTPMessage.del_header(http_msg, name)
3966
3967 Removes all HTTP header fields in the HTTP message **http_msg** whose name is
3968 specified in **name**.
3969
3970 :param class_httpmessage http_msg: The manipulated http message.
3971 :param string name: The header name.
3972
3973.. js:function:: HTTPMessage.get_headers(http_msg)
3974
3975 Returns a table containing all the headers of the HTTP message **http_msg**.
3976
3977 :param class_httpmessage http_msg: The manipulated http message.
3978 :returns: table of headers.
3979
3980 This is the form of the returned table:
3981
3982.. code-block:: lua
3983
3984 http_msg:get_headers()['<header-name>'][<header-index>] = "<header-value>"
3985
3986 local hdr = http_msg:get_headers()
3987 hdr["host"][0] = "www.test.com"
3988 hdr["accept"][0] = "audio/basic q=1"
3989 hdr["accept"][1] = "audio/*, q=0.2"
3990 hdr["accept"][2] = "*.*, q=0.1"
3991..
3992
3993.. js:function:: HTTPMessage.get_stline(http_msg)
3994
3995 Returns a table containing the start-line of the HTTP message **http_msg**.
3996
3997 :param class_httpmessage http_msg: The manipulated http message.
3998 :returns: the start-line.
3999
4000 This is the form of the returned table:
4001
4002.. code-block:: lua
4003
4004 -- for the request :
4005 {"method" = string, "uri" = string, "version" = string}
4006
4007 -- for the response:
4008 {"version" = string, "code" = string, "reason" = string}
4009..
4010
4011.. js:function:: HTTPMessage.forward(http_msg, length)
4012
4013 This function forwards **length** bytes of data from the HTTP message
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02004014 **http_msg**. Because it is called in the filter context, it never yields. Only
Christopher Faulet5a2c6612021-08-15 20:35:25 +02004015 available incoming data may be forwarded, event if the requested length
4016 exceeds the available amount of incoming data. It returns the amount of data
4017 forwarded.
4018
4019 :param class_httpmessage http_msg: The manipulated HTTP message.
4020 :param integer int: The amount of data to forward.
4021
4022.. js:function:: HTTPMessage.input(http_msg)
4023
4024 This function returns the length of incoming data in the HTTP message
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02004025 **http_msg** from the filter point of view.
Christopher Faulet5a2c6612021-08-15 20:35:25 +02004026
4027 :param class_httpmessage http_msg: The manipulated HTTP message.
4028 :returns: an integer containing the amount of available bytes.
4029
4030.. js:function:: HTTPMessage.insert(http_msg, string[, offset])
4031
4032 This function copies the string **string** at the offset **offset** in
4033 incoming data of the HTTP message **http_msg**. The function returns the
4034 copied length on success or -1 if data cannot be copied.
4035
4036 By default, if no offset is provided, the string is copied in front of
Ilya Shipitsinff0f2782021-08-22 22:18:07 +05004037 incoming data. A positive offset is relative to the beginning of incoming data
Christopher Faulet5a2c6612021-08-15 20:35:25 +02004038 of the HTTP message while negative offset is relative to their end.
4039
4040 :param class_httpmessage http_msg: The manipulated HTTP message.
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02004041 :param string string: The data to copy into incoming data.
4042 :param integer offset: *optional* The offset in incoming data where to copy
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02004043 data. 0 by default. May be negative to be relative to the end of incoming
4044 data.
Christopher Faulet5a2c6612021-08-15 20:35:25 +02004045 :returns: an integer containing the amount of bytes copied or -1.
4046
4047.. js:function:: HTTPMessage.is_full(http_msg)
4048
4049 This function returns true if the HTTP message **http_msg** is full.
4050
4051 :param class_httpmessage http_msg: The manipulated HTTP message.
4052 :returns: a boolean
4053
4054.. js:function:: HTTPMessage.is_resp(http_msg)
4055
4056 This function returns true if the HTTP message **http_msg** is the response
4057 one.
4058
4059 :param class_httpmessage http_msg: The manipulated HTTP message.
4060 :returns: a boolean
4061
4062.. js:function:: HTTPMessage.may_recv(http_msg)
4063
4064 This function returns true if the HTTP message **http_msg** may still receive
4065 data.
4066
4067 :param class_httpmessage http_msg: The manipulated HTTP message.
4068 :returns: a boolean
4069
4070.. js:function:: HTTPMessage.output(http_msg)
4071
4072 This function returns the length of outgoing data of the HTTP message
4073 **http_msg**.
4074
4075 :param class_httpmessage http_msg: The manipulated HTTP message.
4076 :returns: an integer containing the amount of available bytes.
4077
4078.. js:function:: HTTPMessage.prepend(http_msg, string)
4079
4080 This function copies the string **string** in front of incoming data of the
4081 HTTP message **http_msg**. The function returns the copied length on success
4082 or -1 if data cannot be copied.
4083
4084 Same that :js:func:`HTTPMessage.insert(http_msg, string, 0)`.
4085
4086 :param class_httpmessage http_msg: The manipulated HTTP message.
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02004087 :param string string: The data to copy in front of incoming data.
Christopher Faulet5a2c6612021-08-15 20:35:25 +02004088 :returns: an integer containing the amount of bytes copied or -1.
4089
4090.. js:function:: HTTPMessage.remove(http_msg[, offset[, length]])
4091
4092 This function removes **length** bytes of incoming data of the HTTP message
4093 **http_msg**, starting at offset **offset**. This function returns number of
4094 bytes removed on success.
4095
4096 By default, if no length is provided, all incoming data, starting at the given
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02004097 offset, are removed. Not providing an offset is the same that setting it
Ilya Shipitsinff0f2782021-08-22 22:18:07 +05004098 to 0. A positive offset is relative to the beginning of incoming data of the
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02004099 HTTP message while negative offset is relative to the end.
Christopher Faulet5a2c6612021-08-15 20:35:25 +02004100
4101 :param class_httpmessage http_msg: The manipulated HTTP message.
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02004102 :param integer offset: *optional* The offset in incoming data where to start
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02004103 to remove data. 0 by default. May be negative to be relative to the end of
4104 incoming data.
Christopher Faulet5a2c6612021-08-15 20:35:25 +02004105 :param integer length: *optional* The length of data to remove. All incoming
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02004106 data by default.
Christopher Faulet5a2c6612021-08-15 20:35:25 +02004107 :returns: an integer containing the amount of bytes removed.
4108
4109.. js:function:: HTTPMessage.rep_header(http_msg, name, regex, replace)
4110
4111 Matches the regular expression in all occurrences of header field **name**
4112 according to regex **regex**, and replaces them with the string **replace**.
4113 The replacement value can contain back references like \1, \2, ... This
4114 function acts on whole header lines, regardless of the number of values they
4115 may contain.
4116
4117 :param class_httpmessage http_msg: The manipulated HTTP message.
4118 :param string name: The header name.
4119 :param string regex: The match regular expression.
4120 :param string replace: The replacement value.
4121
4122.. js:function:: HTTPMessage.rep_value(http_msg, name, regex, replace)
4123
4124 Matches the regular expression on every comma-delimited value of header field
4125 **name** according to regex **regex**, and replaces them with the string
4126 **replace**. The replacement value can contain back references like \1, \2,
4127 ...
4128
4129 :param class_httpmessage http_msg: The manipulated HTTP message.
4130 :param string name: The header name.
4131 :param string regex: The match regular expression.
4132 :param string replace: The replacement value.
4133
4134.. js:function:: HTTPMessage.send(http_msg, string)
4135
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02004136 This function requires immediate send of the string **string**. It means the
Ilya Shipitsinff0f2782021-08-22 22:18:07 +05004137 string is copied at the beginning of incoming data of the HTTP message
Christopher Faulet5a2c6612021-08-15 20:35:25 +02004138 **http_msg** and immediately forwarded. Because it is called in the filter
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02004139 context, it never yields.
Christopher Faulet5a2c6612021-08-15 20:35:25 +02004140
4141 :param class_httpmessage http_msg: The manipulated HTTP message.
4142 :param string string: The data to send.
4143 :returns: an integer containing the amount of bytes copied or -1.
4144
4145.. js:function:: HTTPMessage.set(http_msg, string[, offset[, length]])
4146
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02004147 This function replaces **length** bytes of incoming data of the HTTP message
Christopher Faulet5a2c6612021-08-15 20:35:25 +02004148 **http_msg**, starting at offset **offset**, by the string **string**. The
4149 function returns the copied length on success or -1 if data cannot be copied.
4150
4151 By default, if no length is provided, all incoming data, starting at the given
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02004152 offset, are replaced. Not providing an offset is the same as setting it
Ilya Shipitsinff0f2782021-08-22 22:18:07 +05004153 to 0. A positive offset is relative to the beginning of incoming data of the
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02004154 HTTP message while negative offset is relative to the end.
Christopher Faulet5a2c6612021-08-15 20:35:25 +02004155
4156 :param class_httpmessage http_msg: The manipulated HTTP message.
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02004157 :param string string: The data to copy into incoming data.
4158 :param integer offset: *optional* The offset in incoming data where to start
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02004159 the data replacement. 0 by default. May be negative to be relative to the
4160 end of incoming data.
Christopher Faulet5a2c6612021-08-15 20:35:25 +02004161 :param integer length: *optional* The length of data to replace. All incoming
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02004162 data by default.
Christopher Faulet5a2c6612021-08-15 20:35:25 +02004163 :returns: an integer containing the amount of bytes copied or -1.
4164
4165.. js:function:: HTTPMessage.set_eom(http_msg)
4166
4167 This function set the end of message for the HTTP message **http_msg**.
4168
4169 :param class_httpmessage http_msg: The manipulated HTTP message.
4170
4171.. js:function:: HTTPMessage.set_header(http_msg, name, value)
4172
4173 This variable replace all occurrence of all header matching the name **name**,
4174 by only one containing the value **value**.
4175
4176 :param class_httpmessage http_msg: The manipulated HTTP message.
4177 :param string name: The header name.
4178 :param string value: The header value.
4179
4180 This function does the same work as the following code:
4181
4182.. code-block:: lua
4183
4184 http_msg:del_header("header")
4185 http_msg:add_header("header", "value")
4186..
4187
4188.. js:function:: HTTPMessage.set_method(http_msg, method)
4189
4190 Rewrites the request method with the string **method**. The HTTP message
4191 **http_msg** must be the request.
4192
4193 :param class_httpmessage http_msg: The manipulated HTTP message.
4194 :param string method: The new method.
4195
4196.. js:function:: HTTPMessage.set_path(http_msg, path)
4197
4198 Rewrites the request path with the string **path**. The HTTP message
4199 **http_msg** must be the request.
4200
4201 :param class_httpmessage http_msg: The manipulated HTTP message.
4202 :param string method: The new method.
4203
4204.. js:function:: HTTPMessage.set_query(http_msg, query)
4205
4206 Rewrites the request's query string which appears after the first question
4207 mark ("?") with the string **query**. The HTTP message **http_msg** must be
4208 the request.
4209
4210 :param class_httpmessage http_msg: The manipulated HTTP message.
4211 :param string query: The new query.
4212
4213.. js:function:: HTTPMessage.set_status(http_msg, status[, reason])
4214
Ilya Shipitsinff0f2782021-08-22 22:18:07 +05004215 Rewrites the response status code with the integer **code** and optional the
Christopher Faulet5a2c6612021-08-15 20:35:25 +02004216 reason **reason**. If no custom reason is provided, it will be generated from
4217 the status. The HTTP message **http_msg** must be the response.
4218
4219 :param class_httpmessage http_msg: The manipulated HTTP message.
4220 :param integer status: The new response status code.
4221 :param string reason: The new response reason (optional).
4222
4223.. js:function:: HTTPMessage.set_uri(http_msg, uri)
4224
4225 Rewrites the request URI with the string **uri**. The HTTP message
4226 **http_msg** must be the request.
4227
4228 :param class_httpmessage http_msg: The manipulated HTTP message.
4229 :param string uri: The new uri.
4230
4231.. js:function:: HTTPMessage.unset_eom(http_msg)
4232
4233 This function remove the end of message for the HTTP message **http_msg**.
4234
4235 :param class_httpmessage http_msg: The manipulated HTTP message.
4236
William Lallemand10cea5c2022-03-30 16:02:43 +02004237.. _CertCache_class:
4238
4239CertCache class
4240================
4241
4242.. js:class:: CertCache
4243
4244 This class allows to update an SSL certificate file in the memory of the
4245 current HAProxy process. It will do the same as "set ssl cert" + "commit ssl
4246 cert" over the HAProxy CLI.
4247
4248.. js:function:: CertCache.set(certificate)
4249
4250 This function updates a certificate in memory.
4251
4252 :param table certificate: A table containing the fields to update.
4253 :param string certificate.filename: The mandatory filename of the certificate
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02004254 to update, it must already exist in memory.
William Lallemand10cea5c2022-03-30 16:02:43 +02004255 :param string certificate.crt: A certificate in the PEM format. It can also
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02004256 contain a private key.
William Lallemand10cea5c2022-03-30 16:02:43 +02004257 :param string certificate.key: A private key in the PEM format.
4258 :param string certificate.ocsp: An OCSP response in base64. (cf management.txt)
4259 :param string certificate.issuer: The certificate of the OCSP issuer.
4260 :param string certificate.sctl: An SCTL file.
4261
4262.. code-block:: lua
4263
4264 CertCache.set{filename="certs/localhost9994.pem.rsa", crt=crt}
4265
4266
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01004267External Lua libraries
4268======================
4269
4270A lot of useful lua libraries can be found here:
4271
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02004272* Lua toolbox has been superseded by
4273 `https://luarocks.org/ <https://luarocks.org/>`_
4274
4275 The old lua toolbox source code is still available here
4276 `https://github.com/catwell/lua-toolbox <https://github.com/catwell/lua-toolbox>`_ (DEPRECATED)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01004277
Ilya Shipitsin2075ca82020-03-06 23:22:22 +05004278Redis client library:
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01004279
4280* `https://github.com/nrk/redis-lua <https://github.com/nrk/redis-lua>`_
4281
Aurelien DARRAGON2dac67a2023-04-20 12:16:17 +02004282This is an example about the usage of the Redis library within HAProxy.
4283Note that each call to any function of this library can throw an error if
4284the socket connection fails.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01004285
4286.. code-block:: lua
4287
4288 -- load the redis library
4289 local redis = require("redis");
4290
4291 function do_something(txn)
4292
4293 -- create and connect new tcp socket
4294 local tcp = core.tcp();
4295 tcp:settimeout(1);
4296 tcp:connect("127.0.0.1", 6379);
4297
4298 -- use the redis library with this new socket
4299 local client = redis.connect({socket=tcp});
4300 client:ping();
4301
4302 end
4303
4304OpenSSL:
4305
4306* `http://mkottman.github.io/luacrypto/index.html
4307 <http://mkottman.github.io/luacrypto/index.html>`_
4308
4309* `https://github.com/brunoos/luasec/wiki
4310 <https://github.com/brunoos/luasec/wiki>`_