blob: 06468eb29a4e853e8af43867182a76c4f493a15a [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
23functions. Lua have 6 execution context.
24
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
58 **NOTE**: It is possible that this function cannot found the required data
59 in the original HAProxy sample-fetches, in this case, it cannot return the
60 result. This case is not yet supported
61
David Carlier61fdf8b2015-10-02 11:59:38 +0100626. The **converter context**. It is a Lua function that takes a string as input
Thierry FOURNIER17bd1522015-03-11 20:31:00 +010063 and returns another string as output. These types of function are stateless,
64 it cannot access to any context. They don't execute any blocking function.
65 The call prototype is `function string fcn(string)`. This function can be
66 registered with the Lua function `core.register_converters()`. Each declared
67 converter is prefixed by the string "lua.".
68
69HAProxy Lua Hello world
70-----------------------
71
72HAProxy configuration file (`hello_world.conf`):
73
74::
75
76 global
77 lua-load hello_world.lua
78
79 listen proxy
80 bind 127.0.0.1:10001
Thierry FOURNIERa2d02252015-10-01 15:00:42 +020081 tcp-request inspect-delay 1s
82 tcp-request content use-service lua.hello_world
Thierry FOURNIER17bd1522015-03-11 20:31:00 +010083
84HAProxy Lua file (`hello_world.lua`):
85
86.. code-block:: lua
87
Thierry FOURNIERa2d02252015-10-01 15:00:42 +020088 core.register_service("hello_world", "tcp", function(applet)
89 applet:send("hello world\n")
90 end)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +010091
92How to start HAProxy for testing this configuration:
93
94::
95
96 ./haproxy -f hello_world.conf
97
98On other terminal, you can test with telnet:
99
100::
101
102 #:~ telnet 127.0.0.1 10001
103 hello world
104
105Core class
106==========
107
108.. js:class:: core
109
110 The "core" class contains all the HAProxy core functions. These function are
111 useful for the controlling the execution flow, registering hooks, manipulating
112 global maps or ACL, ...
113
114 "core" class is basically provided with HAProxy. No `require` line is
115 required to uses these function.
116
David Carlier61fdf8b2015-10-02 11:59:38 +0100117 The "core" class is static, it is not possible to create a new object of this
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100118 type.
119
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100120.. js:attribute:: core.emerg
121
Thierry FOURNIERdc595002015-12-21 11:13:52 +0100122 :returns: integer
123
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100124 This attribute is an integer, it contains the value of the loglevel "emergency" (0).
125
126.. js:attribute:: core.alert
127
Thierry FOURNIERdc595002015-12-21 11:13:52 +0100128 :returns: integer
129
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100130 This attribute is an integer, it contains the value of the loglevel "alert" (1).
131
132.. js:attribute:: core.crit
133
Thierry FOURNIERdc595002015-12-21 11:13:52 +0100134 :returns: integer
135
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100136 This attribute is an integer, it contains the value of the loglevel "critical" (2).
137
138.. js:attribute:: core.err
139
Thierry FOURNIERdc595002015-12-21 11:13:52 +0100140 :returns: integer
141
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100142 This attribute is an integer, it contains the value of the loglevel "error" (3).
143
144.. js:attribute:: core.warning
145
Thierry FOURNIERdc595002015-12-21 11:13:52 +0100146 :returns: integer
147
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100148 This attribute is an integer, it contains the value of the loglevel "warning" (4).
149
150.. js:attribute:: core.notice
151
Thierry FOURNIERdc595002015-12-21 11:13:52 +0100152 :returns: integer
153
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100154 This attribute is an integer, it contains the value of the loglevel "notice" (5).
155
156.. js:attribute:: core.info
157
Thierry FOURNIERdc595002015-12-21 11:13:52 +0100158 :returns: integer
159
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100160 This attribute is an integer, it contains the value of the loglevel "info" (6).
161
162.. js:attribute:: core.debug
163
Thierry FOURNIERdc595002015-12-21 11:13:52 +0100164 :returns: integer
165
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100166 This attribute is an integer, it contains the value of the loglevel "debug" (7).
167
Thierry FOURNIER12a865d2016-12-14 19:40:37 +0100168.. js:attribute:: core.proxies
169
170 **context**: task, action, sample-fetch, converter
171
Patrick Hemmerc6a1d712018-05-01 21:30:41 -0400172 This attribute is a table of declared proxies (frontend and backends). Each
173 proxy give an access to his list of listeners and servers. The table is
174 indexed by proxy name, and each entry is of type :ref:`proxy_class`.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +0100175
Thierry FOURNIER9b82a582017-07-24 13:30:43 +0200176 Warning, if you are declared frontend and backend with the same name, only one
177 of these are listed.
178
179 :see: :js:attr:`core.backends`
180 :see: :js:attr:`core.frontends`
181
182.. js:attribute:: core.backends
183
184 **context**: task, action, sample-fetch, converter
185
Patrick Hemmerc6a1d712018-05-01 21:30:41 -0400186 This attribute is a table of declared proxies with backend capability. Each
187 proxy give an access to his list of listeners and servers. The table is
188 indexed by the backend name, and each entry is of type :ref:`proxy_class`.
Thierry FOURNIER9b82a582017-07-24 13:30:43 +0200189
190 :see: :js:attr:`core.proxies`
191 :see: :js:attr:`core.frontends`
192
193.. js:attribute:: core.frontends
194
195 **context**: task, action, sample-fetch, converter
196
Patrick Hemmerc6a1d712018-05-01 21:30:41 -0400197 This attribute is a table of declared proxies with frontend capability. Each
198 proxy give an access to his list of listeners and servers. The table is
199 indexed by the frontend name, and each entry is of type :ref:`proxy_class`.
Thierry FOURNIER9b82a582017-07-24 13:30:43 +0200200
201 :see: :js:attr:`core.proxies`
202 :see: :js:attr:`core.backends`
203
Thierry Fournierecb83c22020-11-28 15:49:44 +0100204.. js:attribute:: core.thread
205
206 **context**: task, action, sample-fetch, converter, applet
207
208 This variable contains the executing thread number starting at 1. 0 is a
209 special case for the common lua context. So, if thread is 0, Lua scope is
210 shared by all threads, otherwise the scope is dedicated to a single thread.
211 A program which needs to execute some parts exactly once regardless of the
212 number of threads can check that core.thread is 0 or 1.
213
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100214.. js:function:: core.log(loglevel, msg)
215
216 **context**: body, init, task, action, sample-fetch, converter
217
David Carlier61fdf8b2015-10-02 11:59:38 +0100218 This function sends a log. The log is sent, according with the HAProxy
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100219 configuration file, on the default syslog server if it is configured and on
220 the stderr if it is allowed.
221
Bertrand Jacquin874a35c2018-09-10 21:26:07 +0100222 :param integer loglevel: Is the log level associated with the message. It is a
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100223 number between 0 and 7.
224 :param string msg: The log content.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +0100225 :see: :js:attr:`core.emerg`, :js:attr:`core.alert`, :js:attr:`core.crit`,
226 :js:attr:`core.err`, :js:attr:`core.warning`, :js:attr:`core.notice`,
227 :js:attr:`core.info`, :js:attr:`core.debug` (log level definitions)
228 :see: :js:func:`core.Debug`
229 :see: :js:func:`core.Info`
230 :see: :js:func:`core.Warning`
231 :see: :js:func:`core.Alert`
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100232
233.. js:function:: core.Debug(msg)
234
235 **context**: body, init, task, action, sample-fetch, converter
236
237 :param string msg: The log content.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +0100238 :see: :js:func:`core.log`
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100239
240 Does the same job than:
241
242.. code-block:: lua
243
Thierry FOURNIERdc595002015-12-21 11:13:52 +0100244 function Debug(msg)
245 core.log(core.debug, msg)
246 end
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100247..
248
249.. js:function:: core.Info(msg)
250
251 **context**: body, init, task, action, sample-fetch, converter
252
253 :param string msg: The log content.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +0100254 :see: :js:func:`core.log`
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100255
256.. code-block:: lua
257
Thierry FOURNIERdc595002015-12-21 11:13:52 +0100258 function Info(msg)
259 core.log(core.info, msg)
260 end
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100261..
262
263.. js:function:: core.Warning(msg)
264
265 **context**: body, init, task, action, sample-fetch, converter
266
267 :param string msg: The log content.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +0100268 :see: :js:func:`core.log`
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100269
270.. code-block:: lua
271
Thierry FOURNIERdc595002015-12-21 11:13:52 +0100272 function Warning(msg)
273 core.log(core.warning, msg)
274 end
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100275..
276
277.. js:function:: core.Alert(msg)
278
279 **context**: body, init, task, action, sample-fetch, converter
280
281 :param string msg: The log content.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +0100282 :see: :js:func:`core.log`
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100283
284.. code-block:: lua
285
Thierry FOURNIERdc595002015-12-21 11:13:52 +0100286 function Alert(msg)
287 core.log(core.alert, msg)
288 end
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100289..
290
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100291.. js:function:: core.add_acl(filename, key)
292
293 **context**: init, task, action, sample-fetch, converter
294
295 Add the ACL *key* in the ACLs list referenced by the file *filename*.
296
297 :param string filename: the filename that reference the ACL entries.
298 :param string key: the key which will be added.
299
300.. js:function:: core.del_acl(filename, key)
301
302 **context**: init, task, action, sample-fetch, converter
303
304 Delete the ACL entry referenced by the key *key* in the list of ACLs
305 referenced by *filename*.
306
307 :param string filename: the filename that reference the ACL entries.
308 :param string key: the key which will be deleted.
309
310.. js:function:: core.del_map(filename, key)
311
312 **context**: init, task, action, sample-fetch, converter
313
314 Delete the map entry indexed with the specified key in the list of maps
315 referenced by his filename.
316
317 :param string filename: the filename that reference the map entries.
318 :param string key: the key which will be deleted.
319
Thierry Fourniereea77c02016-03-18 08:47:13 +0100320.. js:function:: core.get_info()
321
322 **context**: body, init, task, action, sample-fetch, converter
323
Ilya Shipitsin2075ca82020-03-06 23:22:22 +0500324 Returns HAProxy core information. We can found information like the uptime,
Thierry Fourniereea77c02016-03-18 08:47:13 +0100325 the pid, memory pool usage, tasks number, ...
326
Bertrand Jacquin874a35c2018-09-10 21:26:07 +0100327 These information are also returned by the management socket via the command
328 "show info". See the management socket documentation for more information
Thierry Fourniereea77c02016-03-18 08:47:13 +0100329 about the content of these variables.
330
331 :returns: an array of values.
332
Thierry Fournierb1f46562016-01-21 09:46:15 +0100333.. js:function:: core.now()
334
335 **context**: body, init, task, action
336
337 This function returns the current time. The time returned is fixed by the
Bertrand Jacquin874a35c2018-09-10 21:26:07 +0100338 HAProxy core and assures than the hour will be monotonic and that the system
Thierry Fournierb1f46562016-01-21 09:46:15 +0100339 call 'gettimeofday' will not be called too. The time is refreshed between each
340 Lua execution or resume, so two consecutive call to the function "now" will
341 probably returns the same result.
342
Patrick Hemmerc6a1d712018-05-01 21:30:41 -0400343 :returns: a table which contains two entries "sec" and "usec". "sec"
Thierry Fournierb1f46562016-01-21 09:46:15 +0100344 contains the current at the epoch format, and "usec" contains the
345 current microseconds.
346
Thierry FOURNIERa78f0372016-12-14 19:04:41 +0100347.. js:function:: core.http_date(date)
348
349 **context**: body, init, task, action
350
Bertrand Jacquin874a35c2018-09-10 21:26:07 +0100351 This function take a string representing http date, and returns an integer
Thierry FOURNIERa78f0372016-12-14 19:04:41 +0100352 containing the corresponding date with a epoch format. A valid http date
353 me respect the format IMF, RFC850 or ASCTIME.
354
355 :param string date: a date http-date formatted
356 :returns: integer containing epoch date
357 :see: :js:func:`core.imf_date`.
358 :see: :js:func:`core.rfc850_date`.
359 :see: :js:func:`core.asctime_date`.
360 :see: https://tools.ietf.org/html/rfc7231#section-7.1.1.1
361
362.. js:function:: core.imf_date(date)
363
364 **context**: body, init, task, action
365
Bertrand Jacquin874a35c2018-09-10 21:26:07 +0100366 This function take a string representing IMF date, and returns an integer
Thierry FOURNIERa78f0372016-12-14 19:04:41 +0100367 containing the corresponding date with a epoch format.
368
369 :param string date: a date IMF formatted
370 :returns: integer containing epoch date
371 :see: https://tools.ietf.org/html/rfc7231#section-7.1.1.1
372
373 The IMF format is like this:
374
375.. code-block:: text
376
377 Sun, 06 Nov 1994 08:49:37 GMT
378..
379
380.. js:function:: core.rfc850_date(date)
381
382 **context**: body, init, task, action
383
Bertrand Jacquin874a35c2018-09-10 21:26:07 +0100384 This function take a string representing RFC850 date, and returns an integer
Thierry FOURNIERa78f0372016-12-14 19:04:41 +0100385 containing the corresponding date with a epoch format.
386
387 :param string date: a date RFC859 formatted
388 :returns: integer containing epoch date
389 :see: https://tools.ietf.org/html/rfc7231#section-7.1.1.1
390
391 The RFC850 format is like this:
392
393.. code-block:: text
394
395 Sunday, 06-Nov-94 08:49:37 GMT
396..
397
398.. js:function:: core.asctime_date(date)
399
400 **context**: body, init, task, action
401
Bertrand Jacquin874a35c2018-09-10 21:26:07 +0100402 This function take a string representing ASCTIME date, and returns an integer
Thierry FOURNIERa78f0372016-12-14 19:04:41 +0100403 containing the corresponding date with a epoch format.
404
405 :param string date: a date ASCTIME formatted
406 :returns: integer containing epoch date
407 :see: https://tools.ietf.org/html/rfc7231#section-7.1.1.1
408
409 The ASCTIME format is like this:
410
411.. code-block:: text
412
413 Sun Nov 6 08:49:37 1994
414..
415
416.. js:function:: core.rfc850_date(date)
417
418 **context**: body, init, task, action
419
Bertrand Jacquin874a35c2018-09-10 21:26:07 +0100420 This function take a string representing http date, and returns an integer
Thierry FOURNIERa78f0372016-12-14 19:04:41 +0100421 containing the corresponding date with a epoch format.
422
423 :param string date: a date http-date formatted
424
425.. js:function:: core.asctime_date(date)
426
427 **context**: body, init, task, action
428
Bertrand Jacquin874a35c2018-09-10 21:26:07 +0100429 This function take a string representing http date, and returns an integer
Thierry FOURNIERa78f0372016-12-14 19:04:41 +0100430 containing the corresponding date with a epoch format.
431
432 :param string date: a date http-date formatted
433
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100434.. js:function:: core.msleep(milliseconds)
435
436 **context**: body, init, task, action
437
438 The `core.msleep()` stops the Lua execution between specified milliseconds.
439
440 :param integer milliseconds: the required milliseconds.
441
Thierry Fournierf61aa632016-02-19 20:56:00 +0100442.. js:attribute:: core.proxies
443
444 **context**: body, init, task, action, sample-fetch, converter
445
Bertrand Jacquin874a35c2018-09-10 21:26:07 +0100446 Proxies is a table containing the list of all proxies declared in the
Patrick Hemmerc6a1d712018-05-01 21:30:41 -0400447 configuration file. The table is indexed by the proxy name, and each entry
448 of the proxies table is an object of type :ref:`proxy_class`.
449
450 Warning, if you have declared a frontend and backend with the same name, only
451 one of these are listed.
Thierry Fournierf61aa632016-02-19 20:56:00 +0100452
Thierry FOURNIERc5d11c62018-02-12 14:46:54 +0100453.. js:function:: core.register_action(name, actions, func [, nb_args])
Thierry FOURNIER8255a752015-09-23 21:03:35 +0200454
455 **context**: body
456
David Carlier61fdf8b2015-10-02 11:59:38 +0100457 Register a Lua function executed as action. All the registered action can be
Thierry FOURNIER8255a752015-09-23 21:03:35 +0200458 used in HAProxy with the prefix "lua.". An action gets a TXN object class as
459 input.
460
461 :param string name: is the name of the converter.
Willy Tarreau61add3c2015-09-28 15:39:10 +0200462 :param table actions: is a table of string describing the HAProxy actions who
Thierry FOURNIER8255a752015-09-23 21:03:35 +0200463 want to register to. The expected actions are 'tcp-req',
464 'tcp-res', 'http-req' or 'http-res'.
Thierry FOURNIERc5d11c62018-02-12 14:46:54 +0100465 :param integer nb_args: is the expected number of argument for the action.
466 By default the value is 0.
Thierry FOURNIER8255a752015-09-23 21:03:35 +0200467 :param function func: is the Lua function called to work as converter.
468
469 The prototype of the Lua function used as argument is:
470
471.. code-block:: lua
472
Thierry FOURNIERc5d11c62018-02-12 14:46:54 +0100473 function(txn [, arg1 [, arg2]])
Thierry FOURNIER8255a752015-09-23 21:03:35 +0200474..
475
Thierry FOURNIERdc595002015-12-21 11:13:52 +0100476 * **txn** (:ref:`txn_class`): this is a TXN object used for manipulating the
Thierry FOURNIER8255a752015-09-23 21:03:35 +0200477 current request or TCP stream.
478
Bertrand Jacquin874a35c2018-09-10 21:26:07 +0100479 * **argX**: this is argument provided through the HAProxy configuration file.
Thierry FOURNIERc5d11c62018-02-12 14:46:54 +0100480
Bertrand Jacquin874a35c2018-09-10 21:26:07 +0100481 Here, an example of action registration. The action just send an 'Hello world'
Thierry FOURNIER8255a752015-09-23 21:03:35 +0200482 in the logs.
483
484.. code-block:: lua
485
486 core.register_action("hello-world", { "tcp-req", "http-req" }, function(txn)
487 txn:Info("Hello world")
488 end)
489..
490
Willy Tarreau714f3452021-05-09 06:47:26 +0200491 This example code is used in HAProxy configuration like this:
Thierry FOURNIER8255a752015-09-23 21:03:35 +0200492
493::
494
495 frontend tcp_frt
496 mode tcp
497 tcp-request content lua.hello-world
498
499 frontend http_frt
500 mode http
501 http-request lua.hello-world
Thierry FOURNIERc5d11c62018-02-12 14:46:54 +0100502..
503
Bertrand Jacquin874a35c2018-09-10 21:26:07 +0100504 A second example using arguments
Thierry FOURNIERc5d11c62018-02-12 14:46:54 +0100505
506.. code-block:: lua
507
508 function hello_world(txn, arg)
509 txn:Info("Hello world for " .. arg)
510 end
511 core.register_action("hello-world", { "tcp-req", "http-req" }, hello_world, 2)
512..
Thierry FOURNIER8255a752015-09-23 21:03:35 +0200513
Willy Tarreau714f3452021-05-09 06:47:26 +0200514 This example code is used in HAProxy configuration like this:
Thierry FOURNIERc5d11c62018-02-12 14:46:54 +0100515
516::
517
518 frontend tcp_frt
519 mode tcp
520 tcp-request content lua.hello-world everybody
521..
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100522.. js:function:: core.register_converters(name, func)
523
524 **context**: body
525
David Carlier61fdf8b2015-10-02 11:59:38 +0100526 Register a Lua function executed as converter. All the registered converters
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100527 can be used in HAProxy with the prefix "lua.". An converter get a string as
528 input and return a string as output. The registered function can take up to 9
529 values as parameter. All the value are strings.
530
531 :param string name: is the name of the converter.
532 :param function func: is the Lua function called to work as converter.
533
534 The prototype of the Lua function used as argument is:
535
536.. code-block:: lua
537
538 function(str, [p1 [, p2 [, ... [, p5]]]])
539..
540
541 * **str** (*string*): this is the input value automatically converted in
542 string.
543 * **p1** .. **p5** (*string*): this is a list of string arguments declared in
Bertrand Jacquin874a35c2018-09-10 21:26:07 +0100544 the HAProxy configuration file. The number of arguments doesn't exceed 5.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100545 The order and the nature of these is conventionally choose by the
Bertrand Jacquin874a35c2018-09-10 21:26:07 +0100546 developer.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100547
548.. js:function:: core.register_fetches(name, func)
549
550 **context**: body
551
David Carlier61fdf8b2015-10-02 11:59:38 +0100552 Register a Lua function executed as sample fetch. All the registered sample
Bertrand Jacquin874a35c2018-09-10 21:26:07 +0100553 fetch can be used in HAProxy with the prefix "lua.". A Lua sample fetch
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100554 return a string as output. The registered function can take up to 9 values as
555 parameter. All the value are strings.
556
557 :param string name: is the name of the converter.
558 :param function func: is the Lua function called to work as sample fetch.
559
560 The prototype of the Lua function used as argument is:
561
562.. code-block:: lua
563
564 string function(txn, [p1 [, p2 [, ... [, p5]]]])
565..
566
Thierry FOURNIERdc595002015-12-21 11:13:52 +0100567 * **txn** (:ref:`txn_class`): this is the txn object associated with the current
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100568 request.
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.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100571 The order and the nature of these is conventionally choose by the
Bertrand Jacquin874a35c2018-09-10 21:26:07 +0100572 developer.
573 * **Returns**: A string containing some data, or nil if the value cannot be
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100574 returned now.
575
576 lua example code:
577
578.. code-block:: lua
579
580 core.register_fetches("hello", function(txn)
581 return "hello"
582 end)
583..
584
585 HAProxy example configuration:
586
587::
588
589 frontend example
590 http-request redirect location /%[lua.hello]
591
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +0200592.. js:function:: core.register_service(name, mode, func)
593
594 **context**: body
595
David Carlier61fdf8b2015-10-02 11:59:38 +0100596 Register a Lua function executed as a service. All the registered service can
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +0200597 be used in HAProxy with the prefix "lua.". A service gets an object class as
598 input according with the required mode.
599
600 :param string name: is the name of the converter.
Willy Tarreau61add3c2015-09-28 15:39:10 +0200601 :param string mode: is string describing the required mode. Only 'tcp' or
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +0200602 'http' are allowed.
603 :param function func: is the Lua function called to work as converter.
604
605 The prototype of the Lua function used as argument is:
606
607.. code-block:: lua
608
609 function(applet)
610..
611
Thierry FOURNIERdc595002015-12-21 11:13:52 +0100612 * **applet** *applet* will be a :ref:`applettcp_class` or a
613 :ref:`applethttp_class`. It depends the type of registered applet. An applet
614 registered with the 'http' value for the *mode* parameter will gets a
615 :ref:`applethttp_class`. If the *mode* value is 'tcp', the applet will gets
616 a :ref:`applettcp_class`.
617
618 **warning**: Applets of type 'http' cannot be called from 'tcp-*'
619 rulesets. Only the 'http-*' rulesets are authorized, this means
620 that is not possible to call an HTTP applet from a proxy in tcp
Bertrand Jacquin874a35c2018-09-10 21:26:07 +0100621 mode. Applets of type 'tcp' can be called from anywhere.
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +0200622
Bertrand Jacquin874a35c2018-09-10 21:26:07 +0100623 Here, an example of service registration. The service just send an 'Hello world'
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +0200624 as an http response.
625
626.. code-block:: lua
627
Pieter Baauw4d7f7662015-11-08 16:38:08 +0100628 core.register_service("hello-world", "http", function(applet)
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +0200629 local response = "Hello World !"
630 applet:set_status(200)
Pieter Baauw2dcb9bc2015-10-01 22:47:12 +0200631 applet:add_header("content-length", string.len(response))
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +0200632 applet:add_header("content-type", "text/plain")
Pieter Baauw2dcb9bc2015-10-01 22:47:12 +0200633 applet:start_response()
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +0200634 applet:send(response)
635 end)
636..
637
Willy Tarreau714f3452021-05-09 06:47:26 +0200638 This example code is used in HAProxy configuration like this:
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +0200639
640::
641
642 frontend example
643 http-request use-service lua.hello-world
644
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100645.. js:function:: core.register_init(func)
646
647 **context**: body
648
649 Register a function executed after the configuration parsing. This is useful
650 to check any parameters.
651
Pieter Baauw4d7f7662015-11-08 16:38:08 +0100652 :param function func: is the Lua function called to work as initializer.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100653
654 The prototype of the Lua function used as argument is:
655
656.. code-block:: lua
657
658 function()
659..
660
661 It takes no input, and no output is expected.
662
663.. js:function:: core.register_task(func)
664
665 **context**: body, init, task, action, sample-fetch, converter
666
667 Register and start independent task. The task is started when the HAProxy
668 main scheduler starts. For example this type of tasks can be executed to
Thierry FOURNIER2e4893c2015-03-18 13:37:27 +0100669 perform complex health checks.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100670
Pieter Baauw4d7f7662015-11-08 16:38:08 +0100671 :param function func: is the Lua function called to work as initializer.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100672
673 The prototype of the Lua function used as argument is:
674
675.. code-block:: lua
676
677 function()
678..
679
680 It takes no input, and no output is expected.
681
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +0100682.. js:function:: core.register_cli([path], usage, func)
683
684 **context**: body
685
686 Register and start independent task. The task is started when the HAProxy
687 main scheduler starts. For example this type of tasks can be executed to
688 perform complex health checks.
689
690 :param array path: is the sequence of word for which the cli execute the Lua
691 binding.
692 :param string usage: is the usage message displayed in the help.
693 :param function func: is the Lua function called to handle the CLI commands.
694
695 The prototype of the Lua function used as argument is:
696
697.. code-block:: lua
698
699 function(AppletTCP, [arg1, [arg2, [...]]])
700..
701
702 I/O are managed with the :ref:`applettcp_class` object. Args are given as
Bertrand Jacquin874a35c2018-09-10 21:26:07 +0100703 parameter. The args embed the registered path. If the path is declared like
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +0100704 this:
705
706.. code-block:: lua
707
708 core.register_cli({"show", "ssl", "stats"}, "Display SSL stats..", function(applet, arg1, arg2, arg3, arg4, arg5)
709 end)
710..
711
712 And we execute this in the prompt:
713
714.. code-block:: text
715
716 > prompt
717 > show ssl stats all
718..
719
Bertrand Jacquin874a35c2018-09-10 21:26:07 +0100720 Then, arg1, arg2 and arg3 will contains respectively "show", "ssl" and "stats".
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +0100721 arg4 will contain "all". arg5 contains nil.
722
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100723.. js:function:: core.set_nice(nice)
724
725 **context**: task, action, sample-fetch, converter
726
727 Change the nice of the current task or current session.
Thierry FOURNIER2e4893c2015-03-18 13:37:27 +0100728
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100729 :param integer nice: the nice value, it must be between -1024 and 1024.
730
731.. js:function:: core.set_map(filename, key, value)
732
733 **context**: init, task, action, sample-fetch, converter
734
Bertrand Jacquin874a35c2018-09-10 21:26:07 +0100735 Set the value *value* associated to the key *key* in the map referenced by
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100736 *filename*.
737
Thierry FOURNIERdc595002015-12-21 11:13:52 +0100738 :param string filename: the Map reference
739 :param string key: the key to set or replace
740 :param string value: the associated value
741
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100742.. js:function:: core.sleep(int seconds)
743
744 **context**: body, init, task, action
745
746 The `core.sleep()` functions stop the Lua execution between specified seconds.
747
748 :param integer seconds: the required seconds.
749
750.. js:function:: core.tcp()
751
752 **context**: init, task, action
753
754 This function returns a new object of a *socket* class.
755
Thierry FOURNIERdc595002015-12-21 11:13:52 +0100756 :returns: A :ref:`socket_class` object.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100757
Thierry Fournier1de16592016-01-27 09:49:07 +0100758.. js:function:: core.concat()
759
760 **context**: body, init, task, action, sample-fetch, converter
761
Bertrand Jacquin874a35c2018-09-10 21:26:07 +0100762 This function returns a new concat object.
Thierry Fournier1de16592016-01-27 09:49:07 +0100763
764 :returns: A :ref:`concat_class` object.
765
Thierry FOURNIER0a99b892015-08-26 00:14:17 +0200766.. js:function:: core.done(data)
767
768 **context**: body, init, task, action, sample-fetch, converter
769
770 :param any data: Return some data for the caller. It is useful with
771 sample-fetches and sample-converters.
772
773 Immediately stops the current Lua execution and returns to the caller which
774 may be a sample fetch, a converter or an action and returns the specified
Thierry Fournier4234dbd2020-11-28 13:18:23 +0100775 value (ignored for actions and init). It is used when the LUA process finishes
776 its work and wants to give back the control to HAProxy without executing the
Thierry FOURNIER0a99b892015-08-26 00:14:17 +0200777 remaining code. It can be seen as a multi-level "return".
778
Thierry FOURNIER486f5a02015-03-16 15:13:03 +0100779.. js:function:: core.yield()
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100780
781 **context**: task, action, sample-fetch, converter
782
783 Give back the hand at the HAProxy scheduler. It is used when the LUA
784 processing consumes a lot of processing time.
785
Thierry FOURNIER / OZON.IO62fec752016-11-10 20:38:11 +0100786.. js:function:: core.parse_addr(address)
787
788 **context**: body, init, task, action, sample-fetch, converter
789
790 :param network: is a string describing an ipv4 or ipv6 address and optionally
791 its network length, like this: "127.0.0.1/8" or "aaaa::1234/32".
792 :returns: a userdata containing network or nil if an error occurs.
793
Bertrand Jacquin874a35c2018-09-10 21:26:07 +0100794 Parse ipv4 or ipv6 addresses and its facultative associated network.
Thierry FOURNIER / OZON.IO62fec752016-11-10 20:38:11 +0100795
796.. js:function:: core.match_addr(addr1, addr2)
797
798 **context**: body, init, task, action, sample-fetch, converter
799
800 :param addr1: is an address created with "core.parse_addr".
801 :param addr2: is an address created with "core.parse_addr".
Bertrand Jacquin874a35c2018-09-10 21:26:07 +0100802 :returns: boolean, true if the network of the addresses match, else returns
Thierry FOURNIER / OZON.IO62fec752016-11-10 20:38:11 +0100803 false.
804
Ilya Shipitsin2075ca82020-03-06 23:22:22 +0500805 Match two networks. For example "127.0.0.1/32" matches "127.0.0.0/8". The order
Thierry FOURNIER / OZON.IO62fec752016-11-10 20:38:11 +0100806 of network is not important.
807
Thierry FOURNIER / OZON.IO8a1027a2016-11-24 20:48:38 +0100808.. js:function:: core.tokenize(str, separators [, noblank])
809
810 **context**: body, init, task, action, sample-fetch, converter
811
812 This function is useful for tokenizing an entry, or splitting some messages.
813 :param string str: The string which will be split.
814 :param string separators: A string containing a list of separators.
815 :param boolean noblank: Ignore empty entries.
816 :returns: an array of string.
817
818 For example:
819
820.. code-block:: lua
821
822 local array = core.tokenize("This function is useful, for tokenizing an entry.", "., ", true)
823 print_r(array)
824..
825
826 Returns this array:
827
828.. code-block:: text
829
830 (table) table: 0x21c01e0 [
831 1: (string) "This"
832 2: (string) "function"
833 3: (string) "is"
834 4: (string) "useful"
835 5: (string) "for"
836 6: (string) "tokenizing"
837 7: (string) "an"
838 8: (string) "entry"
839 ]
840..
841
Thierry Fournierf61aa632016-02-19 20:56:00 +0100842.. _proxy_class:
843
844Proxy class
845============
846
847.. js:class:: Proxy
848
849 This class provides a way for manipulating proxy and retrieving information
850 like statistics.
851
Thierry FOURNIER817e7592017-07-24 14:35:04 +0200852.. js:attribute:: Proxy.name
853
854 Contain the name of the proxy.
855
Baptiste Assmann46c72552017-10-26 21:51:58 +0200856.. js:attribute:: Proxy.uuid
857
858 Contain the unique identifier of the proxy.
859
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +0100860.. js:attribute:: Proxy.servers
861
Patrick Hemmerc6a1d712018-05-01 21:30:41 -0400862 Contain a table with the attached servers. The table is indexed by server
863 name, and each server entry is an object of type :ref:`server_class`.
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +0100864
Adis Nezirovic8878f8e2018-07-13 12:18:33 +0200865.. js:attribute:: Proxy.stktable
866
867 Contains a stick table object attached to the proxy.
868
Thierry Fournierff480422016-02-25 08:36:46 +0100869.. js:attribute:: Proxy.listeners
870
Patrick Hemmerc6a1d712018-05-01 21:30:41 -0400871 Contain a table with the attached listeners. The table is indexed by listener
872 name, and each each listeners entry is an object of type
873 :ref:`listener_class`.
Thierry Fournierff480422016-02-25 08:36:46 +0100874
Thierry Fournierf61aa632016-02-19 20:56:00 +0100875.. js:function:: Proxy.pause(px)
876
877 Pause the proxy. See the management socket documentation for more information.
878
879 :param class_proxy px: A :ref:`proxy_class` which indicates the manipulated
880 proxy.
881
882.. js:function:: Proxy.resume(px)
883
884 Resume the proxy. See the management socket documentation for more
885 information.
886
887 :param class_proxy px: A :ref:`proxy_class` which indicates the manipulated
888 proxy.
889
890.. js:function:: Proxy.stop(px)
891
892 Stop the proxy. See the management socket documentation for more information.
893
894 :param class_proxy px: A :ref:`proxy_class` which indicates the manipulated
895 proxy.
896
897.. js:function:: Proxy.shut_bcksess(px)
898
899 Kill the session attached to a backup server. See the management socket
900 documentation for more information.
901
902 :param class_proxy px: A :ref:`proxy_class` which indicates the manipulated
903 proxy.
904
905.. js:function:: Proxy.get_cap(px)
906
907 Returns a string describing the capabilities of the proxy.
908
909 :param class_proxy px: A :ref:`proxy_class` which indicates the manipulated
910 proxy.
911 :returns: a string "frontend", "backend", "proxy" or "ruleset".
912
913.. js:function:: Proxy.get_mode(px)
914
915 Returns a string describing the mode of the current proxy.
916
917 :param class_proxy px: A :ref:`proxy_class` which indicates the manipulated
918 proxy.
919 :returns: a string "tcp", "http", "health" or "unknown"
920
921.. js:function:: Proxy.get_stats(px)
922
Bertrand Jacquin874a35c2018-09-10 21:26:07 +0100923 Returns a table containing the proxy statistics. The statistics returned are
Thierry Fournierf61aa632016-02-19 20:56:00 +0100924 not the same if the proxy is frontend or a backend.
925
926 :param class_proxy px: A :ref:`proxy_class` which indicates the manipulated
927 proxy.
Patrick Hemmerc6a1d712018-05-01 21:30:41 -0400928 :returns: a key/value table containing stats
Thierry Fournierf61aa632016-02-19 20:56:00 +0100929
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +0100930.. _server_class:
931
932Server class
933============
934
Patrick Hemmerc6a1d712018-05-01 21:30:41 -0400935.. js:class:: Server
936
937 This class provides a way for manipulating servers and retrieving information.
938
Patrick Hemmera62ae7e2018-04-29 14:23:48 -0400939.. js:attribute:: Server.name
940
941 Contain the name of the server.
942
943.. js:attribute:: Server.puid
944
945 Contain the proxy unique identifier of the server.
946
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +0100947.. js:function:: Server.is_draining(sv)
948
Patrick Hemmerc6a1d712018-05-01 21:30:41 -0400949 Return true if the server is currently draining sticky connections.
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +0100950
951 :param class_server sv: A :ref:`server_class` which indicates the manipulated
952 server.
953 :returns: a boolean
954
Patrick Hemmer32d539f2018-04-29 14:25:46 -0400955.. js:function:: Server.set_maxconn(sv, weight)
956
957 Dynamically change the maximum connections of the server. See the management
958 socket documentation for more information about the format of the string.
959
960 :param class_server sv: A :ref:`server_class` which indicates the manipulated
961 server.
962 :param string maxconn: A string describing the server maximum connections.
963
964.. js:function:: Server.get_maxconn(sv, weight)
965
966 This function returns an integer representing the server maximum connections.
967
968 :param class_server sv: A :ref:`server_class` which indicates the manipulated
969 server.
970 :returns: an integer.
971
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +0100972.. js:function:: Server.set_weight(sv, weight)
973
Patrick Hemmerc6a1d712018-05-01 21:30:41 -0400974 Dynamically change the weight of the server. See the management socket
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +0100975 documentation for more information about the format of the string.
976
977 :param class_server sv: A :ref:`server_class` which indicates the manipulated
978 server.
979 :param string weight: A string describing the server weight.
980
981.. js:function:: Server.get_weight(sv)
982
Patrick Hemmerc6a1d712018-05-01 21:30:41 -0400983 This function returns an integer representing the server weight.
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +0100984
985 :param class_server sv: A :ref:`server_class` which indicates the manipulated
986 server.
987 :returns: an integer.
988
Joseph C. Sible49bbf522020-05-04 22:20:32 -0400989.. js:function:: Server.set_addr(sv, addr[, port])
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +0100990
Patrick Hemmerc6a1d712018-05-01 21:30:41 -0400991 Dynamically change the address of the server. See the management socket
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +0100992 documentation for more information about the format of the string.
993
994 :param class_server sv: A :ref:`server_class` which indicates the manipulated
995 server.
Patrick Hemmerc6a1d712018-05-01 21:30:41 -0400996 :param string addr: A string describing the server address.
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +0100997
998.. js:function:: Server.get_addr(sv)
999
Patrick Hemmerc6a1d712018-05-01 21:30:41 -04001000 Returns a string describing the address of the server.
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001001
1002 :param class_server sv: A :ref:`server_class` which indicates the manipulated
1003 server.
1004 :returns: A string
1005
1006.. js:function:: Server.get_stats(sv)
1007
1008 Returns server statistics.
1009
1010 :param class_server sv: A :ref:`server_class` which indicates the manipulated
1011 server.
Patrick Hemmerc6a1d712018-05-01 21:30:41 -04001012 :returns: a key/value table containing stats
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001013
1014.. js:function:: Server.shut_sess(sv)
1015
1016 Shutdown all the sessions attached to the server. See the management socket
1017 documentation for more information about this function.
1018
1019 :param class_server sv: A :ref:`server_class` which indicates the manipulated
1020 server.
1021
1022.. js:function:: Server.set_drain(sv)
1023
1024 Drain sticky sessions. See the management socket documentation for more
1025 information about this function.
1026
1027 :param class_server sv: A :ref:`server_class` which indicates the manipulated
1028 server.
1029
1030.. js:function:: Server.set_maint(sv)
1031
1032 Set maintenance mode. See the management socket documentation for more
1033 information about this function.
1034
1035 :param class_server sv: A :ref:`server_class` which indicates the manipulated
1036 server.
1037
1038.. js:function:: Server.set_ready(sv)
1039
1040 Set normal mode. See the management socket documentation for more information
1041 about this function.
1042
1043 :param class_server sv: A :ref:`server_class` which indicates the manipulated
1044 server.
1045
1046.. js:function:: Server.check_enable(sv)
1047
1048 Enable health checks. See the management socket documentation for more
1049 information about this function.
1050
1051 :param class_server sv: A :ref:`server_class` which indicates the manipulated
1052 server.
1053
1054.. js:function:: Server.check_disable(sv)
1055
1056 Disable health checks. See the management socket documentation for more
1057 information about this function.
1058
1059 :param class_server sv: A :ref:`server_class` which indicates the manipulated
1060 server.
1061
1062.. js:function:: Server.check_force_up(sv)
1063
1064 Force health-check up. See the management socket documentation for more
1065 information about this function.
1066
1067 :param class_server sv: A :ref:`server_class` which indicates the manipulated
1068 server.
1069
1070.. js:function:: Server.check_force_nolb(sv)
1071
1072 Force health-check nolb mode. See the management socket documentation for more
1073 information about this function.
1074
1075 :param class_server sv: A :ref:`server_class` which indicates the manipulated
1076 server.
1077
1078.. js:function:: Server.check_force_down(sv)
1079
1080 Force health-check down. See the management socket documentation for more
1081 information about this function.
1082
1083 :param class_server sv: A :ref:`server_class` which indicates the manipulated
1084 server.
1085
1086.. js:function:: Server.agent_enable(sv)
1087
1088 Enable agent check. See the management socket documentation for more
1089 information about this function.
1090
1091 :param class_server sv: A :ref:`server_class` which indicates the manipulated
1092 server.
1093
1094.. js:function:: Server.agent_disable(sv)
1095
1096 Disable agent check. See the management socket documentation for more
1097 information about this function.
1098
1099 :param class_server sv: A :ref:`server_class` which indicates the manipulated
1100 server.
1101
1102.. js:function:: Server.agent_force_up(sv)
1103
1104 Force agent check up. See the management socket documentation for more
1105 information about this function.
1106
1107 :param class_server sv: A :ref:`server_class` which indicates the manipulated
1108 server.
1109
1110.. js:function:: Server.agent_force_down(sv)
1111
1112 Force agent check down. See the management socket documentation for more
1113 information about this function.
1114
1115 :param class_server sv: A :ref:`server_class` which indicates the manipulated
1116 server.
1117
Thierry Fournierff480422016-02-25 08:36:46 +01001118.. _listener_class:
1119
1120Listener class
1121==============
1122
1123.. js:function:: Listener.get_stats(ls)
1124
1125 Returns server statistics.
1126
1127 :param class_listener ls: A :ref:`listener_class` which indicates the
1128 manipulated listener.
Patrick Hemmerc6a1d712018-05-01 21:30:41 -04001129 :returns: a key/value table containing stats
Thierry Fournierff480422016-02-25 08:36:46 +01001130
Thierry Fournier1de16592016-01-27 09:49:07 +01001131.. _concat_class:
1132
1133Concat class
1134============
1135
1136.. js:class:: Concat
1137
1138 This class provides a fast way for string concatenation. The way using native
1139 Lua concatenation like the code below is slow for some reasons.
1140
1141.. code-block:: lua
1142
1143 str = "string1"
1144 str = str .. ", string2"
1145 str = str .. ", string3"
1146..
1147
1148 For each concatenation, Lua:
1149 * allocate memory for the result,
Ilya Shipitsin2075ca82020-03-06 23:22:22 +05001150 * catenate the two string copying the strings in the new memory block,
Bertrand Jacquin874a35c2018-09-10 21:26:07 +01001151 * free the old memory block containing the string which is no longer used.
Thierry Fournier1de16592016-01-27 09:49:07 +01001152 This process does many memory move, allocation and free. In addition, the
Bertrand Jacquin874a35c2018-09-10 21:26:07 +01001153 memory is not really freed, it is just mark mark as unused and wait for the
Thierry Fournier1de16592016-01-27 09:49:07 +01001154 garbage collector.
1155
Bertrand Jacquin874a35c2018-09-10 21:26:07 +01001156 The Concat class provide an alternative way to concatenate strings. It uses
Thierry Fournier1de16592016-01-27 09:49:07 +01001157 the internal Lua mechanism (it does not allocate memory), but it doesn't copy
1158 the data more than once.
1159
1160 On my computer, the following loops spends 0.2s for the Concat method and
1161 18.5s for the pure Lua implementation. So, the Concat class is about 1000x
1162 faster than the embedded solution.
1163
1164.. code-block:: lua
1165
1166 for j = 1, 100 do
1167 c = core.concat()
1168 for i = 1, 20000 do
1169 c:add("#####")
1170 end
1171 end
1172..
1173
1174.. code-block:: lua
1175
1176 for j = 1, 100 do
1177 c = ""
1178 for i = 1, 20000 do
1179 c = c .. "#####"
1180 end
1181 end
1182..
1183
1184.. js:function:: Concat.add(concat, string)
1185
1186 This function adds a string to the current concatenated string.
1187
1188 :param class_concat concat: A :ref:`concat_class` which contains the currently
Ilya Shipitsin2075ca82020-03-06 23:22:22 +05001189 built string.
Bertrand Jacquin874a35c2018-09-10 21:26:07 +01001190 :param string string: A new string to concatenate to the current built
Thierry Fournier1de16592016-01-27 09:49:07 +01001191 string.
1192
1193.. js:function:: Concat.dump(concat)
1194
Bertrand Jacquin874a35c2018-09-10 21:26:07 +01001195 This function returns the concatenated string.
Thierry Fournier1de16592016-01-27 09:49:07 +01001196
1197 :param class_concat concat: A :ref:`concat_class` which contains the currently
Ilya Shipitsin2075ca82020-03-06 23:22:22 +05001198 built string.
Thierry Fournier1de16592016-01-27 09:49:07 +01001199 :returns: the concatenated string
1200
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001201.. _fetches_class:
1202
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001203Fetches class
1204=============
1205
1206.. js:class:: Fetches
1207
1208 This class contains a lot of internal HAProxy sample fetches. See the
Thierry FOURNIER2e4893c2015-03-18 13:37:27 +01001209 HAProxy "configuration.txt" documentation for more information about her
Bertrand Jacquin874a35c2018-09-10 21:26:07 +01001210 usage. They are the chapters 7.3.2 to 7.3.6.
Thierry FOURNIER2e4893c2015-03-18 13:37:27 +01001211
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001212 **warning** some sample fetches are not available in some context. These
Patrick Hemmerc6a1d712018-05-01 21:30:41 -04001213 limitations are specified in this documentation when they're useful.
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001214
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01001215 :see: :js:attr:`TXN.f`
1216 :see: :js:attr:`TXN.sf`
Thierry FOURNIER2e4893c2015-03-18 13:37:27 +01001217
1218 Fetches are useful for:
1219
1220 * get system time,
1221 * get environment variable,
1222 * get random numbers,
1223 * known backend status like the number of users in queue or the number of
1224 connections established,
1225 * client information like ip source or destination,
1226 * deal with stick tables,
Ilya Shipitsin2075ca82020-03-06 23:22:22 +05001227 * Established SSL information,
Thierry FOURNIER2e4893c2015-03-18 13:37:27 +01001228 * HTTP information like headers or method.
1229
1230.. code-block:: lua
1231
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001232 function action(txn)
1233 -- Get source IP
1234 local clientip = txn.f:src()
1235 end
Thierry FOURNIER2e4893c2015-03-18 13:37:27 +01001236..
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001237
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001238.. _converters_class:
1239
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001240Converters class
1241================
1242
1243.. js:class:: Converters
1244
1245 This class contains a lot of internal HAProxy sample converters. See the
Thierry FOURNIER2e4893c2015-03-18 13:37:27 +01001246 HAProxy documentation "configuration.txt" for more information about her
1247 usage. Its the chapter 7.3.1.
1248
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01001249 :see: :js:attr:`TXN.c`
1250 :see: :js:attr:`TXN.sc`
Thierry FOURNIER2e4893c2015-03-18 13:37:27 +01001251
1252 Converters provides statefull transformation. They are useful for:
1253
1254 * converting input to base64,
1255 * applying hash on input string (djb2, crc32, sdbm, wt6),
1256 * format date,
1257 * json escape,
Pieter Baauw4d7f7662015-11-08 16:38:08 +01001258 * extracting preferred language comparing two lists,
Thierry FOURNIER2e4893c2015-03-18 13:37:27 +01001259 * turn to lower or upper chars,
1260 * deal with stick tables.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001261
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001262.. _channel_class:
1263
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001264Channel class
1265=============
1266
1267.. js:class:: Channel
1268
1269 HAProxy uses two buffers for the processing of the requests. The first one is
1270 used with the request data (from the client to the server) and the second is
1271 used for the response data (from the server to the client).
1272
1273 Each buffer contains two types of data. The first type is the incoming data
1274 waiting for a processing. The second part is the outgoing data already
1275 processed. Usually, the incoming data is processed, after it is tagged as
1276 outgoing data, and finally it is sent. The following functions provides tools
1277 for manipulating these data in a buffer.
1278
1279 The following diagram shows where the channel class function are applied.
1280
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001281 .. image:: _static/channel.png
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001282
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001283 .. warning::
1284 It is not possible to read from the response in request action, and it is
1285 not possible to read for the request channel in response action.
Christopher Faulet09530392021-06-14 11:43:18 +02001286
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001287 .. warning::
1288 It is forbidden to alter the Channels buffer from HTTP contexts. So only
1289 :js:func:`Channel.input`, :js:func:`Channel.output`,
1290 :js:func:`Channel.may_recv`, :js:func:`Channel.is_full` and
1291 :js:func:`Channel.is_resp` can be called from an HTTP conetext.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001292
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001293 .. note::
1294 Channel object may only be manipulated in the context of a registered
1295 action, sample-fetch or converter. However, only actions are allowed to
1296 yield. When it is said one of the following functions may yield, it is only
1297 true in the context of an action.
1298
1299.. js:function:: Channel.append(channel, string)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001300
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001301 This function copies the string **string** at the end of incoming data of the
1302 channel buffer. The function returns the copied length on success or -1 if
1303 data cannot be copied.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001304
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001305 Same that :js:func:`Channel.insert(channel, string, channel:input())`.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001306
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001307 :param class_channel channel: The manipulated Channel.
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001308 :param string string: The data to copied into incoming data.
1309 :returns: an integer containing the amount of bytes copied or -1.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001310
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001311.. js:function:: Channel.data(channel [, offset [, length]])
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001312
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001313 This function returns **length** bytes of incoming data from the channel
1314 buffer, starting at the offset **offset**. The data are not removed from the
1315 buffer.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001316
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001317 By default, if no length is provided, all incoming data found, starting at the
1318 given offset, are returned. If **length** is set to -1, the function tries to
1319 retrieve a maximum of data and yields if necessary. It also waits for more
1320 data if the requested length exceeds the available amount of incoming data. Do
1321 not providing an offset is the same that setting it to 0. A positive offset is
1322 relative to the begining of incoming data of the channel buffer while negative
1323 offset is relative to their end.
1324
1325 If there is no incoming data and the channel can't receive more data, a 'nil'
1326 value is returned.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001327
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001328 :param class_channel channel: The manipulated Channel.
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001329 :param integer offset: *optional* The offset in incoming data to start to get
1330 data. 0 by default. May be negative to be relative to
1331 the end of incoming data.
1332 :param integer length: *optional* The expected length of data to retrieve. All
1333 incoming data by default. May be set to -1 to get a
1334 maximum of data.
1335 :returns: a string containing the data found or nil.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001336
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001337.. js:function:: Channel.forward(channel, length)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001338
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001339 This function forwards **length** bytes of data from the channel buffer. If
1340 the requested length exceeds the available amount of incoming data, the
1341 function yields, waiting for more data to forward. It returns the amount of
1342 data forwarded.
1343
1344 :param class_channel channel: The manipulated Channel.
1345 :param integer int: The amount of data to forward.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001346
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001347.. js:function:: Channel.input(channel)
1348
1349 This function returns the length of incoming data of the channel buffer.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001350
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001351 :param class_channel channel: The manipulated Channel.
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001352 :returns: an integer containing the amount of available bytes.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001353
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001354.. js:function:: Channel.insert(channel, string [, offset])
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001355
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001356 This function copies the string **string** at the offset **offset** in
1357 incoming data of the channel buffer. The function returns the copied length on
1358 success or -1 if data cannot be copied.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001359
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001360 By default, if no offset is provided, the string is copied in front of
1361 incoming data. A positive offset is relative to the begining of incoming data
1362 of the channel buffer while negative offset is relative to their end.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001363
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001364 :param class_channel channel: The manipulated Channel.
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001365 :param string string: The data to copied into incoming data.
1366 :param integer offset: *optional* The offset in incomding data where to copied
1367 data. 0 by default. May be negative to be relative to
1368 the end of incoming data.
Pieter Baauw4d7f7662015-11-08 16:38:08 +01001369 :returns: an integer containing the amount of bytes copied or -1.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001370
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001371.. js:function:: Channel.is_full(channel)
1372
1373 This function returns true if the channel buffer is full.
1374
1375 :returns: a boolean
1376
1377.. js:function:: Channel.is_resp(channel)
1378
1379 This function returns true if the channel is the response one.
1380
1381 :returns: a boolean
1382
1383.. js:function:: Channel.line(channel [, offset [, length]])
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001384
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001385 This function parses **length** bytes of incoming data of the channel buffer,
1386 starting at offset **offset**, and returns the first line found, including the
1387 '\n'. The data are not removed from the buffer. If no line is found, all data
1388 are returned.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001389
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001390 By default, if no length is provided, all incoming data, starting at the given
1391 offset, are evaluated. If **length** is set to -1, the function tries to
1392 retrieve a maximum of data and yields if necessary. It also waits for more
1393 data if the requested length exceeds the available amount of incoming data. Do
1394 not providing an offset is the same that setting it to 0. A positive offset is
1395 relative to the begining of incoming data of the channel buffer while negative
1396 offset is relative to their end.
1397
1398 If there is no incoming data and the channel can't receive more data, a 'nil'
1399 value is returned.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001400
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001401 :param class_channel channel: The manipulated Channel.
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001402 :param integer offset: *optional* The offset in incomding data to start to
1403 parse data. 0 by default. May be negative to be
1404 relative to the end of incoming data.
1405 :param integer length: *optional* The length of data to parse. All incoming
1406 data by default. May be set to -1 to get a maximum of
1407 data.
1408 :returns: a string containing the line found or nil.
1409
1410.. js:function:: Channel.may_recv(channel)
1411
1412 This function returns true if the channel may still receive data.
1413
1414 :returns: a boolean
1415
1416.. js:function:: Channel.output(channel)
1417
1418 This function returns the length of outgoing data of the channel buffer.
1419
1420 :param class_channel channel: The manipulated Channel.
1421 :returns: an integer containing the amount of available bytes.
1422
1423.. js:function:: Channel.prepend(channel, string)
1424
1425 This function copies the string **string** in front of incoming data of the
1426 channel buffer. The function returns the copied length on success or -1 if
1427 data cannot be copied.
1428
1429 Same that :js:func:`Channel.insert(channel, string, 0)`.
1430
1431 :param class_channel channel: The manipulated Channel.
1432 :param string string: The data to copied into incoming data.
Pieter Baauw4d7f7662015-11-08 16:38:08 +01001433 :returns: an integer containing the amount of bytes copied or -1.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001434
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001435.. js:function:: Channel.remove(channel [, offset [, length]])
1436
1437 This function removes **length** bytes of incoming data of the channel buffer,
1438 starting at offset **offset**. This function returns number of bytes removed
1439 on success.
1440
1441 By default, if no length is provided, all incoming data, starting at the given
1442 offset, are removed. Do not providing an offset is the same that setting it
1443 to 0. A positive offset is relative to the begining of incoming data of the
1444 channel buffer while negative offset is relative to their end.
1445
1446 :param class_channel channel: The manipulated Channel.
1447 :param integer offset: *optional* The offset in incomding data where to start
1448 to remove data. 0 by default. May be negative to
1449 be relative to the end of incoming data.
1450 :param integer length: *optional* The length of data to remove. All incoming
1451 data by default.
1452 :returns: an integer containing the amount of bytes removed.
1453
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001454.. js:function:: Channel.send(channel, string)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001455
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001456 This function required immediate send of the string **string**. It means the
1457 string is copied at the begining of incoming data of the channel buffer and
1458 immediately forwarded. Unless if the connection is close, this function yields
1459 to copied and forward all the string.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001460
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001461 :param class_channel channel: The manipulated Channel.
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001462 :param string string: The data to send.
Pieter Baauw4d7f7662015-11-08 16:38:08 +01001463 :returns: an integer containing the amount of bytes copied or -1.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001464
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001465.. js:function:: Channel.set(channel, string [, offset [, length]])
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001466
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001467 This function replace **length** bytes of incoming data of the channel buffer,
1468 starting at offset **offset**, by the string **string**. The function returns
1469 the copied length on success or -1 if data cannot be copied.
1470
1471 By default, if no length is provided, all incoming data, starting at the given
1472 offset, are replaced. Do not providing an offset is the same that setting it
1473 to 0. A positive offset is relative to the begining of incoming data of the
1474 channel buffer while negative offset is relative to their end.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001475
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001476 :param class_channel channel: The manipulated Channel.
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001477 :param string string: The data to copied into incoming data.
1478 :param integer offset: *optional* The offset in incomding data where to start
1479 the data replacement. 0 by default. May be negative to
1480 be relative to the end of incoming data.
1481 :param integer length: *optional* The length of data to replace. All incoming
1482 data by default.
1483 :returns: an integer containing the amount of bytes copied or -1.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001484
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001485.. js:function:: Channel.dup(channel)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001486
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001487 **DEPRECATED**
1488
1489 This function returns all incoming data found in the channel buffer. The data
1490 are not remove from the buffer and can be reprocessed later.
1491
1492 If there is no incoming data and the channel can't receive more data, a 'nil'
1493 value is returned.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001494
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001495 :param class_channel channel: The manipulated Channel.
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001496 :returns: a string containing all data found or nil.
1497
1498 .. warning::
1499 This function is deprecated. :js:func:`Channel.data()` must be used
1500 instead.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001501
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001502.. js:function:: Channel.get(channel)
1503
1504 **DEPRECATED**
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001505
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001506 This function returns all incoming data found in the channel buffer and remove
1507 them from the buffer.
1508
1509 If there is no incoming data and the channel can't receive more data, a 'nil'
1510 value is returned.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001511
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001512 :param class_channel channel: The manipulated Channel.
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001513 :returns: a string containing all the data found or nil.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001514
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001515 .. warning::
1516 This function is deprecated. :js:func:`Channel.data()` must be used to
1517 retrieve data followed by a call to :js:func:`Channel:remove()` to remove
1518 data.
Thierry FOURNIER / OZON.IO65192f32016-11-07 15:28:40 +01001519
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001520 .. code-block:: lua
Thierry FOURNIER / OZON.IO65192f32016-11-07 15:28:40 +01001521
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001522 local data = chn:data()
1523 chn:remove(0, data:len())
1524
1525 ..
1526
1527.. js:function:: Channel.getline(channel)
1528
1529 **DEPRECATED**
1530
1531 This function returns the first line found in incoming data of the channel
1532 buffer, including the '\n'. The returned data are removed from the buffer. If
1533 no line is found, this function yields to wait for more data, except if the
1534 channel can't receive more data. In this case all data are returned.
1535
1536 If there is no incoming data and the channel can't receive more data, a 'nil'
1537 value is returned.
1538
1539 :param class_channel channel: The manipulated Channel.
1540 :returns: a string containing the line found or nil.
1541
1542 .. warning::
1543 This function is depdrecated. :js:func:`Channel.line()` must be used to
1544 retrieve a line followed by a call to :js:func:`Channel:remove()` to remove
1545 data.
1546
1547 .. code-block:: lua
1548
1549 local line = chn:line(0, -1)
1550 chn:remove(0, line:len())
1551
1552 ..
1553
1554.. js:function:: Channel.get_in_len(channel)
1555
1556 **DEPDRECATED**
1557
1558 This function returns the length of the input part of the buffer.
1559
1560 :param class_channel channel: The manipulated Channel.
1561 :returns: an integer containing the amount of available bytes.
1562
1563 .. warning::
1564 This function is deprecated. :js:func:`Channel.input()` must be used
1565 instead.
1566
1567.. js:function:: Channel.get_out_len(channel)
1568
1569 **DEPDRECATED**
1570
1571 This function returns the length of the output part of the buffer.
1572
1573 :param class_channel channel: The manipulated Channel.
1574 :returns: an integer containing the amount of available bytes.
1575
1576 .. warning::
1577 This function is deprecated. :js:func:`Channel.output()` must be used
1578 instead.
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001579
1580.. _http_class:
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001581
1582HTTP class
1583==========
1584
1585.. js:class:: HTTP
1586
1587 This class contain all the HTTP manipulation functions.
1588
Pieter Baauw386a1272015-08-16 15:26:24 +02001589.. js:function:: HTTP.req_get_headers(http)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001590
Patrick Hemmerc6a1d712018-05-01 21:30:41 -04001591 Returns a table containing all the request headers.
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001592
1593 :param class_http http: The related http object.
Patrick Hemmerc6a1d712018-05-01 21:30:41 -04001594 :returns: table of headers.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01001595 :see: :js:func:`HTTP.res_get_headers`
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001596
Patrick Hemmerc6a1d712018-05-01 21:30:41 -04001597 This is the form of the returned table:
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001598
1599.. code-block:: lua
1600
1601 HTTP:req_get_headers()['<header-name>'][<header-index>] = "<header-value>"
1602
1603 local hdr = HTTP:req_get_headers()
1604 hdr["host"][0] = "www.test.com"
1605 hdr["accept"][0] = "audio/basic q=1"
1606 hdr["accept"][1] = "audio/*, q=0.2"
1607 hdr["accept"][2] = "*/*, q=0.1"
1608..
1609
Pieter Baauw386a1272015-08-16 15:26:24 +02001610.. js:function:: HTTP.res_get_headers(http)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001611
Patrick Hemmerc6a1d712018-05-01 21:30:41 -04001612 Returns a table containing all the response headers.
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001613
1614 :param class_http http: The related http object.
Patrick Hemmerc6a1d712018-05-01 21:30:41 -04001615 :returns: table of headers.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01001616 :see: :js:func:`HTTP.req_get_headers`
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001617
Patrick Hemmerc6a1d712018-05-01 21:30:41 -04001618 This is the form of the returned table:
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001619
1620.. code-block:: lua
1621
1622 HTTP:res_get_headers()['<header-name>'][<header-index>] = "<header-value>"
1623
1624 local hdr = HTTP:req_get_headers()
1625 hdr["host"][0] = "www.test.com"
1626 hdr["accept"][0] = "audio/basic q=1"
1627 hdr["accept"][1] = "audio/*, q=0.2"
1628 hdr["accept"][2] = "*.*, q=0.1"
1629..
1630
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001631.. js:function:: HTTP.req_add_header(http, name, value)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001632
1633 Appends an HTTP header field in the request whose name is
1634 specified in "name" and whose value is defined in "value".
1635
1636 :param class_http http: The related http object.
1637 :param string name: The header name.
1638 :param string value: The header value.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01001639 :see: :js:func:`HTTP.res_add_header`
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001640
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001641.. js:function:: HTTP.res_add_header(http, name, value)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001642
Bertrand Jacquin874a35c2018-09-10 21:26:07 +01001643 Appends an HTTP header field in the response whose name is
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001644 specified in "name" and whose value is defined in "value".
1645
1646 :param class_http http: The related http object.
1647 :param string name: The header name.
1648 :param string value: The header value.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01001649 :see: :js:func:`HTTP.req_add_header`
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001650
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001651.. js:function:: HTTP.req_del_header(http, name)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001652
1653 Removes all HTTP header fields in the request whose name is
1654 specified in "name".
1655
1656 :param class_http http: The related http object.
1657 :param string name: The header name.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01001658 :see: :js:func:`HTTP.res_del_header`
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001659
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001660.. js:function:: HTTP.res_del_header(http, name)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001661
1662 Removes all HTTP header fields in the response whose name is
1663 specified in "name".
1664
1665 :param class_http http: The related http object.
1666 :param string name: The header name.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01001667 :see: :js:func:`HTTP.req_del_header`
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001668
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001669.. js:function:: HTTP.req_set_header(http, name, value)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001670
Bertrand Jacquin874a35c2018-09-10 21:26:07 +01001671 This variable replace all occurrence of all header "name", by only
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001672 one containing the "value".
1673
1674 :param class_http http: The related http object.
1675 :param string name: The header name.
1676 :param string value: The header value.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01001677 :see: :js:func:`HTTP.res_set_header`
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001678
Bertrand Jacquin874a35c2018-09-10 21:26:07 +01001679 This function does the same work as the following code:
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001680
1681.. code-block:: lua
1682
1683 function fcn(txn)
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001684 TXN.http:req_del_header("header")
1685 TXN.http:req_add_header("header", "value")
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001686 end
1687..
1688
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001689.. js:function:: HTTP.res_set_header(http, name, value)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001690
Bertrand Jacquin874a35c2018-09-10 21:26:07 +01001691 This variable replace all occurrence of all header "name", by only
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001692 one containing the "value".
1693
1694 :param class_http http: The related http object.
1695 :param string name: The header name.
1696 :param string value: The header value.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01001697 :see: :js:func:`HTTP.req_rep_header()`
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001698
Pieter Baauw386a1272015-08-16 15:26:24 +02001699.. js:function:: HTTP.req_rep_header(http, name, regex, replace)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001700
1701 Matches the regular expression in all occurrences of header field "name"
1702 according to "regex", and replaces them with the "replace" argument. The
1703 replacement value can contain back references like \1, \2, ... This
1704 function works with the request.
1705
1706 :param class_http http: The related http object.
1707 :param string name: The header name.
1708 :param string regex: The match regular expression.
1709 :param string replace: The replacement value.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01001710 :see: :js:func:`HTTP.res_rep_header()`
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001711
Pieter Baauw386a1272015-08-16 15:26:24 +02001712.. js:function:: HTTP.res_rep_header(http, name, regex, string)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001713
1714 Matches the regular expression in all occurrences of header field "name"
1715 according to "regex", and replaces them with the "replace" argument. The
1716 replacement value can contain back references like \1, \2, ... This
1717 function works with the request.
1718
1719 :param class_http http: The related http object.
1720 :param string name: The header name.
1721 :param string regex: The match regular expression.
1722 :param string replace: The replacement value.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01001723 :see: :js:func:`HTTP.req_rep_header()`
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001724
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001725.. js:function:: HTTP.req_set_method(http, method)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001726
1727 Rewrites the request method with the parameter "method".
1728
1729 :param class_http http: The related http object.
1730 :param string method: The new method.
1731
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001732.. js:function:: HTTP.req_set_path(http, path)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001733
1734 Rewrites the request path with the "path" parameter.
1735
1736 :param class_http http: The related http object.
1737 :param string path: The new path.
1738
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001739.. js:function:: HTTP.req_set_query(http, query)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001740
1741 Rewrites the request's query string which appears after the first question
1742 mark ("?") with the parameter "query".
1743
1744 :param class_http http: The related http object.
1745 :param string query: The new query.
1746
Thierry FOURNIER0d79cf62015-08-26 14:20:58 +02001747.. js:function:: HTTP.req_set_uri(http, uri)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001748
1749 Rewrites the request URI with the parameter "uri".
1750
1751 :param class_http http: The related http object.
1752 :param string uri: The new uri.
1753
Robin H. Johnson52f5db22017-01-01 13:10:52 -08001754.. js:function:: HTTP.res_set_status(http, status [, reason])
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +02001755
Robin H. Johnson52f5db22017-01-01 13:10:52 -08001756 Rewrites the response status code with the parameter "code".
1757
1758 If no custom reason is provided, it will be generated from the status.
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +02001759
1760 :param class_http http: The related http object.
1761 :param integer status: The new response status code.
Robin H. Johnson52f5db22017-01-01 13:10:52 -08001762 :param string reason: The new response reason (optional).
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +02001763
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001764.. _txn_class:
1765
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001766TXN class
1767=========
1768
1769.. js:class:: TXN
1770
1771 The txn class contain all the functions relative to the http or tcp
1772 transaction (Note than a tcp stream is the same than a tcp transaction, but
1773 an HTTP transaction is not the same than a tcp stream).
1774
1775 The usage of this class permits to retrieve data from the requests, alter it
1776 and forward it.
1777
1778 All the functions provided by this class are available in the context
1779 **sample-fetches** and **actions**.
1780
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001781.. js:attribute:: TXN.c
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001782
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001783 :returns: An :ref:`converters_class`.
1784
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001785 This attribute contains a Converters class object.
1786
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001787.. js:attribute:: TXN.sc
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001788
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001789 :returns: An :ref:`converters_class`.
1790
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001791 This attribute contains a Converters class object. The functions of
1792 this object returns always a string.
1793
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001794.. js:attribute:: TXN.f
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001795
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001796 :returns: An :ref:`fetches_class`.
1797
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001798 This attribute contains a Fetches class object.
1799
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001800.. js:attribute:: TXN.sf
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001801
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001802 :returns: An :ref:`fetches_class`.
1803
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001804 This attribute contains a Fetches class object. The functions of
1805 this object returns always a string.
1806
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001807.. js:attribute:: TXN.req
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001808
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001809 :returns: An :ref:`channel_class`.
1810
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001811 This attribute contains a channel class object for the request buffer.
1812
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001813.. js:attribute:: TXN.res
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001814
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001815 :returns: An :ref:`channel_class`.
1816
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001817 This attribute contains a channel class object for the response buffer.
1818
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001819.. js:attribute:: TXN.http
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001820
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001821 :returns: An :ref:`http_class`.
1822
Bertrand Jacquin874a35c2018-09-10 21:26:07 +01001823 This attribute contains an HTTP class object. It is available only if the
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001824 proxy has the "mode http" enabled.
1825
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01001826.. js:function:: TXN.log(TXN, loglevel, msg)
1827
1828 This function sends a log. The log is sent, according with the HAProxy
1829 configuration file, on the default syslog server if it is configured and on
1830 the stderr if it is allowed.
1831
1832 :param class_txn txn: The class txn object containing the data.
Bertrand Jacquin874a35c2018-09-10 21:26:07 +01001833 :param integer loglevel: Is the log level associated with the message. It is a
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01001834 number between 0 and 7.
1835 :param string msg: The log content.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01001836 :see: :js:attr:`core.emerg`, :js:attr:`core.alert`, :js:attr:`core.crit`,
1837 :js:attr:`core.err`, :js:attr:`core.warning`, :js:attr:`core.notice`,
1838 :js:attr:`core.info`, :js:attr:`core.debug` (log level definitions)
1839 :see: :js:func:`TXN.deflog`
1840 :see: :js:func:`TXN.Debug`
1841 :see: :js:func:`TXN.Info`
1842 :see: :js:func:`TXN.Warning`
1843 :see: :js:func:`TXN.Alert`
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01001844
1845.. js:function:: TXN.deflog(TXN, msg)
1846
Bertrand Jacquin874a35c2018-09-10 21:26:07 +01001847 Sends a log line with the default loglevel for the proxy associated with the
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01001848 transaction.
1849
1850 :param class_txn txn: The class txn object containing the data.
1851 :param string msg: The log content.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01001852 :see: :js:func:`TXN.log
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01001853
1854.. js:function:: TXN.Debug(txn, msg)
1855
1856 :param class_txn txn: The class txn object containing the data.
1857 :param string msg: The log content.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01001858 :see: :js:func:`TXN.log`
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01001859
1860 Does the same job than:
1861
1862.. code-block:: lua
1863
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001864 function Debug(txn, msg)
1865 TXN.log(txn, core.debug, msg)
1866 end
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01001867..
1868
1869.. js:function:: TXN.Info(txn, msg)
1870
1871 :param class_txn txn: The class txn object containing the data.
1872 :param string msg: The log content.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01001873 :see: :js:func:`TXN.log`
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01001874
1875.. code-block:: lua
1876
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001877 function Debug(txn, msg)
1878 TXN.log(txn, core.info, msg)
1879 end
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01001880..
1881
1882.. js:function:: TXN.Warning(txn, msg)
1883
1884 :param class_txn txn: The class txn object containing the data.
1885 :param string msg: The log content.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01001886 :see: :js:func:`TXN.log`
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01001887
1888.. code-block:: lua
1889
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001890 function Debug(txn, msg)
1891 TXN.log(txn, core.warning, msg)
1892 end
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01001893..
1894
1895.. js:function:: TXN.Alert(txn, msg)
1896
1897 :param class_txn txn: The class txn object containing the data.
1898 :param string msg: The log content.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01001899 :see: :js:func:`TXN.log`
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01001900
1901.. code-block:: lua
1902
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001903 function Debug(txn, msg)
1904 TXN.log(txn, core.alert, msg)
1905 end
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01001906..
1907
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001908.. js:function:: TXN.get_priv(txn)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001909
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001910 Return Lua data stored in the current transaction (with the `TXN.set_priv()`)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001911 function. If no data are stored, it returns a nil value.
1912
1913 :param class_txn txn: The class txn object containing the data.
Bertrand Jacquin874a35c2018-09-10 21:26:07 +01001914 :returns: the opaque data previously stored, or nil if nothing is
1915 available.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001916
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001917.. js:function:: TXN.set_priv(txn, data)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001918
1919 Store any data in the current HAProxy transaction. This action replace the
1920 old stored data.
1921
1922 :param class_txn txn: The class txn object containing the data.
1923 :param opaque data: The data which is stored in the transaction.
1924
Tim Duesterhus4e172c92020-05-19 13:49:42 +02001925.. js:function:: TXN.set_var(TXN, var, value[, ifexist])
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +02001926
David Carlier61fdf8b2015-10-02 11:59:38 +01001927 Converts a Lua type in a HAProxy type and store it in a variable <var>.
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +02001928
1929 :param class_txn txn: The class txn object containing the data.
1930 :param string var: The variable name according with the HAProxy variable syntax.
Thierry FOURNIER / OZON.IOb210bcc2016-12-12 16:24:16 +01001931 :param type value: The value associated to the variable. The type can be string or
1932 integer.
Tim Duesterhus4e172c92020-05-19 13:49:42 +02001933 :param boolean ifexist: If this parameter is set to a truthy value the variable
1934 will only be set if it was defined elsewhere (i.e. used
1935 within the configuration). It is highly recommended to
1936 always set this to true.
Christopher Faulet85d79c92016-11-09 16:54:56 +01001937
1938.. js:function:: TXN.unset_var(TXN, var)
1939
1940 Unset the variable <var>.
1941
1942 :param class_txn txn: The class txn object containing the data.
1943 :param string var: The variable name according with the HAProxy variable syntax.
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +02001944
1945.. js:function:: TXN.get_var(TXN, var)
1946
1947 Returns data stored in the variable <var> converter in Lua type.
1948
1949 :param class_txn txn: The class txn object containing the data.
1950 :param string var: The variable name according with the HAProxy variable syntax.
1951
Christopher Faulet700d9e82020-01-31 12:21:52 +01001952.. js:function:: TXN.reply([reply])
1953
1954 Return a new reply object
1955
1956 :param table reply: A table containing info to initialize the reply fields.
1957 :returns: A :ref:`reply_class` object.
1958
1959 The table used to initialized the reply object may contain following entries :
1960
1961 * status : The reply status code. the code 200 is used by default.
1962 * reason : The reply reason. The reason corresponding to the status code is
1963 used by default.
1964 * headers : An list of headers, indexed by header name. Empty by default. For
1965 a given name, multiple values are possible, stored in an ordered list.
1966 * body : The reply body, empty by default.
1967
1968.. code-block:: lua
1969
1970 local reply = txn:reply{
1971 status = 400,
1972 reason = "Bad request",
1973 headers = {
1974 ["content-type"] = { "text/html" },
1975 ["cache-control"] = {"no-cache", "no-store" }
1976 },
1977 body = "<html><body><h1>invalid request<h1></body></html>"
1978 }
1979..
1980 :see: :js:class:`Reply`
1981
1982.. js:function:: TXN.done(txn[, reply])
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001983
Willy Tarreaubc183a62015-08-28 10:39:11 +02001984 This function terminates processing of the transaction and the associated
Christopher Faulet700d9e82020-01-31 12:21:52 +01001985 session and optionally reply to the client for HTTP sessions.
1986
1987 :param class_txn txn: The class txn object containing the data.
1988 :param class_reply reply: The class reply object to return to the client.
1989
1990 This functions can be used when a critical error is detected or to terminate
Willy Tarreaubc183a62015-08-28 10:39:11 +02001991 processing after some data have been returned to the client (eg: a redirect).
Christopher Faulet700d9e82020-01-31 12:21:52 +01001992 To do so, a reply may be provided. This object is optional and may contain a
1993 status code, a reason, a header list and a body. All these fields are
1994 optionnals. When not provided, the default values are used. By default, with
1995 an empty reply object, an empty HTTP 200 response is returned to the
1996 client. If no reply object is provided, the transaction is terminated without
1997 any reply.
1998
1999 The reply object may be fully created in lua or the class Reply may be used to
2000 create it.
2001
2002.. code-block:: lua
2003
2004 local reply = txn:reply()
2005 reply:set_status(400, "Bad request")
2006 reply:add_header("content-type", "text/html")
2007 reply:add_header("cache-control", "no-cache")
2008 reply:add_header("cache-control", "no-store")
2009 reply:set_body("<html><body><h1>invalid request<h1></body></html>")
2010 txn:done(reply)
2011..
2012
2013.. code-block:: lua
2014
2015 txn:done{
2016 status = 400,
2017 reason = "Bad request",
2018 headers = {
2019 ["content-type"] = { "text/html" },
2020 ["cache-control"] = { "no-cache", "no-store" },
2021 },
2022 body = "<html><body><h1>invalid request<h1></body></html>"
2023 }
2024..
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002025
Thierry FOURNIERab00df62016-07-14 11:42:37 +02002026 *Warning*: It not make sense to call this function from sample-fetches. In
2027 this case the behaviour of this one is the same than core.done(): it quit
2028 the Lua execution. The transaction is really aborted only from an action
2029 registered function.
2030
Christopher Faulet700d9e82020-01-31 12:21:52 +01002031 :see: :js:func:`TXN.reply`, :js:class:`Reply`
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002032
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002033.. js:function:: TXN.set_loglevel(txn, loglevel)
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01002034
2035 Is used to change the log level of the current request. The "loglevel" must
2036 be an integer between 0 and 7.
2037
2038 :param class_txn txn: The class txn object containing the data.
2039 :param integer loglevel: The required log level. This variable can be one of
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01002040 :see: :js:attr:`core.emerg`, :js:attr:`core.alert`, :js:attr:`core.crit`,
2041 :js:attr:`core.err`, :js:attr:`core.warning`, :js:attr:`core.notice`,
2042 :js:attr:`core.info`, :js:attr:`core.debug` (log level definitions)
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01002043
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002044.. js:function:: TXN.set_tos(txn, tos)
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01002045
2046 Is used to set the TOS or DSCP field value of packets sent to the client to
2047 the value passed in "tos" on platforms which support this.
2048
2049 :param class_txn txn: The class txn object containing the data.
2050 :param integer tos: The new TOS os DSCP.
2051
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002052.. js:function:: TXN.set_mark(txn, mark)
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01002053
2054 Is used to set the Netfilter MARK on all packets sent to the client to the
2055 value passed in "mark" on platforms which support it.
2056
2057 :param class_txn txn: The class txn object containing the data.
2058 :param integer mark: The mark value.
2059
Patrick Hemmer268a7072018-05-11 12:52:31 -04002060.. js:function:: TXN.set_priority_class(txn, prio)
2061
2062 This function adjusts the priority class of the transaction. The value should
2063 be within the range -2047..2047. Values outside this range will be
2064 truncated.
2065
2066 See the HAProxy configuration.txt file keyword "http-request" action
2067 "set-priority-class" for details.
2068
2069.. js:function:: TXN.set_priority_offset(txn, prio)
2070
2071 This function adjusts the priority offset of the transaction. The value
2072 should be within the range -524287..524287. Values outside this range will be
2073 truncated.
2074
2075 See the HAProxy configuration.txt file keyword "http-request" action
2076 "set-priority-offset" for details.
2077
Christopher Faulet700d9e82020-01-31 12:21:52 +01002078.. _reply_class:
2079
2080Reply class
2081============
2082
2083.. js:class:: Reply
2084
2085 **context**: action
2086
2087 This class represents an HTTP response message. It provides some methods to
2088 enrich it.
2089
2090.. code-block:: lua
2091
2092 local reply = txn:reply({status = 400}) -- default HTTP 400 reason-phase used
2093 reply:add_header("content-type", "text/html")
2094 reply:add_header("cache-control", "no-cache")
2095 reply:add_header("cache-control", "no-store")
2096 reply:set_body("<html><body><h1>invalid request<h1></body></html>")
2097..
2098
2099 :see: :js:func:`TXN.reply`
2100
2101.. js:attribute:: Reply.status
2102
2103 The reply status code. By default, the status code is set to 200.
2104
2105 :returns: integer
2106
2107.. js:attribute:: Reply.reason
2108
2109 The reason string describing the status code.
2110
2111 :returns: string
2112
2113.. js:attribute:: Reply.headers
2114
2115 A table indexing all reply headers by name. To each name is associated an
2116 ordered list of values.
2117
2118 :returns: Lua table
2119
2120.. code-block:: lua
2121
2122 {
2123 ["content-type"] = { "text/html" },
2124 ["cache-control"] = {"no-cache", "no-store" },
2125 x_header_name = { "value1", "value2", ... }
2126 ...
2127 }
2128..
2129
2130.. js:attribute:: Reply.body
2131
2132 The reply payload.
2133
2134 :returns: string
2135
2136.. js:function:: Reply.set_status(REPLY, status[, reason])
2137
2138 Set the reply status code and optionally the reason-phrase. If the reason is
2139 not provided, the default reason corresponding to the status code is used.
2140
2141 :param class_reply reply: The related Reply object.
2142 :param integer status: The reply status code.
2143 :param string reason: The reply status reason (optional).
2144
2145.. js:function:: Reply.add_header(REPLY, name, value)
2146
2147 Add a header to the reply object. If the header does not already exist, a new
2148 entry is created with its name as index and a one-element list containing its
2149 value as value. Otherwise, the header value is appended to the ordered list of
2150 values associated to the header name.
2151
2152 :param class_reply reply: The related Reply object.
2153 :param string name: The header field name.
2154 :param string value: The header field value.
2155
2156.. js:function:: Reply.del_header(REPLY, name)
2157
2158 Remove all occurrences of a header name from the reply object.
2159
2160 :param class_reply reply: The related Reply object.
2161 :param string name: The header field name.
2162
2163.. js:function:: Reply.set_body(REPLY, body)
2164
2165 Set the reply payload.
2166
2167 :param class_reply reply: The related Reply object.
2168 :param string body: The reply payload.
2169
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002170.. _socket_class:
2171
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002172Socket class
2173============
2174
2175.. js:class:: Socket
2176
2177 This class must be compatible with the Lua Socket class. Only the 'client'
2178 functions are available. See the Lua Socket documentation:
2179
2180 `http://w3.impa.br/~diego/software/luasocket/tcp.html
2181 <http://w3.impa.br/~diego/software/luasocket/tcp.html>`_
2182
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002183.. js:function:: Socket.close(socket)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002184
2185 Closes a TCP object. The internal socket used by the object is closed and the
2186 local address to which the object was bound is made available to other
2187 applications. No further operations (except for further calls to the close
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002188 method) are allowed on a closed Socket.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002189
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002190 :param class_socket socket: Is the manipulated Socket.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002191
2192 Note: It is important to close all used sockets once they are not needed,
2193 since, in many systems, each socket uses a file descriptor, which are limited
2194 system resources. Garbage-collected objects are automatically closed before
2195 destruction, though.
2196
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002197.. js:function:: Socket.connect(socket, address[, port])
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002198
2199 Attempts to connect a socket object to a remote host.
2200
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002201
2202 In case of error, the method returns nil followed by a string describing the
2203 error. In case of success, the method returns 1.
2204
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002205 :param class_socket socket: Is the manipulated Socket.
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002206 :param string address: can be an IP address or a host name. See below for more
2207 information.
2208 :param integer port: must be an integer number in the range [1..64K].
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002209 :returns: 1 or nil.
2210
Bertrand Jacquin874a35c2018-09-10 21:26:07 +01002211 An address field extension permits to use the connect() function to connect to
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002212 other stream than TCP. The syntax containing a simpleipv4 or ipv6 address is
2213 the basically expected format. This format requires the port.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002214
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002215 Other format accepted are a socket path like "/socket/path", it permits to
Bertrand Jacquin874a35c2018-09-10 21:26:07 +01002216 connect to a socket. Abstract namespaces are supported with the prefix
Joseph Herlant02cedc42018-11-13 19:45:17 -08002217 "abns@", and finally a file descriptor can be passed with the prefix "fd@".
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002218 The prefix "ipv4@", "ipv6@" and "unix@" are also supported. The port can be
Bertrand Jacquin874a35c2018-09-10 21:26:07 +01002219 passed int the string. The syntax "127.0.0.1:1234" is valid. In this case, the
Tim Duesterhus6edab862018-01-06 19:04:45 +01002220 parameter *port* must not be set.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002221
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002222.. js:function:: Socket.connect_ssl(socket, address, port)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002223
2224 Same behavior than the function socket:connect, but uses SSL.
2225
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002226 :param class_socket socket: Is the manipulated Socket.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002227 :returns: 1 or nil.
2228
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002229.. js:function:: Socket.getpeername(socket)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002230
2231 Returns information about the remote side of a connected client object.
2232
2233 Returns a string with the IP address of the peer, followed by the port number
2234 that peer is using for the connection. In case of error, the method returns
2235 nil.
2236
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002237 :param class_socket socket: Is the manipulated Socket.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002238 :returns: a string containing the server information.
2239
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002240.. js:function:: Socket.getsockname(socket)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002241
2242 Returns the local address information associated to the object.
2243
2244 The method returns a string with local IP address and a number with the port.
2245 In case of error, the method returns nil.
2246
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002247 :param class_socket socket: Is the manipulated Socket.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002248 :returns: a string containing the client information.
2249
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002250.. js:function:: Socket.receive(socket, [pattern [, prefix]])
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002251
2252 Reads data from a client object, according to the specified read pattern.
2253 Patterns follow the Lua file I/O format, and the difference in performance
2254 between all patterns is negligible.
2255
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002256 :param class_socket socket: Is the manipulated Socket.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002257 :param string|integer pattern: Describe what is required (see below).
2258 :param string prefix: A string which will be prefix the returned data.
2259 :returns: a string containing the required data or nil.
2260
2261 Pattern can be any of the following:
2262
2263 * **`*a`**: reads from the socket until the connection is closed. No
2264 end-of-line translation is performed;
2265
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002266 * **`*l`**: reads a line of text from the Socket. The line is terminated by a
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002267 LF character (ASCII 10), optionally preceded by a CR character
2268 (ASCII 13). The CR and LF characters are not included in the
2269 returned line. In fact, all CR characters are ignored by the
2270 pattern. This is the default pattern.
2271
2272 * **number**: causes the method to read a specified number of bytes from the
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002273 Socket. Prefix is an optional string to be concatenated to the
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002274 beginning of any received data before return.
2275
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002276 * **empty**: If the pattern is left empty, the default option is `*l`.
2277
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002278 If successful, the method returns the received pattern. In case of error, the
2279 method returns nil followed by an error message which can be the string
2280 'closed' in case the connection was closed before the transmission was
2281 completed or the string 'timeout' in case there was a timeout during the
2282 operation. Also, after the error message, the function returns the partial
2283 result of the transmission.
2284
2285 Important note: This function was changed severely. It used to support
2286 multiple patterns (but I have never seen this feature used) and now it
2287 doesn't anymore. Partial results used to be returned in the same way as
2288 successful results. This last feature violated the idea that all functions
2289 should return nil on error. Thus it was changed too.
2290
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002291.. js:function:: Socket.send(socket, data [, start [, end ]])
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002292
2293 Sends data through client object.
2294
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002295 :param class_socket socket: Is the manipulated Socket.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002296 :param string data: The data that will be sent.
2297 :param integer start: The start position in the buffer of the data which will
2298 be sent.
2299 :param integer end: The end position in the buffer of the data which will
2300 be sent.
2301 :returns: see below.
2302
2303 Data is the string to be sent. The optional arguments i and j work exactly
2304 like the standard string.sub Lua function to allow the selection of a
2305 substring to be sent.
2306
2307 If successful, the method returns the index of the last byte within [start,
2308 end] that has been sent. Notice that, if start is 1 or absent, this is
2309 effectively the total number of bytes sent. In case of error, the method
2310 returns nil, followed by an error message, followed by the index of the last
2311 byte within [start, end] that has been sent. You might want to try again from
2312 the byte following that. The error message can be 'closed' in case the
2313 connection was closed before the transmission was completed or the string
2314 'timeout' in case there was a timeout during the operation.
2315
2316 Note: Output is not buffered. For small strings, it is always better to
2317 concatenate them in Lua (with the '..' operator) and send the result in one
2318 call instead of calling the method several times.
2319
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002320.. js:function:: Socket.setoption(socket, option [, value])
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002321
2322 Just implemented for compatibility, this cal does nothing.
2323
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002324.. js:function:: Socket.settimeout(socket, value [, mode])
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002325
2326 Changes the timeout values for the object. All I/O operations are blocking.
2327 That is, any call to the methods send, receive, and accept will block
2328 indefinitely, until the operation completes. The settimeout method defines a
2329 limit on the amount of time the I/O methods can block. When a timeout time
2330 has elapsed, the affected methods give up and fail with an error code.
2331
2332 The amount of time to wait is specified as the value parameter, in seconds.
2333
Mark Lakes56cc1252018-03-27 09:48:06 +02002334 The timeout modes are not implemented, the only settable timeout is the
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002335 inactivity time waiting for complete the internal buffer send or waiting for
2336 receive data.
2337
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002338 :param class_socket socket: Is the manipulated Socket.
Bertrand Jacquin874a35c2018-09-10 21:26:07 +01002339 :param float value: The timeout value. Use floating point to specify
Mark Lakes56cc1252018-03-27 09:48:06 +02002340 milliseconds.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002341
Thierry FOURNIER31904272017-10-25 12:59:51 +02002342.. _regex_class:
2343
2344Regex class
2345===========
2346
2347.. js:class:: Regex
2348
2349 This class allows the usage of HAProxy regexes because classic lua doesn't
2350 provides regexes. This class inherits the HAProxy compilation options, so the
2351 regexes can be libc regex, pcre regex or pcre JIT regex.
2352
2353 The expression matching number is limited to 20 per regex. The only available
2354 option is case sensitive.
2355
2356 Because regexes compilation is a heavy process, it is better to define all
2357 your regexes in the **body context** and use it during the runtime.
2358
2359.. code-block:: lua
2360
2361 -- Create the regex
2362 st, regex = Regex.new("needle (..) (...)", true);
2363
2364 -- Check compilation errors
2365 if st == false then
2366 print "error: " .. regex
2367 end
2368
2369 -- Match the regexes
2370 print(regex:exec("Looking for a needle in the haystack")) -- true
2371 print(regex:exec("Lokking for a cat in the haystack")) -- false
2372
2373 -- Extract words
2374 st, list = regex:match("Looking for a needle in the haystack")
2375 print(st) -- true
2376 print(list[1]) -- needle in the
2377 print(list[2]) -- in
2378 print(list[3]) -- the
2379
2380.. js:function:: Regex.new(regex, case_sensitive)
2381
2382 Create and compile a regex.
2383
2384 :param string regex: The regular expression according with the libc or pcre
2385 standard
2386 :param boolean case_sensitive: Match is case sensitive or not.
2387 :returns: boolean status and :ref:`regex_class` or string containing fail reason.
2388
2389.. js:function:: Regex.exec(regex, str)
2390
2391 Execute the regex.
2392
2393 :param class_regex regex: A :ref:`regex_class` object.
2394 :param string str: The input string will be compared with the compiled regex.
2395 :returns: a boolean status according with the match result.
2396
2397.. js:function:: Regex.match(regex, str)
2398
2399 Execute the regex and return matched expressions.
2400
2401 :param class_map map: A :ref:`regex_class` object.
2402 :param string str: The input string will be compared with the compiled regex.
2403 :returns: a boolean status according with the match result, and
2404 a table containing all the string matched in order of declaration.
2405
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002406.. _map_class:
2407
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002408Map class
2409=========
2410
2411.. js:class:: Map
2412
2413 This class permits to do some lookup in HAProxy maps. The declared maps can
Bertrand Jacquin874a35c2018-09-10 21:26:07 +01002414 be modified during the runtime through the HAProxy management socket.
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002415
2416.. code-block:: lua
2417
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002418 default = "usa"
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002419
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002420 -- Create and load map
Thierry FOURNIER4dc71972017-01-28 08:33:08 +01002421 geo = Map.new("geo.map", Map._ip);
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002422
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002423 -- Create new fetch that returns the user country
2424 core.register_fetches("country", function(txn)
2425 local src;
2426 local loc;
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002427
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002428 src = txn.f:fhdr("x-forwarded-for");
2429 if (src == nil) then
2430 src = txn.f:src()
2431 if (src == nil) then
2432 return default;
2433 end
2434 end
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002435
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002436 -- Perform lookup
2437 loc = geo:lookup(src);
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002438
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002439 if (loc == nil) then
2440 return default;
2441 end
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002442
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002443 return loc;
2444 end);
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002445
Thierry FOURNIER4dc71972017-01-28 08:33:08 +01002446.. js:attribute:: Map._int
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002447
2448 See the HAProxy configuration.txt file, chapter "Using ACLs and fetching
Ilya Shipitsin2075ca82020-03-06 23:22:22 +05002449 samples" and subchapter "ACL basics" to understand this pattern matching
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002450 method.
2451
Thierry FOURNIER4dc71972017-01-28 08:33:08 +01002452 Note that :js:attr:`Map.int` is also available for compatibility.
2453
2454.. js:attribute:: Map._ip
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002455
2456 See the HAProxy configuration.txt file, chapter "Using ACLs and fetching
Ilya Shipitsin2075ca82020-03-06 23:22:22 +05002457 samples" and subchapter "ACL basics" to understand this pattern matching
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002458 method.
2459
Thierry FOURNIER4dc71972017-01-28 08:33:08 +01002460 Note that :js:attr:`Map.ip` is also available for compatibility.
2461
2462.. js:attribute:: Map._str
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002463
2464 See the HAProxy configuration.txt file, chapter "Using ACLs and fetching
Ilya Shipitsin2075ca82020-03-06 23:22:22 +05002465 samples" and subchapter "ACL basics" to understand this pattern matching
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002466 method.
2467
Thierry FOURNIER4dc71972017-01-28 08:33:08 +01002468 Note that :js:attr:`Map.str` is also available for compatibility.
2469
2470.. js:attribute:: Map._beg
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002471
2472 See the HAProxy configuration.txt file, chapter "Using ACLs and fetching
Ilya Shipitsin2075ca82020-03-06 23:22:22 +05002473 samples" and subchapter "ACL basics" to understand this pattern matching
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002474 method.
2475
Thierry FOURNIER4dc71972017-01-28 08:33:08 +01002476 Note that :js:attr:`Map.beg` is also available for compatibility.
2477
2478.. js:attribute:: Map._sub
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002479
2480 See the HAProxy configuration.txt file, chapter "Using ACLs and fetching
Ilya Shipitsin2075ca82020-03-06 23:22:22 +05002481 samples" and subchapter "ACL basics" to understand this pattern matching
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002482 method.
2483
Thierry FOURNIER4dc71972017-01-28 08:33:08 +01002484 Note that :js:attr:`Map.sub` is also available for compatibility.
2485
2486.. js:attribute:: Map._dir
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002487
2488 See the HAProxy configuration.txt file, chapter "Using ACLs and fetching
Ilya Shipitsin2075ca82020-03-06 23:22:22 +05002489 samples" and subchapter "ACL basics" to understand this pattern matching
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002490 method.
2491
Thierry FOURNIER4dc71972017-01-28 08:33:08 +01002492 Note that :js:attr:`Map.dir` is also available for compatibility.
2493
2494.. js:attribute:: Map._dom
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002495
2496 See the HAProxy configuration.txt file, chapter "Using ACLs and fetching
Ilya Shipitsin2075ca82020-03-06 23:22:22 +05002497 samples" and subchapter "ACL basics" to understand this pattern matching
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002498 method.
2499
Thierry FOURNIER4dc71972017-01-28 08:33:08 +01002500 Note that :js:attr:`Map.dom` is also available for compatibility.
2501
2502.. js:attribute:: Map._end
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002503
2504 See the HAProxy configuration.txt file, chapter "Using ACLs and fetching
Ilya Shipitsin2075ca82020-03-06 23:22:22 +05002505 samples" and subchapter "ACL basics" to understand this pattern matching
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002506 method.
2507
Thierry FOURNIER4dc71972017-01-28 08:33:08 +01002508.. js:attribute:: Map._reg
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002509
2510 See the HAProxy configuration.txt file, chapter "Using ACLs and fetching
Ilya Shipitsin2075ca82020-03-06 23:22:22 +05002511 samples" and subchapter "ACL basics" to understand this pattern matching
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002512 method.
2513
Thierry FOURNIER4dc71972017-01-28 08:33:08 +01002514 Note that :js:attr:`Map.reg` is also available for compatibility.
2515
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002516
2517.. js:function:: Map.new(file, method)
2518
2519 Creates and load a map.
2520
2521 :param string file: Is the file containing the map.
2522 :param integer method: Is the map pattern matching method. See the attributes
2523 of the Map class.
2524 :returns: a class Map object.
Thierry FOURNIER4dc71972017-01-28 08:33:08 +01002525 :see: The Map attributes: :js:attr:`Map._int`, :js:attr:`Map._ip`,
2526 :js:attr:`Map._str`, :js:attr:`Map._beg`, :js:attr:`Map._sub`,
2527 :js:attr:`Map._dir`, :js:attr:`Map._dom`, :js:attr:`Map._end` and
2528 :js:attr:`Map._reg`.
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002529
2530.. js:function:: Map.lookup(map, str)
2531
2532 Perform a lookup in a map.
2533
2534 :param class_map map: Is the class Map object.
2535 :param string str: Is the string used as key.
2536 :returns: a string containing the result or nil if no match.
2537
2538.. js:function:: Map.slookup(map, str)
2539
2540 Perform a lookup in a map.
2541
2542 :param class_map map: Is the class Map object.
2543 :param string str: Is the string used as key.
2544 :returns: a string containing the result or empty string if no match.
2545
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002546.. _applethttp_class:
2547
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002548AppletHTTP class
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002549================
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002550
2551.. js:class:: AppletHTTP
2552
2553 This class is used with applets that requires the 'http' mode. The http applet
2554 can be registered with the *core.register_service()* function. They are used
2555 for processing an http request like a server in back of HAProxy.
2556
2557 This is an hello world sample code:
2558
2559.. code-block:: lua
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002560
Pieter Baauw4d7f7662015-11-08 16:38:08 +01002561 core.register_service("hello-world", "http", function(applet)
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002562 local response = "Hello World !"
2563 applet:set_status(200)
Pieter Baauw2dcb9bc2015-10-01 22:47:12 +02002564 applet:add_header("content-length", string.len(response))
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002565 applet:add_header("content-type", "text/plain")
Pieter Baauw2dcb9bc2015-10-01 22:47:12 +02002566 applet:start_response()
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002567 applet:send(response)
2568 end)
2569
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002570.. js:attribute:: AppletHTTP.c
2571
2572 :returns: A :ref:`converters_class`
2573
2574 This attribute contains a Converters class object.
2575
2576.. js:attribute:: AppletHTTP.sc
2577
2578 :returns: A :ref:`converters_class`
2579
2580 This attribute contains a Converters class object. The
2581 functions of this object returns always a string.
2582
2583.. js:attribute:: AppletHTTP.f
2584
2585 :returns: A :ref:`fetches_class`
2586
2587 This attribute contains a Fetches class object. Note that the
2588 applet execution place cannot access to a valid HAProxy core HTTP
Ilya Shipitsin2075ca82020-03-06 23:22:22 +05002589 transaction, so some sample fetches related to the HTTP dependent
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002590 values (hdr, path, ...) are not available.
2591
2592.. js:attribute:: AppletHTTP.sf
2593
2594 :returns: A :ref:`fetches_class`
2595
2596 This attribute contains a Fetches class object. The functions of
2597 this object returns always a string. Note that the applet
2598 execution place cannot access to a valid HAProxy core HTTP
Ilya Shipitsin2075ca82020-03-06 23:22:22 +05002599 transaction, so some sample fetches related to the HTTP dependent
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002600 values (hdr, path, ...) are not available.
2601
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01002602.. js:attribute:: AppletHTTP.method
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002603
2604 :returns: string
2605
2606 The attribute method returns a string containing the HTTP
2607 method.
2608
2609.. js:attribute:: AppletHTTP.version
2610
2611 :returns: string
2612
2613 The attribute version, returns a string containing the HTTP
2614 request version.
2615
2616.. js:attribute:: AppletHTTP.path
2617
2618 :returns: string
2619
2620 The attribute path returns a string containing the HTTP
2621 request path.
2622
2623.. js:attribute:: AppletHTTP.qs
2624
2625 :returns: string
2626
2627 The attribute qs returns a string containing the HTTP
2628 request query string.
2629
2630.. js:attribute:: AppletHTTP.length
2631
2632 :returns: integer
2633
2634 The attribute length returns an integer containing the HTTP
2635 body length.
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002636
Thierry FOURNIER841475e2015-12-11 17:10:09 +01002637.. js:attribute:: AppletHTTP.headers
2638
Patrick Hemmerc6a1d712018-05-01 21:30:41 -04002639 :returns: table
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002640
Patrick Hemmerc6a1d712018-05-01 21:30:41 -04002641 The attribute headers returns a table containing the HTTP
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002642 headers. The header names are always in lower case. As the header name can be
2643 encountered more than once in each request, the value is indexed with 0 as
Patrick Hemmerc6a1d712018-05-01 21:30:41 -04002644 first index value. The table have this form:
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002645
2646.. code-block:: lua
2647
2648 AppletHTTP.headers['<header-name>'][<header-index>] = "<header-value>"
2649
2650 AppletHTTP.headers["host"][0] = "www.test.com"
2651 AppletHTTP.headers["accept"][0] = "audio/basic q=1"
2652 AppletHTTP.headers["accept"][1] = "audio/*, q=0.2"
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01002653 AppletHTTP.headers["accept"][2] = "*/*, q=0.1"
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002654..
2655
Robin H. Johnson52f5db22017-01-01 13:10:52 -08002656.. js:function:: AppletHTTP.set_status(applet, code [, reason])
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002657
2658 This function sets the HTTP status code for the response. The allowed code are
2659 from 100 to 599.
2660
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01002661 :param class_AppletHTTP applet: An :ref:`applethttp_class`
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002662 :param integer code: the status code returned to the client.
Robin H. Johnson52f5db22017-01-01 13:10:52 -08002663 :param string reason: the status reason returned to the client (optional).
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002664
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01002665.. js:function:: AppletHTTP.add_header(applet, name, value)
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002666
2667 This function add an header in the response. Duplicated headers are not
2668 collapsed. The special header *content-length* is used to determinate the
2669 response length. If it not exists, a *transfer-encoding: chunked* is set, and
Ilya Shipitsin2075ca82020-03-06 23:22:22 +05002670 all the write from the function *AppletHTTP:send()* become a chunk.
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002671
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01002672 :param class_AppletHTTP applet: An :ref:`applethttp_class`
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002673 :param string name: the header name
2674 :param string value: the header value
2675
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01002676.. js:function:: AppletHTTP.start_response(applet)
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002677
2678 This function indicates to the HTTP engine that it can process and send the
2679 response headers. After this called we cannot add headers to the response; We
2680 cannot use the *AppletHTTP:send()* function if the
2681 *AppletHTTP:start_response()* is not called.
2682
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01002683 :param class_AppletHTTP applet: An :ref:`applethttp_class`
2684
2685.. js:function:: AppletHTTP.getline(applet)
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002686
2687 This function returns a string containing one line from the http body. If the
2688 data returned doesn't contains a final '\\n' its assumed than its the last
2689 available data before the end of stream.
2690
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01002691 :param class_AppletHTTP applet: An :ref:`applethttp_class`
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002692 :returns: a string. The string can be empty if we reach the end of the stream.
2693
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01002694.. js:function:: AppletHTTP.receive(applet, [size])
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002695
2696 Reads data from the HTTP body, according to the specified read *size*. If the
2697 *size* is missing, the function tries to read all the content of the stream
2698 until the end. If the *size* is bigger than the http body, it returns the
Bertrand Jacquin874a35c2018-09-10 21:26:07 +01002699 amount of data available.
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002700
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01002701 :param class_AppletHTTP applet: An :ref:`applethttp_class`
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002702 :param integer size: the required read size.
Ilya Shipitsin11057a32020-06-21 21:18:27 +05002703 :returns: always return a string,the string can be empty is the connection is
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002704 closed.
2705
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01002706.. js:function:: AppletHTTP.send(applet, msg)
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002707
2708 Send the message *msg* on the http request body.
2709
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01002710 :param class_AppletHTTP applet: An :ref:`applethttp_class`
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002711 :param string msg: the message to send.
2712
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01002713.. js:function:: AppletHTTP.get_priv(applet)
2714
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01002715 Return Lua data stored in the current transaction. If no data are stored,
2716 it returns a nil value.
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01002717
2718 :param class_AppletHTTP applet: An :ref:`applethttp_class`
Bertrand Jacquin874a35c2018-09-10 21:26:07 +01002719 :returns: the opaque data previously stored, or nil if nothing is
2720 available.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01002721 :see: :js:func:`AppletHTTP.set_priv`
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01002722
2723.. js:function:: AppletHTTP.set_priv(applet, data)
2724
2725 Store any data in the current HAProxy transaction. This action replace the
2726 old stored data.
2727
2728 :param class_AppletHTTP applet: An :ref:`applethttp_class`
2729 :param opaque data: The data which is stored in the transaction.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01002730 :see: :js:func:`AppletHTTP.get_priv`
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01002731
Tim Duesterhus4e172c92020-05-19 13:49:42 +02002732.. js:function:: AppletHTTP.set_var(applet, var, value[, ifexist])
Thierry FOURNIER / OZON.IOc1edafe2016-12-12 16:25:30 +01002733
2734 Converts a Lua type in a HAProxy type and store it in a variable <var>.
2735
2736 :param class_AppletHTTP applet: An :ref:`applethttp_class`
2737 :param string var: The variable name according with the HAProxy variable syntax.
2738 :param type value: The value associated to the variable. The type ca be string or
2739 integer.
Tim Duesterhus4e172c92020-05-19 13:49:42 +02002740 :param boolean ifexist: If this parameter is set to a truthy value the variable
2741 will only be set if it was defined elsewhere (i.e. used
2742 within the configuration). It is highly recommended to
2743 always set this to true.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01002744 :see: :js:func:`AppletHTTP.unset_var`
2745 :see: :js:func:`AppletHTTP.get_var`
Thierry FOURNIER / OZON.IOc1edafe2016-12-12 16:25:30 +01002746
2747.. js:function:: AppletHTTP.unset_var(applet, var)
2748
2749 Unset the variable <var>.
2750
2751 :param class_AppletHTTP applet: An :ref:`applethttp_class`
2752 :param string var: The variable name according with the HAProxy variable syntax.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01002753 :see: :js:func:`AppletHTTP.set_var`
2754 :see: :js:func:`AppletHTTP.get_var`
Thierry FOURNIER / OZON.IOc1edafe2016-12-12 16:25:30 +01002755
2756.. js:function:: AppletHTTP.get_var(applet, var)
2757
2758 Returns data stored in the variable <var> converter in Lua type.
2759
2760 :param class_AppletHTTP applet: An :ref:`applethttp_class`
2761 :param string var: The variable name according with the HAProxy variable syntax.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01002762 :see: :js:func:`AppletHTTP.set_var`
2763 :see: :js:func:`AppletHTTP.unset_var`
Thierry FOURNIER / OZON.IOc1edafe2016-12-12 16:25:30 +01002764
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002765.. _applettcp_class:
2766
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002767AppletTCP class
2768===============
2769
2770.. js:class:: AppletTCP
2771
2772 This class is used with applets that requires the 'tcp' mode. The tcp applet
2773 can be registered with the *core.register_service()* function. They are used
2774 for processing a tcp stream like a server in back of HAProxy.
2775
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002776.. js:attribute:: AppletTCP.c
2777
2778 :returns: A :ref:`converters_class`
2779
2780 This attribute contains a Converters class object.
2781
2782.. js:attribute:: AppletTCP.sc
2783
2784 :returns: A :ref:`converters_class`
2785
2786 This attribute contains a Converters class object. The
2787 functions of this object returns always a string.
2788
2789.. js:attribute:: AppletTCP.f
2790
2791 :returns: A :ref:`fetches_class`
2792
2793 This attribute contains a Fetches class object.
2794
2795.. js:attribute:: AppletTCP.sf
2796
2797 :returns: A :ref:`fetches_class`
2798
2799 This attribute contains a Fetches class object.
2800
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01002801.. js:function:: AppletTCP.getline(applet)
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002802
2803 This function returns a string containing one line from the stream. If the
2804 data returned doesn't contains a final '\\n' its assumed than its the last
2805 available data before the end of stream.
2806
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01002807 :param class_AppletTCP applet: An :ref:`applettcp_class`
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002808 :returns: a string. The string can be empty if we reach the end of the stream.
2809
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01002810.. js:function:: AppletTCP.receive(applet, [size])
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002811
2812 Reads data from the TCP stream, according to the specified read *size*. If the
2813 *size* is missing, the function tries to read all the content of the stream
2814 until the end.
2815
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01002816 :param class_AppletTCP applet: An :ref:`applettcp_class`
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002817 :param integer size: the required read size.
Ilya Shipitsin11057a32020-06-21 21:18:27 +05002818 :returns: always return a string,the string can be empty is the connection is
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002819 closed.
2820
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01002821.. js:function:: AppletTCP.send(appletmsg)
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002822
2823 Send the message on the stream.
2824
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01002825 :param class_AppletTCP applet: An :ref:`applettcp_class`
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002826 :param string msg: the message to send.
2827
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01002828.. js:function:: AppletTCP.get_priv(applet)
2829
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01002830 Return Lua data stored in the current transaction. If no data are stored,
2831 it returns a nil value.
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01002832
2833 :param class_AppletTCP applet: An :ref:`applettcp_class`
Bertrand Jacquin874a35c2018-09-10 21:26:07 +01002834 :returns: the opaque data previously stored, or nil if nothing is
2835 available.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01002836 :see: :js:func:`AppletTCP.set_priv`
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01002837
2838.. js:function:: AppletTCP.set_priv(applet, data)
2839
2840 Store any data in the current HAProxy transaction. This action replace the
2841 old stored data.
2842
2843 :param class_AppletTCP applet: An :ref:`applettcp_class`
2844 :param opaque data: The data which is stored in the transaction.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01002845 :see: :js:func:`AppletTCP.get_priv`
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01002846
Tim Duesterhus4e172c92020-05-19 13:49:42 +02002847.. js:function:: AppletTCP.set_var(applet, var, value[, ifexist])
Thierry FOURNIER / OZON.IOc1edafe2016-12-12 16:25:30 +01002848
2849 Converts a Lua type in a HAProxy type and stores it in a variable <var>.
2850
2851 :param class_AppletTCP applet: An :ref:`applettcp_class`
2852 :param string var: The variable name according with the HAProxy variable syntax.
2853 :param type value: The value associated to the variable. The type can be string or
2854 integer.
Tim Duesterhus4e172c92020-05-19 13:49:42 +02002855 :param boolean ifexist: If this parameter is set to a truthy value the variable
2856 will only be set if it was defined elsewhere (i.e. used
2857 within the configuration). It is highly recommended to
2858 always set this to true.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01002859 :see: :js:func:`AppletTCP.unset_var`
2860 :see: :js:func:`AppletTCP.get_var`
Thierry FOURNIER / OZON.IOc1edafe2016-12-12 16:25:30 +01002861
2862.. js:function:: AppletTCP.unset_var(applet, var)
2863
2864 Unsets the variable <var>.
2865
2866 :param class_AppletTCP applet: An :ref:`applettcp_class`
2867 :param string var: The variable name according with the HAProxy variable syntax.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01002868 :see: :js:func:`AppletTCP.unset_var`
2869 :see: :js:func:`AppletTCP.set_var`
Thierry FOURNIER / OZON.IOc1edafe2016-12-12 16:25:30 +01002870
2871.. js:function:: AppletTCP.get_var(applet, var)
2872
2873 Returns data stored in the variable <var> converter in Lua type.
2874
2875 :param class_AppletTCP applet: An :ref:`applettcp_class`
2876 :param string var: The variable name according with the HAProxy variable syntax.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01002877 :see: :js:func:`AppletTCP.unset_var`
2878 :see: :js:func:`AppletTCP.set_var`
Thierry FOURNIER / OZON.IOc1edafe2016-12-12 16:25:30 +01002879
Adis Nezirovic8878f8e2018-07-13 12:18:33 +02002880StickTable class
2881================
2882
2883.. js:class:: StickTable
2884
2885 **context**: task, action, sample-fetch
2886
2887 This class can be used to access the HAProxy stick tables from Lua.
2888
2889.. js:function:: StickTable.info()
2890
2891 Returns stick table attributes as a Lua table. See HAProxy documentation for
Ilya Shipitsin2272d8a2020-12-21 01:22:40 +05002892 "stick-table" for canonical info, or check out example below.
Adis Nezirovic8878f8e2018-07-13 12:18:33 +02002893
2894 :returns: Lua table
2895
2896 Assume our table has IPv4 key and gpc0 and conn_rate "columns":
2897
2898.. code-block:: lua
2899
2900 {
2901 expire=<int>, # Value in ms
2902 size=<int>, # Maximum table size
2903 used=<int>, # Actual number of entries in table
2904 data={ # Data columns, with types as key, and periods as values
2905 (-1 if type is not rate counter)
2906 conn_rate=<int>,
2907 gpc0=-1
2908 },
2909 length=<int>, # max string length for string table keys, key length
2910 # otherwise
2911 nopurge=<boolean>, # purge oldest entries when table is full
2912 type="ip" # can be "ip", "ipv6", "integer", "string", "binary"
2913 }
2914
2915.. js:function:: StickTable.lookup(key)
2916
2917 Returns stick table entry for given <key>
2918
2919 :param string key: Stick table key (IP addresses and strings are supported)
2920 :returns: Lua table
2921
2922.. js:function:: StickTable.dump([filter])
2923
2924 Returns all entries in stick table. An optional filter can be used
2925 to extract entries with specific data values. Filter is a table with valid
2926 comparison operators as keys followed by data type name and value pairs.
2927 Check out the HAProxy docs for "show table" for more details. For the
2928 reference, the supported operators are:
2929 "eq", "ne", "le", "lt", "ge", "gt"
2930
2931 For large tables, execution of this function can take a long time (for
2932 HAProxy standards). That's also true when filter is used, so take care and
2933 measure the impact.
2934
2935 :param table filter: Stick table filter
2936 :returns: Stick table entries (table)
2937
2938 See below for example filter, which contains 4 entries (or comparisons).
2939 (Maximum number of filter entries is 4, defined in the source code)
2940
2941.. code-block:: lua
2942
2943 local filter = {
2944 {"gpc0", "gt", 30}, {"gpc1", "gt", 20}}, {"conn_rate", "le", 10}
2945 }
2946
Christopher Faulet0f3c8902020-01-31 18:57:12 +01002947.. _action_class:
2948
2949Action class
2950=============
2951
2952.. js:class:: Act
2953
2954 **context**: action
2955
2956 This class contains all return codes an action may return. It is the lua
2957 equivalent to HAProxy "ACT_RET_*" code.
2958
2959.. code-block:: lua
2960
2961 core.register_action("deny", { "http-req" }, function (txn)
2962 return act.DENY
2963 end)
2964..
2965.. js:attribute:: act.CONTINUE
2966
2967 This attribute is an integer (0). It instructs HAProxy to continue the current
2968 ruleset processing on the message. It is the default return code for a lua
2969 action.
2970
2971 :returns: integer
2972
2973.. js:attribute:: act.STOP
2974
2975 This attribute is an integer (1). It instructs HAProxy to stop the current
2976 ruleset processing on the message.
2977
2978.. js:attribute:: act.YIELD
2979
2980 This attribute is an integer (2). It instructs HAProxy to temporarily pause
2981 the message processing. It will be resumed later on the same rule. The
2982 corresponding lua script is re-executed for the start.
2983
2984.. js:attribute:: act.ERROR
2985
2986 This attribute is an integer (3). It triggers an internal errors The message
2987 processing is stopped and the transaction is terminated. For HTTP streams, an
2988 HTTP 500 error is returned to the client.
2989
2990 :returns: integer
2991
2992.. js:attribute:: act.DONE
2993
2994 This attribute is an integer (4). It instructs HAProxy to stop the message
2995 processing.
2996
2997 :returns: integer
2998
2999.. js:attribute:: act.DENY
3000
3001 This attribute is an integer (5). It denies the current message. The message
3002 processing is stopped and the transaction is terminated. For HTTP streams, an
3003 HTTP 403 error is returned to the client if the deny is returned during the
3004 request analysis. During the response analysis, an HTTP 502 error is returned
3005 and the server response is discarded.
3006
3007 :returns: integer
3008
3009.. js:attribute:: act.ABORT
3010
3011 This attribute is an integer (6). It aborts the current message. The message
3012 processing is stopped and the transaction is terminated. For HTTP streams,
Willy Tarreau714f3452021-05-09 06:47:26 +02003013 HAProxy assumes a response was already sent to the client. From the Lua
Christopher Faulet0f3c8902020-01-31 18:57:12 +01003014 actions point of view, when this code is used, the transaction is terminated
3015 with no reply.
3016
3017 :returns: integer
3018
3019.. js:attribute:: act.INVALID
3020
3021 This attribute is an integer (7). It triggers an internal errors. The message
3022 processing is stopped and the transaction is terminated. For HTTP streams, an
3023 HTTP 400 error is returned to the client if the error is returned during the
3024 request analysis. During the response analysis, an HTTP 502 error is returned
3025 and the server response is discarded.
3026
3027 :returns: integer
Adis Nezirovic8878f8e2018-07-13 12:18:33 +02003028
Christopher Faulet2c2c2e32020-01-31 19:07:52 +01003029.. js:function:: act:wake_time(milliseconds)
3030
3031 **context**: action
3032
3033 Set the script pause timeout to the specified time, defined in
3034 milliseconds.
3035
3036 :param integer milliseconds: the required milliseconds.
3037
3038 This function may be used when a lua action returns `act.YIELD`, to force its
3039 wake-up at most after the specified number of milliseconds.
3040
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01003041External Lua libraries
3042======================
3043
3044A lot of useful lua libraries can be found here:
3045
3046* `https://lua-toolbox.com/ <https://lua-toolbox.com/>`_
3047
Ilya Shipitsin2075ca82020-03-06 23:22:22 +05003048Redis client library:
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01003049
3050* `https://github.com/nrk/redis-lua <https://github.com/nrk/redis-lua>`_
3051
3052This is an example about the usage of the Redis library with HAProxy. Note that
3053each call of any function of this library can throw an error if the socket
3054connection fails.
3055
3056.. code-block:: lua
3057
3058 -- load the redis library
3059 local redis = require("redis");
3060
3061 function do_something(txn)
3062
3063 -- create and connect new tcp socket
3064 local tcp = core.tcp();
3065 tcp:settimeout(1);
3066 tcp:connect("127.0.0.1", 6379);
3067
3068 -- use the redis library with this new socket
3069 local client = redis.connect({socket=tcp});
3070 client:ping();
3071
3072 end
3073
3074OpenSSL:
3075
3076* `http://mkottman.github.io/luacrypto/index.html
3077 <http://mkottman.github.io/luacrypto/index.html>`_
3078
3079* `https://github.com/brunoos/luasec/wiki
3080 <https://github.com/brunoos/luasec/wiki>`_