blob: d98c77eeb472d7573c68a1455a9583361143105c [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
1281 **Warning**: It is not possible to read from the response in request action,
1282 and it is not possible to read for the request channel in response action.
1283
Christopher Faulet1cda6c82021-06-14 11:43:18 +02001284 **Warning**: It is forbidden to alter the Channels buffer from HTTP contexts.
1285 So only :js:func:`Channel.get_in_length`, :js:func:`Channel.get_out_length`
1286 and :js:func:`Channel.is_full` can be called from an HTTP conetext.
1287
Thierry FOURNIER2e4893c2015-03-18 13:37:27 +01001288.. image:: _static/channel.png
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001289
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001290.. js:function:: Channel.dup(channel)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001291
1292 This function returns a string that contain the entire buffer. The data is
1293 not remove from the buffer and can be reprocessed later.
1294
Ilya Shipitsin2075ca82020-03-06 23:22:22 +05001295 If the buffer can't receive more data, a 'nil' value is returned.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001296
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001297 :param class_channel channel: The manipulated Channel.
Pieter Baauw4d7f7662015-11-08 16:38:08 +01001298 :returns: a string containing all the available data or nil.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001299
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001300.. js:function:: Channel.get(channel)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001301
1302 This function returns a string that contain the entire buffer. The data is
1303 consumed from the buffer.
1304
Ilya Shipitsin2075ca82020-03-06 23:22:22 +05001305 If the buffer can't receive more data, a 'nil' value is returned.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001306
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001307 :param class_channel channel: The manipulated Channel.
Pieter Baauw4d7f7662015-11-08 16:38:08 +01001308 :returns: a string containing all the available data or nil.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001309
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02001310.. js:function:: Channel.getline(channel)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001311
1312 This function returns a string that contain the first line of the buffer. The
1313 data is consumed. If the data returned doesn't contains a final '\n' its
1314 assumed than its the last available data in the buffer.
1315
Ilya Shipitsin2075ca82020-03-06 23:22:22 +05001316 If the buffer can't receive more data, a 'nil' value is returned.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001317
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001318 :param class_channel channel: The manipulated Channel.
Pieter Baauw386a1272015-08-16 15:26:24 +02001319 :returns: a string containing the available line or nil.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001320
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001321.. js:function:: Channel.set(channel, string)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001322
1323 This function replace the content of the buffer by the string. The function
1324 returns the copied length, otherwise, it returns -1.
1325
1326 The data set with this function are not send. They wait for the end of
1327 HAProxy processing, so the buffer can be full.
1328
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001329 :param class_channel channel: The manipulated Channel.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001330 :param string string: The data which will sent.
Pieter Baauw4d7f7662015-11-08 16:38:08 +01001331 :returns: an integer containing the amount of bytes copied or -1.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001332
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001333.. js:function:: Channel.append(channel, string)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001334
1335 This function append the string argument to the content of the buffer. The
1336 function returns the copied length, otherwise, it returns -1.
1337
1338 The data set with this function are not send. They wait for the end of
1339 HAProxy processing, so the buffer can be full.
1340
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001341 :param class_channel channel: The manipulated Channel.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001342 :param string string: The data which will sent.
Pieter Baauw4d7f7662015-11-08 16:38:08 +01001343 :returns: an integer containing the amount of bytes copied or -1.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001344
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001345.. js:function:: Channel.send(channel, string)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001346
1347 This function required immediate send of the data. Unless if the connection
1348 is close, the buffer is regularly flushed and all the string can be sent.
1349
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001350 :param class_channel channel: The manipulated Channel.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001351 :param string string: The data which will sent.
Pieter Baauw4d7f7662015-11-08 16:38:08 +01001352 :returns: an integer containing the amount of bytes copied or -1.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001353
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001354.. js:function:: Channel.get_in_length(channel)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001355
1356 This function returns the length of the input part of the buffer.
1357
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001358 :param class_channel channel: The manipulated Channel.
Pieter Baauw4d7f7662015-11-08 16:38:08 +01001359 :returns: an integer containing the amount of available bytes.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001360
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001361.. js:function:: Channel.get_out_length(channel)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001362
1363 This function returns the length of the output part of the buffer.
1364
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001365 :param class_channel channel: The manipulated Channel.
Pieter Baauw4d7f7662015-11-08 16:38:08 +01001366 :returns: an integer containing the amount of available bytes.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001367
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001368.. js:function:: Channel.forward(channel, int)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001369
1370 This function transfer bytes from the input part of the buffer to the output
1371 part.
1372
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001373 :param class_channel channel: The manipulated Channel.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001374 :param integer int: The amount of data which will be forwarded.
1375
Thierry FOURNIER / OZON.IO65192f32016-11-07 15:28:40 +01001376.. js:function:: Channel.is_full(channel)
1377
1378 This function returns true if the buffer channel is full.
1379
1380 :returns: a boolean
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001381
1382.. _http_class:
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001383
1384HTTP class
1385==========
1386
1387.. js:class:: HTTP
1388
1389 This class contain all the HTTP manipulation functions.
1390
Pieter Baauw386a1272015-08-16 15:26:24 +02001391.. js:function:: HTTP.req_get_headers(http)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001392
Patrick Hemmerc6a1d712018-05-01 21:30:41 -04001393 Returns a table containing all the request headers.
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001394
1395 :param class_http http: The related http object.
Patrick Hemmerc6a1d712018-05-01 21:30:41 -04001396 :returns: table of headers.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01001397 :see: :js:func:`HTTP.res_get_headers`
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001398
Patrick Hemmerc6a1d712018-05-01 21:30:41 -04001399 This is the form of the returned table:
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001400
1401.. code-block:: lua
1402
1403 HTTP:req_get_headers()['<header-name>'][<header-index>] = "<header-value>"
1404
1405 local hdr = HTTP:req_get_headers()
1406 hdr["host"][0] = "www.test.com"
1407 hdr["accept"][0] = "audio/basic q=1"
1408 hdr["accept"][1] = "audio/*, q=0.2"
1409 hdr["accept"][2] = "*/*, q=0.1"
1410..
1411
Pieter Baauw386a1272015-08-16 15:26:24 +02001412.. js:function:: HTTP.res_get_headers(http)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001413
Patrick Hemmerc6a1d712018-05-01 21:30:41 -04001414 Returns a table containing all the response headers.
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001415
1416 :param class_http http: The related http object.
Patrick Hemmerc6a1d712018-05-01 21:30:41 -04001417 :returns: table of headers.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01001418 :see: :js:func:`HTTP.req_get_headers`
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001419
Patrick Hemmerc6a1d712018-05-01 21:30:41 -04001420 This is the form of the returned table:
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001421
1422.. code-block:: lua
1423
1424 HTTP:res_get_headers()['<header-name>'][<header-index>] = "<header-value>"
1425
1426 local hdr = HTTP:req_get_headers()
1427 hdr["host"][0] = "www.test.com"
1428 hdr["accept"][0] = "audio/basic q=1"
1429 hdr["accept"][1] = "audio/*, q=0.2"
1430 hdr["accept"][2] = "*.*, q=0.1"
1431..
1432
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001433.. js:function:: HTTP.req_add_header(http, name, value)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001434
1435 Appends an HTTP header field in the request whose name is
1436 specified in "name" and whose value is defined in "value".
1437
1438 :param class_http http: The related http object.
1439 :param string name: The header name.
1440 :param string value: The header value.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01001441 :see: :js:func:`HTTP.res_add_header`
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001442
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001443.. js:function:: HTTP.res_add_header(http, name, value)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001444
Bertrand Jacquin874a35c2018-09-10 21:26:07 +01001445 Appends an HTTP header field in the response whose name is
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001446 specified in "name" and whose value is defined in "value".
1447
1448 :param class_http http: The related http object.
1449 :param string name: The header name.
1450 :param string value: The header value.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01001451 :see: :js:func:`HTTP.req_add_header`
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001452
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001453.. js:function:: HTTP.req_del_header(http, name)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001454
1455 Removes all HTTP header fields in the request whose name is
1456 specified in "name".
1457
1458 :param class_http http: The related http object.
1459 :param string name: The header name.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01001460 :see: :js:func:`HTTP.res_del_header`
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001461
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001462.. js:function:: HTTP.res_del_header(http, name)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001463
1464 Removes all HTTP header fields in the response whose name is
1465 specified in "name".
1466
1467 :param class_http http: The related http object.
1468 :param string name: The header name.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01001469 :see: :js:func:`HTTP.req_del_header`
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001470
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001471.. js:function:: HTTP.req_set_header(http, name, value)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001472
Bertrand Jacquin874a35c2018-09-10 21:26:07 +01001473 This variable replace all occurrence of all header "name", by only
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001474 one containing the "value".
1475
1476 :param class_http http: The related http object.
1477 :param string name: The header name.
1478 :param string value: The header value.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01001479 :see: :js:func:`HTTP.res_set_header`
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001480
Bertrand Jacquin874a35c2018-09-10 21:26:07 +01001481 This function does the same work as the following code:
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001482
1483.. code-block:: lua
1484
1485 function fcn(txn)
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001486 TXN.http:req_del_header("header")
1487 TXN.http:req_add_header("header", "value")
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001488 end
1489..
1490
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001491.. js:function:: HTTP.res_set_header(http, name, value)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001492
Bertrand Jacquin874a35c2018-09-10 21:26:07 +01001493 This variable replace all occurrence of all header "name", by only
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001494 one containing the "value".
1495
1496 :param class_http http: The related http object.
1497 :param string name: The header name.
1498 :param string value: The header value.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01001499 :see: :js:func:`HTTP.req_rep_header()`
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001500
Pieter Baauw386a1272015-08-16 15:26:24 +02001501.. js:function:: HTTP.req_rep_header(http, name, regex, replace)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001502
1503 Matches the regular expression in all occurrences of header field "name"
1504 according to "regex", and replaces them with the "replace" argument. The
1505 replacement value can contain back references like \1, \2, ... This
1506 function works with the request.
1507
1508 :param class_http http: The related http object.
1509 :param string name: The header name.
1510 :param string regex: The match regular expression.
1511 :param string replace: The replacement value.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01001512 :see: :js:func:`HTTP.res_rep_header()`
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001513
Pieter Baauw386a1272015-08-16 15:26:24 +02001514.. js:function:: HTTP.res_rep_header(http, name, regex, string)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001515
1516 Matches the regular expression in all occurrences of header field "name"
1517 according to "regex", and replaces them with the "replace" argument. The
1518 replacement value can contain back references like \1, \2, ... This
1519 function works with the request.
1520
1521 :param class_http http: The related http object.
1522 :param string name: The header name.
1523 :param string regex: The match regular expression.
1524 :param string replace: The replacement value.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01001525 :see: :js:func:`HTTP.req_rep_header()`
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001526
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001527.. js:function:: HTTP.req_set_method(http, method)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001528
1529 Rewrites the request method with the parameter "method".
1530
1531 :param class_http http: The related http object.
1532 :param string method: The new method.
1533
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001534.. js:function:: HTTP.req_set_path(http, path)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001535
1536 Rewrites the request path with the "path" parameter.
1537
1538 :param class_http http: The related http object.
1539 :param string path: The new path.
1540
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001541.. js:function:: HTTP.req_set_query(http, query)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001542
1543 Rewrites the request's query string which appears after the first question
1544 mark ("?") with the parameter "query".
1545
1546 :param class_http http: The related http object.
1547 :param string query: The new query.
1548
Thierry FOURNIER0d79cf62015-08-26 14:20:58 +02001549.. js:function:: HTTP.req_set_uri(http, uri)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001550
1551 Rewrites the request URI with the parameter "uri".
1552
1553 :param class_http http: The related http object.
1554 :param string uri: The new uri.
1555
Robin H. Johnson52f5db22017-01-01 13:10:52 -08001556.. js:function:: HTTP.res_set_status(http, status [, reason])
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +02001557
Robin H. Johnson52f5db22017-01-01 13:10:52 -08001558 Rewrites the response status code with the parameter "code".
1559
1560 If no custom reason is provided, it will be generated from the status.
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +02001561
1562 :param class_http http: The related http object.
1563 :param integer status: The new response status code.
Robin H. Johnson52f5db22017-01-01 13:10:52 -08001564 :param string reason: The new response reason (optional).
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +02001565
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001566.. _txn_class:
1567
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001568TXN class
1569=========
1570
1571.. js:class:: TXN
1572
1573 The txn class contain all the functions relative to the http or tcp
1574 transaction (Note than a tcp stream is the same than a tcp transaction, but
1575 an HTTP transaction is not the same than a tcp stream).
1576
1577 The usage of this class permits to retrieve data from the requests, alter it
1578 and forward it.
1579
1580 All the functions provided by this class are available in the context
1581 **sample-fetches** and **actions**.
1582
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001583.. js:attribute:: TXN.c
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001584
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001585 :returns: An :ref:`converters_class`.
1586
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001587 This attribute contains a Converters class object.
1588
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001589.. js:attribute:: TXN.sc
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001590
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001591 :returns: An :ref:`converters_class`.
1592
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001593 This attribute contains a Converters class object. The functions of
1594 this object returns always a string.
1595
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001596.. js:attribute:: TXN.f
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001597
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001598 :returns: An :ref:`fetches_class`.
1599
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001600 This attribute contains a Fetches class object.
1601
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001602.. js:attribute:: TXN.sf
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001603
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001604 :returns: An :ref:`fetches_class`.
1605
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001606 This attribute contains a Fetches class object. The functions of
1607 this object returns always a string.
1608
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001609.. js:attribute:: TXN.req
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001610
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001611 :returns: An :ref:`channel_class`.
1612
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001613 This attribute contains a channel class object for the request buffer.
1614
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001615.. js:attribute:: TXN.res
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001616
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001617 :returns: An :ref:`channel_class`.
1618
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001619 This attribute contains a channel class object for the response buffer.
1620
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001621.. js:attribute:: TXN.http
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001622
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001623 :returns: An :ref:`http_class`.
1624
Bertrand Jacquin874a35c2018-09-10 21:26:07 +01001625 This attribute contains an HTTP class object. It is available only if the
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001626 proxy has the "mode http" enabled.
1627
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01001628.. js:function:: TXN.log(TXN, loglevel, msg)
1629
1630 This function sends a log. The log is sent, according with the HAProxy
1631 configuration file, on the default syslog server if it is configured and on
1632 the stderr if it is allowed.
1633
1634 :param class_txn txn: The class txn object containing the data.
Bertrand Jacquin874a35c2018-09-10 21:26:07 +01001635 :param integer loglevel: Is the log level associated with the message. It is a
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01001636 number between 0 and 7.
1637 :param string msg: The log content.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01001638 :see: :js:attr:`core.emerg`, :js:attr:`core.alert`, :js:attr:`core.crit`,
1639 :js:attr:`core.err`, :js:attr:`core.warning`, :js:attr:`core.notice`,
1640 :js:attr:`core.info`, :js:attr:`core.debug` (log level definitions)
1641 :see: :js:func:`TXN.deflog`
1642 :see: :js:func:`TXN.Debug`
1643 :see: :js:func:`TXN.Info`
1644 :see: :js:func:`TXN.Warning`
1645 :see: :js:func:`TXN.Alert`
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01001646
1647.. js:function:: TXN.deflog(TXN, msg)
1648
Bertrand Jacquin874a35c2018-09-10 21:26:07 +01001649 Sends a log line with the default loglevel for the proxy associated with the
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01001650 transaction.
1651
1652 :param class_txn txn: The class txn object containing the data.
1653 :param string msg: The log content.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01001654 :see: :js:func:`TXN.log
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01001655
1656.. js:function:: TXN.Debug(txn, msg)
1657
1658 :param class_txn txn: The class txn object containing the data.
1659 :param string msg: The log content.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01001660 :see: :js:func:`TXN.log`
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01001661
1662 Does the same job than:
1663
1664.. code-block:: lua
1665
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001666 function Debug(txn, msg)
1667 TXN.log(txn, core.debug, msg)
1668 end
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01001669..
1670
1671.. js:function:: TXN.Info(txn, msg)
1672
1673 :param class_txn txn: The class txn object containing the data.
1674 :param string msg: The log content.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01001675 :see: :js:func:`TXN.log`
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01001676
1677.. code-block:: lua
1678
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001679 function Debug(txn, msg)
1680 TXN.log(txn, core.info, msg)
1681 end
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01001682..
1683
1684.. js:function:: TXN.Warning(txn, msg)
1685
1686 :param class_txn txn: The class txn object containing the data.
1687 :param string msg: The log content.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01001688 :see: :js:func:`TXN.log`
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01001689
1690.. code-block:: lua
1691
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001692 function Debug(txn, msg)
1693 TXN.log(txn, core.warning, msg)
1694 end
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01001695..
1696
1697.. js:function:: TXN.Alert(txn, msg)
1698
1699 :param class_txn txn: The class txn object containing the data.
1700 :param string msg: The log content.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01001701 :see: :js:func:`TXN.log`
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01001702
1703.. code-block:: lua
1704
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001705 function Debug(txn, msg)
1706 TXN.log(txn, core.alert, msg)
1707 end
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01001708..
1709
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001710.. js:function:: TXN.get_priv(txn)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001711
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001712 Return Lua data stored in the current transaction (with the `TXN.set_priv()`)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001713 function. If no data are stored, it returns a nil value.
1714
1715 :param class_txn txn: The class txn object containing the data.
Bertrand Jacquin874a35c2018-09-10 21:26:07 +01001716 :returns: the opaque data previously stored, or nil if nothing is
1717 available.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001718
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001719.. js:function:: TXN.set_priv(txn, data)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001720
1721 Store any data in the current HAProxy transaction. This action replace the
1722 old stored data.
1723
1724 :param class_txn txn: The class txn object containing the data.
1725 :param opaque data: The data which is stored in the transaction.
1726
Tim Duesterhus4e172c92020-05-19 13:49:42 +02001727.. js:function:: TXN.set_var(TXN, var, value[, ifexist])
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +02001728
David Carlier61fdf8b2015-10-02 11:59:38 +01001729 Converts a Lua type in a HAProxy type and store it in a variable <var>.
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +02001730
1731 :param class_txn txn: The class txn object containing the data.
1732 :param string var: The variable name according with the HAProxy variable syntax.
Thierry FOURNIER / OZON.IOb210bcc2016-12-12 16:24:16 +01001733 :param type value: The value associated to the variable. The type can be string or
1734 integer.
Tim Duesterhus4e172c92020-05-19 13:49:42 +02001735 :param boolean ifexist: If this parameter is set to a truthy value the variable
1736 will only be set if it was defined elsewhere (i.e. used
1737 within the configuration). It is highly recommended to
1738 always set this to true.
Christopher Faulet85d79c92016-11-09 16:54:56 +01001739
1740.. js:function:: TXN.unset_var(TXN, var)
1741
1742 Unset the variable <var>.
1743
1744 :param class_txn txn: The class txn object containing the data.
1745 :param string var: The variable name according with the HAProxy variable syntax.
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +02001746
1747.. js:function:: TXN.get_var(TXN, var)
1748
1749 Returns data stored in the variable <var> converter in Lua type.
1750
1751 :param class_txn txn: The class txn object containing the data.
1752 :param string var: The variable name according with the HAProxy variable syntax.
1753
Christopher Faulet700d9e82020-01-31 12:21:52 +01001754.. js:function:: TXN.reply([reply])
1755
1756 Return a new reply object
1757
1758 :param table reply: A table containing info to initialize the reply fields.
1759 :returns: A :ref:`reply_class` object.
1760
1761 The table used to initialized the reply object may contain following entries :
1762
1763 * status : The reply status code. the code 200 is used by default.
1764 * reason : The reply reason. The reason corresponding to the status code is
1765 used by default.
1766 * headers : An list of headers, indexed by header name. Empty by default. For
1767 a given name, multiple values are possible, stored in an ordered list.
1768 * body : The reply body, empty by default.
1769
1770.. code-block:: lua
1771
1772 local reply = txn:reply{
1773 status = 400,
1774 reason = "Bad request",
1775 headers = {
1776 ["content-type"] = { "text/html" },
1777 ["cache-control"] = {"no-cache", "no-store" }
1778 },
1779 body = "<html><body><h1>invalid request<h1></body></html>"
1780 }
1781..
1782 :see: :js:class:`Reply`
1783
1784.. js:function:: TXN.done(txn[, reply])
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001785
Willy Tarreaubc183a62015-08-28 10:39:11 +02001786 This function terminates processing of the transaction and the associated
Christopher Faulet700d9e82020-01-31 12:21:52 +01001787 session and optionally reply to the client for HTTP sessions.
1788
1789 :param class_txn txn: The class txn object containing the data.
1790 :param class_reply reply: The class reply object to return to the client.
1791
1792 This functions can be used when a critical error is detected or to terminate
Willy Tarreaubc183a62015-08-28 10:39:11 +02001793 processing after some data have been returned to the client (eg: a redirect).
Christopher Faulet700d9e82020-01-31 12:21:52 +01001794 To do so, a reply may be provided. This object is optional and may contain a
1795 status code, a reason, a header list and a body. All these fields are
1796 optionnals. When not provided, the default values are used. By default, with
1797 an empty reply object, an empty HTTP 200 response is returned to the
1798 client. If no reply object is provided, the transaction is terminated without
1799 any reply.
1800
1801 The reply object may be fully created in lua or the class Reply may be used to
1802 create it.
1803
1804.. code-block:: lua
1805
1806 local reply = txn:reply()
1807 reply:set_status(400, "Bad request")
1808 reply:add_header("content-type", "text/html")
1809 reply:add_header("cache-control", "no-cache")
1810 reply:add_header("cache-control", "no-store")
1811 reply:set_body("<html><body><h1>invalid request<h1></body></html>")
1812 txn:done(reply)
1813..
1814
1815.. code-block:: lua
1816
1817 txn:done{
1818 status = 400,
1819 reason = "Bad request",
1820 headers = {
1821 ["content-type"] = { "text/html" },
1822 ["cache-control"] = { "no-cache", "no-store" },
1823 },
1824 body = "<html><body><h1>invalid request<h1></body></html>"
1825 }
1826..
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001827
Thierry FOURNIERab00df62016-07-14 11:42:37 +02001828 *Warning*: It not make sense to call this function from sample-fetches. In
1829 this case the behaviour of this one is the same than core.done(): it quit
1830 the Lua execution. The transaction is really aborted only from an action
1831 registered function.
1832
Christopher Faulet700d9e82020-01-31 12:21:52 +01001833 :see: :js:func:`TXN.reply`, :js:class:`Reply`
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001834
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001835.. js:function:: TXN.set_loglevel(txn, loglevel)
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01001836
1837 Is used to change the log level of the current request. The "loglevel" must
1838 be an integer between 0 and 7.
1839
1840 :param class_txn txn: The class txn object containing the data.
1841 :param integer loglevel: The required log level. This variable can be one of
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01001842 :see: :js:attr:`core.emerg`, :js:attr:`core.alert`, :js:attr:`core.crit`,
1843 :js:attr:`core.err`, :js:attr:`core.warning`, :js:attr:`core.notice`,
1844 :js:attr:`core.info`, :js:attr:`core.debug` (log level definitions)
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01001845
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001846.. js:function:: TXN.set_tos(txn, tos)
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01001847
1848 Is used to set the TOS or DSCP field value of packets sent to the client to
1849 the value passed in "tos" on platforms which support this.
1850
1851 :param class_txn txn: The class txn object containing the data.
1852 :param integer tos: The new TOS os DSCP.
1853
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001854.. js:function:: TXN.set_mark(txn, mark)
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01001855
1856 Is used to set the Netfilter MARK on all packets sent to the client to the
1857 value passed in "mark" on platforms which support it.
1858
1859 :param class_txn txn: The class txn object containing the data.
1860 :param integer mark: The mark value.
1861
Patrick Hemmer268a7072018-05-11 12:52:31 -04001862.. js:function:: TXN.set_priority_class(txn, prio)
1863
1864 This function adjusts the priority class of the transaction. The value should
1865 be within the range -2047..2047. Values outside this range will be
1866 truncated.
1867
1868 See the HAProxy configuration.txt file keyword "http-request" action
1869 "set-priority-class" for details.
1870
1871.. js:function:: TXN.set_priority_offset(txn, prio)
1872
1873 This function adjusts the priority offset of the transaction. The value
1874 should be within the range -524287..524287. Values outside this range will be
1875 truncated.
1876
1877 See the HAProxy configuration.txt file keyword "http-request" action
1878 "set-priority-offset" for details.
1879
Christopher Faulet700d9e82020-01-31 12:21:52 +01001880.. _reply_class:
1881
1882Reply class
1883============
1884
1885.. js:class:: Reply
1886
1887 **context**: action
1888
1889 This class represents an HTTP response message. It provides some methods to
1890 enrich it.
1891
1892.. code-block:: lua
1893
1894 local reply = txn:reply({status = 400}) -- default HTTP 400 reason-phase used
1895 reply:add_header("content-type", "text/html")
1896 reply:add_header("cache-control", "no-cache")
1897 reply:add_header("cache-control", "no-store")
1898 reply:set_body("<html><body><h1>invalid request<h1></body></html>")
1899..
1900
1901 :see: :js:func:`TXN.reply`
1902
1903.. js:attribute:: Reply.status
1904
1905 The reply status code. By default, the status code is set to 200.
1906
1907 :returns: integer
1908
1909.. js:attribute:: Reply.reason
1910
1911 The reason string describing the status code.
1912
1913 :returns: string
1914
1915.. js:attribute:: Reply.headers
1916
1917 A table indexing all reply headers by name. To each name is associated an
1918 ordered list of values.
1919
1920 :returns: Lua table
1921
1922.. code-block:: lua
1923
1924 {
1925 ["content-type"] = { "text/html" },
1926 ["cache-control"] = {"no-cache", "no-store" },
1927 x_header_name = { "value1", "value2", ... }
1928 ...
1929 }
1930..
1931
1932.. js:attribute:: Reply.body
1933
1934 The reply payload.
1935
1936 :returns: string
1937
1938.. js:function:: Reply.set_status(REPLY, status[, reason])
1939
1940 Set the reply status code and optionally the reason-phrase. If the reason is
1941 not provided, the default reason corresponding to the status code is used.
1942
1943 :param class_reply reply: The related Reply object.
1944 :param integer status: The reply status code.
1945 :param string reason: The reply status reason (optional).
1946
1947.. js:function:: Reply.add_header(REPLY, name, value)
1948
1949 Add a header to the reply object. If the header does not already exist, a new
1950 entry is created with its name as index and a one-element list containing its
1951 value as value. Otherwise, the header value is appended to the ordered list of
1952 values associated to the header name.
1953
1954 :param class_reply reply: The related Reply object.
1955 :param string name: The header field name.
1956 :param string value: The header field value.
1957
1958.. js:function:: Reply.del_header(REPLY, name)
1959
1960 Remove all occurrences of a header name from the reply object.
1961
1962 :param class_reply reply: The related Reply object.
1963 :param string name: The header field name.
1964
1965.. js:function:: Reply.set_body(REPLY, body)
1966
1967 Set the reply payload.
1968
1969 :param class_reply reply: The related Reply object.
1970 :param string body: The reply payload.
1971
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001972.. _socket_class:
1973
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001974Socket class
1975============
1976
1977.. js:class:: Socket
1978
1979 This class must be compatible with the Lua Socket class. Only the 'client'
1980 functions are available. See the Lua Socket documentation:
1981
1982 `http://w3.impa.br/~diego/software/luasocket/tcp.html
1983 <http://w3.impa.br/~diego/software/luasocket/tcp.html>`_
1984
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001985.. js:function:: Socket.close(socket)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001986
1987 Closes a TCP object. The internal socket used by the object is closed and the
1988 local address to which the object was bound is made available to other
1989 applications. No further operations (except for further calls to the close
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001990 method) are allowed on a closed Socket.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001991
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001992 :param class_socket socket: Is the manipulated Socket.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001993
1994 Note: It is important to close all used sockets once they are not needed,
1995 since, in many systems, each socket uses a file descriptor, which are limited
1996 system resources. Garbage-collected objects are automatically closed before
1997 destruction, though.
1998
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02001999.. js:function:: Socket.connect(socket, address[, port])
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002000
2001 Attempts to connect a socket object to a remote host.
2002
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002003
2004 In case of error, the method returns nil followed by a string describing the
2005 error. In case of success, the method returns 1.
2006
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002007 :param class_socket socket: Is the manipulated Socket.
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002008 :param string address: can be an IP address or a host name. See below for more
2009 information.
2010 :param integer port: must be an integer number in the range [1..64K].
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002011 :returns: 1 or nil.
2012
Bertrand Jacquin874a35c2018-09-10 21:26:07 +01002013 An address field extension permits to use the connect() function to connect to
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002014 other stream than TCP. The syntax containing a simpleipv4 or ipv6 address is
2015 the basically expected format. This format requires the port.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002016
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002017 Other format accepted are a socket path like "/socket/path", it permits to
Bertrand Jacquin874a35c2018-09-10 21:26:07 +01002018 connect to a socket. Abstract namespaces are supported with the prefix
Joseph Herlant02cedc42018-11-13 19:45:17 -08002019 "abns@", and finally a file descriptor can be passed with the prefix "fd@".
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002020 The prefix "ipv4@", "ipv6@" and "unix@" are also supported. The port can be
Bertrand Jacquin874a35c2018-09-10 21:26:07 +01002021 passed int the string. The syntax "127.0.0.1:1234" is valid. In this case, the
Tim Duesterhus6edab862018-01-06 19:04:45 +01002022 parameter *port* must not be set.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002023
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002024.. js:function:: Socket.connect_ssl(socket, address, port)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002025
2026 Same behavior than the function socket:connect, but uses SSL.
2027
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002028 :param class_socket socket: Is the manipulated Socket.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002029 :returns: 1 or nil.
2030
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002031.. js:function:: Socket.getpeername(socket)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002032
2033 Returns information about the remote side of a connected client object.
2034
2035 Returns a string with the IP address of the peer, followed by the port number
2036 that peer is using for the connection. In case of error, the method returns
2037 nil.
2038
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002039 :param class_socket socket: Is the manipulated Socket.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002040 :returns: a string containing the server information.
2041
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002042.. js:function:: Socket.getsockname(socket)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002043
2044 Returns the local address information associated to the object.
2045
2046 The method returns a string with local IP address and a number with the port.
2047 In case of error, the method returns nil.
2048
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002049 :param class_socket socket: Is the manipulated Socket.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002050 :returns: a string containing the client information.
2051
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002052.. js:function:: Socket.receive(socket, [pattern [, prefix]])
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002053
2054 Reads data from a client object, according to the specified read pattern.
2055 Patterns follow the Lua file I/O format, and the difference in performance
2056 between all patterns is negligible.
2057
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002058 :param class_socket socket: Is the manipulated Socket.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002059 :param string|integer pattern: Describe what is required (see below).
2060 :param string prefix: A string which will be prefix the returned data.
2061 :returns: a string containing the required data or nil.
2062
2063 Pattern can be any of the following:
2064
2065 * **`*a`**: reads from the socket until the connection is closed. No
2066 end-of-line translation is performed;
2067
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002068 * **`*l`**: reads a line of text from the Socket. The line is terminated by a
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002069 LF character (ASCII 10), optionally preceded by a CR character
2070 (ASCII 13). The CR and LF characters are not included in the
2071 returned line. In fact, all CR characters are ignored by the
2072 pattern. This is the default pattern.
2073
2074 * **number**: causes the method to read a specified number of bytes from the
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002075 Socket. Prefix is an optional string to be concatenated to the
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002076 beginning of any received data before return.
2077
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002078 * **empty**: If the pattern is left empty, the default option is `*l`.
2079
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002080 If successful, the method returns the received pattern. In case of error, the
2081 method returns nil followed by an error message which can be the string
2082 'closed' in case the connection was closed before the transmission was
2083 completed or the string 'timeout' in case there was a timeout during the
2084 operation. Also, after the error message, the function returns the partial
2085 result of the transmission.
2086
2087 Important note: This function was changed severely. It used to support
2088 multiple patterns (but I have never seen this feature used) and now it
2089 doesn't anymore. Partial results used to be returned in the same way as
2090 successful results. This last feature violated the idea that all functions
2091 should return nil on error. Thus it was changed too.
2092
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002093.. js:function:: Socket.send(socket, data [, start [, end ]])
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002094
2095 Sends data through client object.
2096
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002097 :param class_socket socket: Is the manipulated Socket.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002098 :param string data: The data that will be sent.
2099 :param integer start: The start position in the buffer of the data which will
2100 be sent.
2101 :param integer end: The end position in the buffer of the data which will
2102 be sent.
2103 :returns: see below.
2104
2105 Data is the string to be sent. The optional arguments i and j work exactly
2106 like the standard string.sub Lua function to allow the selection of a
2107 substring to be sent.
2108
2109 If successful, the method returns the index of the last byte within [start,
2110 end] that has been sent. Notice that, if start is 1 or absent, this is
2111 effectively the total number of bytes sent. In case of error, the method
2112 returns nil, followed by an error message, followed by the index of the last
2113 byte within [start, end] that has been sent. You might want to try again from
2114 the byte following that. The error message can be 'closed' in case the
2115 connection was closed before the transmission was completed or the string
2116 'timeout' in case there was a timeout during the operation.
2117
2118 Note: Output is not buffered. For small strings, it is always better to
2119 concatenate them in Lua (with the '..' operator) and send the result in one
2120 call instead of calling the method several times.
2121
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002122.. js:function:: Socket.setoption(socket, option [, value])
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002123
2124 Just implemented for compatibility, this cal does nothing.
2125
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002126.. js:function:: Socket.settimeout(socket, value [, mode])
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002127
2128 Changes the timeout values for the object. All I/O operations are blocking.
2129 That is, any call to the methods send, receive, and accept will block
2130 indefinitely, until the operation completes. The settimeout method defines a
2131 limit on the amount of time the I/O methods can block. When a timeout time
2132 has elapsed, the affected methods give up and fail with an error code.
2133
2134 The amount of time to wait is specified as the value parameter, in seconds.
2135
Mark Lakes56cc1252018-03-27 09:48:06 +02002136 The timeout modes are not implemented, the only settable timeout is the
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002137 inactivity time waiting for complete the internal buffer send or waiting for
2138 receive data.
2139
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002140 :param class_socket socket: Is the manipulated Socket.
Bertrand Jacquin874a35c2018-09-10 21:26:07 +01002141 :param float value: The timeout value. Use floating point to specify
Mark Lakes56cc1252018-03-27 09:48:06 +02002142 milliseconds.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002143
Thierry FOURNIER31904272017-10-25 12:59:51 +02002144.. _regex_class:
2145
2146Regex class
2147===========
2148
2149.. js:class:: Regex
2150
2151 This class allows the usage of HAProxy regexes because classic lua doesn't
2152 provides regexes. This class inherits the HAProxy compilation options, so the
2153 regexes can be libc regex, pcre regex or pcre JIT regex.
2154
2155 The expression matching number is limited to 20 per regex. The only available
2156 option is case sensitive.
2157
2158 Because regexes compilation is a heavy process, it is better to define all
2159 your regexes in the **body context** and use it during the runtime.
2160
2161.. code-block:: lua
2162
2163 -- Create the regex
2164 st, regex = Regex.new("needle (..) (...)", true);
2165
2166 -- Check compilation errors
2167 if st == false then
2168 print "error: " .. regex
2169 end
2170
2171 -- Match the regexes
2172 print(regex:exec("Looking for a needle in the haystack")) -- true
2173 print(regex:exec("Lokking for a cat in the haystack")) -- false
2174
2175 -- Extract words
2176 st, list = regex:match("Looking for a needle in the haystack")
2177 print(st) -- true
2178 print(list[1]) -- needle in the
2179 print(list[2]) -- in
2180 print(list[3]) -- the
2181
2182.. js:function:: Regex.new(regex, case_sensitive)
2183
2184 Create and compile a regex.
2185
2186 :param string regex: The regular expression according with the libc or pcre
2187 standard
2188 :param boolean case_sensitive: Match is case sensitive or not.
2189 :returns: boolean status and :ref:`regex_class` or string containing fail reason.
2190
2191.. js:function:: Regex.exec(regex, str)
2192
2193 Execute the regex.
2194
2195 :param class_regex regex: A :ref:`regex_class` object.
2196 :param string str: The input string will be compared with the compiled regex.
2197 :returns: a boolean status according with the match result.
2198
2199.. js:function:: Regex.match(regex, str)
2200
2201 Execute the regex and return matched expressions.
2202
2203 :param class_map map: A :ref:`regex_class` object.
2204 :param string str: The input string will be compared with the compiled regex.
2205 :returns: a boolean status according with the match result, and
2206 a table containing all the string matched in order of declaration.
2207
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002208.. _map_class:
2209
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002210Map class
2211=========
2212
2213.. js:class:: Map
2214
2215 This class permits to do some lookup in HAProxy maps. The declared maps can
Bertrand Jacquin874a35c2018-09-10 21:26:07 +01002216 be modified during the runtime through the HAProxy management socket.
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002217
2218.. code-block:: lua
2219
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002220 default = "usa"
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002221
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002222 -- Create and load map
Thierry FOURNIER4dc71972017-01-28 08:33:08 +01002223 geo = Map.new("geo.map", Map._ip);
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002224
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002225 -- Create new fetch that returns the user country
2226 core.register_fetches("country", function(txn)
2227 local src;
2228 local loc;
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002229
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002230 src = txn.f:fhdr("x-forwarded-for");
2231 if (src == nil) then
2232 src = txn.f:src()
2233 if (src == nil) then
2234 return default;
2235 end
2236 end
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002237
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002238 -- Perform lookup
2239 loc = geo:lookup(src);
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002240
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002241 if (loc == nil) then
2242 return default;
2243 end
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002244
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002245 return loc;
2246 end);
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002247
Thierry FOURNIER4dc71972017-01-28 08:33:08 +01002248.. js:attribute:: Map._int
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002249
2250 See the HAProxy configuration.txt file, chapter "Using ACLs and fetching
Ilya Shipitsin2075ca82020-03-06 23:22:22 +05002251 samples" and subchapter "ACL basics" to understand this pattern matching
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002252 method.
2253
Thierry FOURNIER4dc71972017-01-28 08:33:08 +01002254 Note that :js:attr:`Map.int` is also available for compatibility.
2255
2256.. js:attribute:: Map._ip
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002257
2258 See the HAProxy configuration.txt file, chapter "Using ACLs and fetching
Ilya Shipitsin2075ca82020-03-06 23:22:22 +05002259 samples" and subchapter "ACL basics" to understand this pattern matching
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002260 method.
2261
Thierry FOURNIER4dc71972017-01-28 08:33:08 +01002262 Note that :js:attr:`Map.ip` is also available for compatibility.
2263
2264.. js:attribute:: Map._str
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002265
2266 See the HAProxy configuration.txt file, chapter "Using ACLs and fetching
Ilya Shipitsin2075ca82020-03-06 23:22:22 +05002267 samples" and subchapter "ACL basics" to understand this pattern matching
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002268 method.
2269
Thierry FOURNIER4dc71972017-01-28 08:33:08 +01002270 Note that :js:attr:`Map.str` is also available for compatibility.
2271
2272.. js:attribute:: Map._beg
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002273
2274 See the HAProxy configuration.txt file, chapter "Using ACLs and fetching
Ilya Shipitsin2075ca82020-03-06 23:22:22 +05002275 samples" and subchapter "ACL basics" to understand this pattern matching
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002276 method.
2277
Thierry FOURNIER4dc71972017-01-28 08:33:08 +01002278 Note that :js:attr:`Map.beg` is also available for compatibility.
2279
2280.. js:attribute:: Map._sub
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002281
2282 See the HAProxy configuration.txt file, chapter "Using ACLs and fetching
Ilya Shipitsin2075ca82020-03-06 23:22:22 +05002283 samples" and subchapter "ACL basics" to understand this pattern matching
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002284 method.
2285
Thierry FOURNIER4dc71972017-01-28 08:33:08 +01002286 Note that :js:attr:`Map.sub` is also available for compatibility.
2287
2288.. js:attribute:: Map._dir
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002289
2290 See the HAProxy configuration.txt file, chapter "Using ACLs and fetching
Ilya Shipitsin2075ca82020-03-06 23:22:22 +05002291 samples" and subchapter "ACL basics" to understand this pattern matching
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002292 method.
2293
Thierry FOURNIER4dc71972017-01-28 08:33:08 +01002294 Note that :js:attr:`Map.dir` is also available for compatibility.
2295
2296.. js:attribute:: Map._dom
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002297
2298 See the HAProxy configuration.txt file, chapter "Using ACLs and fetching
Ilya Shipitsin2075ca82020-03-06 23:22:22 +05002299 samples" and subchapter "ACL basics" to understand this pattern matching
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002300 method.
2301
Thierry FOURNIER4dc71972017-01-28 08:33:08 +01002302 Note that :js:attr:`Map.dom` is also available for compatibility.
2303
2304.. js:attribute:: Map._end
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002305
2306 See the HAProxy configuration.txt file, chapter "Using ACLs and fetching
Ilya Shipitsin2075ca82020-03-06 23:22:22 +05002307 samples" and subchapter "ACL basics" to understand this pattern matching
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002308 method.
2309
Thierry FOURNIER4dc71972017-01-28 08:33:08 +01002310.. js:attribute:: Map._reg
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002311
2312 See the HAProxy configuration.txt file, chapter "Using ACLs and fetching
Ilya Shipitsin2075ca82020-03-06 23:22:22 +05002313 samples" and subchapter "ACL basics" to understand this pattern matching
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002314 method.
2315
Thierry FOURNIER4dc71972017-01-28 08:33:08 +01002316 Note that :js:attr:`Map.reg` is also available for compatibility.
2317
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002318
2319.. js:function:: Map.new(file, method)
2320
2321 Creates and load a map.
2322
2323 :param string file: Is the file containing the map.
2324 :param integer method: Is the map pattern matching method. See the attributes
2325 of the Map class.
2326 :returns: a class Map object.
Thierry FOURNIER4dc71972017-01-28 08:33:08 +01002327 :see: The Map attributes: :js:attr:`Map._int`, :js:attr:`Map._ip`,
2328 :js:attr:`Map._str`, :js:attr:`Map._beg`, :js:attr:`Map._sub`,
2329 :js:attr:`Map._dir`, :js:attr:`Map._dom`, :js:attr:`Map._end` and
2330 :js:attr:`Map._reg`.
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002331
2332.. js:function:: Map.lookup(map, str)
2333
2334 Perform a lookup in a map.
2335
2336 :param class_map map: Is the class Map object.
2337 :param string str: Is the string used as key.
2338 :returns: a string containing the result or nil if no match.
2339
2340.. js:function:: Map.slookup(map, str)
2341
2342 Perform a lookup in a map.
2343
2344 :param class_map map: Is the class Map object.
2345 :param string str: Is the string used as key.
2346 :returns: a string containing the result or empty string if no match.
2347
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002348.. _applethttp_class:
2349
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002350AppletHTTP class
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002351================
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002352
2353.. js:class:: AppletHTTP
2354
2355 This class is used with applets that requires the 'http' mode. The http applet
2356 can be registered with the *core.register_service()* function. They are used
2357 for processing an http request like a server in back of HAProxy.
2358
2359 This is an hello world sample code:
2360
2361.. code-block:: lua
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002362
Pieter Baauw4d7f7662015-11-08 16:38:08 +01002363 core.register_service("hello-world", "http", function(applet)
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002364 local response = "Hello World !"
2365 applet:set_status(200)
Pieter Baauw2dcb9bc2015-10-01 22:47:12 +02002366 applet:add_header("content-length", string.len(response))
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002367 applet:add_header("content-type", "text/plain")
Pieter Baauw2dcb9bc2015-10-01 22:47:12 +02002368 applet:start_response()
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002369 applet:send(response)
2370 end)
2371
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002372.. js:attribute:: AppletHTTP.c
2373
2374 :returns: A :ref:`converters_class`
2375
2376 This attribute contains a Converters class object.
2377
2378.. js:attribute:: AppletHTTP.sc
2379
2380 :returns: A :ref:`converters_class`
2381
2382 This attribute contains a Converters class object. The
2383 functions of this object returns always a string.
2384
2385.. js:attribute:: AppletHTTP.f
2386
2387 :returns: A :ref:`fetches_class`
2388
2389 This attribute contains a Fetches class object. Note that the
2390 applet execution place cannot access to a valid HAProxy core HTTP
Ilya Shipitsin2075ca82020-03-06 23:22:22 +05002391 transaction, so some sample fetches related to the HTTP dependent
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002392 values (hdr, path, ...) are not available.
2393
2394.. js:attribute:: AppletHTTP.sf
2395
2396 :returns: A :ref:`fetches_class`
2397
2398 This attribute contains a Fetches class object. The functions of
2399 this object returns always a string. Note that the applet
2400 execution place cannot access to a valid HAProxy core HTTP
Ilya Shipitsin2075ca82020-03-06 23:22:22 +05002401 transaction, so some sample fetches related to the HTTP dependent
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002402 values (hdr, path, ...) are not available.
2403
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01002404.. js:attribute:: AppletHTTP.method
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002405
2406 :returns: string
2407
2408 The attribute method returns a string containing the HTTP
2409 method.
2410
2411.. js:attribute:: AppletHTTP.version
2412
2413 :returns: string
2414
2415 The attribute version, returns a string containing the HTTP
2416 request version.
2417
2418.. js:attribute:: AppletHTTP.path
2419
2420 :returns: string
2421
2422 The attribute path returns a string containing the HTTP
2423 request path.
2424
2425.. js:attribute:: AppletHTTP.qs
2426
2427 :returns: string
2428
2429 The attribute qs returns a string containing the HTTP
2430 request query string.
2431
2432.. js:attribute:: AppletHTTP.length
2433
2434 :returns: integer
2435
2436 The attribute length returns an integer containing the HTTP
2437 body length.
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002438
Thierry FOURNIER841475e2015-12-11 17:10:09 +01002439.. js:attribute:: AppletHTTP.headers
2440
Patrick Hemmerc6a1d712018-05-01 21:30:41 -04002441 :returns: table
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002442
Patrick Hemmerc6a1d712018-05-01 21:30:41 -04002443 The attribute headers returns a table containing the HTTP
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002444 headers. The header names are always in lower case. As the header name can be
2445 encountered more than once in each request, the value is indexed with 0 as
Patrick Hemmerc6a1d712018-05-01 21:30:41 -04002446 first index value. The table have this form:
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002447
2448.. code-block:: lua
2449
2450 AppletHTTP.headers['<header-name>'][<header-index>] = "<header-value>"
2451
2452 AppletHTTP.headers["host"][0] = "www.test.com"
2453 AppletHTTP.headers["accept"][0] = "audio/basic q=1"
2454 AppletHTTP.headers["accept"][1] = "audio/*, q=0.2"
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01002455 AppletHTTP.headers["accept"][2] = "*/*, q=0.1"
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002456..
2457
Robin H. Johnson52f5db22017-01-01 13:10:52 -08002458.. js:function:: AppletHTTP.set_status(applet, code [, reason])
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002459
2460 This function sets the HTTP status code for the response. The allowed code are
2461 from 100 to 599.
2462
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01002463 :param class_AppletHTTP applet: An :ref:`applethttp_class`
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002464 :param integer code: the status code returned to the client.
Robin H. Johnson52f5db22017-01-01 13:10:52 -08002465 :param string reason: the status reason returned to the client (optional).
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002466
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01002467.. js:function:: AppletHTTP.add_header(applet, name, value)
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002468
2469 This function add an header in the response. Duplicated headers are not
2470 collapsed. The special header *content-length* is used to determinate the
2471 response length. If it not exists, a *transfer-encoding: chunked* is set, and
Ilya Shipitsin2075ca82020-03-06 23:22:22 +05002472 all the write from the function *AppletHTTP:send()* become a chunk.
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002473
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01002474 :param class_AppletHTTP applet: An :ref:`applethttp_class`
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002475 :param string name: the header name
2476 :param string value: the header value
2477
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01002478.. js:function:: AppletHTTP.start_response(applet)
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002479
2480 This function indicates to the HTTP engine that it can process and send the
2481 response headers. After this called we cannot add headers to the response; We
2482 cannot use the *AppletHTTP:send()* function if the
2483 *AppletHTTP:start_response()* is not called.
2484
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01002485 :param class_AppletHTTP applet: An :ref:`applethttp_class`
2486
2487.. js:function:: AppletHTTP.getline(applet)
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002488
2489 This function returns a string containing one line from the http body. If the
2490 data returned doesn't contains a final '\\n' its assumed than its the last
2491 available data before the end of stream.
2492
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01002493 :param class_AppletHTTP applet: An :ref:`applethttp_class`
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002494 :returns: a string. The string can be empty if we reach the end of the stream.
2495
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01002496.. js:function:: AppletHTTP.receive(applet, [size])
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002497
2498 Reads data from the HTTP body, according to the specified read *size*. If the
2499 *size* is missing, the function tries to read all the content of the stream
2500 until the end. If the *size* is bigger than the http body, it returns the
Bertrand Jacquin874a35c2018-09-10 21:26:07 +01002501 amount of data available.
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002502
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01002503 :param class_AppletHTTP applet: An :ref:`applethttp_class`
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002504 :param integer size: the required read size.
Ilya Shipitsin11057a32020-06-21 21:18:27 +05002505 :returns: always return a string,the string can be empty is the connection is
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002506 closed.
2507
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01002508.. js:function:: AppletHTTP.send(applet, msg)
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002509
2510 Send the message *msg* on the http request body.
2511
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01002512 :param class_AppletHTTP applet: An :ref:`applethttp_class`
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002513 :param string msg: the message to send.
2514
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01002515.. js:function:: AppletHTTP.get_priv(applet)
2516
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01002517 Return Lua data stored in the current transaction. If no data are stored,
2518 it returns a nil value.
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01002519
2520 :param class_AppletHTTP applet: An :ref:`applethttp_class`
Bertrand Jacquin874a35c2018-09-10 21:26:07 +01002521 :returns: the opaque data previously stored, or nil if nothing is
2522 available.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01002523 :see: :js:func:`AppletHTTP.set_priv`
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01002524
2525.. js:function:: AppletHTTP.set_priv(applet, data)
2526
2527 Store any data in the current HAProxy transaction. This action replace the
2528 old stored data.
2529
2530 :param class_AppletHTTP applet: An :ref:`applethttp_class`
2531 :param opaque data: The data which is stored in the transaction.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01002532 :see: :js:func:`AppletHTTP.get_priv`
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01002533
Tim Duesterhus4e172c92020-05-19 13:49:42 +02002534.. js:function:: AppletHTTP.set_var(applet, var, value[, ifexist])
Thierry FOURNIER / OZON.IOc1edafe2016-12-12 16:25:30 +01002535
2536 Converts a Lua type in a HAProxy type and store it in a variable <var>.
2537
2538 :param class_AppletHTTP applet: An :ref:`applethttp_class`
2539 :param string var: The variable name according with the HAProxy variable syntax.
2540 :param type value: The value associated to the variable. The type ca be string or
2541 integer.
Tim Duesterhus4e172c92020-05-19 13:49:42 +02002542 :param boolean ifexist: If this parameter is set to a truthy value the variable
2543 will only be set if it was defined elsewhere (i.e. used
2544 within the configuration). It is highly recommended to
2545 always set this to true.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01002546 :see: :js:func:`AppletHTTP.unset_var`
2547 :see: :js:func:`AppletHTTP.get_var`
Thierry FOURNIER / OZON.IOc1edafe2016-12-12 16:25:30 +01002548
2549.. js:function:: AppletHTTP.unset_var(applet, var)
2550
2551 Unset the variable <var>.
2552
2553 :param class_AppletHTTP applet: An :ref:`applethttp_class`
2554 :param string var: The variable name according with the HAProxy variable syntax.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01002555 :see: :js:func:`AppletHTTP.set_var`
2556 :see: :js:func:`AppletHTTP.get_var`
Thierry FOURNIER / OZON.IOc1edafe2016-12-12 16:25:30 +01002557
2558.. js:function:: AppletHTTP.get_var(applet, var)
2559
2560 Returns data stored in the variable <var> converter in Lua type.
2561
2562 :param class_AppletHTTP applet: An :ref:`applethttp_class`
2563 :param string var: The variable name according with the HAProxy variable syntax.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01002564 :see: :js:func:`AppletHTTP.set_var`
2565 :see: :js:func:`AppletHTTP.unset_var`
Thierry FOURNIER / OZON.IOc1edafe2016-12-12 16:25:30 +01002566
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002567.. _applettcp_class:
2568
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002569AppletTCP class
2570===============
2571
2572.. js:class:: AppletTCP
2573
2574 This class is used with applets that requires the 'tcp' mode. The tcp applet
2575 can be registered with the *core.register_service()* function. They are used
2576 for processing a tcp stream like a server in back of HAProxy.
2577
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002578.. js:attribute:: AppletTCP.c
2579
2580 :returns: A :ref:`converters_class`
2581
2582 This attribute contains a Converters class object.
2583
2584.. js:attribute:: AppletTCP.sc
2585
2586 :returns: A :ref:`converters_class`
2587
2588 This attribute contains a Converters class object. The
2589 functions of this object returns always a string.
2590
2591.. js:attribute:: AppletTCP.f
2592
2593 :returns: A :ref:`fetches_class`
2594
2595 This attribute contains a Fetches class object.
2596
2597.. js:attribute:: AppletTCP.sf
2598
2599 :returns: A :ref:`fetches_class`
2600
2601 This attribute contains a Fetches class object.
2602
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01002603.. js:function:: AppletTCP.getline(applet)
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002604
2605 This function returns a string containing one line from the stream. If the
2606 data returned doesn't contains a final '\\n' its assumed than its the last
2607 available data before the end of stream.
2608
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01002609 :param class_AppletTCP applet: An :ref:`applettcp_class`
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002610 :returns: a string. The string can be empty if we reach the end of the stream.
2611
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01002612.. js:function:: AppletTCP.receive(applet, [size])
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002613
2614 Reads data from the TCP stream, according to the specified read *size*. If the
2615 *size* is missing, the function tries to read all the content of the stream
2616 until the end.
2617
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01002618 :param class_AppletTCP applet: An :ref:`applettcp_class`
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002619 :param integer size: the required read size.
Ilya Shipitsin11057a32020-06-21 21:18:27 +05002620 :returns: always return a string,the string can be empty is the connection is
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002621 closed.
2622
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01002623.. js:function:: AppletTCP.send(appletmsg)
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002624
2625 Send the message on the stream.
2626
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01002627 :param class_AppletTCP applet: An :ref:`applettcp_class`
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002628 :param string msg: the message to send.
2629
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01002630.. js:function:: AppletTCP.get_priv(applet)
2631
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01002632 Return Lua data stored in the current transaction. If no data are stored,
2633 it returns a nil value.
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01002634
2635 :param class_AppletTCP applet: An :ref:`applettcp_class`
Bertrand Jacquin874a35c2018-09-10 21:26:07 +01002636 :returns: the opaque data previously stored, or nil if nothing is
2637 available.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01002638 :see: :js:func:`AppletTCP.set_priv`
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01002639
2640.. js:function:: AppletTCP.set_priv(applet, data)
2641
2642 Store any data in the current HAProxy transaction. This action replace the
2643 old stored data.
2644
2645 :param class_AppletTCP applet: An :ref:`applettcp_class`
2646 :param opaque data: The data which is stored in the transaction.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01002647 :see: :js:func:`AppletTCP.get_priv`
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01002648
Tim Duesterhus4e172c92020-05-19 13:49:42 +02002649.. js:function:: AppletTCP.set_var(applet, var, value[, ifexist])
Thierry FOURNIER / OZON.IOc1edafe2016-12-12 16:25:30 +01002650
2651 Converts a Lua type in a HAProxy type and stores it in a variable <var>.
2652
2653 :param class_AppletTCP applet: An :ref:`applettcp_class`
2654 :param string var: The variable name according with the HAProxy variable syntax.
2655 :param type value: The value associated to the variable. The type can be string or
2656 integer.
Tim Duesterhus4e172c92020-05-19 13:49:42 +02002657 :param boolean ifexist: If this parameter is set to a truthy value the variable
2658 will only be set if it was defined elsewhere (i.e. used
2659 within the configuration). It is highly recommended to
2660 always set this to true.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01002661 :see: :js:func:`AppletTCP.unset_var`
2662 :see: :js:func:`AppletTCP.get_var`
Thierry FOURNIER / OZON.IOc1edafe2016-12-12 16:25:30 +01002663
2664.. js:function:: AppletTCP.unset_var(applet, var)
2665
2666 Unsets the variable <var>.
2667
2668 :param class_AppletTCP applet: An :ref:`applettcp_class`
2669 :param string var: The variable name according with the HAProxy variable syntax.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01002670 :see: :js:func:`AppletTCP.unset_var`
2671 :see: :js:func:`AppletTCP.set_var`
Thierry FOURNIER / OZON.IOc1edafe2016-12-12 16:25:30 +01002672
2673.. js:function:: AppletTCP.get_var(applet, var)
2674
2675 Returns data stored in the variable <var> converter in Lua type.
2676
2677 :param class_AppletTCP applet: An :ref:`applettcp_class`
2678 :param string var: The variable name according with the HAProxy variable syntax.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01002679 :see: :js:func:`AppletTCP.unset_var`
2680 :see: :js:func:`AppletTCP.set_var`
Thierry FOURNIER / OZON.IOc1edafe2016-12-12 16:25:30 +01002681
Adis Nezirovic8878f8e2018-07-13 12:18:33 +02002682StickTable class
2683================
2684
2685.. js:class:: StickTable
2686
2687 **context**: task, action, sample-fetch
2688
2689 This class can be used to access the HAProxy stick tables from Lua.
2690
2691.. js:function:: StickTable.info()
2692
2693 Returns stick table attributes as a Lua table. See HAProxy documentation for
Ilya Shipitsin2272d8a2020-12-21 01:22:40 +05002694 "stick-table" for canonical info, or check out example below.
Adis Nezirovic8878f8e2018-07-13 12:18:33 +02002695
2696 :returns: Lua table
2697
2698 Assume our table has IPv4 key and gpc0 and conn_rate "columns":
2699
2700.. code-block:: lua
2701
2702 {
2703 expire=<int>, # Value in ms
2704 size=<int>, # Maximum table size
2705 used=<int>, # Actual number of entries in table
2706 data={ # Data columns, with types as key, and periods as values
2707 (-1 if type is not rate counter)
2708 conn_rate=<int>,
2709 gpc0=-1
2710 },
2711 length=<int>, # max string length for string table keys, key length
2712 # otherwise
2713 nopurge=<boolean>, # purge oldest entries when table is full
2714 type="ip" # can be "ip", "ipv6", "integer", "string", "binary"
2715 }
2716
2717.. js:function:: StickTable.lookup(key)
2718
2719 Returns stick table entry for given <key>
2720
2721 :param string key: Stick table key (IP addresses and strings are supported)
2722 :returns: Lua table
2723
2724.. js:function:: StickTable.dump([filter])
2725
2726 Returns all entries in stick table. An optional filter can be used
2727 to extract entries with specific data values. Filter is a table with valid
2728 comparison operators as keys followed by data type name and value pairs.
2729 Check out the HAProxy docs for "show table" for more details. For the
2730 reference, the supported operators are:
2731 "eq", "ne", "le", "lt", "ge", "gt"
2732
2733 For large tables, execution of this function can take a long time (for
2734 HAProxy standards). That's also true when filter is used, so take care and
2735 measure the impact.
2736
2737 :param table filter: Stick table filter
2738 :returns: Stick table entries (table)
2739
2740 See below for example filter, which contains 4 entries (or comparisons).
2741 (Maximum number of filter entries is 4, defined in the source code)
2742
2743.. code-block:: lua
2744
2745 local filter = {
2746 {"gpc0", "gt", 30}, {"gpc1", "gt", 20}}, {"conn_rate", "le", 10}
2747 }
2748
Christopher Faulet0f3c8902020-01-31 18:57:12 +01002749.. _action_class:
2750
2751Action class
2752=============
2753
2754.. js:class:: Act
2755
2756 **context**: action
2757
2758 This class contains all return codes an action may return. It is the lua
2759 equivalent to HAProxy "ACT_RET_*" code.
2760
2761.. code-block:: lua
2762
2763 core.register_action("deny", { "http-req" }, function (txn)
2764 return act.DENY
2765 end)
2766..
2767.. js:attribute:: act.CONTINUE
2768
2769 This attribute is an integer (0). It instructs HAProxy to continue the current
2770 ruleset processing on the message. It is the default return code for a lua
2771 action.
2772
2773 :returns: integer
2774
2775.. js:attribute:: act.STOP
2776
2777 This attribute is an integer (1). It instructs HAProxy to stop the current
2778 ruleset processing on the message.
2779
2780.. js:attribute:: act.YIELD
2781
2782 This attribute is an integer (2). It instructs HAProxy to temporarily pause
2783 the message processing. It will be resumed later on the same rule. The
2784 corresponding lua script is re-executed for the start.
2785
2786.. js:attribute:: act.ERROR
2787
2788 This attribute is an integer (3). It triggers an internal errors The message
2789 processing is stopped and the transaction is terminated. For HTTP streams, an
2790 HTTP 500 error is returned to the client.
2791
2792 :returns: integer
2793
2794.. js:attribute:: act.DONE
2795
2796 This attribute is an integer (4). It instructs HAProxy to stop the message
2797 processing.
2798
2799 :returns: integer
2800
2801.. js:attribute:: act.DENY
2802
2803 This attribute is an integer (5). It denies the current message. The message
2804 processing is stopped and the transaction is terminated. For HTTP streams, an
2805 HTTP 403 error is returned to the client if the deny is returned during the
2806 request analysis. During the response analysis, an HTTP 502 error is returned
2807 and the server response is discarded.
2808
2809 :returns: integer
2810
2811.. js:attribute:: act.ABORT
2812
2813 This attribute is an integer (6). It aborts the current message. The message
2814 processing is stopped and the transaction is terminated. For HTTP streams,
Willy Tarreau714f3452021-05-09 06:47:26 +02002815 HAProxy assumes a response was already sent to the client. From the Lua
Christopher Faulet0f3c8902020-01-31 18:57:12 +01002816 actions point of view, when this code is used, the transaction is terminated
2817 with no reply.
2818
2819 :returns: integer
2820
2821.. js:attribute:: act.INVALID
2822
2823 This attribute is an integer (7). It triggers an internal errors. The message
2824 processing is stopped and the transaction is terminated. For HTTP streams, an
2825 HTTP 400 error is returned to the client if the error is returned during the
2826 request analysis. During the response analysis, an HTTP 502 error is returned
2827 and the server response is discarded.
2828
2829 :returns: integer
Adis Nezirovic8878f8e2018-07-13 12:18:33 +02002830
Christopher Faulet2c2c2e32020-01-31 19:07:52 +01002831.. js:function:: act:wake_time(milliseconds)
2832
2833 **context**: action
2834
2835 Set the script pause timeout to the specified time, defined in
2836 milliseconds.
2837
2838 :param integer milliseconds: the required milliseconds.
2839
2840 This function may be used when a lua action returns `act.YIELD`, to force its
2841 wake-up at most after the specified number of milliseconds.
2842
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002843External Lua libraries
2844======================
2845
2846A lot of useful lua libraries can be found here:
2847
2848* `https://lua-toolbox.com/ <https://lua-toolbox.com/>`_
2849
Ilya Shipitsin2075ca82020-03-06 23:22:22 +05002850Redis client library:
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002851
2852* `https://github.com/nrk/redis-lua <https://github.com/nrk/redis-lua>`_
2853
2854This is an example about the usage of the Redis library with HAProxy. Note that
2855each call of any function of this library can throw an error if the socket
2856connection fails.
2857
2858.. code-block:: lua
2859
2860 -- load the redis library
2861 local redis = require("redis");
2862
2863 function do_something(txn)
2864
2865 -- create and connect new tcp socket
2866 local tcp = core.tcp();
2867 tcp:settimeout(1);
2868 tcp:connect("127.0.0.1", 6379);
2869
2870 -- use the redis library with this new socket
2871 local client = redis.connect({socket=tcp});
2872 client:ping();
2873
2874 end
2875
2876OpenSSL:
2877
2878* `http://mkottman.github.io/luacrypto/index.html
2879 <http://mkottman.github.io/luacrypto/index.html>`_
2880
2881* `https://github.com/brunoos/luasec/wiki
2882 <https://github.com/brunoos/luasec/wiki>`_