blob: 2f2744ffaace6e145dbc3c282511d0c7a103041d [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
Christopher Faulet5a2c6612021-08-15 20:35:25 +020023functions. Lua have 7 execution context.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +010024
251. The Lua file **body context**. It is executed during the load of the Lua file
26 in the HAProxy `[global]` section with the directive `lua-load`. It is
27 executed in initialisation mode. This section is use for configuring Lua
28 bindings in HAProxy.
29
David Carlier61fdf8b2015-10-02 11:59:38 +0100302. The Lua **init context**. It is a Lua function executed just after the
Thierry FOURNIER17bd1522015-03-11 20:31:00 +010031 HAProxy configuration parsing. The execution is in initialisation mode. In
32 this context the HAProxy environment are already initialized. It is useful to
33 check configuration, or initializing socket connections or tasks. These
34 functions are declared in the body context with the Lua function
35 `core.register_init()`. The prototype of the function is a simple function
36 without return value and without parameters, like this: `function fcn()`.
37
David Carlier61fdf8b2015-10-02 11:59:38 +0100383. The Lua **task context**. It is a Lua function executed after the start
Thierry FOURNIER17bd1522015-03-11 20:31:00 +010039 of the HAProxy scheduler, and just after the declaration of the task with the
40 Lua function `core.register_task()`. This context can be concurrent with the
41 traffic processing. It is executed in runtime mode. The prototype of the
42 function is a simple function without return value and without parameters,
43 like this: `function fcn()`.
44
David Carlier61fdf8b2015-10-02 11:59:38 +0100454. The **action context**. It is a Lua function conditionally executed. These
Thierry FOURNIERa2d02252015-10-01 15:00:42 +020046 actions are registered by the Lua directives "`core.register_action()`". The
47 prototype of the Lua called function is a function with doesn't returns
48 anything and that take an object of class TXN as entry. `function fcn(txn)`.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +010049
505. The **sample-fetch context**. This function takes a TXN object as entry
51 argument and returns a string. These types of function cannot execute any
52 blocking function. They are useful to aggregate some of original HAProxy
53 sample-fetches and return the result. The prototype of the function is
54 `function string fcn(txn)`. These functions can be registered with the Lua
55 function `core.register_fetches()`. Each declared sample-fetch is prefixed by
56 the string "lua.".
57
Christopher Faulet1e9b1b62021-08-11 10:14:30 +020058 .. note::
59 It is possible that this function cannot found the required data in the
60 original HAProxy sample-fetches, in this case, it cannot return the
61 result. This case is not yet supported
Thierry FOURNIER17bd1522015-03-11 20:31:00 +010062
David Carlier61fdf8b2015-10-02 11:59:38 +0100636. The **converter context**. It is a Lua function that takes a string as input
Thierry FOURNIER17bd1522015-03-11 20:31:00 +010064 and returns another string as output. These types of function are stateless,
65 it cannot access to any context. They don't execute any blocking function.
66 The call prototype is `function string fcn(string)`. This function can be
67 registered with the Lua function `core.register_converters()`. Each declared
68 converter is prefixed by the string "lua.".
69
Christopher Faulet5a2c6612021-08-15 20:35:25 +0200707. The **filter context**: It is a Lua object based on a class defining filter
71 callback functions. Lua filters are registered using
72 `core.register_filter()`. Each declared filter is prefixed by the string
73 "lua.".
74
Christopher Faulet5a2c6612021-08-15 20:35:25 +020075
Thierry FOURNIER17bd1522015-03-11 20:31:00 +010076HAProxy Lua Hello world
77-----------------------
78
79HAProxy configuration file (`hello_world.conf`):
80
81::
82
83 global
84 lua-load hello_world.lua
85
86 listen proxy
87 bind 127.0.0.1:10001
Thierry FOURNIERa2d02252015-10-01 15:00:42 +020088 tcp-request inspect-delay 1s
89 tcp-request content use-service lua.hello_world
Thierry FOURNIER17bd1522015-03-11 20:31:00 +010090
91HAProxy Lua file (`hello_world.lua`):
92
93.. code-block:: lua
94
Thierry FOURNIERa2d02252015-10-01 15:00:42 +020095 core.register_service("hello_world", "tcp", function(applet)
96 applet:send("hello world\n")
97 end)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +010098
99How to start HAProxy for testing this configuration:
100
101::
102
103 ./haproxy -f hello_world.conf
104
105On other terminal, you can test with telnet:
106
107::
108
109 #:~ telnet 127.0.0.1 10001
110 hello world
111
Thierry Fournierae6b5682022-09-19 09:04:16 +0200112Usage of load parameters
113------------------------
114
Ilya Shipitsin4a689da2022-10-29 09:34:32 +0500115HAProxy lua-load(-per-thread) directives allow a list of parameters after
Thierry Fournierae6b5682022-09-19 09:04:16 +0200116the lua file name. These parameters are accessible through an array of args
117using this code `local args = table.pack(...)` in the body of loaded file.
118
119Below, a new version of the hello world using load parameters
120
121HAProxy configuration file (`hello_world.conf`):
122
123::
124
125 global
126 lua-load hello_world.lua "this is not an hello world"
127
128 listen proxy
129 bind 127.0.0.1:10001
130 tcp-request inspect-delay 1s
131 tcp-request content use-service lua.hello_world
132
133HAProxy Lua file (`hello_world.lua`):
134
135.. code-block:: lua
136
137 local args = table.pack(...)
138
139 core.register_service("hello_world", "tcp", function(applet)
140 applet:send(args[1] .. "\n")
141 end)
142
143
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100144Core class
145==========
146
147.. js:class:: core
148
149 The "core" class contains all the HAProxy core functions. These function are
Aurelien DARRAGON53901f42022-10-13 19:49:42 +0200150 useful for the controlling of the execution flow, registering hooks, manipulating
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100151 global maps or ACL, ...
152
153 "core" class is basically provided with HAProxy. No `require` line is
154 required to uses these function.
155
David Carlier61fdf8b2015-10-02 11:59:38 +0100156 The "core" class is static, it is not possible to create a new object of this
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100157 type.
158
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100159.. js:attribute:: core.emerg
160
Thierry FOURNIERdc595002015-12-21 11:13:52 +0100161 :returns: integer
162
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100163 This attribute is an integer, it contains the value of the loglevel "emergency" (0).
164
165.. js:attribute:: core.alert
166
Thierry FOURNIERdc595002015-12-21 11:13:52 +0100167 :returns: integer
168
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100169 This attribute is an integer, it contains the value of the loglevel "alert" (1).
170
171.. js:attribute:: core.crit
172
Thierry FOURNIERdc595002015-12-21 11:13:52 +0100173 :returns: integer
174
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100175 This attribute is an integer, it contains the value of the loglevel "critical" (2).
176
177.. js:attribute:: core.err
178
Thierry FOURNIERdc595002015-12-21 11:13:52 +0100179 :returns: integer
180
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100181 This attribute is an integer, it contains the value of the loglevel "error" (3).
182
183.. js:attribute:: core.warning
184
Thierry FOURNIERdc595002015-12-21 11:13:52 +0100185 :returns: integer
186
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100187 This attribute is an integer, it contains the value of the loglevel "warning" (4).
188
189.. js:attribute:: core.notice
190
Thierry FOURNIERdc595002015-12-21 11:13:52 +0100191 :returns: integer
192
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100193 This attribute is an integer, it contains the value of the loglevel "notice" (5).
194
195.. js:attribute:: core.info
196
Thierry FOURNIERdc595002015-12-21 11:13:52 +0100197 :returns: integer
198
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100199 This attribute is an integer, it contains the value of the loglevel "info" (6).
200
201.. js:attribute:: core.debug
202
Thierry FOURNIERdc595002015-12-21 11:13:52 +0100203 :returns: integer
204
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100205 This attribute is an integer, it contains the value of the loglevel "debug" (7).
206
Thierry FOURNIER12a865d2016-12-14 19:40:37 +0100207.. js:attribute:: core.proxies
208
209 **context**: task, action, sample-fetch, converter
210
Patrick Hemmerc6a1d712018-05-01 21:30:41 -0400211 This attribute is a table of declared proxies (frontend and backends). Each
212 proxy give an access to his list of listeners and servers. The table is
213 indexed by proxy name, and each entry is of type :ref:`proxy_class`.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +0100214
Christopher Faulet1e9b1b62021-08-11 10:14:30 +0200215 .. Warning::
Aurelien DARRAGON53901f42022-10-13 19:49:42 +0200216 if you declared a frontend and backend with the same name, only one of
217 them will be listed.
Thierry FOURNIER9b82a582017-07-24 13:30:43 +0200218
219 :see: :js:attr:`core.backends`
220 :see: :js:attr:`core.frontends`
221
222.. js:attribute:: core.backends
223
224 **context**: task, action, sample-fetch, converter
225
Patrick Hemmerc6a1d712018-05-01 21:30:41 -0400226 This attribute is a table of declared proxies with backend capability. Each
227 proxy give an access to his list of listeners and servers. The table is
228 indexed by the backend name, and each entry is of type :ref:`proxy_class`.
Thierry FOURNIER9b82a582017-07-24 13:30:43 +0200229
230 :see: :js:attr:`core.proxies`
231 :see: :js:attr:`core.frontends`
232
233.. js:attribute:: core.frontends
234
235 **context**: task, action, sample-fetch, converter
236
Patrick Hemmerc6a1d712018-05-01 21:30:41 -0400237 This attribute is a table of declared proxies with frontend capability. Each
238 proxy give an access to his list of listeners and servers. The table is
239 indexed by the frontend name, and each entry is of type :ref:`proxy_class`.
Thierry FOURNIER9b82a582017-07-24 13:30:43 +0200240
241 :see: :js:attr:`core.proxies`
242 :see: :js:attr:`core.backends`
243
Thierry Fournierecb83c22020-11-28 15:49:44 +0100244.. js:attribute:: core.thread
245
246 **context**: task, action, sample-fetch, converter, applet
247
248 This variable contains the executing thread number starting at 1. 0 is a
249 special case for the common lua context. So, if thread is 0, Lua scope is
250 shared by all threads, otherwise the scope is dedicated to a single thread.
251 A program which needs to execute some parts exactly once regardless of the
252 number of threads can check that core.thread is 0 or 1.
253
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100254.. js:function:: core.log(loglevel, msg)
255
256 **context**: body, init, task, action, sample-fetch, converter
257
David Carlier61fdf8b2015-10-02 11:59:38 +0100258 This function sends a log. The log is sent, according with the HAProxy
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100259 configuration file, on the default syslog server if it is configured and on
260 the stderr if it is allowed.
261
Bertrand Jacquin874a35c2018-09-10 21:26:07 +0100262 :param integer loglevel: Is the log level associated with the message. It is a
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100263 number between 0 and 7.
264 :param string msg: The log content.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +0100265 :see: :js:attr:`core.emerg`, :js:attr:`core.alert`, :js:attr:`core.crit`,
266 :js:attr:`core.err`, :js:attr:`core.warning`, :js:attr:`core.notice`,
267 :js:attr:`core.info`, :js:attr:`core.debug` (log level definitions)
268 :see: :js:func:`core.Debug`
269 :see: :js:func:`core.Info`
270 :see: :js:func:`core.Warning`
271 :see: :js:func:`core.Alert`
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100272
273.. js:function:: core.Debug(msg)
274
275 **context**: body, init, task, action, sample-fetch, converter
276
277 :param string msg: The log content.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +0100278 :see: :js:func:`core.log`
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100279
280 Does the same job than:
281
282.. code-block:: lua
283
Thierry FOURNIERdc595002015-12-21 11:13:52 +0100284 function Debug(msg)
285 core.log(core.debug, msg)
286 end
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100287..
288
289.. js:function:: core.Info(msg)
290
291 **context**: body, init, task, action, sample-fetch, converter
292
293 :param string msg: The log content.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +0100294 :see: :js:func:`core.log`
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100295
296.. code-block:: lua
297
Thierry FOURNIERdc595002015-12-21 11:13:52 +0100298 function Info(msg)
299 core.log(core.info, msg)
300 end
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100301..
302
303.. js:function:: core.Warning(msg)
304
305 **context**: body, init, task, action, sample-fetch, converter
306
307 :param string msg: The log content.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +0100308 :see: :js:func:`core.log`
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100309
310.. code-block:: lua
311
Thierry FOURNIERdc595002015-12-21 11:13:52 +0100312 function Warning(msg)
313 core.log(core.warning, msg)
314 end
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100315..
316
317.. js:function:: core.Alert(msg)
318
319 **context**: body, init, task, action, sample-fetch, converter
320
321 :param string msg: The log content.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +0100322 :see: :js:func:`core.log`
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100323
324.. code-block:: lua
325
Thierry FOURNIERdc595002015-12-21 11:13:52 +0100326 function Alert(msg)
327 core.log(core.alert, msg)
328 end
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100329..
330
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100331.. js:function:: core.add_acl(filename, key)
332
333 **context**: init, task, action, sample-fetch, converter
334
335 Add the ACL *key* in the ACLs list referenced by the file *filename*.
336
337 :param string filename: the filename that reference the ACL entries.
338 :param string key: the key which will be added.
339
340.. js:function:: core.del_acl(filename, key)
341
342 **context**: init, task, action, sample-fetch, converter
343
344 Delete the ACL entry referenced by the key *key* in the list of ACLs
345 referenced by *filename*.
346
347 :param string filename: the filename that reference the ACL entries.
348 :param string key: the key which will be deleted.
349
350.. js:function:: core.del_map(filename, key)
351
352 **context**: init, task, action, sample-fetch, converter
353
354 Delete the map entry indexed with the specified key in the list of maps
355 referenced by his filename.
356
357 :param string filename: the filename that reference the map entries.
358 :param string key: the key which will be deleted.
359
Thierry Fourniereea77c02016-03-18 08:47:13 +0100360.. js:function:: core.get_info()
361
362 **context**: body, init, task, action, sample-fetch, converter
363
Aurelien DARRAGON53901f42022-10-13 19:49:42 +0200364 Returns HAProxy core information. We can find information like the uptime,
Thierry Fourniereea77c02016-03-18 08:47:13 +0100365 the pid, memory pool usage, tasks number, ...
366
Aurelien DARRAGON53901f42022-10-13 19:49:42 +0200367 These informations are also returned by the management socket via the command
Bertrand Jacquin874a35c2018-09-10 21:26:07 +0100368 "show info". See the management socket documentation for more information
Thierry Fourniereea77c02016-03-18 08:47:13 +0100369 about the content of these variables.
370
371 :returns: an array of values.
372
Thierry Fournierb1f46562016-01-21 09:46:15 +0100373.. js:function:: core.now()
374
375 **context**: body, init, task, action
376
377 This function returns the current time. The time returned is fixed by the
Bertrand Jacquin874a35c2018-09-10 21:26:07 +0100378 HAProxy core and assures than the hour will be monotonic and that the system
Thierry Fournierb1f46562016-01-21 09:46:15 +0100379 call 'gettimeofday' will not be called too. The time is refreshed between each
380 Lua execution or resume, so two consecutive call to the function "now" will
381 probably returns the same result.
382
Patrick Hemmerc6a1d712018-05-01 21:30:41 -0400383 :returns: a table which contains two entries "sec" and "usec". "sec"
Thierry Fournierb1f46562016-01-21 09:46:15 +0100384 contains the current at the epoch format, and "usec" contains the
385 current microseconds.
386
Thierry FOURNIERa78f0372016-12-14 19:04:41 +0100387.. js:function:: core.http_date(date)
388
389 **context**: body, init, task, action
390
Bertrand Jacquin874a35c2018-09-10 21:26:07 +0100391 This function take a string representing http date, and returns an integer
Thierry FOURNIERa78f0372016-12-14 19:04:41 +0100392 containing the corresponding date with a epoch format. A valid http date
393 me respect the format IMF, RFC850 or ASCTIME.
394
395 :param string date: a date http-date formatted
396 :returns: integer containing epoch date
397 :see: :js:func:`core.imf_date`.
398 :see: :js:func:`core.rfc850_date`.
399 :see: :js:func:`core.asctime_date`.
400 :see: https://tools.ietf.org/html/rfc7231#section-7.1.1.1
401
402.. js:function:: core.imf_date(date)
403
404 **context**: body, init, task, action
405
Bertrand Jacquin874a35c2018-09-10 21:26:07 +0100406 This function take a string representing IMF date, and returns an integer
Thierry FOURNIERa78f0372016-12-14 19:04:41 +0100407 containing the corresponding date with a epoch format.
408
409 :param string date: a date IMF formatted
410 :returns: integer containing epoch date
411 :see: https://tools.ietf.org/html/rfc7231#section-7.1.1.1
412
413 The IMF format is like this:
414
415.. code-block:: text
416
417 Sun, 06 Nov 1994 08:49:37 GMT
418..
419
420.. js:function:: core.rfc850_date(date)
421
422 **context**: body, init, task, action
423
Bertrand Jacquin874a35c2018-09-10 21:26:07 +0100424 This function take a string representing RFC850 date, and returns an integer
Thierry FOURNIERa78f0372016-12-14 19:04:41 +0100425 containing the corresponding date with a epoch format.
426
427 :param string date: a date RFC859 formatted
428 :returns: integer containing epoch date
429 :see: https://tools.ietf.org/html/rfc7231#section-7.1.1.1
430
431 The RFC850 format is like this:
432
433.. code-block:: text
434
435 Sunday, 06-Nov-94 08:49:37 GMT
436..
437
438.. js:function:: core.asctime_date(date)
439
440 **context**: body, init, task, action
441
Bertrand Jacquin874a35c2018-09-10 21:26:07 +0100442 This function take a string representing ASCTIME date, and returns an integer
Thierry FOURNIERa78f0372016-12-14 19:04:41 +0100443 containing the corresponding date with a epoch format.
444
445 :param string date: a date ASCTIME formatted
446 :returns: integer containing epoch date
447 :see: https://tools.ietf.org/html/rfc7231#section-7.1.1.1
448
449 The ASCTIME format is like this:
450
451.. code-block:: text
452
453 Sun Nov 6 08:49:37 1994
454..
455
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100456.. js:function:: core.msleep(milliseconds)
457
458 **context**: body, init, task, action
459
460 The `core.msleep()` stops the Lua execution between specified milliseconds.
461
462 :param integer milliseconds: the required milliseconds.
463
Thierry FOURNIERc5d11c62018-02-12 14:46:54 +0100464.. js:function:: core.register_action(name, actions, func [, nb_args])
Thierry FOURNIER8255a752015-09-23 21:03:35 +0200465
466 **context**: body
467
David Carlier61fdf8b2015-10-02 11:59:38 +0100468 Register a Lua function executed as action. All the registered action can be
Thierry FOURNIER8255a752015-09-23 21:03:35 +0200469 used in HAProxy with the prefix "lua.". An action gets a TXN object class as
470 input.
471
472 :param string name: is the name of the converter.
Willy Tarreau61add3c2015-09-28 15:39:10 +0200473 :param table actions: is a table of string describing the HAProxy actions who
Thierry FOURNIER8255a752015-09-23 21:03:35 +0200474 want to register to. The expected actions are 'tcp-req',
475 'tcp-res', 'http-req' or 'http-res'.
Aurelien DARRAGON53901f42022-10-13 19:49:42 +0200476 :param function func: is the Lua function called to work as converter.
Thierry FOURNIERc5d11c62018-02-12 14:46:54 +0100477 :param integer nb_args: is the expected number of argument for the action.
478 By default the value is 0.
Thierry FOURNIER8255a752015-09-23 21:03:35 +0200479
480 The prototype of the Lua function used as argument is:
481
482.. code-block:: lua
483
Thierry FOURNIERc5d11c62018-02-12 14:46:54 +0100484 function(txn [, arg1 [, arg2]])
Thierry FOURNIER8255a752015-09-23 21:03:35 +0200485..
486
Thierry FOURNIERdc595002015-12-21 11:13:52 +0100487 * **txn** (:ref:`txn_class`): this is a TXN object used for manipulating the
Thierry FOURNIER8255a752015-09-23 21:03:35 +0200488 current request or TCP stream.
489
Bertrand Jacquin874a35c2018-09-10 21:26:07 +0100490 * **argX**: this is argument provided through the HAProxy configuration file.
Thierry FOURNIERc5d11c62018-02-12 14:46:54 +0100491
Bertrand Jacquin874a35c2018-09-10 21:26:07 +0100492 Here, an example of action registration. The action just send an 'Hello world'
Thierry FOURNIER8255a752015-09-23 21:03:35 +0200493 in the logs.
494
495.. code-block:: lua
496
497 core.register_action("hello-world", { "tcp-req", "http-req" }, function(txn)
498 txn:Info("Hello world")
499 end)
500..
501
Willy Tarreau714f3452021-05-09 06:47:26 +0200502 This example code is used in HAProxy configuration like this:
Thierry FOURNIER8255a752015-09-23 21:03:35 +0200503
504::
505
506 frontend tcp_frt
507 mode tcp
508 tcp-request content lua.hello-world
509
510 frontend http_frt
511 mode http
512 http-request lua.hello-world
Thierry FOURNIERc5d11c62018-02-12 14:46:54 +0100513..
514
Bertrand Jacquin874a35c2018-09-10 21:26:07 +0100515 A second example using arguments
Thierry FOURNIERc5d11c62018-02-12 14:46:54 +0100516
517.. code-block:: lua
518
519 function hello_world(txn, arg)
520 txn:Info("Hello world for " .. arg)
521 end
522 core.register_action("hello-world", { "tcp-req", "http-req" }, hello_world, 2)
523..
Thierry FOURNIER8255a752015-09-23 21:03:35 +0200524
Willy Tarreau714f3452021-05-09 06:47:26 +0200525 This example code is used in HAProxy configuration like this:
Thierry FOURNIERc5d11c62018-02-12 14:46:54 +0100526
527::
528
529 frontend tcp_frt
530 mode tcp
531 tcp-request content lua.hello-world everybody
532..
Aurelien DARRAGON53901f42022-10-13 19:49:42 +0200533
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100534.. js:function:: core.register_converters(name, func)
535
536 **context**: body
537
David Carlier61fdf8b2015-10-02 11:59:38 +0100538 Register a Lua function executed as converter. All the registered converters
Aurelien DARRAGON53901f42022-10-13 19:49:42 +0200539 can be used in HAProxy with the prefix "lua.". A converter gets a string as
540 input and returns a string as output. The registered function can take up to 9
541 values as parameter. All the values are strings.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100542
543 :param string name: is the name of the converter.
544 :param function func: is the Lua function called to work as converter.
545
546 The prototype of the Lua function used as argument is:
547
548.. code-block:: lua
549
550 function(str, [p1 [, p2 [, ... [, p5]]]])
551..
552
553 * **str** (*string*): this is the input value automatically converted in
554 string.
555 * **p1** .. **p5** (*string*): this is a list of string arguments declared in
Bertrand Jacquin874a35c2018-09-10 21:26:07 +0100556 the HAProxy configuration file. The number of arguments doesn't exceed 5.
Aurelien DARRAGON53901f42022-10-13 19:49:42 +0200557 The order and the nature of these is conventionally chosen by the
Bertrand Jacquin874a35c2018-09-10 21:26:07 +0100558 developer.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100559
560.. js:function:: core.register_fetches(name, func)
561
562 **context**: body
563
David Carlier61fdf8b2015-10-02 11:59:38 +0100564 Register a Lua function executed as sample fetch. All the registered sample
Bertrand Jacquin874a35c2018-09-10 21:26:07 +0100565 fetch can be used in HAProxy with the prefix "lua.". A Lua sample fetch
Aurelien DARRAGON53901f42022-10-13 19:49:42 +0200566 returns a string as output. The registered function can take up to 9 values as
567 parameter. All the values are strings.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100568
Aurelien DARRAGON53901f42022-10-13 19:49:42 +0200569 :param string name: is the name of the sample fetch.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100570 :param function func: is the Lua function called to work as sample fetch.
571
572 The prototype of the Lua function used as argument is:
573
574.. code-block:: lua
575
576 string function(txn, [p1 [, p2 [, ... [, p5]]]])
577..
578
Thierry FOURNIERdc595002015-12-21 11:13:52 +0100579 * **txn** (:ref:`txn_class`): this is the txn object associated with the current
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100580 request.
581 * **p1** .. **p5** (*string*): this is a list of string arguments declared in
Bertrand Jacquin874a35c2018-09-10 21:26:07 +0100582 the HAProxy configuration file. The number of arguments doesn't exceed 5.
Aurelien DARRAGON53901f42022-10-13 19:49:42 +0200583 The order and the nature of these is conventionally chosen by the
Bertrand Jacquin874a35c2018-09-10 21:26:07 +0100584 developer.
585 * **Returns**: A string containing some data, or nil if the value cannot be
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100586 returned now.
587
588 lua example code:
589
590.. code-block:: lua
591
592 core.register_fetches("hello", function(txn)
593 return "hello"
594 end)
595..
596
597 HAProxy example configuration:
598
599::
600
601 frontend example
602 http-request redirect location /%[lua.hello]
603
Christopher Faulet5a2c6612021-08-15 20:35:25 +0200604.. js:function:: core.register_filter(name, Flt, func)
605
606 **context**: body
607
608 Register a Lua function used to declare a filter. All the registered filters
609 can by used in HAProxy with the prefix "lua.".
610
611 :param string name: is the name of the filter.
612 :param table Flt: is a Lua class containing the filter definition (id, flags,
613 callbacks).
614 :param function func: is the Lua function called to create the Lua filter.
615
616 The prototype of the Lua function used as argument is:
617
618.. code-block:: lua
619
620 function(flt, args)
621..
622
623 * **flt** : Is a filter object based on the class provided in
624 :js:func:`core.register_filter()` function.
625
626 * **args**: Is a table of strings containing all arguments provided through
627 the HAProxy configuration file, on the filter line.
628
629 It must return the filter to use or nil to ignore it. Here, an example of
630 filter registration.
631
632.. code-block:: lua
633
634 core.register_filter("my-filter", MyFilter, function(flt, args)
635 flt.args = args -- Save arguments
636 return flt
637 end)
638..
639
640 This example code is used in HAProxy configuration like this:
641
642::
643
644 frontend http
645 mode http
646 filter lua.my-filter arg1 arg2 arg3
647..
648
649 :see: :js:class:`Filter`
650
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +0200651.. js:function:: core.register_service(name, mode, func)
652
653 **context**: body
654
Aurelien DARRAGON53901f42022-10-13 19:49:42 +0200655 Register a Lua function executed as a service. All the registered services can
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +0200656 be used in HAProxy with the prefix "lua.". A service gets an object class as
657 input according with the required mode.
658
Aurelien DARRAGON53901f42022-10-13 19:49:42 +0200659 :param string name: is the name of the service.
Willy Tarreau61add3c2015-09-28 15:39:10 +0200660 :param string mode: is string describing the required mode. Only 'tcp' or
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +0200661 'http' are allowed.
Aurelien DARRAGON53901f42022-10-13 19:49:42 +0200662 :param function func: is the Lua function called to work as service.
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +0200663
664 The prototype of the Lua function used as argument is:
665
666.. code-block:: lua
667
668 function(applet)
669..
670
Thierry FOURNIERdc595002015-12-21 11:13:52 +0100671 * **applet** *applet* will be a :ref:`applettcp_class` or a
672 :ref:`applethttp_class`. It depends the type of registered applet. An applet
673 registered with the 'http' value for the *mode* parameter will gets a
674 :ref:`applethttp_class`. If the *mode* value is 'tcp', the applet will gets
675 a :ref:`applettcp_class`.
676
Christopher Faulet1e9b1b62021-08-11 10:14:30 +0200677 .. warning::
678 Applets of type 'http' cannot be called from 'tcp-*' rulesets. Only the
679 'http-*' rulesets are authorized, this means that is not possible to call
Aurelien DARRAGON53901f42022-10-13 19:49:42 +0200680 a HTTP applet from a proxy in tcp mode. Applets of type 'tcp' can be
Christopher Faulet1e9b1b62021-08-11 10:14:30 +0200681 called from anywhere.
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +0200682
Bertrand Jacquin874a35c2018-09-10 21:26:07 +0100683 Here, an example of service registration. The service just send an 'Hello world'
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +0200684 as an http response.
685
686.. code-block:: lua
687
Pieter Baauw4d7f7662015-11-08 16:38:08 +0100688 core.register_service("hello-world", "http", function(applet)
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +0200689 local response = "Hello World !"
690 applet:set_status(200)
Pieter Baauw2dcb9bc2015-10-01 22:47:12 +0200691 applet:add_header("content-length", string.len(response))
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +0200692 applet:add_header("content-type", "text/plain")
Pieter Baauw2dcb9bc2015-10-01 22:47:12 +0200693 applet:start_response()
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +0200694 applet:send(response)
695 end)
696..
697
Willy Tarreau714f3452021-05-09 06:47:26 +0200698 This example code is used in HAProxy configuration like this:
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +0200699
700::
701
702 frontend example
703 http-request use-service lua.hello-world
704
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100705.. js:function:: core.register_init(func)
706
707 **context**: body
708
709 Register a function executed after the configuration parsing. This is useful
710 to check any parameters.
711
Pieter Baauw4d7f7662015-11-08 16:38:08 +0100712 :param function func: is the Lua function called to work as initializer.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100713
714 The prototype of the Lua function used as argument is:
715
716.. code-block:: lua
717
718 function()
719..
720
721 It takes no input, and no output is expected.
722
723.. js:function:: core.register_task(func)
724
725 **context**: body, init, task, action, sample-fetch, converter
726
727 Register and start independent task. The task is started when the HAProxy
728 main scheduler starts. For example this type of tasks can be executed to
Thierry FOURNIER2e4893c2015-03-18 13:37:27 +0100729 perform complex health checks.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100730
Pieter Baauw4d7f7662015-11-08 16:38:08 +0100731 :param function func: is the Lua function called to work as initializer.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100732
733 The prototype of the Lua function used as argument is:
734
735.. code-block:: lua
736
737 function()
738..
739
740 It takes no input, and no output is expected.
741
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +0100742.. js:function:: core.register_cli([path], usage, func)
743
744 **context**: body
745
Aurelien DARRAGON53901f42022-10-13 19:49:42 +0200746 Register a custom cli that will be available from haproxy stats socket.
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +0100747
748 :param array path: is the sequence of word for which the cli execute the Lua
749 binding.
750 :param string usage: is the usage message displayed in the help.
751 :param function func: is the Lua function called to handle the CLI commands.
752
753 The prototype of the Lua function used as argument is:
754
755.. code-block:: lua
756
757 function(AppletTCP, [arg1, [arg2, [...]]])
758..
759
760 I/O are managed with the :ref:`applettcp_class` object. Args are given as
Bertrand Jacquin874a35c2018-09-10 21:26:07 +0100761 parameter. The args embed the registered path. If the path is declared like
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +0100762 this:
763
764.. code-block:: lua
765
766 core.register_cli({"show", "ssl", "stats"}, "Display SSL stats..", function(applet, arg1, arg2, arg3, arg4, arg5)
767 end)
768..
769
770 And we execute this in the prompt:
771
772.. code-block:: text
773
774 > prompt
775 > show ssl stats all
776..
777
Bertrand Jacquin874a35c2018-09-10 21:26:07 +0100778 Then, arg1, arg2 and arg3 will contains respectively "show", "ssl" and "stats".
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +0100779 arg4 will contain "all". arg5 contains nil.
780
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100781.. js:function:: core.set_nice(nice)
782
783 **context**: task, action, sample-fetch, converter
784
785 Change the nice of the current task or current session.
Thierry FOURNIER2e4893c2015-03-18 13:37:27 +0100786
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100787 :param integer nice: the nice value, it must be between -1024 and 1024.
788
789.. js:function:: core.set_map(filename, key, value)
790
791 **context**: init, task, action, sample-fetch, converter
792
Bertrand Jacquin874a35c2018-09-10 21:26:07 +0100793 Set the value *value* associated to the key *key* in the map referenced by
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100794 *filename*.
795
Thierry FOURNIERdc595002015-12-21 11:13:52 +0100796 :param string filename: the Map reference
797 :param string key: the key to set or replace
798 :param string value: the associated value
799
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100800.. js:function:: core.sleep(int seconds)
801
802 **context**: body, init, task, action
803
804 The `core.sleep()` functions stop the Lua execution between specified seconds.
805
806 :param integer seconds: the required seconds.
807
808.. js:function:: core.tcp()
809
810 **context**: init, task, action
811
812 This function returns a new object of a *socket* class.
813
Thierry FOURNIERdc595002015-12-21 11:13:52 +0100814 :returns: A :ref:`socket_class` object.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100815
William Lallemand00a15022021-11-19 16:02:44 +0100816.. js:function:: core.httpclient()
817
818 **context**: init, task, action
819
820 This function returns a new object of a *httpclient* class.
821
822 :returns: A :ref:`httpclient_class` object.
823
Thierry Fournier1de16592016-01-27 09:49:07 +0100824.. js:function:: core.concat()
825
826 **context**: body, init, task, action, sample-fetch, converter
827
Bertrand Jacquin874a35c2018-09-10 21:26:07 +0100828 This function returns a new concat object.
Thierry Fournier1de16592016-01-27 09:49:07 +0100829
830 :returns: A :ref:`concat_class` object.
831
Thierry FOURNIER0a99b892015-08-26 00:14:17 +0200832.. js:function:: core.done(data)
833
834 **context**: body, init, task, action, sample-fetch, converter
835
836 :param any data: Return some data for the caller. It is useful with
837 sample-fetches and sample-converters.
838
839 Immediately stops the current Lua execution and returns to the caller which
840 may be a sample fetch, a converter or an action and returns the specified
Thierry Fournier4234dbd2020-11-28 13:18:23 +0100841 value (ignored for actions and init). It is used when the LUA process finishes
842 its work and wants to give back the control to HAProxy without executing the
Thierry FOURNIER0a99b892015-08-26 00:14:17 +0200843 remaining code. It can be seen as a multi-level "return".
844
Thierry FOURNIER486f5a02015-03-16 15:13:03 +0100845.. js:function:: core.yield()
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100846
847 **context**: task, action, sample-fetch, converter
848
849 Give back the hand at the HAProxy scheduler. It is used when the LUA
850 processing consumes a lot of processing time.
851
Thierry FOURNIER / OZON.IO62fec752016-11-10 20:38:11 +0100852.. js:function:: core.parse_addr(address)
853
854 **context**: body, init, task, action, sample-fetch, converter
855
856 :param network: is a string describing an ipv4 or ipv6 address and optionally
857 its network length, like this: "127.0.0.1/8" or "aaaa::1234/32".
858 :returns: a userdata containing network or nil if an error occurs.
859
Bertrand Jacquin874a35c2018-09-10 21:26:07 +0100860 Parse ipv4 or ipv6 addresses and its facultative associated network.
Thierry FOURNIER / OZON.IO62fec752016-11-10 20:38:11 +0100861
862.. js:function:: core.match_addr(addr1, addr2)
863
864 **context**: body, init, task, action, sample-fetch, converter
865
866 :param addr1: is an address created with "core.parse_addr".
867 :param addr2: is an address created with "core.parse_addr".
Bertrand Jacquin874a35c2018-09-10 21:26:07 +0100868 :returns: boolean, true if the network of the addresses match, else returns
Thierry FOURNIER / OZON.IO62fec752016-11-10 20:38:11 +0100869 false.
870
Ilya Shipitsin2075ca82020-03-06 23:22:22 +0500871 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 +0100872 of network is not important.
873
Thierry FOURNIER / OZON.IO8a1027a2016-11-24 20:48:38 +0100874.. js:function:: core.tokenize(str, separators [, noblank])
875
876 **context**: body, init, task, action, sample-fetch, converter
877
878 This function is useful for tokenizing an entry, or splitting some messages.
879 :param string str: The string which will be split.
880 :param string separators: A string containing a list of separators.
881 :param boolean noblank: Ignore empty entries.
882 :returns: an array of string.
883
884 For example:
885
886.. code-block:: lua
887
888 local array = core.tokenize("This function is useful, for tokenizing an entry.", "., ", true)
889 print_r(array)
890..
891
892 Returns this array:
893
894.. code-block:: text
895
896 (table) table: 0x21c01e0 [
897 1: (string) "This"
898 2: (string) "function"
899 3: (string) "is"
900 4: (string) "useful"
901 5: (string) "for"
902 6: (string) "tokenizing"
903 7: (string) "an"
904 8: (string) "entry"
905 ]
906..
907
Thierry Fournierf61aa632016-02-19 20:56:00 +0100908.. _proxy_class:
909
910Proxy class
911============
912
913.. js:class:: Proxy
914
915 This class provides a way for manipulating proxy and retrieving information
916 like statistics.
917
Thierry FOURNIER817e7592017-07-24 14:35:04 +0200918.. js:attribute:: Proxy.name
919
920 Contain the name of the proxy.
921
Baptiste Assmann46c72552017-10-26 21:51:58 +0200922.. js:attribute:: Proxy.uuid
923
924 Contain the unique identifier of the proxy.
925
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +0100926.. js:attribute:: Proxy.servers
927
Patrick Hemmerc6a1d712018-05-01 21:30:41 -0400928 Contain a table with the attached servers. The table is indexed by server
929 name, and each server entry is an object of type :ref:`server_class`.
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +0100930
Adis Nezirovic8878f8e2018-07-13 12:18:33 +0200931.. js:attribute:: Proxy.stktable
932
933 Contains a stick table object attached to the proxy.
934
Thierry Fournierff480422016-02-25 08:36:46 +0100935.. js:attribute:: Proxy.listeners
936
Patrick Hemmerc6a1d712018-05-01 21:30:41 -0400937 Contain a table with the attached listeners. The table is indexed by listener
938 name, and each each listeners entry is an object of type
939 :ref:`listener_class`.
Thierry Fournierff480422016-02-25 08:36:46 +0100940
Thierry Fournierf61aa632016-02-19 20:56:00 +0100941.. js:function:: Proxy.pause(px)
942
943 Pause the proxy. See the management socket documentation for more information.
944
945 :param class_proxy px: A :ref:`proxy_class` which indicates the manipulated
946 proxy.
947
948.. js:function:: Proxy.resume(px)
949
950 Resume the proxy. See the management socket documentation for more
951 information.
952
953 :param class_proxy px: A :ref:`proxy_class` which indicates the manipulated
954 proxy.
955
956.. js:function:: Proxy.stop(px)
957
958 Stop the proxy. See the management socket documentation for more information.
959
960 :param class_proxy px: A :ref:`proxy_class` which indicates the manipulated
961 proxy.
962
963.. js:function:: Proxy.shut_bcksess(px)
964
965 Kill the session attached to a backup server. See the management socket
966 documentation for more information.
967
968 :param class_proxy px: A :ref:`proxy_class` which indicates the manipulated
969 proxy.
970
971.. js:function:: Proxy.get_cap(px)
972
973 Returns a string describing the capabilities of the proxy.
974
975 :param class_proxy px: A :ref:`proxy_class` which indicates the manipulated
976 proxy.
977 :returns: a string "frontend", "backend", "proxy" or "ruleset".
978
979.. js:function:: Proxy.get_mode(px)
980
981 Returns a string describing the mode of the current proxy.
982
983 :param class_proxy px: A :ref:`proxy_class` which indicates the manipulated
984 proxy.
985 :returns: a string "tcp", "http", "health" or "unknown"
986
987.. js:function:: Proxy.get_stats(px)
988
Bertrand Jacquin874a35c2018-09-10 21:26:07 +0100989 Returns a table containing the proxy statistics. The statistics returned are
Thierry Fournierf61aa632016-02-19 20:56:00 +0100990 not the same if the proxy is frontend or a backend.
991
992 :param class_proxy px: A :ref:`proxy_class` which indicates the manipulated
993 proxy.
Patrick Hemmerc6a1d712018-05-01 21:30:41 -0400994 :returns: a key/value table containing stats
Thierry Fournierf61aa632016-02-19 20:56:00 +0100995
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +0100996.. _server_class:
997
998Server class
999============
1000
Patrick Hemmerc6a1d712018-05-01 21:30:41 -04001001.. js:class:: Server
1002
1003 This class provides a way for manipulating servers and retrieving information.
1004
Patrick Hemmera62ae7e2018-04-29 14:23:48 -04001005.. js:attribute:: Server.name
1006
1007 Contain the name of the server.
1008
1009.. js:attribute:: Server.puid
1010
1011 Contain the proxy unique identifier of the server.
1012
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001013.. js:function:: Server.is_draining(sv)
1014
Patrick Hemmerc6a1d712018-05-01 21:30:41 -04001015 Return true if the server is currently draining sticky connections.
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001016
1017 :param class_server sv: A :ref:`server_class` which indicates the manipulated
1018 server.
1019 :returns: a boolean
1020
Patrick Hemmer32d539f2018-04-29 14:25:46 -04001021.. js:function:: Server.set_maxconn(sv, weight)
1022
1023 Dynamically change the maximum connections of the server. See the management
1024 socket documentation for more information about the format of the string.
1025
1026 :param class_server sv: A :ref:`server_class` which indicates the manipulated
1027 server.
1028 :param string maxconn: A string describing the server maximum connections.
1029
1030.. js:function:: Server.get_maxconn(sv, weight)
1031
1032 This function returns an integer representing the server maximum connections.
1033
1034 :param class_server sv: A :ref:`server_class` which indicates the manipulated
1035 server.
1036 :returns: an integer.
1037
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001038.. js:function:: Server.set_weight(sv, weight)
1039
Patrick Hemmerc6a1d712018-05-01 21:30:41 -04001040 Dynamically change the weight of the server. See the management socket
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001041 documentation for more information about the format of the string.
1042
1043 :param class_server sv: A :ref:`server_class` which indicates the manipulated
1044 server.
1045 :param string weight: A string describing the server weight.
1046
1047.. js:function:: Server.get_weight(sv)
1048
Patrick Hemmerc6a1d712018-05-01 21:30:41 -04001049 This function returns an integer representing the server weight.
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001050
1051 :param class_server sv: A :ref:`server_class` which indicates the manipulated
1052 server.
1053 :returns: an integer.
1054
Joseph C. Sible49bbf522020-05-04 22:20:32 -04001055.. js:function:: Server.set_addr(sv, addr[, port])
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001056
Patrick Hemmerc6a1d712018-05-01 21:30:41 -04001057 Dynamically change the address of the server. See the management socket
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001058 documentation for more information about the format of the string.
1059
1060 :param class_server sv: A :ref:`server_class` which indicates the manipulated
1061 server.
Patrick Hemmerc6a1d712018-05-01 21:30:41 -04001062 :param string addr: A string describing the server address.
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001063
1064.. js:function:: Server.get_addr(sv)
1065
Patrick Hemmerc6a1d712018-05-01 21:30:41 -04001066 Returns a string describing the address of the server.
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001067
1068 :param class_server sv: A :ref:`server_class` which indicates the manipulated
1069 server.
1070 :returns: A string
1071
1072.. js:function:: Server.get_stats(sv)
1073
1074 Returns server statistics.
1075
1076 :param class_server sv: A :ref:`server_class` which indicates the manipulated
1077 server.
Patrick Hemmerc6a1d712018-05-01 21:30:41 -04001078 :returns: a key/value table containing stats
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001079
1080.. js:function:: Server.shut_sess(sv)
1081
1082 Shutdown all the sessions attached to the server. See the management socket
1083 documentation for more information about this function.
1084
1085 :param class_server sv: A :ref:`server_class` which indicates the manipulated
1086 server.
1087
1088.. js:function:: Server.set_drain(sv)
1089
1090 Drain sticky sessions. See the management socket documentation for more
1091 information about this function.
1092
1093 :param class_server sv: A :ref:`server_class` which indicates the manipulated
1094 server.
1095
1096.. js:function:: Server.set_maint(sv)
1097
1098 Set maintenance mode. See the management socket documentation for more
1099 information about this function.
1100
1101 :param class_server sv: A :ref:`server_class` which indicates the manipulated
1102 server.
1103
1104.. js:function:: Server.set_ready(sv)
1105
1106 Set normal mode. See the management socket documentation for more information
1107 about this function.
1108
1109 :param class_server sv: A :ref:`server_class` which indicates the manipulated
1110 server.
1111
1112.. js:function:: Server.check_enable(sv)
1113
1114 Enable health checks. See the management socket documentation for more
1115 information about this function.
1116
1117 :param class_server sv: A :ref:`server_class` which indicates the manipulated
1118 server.
1119
1120.. js:function:: Server.check_disable(sv)
1121
1122 Disable health checks. See the management socket documentation for more
1123 information about this function.
1124
1125 :param class_server sv: A :ref:`server_class` which indicates the manipulated
1126 server.
1127
1128.. js:function:: Server.check_force_up(sv)
1129
1130 Force health-check up. See the management socket documentation for more
1131 information about this function.
1132
1133 :param class_server sv: A :ref:`server_class` which indicates the manipulated
1134 server.
1135
1136.. js:function:: Server.check_force_nolb(sv)
1137
1138 Force health-check nolb mode. See the management socket documentation for more
1139 information about this function.
1140
1141 :param class_server sv: A :ref:`server_class` which indicates the manipulated
1142 server.
1143
1144.. js:function:: Server.check_force_down(sv)
1145
1146 Force health-check down. See the management socket documentation for more
1147 information about this function.
1148
1149 :param class_server sv: A :ref:`server_class` which indicates the manipulated
1150 server.
1151
1152.. js:function:: Server.agent_enable(sv)
1153
1154 Enable agent check. See the management socket documentation for more
1155 information about this function.
1156
1157 :param class_server sv: A :ref:`server_class` which indicates the manipulated
1158 server.
1159
1160.. js:function:: Server.agent_disable(sv)
1161
1162 Disable agent check. See the management socket documentation for more
1163 information about this function.
1164
1165 :param class_server sv: A :ref:`server_class` which indicates the manipulated
1166 server.
1167
1168.. js:function:: Server.agent_force_up(sv)
1169
1170 Force agent check up. See the management socket documentation for more
1171 information about this function.
1172
1173 :param class_server sv: A :ref:`server_class` which indicates the manipulated
1174 server.
1175
1176.. js:function:: Server.agent_force_down(sv)
1177
1178 Force agent check down. See the management socket documentation for more
1179 information about this function.
1180
1181 :param class_server sv: A :ref:`server_class` which indicates the manipulated
1182 server.
1183
Thierry Fournierff480422016-02-25 08:36:46 +01001184.. _listener_class:
1185
1186Listener class
1187==============
1188
1189.. js:function:: Listener.get_stats(ls)
1190
1191 Returns server statistics.
1192
1193 :param class_listener ls: A :ref:`listener_class` which indicates the
1194 manipulated listener.
Patrick Hemmerc6a1d712018-05-01 21:30:41 -04001195 :returns: a key/value table containing stats
Thierry Fournierff480422016-02-25 08:36:46 +01001196
Thierry Fournier1de16592016-01-27 09:49:07 +01001197.. _concat_class:
1198
1199Concat class
1200============
1201
1202.. js:class:: Concat
1203
1204 This class provides a fast way for string concatenation. The way using native
1205 Lua concatenation like the code below is slow for some reasons.
1206
1207.. code-block:: lua
1208
1209 str = "string1"
1210 str = str .. ", string2"
1211 str = str .. ", string3"
1212..
1213
1214 For each concatenation, Lua:
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02001215 - allocates memory for the result,
1216 - catenates the two string copying the strings in the new memory block,
1217 - frees the old memory block containing the string which is no longer used.
1218
Thierry Fournier1de16592016-01-27 09:49:07 +01001219 This process does many memory move, allocation and free. In addition, the
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02001220 memory is not really freed, it is just marked as unused and waits for the
Thierry Fournier1de16592016-01-27 09:49:07 +01001221 garbage collector.
1222
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02001223 The Concat class provides an alternative way to concatenate strings. It uses
Thierry Fournier1de16592016-01-27 09:49:07 +01001224 the internal Lua mechanism (it does not allocate memory), but it doesn't copy
1225 the data more than once.
1226
1227 On my computer, the following loops spends 0.2s for the Concat method and
1228 18.5s for the pure Lua implementation. So, the Concat class is about 1000x
1229 faster than the embedded solution.
1230
1231.. code-block:: lua
1232
1233 for j = 1, 100 do
1234 c = core.concat()
1235 for i = 1, 20000 do
1236 c:add("#####")
1237 end
1238 end
1239..
1240
1241.. code-block:: lua
1242
1243 for j = 1, 100 do
1244 c = ""
1245 for i = 1, 20000 do
1246 c = c .. "#####"
1247 end
1248 end
1249..
1250
1251.. js:function:: Concat.add(concat, string)
1252
1253 This function adds a string to the current concatenated string.
1254
1255 :param class_concat concat: A :ref:`concat_class` which contains the currently
Ilya Shipitsin2075ca82020-03-06 23:22:22 +05001256 built string.
Bertrand Jacquin874a35c2018-09-10 21:26:07 +01001257 :param string string: A new string to concatenate to the current built
Thierry Fournier1de16592016-01-27 09:49:07 +01001258 string.
1259
1260.. js:function:: Concat.dump(concat)
1261
Bertrand Jacquin874a35c2018-09-10 21:26:07 +01001262 This function returns the concatenated string.
Thierry Fournier1de16592016-01-27 09:49:07 +01001263
1264 :param class_concat concat: A :ref:`concat_class` which contains the currently
Ilya Shipitsin2075ca82020-03-06 23:22:22 +05001265 built string.
Thierry Fournier1de16592016-01-27 09:49:07 +01001266 :returns: the concatenated string
1267
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001268.. _fetches_class:
1269
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001270Fetches class
1271=============
1272
1273.. js:class:: Fetches
1274
1275 This class contains a lot of internal HAProxy sample fetches. See the
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02001276 HAProxy "configuration.txt" documentation for more information.
1277 (chapters 7.3.2 to 7.3.6)
Thierry FOURNIER2e4893c2015-03-18 13:37:27 +01001278
Christopher Faulet1e9b1b62021-08-11 10:14:30 +02001279 .. warning::
1280 some sample fetches are not available in some context. These limitations
1281 are specified in this documentation when they're useful.
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001282
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01001283 :see: :js:attr:`TXN.f`
1284 :see: :js:attr:`TXN.sf`
Thierry FOURNIER2e4893c2015-03-18 13:37:27 +01001285
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02001286 Fetches are useful to:
Thierry FOURNIER2e4893c2015-03-18 13:37:27 +01001287
1288 * get system time,
1289 * get environment variable,
1290 * get random numbers,
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02001291 * know backend status like the number of users in queue or the number of
Thierry FOURNIER2e4893c2015-03-18 13:37:27 +01001292 connections established,
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02001293 * get client information like ip source or destination,
Thierry FOURNIER2e4893c2015-03-18 13:37:27 +01001294 * deal with stick tables,
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02001295 * fetch established SSL information,
1296 * fetch HTTP information like headers or method.
Thierry FOURNIER2e4893c2015-03-18 13:37:27 +01001297
1298.. code-block:: lua
1299
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001300 function action(txn)
1301 -- Get source IP
1302 local clientip = txn.f:src()
1303 end
Thierry FOURNIER2e4893c2015-03-18 13:37:27 +01001304..
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001305
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001306.. _converters_class:
1307
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001308Converters class
1309================
1310
1311.. js:class:: Converters
1312
1313 This class contains a lot of internal HAProxy sample converters. See the
Thierry FOURNIER2e4893c2015-03-18 13:37:27 +01001314 HAProxy documentation "configuration.txt" for more information about her
1315 usage. Its the chapter 7.3.1.
1316
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01001317 :see: :js:attr:`TXN.c`
1318 :see: :js:attr:`TXN.sc`
Thierry FOURNIER2e4893c2015-03-18 13:37:27 +01001319
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02001320 Converters provides stateful transformation. They are useful to:
Thierry FOURNIER2e4893c2015-03-18 13:37:27 +01001321
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02001322 * convert input to base64,
1323 * apply hash on input string (djb2, crc32, sdbm, wt6),
Thierry FOURNIER2e4893c2015-03-18 13:37:27 +01001324 * format date,
1325 * json escape,
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02001326 * extract preferred language comparing two lists,
Thierry FOURNIER2e4893c2015-03-18 13:37:27 +01001327 * turn to lower or upper chars,
1328 * deal with stick tables.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001329
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001330.. _channel_class:
1331
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001332Channel class
1333=============
1334
1335.. js:class:: Channel
1336
Christopher Faulet5a2c6612021-08-15 20:35:25 +02001337 **context**: action, sample-fetch, convert, filter
1338
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001339 HAProxy uses two buffers for the processing of the requests. The first one is
1340 used with the request data (from the client to the server) and the second is
1341 used for the response data (from the server to the client).
1342
1343 Each buffer contains two types of data. The first type is the incoming data
1344 waiting for a processing. The second part is the outgoing data already
1345 processed. Usually, the incoming data is processed, after it is tagged as
1346 outgoing data, and finally it is sent. The following functions provides tools
1347 for manipulating these data in a buffer.
1348
1349 The following diagram shows where the channel class function are applied.
1350
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001351 .. image:: _static/channel.png
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001352
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001353 .. warning::
1354 It is not possible to read from the response in request action, and it is
Boyang Li60cfe8b2022-05-10 18:11:00 +00001355 not possible to read from the request channel in response action.
Christopher Faulet09530392021-06-14 11:43:18 +02001356
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001357 .. warning::
1358 It is forbidden to alter the Channels buffer from HTTP contexts. So only
1359 :js:func:`Channel.input`, :js:func:`Channel.output`,
1360 :js:func:`Channel.may_recv`, :js:func:`Channel.is_full` and
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02001361 :js:func:`Channel.is_resp` can be called from a HTTP context.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001362
Christopher Faulet5a2c6612021-08-15 20:35:25 +02001363 All the functions provided by this class are available in the
1364 **sample-fetches**, **actions** and **filters** contexts. For **filters**,
1365 incoming data (offset and length) are relative to the filter. Some functions
Boyang Li60cfe8b2022-05-10 18:11:00 +00001366 may yield, but only for **actions**. Yield is not possible for
Christopher Faulet5a2c6612021-08-15 20:35:25 +02001367 **sample-fetches**, **converters** and **filters**.
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001368
1369.. js:function:: Channel.append(channel, string)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001370
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001371 This function copies the string **string** at the end of incoming data of the
1372 channel buffer. The function returns the copied length on success or -1 if
1373 data cannot be copied.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001374
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001375 Same that :js:func:`Channel.insert(channel, string, channel:input())`.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001376
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001377 :param class_channel channel: The manipulated Channel.
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02001378 :param string string: The data to copy at the end of incoming data.
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001379 :returns: an integer containing the amount of bytes copied or -1.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001380
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001381.. js:function:: Channel.data(channel [, offset [, length]])
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001382
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001383 This function returns **length** bytes of incoming data from the channel
1384 buffer, starting at the offset **offset**. The data are not removed from the
1385 buffer.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001386
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001387 By default, if no length is provided, all incoming data found, starting at the
1388 given offset, are returned. If **length** is set to -1, the function tries to
Christopher Faulet5a2c6612021-08-15 20:35:25 +02001389 retrieve a maximum of data and, if called by an action, it yields if
1390 necessary. It also waits for more data if the requested length exceeds the
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02001391 available amount of incoming data. Not providing an offset is the same as
Ilya Shipitsinff0f2782021-08-22 22:18:07 +05001392 setting it to 0. A positive offset is relative to the beginning of incoming
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02001393 data of the channel buffer while negative offset is relative to the end.
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001394
1395 If there is no incoming data and the channel can't receive more data, a 'nil'
1396 value is returned.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001397
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001398 :param class_channel channel: The manipulated Channel.
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001399 :param integer offset: *optional* The offset in incoming data to start to get
1400 data. 0 by default. May be negative to be relative to
1401 the end of incoming data.
1402 :param integer length: *optional* The expected length of data to retrieve. All
1403 incoming data by default. May be set to -1 to get a
1404 maximum of data.
1405 :returns: a string containing the data found or nil.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001406
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001407.. js:function:: Channel.forward(channel, length)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001408
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001409 This function forwards **length** bytes of data from the channel buffer. If
Christopher Faulet5a2c6612021-08-15 20:35:25 +02001410 the requested length exceeds the available amount of incoming data, and if
1411 called by an action, the function yields, waiting for more data to forward. It
1412 returns the amount of data forwarded.
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001413
1414 :param class_channel channel: The manipulated Channel.
1415 :param integer int: The amount of data to forward.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001416
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001417.. js:function:: Channel.input(channel)
1418
Christopher Faulet5a2c6612021-08-15 20:35:25 +02001419 This function returns the length of incoming data in the channel buffer. When
1420 called by a filter, this value is relative to the filter.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001421
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001422 :param class_channel channel: The manipulated Channel.
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001423 :returns: an integer containing the amount of available bytes.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001424
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001425.. js:function:: Channel.insert(channel, string [, offset])
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001426
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001427 This function copies the string **string** at the offset **offset** in
1428 incoming data of the channel buffer. The function returns the copied length on
1429 success or -1 if data cannot be copied.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001430
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001431 By default, if no offset is provided, the string is copied in front of
Ilya Shipitsinff0f2782021-08-22 22:18:07 +05001432 incoming data. A positive offset is relative to the beginning of incoming data
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001433 of the channel buffer while negative offset is relative to their end.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001434
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001435 :param class_channel channel: The manipulated Channel.
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02001436 :param string string: The data to copy into incoming data.
1437 :param integer offset: *optional* The offset in incoming data where to copy
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001438 data. 0 by default. May be negative to be relative to
1439 the end of incoming data.
Pieter Baauw4d7f7662015-11-08 16:38:08 +01001440 :returns: an integer containing the amount of bytes copied or -1.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001441
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001442.. js:function:: Channel.is_full(channel)
1443
1444 This function returns true if the channel buffer is full.
1445
Christopher Faulet5a2c6612021-08-15 20:35:25 +02001446 :param class_channel channel: The manipulated Channel.
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001447 :returns: a boolean
1448
1449.. js:function:: Channel.is_resp(channel)
1450
1451 This function returns true if the channel is the response one.
1452
Christopher Faulet5a2c6612021-08-15 20:35:25 +02001453 :param class_channel channel: The manipulated Channel.
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001454 :returns: a boolean
1455
1456.. js:function:: Channel.line(channel [, offset [, length]])
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001457
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001458 This function parses **length** bytes of incoming data of the channel buffer,
1459 starting at offset **offset**, and returns the first line found, including the
Christopher Faulet5a2c6612021-08-15 20:35:25 +02001460 '\\n'. The data are not removed from the buffer. If no line is found, all data
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001461 are returned.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001462
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001463 By default, if no length is provided, all incoming data, starting at the given
1464 offset, are evaluated. If **length** is set to -1, the function tries to
Christopher Faulet5a2c6612021-08-15 20:35:25 +02001465 retrieve a maximum of data and, if called by an action, yields if
1466 necessary. It also waits for more data if the requested length exceeds the
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02001467 available amount of incoming data. Not providing an offset is the same as
Ilya Shipitsinff0f2782021-08-22 22:18:07 +05001468 setting it to 0. A positive offset is relative to the beginning of incoming
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02001469 data of the channel buffer while negative offset is relative to the end.
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001470
1471 If there is no incoming data and the channel can't receive more data, a 'nil'
1472 value is returned.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001473
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001474 :param class_channel channel: The manipulated Channel.
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02001475 :param integer offset: *optional* The offset in incoming data to start to
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001476 parse data. 0 by default. May be negative to be
1477 relative to the end of incoming data.
1478 :param integer length: *optional* The length of data to parse. All incoming
1479 data by default. May be set to -1 to get a maximum of
1480 data.
1481 :returns: a string containing the line found or nil.
1482
1483.. js:function:: Channel.may_recv(channel)
1484
1485 This function returns true if the channel may still receive data.
1486
Christopher Faulet5a2c6612021-08-15 20:35:25 +02001487 :param class_channel channel: The manipulated Channel.
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001488 :returns: a boolean
1489
1490.. js:function:: Channel.output(channel)
1491
Christopher Faulet5a2c6612021-08-15 20:35:25 +02001492 This function returns the length of outgoing data of the channel buffer. When
1493 called by a filter, this value is relative to the filter.
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001494
1495 :param class_channel channel: The manipulated Channel.
1496 :returns: an integer containing the amount of available bytes.
1497
1498.. js:function:: Channel.prepend(channel, string)
1499
1500 This function copies the string **string** in front of incoming data of the
1501 channel buffer. The function returns the copied length on success or -1 if
1502 data cannot be copied.
1503
1504 Same that :js:func:`Channel.insert(channel, string, 0)`.
1505
1506 :param class_channel channel: The manipulated Channel.
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02001507 :param string string: The data to copy in front of incoming data.
Pieter Baauw4d7f7662015-11-08 16:38:08 +01001508 :returns: an integer containing the amount of bytes copied or -1.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001509
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001510.. js:function:: Channel.remove(channel [, offset [, length]])
1511
1512 This function removes **length** bytes of incoming data of the channel buffer,
1513 starting at offset **offset**. This function returns number of bytes removed
1514 on success.
1515
1516 By default, if no length is provided, all incoming data, starting at the given
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02001517 offset, are removed. Not providing an offset is the same as setting it
Ilya Shipitsinff0f2782021-08-22 22:18:07 +05001518 to 0. A positive offset is relative to the beginning of incoming data of the
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02001519 channel buffer while negative offset is relative to the end.
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001520
1521 :param class_channel channel: The manipulated Channel.
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02001522 :param integer offset: *optional* The offset in incoming data where to start
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001523 to remove data. 0 by default. May be negative to
1524 be relative to the end of incoming data.
1525 :param integer length: *optional* The length of data to remove. All incoming
1526 data by default.
1527 :returns: an integer containing the amount of bytes removed.
1528
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001529.. js:function:: Channel.send(channel, string)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001530
Christopher Faulet5a2c6612021-08-15 20:35:25 +02001531 This function requires immediate send of the string **string**. It means the
Ilya Shipitsinff0f2782021-08-22 22:18:07 +05001532 string is copied at the beginning of incoming data of the channel buffer and
Christopher Faulet5a2c6612021-08-15 20:35:25 +02001533 immediately forwarded. Unless if the connection is close, and if called by an
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02001534 action, this function yields to copy and forward all the string.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001535
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001536 :param class_channel channel: The manipulated Channel.
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001537 :param string string: The data to send.
Pieter Baauw4d7f7662015-11-08 16:38:08 +01001538 :returns: an integer containing the amount of bytes copied or -1.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001539
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001540.. js:function:: Channel.set(channel, string [, offset [, length]])
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001541
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02001542 This function replaces **length** bytes of incoming data of the channel buffer,
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001543 starting at offset **offset**, by the string **string**. The function returns
1544 the copied length on success or -1 if data cannot be copied.
1545
1546 By default, if no length is provided, all incoming data, starting at the given
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02001547 offset, are replaced. Not providing an offset is the same as setting it
Ilya Shipitsinff0f2782021-08-22 22:18:07 +05001548 to 0. A positive offset is relative to the beginning of incoming data of the
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02001549 channel buffer while negative offset is relative to the end.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001550
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001551 :param class_channel channel: The manipulated Channel.
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02001552 :param string string: The data to copy into incoming data.
1553 :param integer offset: *optional* The offset in incoming data where to start
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001554 the data replacement. 0 by default. May be negative to
1555 be relative to the end of incoming data.
1556 :param integer length: *optional* The length of data to replace. All incoming
1557 data by default.
1558 :returns: an integer containing the amount of bytes copied or -1.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001559
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001560.. js:function:: Channel.dup(channel)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001561
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001562 **DEPRECATED**
1563
1564 This function returns all incoming data found in the channel buffer. The data
Boyang Li60cfe8b2022-05-10 18:11:00 +00001565 are not removed from the buffer and can be reprocessed later.
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001566
1567 If there is no incoming data and the channel can't receive more data, a 'nil'
1568 value is returned.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001569
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001570 :param class_channel channel: The manipulated Channel.
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001571 :returns: a string containing all data found or nil.
1572
1573 .. warning::
1574 This function is deprecated. :js:func:`Channel.data()` must be used
1575 instead.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001576
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001577.. js:function:: Channel.get(channel)
1578
1579 **DEPRECATED**
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001580
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001581 This function returns all incoming data found in the channel buffer and remove
1582 them from the buffer.
1583
1584 If there is no incoming data and the channel can't receive more data, a 'nil'
1585 value is returned.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001586
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001587 :param class_channel channel: The manipulated Channel.
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001588 :returns: a string containing all the data found or nil.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001589
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001590 .. warning::
1591 This function is deprecated. :js:func:`Channel.data()` must be used to
1592 retrieve data followed by a call to :js:func:`Channel:remove()` to remove
1593 data.
Thierry FOURNIER / OZON.IO65192f32016-11-07 15:28:40 +01001594
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001595 .. code-block:: lua
Thierry FOURNIER / OZON.IO65192f32016-11-07 15:28:40 +01001596
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001597 local data = chn:data()
1598 chn:remove(0, data:len())
1599
1600 ..
1601
1602.. js:function:: Channel.getline(channel)
1603
1604 **DEPRECATED**
1605
1606 This function returns the first line found in incoming data of the channel
Christopher Faulet5a2c6612021-08-15 20:35:25 +02001607 buffer, including the '\\n'. The returned data are removed from the buffer. If
1608 no line is found, and if called by an action, this function yields to wait for
1609 more data, except if the channel can't receive more data. In this case all
1610 data are returned.
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001611
1612 If there is no incoming data and the channel can't receive more data, a 'nil'
1613 value is returned.
1614
1615 :param class_channel channel: The manipulated Channel.
1616 :returns: a string containing the line found or nil.
1617
1618 .. warning::
Boyang Li60cfe8b2022-05-10 18:11:00 +00001619 This function is deprecated. :js:func:`Channel.line()` must be used to
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001620 retrieve a line followed by a call to :js:func:`Channel:remove()` to remove
1621 data.
1622
1623 .. code-block:: lua
1624
1625 local line = chn:line(0, -1)
1626 chn:remove(0, line:len())
1627
1628 ..
1629
1630.. js:function:: Channel.get_in_len(channel)
1631
Boyang Li60cfe8b2022-05-10 18:11:00 +00001632 **DEPRECATED**
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001633
Christopher Faulet5a2c6612021-08-15 20:35:25 +02001634 This function returns the length of the input part of the buffer. When called
1635 by a filter, this value is relative to the filter.
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001636
1637 :param class_channel channel: The manipulated Channel.
1638 :returns: an integer containing the amount of available bytes.
1639
1640 .. warning::
1641 This function is deprecated. :js:func:`Channel.input()` must be used
1642 instead.
1643
1644.. js:function:: Channel.get_out_len(channel)
1645
Boyang Li60cfe8b2022-05-10 18:11:00 +00001646 **DEPRECATED**
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001647
Christopher Faulet5a2c6612021-08-15 20:35:25 +02001648 This function returns the length of the output part of the buffer. When called
1649 by a filter, this value is relative to the filter.
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001650
1651 :param class_channel channel: The manipulated Channel.
1652 :returns: an integer containing the amount of available bytes.
1653
1654 .. warning::
1655 This function is deprecated. :js:func:`Channel.output()` must be used
1656 instead.
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001657
1658.. _http_class:
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001659
1660HTTP class
1661==========
1662
1663.. js:class:: HTTP
1664
1665 This class contain all the HTTP manipulation functions.
1666
Pieter Baauw386a1272015-08-16 15:26:24 +02001667.. js:function:: HTTP.req_get_headers(http)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001668
Patrick Hemmerc6a1d712018-05-01 21:30:41 -04001669 Returns a table containing all the request headers.
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001670
1671 :param class_http http: The related http object.
Patrick Hemmerc6a1d712018-05-01 21:30:41 -04001672 :returns: table of headers.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01001673 :see: :js:func:`HTTP.res_get_headers`
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001674
Patrick Hemmerc6a1d712018-05-01 21:30:41 -04001675 This is the form of the returned table:
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001676
1677.. code-block:: lua
1678
1679 HTTP:req_get_headers()['<header-name>'][<header-index>] = "<header-value>"
1680
1681 local hdr = HTTP:req_get_headers()
1682 hdr["host"][0] = "www.test.com"
1683 hdr["accept"][0] = "audio/basic q=1"
1684 hdr["accept"][1] = "audio/*, q=0.2"
1685 hdr["accept"][2] = "*/*, q=0.1"
1686..
1687
Pieter Baauw386a1272015-08-16 15:26:24 +02001688.. js:function:: HTTP.res_get_headers(http)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001689
Patrick Hemmerc6a1d712018-05-01 21:30:41 -04001690 Returns a table containing all the response headers.
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001691
1692 :param class_http http: The related http object.
Patrick Hemmerc6a1d712018-05-01 21:30:41 -04001693 :returns: table of headers.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01001694 :see: :js:func:`HTTP.req_get_headers`
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001695
Patrick Hemmerc6a1d712018-05-01 21:30:41 -04001696 This is the form of the returned table:
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001697
1698.. code-block:: lua
1699
1700 HTTP:res_get_headers()['<header-name>'][<header-index>] = "<header-value>"
1701
1702 local hdr = HTTP:req_get_headers()
1703 hdr["host"][0] = "www.test.com"
1704 hdr["accept"][0] = "audio/basic q=1"
1705 hdr["accept"][1] = "audio/*, q=0.2"
1706 hdr["accept"][2] = "*.*, q=0.1"
1707..
1708
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001709.. js:function:: HTTP.req_add_header(http, name, value)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001710
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02001711 Appends a HTTP header field in the request whose name is
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001712 specified in "name" and whose value is defined in "value".
1713
1714 :param class_http http: The related http object.
1715 :param string name: The header name.
1716 :param string value: The header value.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01001717 :see: :js:func:`HTTP.res_add_header`
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001718
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001719.. js:function:: HTTP.res_add_header(http, name, value)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001720
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02001721 Appends a HTTP header field in the response whose name is
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001722 specified in "name" and whose value is defined in "value".
1723
1724 :param class_http http: The related http object.
1725 :param string name: The header name.
1726 :param string value: The header value.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01001727 :see: :js:func:`HTTP.req_add_header`
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001728
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001729.. js:function:: HTTP.req_del_header(http, name)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001730
1731 Removes all HTTP header fields in the request whose name is
1732 specified in "name".
1733
1734 :param class_http http: The related http object.
1735 :param string name: The header name.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01001736 :see: :js:func:`HTTP.res_del_header`
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001737
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001738.. js:function:: HTTP.res_del_header(http, name)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001739
1740 Removes all HTTP header fields in the response whose name is
1741 specified in "name".
1742
1743 :param class_http http: The related http object.
1744 :param string name: The header name.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01001745 :see: :js:func:`HTTP.req_del_header`
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001746
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001747.. js:function:: HTTP.req_set_header(http, name, value)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001748
Bertrand Jacquin874a35c2018-09-10 21:26:07 +01001749 This variable replace all occurrence of all header "name", by only
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001750 one containing the "value".
1751
1752 :param class_http http: The related http object.
1753 :param string name: The header name.
1754 :param string value: The header value.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01001755 :see: :js:func:`HTTP.res_set_header`
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001756
Bertrand Jacquin874a35c2018-09-10 21:26:07 +01001757 This function does the same work as the following code:
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001758
1759.. code-block:: lua
1760
1761 function fcn(txn)
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001762 TXN.http:req_del_header("header")
1763 TXN.http:req_add_header("header", "value")
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001764 end
1765..
1766
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001767.. js:function:: HTTP.res_set_header(http, name, value)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001768
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02001769 This function replaces all occurrence of all header "name", by only
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001770 one containing the "value".
1771
1772 :param class_http http: The related http object.
1773 :param string name: The header name.
1774 :param string value: The header value.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01001775 :see: :js:func:`HTTP.req_rep_header()`
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001776
Pieter Baauw386a1272015-08-16 15:26:24 +02001777.. js:function:: HTTP.req_rep_header(http, name, regex, replace)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001778
1779 Matches the regular expression in all occurrences of header field "name"
1780 according to "regex", and replaces them with the "replace" argument. The
1781 replacement value can contain back references like \1, \2, ... This
1782 function works with the request.
1783
1784 :param class_http http: The related http object.
1785 :param string name: The header name.
1786 :param string regex: The match regular expression.
1787 :param string replace: The replacement value.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01001788 :see: :js:func:`HTTP.res_rep_header()`
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001789
Pieter Baauw386a1272015-08-16 15:26:24 +02001790.. js:function:: HTTP.res_rep_header(http, name, regex, string)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001791
1792 Matches the regular expression in all occurrences of header field "name"
1793 according to "regex", and replaces them with the "replace" argument. The
1794 replacement value can contain back references like \1, \2, ... This
1795 function works with the request.
1796
1797 :param class_http http: The related http object.
1798 :param string name: The header name.
1799 :param string regex: The match regular expression.
1800 :param string replace: The replacement value.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01001801 :see: :js:func:`HTTP.req_rep_header()`
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001802
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001803.. js:function:: HTTP.req_set_method(http, method)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001804
1805 Rewrites the request method with the parameter "method".
1806
1807 :param class_http http: The related http object.
1808 :param string method: The new method.
1809
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001810.. js:function:: HTTP.req_set_path(http, path)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001811
1812 Rewrites the request path with the "path" parameter.
1813
1814 :param class_http http: The related http object.
1815 :param string path: The new path.
1816
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001817.. js:function:: HTTP.req_set_query(http, query)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001818
1819 Rewrites the request's query string which appears after the first question
1820 mark ("?") with the parameter "query".
1821
1822 :param class_http http: The related http object.
1823 :param string query: The new query.
1824
Thierry FOURNIER0d79cf62015-08-26 14:20:58 +02001825.. js:function:: HTTP.req_set_uri(http, uri)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001826
1827 Rewrites the request URI with the parameter "uri".
1828
1829 :param class_http http: The related http object.
1830 :param string uri: The new uri.
1831
Robin H. Johnson52f5db22017-01-01 13:10:52 -08001832.. js:function:: HTTP.res_set_status(http, status [, reason])
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +02001833
Robin H. Johnson52f5db22017-01-01 13:10:52 -08001834 Rewrites the response status code with the parameter "code".
1835
1836 If no custom reason is provided, it will be generated from the status.
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +02001837
1838 :param class_http http: The related http object.
1839 :param integer status: The new response status code.
Robin H. Johnson52f5db22017-01-01 13:10:52 -08001840 :param string reason: The new response reason (optional).
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +02001841
William Lallemand00a15022021-11-19 16:02:44 +01001842.. _httpclient_class:
1843
1844HTTPClient class
1845================
1846
1847.. js:class:: HTTPClient
1848
1849 The httpclient class allows issue of outbound HTTP requests through a simple
1850 API without the knowledge of HAProxy internals.
1851
1852.. js:function:: HTTPClient.get(httpclient, request)
1853.. js:function:: HTTPClient.head(httpclient, request)
1854.. js:function:: HTTPClient.put(httpclient, request)
1855.. js:function:: HTTPClient.post(httpclient, request)
1856.. js:function:: HTTPClient.delete(httpclient, request)
1857
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02001858 Send a HTTP request and wait for a response. GET, HEAD PUT, POST and DELETE methods can be used.
1859 The HTTPClient will send asynchronously the data and is able to send and receive more than HAProxy bufsize.
William Lallemand00a15022021-11-19 16:02:44 +01001860
William Lallemanda9256192022-10-21 11:48:24 +02001861 The HTTPClient interface is not able to decompress responses, it is not
1862 recommended to send an Accept-Encoding in the request so the response is
1863 received uncompressed.
William Lallemand00a15022021-11-19 16:02:44 +01001864
1865 :param class httpclient: Is the manipulated HTTPClient.
1866 :param table request: Is a table containing the parameters of the request that will be send.
1867 :param string request.url: Is a mandatory parameter for the request that contains the URL.
1868 :param string request.body: Is an optional parameter for the request that contains the body to send.
1869 :param table request.headers: Is an optional parameter for the request that contains the headers to send.
William Lallemand18340302022-02-23 15:57:45 +01001870 :param string request.dst: Is an optional parameter for the destination in haproxy address format.
William Lallemandb4a4ef62022-02-23 14:18:16 +01001871 :param integer request.timeout: Optional timeout parameter, set a "timeout server" on the connections.
William Lallemand00a15022021-11-19 16:02:44 +01001872 :returns: Lua table containing the response
1873
1874
1875.. code-block:: lua
1876
1877 local httpclient = core.httpclient()
William Lallemand4f4f2b72022-02-17 20:00:23 +01001878 local response = httpclient:post{url="http://127.0.0.1", body=body, dst="unix@/var/run/http.sock"}
William Lallemand00a15022021-11-19 16:02:44 +01001879
1880..
1881
1882.. code-block:: lua
1883
1884 response = {
1885 status = 400,
1886 reason = "Bad request",
1887 headers = {
1888 ["content-type"] = { "text/html" },
1889 ["cache-control"] = { "no-cache", "no-store" },
1890 },
William Lallemand4f4f2b72022-02-17 20:00:23 +01001891 body = "<html><body><h1>invalid request<h1></body></html>",
William Lallemand00a15022021-11-19 16:02:44 +01001892 }
1893..
1894
1895
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001896.. _txn_class:
1897
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001898TXN class
1899=========
1900
1901.. js:class:: TXN
1902
1903 The txn class contain all the functions relative to the http or tcp
1904 transaction (Note than a tcp stream is the same than a tcp transaction, but
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02001905 a HTTP transaction is not the same than a tcp stream).
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001906
1907 The usage of this class permits to retrieve data from the requests, alter it
1908 and forward it.
1909
1910 All the functions provided by this class are available in the context
Christopher Faulet5a2c6612021-08-15 20:35:25 +02001911 **sample-fetches**, **actions** and **filters**.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001912
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001913.. js:attribute:: TXN.c
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001914
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001915 :returns: An :ref:`converters_class`.
1916
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001917 This attribute contains a Converters class object.
1918
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001919.. js:attribute:: TXN.sc
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001920
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001921 :returns: An :ref:`converters_class`.
1922
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001923 This attribute contains a Converters class object. The functions of
1924 this object returns always a string.
1925
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001926.. js:attribute:: TXN.f
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001927
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001928 :returns: An :ref:`fetches_class`.
1929
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001930 This attribute contains a Fetches class object.
1931
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001932.. js:attribute:: TXN.sf
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001933
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001934 :returns: An :ref:`fetches_class`.
1935
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001936 This attribute contains a Fetches class object. The functions of
1937 this object returns always a string.
1938
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001939.. js:attribute:: TXN.req
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001940
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001941 :returns: An :ref:`channel_class`.
1942
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001943 This attribute contains a channel class object for the request buffer.
1944
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001945.. js:attribute:: TXN.res
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001946
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001947 :returns: An :ref:`channel_class`.
1948
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001949 This attribute contains a channel class object for the response buffer.
1950
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001951.. js:attribute:: TXN.http
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001952
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001953 :returns: An :ref:`http_class`.
1954
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02001955 This attribute contains a HTTP class object. It is available only if the
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001956 proxy has the "mode http" enabled.
1957
Christopher Faulet5a2c6612021-08-15 20:35:25 +02001958.. js:attribute:: TXN.http_req
1959
1960 :returns: An :ref:`httpmessage_class`.
1961
1962 This attribute contains the request HTTPMessage class object. It is available
1963 only if the proxy has the "mode http" enabled and only in the **filters**
1964 context.
1965
1966.. js:attribute:: TXN.http_res
1967
1968 :returns: An :ref:`httpmessage_class`.
1969
1970 This attribute contains the response HTTPMessage class object. It is available
1971 only if the proxy has the "mode http" enabled and only in the **filters**
1972 context.
1973
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01001974.. js:function:: TXN.log(TXN, loglevel, msg)
1975
1976 This function sends a log. The log is sent, according with the HAProxy
1977 configuration file, on the default syslog server if it is configured and on
1978 the stderr if it is allowed.
1979
1980 :param class_txn txn: The class txn object containing the data.
Bertrand Jacquin874a35c2018-09-10 21:26:07 +01001981 :param integer loglevel: Is the log level associated with the message. It is a
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01001982 number between 0 and 7.
1983 :param string msg: The log content.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01001984 :see: :js:attr:`core.emerg`, :js:attr:`core.alert`, :js:attr:`core.crit`,
1985 :js:attr:`core.err`, :js:attr:`core.warning`, :js:attr:`core.notice`,
1986 :js:attr:`core.info`, :js:attr:`core.debug` (log level definitions)
1987 :see: :js:func:`TXN.deflog`
1988 :see: :js:func:`TXN.Debug`
1989 :see: :js:func:`TXN.Info`
1990 :see: :js:func:`TXN.Warning`
1991 :see: :js:func:`TXN.Alert`
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01001992
1993.. js:function:: TXN.deflog(TXN, msg)
1994
Bertrand Jacquin874a35c2018-09-10 21:26:07 +01001995 Sends a log line with the default loglevel for the proxy associated with the
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01001996 transaction.
1997
1998 :param class_txn txn: The class txn object containing the data.
1999 :param string msg: The log content.
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02002000 :see: :js:func:`TXN.log`
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01002001
2002.. js:function:: TXN.Debug(txn, msg)
2003
2004 :param class_txn txn: The class txn object containing the data.
2005 :param string msg: The log content.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01002006 :see: :js:func:`TXN.log`
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01002007
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02002008 Does the same job as:
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01002009
2010.. code-block:: lua
2011
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002012 function Debug(txn, msg)
2013 TXN.log(txn, core.debug, msg)
2014 end
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01002015..
2016
2017.. js:function:: TXN.Info(txn, msg)
2018
2019 :param class_txn txn: The class txn object containing the data.
2020 :param string msg: The log content.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01002021 :see: :js:func:`TXN.log`
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01002022
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02002023 Does the same job as:
2024
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01002025.. code-block:: lua
2026
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02002027 function Info(txn, msg)
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002028 TXN.log(txn, core.info, msg)
2029 end
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01002030..
2031
2032.. js:function:: TXN.Warning(txn, msg)
2033
2034 :param class_txn txn: The class txn object containing the data.
2035 :param string msg: The log content.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01002036 :see: :js:func:`TXN.log`
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01002037
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02002038 Does the same job as:
2039
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01002040.. code-block:: lua
2041
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02002042 function Warning(txn, msg)
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002043 TXN.log(txn, core.warning, msg)
2044 end
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01002045..
2046
2047.. js:function:: TXN.Alert(txn, msg)
2048
2049 :param class_txn txn: The class txn object containing the data.
2050 :param string msg: The log content.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01002051 :see: :js:func:`TXN.log`
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01002052
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02002053 Does the same job as:
2054
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01002055.. code-block:: lua
2056
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02002057 function Alert(txn, msg)
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002058 TXN.log(txn, core.alert, msg)
2059 end
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01002060..
2061
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002062.. js:function:: TXN.get_priv(txn)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002063
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002064 Return Lua data stored in the current transaction (with the `TXN.set_priv()`)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002065 function. If no data are stored, it returns a nil value.
2066
2067 :param class_txn txn: The class txn object containing the data.
Bertrand Jacquin874a35c2018-09-10 21:26:07 +01002068 :returns: the opaque data previously stored, or nil if nothing is
2069 available.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002070
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002071.. js:function:: TXN.set_priv(txn, data)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002072
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02002073 Store any data in the current HAProxy transaction. This action replaces the
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002074 old stored data.
2075
2076 :param class_txn txn: The class txn object containing the data.
2077 :param opaque data: The data which is stored in the transaction.
2078
Tim Duesterhus4e172c92020-05-19 13:49:42 +02002079.. js:function:: TXN.set_var(TXN, var, value[, ifexist])
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +02002080
David Carlier61fdf8b2015-10-02 11:59:38 +01002081 Converts a Lua type in a HAProxy type and store it in a variable <var>.
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +02002082
2083 :param class_txn txn: The class txn object containing the data.
2084 :param string var: The variable name according with the HAProxy variable syntax.
Thierry FOURNIER / OZON.IOb210bcc2016-12-12 16:24:16 +01002085 :param type value: The value associated to the variable. The type can be string or
2086 integer.
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02002087 :param boolean ifexist: If this parameter is set to true the variable
Tim Duesterhus4e172c92020-05-19 13:49:42 +02002088 will only be set if it was defined elsewhere (i.e. used
Willy Tarreau7978c5c2021-09-07 14:24:07 +02002089 within the configuration). For global variables (using the
2090 "proc" scope), they will only be updated and never created.
2091 It is highly recommended to always set this to true.
Christopher Faulet85d79c92016-11-09 16:54:56 +01002092
2093.. js:function:: TXN.unset_var(TXN, var)
2094
2095 Unset the variable <var>.
2096
2097 :param class_txn txn: The class txn object containing the data.
2098 :param string var: The variable name according with the HAProxy variable syntax.
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +02002099
2100.. js:function:: TXN.get_var(TXN, var)
2101
2102 Returns data stored in the variable <var> converter in Lua type.
2103
2104 :param class_txn txn: The class txn object containing the data.
2105 :param string var: The variable name according with the HAProxy variable syntax.
2106
Christopher Faulet700d9e82020-01-31 12:21:52 +01002107.. js:function:: TXN.reply([reply])
2108
2109 Return a new reply object
2110
2111 :param table reply: A table containing info to initialize the reply fields.
2112 :returns: A :ref:`reply_class` object.
2113
2114 The table used to initialized the reply object may contain following entries :
2115
2116 * status : The reply status code. the code 200 is used by default.
2117 * reason : The reply reason. The reason corresponding to the status code is
2118 used by default.
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02002119 * headers : A list of headers, indexed by header name. Empty by default. For
Christopher Faulet700d9e82020-01-31 12:21:52 +01002120 a given name, multiple values are possible, stored in an ordered list.
2121 * body : The reply body, empty by default.
2122
2123.. code-block:: lua
2124
2125 local reply = txn:reply{
2126 status = 400,
2127 reason = "Bad request",
2128 headers = {
2129 ["content-type"] = { "text/html" },
2130 ["cache-control"] = {"no-cache", "no-store" }
2131 },
2132 body = "<html><body><h1>invalid request<h1></body></html>"
2133 }
2134..
2135 :see: :js:class:`Reply`
2136
2137.. js:function:: TXN.done(txn[, reply])
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002138
Willy Tarreaubc183a62015-08-28 10:39:11 +02002139 This function terminates processing of the transaction and the associated
Christopher Faulet700d9e82020-01-31 12:21:52 +01002140 session and optionally reply to the client for HTTP sessions.
2141
2142 :param class_txn txn: The class txn object containing the data.
2143 :param class_reply reply: The class reply object to return to the client.
2144
2145 This functions can be used when a critical error is detected or to terminate
Willy Tarreaubc183a62015-08-28 10:39:11 +02002146 processing after some data have been returned to the client (eg: a redirect).
Christopher Faulet700d9e82020-01-31 12:21:52 +01002147 To do so, a reply may be provided. This object is optional and may contain a
2148 status code, a reason, a header list and a body. All these fields are
Christopher Faulet7855b192021-11-09 18:39:51 +01002149 optional. When not provided, the default values are used. By default, with an
2150 empty reply object, an empty HTTP 200 response is returned to the client. If
2151 no reply object is provided, the transaction is terminated without any
2152 reply. If a reply object is provided, it must not exceed the buffer size once
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02002153 converted into the internal HTTP representation. Because for now there is no
Christopher Faulet7855b192021-11-09 18:39:51 +01002154 easy way to be sure it fits, it is probably better to keep it reasonably
2155 small.
Christopher Faulet700d9e82020-01-31 12:21:52 +01002156
2157 The reply object may be fully created in lua or the class Reply may be used to
2158 create it.
2159
2160.. code-block:: lua
2161
2162 local reply = txn:reply()
2163 reply:set_status(400, "Bad request")
2164 reply:add_header("content-type", "text/html")
2165 reply:add_header("cache-control", "no-cache")
2166 reply:add_header("cache-control", "no-store")
2167 reply:set_body("<html><body><h1>invalid request<h1></body></html>")
2168 txn:done(reply)
2169..
2170
2171.. code-block:: lua
2172
2173 txn:done{
2174 status = 400,
2175 reason = "Bad request",
2176 headers = {
2177 ["content-type"] = { "text/html" },
2178 ["cache-control"] = { "no-cache", "no-store" },
2179 },
2180 body = "<html><body><h1>invalid request<h1></body></html>"
2181 }
2182..
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002183
Christopher Faulet1e9b1b62021-08-11 10:14:30 +02002184 .. warning::
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02002185 It does not make sense to call this function from sample-fetches. In this case
2186 the behavior is the same than core.done(): it finishes the Lua
Christopher Faulet1e9b1b62021-08-11 10:14:30 +02002187 execution. The transaction is really aborted only from an action registered
2188 function.
Thierry FOURNIERab00df62016-07-14 11:42:37 +02002189
Christopher Faulet700d9e82020-01-31 12:21:52 +01002190 :see: :js:func:`TXN.reply`, :js:class:`Reply`
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002191
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002192.. js:function:: TXN.set_loglevel(txn, loglevel)
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01002193
2194 Is used to change the log level of the current request. The "loglevel" must
2195 be an integer between 0 and 7.
2196
2197 :param class_txn txn: The class txn object containing the data.
2198 :param integer loglevel: The required log level. This variable can be one of
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01002199 :see: :js:attr:`core.emerg`, :js:attr:`core.alert`, :js:attr:`core.crit`,
2200 :js:attr:`core.err`, :js:attr:`core.warning`, :js:attr:`core.notice`,
2201 :js:attr:`core.info`, :js:attr:`core.debug` (log level definitions)
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01002202
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002203.. js:function:: TXN.set_tos(txn, tos)
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01002204
2205 Is used to set the TOS or DSCP field value of packets sent to the client to
2206 the value passed in "tos" on platforms which support this.
2207
2208 :param class_txn txn: The class txn object containing the data.
2209 :param integer tos: The new TOS os DSCP.
2210
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002211.. js:function:: TXN.set_mark(txn, mark)
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01002212
2213 Is used to set the Netfilter MARK on all packets sent to the client to the
2214 value passed in "mark" on platforms which support it.
2215
2216 :param class_txn txn: The class txn object containing the data.
2217 :param integer mark: The mark value.
2218
Patrick Hemmer268a7072018-05-11 12:52:31 -04002219.. js:function:: TXN.set_priority_class(txn, prio)
2220
2221 This function adjusts the priority class of the transaction. The value should
2222 be within the range -2047..2047. Values outside this range will be
2223 truncated.
2224
2225 See the HAProxy configuration.txt file keyword "http-request" action
2226 "set-priority-class" for details.
2227
2228.. js:function:: TXN.set_priority_offset(txn, prio)
2229
2230 This function adjusts the priority offset of the transaction. The value
2231 should be within the range -524287..524287. Values outside this range will be
2232 truncated.
2233
2234 See the HAProxy configuration.txt file keyword "http-request" action
2235 "set-priority-offset" for details.
2236
Christopher Faulet700d9e82020-01-31 12:21:52 +01002237.. _reply_class:
2238
2239Reply class
2240============
2241
2242.. js:class:: Reply
2243
2244 **context**: action
2245
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02002246 This class represents a HTTP response message. It provides some methods to
Christopher Faulet7855b192021-11-09 18:39:51 +01002247 enrich it. Once converted into the internal HTTP representation, the response
2248 message must not exceed the buffer size. Because for now there is no
2249 easy way to be sure it fits, it is probably better to keep it reasonably
2250 small.
2251
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02002252 See tune.bufsize in the configuration manual for details.
Christopher Faulet700d9e82020-01-31 12:21:52 +01002253
2254.. code-block:: lua
2255
2256 local reply = txn:reply({status = 400}) -- default HTTP 400 reason-phase used
2257 reply:add_header("content-type", "text/html")
2258 reply:add_header("cache-control", "no-cache")
2259 reply:add_header("cache-control", "no-store")
2260 reply:set_body("<html><body><h1>invalid request<h1></body></html>")
2261..
2262
2263 :see: :js:func:`TXN.reply`
2264
2265.. js:attribute:: Reply.status
2266
2267 The reply status code. By default, the status code is set to 200.
2268
2269 :returns: integer
2270
2271.. js:attribute:: Reply.reason
2272
2273 The reason string describing the status code.
2274
2275 :returns: string
2276
2277.. js:attribute:: Reply.headers
2278
2279 A table indexing all reply headers by name. To each name is associated an
2280 ordered list of values.
2281
2282 :returns: Lua table
2283
2284.. code-block:: lua
2285
2286 {
2287 ["content-type"] = { "text/html" },
2288 ["cache-control"] = {"no-cache", "no-store" },
2289 x_header_name = { "value1", "value2", ... }
2290 ...
2291 }
2292..
2293
2294.. js:attribute:: Reply.body
2295
2296 The reply payload.
2297
2298 :returns: string
2299
2300.. js:function:: Reply.set_status(REPLY, status[, reason])
2301
2302 Set the reply status code and optionally the reason-phrase. If the reason is
2303 not provided, the default reason corresponding to the status code is used.
2304
2305 :param class_reply reply: The related Reply object.
2306 :param integer status: The reply status code.
2307 :param string reason: The reply status reason (optional).
2308
2309.. js:function:: Reply.add_header(REPLY, name, value)
2310
2311 Add a header to the reply object. If the header does not already exist, a new
2312 entry is created with its name as index and a one-element list containing its
2313 value as value. Otherwise, the header value is appended to the ordered list of
2314 values associated to the header name.
2315
2316 :param class_reply reply: The related Reply object.
2317 :param string name: The header field name.
2318 :param string value: The header field value.
2319
2320.. js:function:: Reply.del_header(REPLY, name)
2321
2322 Remove all occurrences of a header name from the reply object.
2323
2324 :param class_reply reply: The related Reply object.
2325 :param string name: The header field name.
2326
2327.. js:function:: Reply.set_body(REPLY, body)
2328
2329 Set the reply payload.
2330
2331 :param class_reply reply: The related Reply object.
2332 :param string body: The reply payload.
2333
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002334.. _socket_class:
2335
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002336Socket class
2337============
2338
2339.. js:class:: Socket
2340
2341 This class must be compatible with the Lua Socket class. Only the 'client'
2342 functions are available. See the Lua Socket documentation:
2343
2344 `http://w3.impa.br/~diego/software/luasocket/tcp.html
2345 <http://w3.impa.br/~diego/software/luasocket/tcp.html>`_
2346
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002347.. js:function:: Socket.close(socket)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002348
2349 Closes a TCP object. The internal socket used by the object is closed and the
2350 local address to which the object was bound is made available to other
2351 applications. No further operations (except for further calls to the close
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002352 method) are allowed on a closed Socket.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002353
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002354 :param class_socket socket: Is the manipulated Socket.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002355
2356 Note: It is important to close all used sockets once they are not needed,
2357 since, in many systems, each socket uses a file descriptor, which are limited
2358 system resources. Garbage-collected objects are automatically closed before
2359 destruction, though.
2360
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002361.. js:function:: Socket.connect(socket, address[, port])
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002362
2363 Attempts to connect a socket object to a remote host.
2364
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002365
2366 In case of error, the method returns nil followed by a string describing the
2367 error. In case of success, the method returns 1.
2368
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002369 :param class_socket socket: Is the manipulated Socket.
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002370 :param string address: can be an IP address or a host name. See below for more
2371 information.
2372 :param integer port: must be an integer number in the range [1..64K].
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002373 :returns: 1 or nil.
2374
Bertrand Jacquin874a35c2018-09-10 21:26:07 +01002375 An address field extension permits to use the connect() function to connect to
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002376 other stream than TCP. The syntax containing a simpleipv4 or ipv6 address is
2377 the basically expected format. This format requires the port.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002378
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002379 Other format accepted are a socket path like "/socket/path", it permits to
Bertrand Jacquin874a35c2018-09-10 21:26:07 +01002380 connect to a socket. Abstract namespaces are supported with the prefix
Joseph Herlant02cedc42018-11-13 19:45:17 -08002381 "abns@", and finally a file descriptor can be passed with the prefix "fd@".
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002382 The prefix "ipv4@", "ipv6@" and "unix@" are also supported. The port can be
Bertrand Jacquin874a35c2018-09-10 21:26:07 +01002383 passed int the string. The syntax "127.0.0.1:1234" is valid. In this case, the
Tim Duesterhus6edab862018-01-06 19:04:45 +01002384 parameter *port* must not be set.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002385
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002386.. js:function:: Socket.connect_ssl(socket, address, port)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002387
2388 Same behavior than the function socket:connect, but uses SSL.
2389
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002390 :param class_socket socket: Is the manipulated Socket.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002391 :returns: 1 or nil.
2392
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002393.. js:function:: Socket.getpeername(socket)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002394
2395 Returns information about the remote side of a connected client object.
2396
2397 Returns a string with the IP address of the peer, followed by the port number
2398 that peer is using for the connection. In case of error, the method returns
2399 nil.
2400
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002401 :param class_socket socket: Is the manipulated Socket.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002402 :returns: a string containing the server information.
2403
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002404.. js:function:: Socket.getsockname(socket)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002405
2406 Returns the local address information associated to the object.
2407
2408 The method returns a string with local IP address and a number with the port.
2409 In case of error, the method returns nil.
2410
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002411 :param class_socket socket: Is the manipulated Socket.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002412 :returns: a string containing the client information.
2413
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002414.. js:function:: Socket.receive(socket, [pattern [, prefix]])
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002415
2416 Reads data from a client object, according to the specified read pattern.
2417 Patterns follow the Lua file I/O format, and the difference in performance
2418 between all patterns is negligible.
2419
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002420 :param class_socket socket: Is the manipulated Socket.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002421 :param string|integer pattern: Describe what is required (see below).
2422 :param string prefix: A string which will be prefix the returned data.
2423 :returns: a string containing the required data or nil.
2424
2425 Pattern can be any of the following:
2426
2427 * **`*a`**: reads from the socket until the connection is closed. No
2428 end-of-line translation is performed;
2429
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002430 * **`*l`**: reads a line of text from the Socket. The line is terminated by a
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002431 LF character (ASCII 10), optionally preceded by a CR character
2432 (ASCII 13). The CR and LF characters are not included in the
2433 returned line. In fact, all CR characters are ignored by the
2434 pattern. This is the default pattern.
2435
2436 * **number**: causes the method to read a specified number of bytes from the
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002437 Socket. Prefix is an optional string to be concatenated to the
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002438 beginning of any received data before return.
2439
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002440 * **empty**: If the pattern is left empty, the default option is `*l`.
2441
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002442 If successful, the method returns the received pattern. In case of error, the
2443 method returns nil followed by an error message which can be the string
2444 'closed' in case the connection was closed before the transmission was
2445 completed or the string 'timeout' in case there was a timeout during the
2446 operation. Also, after the error message, the function returns the partial
2447 result of the transmission.
2448
2449 Important note: This function was changed severely. It used to support
2450 multiple patterns (but I have never seen this feature used) and now it
2451 doesn't anymore. Partial results used to be returned in the same way as
2452 successful results. This last feature violated the idea that all functions
2453 should return nil on error. Thus it was changed too.
2454
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002455.. js:function:: Socket.send(socket, data [, start [, end ]])
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002456
2457 Sends data through client object.
2458
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002459 :param class_socket socket: Is the manipulated Socket.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002460 :param string data: The data that will be sent.
2461 :param integer start: The start position in the buffer of the data which will
2462 be sent.
2463 :param integer end: The end position in the buffer of the data which will
2464 be sent.
2465 :returns: see below.
2466
2467 Data is the string to be sent. The optional arguments i and j work exactly
2468 like the standard string.sub Lua function to allow the selection of a
2469 substring to be sent.
2470
2471 If successful, the method returns the index of the last byte within [start,
2472 end] that has been sent. Notice that, if start is 1 or absent, this is
2473 effectively the total number of bytes sent. In case of error, the method
2474 returns nil, followed by an error message, followed by the index of the last
2475 byte within [start, end] that has been sent. You might want to try again from
2476 the byte following that. The error message can be 'closed' in case the
2477 connection was closed before the transmission was completed or the string
2478 'timeout' in case there was a timeout during the operation.
2479
2480 Note: Output is not buffered. For small strings, it is always better to
2481 concatenate them in Lua (with the '..' operator) and send the result in one
2482 call instead of calling the method several times.
2483
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002484.. js:function:: Socket.setoption(socket, option [, value])
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002485
2486 Just implemented for compatibility, this cal does nothing.
2487
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002488.. js:function:: Socket.settimeout(socket, value [, mode])
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002489
2490 Changes the timeout values for the object. All I/O operations are blocking.
2491 That is, any call to the methods send, receive, and accept will block
2492 indefinitely, until the operation completes. The settimeout method defines a
2493 limit on the amount of time the I/O methods can block. When a timeout time
2494 has elapsed, the affected methods give up and fail with an error code.
2495
2496 The amount of time to wait is specified as the value parameter, in seconds.
2497
Mark Lakes56cc1252018-03-27 09:48:06 +02002498 The timeout modes are not implemented, the only settable timeout is the
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002499 inactivity time waiting for complete the internal buffer send or waiting for
2500 receive data.
2501
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002502 :param class_socket socket: Is the manipulated Socket.
Bertrand Jacquin874a35c2018-09-10 21:26:07 +01002503 :param float value: The timeout value. Use floating point to specify
Mark Lakes56cc1252018-03-27 09:48:06 +02002504 milliseconds.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002505
Thierry FOURNIER31904272017-10-25 12:59:51 +02002506.. _regex_class:
2507
2508Regex class
2509===========
2510
2511.. js:class:: Regex
2512
2513 This class allows the usage of HAProxy regexes because classic lua doesn't
2514 provides regexes. This class inherits the HAProxy compilation options, so the
2515 regexes can be libc regex, pcre regex or pcre JIT regex.
2516
2517 The expression matching number is limited to 20 per regex. The only available
2518 option is case sensitive.
2519
2520 Because regexes compilation is a heavy process, it is better to define all
2521 your regexes in the **body context** and use it during the runtime.
2522
2523.. code-block:: lua
2524
2525 -- Create the regex
2526 st, regex = Regex.new("needle (..) (...)", true);
2527
2528 -- Check compilation errors
2529 if st == false then
2530 print "error: " .. regex
2531 end
2532
2533 -- Match the regexes
2534 print(regex:exec("Looking for a needle in the haystack")) -- true
2535 print(regex:exec("Lokking for a cat in the haystack")) -- false
2536
2537 -- Extract words
2538 st, list = regex:match("Looking for a needle in the haystack")
2539 print(st) -- true
2540 print(list[1]) -- needle in the
2541 print(list[2]) -- in
2542 print(list[3]) -- the
2543
2544.. js:function:: Regex.new(regex, case_sensitive)
2545
2546 Create and compile a regex.
2547
2548 :param string regex: The regular expression according with the libc or pcre
2549 standard
2550 :param boolean case_sensitive: Match is case sensitive or not.
2551 :returns: boolean status and :ref:`regex_class` or string containing fail reason.
2552
2553.. js:function:: Regex.exec(regex, str)
2554
2555 Execute the regex.
2556
2557 :param class_regex regex: A :ref:`regex_class` object.
2558 :param string str: The input string will be compared with the compiled regex.
2559 :returns: a boolean status according with the match result.
2560
2561.. js:function:: Regex.match(regex, str)
2562
2563 Execute the regex and return matched expressions.
2564
2565 :param class_map map: A :ref:`regex_class` object.
2566 :param string str: The input string will be compared with the compiled regex.
2567 :returns: a boolean status according with the match result, and
2568 a table containing all the string matched in order of declaration.
2569
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002570.. _map_class:
2571
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002572Map class
2573=========
2574
2575.. js:class:: Map
2576
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02002577 This class permits to do some lookups in HAProxy maps. The declared maps can
Bertrand Jacquin874a35c2018-09-10 21:26:07 +01002578 be modified during the runtime through the HAProxy management socket.
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002579
2580.. code-block:: lua
2581
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002582 default = "usa"
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002583
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002584 -- Create and load map
Thierry FOURNIER4dc71972017-01-28 08:33:08 +01002585 geo = Map.new("geo.map", Map._ip);
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002586
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002587 -- Create new fetch that returns the user country
2588 core.register_fetches("country", function(txn)
2589 local src;
2590 local loc;
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002591
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002592 src = txn.f:fhdr("x-forwarded-for");
2593 if (src == nil) then
2594 src = txn.f:src()
2595 if (src == nil) then
2596 return default;
2597 end
2598 end
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002599
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002600 -- Perform lookup
2601 loc = geo:lookup(src);
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002602
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002603 if (loc == nil) then
2604 return default;
2605 end
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002606
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002607 return loc;
2608 end);
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002609
Thierry FOURNIER4dc71972017-01-28 08:33:08 +01002610.. js:attribute:: Map._int
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002611
2612 See the HAProxy configuration.txt file, chapter "Using ACLs and fetching
Ilya Shipitsin2075ca82020-03-06 23:22:22 +05002613 samples" and subchapter "ACL basics" to understand this pattern matching
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002614 method.
2615
Thierry FOURNIER4dc71972017-01-28 08:33:08 +01002616 Note that :js:attr:`Map.int` is also available for compatibility.
2617
2618.. js:attribute:: Map._ip
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002619
2620 See the HAProxy configuration.txt file, chapter "Using ACLs and fetching
Ilya Shipitsin2075ca82020-03-06 23:22:22 +05002621 samples" and subchapter "ACL basics" to understand this pattern matching
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002622 method.
2623
Thierry FOURNIER4dc71972017-01-28 08:33:08 +01002624 Note that :js:attr:`Map.ip` is also available for compatibility.
2625
2626.. js:attribute:: Map._str
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002627
2628 See the HAProxy configuration.txt file, chapter "Using ACLs and fetching
Ilya Shipitsin2075ca82020-03-06 23:22:22 +05002629 samples" and subchapter "ACL basics" to understand this pattern matching
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002630 method.
2631
Thierry FOURNIER4dc71972017-01-28 08:33:08 +01002632 Note that :js:attr:`Map.str` is also available for compatibility.
2633
2634.. js:attribute:: Map._beg
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002635
2636 See the HAProxy configuration.txt file, chapter "Using ACLs and fetching
Ilya Shipitsin2075ca82020-03-06 23:22:22 +05002637 samples" and subchapter "ACL basics" to understand this pattern matching
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002638 method.
2639
Thierry FOURNIER4dc71972017-01-28 08:33:08 +01002640 Note that :js:attr:`Map.beg` is also available for compatibility.
2641
2642.. js:attribute:: Map._sub
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002643
2644 See the HAProxy configuration.txt file, chapter "Using ACLs and fetching
Ilya Shipitsin2075ca82020-03-06 23:22:22 +05002645 samples" and subchapter "ACL basics" to understand this pattern matching
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002646 method.
2647
Thierry FOURNIER4dc71972017-01-28 08:33:08 +01002648 Note that :js:attr:`Map.sub` is also available for compatibility.
2649
2650.. js:attribute:: Map._dir
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002651
2652 See the HAProxy configuration.txt file, chapter "Using ACLs and fetching
Ilya Shipitsin2075ca82020-03-06 23:22:22 +05002653 samples" and subchapter "ACL basics" to understand this pattern matching
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002654 method.
2655
Thierry FOURNIER4dc71972017-01-28 08:33:08 +01002656 Note that :js:attr:`Map.dir` is also available for compatibility.
2657
2658.. js:attribute:: Map._dom
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002659
2660 See the HAProxy configuration.txt file, chapter "Using ACLs and fetching
Ilya Shipitsin2075ca82020-03-06 23:22:22 +05002661 samples" and subchapter "ACL basics" to understand this pattern matching
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002662 method.
2663
Thierry FOURNIER4dc71972017-01-28 08:33:08 +01002664 Note that :js:attr:`Map.dom` is also available for compatibility.
2665
2666.. js:attribute:: Map._end
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002667
2668 See the HAProxy configuration.txt file, chapter "Using ACLs and fetching
Ilya Shipitsin2075ca82020-03-06 23:22:22 +05002669 samples" and subchapter "ACL basics" to understand this pattern matching
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002670 method.
2671
Thierry FOURNIER4dc71972017-01-28 08:33:08 +01002672.. js:attribute:: Map._reg
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002673
2674 See the HAProxy configuration.txt file, chapter "Using ACLs and fetching
Ilya Shipitsin2075ca82020-03-06 23:22:22 +05002675 samples" and subchapter "ACL basics" to understand this pattern matching
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002676 method.
2677
Thierry FOURNIER4dc71972017-01-28 08:33:08 +01002678 Note that :js:attr:`Map.reg` is also available for compatibility.
2679
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002680
2681.. js:function:: Map.new(file, method)
2682
2683 Creates and load a map.
2684
2685 :param string file: Is the file containing the map.
2686 :param integer method: Is the map pattern matching method. See the attributes
2687 of the Map class.
2688 :returns: a class Map object.
Thierry FOURNIER4dc71972017-01-28 08:33:08 +01002689 :see: The Map attributes: :js:attr:`Map._int`, :js:attr:`Map._ip`,
2690 :js:attr:`Map._str`, :js:attr:`Map._beg`, :js:attr:`Map._sub`,
2691 :js:attr:`Map._dir`, :js:attr:`Map._dom`, :js:attr:`Map._end` and
2692 :js:attr:`Map._reg`.
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002693
2694.. js:function:: Map.lookup(map, str)
2695
2696 Perform a lookup in a map.
2697
2698 :param class_map map: Is the class Map object.
2699 :param string str: Is the string used as key.
2700 :returns: a string containing the result or nil if no match.
2701
2702.. js:function:: Map.slookup(map, str)
2703
2704 Perform a lookup in a map.
2705
2706 :param class_map map: Is the class Map object.
2707 :param string str: Is the string used as key.
2708 :returns: a string containing the result or empty string if no match.
2709
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002710.. _applethttp_class:
2711
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002712AppletHTTP class
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002713================
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002714
2715.. js:class:: AppletHTTP
2716
2717 This class is used with applets that requires the 'http' mode. The http applet
2718 can be registered with the *core.register_service()* function. They are used
2719 for processing an http request like a server in back of HAProxy.
2720
2721 This is an hello world sample code:
2722
2723.. code-block:: lua
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002724
Pieter Baauw4d7f7662015-11-08 16:38:08 +01002725 core.register_service("hello-world", "http", function(applet)
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002726 local response = "Hello World !"
2727 applet:set_status(200)
Pieter Baauw2dcb9bc2015-10-01 22:47:12 +02002728 applet:add_header("content-length", string.len(response))
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002729 applet:add_header("content-type", "text/plain")
Pieter Baauw2dcb9bc2015-10-01 22:47:12 +02002730 applet:start_response()
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002731 applet:send(response)
2732 end)
2733
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002734.. js:attribute:: AppletHTTP.c
2735
2736 :returns: A :ref:`converters_class`
2737
2738 This attribute contains a Converters class object.
2739
2740.. js:attribute:: AppletHTTP.sc
2741
2742 :returns: A :ref:`converters_class`
2743
2744 This attribute contains a Converters class object. The
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02002745 functions of this object always return a string.
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002746
2747.. js:attribute:: AppletHTTP.f
2748
2749 :returns: A :ref:`fetches_class`
2750
2751 This attribute contains a Fetches class object. Note that the
2752 applet execution place cannot access to a valid HAProxy core HTTP
Ilya Shipitsin2075ca82020-03-06 23:22:22 +05002753 transaction, so some sample fetches related to the HTTP dependent
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002754 values (hdr, path, ...) are not available.
2755
2756.. js:attribute:: AppletHTTP.sf
2757
2758 :returns: A :ref:`fetches_class`
2759
2760 This attribute contains a Fetches class object. The functions of
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02002761 this object always return a string. Note that the applet
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002762 execution place cannot access to a valid HAProxy core HTTP
Ilya Shipitsin2075ca82020-03-06 23:22:22 +05002763 transaction, so some sample fetches related to the HTTP dependent
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002764 values (hdr, path, ...) are not available.
2765
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01002766.. js:attribute:: AppletHTTP.method
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002767
2768 :returns: string
2769
2770 The attribute method returns a string containing the HTTP
2771 method.
2772
2773.. js:attribute:: AppletHTTP.version
2774
2775 :returns: string
2776
2777 The attribute version, returns a string containing the HTTP
2778 request version.
2779
2780.. js:attribute:: AppletHTTP.path
2781
2782 :returns: string
2783
2784 The attribute path returns a string containing the HTTP
2785 request path.
2786
2787.. js:attribute:: AppletHTTP.qs
2788
2789 :returns: string
2790
2791 The attribute qs returns a string containing the HTTP
2792 request query string.
2793
2794.. js:attribute:: AppletHTTP.length
2795
2796 :returns: integer
2797
2798 The attribute length returns an integer containing the HTTP
2799 body length.
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002800
Thierry FOURNIER841475e2015-12-11 17:10:09 +01002801.. js:attribute:: AppletHTTP.headers
2802
Patrick Hemmerc6a1d712018-05-01 21:30:41 -04002803 :returns: table
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002804
Patrick Hemmerc6a1d712018-05-01 21:30:41 -04002805 The attribute headers returns a table containing the HTTP
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002806 headers. The header names are always in lower case. As the header name can be
2807 encountered more than once in each request, the value is indexed with 0 as
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02002808 first index value. The table has this form:
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002809
2810.. code-block:: lua
2811
2812 AppletHTTP.headers['<header-name>'][<header-index>] = "<header-value>"
2813
2814 AppletHTTP.headers["host"][0] = "www.test.com"
2815 AppletHTTP.headers["accept"][0] = "audio/basic q=1"
2816 AppletHTTP.headers["accept"][1] = "audio/*, q=0.2"
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01002817 AppletHTTP.headers["accept"][2] = "*/*, q=0.1"
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002818..
2819
Robin H. Johnson52f5db22017-01-01 13:10:52 -08002820.. js:function:: AppletHTTP.set_status(applet, code [, reason])
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002821
2822 This function sets the HTTP status code for the response. The allowed code are
2823 from 100 to 599.
2824
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01002825 :param class_AppletHTTP applet: An :ref:`applethttp_class`
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002826 :param integer code: the status code returned to the client.
Robin H. Johnson52f5db22017-01-01 13:10:52 -08002827 :param string reason: the status reason returned to the client (optional).
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002828
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01002829.. js:function:: AppletHTTP.add_header(applet, name, value)
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002830
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02002831 This function adds a header in the response. Duplicated headers are not
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002832 collapsed. The special header *content-length* is used to determinate the
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02002833 response length. If it does not exist, a *transfer-encoding: chunked* is set, and
Ilya Shipitsin2075ca82020-03-06 23:22:22 +05002834 all the write from the function *AppletHTTP:send()* become a chunk.
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002835
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01002836 :param class_AppletHTTP applet: An :ref:`applethttp_class`
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002837 :param string name: the header name
2838 :param string value: the header value
2839
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01002840.. js:function:: AppletHTTP.start_response(applet)
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002841
2842 This function indicates to the HTTP engine that it can process and send the
2843 response headers. After this called we cannot add headers to the response; We
2844 cannot use the *AppletHTTP:send()* function if the
2845 *AppletHTTP:start_response()* is not called.
2846
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01002847 :param class_AppletHTTP applet: An :ref:`applethttp_class`
2848
2849.. js:function:: AppletHTTP.getline(applet)
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002850
2851 This function returns a string containing one line from the http body. If the
2852 data returned doesn't contains a final '\\n' its assumed than its the last
2853 available data before the end of stream.
2854
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01002855 :param class_AppletHTTP applet: An :ref:`applethttp_class`
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002856 :returns: a string. The string can be empty if we reach the end of the stream.
2857
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01002858.. js:function:: AppletHTTP.receive(applet, [size])
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002859
2860 Reads data from the HTTP body, according to the specified read *size*. If the
2861 *size* is missing, the function tries to read all the content of the stream
2862 until the end. If the *size* is bigger than the http body, it returns the
Bertrand Jacquin874a35c2018-09-10 21:26:07 +01002863 amount of data available.
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002864
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01002865 :param class_AppletHTTP applet: An :ref:`applethttp_class`
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002866 :param integer size: the required read size.
Ilya Shipitsin11057a32020-06-21 21:18:27 +05002867 :returns: always return a string,the string can be empty is the connection is
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002868 closed.
2869
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01002870.. js:function:: AppletHTTP.send(applet, msg)
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002871
2872 Send the message *msg* on the http request body.
2873
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01002874 :param class_AppletHTTP applet: An :ref:`applethttp_class`
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002875 :param string msg: the message to send.
2876
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01002877.. js:function:: AppletHTTP.get_priv(applet)
2878
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01002879 Return Lua data stored in the current transaction. If no data are stored,
2880 it returns a nil value.
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01002881
2882 :param class_AppletHTTP applet: An :ref:`applethttp_class`
Bertrand Jacquin874a35c2018-09-10 21:26:07 +01002883 :returns: the opaque data previously stored, or nil if nothing is
2884 available.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01002885 :see: :js:func:`AppletHTTP.set_priv`
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01002886
2887.. js:function:: AppletHTTP.set_priv(applet, data)
2888
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02002889 Store any data in the current HAProxy transaction. This action replaces the
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01002890 old stored data.
2891
2892 :param class_AppletHTTP applet: An :ref:`applethttp_class`
2893 :param opaque data: The data which is stored in the transaction.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01002894 :see: :js:func:`AppletHTTP.get_priv`
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01002895
Tim Duesterhus4e172c92020-05-19 13:49:42 +02002896.. js:function:: AppletHTTP.set_var(applet, var, value[, ifexist])
Thierry FOURNIER / OZON.IOc1edafe2016-12-12 16:25:30 +01002897
2898 Converts a Lua type in a HAProxy type and store it in a variable <var>.
2899
2900 :param class_AppletHTTP applet: An :ref:`applethttp_class`
2901 :param string var: The variable name according with the HAProxy variable syntax.
2902 :param type value: The value associated to the variable. The type ca be string or
2903 integer.
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02002904 :param boolean ifexist: If this parameter is set to true the variable
Tim Duesterhus4e172c92020-05-19 13:49:42 +02002905 will only be set if it was defined elsewhere (i.e. used
Willy Tarreau7978c5c2021-09-07 14:24:07 +02002906 within the configuration). For global variables (using the
2907 "proc" scope), they will only be updated and never created.
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02002908 It is highly recommended to always set this to true.
2909
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01002910 :see: :js:func:`AppletHTTP.unset_var`
2911 :see: :js:func:`AppletHTTP.get_var`
Thierry FOURNIER / OZON.IOc1edafe2016-12-12 16:25:30 +01002912
2913.. js:function:: AppletHTTP.unset_var(applet, var)
2914
2915 Unset the variable <var>.
2916
2917 :param class_AppletHTTP applet: An :ref:`applethttp_class`
2918 :param string var: The variable name according with the HAProxy variable syntax.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01002919 :see: :js:func:`AppletHTTP.set_var`
2920 :see: :js:func:`AppletHTTP.get_var`
Thierry FOURNIER / OZON.IOc1edafe2016-12-12 16:25:30 +01002921
2922.. js:function:: AppletHTTP.get_var(applet, var)
2923
2924 Returns data stored in the variable <var> converter in Lua type.
2925
2926 :param class_AppletHTTP applet: An :ref:`applethttp_class`
2927 :param string var: The variable name according with the HAProxy variable syntax.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01002928 :see: :js:func:`AppletHTTP.set_var`
2929 :see: :js:func:`AppletHTTP.unset_var`
Thierry FOURNIER / OZON.IOc1edafe2016-12-12 16:25:30 +01002930
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002931.. _applettcp_class:
2932
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002933AppletTCP class
2934===============
2935
2936.. js:class:: AppletTCP
2937
2938 This class is used with applets that requires the 'tcp' mode. The tcp applet
2939 can be registered with the *core.register_service()* function. They are used
2940 for processing a tcp stream like a server in back of HAProxy.
2941
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002942.. js:attribute:: AppletTCP.c
2943
2944 :returns: A :ref:`converters_class`
2945
2946 This attribute contains a Converters class object.
2947
2948.. js:attribute:: AppletTCP.sc
2949
2950 :returns: A :ref:`converters_class`
2951
2952 This attribute contains a Converters class object. The
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02002953 functions of this object always return a string.
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002954
2955.. js:attribute:: AppletTCP.f
2956
2957 :returns: A :ref:`fetches_class`
2958
2959 This attribute contains a Fetches class object.
2960
2961.. js:attribute:: AppletTCP.sf
2962
2963 :returns: A :ref:`fetches_class`
2964
2965 This attribute contains a Fetches class object.
2966
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01002967.. js:function:: AppletTCP.getline(applet)
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002968
2969 This function returns a string containing one line from the stream. If the
2970 data returned doesn't contains a final '\\n' its assumed than its the last
2971 available data before the end of stream.
2972
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01002973 :param class_AppletTCP applet: An :ref:`applettcp_class`
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002974 :returns: a string. The string can be empty if we reach the end of the stream.
2975
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01002976.. js:function:: AppletTCP.receive(applet, [size])
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002977
2978 Reads data from the TCP stream, according to the specified read *size*. If the
2979 *size* is missing, the function tries to read all the content of the stream
2980 until the end.
2981
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01002982 :param class_AppletTCP applet: An :ref:`applettcp_class`
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002983 :param integer size: the required read size.
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02002984 :returns: always return a string, the string can be empty if the connection is
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002985 closed.
2986
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01002987.. js:function:: AppletTCP.send(appletmsg)
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002988
2989 Send the message on the stream.
2990
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01002991 :param class_AppletTCP applet: An :ref:`applettcp_class`
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002992 :param string msg: the message to send.
2993
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01002994.. js:function:: AppletTCP.get_priv(applet)
2995
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01002996 Return Lua data stored in the current transaction. If no data are stored,
2997 it returns a nil value.
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01002998
2999 :param class_AppletTCP applet: An :ref:`applettcp_class`
Bertrand Jacquin874a35c2018-09-10 21:26:07 +01003000 :returns: the opaque data previously stored, or nil if nothing is
3001 available.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01003002 :see: :js:func:`AppletTCP.set_priv`
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01003003
3004.. js:function:: AppletTCP.set_priv(applet, data)
3005
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02003006 Store any data in the current HAProxy transaction. This action replaces the
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01003007 old stored data.
3008
3009 :param class_AppletTCP applet: An :ref:`applettcp_class`
3010 :param opaque data: The data which is stored in the transaction.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01003011 :see: :js:func:`AppletTCP.get_priv`
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01003012
Tim Duesterhus4e172c92020-05-19 13:49:42 +02003013.. js:function:: AppletTCP.set_var(applet, var, value[, ifexist])
Thierry FOURNIER / OZON.IOc1edafe2016-12-12 16:25:30 +01003014
3015 Converts a Lua type in a HAProxy type and stores it in a variable <var>.
3016
3017 :param class_AppletTCP applet: An :ref:`applettcp_class`
3018 :param string var: The variable name according with the HAProxy variable syntax.
3019 :param type value: The value associated to the variable. The type can be string or
3020 integer.
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02003021 :param boolean ifexist: If this parameter is set to true the variable
Tim Duesterhus4e172c92020-05-19 13:49:42 +02003022 will only be set if it was defined elsewhere (i.e. used
Willy Tarreau7978c5c2021-09-07 14:24:07 +02003023 within the configuration). For global variables (using the
3024 "proc" scope), they will only be updated and never created.
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02003025 It is highly recommended to always set this to true.
3026
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01003027 :see: :js:func:`AppletTCP.unset_var`
3028 :see: :js:func:`AppletTCP.get_var`
Thierry FOURNIER / OZON.IOc1edafe2016-12-12 16:25:30 +01003029
3030.. js:function:: AppletTCP.unset_var(applet, var)
3031
3032 Unsets the variable <var>.
3033
3034 :param class_AppletTCP applet: An :ref:`applettcp_class`
3035 :param string var: The variable name according with the HAProxy variable syntax.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01003036 :see: :js:func:`AppletTCP.unset_var`
3037 :see: :js:func:`AppletTCP.set_var`
Thierry FOURNIER / OZON.IOc1edafe2016-12-12 16:25:30 +01003038
3039.. js:function:: AppletTCP.get_var(applet, var)
3040
3041 Returns data stored in the variable <var> converter in Lua type.
3042
3043 :param class_AppletTCP applet: An :ref:`applettcp_class`
3044 :param string var: The variable name according with the HAProxy variable syntax.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01003045 :see: :js:func:`AppletTCP.unset_var`
3046 :see: :js:func:`AppletTCP.set_var`
Thierry FOURNIER / OZON.IOc1edafe2016-12-12 16:25:30 +01003047
Adis Nezirovic8878f8e2018-07-13 12:18:33 +02003048StickTable class
3049================
3050
3051.. js:class:: StickTable
3052
3053 **context**: task, action, sample-fetch
3054
3055 This class can be used to access the HAProxy stick tables from Lua.
3056
3057.. js:function:: StickTable.info()
3058
3059 Returns stick table attributes as a Lua table. See HAProxy documentation for
Ilya Shipitsin2272d8a2020-12-21 01:22:40 +05003060 "stick-table" for canonical info, or check out example below.
Adis Nezirovic8878f8e2018-07-13 12:18:33 +02003061
3062 :returns: Lua table
3063
3064 Assume our table has IPv4 key and gpc0 and conn_rate "columns":
3065
3066.. code-block:: lua
3067
3068 {
3069 expire=<int>, # Value in ms
3070 size=<int>, # Maximum table size
3071 used=<int>, # Actual number of entries in table
3072 data={ # Data columns, with types as key, and periods as values
3073 (-1 if type is not rate counter)
3074 conn_rate=<int>,
3075 gpc0=-1
3076 },
3077 length=<int>, # max string length for string table keys, key length
3078 # otherwise
3079 nopurge=<boolean>, # purge oldest entries when table is full
3080 type="ip" # can be "ip", "ipv6", "integer", "string", "binary"
3081 }
3082
3083.. js:function:: StickTable.lookup(key)
3084
3085 Returns stick table entry for given <key>
3086
3087 :param string key: Stick table key (IP addresses and strings are supported)
3088 :returns: Lua table
3089
3090.. js:function:: StickTable.dump([filter])
3091
3092 Returns all entries in stick table. An optional filter can be used
3093 to extract entries with specific data values. Filter is a table with valid
3094 comparison operators as keys followed by data type name and value pairs.
3095 Check out the HAProxy docs for "show table" for more details. For the
3096 reference, the supported operators are:
3097 "eq", "ne", "le", "lt", "ge", "gt"
3098
3099 For large tables, execution of this function can take a long time (for
3100 HAProxy standards). That's also true when filter is used, so take care and
3101 measure the impact.
3102
3103 :param table filter: Stick table filter
3104 :returns: Stick table entries (table)
3105
3106 See below for example filter, which contains 4 entries (or comparisons).
3107 (Maximum number of filter entries is 4, defined in the source code)
3108
3109.. code-block:: lua
3110
3111 local filter = {
3112 {"gpc0", "gt", 30}, {"gpc1", "gt", 20}}, {"conn_rate", "le", 10}
3113 }
3114
Christopher Faulet0f3c8902020-01-31 18:57:12 +01003115.. _action_class:
3116
3117Action class
3118=============
3119
3120.. js:class:: Act
3121
3122 **context**: action
3123
3124 This class contains all return codes an action may return. It is the lua
3125 equivalent to HAProxy "ACT_RET_*" code.
3126
3127.. code-block:: lua
3128
3129 core.register_action("deny", { "http-req" }, function (txn)
3130 return act.DENY
3131 end)
3132..
3133.. js:attribute:: act.CONTINUE
3134
3135 This attribute is an integer (0). It instructs HAProxy to continue the current
3136 ruleset processing on the message. It is the default return code for a lua
3137 action.
3138
3139 :returns: integer
3140
3141.. js:attribute:: act.STOP
3142
3143 This attribute is an integer (1). It instructs HAProxy to stop the current
3144 ruleset processing on the message.
3145
3146.. js:attribute:: act.YIELD
3147
3148 This attribute is an integer (2). It instructs HAProxy to temporarily pause
3149 the message processing. It will be resumed later on the same rule. The
3150 corresponding lua script is re-executed for the start.
3151
3152.. js:attribute:: act.ERROR
3153
3154 This attribute is an integer (3). It triggers an internal errors The message
3155 processing is stopped and the transaction is terminated. For HTTP streams, an
3156 HTTP 500 error is returned to the client.
3157
3158 :returns: integer
3159
3160.. js:attribute:: act.DONE
3161
3162 This attribute is an integer (4). It instructs HAProxy to stop the message
3163 processing.
3164
3165 :returns: integer
3166
3167.. js:attribute:: act.DENY
3168
3169 This attribute is an integer (5). It denies the current message. The message
3170 processing is stopped and the transaction is terminated. For HTTP streams, an
3171 HTTP 403 error is returned to the client if the deny is returned during the
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02003172 request analysis. During the response analysis, a HTTP 502 error is returned
Christopher Faulet0f3c8902020-01-31 18:57:12 +01003173 and the server response is discarded.
3174
3175 :returns: integer
3176
3177.. js:attribute:: act.ABORT
3178
3179 This attribute is an integer (6). It aborts the current message. The message
3180 processing is stopped and the transaction is terminated. For HTTP streams,
Willy Tarreau714f3452021-05-09 06:47:26 +02003181 HAProxy assumes a response was already sent to the client. From the Lua
Christopher Faulet0f3c8902020-01-31 18:57:12 +01003182 actions point of view, when this code is used, the transaction is terminated
3183 with no reply.
3184
3185 :returns: integer
3186
3187.. js:attribute:: act.INVALID
3188
3189 This attribute is an integer (7). It triggers an internal errors. The message
3190 processing is stopped and the transaction is terminated. For HTTP streams, an
3191 HTTP 400 error is returned to the client if the error is returned during the
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02003192 request analysis. During the response analysis, a HTTP 502 error is returned
Christopher Faulet0f3c8902020-01-31 18:57:12 +01003193 and the server response is discarded.
3194
3195 :returns: integer
Adis Nezirovic8878f8e2018-07-13 12:18:33 +02003196
Christopher Faulet2c2c2e32020-01-31 19:07:52 +01003197.. js:function:: act:wake_time(milliseconds)
3198
3199 **context**: action
3200
3201 Set the script pause timeout to the specified time, defined in
3202 milliseconds.
3203
3204 :param integer milliseconds: the required milliseconds.
3205
3206 This function may be used when a lua action returns `act.YIELD`, to force its
3207 wake-up at most after the specified number of milliseconds.
3208
Christopher Faulet5a2c6612021-08-15 20:35:25 +02003209.. _filter_class:
3210
3211Filter class
3212=============
3213
3214.. js:class:: filter
3215
3216 **context**: filter
3217
3218 This class contains return codes some filter callback functions may return. It
3219 also contains configuration flags and some helper functions. To understand how
3220 the filter API works, see `doc/internal/filters.txt` documentation.
3221
3222.. js:attribute:: filter.CONTINUE
3223
3224 This attribute is an integer (1). It may be returned by some filter callback
3225 functions to instruct this filtering step is finished for this filter.
3226
3227.. js:attribute:: filter.WAIT
3228
3229 This attribute is an integer (0). It may be returned by some filter callback
3230 functions to instruct the filtering must be paused, waiting for more data or
3231 for an external event depending on this filter.
3232
3233.. js:attribute:: filter.ERROR
3234
3235 This attribute is an integer (-1). It may be returned by some filter callback
3236 functions to trigger an error.
3237
3238.. js:attribute:: filter.FLT_CFG_FL_HTX
3239
3240 This attribute is a flag corresponding to the filter flag FLT_CFG_FL_HTX. When
3241 it is set for a filter, it means the filter is able to filter HTTP streams.
3242
3243.. js:function:: filter.register_data_filter(chn)
3244
3245 **context**: filter
3246
3247 Enable the data filtering on the channel **chn** for the current filter. It
Ilya Shipitsinff0f2782021-08-22 22:18:07 +05003248 may be called at any time from any callback functions proceeding the data
Christopher Faulet5a2c6612021-08-15 20:35:25 +02003249 analysis.
3250
3251 :param class_Channel chn: A :ref:`channel_class`.
3252
3253.. js:function:: filter.unregister_data_filter(chn)
3254
3255 **context**: filter
3256
3257 Disable the data filtering on the channel **chn** for the current filter. It
3258 may be called at any time from any callback functions.
3259
3260 :param class_Channel chn: A :ref:`channel_class`.
3261
3262.. js:function:: filter.wake_time(milliseconds)
3263
3264 **context**: filter
3265
3266 Set the script pause timeout to the specified time, defined in
3267 milliseconds.
3268
3269 :param integer milliseconds: the required milliseconds.
3270
3271 This function may be used from any lua filter callback function to force its
3272 wake-up at most after the specified number of milliseconds. Especially, when
3273 `filter.CONTINUE` is returned.
3274
3275
3276A filters is declared using :js:func:`core.register_filter()` function. The
3277provided class will be used to instantiate filters. It may define following
3278attributes:
3279
3280* id: The filter identifier. It is a string that identifies the filter and is
3281 optional.
3282
3283* flags: The filter flags. Only :js:attr:`filter.FLT_CFG_FL_HTX` may be set for now.
3284
3285Such filter class must also define all required callback functions in the
3286following list. Note that :js:func:`Filter.new()` must be defined otherwise the
Ilya Shipitsinff0f2782021-08-22 22:18:07 +05003287filter is ignored. Others are optional.
Christopher Faulet5a2c6612021-08-15 20:35:25 +02003288
3289* .. js:function:: FILTER.new()
3290
3291 Called to instantiate a new filter. This function must be defined.
3292
3293 :returns: a Lua object that will be used as filter instance for the current
3294 stream.
3295
3296* .. js:function:: FILTER.start_analyze(flt, txn, chn)
3297
3298 Called when the analysis starts on the channel **chn**.
3299
3300* .. js:function:: FILTER.end_analyze(flt, txn, chn)
3301
3302 Called when the analysis ends on the channel **chn**.
3303
3304* .. js:function:: FILTER.http_headers(flt, txn, http_msg)
3305
3306 Called just before the HTTP payload analysis and after any processing on the
3307 HTTP message **http_msg**. This callback functions is only called for HTTP
3308 streams.
3309
3310* .. js:function:: FILTER.http_payload(flt, txn, http_msg)
3311
3312 Called during the HTTP payload analysis on the HTTP message **http_msg**. This
3313 callback functions is only called for HTTP streams.
3314
3315* .. js:function:: FILTER.http_end(flt, txn, http_msg)
3316
3317 Called after the HTTP payload analysis on the HTTP message **http_msg**. This
3318 callback functions is only called for HTTP streams.
3319
3320* .. js:function:: FILTER.tcp_payload(flt, txn, chn)
3321
3322 Called during the TCP payload analysis on the channel **chn**.
3323
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02003324Here is a full example:
Christopher Faulet5a2c6612021-08-15 20:35:25 +02003325
3326.. code-block:: lua
3327
3328 Trace = {}
3329 Trace.id = "Lua trace filter"
3330 Trace.flags = filter.FLT_CFG_FL_HTX;
3331 Trace.__index = Trace
3332
3333 function Trace:new()
3334 local trace = {}
3335 setmetatable(trace, Trace)
3336 trace.req_len = 0
3337 trace.res_len = 0
3338 return trace
3339 end
3340
3341 function Trace:start_analyze(txn, chn)
3342 if chn:is_resp() then
3343 print("Start response analysis")
3344 else
3345 print("Start request analysis")
3346 end
3347 filter.register_data_filter(self, chn)
3348 end
3349
3350 function Trace:end_analyze(txn, chn)
3351 if chn:is_resp() then
3352 print("End response analysis: "..self.res_len.." bytes filtered")
3353 else
3354 print("End request analysis: "..self.req_len.." bytes filtered")
3355 end
3356 end
3357
3358 function Trace:http_headers(txn, http_msg)
3359 stline = http_msg:get_stline()
3360 if http_msg.channel:is_resp() then
3361 print("response:")
3362 print(stline.version.." "..stline.code.." "..stline.reason)
3363 else
3364 print("request:")
3365 print(stline.method.." "..stline.uri.." "..stline.version)
3366 end
3367
3368 for n, hdrs in pairs(http_msg:get_headers()) do
3369 for i,v in pairs(hdrs) do
3370 print(n..": "..v)
3371 end
3372 end
3373 return filter.CONTINUE
3374 end
3375
3376 function Trace:http_payload(txn, http_msg)
3377 body = http_msg:body(-20000)
3378 if http_msg.channel:is_resp() then
3379 self.res_len = self.res_len + body:len()
3380 else
3381 self.req_len = self.req_len + body:len()
3382 end
3383 end
3384
3385 core.register_filter("trace", Trace, function(trace, args)
3386 return trace
3387 end)
3388
3389..
3390
3391.. _httpmessage_class:
3392
3393HTTPMessage class
3394===================
3395
3396.. js:class:: HTTPMessage
3397
3398 **context**: filter
3399
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02003400 This class contains all functions to manipulate a HTTP message. For now, this
Christopher Faulet5a2c6612021-08-15 20:35:25 +02003401 class is only available from a filter context.
3402
3403.. js:function:: HTTPMessage.add_header(http_msg, name, value)
3404
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02003405 Appends a HTTP header field in the HTTP message **http_msg** whose name is
Christopher Faulet5a2c6612021-08-15 20:35:25 +02003406 specified in **name** and whose value is defined in **value**.
3407
3408 :param class_httpmessage http_msg: The manipulated HTTP message.
3409 :param string name: The header name.
3410 :param string value: The header value.
3411
3412.. js:function:: HTTPMessage.append(http_msg, string)
3413
3414 This function copies the string **string** at the end of incoming data of the
3415 HTTP message **http_msg**. The function returns the copied length on success
3416 or -1 if data cannot be copied.
3417
3418 Same that :js:func:`HTTPMessage.insert(http_msg, string, http_msg:input())`.
3419
3420 :param class_httpmessage http_msg: The manipulated HTTP message.
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02003421 :param string string: The data to copy at the end of incoming data.
Christopher Faulet5a2c6612021-08-15 20:35:25 +02003422 :returns: an integer containing the amount of bytes copied or -1.
3423
3424.. js:function:: HTTPMessage.body(http_msgl[, offset[, length]])
3425
3426 This function returns **length** bytes of incoming data from the HTTP message
3427 **http_msg**, starting at the offset **offset**. The data are not removed from
3428 the buffer.
3429
3430 By default, if no length is provided, all incoming data found, starting at the
3431 given offset, are returned. If **length** is set to -1, the function tries to
3432 retrieve a maximum of data. Because it is called in the filter context, it
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02003433 never yield. Not providing an offset is the same as setting it to 0. A
Ilya Shipitsinff0f2782021-08-22 22:18:07 +05003434 positive offset is relative to the beginning of incoming data of the
Christopher Faulet5a2c6612021-08-15 20:35:25 +02003435 http_message buffer while negative offset is relative to their end.
3436
3437 If there is no incoming data and the HTTP message can't receive more data, a 'nil'
3438 value is returned.
3439
3440 :param class_httpmessage http_msg: The manipulated HTTP message.
3441 :param integer offset: *optional* The offset in incoming data to start to get
3442 data. 0 by default. May be negative to be relative to
3443 the end of incoming data.
3444 :param integer length: *optional* The expected length of data to retrieve. All
3445 incoming data by default. May be set to -1 to get a
3446 maximum of data.
3447 :returns: a string containing the data found or nil.
3448
3449.. js:function:: HTTPMessage.eom(http_msg)
3450
3451 This function returns true if the end of message is reached for the HTTP
3452 message **http_msg**.
3453
3454 :param class_httpmessage http_msg: The manipulated HTTP message.
3455 :returns: an integer containing the amount of available bytes.
3456
3457.. js:function:: HTTPMessage.del_header(http_msg, name)
3458
3459 Removes all HTTP header fields in the HTTP message **http_msg** whose name is
3460 specified in **name**.
3461
3462 :param class_httpmessage http_msg: The manipulated http message.
3463 :param string name: The header name.
3464
3465.. js:function:: HTTPMessage.get_headers(http_msg)
3466
3467 Returns a table containing all the headers of the HTTP message **http_msg**.
3468
3469 :param class_httpmessage http_msg: The manipulated http message.
3470 :returns: table of headers.
3471
3472 This is the form of the returned table:
3473
3474.. code-block:: lua
3475
3476 http_msg:get_headers()['<header-name>'][<header-index>] = "<header-value>"
3477
3478 local hdr = http_msg:get_headers()
3479 hdr["host"][0] = "www.test.com"
3480 hdr["accept"][0] = "audio/basic q=1"
3481 hdr["accept"][1] = "audio/*, q=0.2"
3482 hdr["accept"][2] = "*.*, q=0.1"
3483..
3484
3485.. js:function:: HTTPMessage.get_stline(http_msg)
3486
3487 Returns a table containing the start-line of the HTTP message **http_msg**.
3488
3489 :param class_httpmessage http_msg: The manipulated http message.
3490 :returns: the start-line.
3491
3492 This is the form of the returned table:
3493
3494.. code-block:: lua
3495
3496 -- for the request :
3497 {"method" = string, "uri" = string, "version" = string}
3498
3499 -- for the response:
3500 {"version" = string, "code" = string, "reason" = string}
3501..
3502
3503.. js:function:: HTTPMessage.forward(http_msg, length)
3504
3505 This function forwards **length** bytes of data from the HTTP message
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02003506 **http_msg**. Because it is called in the filter context, it never yields. Only
Christopher Faulet5a2c6612021-08-15 20:35:25 +02003507 available incoming data may be forwarded, event if the requested length
3508 exceeds the available amount of incoming data. It returns the amount of data
3509 forwarded.
3510
3511 :param class_httpmessage http_msg: The manipulated HTTP message.
3512 :param integer int: The amount of data to forward.
3513
3514.. js:function:: HTTPMessage.input(http_msg)
3515
3516 This function returns the length of incoming data in the HTTP message
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02003517 **http_msg** from the filter point of view.
Christopher Faulet5a2c6612021-08-15 20:35:25 +02003518
3519 :param class_httpmessage http_msg: The manipulated HTTP message.
3520 :returns: an integer containing the amount of available bytes.
3521
3522.. js:function:: HTTPMessage.insert(http_msg, string[, offset])
3523
3524 This function copies the string **string** at the offset **offset** in
3525 incoming data of the HTTP message **http_msg**. The function returns the
3526 copied length on success or -1 if data cannot be copied.
3527
3528 By default, if no offset is provided, the string is copied in front of
Ilya Shipitsinff0f2782021-08-22 22:18:07 +05003529 incoming data. A positive offset is relative to the beginning of incoming data
Christopher Faulet5a2c6612021-08-15 20:35:25 +02003530 of the HTTP message while negative offset is relative to their end.
3531
3532 :param class_httpmessage http_msg: The manipulated HTTP message.
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02003533 :param string string: The data to copy into incoming data.
3534 :param integer offset: *optional* The offset in incoming data where to copy
Christopher Faulet5a2c6612021-08-15 20:35:25 +02003535 data. 0 by default. May be negative to be relative to
3536 the end of incoming data.
3537 :returns: an integer containing the amount of bytes copied or -1.
3538
3539.. js:function:: HTTPMessage.is_full(http_msg)
3540
3541 This function returns true if the HTTP message **http_msg** is full.
3542
3543 :param class_httpmessage http_msg: The manipulated HTTP message.
3544 :returns: a boolean
3545
3546.. js:function:: HTTPMessage.is_resp(http_msg)
3547
3548 This function returns true if the HTTP message **http_msg** is the response
3549 one.
3550
3551 :param class_httpmessage http_msg: The manipulated HTTP message.
3552 :returns: a boolean
3553
3554.. js:function:: HTTPMessage.may_recv(http_msg)
3555
3556 This function returns true if the HTTP message **http_msg** may still receive
3557 data.
3558
3559 :param class_httpmessage http_msg: The manipulated HTTP message.
3560 :returns: a boolean
3561
3562.. js:function:: HTTPMessage.output(http_msg)
3563
3564 This function returns the length of outgoing data of the HTTP message
3565 **http_msg**.
3566
3567 :param class_httpmessage http_msg: The manipulated HTTP message.
3568 :returns: an integer containing the amount of available bytes.
3569
3570.. js:function:: HTTPMessage.prepend(http_msg, string)
3571
3572 This function copies the string **string** in front of incoming data of the
3573 HTTP message **http_msg**. The function returns the copied length on success
3574 or -1 if data cannot be copied.
3575
3576 Same that :js:func:`HTTPMessage.insert(http_msg, string, 0)`.
3577
3578 :param class_httpmessage http_msg: The manipulated HTTP message.
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02003579 :param string string: The data to copy in front of incoming data.
Christopher Faulet5a2c6612021-08-15 20:35:25 +02003580 :returns: an integer containing the amount of bytes copied or -1.
3581
3582.. js:function:: HTTPMessage.remove(http_msg[, offset[, length]])
3583
3584 This function removes **length** bytes of incoming data of the HTTP message
3585 **http_msg**, starting at offset **offset**. This function returns number of
3586 bytes removed on success.
3587
3588 By default, if no length is provided, all incoming data, starting at the given
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02003589 offset, are removed. Not providing an offset is the same that setting it
Ilya Shipitsinff0f2782021-08-22 22:18:07 +05003590 to 0. A positive offset is relative to the beginning of incoming data of the
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02003591 HTTP message while negative offset is relative to the end.
Christopher Faulet5a2c6612021-08-15 20:35:25 +02003592
3593 :param class_httpmessage http_msg: The manipulated HTTP message.
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02003594 :param integer offset: *optional* The offset in incoming data where to start
Christopher Faulet5a2c6612021-08-15 20:35:25 +02003595 to remove data. 0 by default. May be negative to
3596 be relative to the end of incoming data.
3597 :param integer length: *optional* The length of data to remove. All incoming
3598 data by default.
3599 :returns: an integer containing the amount of bytes removed.
3600
3601.. js:function:: HTTPMessage.rep_header(http_msg, name, regex, replace)
3602
3603 Matches the regular expression in all occurrences of header field **name**
3604 according to regex **regex**, and replaces them with the string **replace**.
3605 The replacement value can contain back references like \1, \2, ... This
3606 function acts on whole header lines, regardless of the number of values they
3607 may contain.
3608
3609 :param class_httpmessage http_msg: The manipulated HTTP message.
3610 :param string name: The header name.
3611 :param string regex: The match regular expression.
3612 :param string replace: The replacement value.
3613
3614.. js:function:: HTTPMessage.rep_value(http_msg, name, regex, replace)
3615
3616 Matches the regular expression on every comma-delimited value of header field
3617 **name** according to regex **regex**, and replaces them with the string
3618 **replace**. The replacement value can contain back references like \1, \2,
3619 ...
3620
3621 :param class_httpmessage http_msg: The manipulated HTTP message.
3622 :param string name: The header name.
3623 :param string regex: The match regular expression.
3624 :param string replace: The replacement value.
3625
3626.. js:function:: HTTPMessage.send(http_msg, string)
3627
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02003628 This function requires immediate send of the string **string**. It means the
Ilya Shipitsinff0f2782021-08-22 22:18:07 +05003629 string is copied at the beginning of incoming data of the HTTP message
Christopher Faulet5a2c6612021-08-15 20:35:25 +02003630 **http_msg** and immediately forwarded. Because it is called in the filter
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02003631 context, it never yields.
Christopher Faulet5a2c6612021-08-15 20:35:25 +02003632
3633 :param class_httpmessage http_msg: The manipulated HTTP message.
3634 :param string string: The data to send.
3635 :returns: an integer containing the amount of bytes copied or -1.
3636
3637.. js:function:: HTTPMessage.set(http_msg, string[, offset[, length]])
3638
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02003639 This function replaces **length** bytes of incoming data of the HTTP message
Christopher Faulet5a2c6612021-08-15 20:35:25 +02003640 **http_msg**, starting at offset **offset**, by the string **string**. The
3641 function returns the copied length on success or -1 if data cannot be copied.
3642
3643 By default, if no length is provided, all incoming data, starting at the given
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02003644 offset, are replaced. Not providing an offset is the same as setting it
Ilya Shipitsinff0f2782021-08-22 22:18:07 +05003645 to 0. A positive offset is relative to the beginning of incoming data of the
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02003646 HTTP message while negative offset is relative to the end.
Christopher Faulet5a2c6612021-08-15 20:35:25 +02003647
3648 :param class_httpmessage http_msg: The manipulated HTTP message.
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02003649 :param string string: The data to copy into incoming data.
3650 :param integer offset: *optional* The offset in incoming data where to start
Christopher Faulet5a2c6612021-08-15 20:35:25 +02003651 the data replacement. 0 by default. May be negative to
3652 be relative to the end of incoming data.
3653 :param integer length: *optional* The length of data to replace. All incoming
3654 data by default.
3655 :returns: an integer containing the amount of bytes copied or -1.
3656
3657.. js:function:: HTTPMessage.set_eom(http_msg)
3658
3659 This function set the end of message for the HTTP message **http_msg**.
3660
3661 :param class_httpmessage http_msg: The manipulated HTTP message.
3662
3663.. js:function:: HTTPMessage.set_header(http_msg, name, value)
3664
3665 This variable replace all occurrence of all header matching the name **name**,
3666 by only one containing the value **value**.
3667
3668 :param class_httpmessage http_msg: The manipulated HTTP message.
3669 :param string name: The header name.
3670 :param string value: The header value.
3671
3672 This function does the same work as the following code:
3673
3674.. code-block:: lua
3675
3676 http_msg:del_header("header")
3677 http_msg:add_header("header", "value")
3678..
3679
3680.. js:function:: HTTPMessage.set_method(http_msg, method)
3681
3682 Rewrites the request method with the string **method**. The HTTP message
3683 **http_msg** must be the request.
3684
3685 :param class_httpmessage http_msg: The manipulated HTTP message.
3686 :param string method: The new method.
3687
3688.. js:function:: HTTPMessage.set_path(http_msg, path)
3689
3690 Rewrites the request path with the string **path**. The HTTP message
3691 **http_msg** must be the request.
3692
3693 :param class_httpmessage http_msg: The manipulated HTTP message.
3694 :param string method: The new method.
3695
3696.. js:function:: HTTPMessage.set_query(http_msg, query)
3697
3698 Rewrites the request's query string which appears after the first question
3699 mark ("?") with the string **query**. The HTTP message **http_msg** must be
3700 the request.
3701
3702 :param class_httpmessage http_msg: The manipulated HTTP message.
3703 :param string query: The new query.
3704
3705.. js:function:: HTTPMessage.set_status(http_msg, status[, reason])
3706
Ilya Shipitsinff0f2782021-08-22 22:18:07 +05003707 Rewrites the response status code with the integer **code** and optional the
Christopher Faulet5a2c6612021-08-15 20:35:25 +02003708 reason **reason**. If no custom reason is provided, it will be generated from
3709 the status. The HTTP message **http_msg** must be the response.
3710
3711 :param class_httpmessage http_msg: The manipulated HTTP message.
3712 :param integer status: The new response status code.
3713 :param string reason: The new response reason (optional).
3714
3715.. js:function:: HTTPMessage.set_uri(http_msg, uri)
3716
3717 Rewrites the request URI with the string **uri**. The HTTP message
3718 **http_msg** must be the request.
3719
3720 :param class_httpmessage http_msg: The manipulated HTTP message.
3721 :param string uri: The new uri.
3722
3723.. js:function:: HTTPMessage.unset_eom(http_msg)
3724
3725 This function remove the end of message for the HTTP message **http_msg**.
3726
3727 :param class_httpmessage http_msg: The manipulated HTTP message.
3728
William Lallemand10cea5c2022-03-30 16:02:43 +02003729.. _CertCache_class:
3730
3731CertCache class
3732================
3733
3734.. js:class:: CertCache
3735
3736 This class allows to update an SSL certificate file in the memory of the
3737 current HAProxy process. It will do the same as "set ssl cert" + "commit ssl
3738 cert" over the HAProxy CLI.
3739
3740.. js:function:: CertCache.set(certificate)
3741
3742 This function updates a certificate in memory.
3743
3744 :param table certificate: A table containing the fields to update.
3745 :param string certificate.filename: The mandatory filename of the certificate
3746 to update, it must already exist in memory.
3747 :param string certificate.crt: A certificate in the PEM format. It can also
3748 contain a private key.
3749 :param string certificate.key: A private key in the PEM format.
3750 :param string certificate.ocsp: An OCSP response in base64. (cf management.txt)
3751 :param string certificate.issuer: The certificate of the OCSP issuer.
3752 :param string certificate.sctl: An SCTL file.
3753
3754.. code-block:: lua
3755
3756 CertCache.set{filename="certs/localhost9994.pem.rsa", crt=crt}
3757
3758
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01003759External Lua libraries
3760======================
3761
3762A lot of useful lua libraries can be found here:
3763
Aurelien DARRAGON846fc7d2022-10-14 08:48:57 +02003764* Lua toolbox has been superseded by `https://luarocks.org/ <https://luarocks.org/>`_
3765 The old lua toolbox source code is still available here `https://github.com/catwell/lua-toolbox <https://github.com/catwell/lua-toolbox>`_ (DEPRECATED)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01003766
Ilya Shipitsin2075ca82020-03-06 23:22:22 +05003767Redis client library:
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01003768
3769* `https://github.com/nrk/redis-lua <https://github.com/nrk/redis-lua>`_
3770
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02003771This is an example about the usage of the Redis library within HAProxy. Note that
3772each call to any function of this library can throw an error if the socket
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01003773connection fails.
3774
3775.. code-block:: lua
3776
3777 -- load the redis library
3778 local redis = require("redis");
3779
3780 function do_something(txn)
3781
3782 -- create and connect new tcp socket
3783 local tcp = core.tcp();
3784 tcp:settimeout(1);
3785 tcp:connect("127.0.0.1", 6379);
3786
3787 -- use the redis library with this new socket
3788 local client = redis.connect({socket=tcp});
3789 client:ping();
3790
3791 end
3792
3793OpenSSL:
3794
3795* `http://mkottman.github.io/luacrypto/index.html
3796 <http://mkottman.github.io/luacrypto/index.html>`_
3797
3798* `https://github.com/brunoos/luasec/wiki
3799 <https://github.com/brunoos/luasec/wiki>`_