blob: e20042ddae38b4544e84baa7caf1c19b613e9d95 [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
Ilya Shipitsin5fa29b82022-12-07 09:46:19 +0500367 This information is 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
Aurelien DARRAGONc4b24372023-03-02 12:00:06 +0100922 .. warning::
923 This attribute is now deprecated and will eventually be removed.
924 Please use :js:func:`Proxy.get_name()` function instead.
925
Thierry Fournierb0467732022-10-07 12:07:24 +0200926.. js:function:: Proxy.get_name()
927
928 Returns the name of the proxy.
929
Baptiste Assmann46c72552017-10-26 21:51:58 +0200930.. js:attribute:: Proxy.uuid
931
932 Contain the unique identifier of the proxy.
933
Aurelien DARRAGONc4b24372023-03-02 12:00:06 +0100934 .. warning::
935 This attribute is now deprecated and will eventually be removed.
936 Please use :js:func:`Proxy.get_uuid()` function instead.
937
Thierry Fournierb0467732022-10-07 12:07:24 +0200938.. js:function:: Proxy.get_uuid()
939
940 Returns the unique identifier of the proxy.
941
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +0100942.. js:attribute:: Proxy.servers
943
Patrick Hemmerc6a1d712018-05-01 21:30:41 -0400944 Contain a table with the attached servers. The table is indexed by server
945 name, and each server entry is an object of type :ref:`server_class`.
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +0100946
Adis Nezirovic8878f8e2018-07-13 12:18:33 +0200947.. js:attribute:: Proxy.stktable
948
949 Contains a stick table object attached to the proxy.
950
Thierry Fournierff480422016-02-25 08:36:46 +0100951.. js:attribute:: Proxy.listeners
952
Patrick Hemmerc6a1d712018-05-01 21:30:41 -0400953 Contain a table with the attached listeners. The table is indexed by listener
954 name, and each each listeners entry is an object of type
955 :ref:`listener_class`.
Thierry Fournierff480422016-02-25 08:36:46 +0100956
Thierry Fournierf61aa632016-02-19 20:56:00 +0100957.. js:function:: Proxy.pause(px)
958
959 Pause the proxy. See the management socket documentation for more information.
960
961 :param class_proxy px: A :ref:`proxy_class` which indicates the manipulated
962 proxy.
963
964.. js:function:: Proxy.resume(px)
965
966 Resume the proxy. See the management socket documentation for more
967 information.
968
969 :param class_proxy px: A :ref:`proxy_class` which indicates the manipulated
970 proxy.
971
972.. js:function:: Proxy.stop(px)
973
974 Stop the proxy. See the management socket documentation for more information.
975
976 :param class_proxy px: A :ref:`proxy_class` which indicates the manipulated
977 proxy.
978
979.. js:function:: Proxy.shut_bcksess(px)
980
981 Kill the session attached to a backup server. See the management socket
982 documentation for more information.
983
984 :param class_proxy px: A :ref:`proxy_class` which indicates the manipulated
985 proxy.
986
987.. js:function:: Proxy.get_cap(px)
988
989 Returns a string describing the capabilities of the proxy.
990
991 :param class_proxy px: A :ref:`proxy_class` which indicates the manipulated
992 proxy.
993 :returns: a string "frontend", "backend", "proxy" or "ruleset".
994
995.. js:function:: Proxy.get_mode(px)
996
997 Returns a string describing the mode of the current proxy.
998
999 :param class_proxy px: A :ref:`proxy_class` which indicates the manipulated
1000 proxy.
1001 :returns: a string "tcp", "http", "health" or "unknown"
1002
1003.. js:function:: Proxy.get_stats(px)
1004
Bertrand Jacquin874a35c2018-09-10 21:26:07 +01001005 Returns a table containing the proxy statistics. The statistics returned are
Thierry Fournierf61aa632016-02-19 20:56:00 +01001006 not the same if the proxy is frontend or a backend.
1007
1008 :param class_proxy px: A :ref:`proxy_class` which indicates the manipulated
1009 proxy.
Patrick Hemmerc6a1d712018-05-01 21:30:41 -04001010 :returns: a key/value table containing stats
Thierry Fournierf61aa632016-02-19 20:56:00 +01001011
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001012.. _server_class:
1013
1014Server class
1015============
1016
Patrick Hemmerc6a1d712018-05-01 21:30:41 -04001017.. js:class:: Server
1018
1019 This class provides a way for manipulating servers and retrieving information.
1020
Patrick Hemmera62ae7e2018-04-29 14:23:48 -04001021.. js:attribute:: Server.name
1022
1023 Contain the name of the server.
1024
Aurelien DARRAGONc4b24372023-03-02 12:00:06 +01001025 .. warning::
1026 This attribute is now deprecated and will eventually be removed.
1027 Please use :js:func:`Server.get_name()` function instead.
1028
Thierry Fournierb0467732022-10-07 12:07:24 +02001029.. js:function:: Server.get_name(sv)
1030
1031 Returns the name of the server.
1032
Patrick Hemmera62ae7e2018-04-29 14:23:48 -04001033.. js:attribute:: Server.puid
1034
1035 Contain the proxy unique identifier of the server.
1036
Aurelien DARRAGONc4b24372023-03-02 12:00:06 +01001037 .. warning::
1038 This attribute is now deprecated and will eventually be removed.
1039 Please use :js:func:`Server.get_puid()` function instead.
1040
Thierry Fournierb0467732022-10-07 12:07:24 +02001041.. js:function:: Server.get_puid(sv)
1042
1043 Returns the proxy unique identifier of the server.
1044
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001045.. js:function:: Server.is_draining(sv)
1046
Patrick Hemmerc6a1d712018-05-01 21:30:41 -04001047 Return true if the server is currently draining sticky connections.
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001048
1049 :param class_server sv: A :ref:`server_class` which indicates the manipulated
1050 server.
1051 :returns: a boolean
1052
Patrick Hemmer32d539f2018-04-29 14:25:46 -04001053.. js:function:: Server.set_maxconn(sv, weight)
1054
1055 Dynamically change the maximum connections of the server. See the management
1056 socket documentation for more information about the format of the string.
1057
1058 :param class_server sv: A :ref:`server_class` which indicates the manipulated
1059 server.
1060 :param string maxconn: A string describing the server maximum connections.
1061
1062.. js:function:: Server.get_maxconn(sv, weight)
1063
1064 This function returns an integer representing the server maximum connections.
1065
1066 :param class_server sv: A :ref:`server_class` which indicates the manipulated
1067 server.
1068 :returns: an integer.
1069
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001070.. js:function:: Server.set_weight(sv, weight)
1071
Patrick Hemmerc6a1d712018-05-01 21:30:41 -04001072 Dynamically change the weight of the server. See the management socket
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001073 documentation for more information about the format of the string.
1074
1075 :param class_server sv: A :ref:`server_class` which indicates the manipulated
1076 server.
1077 :param string weight: A string describing the server weight.
1078
1079.. js:function:: Server.get_weight(sv)
1080
Patrick Hemmerc6a1d712018-05-01 21:30:41 -04001081 This function returns an integer representing the server weight.
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001082
1083 :param class_server sv: A :ref:`server_class` which indicates the manipulated
1084 server.
1085 :returns: an integer.
1086
Joseph C. Sible49bbf522020-05-04 22:20:32 -04001087.. js:function:: Server.set_addr(sv, addr[, port])
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001088
Patrick Hemmerc6a1d712018-05-01 21:30:41 -04001089 Dynamically change the address of the server. See the management socket
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001090 documentation for more information about the format of the string.
1091
1092 :param class_server sv: A :ref:`server_class` which indicates the manipulated
1093 server.
Patrick Hemmerc6a1d712018-05-01 21:30:41 -04001094 :param string addr: A string describing the server address.
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001095
1096.. js:function:: Server.get_addr(sv)
1097
Patrick Hemmerc6a1d712018-05-01 21:30:41 -04001098 Returns a string describing the address of the server.
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001099
1100 :param class_server sv: A :ref:`server_class` which indicates the manipulated
1101 server.
1102 :returns: A string
1103
1104.. js:function:: Server.get_stats(sv)
1105
1106 Returns server statistics.
1107
1108 :param class_server sv: A :ref:`server_class` which indicates the manipulated
1109 server.
Patrick Hemmerc6a1d712018-05-01 21:30:41 -04001110 :returns: a key/value table containing stats
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001111
1112.. js:function:: Server.shut_sess(sv)
1113
1114 Shutdown all the sessions attached to the server. See the management socket
1115 documentation for more 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.set_drain(sv)
1121
1122 Drain sticky sessions. 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.set_maint(sv)
1129
1130 Set maintenance mode. 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.set_ready(sv)
1137
1138 Set normal mode. See the management socket documentation for more information
1139 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_enable(sv)
1145
1146 Enable health checks. 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.check_disable(sv)
1153
1154 Disable health checks. 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.check_force_up(sv)
1161
1162 Force health-check up. 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.check_force_nolb(sv)
1169
1170 Force health-check nolb mode. 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.check_force_down(sv)
1177
1178 Force health-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
1184.. js:function:: Server.agent_enable(sv)
1185
1186 Enable agent check. See the management socket documentation for more
1187 information about this function.
1188
1189 :param class_server sv: A :ref:`server_class` which indicates the manipulated
1190 server.
1191
1192.. js:function:: Server.agent_disable(sv)
1193
1194 Disable agent check. See the management socket documentation for more
1195 information about this function.
1196
1197 :param class_server sv: A :ref:`server_class` which indicates the manipulated
1198 server.
1199
1200.. js:function:: Server.agent_force_up(sv)
1201
1202 Force agent check up. See the management socket documentation for more
1203 information about this function.
1204
1205 :param class_server sv: A :ref:`server_class` which indicates the manipulated
1206 server.
1207
1208.. js:function:: Server.agent_force_down(sv)
1209
1210 Force agent check down. See the management socket documentation for more
1211 information about this function.
1212
1213 :param class_server sv: A :ref:`server_class` which indicates the manipulated
1214 server.
1215
Thierry Fournierff480422016-02-25 08:36:46 +01001216.. _listener_class:
1217
1218Listener class
1219==============
1220
1221.. js:function:: Listener.get_stats(ls)
1222
1223 Returns server statistics.
1224
1225 :param class_listener ls: A :ref:`listener_class` which indicates the
1226 manipulated listener.
Patrick Hemmerc6a1d712018-05-01 21:30:41 -04001227 :returns: a key/value table containing stats
Thierry Fournierff480422016-02-25 08:36:46 +01001228
Thierry Fournier1de16592016-01-27 09:49:07 +01001229.. _concat_class:
1230
1231Concat class
1232============
1233
1234.. js:class:: Concat
1235
1236 This class provides a fast way for string concatenation. The way using native
1237 Lua concatenation like the code below is slow for some reasons.
1238
1239.. code-block:: lua
1240
1241 str = "string1"
1242 str = str .. ", string2"
1243 str = str .. ", string3"
1244..
1245
1246 For each concatenation, Lua:
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02001247 - allocates memory for the result,
1248 - catenates the two string copying the strings in the new memory block,
1249 - frees the old memory block containing the string which is no longer used.
1250
Thierry Fournier1de16592016-01-27 09:49:07 +01001251 This process does many memory move, allocation and free. In addition, the
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02001252 memory is not really freed, it is just marked as unused and waits for the
Thierry Fournier1de16592016-01-27 09:49:07 +01001253 garbage collector.
1254
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02001255 The Concat class provides an alternative way to concatenate strings. It uses
Thierry Fournier1de16592016-01-27 09:49:07 +01001256 the internal Lua mechanism (it does not allocate memory), but it doesn't copy
1257 the data more than once.
1258
1259 On my computer, the following loops spends 0.2s for the Concat method and
1260 18.5s for the pure Lua implementation. So, the Concat class is about 1000x
1261 faster than the embedded solution.
1262
1263.. code-block:: lua
1264
1265 for j = 1, 100 do
1266 c = core.concat()
1267 for i = 1, 20000 do
1268 c:add("#####")
1269 end
1270 end
1271..
1272
1273.. code-block:: lua
1274
1275 for j = 1, 100 do
1276 c = ""
1277 for i = 1, 20000 do
1278 c = c .. "#####"
1279 end
1280 end
1281..
1282
1283.. js:function:: Concat.add(concat, string)
1284
1285 This function adds a string to the current concatenated string.
1286
1287 :param class_concat concat: A :ref:`concat_class` which contains the currently
Ilya Shipitsin2075ca82020-03-06 23:22:22 +05001288 built string.
Bertrand Jacquin874a35c2018-09-10 21:26:07 +01001289 :param string string: A new string to concatenate to the current built
Thierry Fournier1de16592016-01-27 09:49:07 +01001290 string.
1291
1292.. js:function:: Concat.dump(concat)
1293
Bertrand Jacquin874a35c2018-09-10 21:26:07 +01001294 This function returns the concatenated string.
Thierry Fournier1de16592016-01-27 09:49:07 +01001295
1296 :param class_concat concat: A :ref:`concat_class` which contains the currently
Ilya Shipitsin2075ca82020-03-06 23:22:22 +05001297 built string.
Thierry Fournier1de16592016-01-27 09:49:07 +01001298 :returns: the concatenated string
1299
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001300.. _fetches_class:
1301
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001302Fetches class
1303=============
1304
1305.. js:class:: Fetches
1306
1307 This class contains a lot of internal HAProxy sample fetches. See the
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02001308 HAProxy "configuration.txt" documentation for more information.
1309 (chapters 7.3.2 to 7.3.6)
Thierry FOURNIER2e4893c2015-03-18 13:37:27 +01001310
Christopher Faulet1e9b1b62021-08-11 10:14:30 +02001311 .. warning::
1312 some sample fetches are not available in some context. These limitations
1313 are specified in this documentation when they're useful.
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001314
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01001315 :see: :js:attr:`TXN.f`
1316 :see: :js:attr:`TXN.sf`
Thierry FOURNIER2e4893c2015-03-18 13:37:27 +01001317
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02001318 Fetches are useful to:
Thierry FOURNIER2e4893c2015-03-18 13:37:27 +01001319
1320 * get system time,
1321 * get environment variable,
1322 * get random numbers,
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02001323 * know backend status like the number of users in queue or the number of
Thierry FOURNIER2e4893c2015-03-18 13:37:27 +01001324 connections established,
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02001325 * get client information like ip source or destination,
Thierry FOURNIER2e4893c2015-03-18 13:37:27 +01001326 * deal with stick tables,
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02001327 * fetch established SSL information,
1328 * fetch HTTP information like headers or method.
Thierry FOURNIER2e4893c2015-03-18 13:37:27 +01001329
1330.. code-block:: lua
1331
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001332 function action(txn)
1333 -- Get source IP
1334 local clientip = txn.f:src()
1335 end
Thierry FOURNIER2e4893c2015-03-18 13:37:27 +01001336..
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001337
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001338.. _converters_class:
1339
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001340Converters class
1341================
1342
1343.. js:class:: Converters
1344
1345 This class contains a lot of internal HAProxy sample converters. See the
Thierry FOURNIER2e4893c2015-03-18 13:37:27 +01001346 HAProxy documentation "configuration.txt" for more information about her
1347 usage. Its the chapter 7.3.1.
1348
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01001349 :see: :js:attr:`TXN.c`
1350 :see: :js:attr:`TXN.sc`
Thierry FOURNIER2e4893c2015-03-18 13:37:27 +01001351
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02001352 Converters provides stateful transformation. They are useful to:
Thierry FOURNIER2e4893c2015-03-18 13:37:27 +01001353
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02001354 * convert input to base64,
1355 * apply hash on input string (djb2, crc32, sdbm, wt6),
Thierry FOURNIER2e4893c2015-03-18 13:37:27 +01001356 * format date,
1357 * json escape,
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02001358 * extract preferred language comparing two lists,
Thierry FOURNIER2e4893c2015-03-18 13:37:27 +01001359 * turn to lower or upper chars,
1360 * deal with stick tables.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001361
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001362.. _channel_class:
1363
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001364Channel class
1365=============
1366
1367.. js:class:: Channel
1368
Christopher Faulet5a2c6612021-08-15 20:35:25 +02001369 **context**: action, sample-fetch, convert, filter
1370
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001371 HAProxy uses two buffers for the processing of the requests. The first one is
1372 used with the request data (from the client to the server) and the second is
1373 used for the response data (from the server to the client).
1374
1375 Each buffer contains two types of data. The first type is the incoming data
1376 waiting for a processing. The second part is the outgoing data already
1377 processed. Usually, the incoming data is processed, after it is tagged as
1378 outgoing data, and finally it is sent. The following functions provides tools
1379 for manipulating these data in a buffer.
1380
1381 The following diagram shows where the channel class function are applied.
1382
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001383 .. image:: _static/channel.png
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001384
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001385 .. warning::
1386 It is not possible to read from the response in request action, and it is
Boyang Li60cfe8b2022-05-10 18:11:00 +00001387 not possible to read from the request channel in response action.
Christopher Faulet09530392021-06-14 11:43:18 +02001388
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001389 .. warning::
1390 It is forbidden to alter the Channels buffer from HTTP contexts. So only
1391 :js:func:`Channel.input`, :js:func:`Channel.output`,
1392 :js:func:`Channel.may_recv`, :js:func:`Channel.is_full` and
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02001393 :js:func:`Channel.is_resp` can be called from a HTTP context.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001394
Christopher Faulet5a2c6612021-08-15 20:35:25 +02001395 All the functions provided by this class are available in the
1396 **sample-fetches**, **actions** and **filters** contexts. For **filters**,
1397 incoming data (offset and length) are relative to the filter. Some functions
Boyang Li60cfe8b2022-05-10 18:11:00 +00001398 may yield, but only for **actions**. Yield is not possible for
Christopher Faulet5a2c6612021-08-15 20:35:25 +02001399 **sample-fetches**, **converters** and **filters**.
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001400
1401.. js:function:: Channel.append(channel, string)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001402
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001403 This function copies the string **string** at the end of incoming data of the
1404 channel buffer. The function returns the copied length on success or -1 if
1405 data cannot be copied.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001406
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001407 Same that :js:func:`Channel.insert(channel, string, channel:input())`.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001408
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001409 :param class_channel channel: The manipulated Channel.
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02001410 :param string string: The data to copy at the end of incoming data.
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001411 :returns: an integer containing the amount of bytes copied or -1.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001412
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001413.. js:function:: Channel.data(channel [, offset [, length]])
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001414
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001415 This function returns **length** bytes of incoming data from the channel
1416 buffer, starting at the offset **offset**. The data are not removed from the
1417 buffer.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001418
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001419 By default, if no length is provided, all incoming data found, starting at the
1420 given offset, are returned. If **length** is set to -1, the function tries to
Christopher Faulet5a2c6612021-08-15 20:35:25 +02001421 retrieve a maximum of data and, if called by an action, it yields if
1422 necessary. It also waits for more data if the requested length exceeds the
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02001423 available amount of incoming data. Not providing an offset is the same as
Ilya Shipitsinff0f2782021-08-22 22:18:07 +05001424 setting it to 0. A positive offset is relative to the beginning of incoming
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02001425 data of the channel buffer while negative offset is relative to the end.
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001426
1427 If there is no incoming data and the channel can't receive more data, a 'nil'
1428 value is returned.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001429
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001430 :param class_channel channel: The manipulated Channel.
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001431 :param integer offset: *optional* The offset in incoming data to start to get
1432 data. 0 by default. May be negative to be relative to
1433 the end of incoming data.
1434 :param integer length: *optional* The expected length of data to retrieve. All
1435 incoming data by default. May be set to -1 to get a
1436 maximum of data.
1437 :returns: a string containing the data found or nil.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001438
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001439.. js:function:: Channel.forward(channel, length)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001440
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001441 This function forwards **length** bytes of data from the channel buffer. If
Christopher Faulet5a2c6612021-08-15 20:35:25 +02001442 the requested length exceeds the available amount of incoming data, and if
1443 called by an action, the function yields, waiting for more data to forward. It
1444 returns the amount of data forwarded.
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001445
1446 :param class_channel channel: The manipulated Channel.
1447 :param integer int: The amount of data to forward.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001448
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001449.. js:function:: Channel.input(channel)
1450
Christopher Faulet5a2c6612021-08-15 20:35:25 +02001451 This function returns the length of incoming data in the channel buffer. When
1452 called by a filter, this value is relative to the filter.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001453
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001454 :param class_channel channel: The manipulated Channel.
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001455 :returns: an integer containing the amount of available bytes.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001456
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001457.. js:function:: Channel.insert(channel, string [, offset])
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001458
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001459 This function copies the string **string** at the offset **offset** in
1460 incoming data of the channel buffer. The function returns the copied length on
1461 success or -1 if data cannot be copied.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001462
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001463 By default, if no offset is provided, the string is copied in front of
Ilya Shipitsinff0f2782021-08-22 22:18:07 +05001464 incoming data. A positive offset is relative to the beginning of incoming data
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001465 of the channel buffer while negative offset is relative to their end.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001466
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001467 :param class_channel channel: The manipulated Channel.
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02001468 :param string string: The data to copy into incoming data.
1469 :param integer offset: *optional* The offset in incoming data where to copy
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001470 data. 0 by default. May be negative to be relative to
1471 the end of incoming data.
Pieter Baauw4d7f7662015-11-08 16:38:08 +01001472 :returns: an integer containing the amount of bytes copied or -1.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001473
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001474.. js:function:: Channel.is_full(channel)
1475
1476 This function returns true if the channel buffer is full.
1477
Christopher Faulet5a2c6612021-08-15 20:35:25 +02001478 :param class_channel channel: The manipulated Channel.
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001479 :returns: a boolean
1480
1481.. js:function:: Channel.is_resp(channel)
1482
1483 This function returns true if the channel is the response one.
1484
Christopher Faulet5a2c6612021-08-15 20:35:25 +02001485 :param class_channel channel: The manipulated Channel.
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001486 :returns: a boolean
1487
1488.. js:function:: Channel.line(channel [, offset [, length]])
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001489
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001490 This function parses **length** bytes of incoming data of the channel buffer,
1491 starting at offset **offset**, and returns the first line found, including the
Christopher Faulet5a2c6612021-08-15 20:35:25 +02001492 '\\n'. The data are not removed from the buffer. If no line is found, all data
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001493 are returned.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001494
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001495 By default, if no length is provided, all incoming data, starting at the given
1496 offset, are evaluated. If **length** is set to -1, the function tries to
Christopher Faulet5a2c6612021-08-15 20:35:25 +02001497 retrieve a maximum of data and, if called by an action, yields if
1498 necessary. It also waits for more data if the requested length exceeds the
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02001499 available amount of incoming data. Not providing an offset is the same as
Ilya Shipitsinff0f2782021-08-22 22:18:07 +05001500 setting it to 0. A positive offset is relative to the beginning of incoming
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02001501 data of the channel buffer while negative offset is relative to the end.
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001502
1503 If there is no incoming data and the channel can't receive more data, a 'nil'
1504 value is returned.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001505
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001506 :param class_channel channel: The manipulated Channel.
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02001507 :param integer offset: *optional* The offset in incoming data to start to
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001508 parse data. 0 by default. May be negative to be
1509 relative to the end of incoming data.
1510 :param integer length: *optional* The length of data to parse. All incoming
1511 data by default. May be set to -1 to get a maximum of
1512 data.
1513 :returns: a string containing the line found or nil.
1514
1515.. js:function:: Channel.may_recv(channel)
1516
1517 This function returns true if the channel may still receive data.
1518
Christopher Faulet5a2c6612021-08-15 20:35:25 +02001519 :param class_channel channel: The manipulated Channel.
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001520 :returns: a boolean
1521
1522.. js:function:: Channel.output(channel)
1523
Christopher Faulet5a2c6612021-08-15 20:35:25 +02001524 This function returns the length of outgoing data of the channel buffer. When
1525 called by a filter, this value is relative to the filter.
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001526
1527 :param class_channel channel: The manipulated Channel.
1528 :returns: an integer containing the amount of available bytes.
1529
1530.. js:function:: Channel.prepend(channel, string)
1531
1532 This function copies the string **string** in front of incoming data of the
1533 channel buffer. The function returns the copied length on success or -1 if
1534 data cannot be copied.
1535
1536 Same that :js:func:`Channel.insert(channel, string, 0)`.
1537
1538 :param class_channel channel: The manipulated Channel.
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02001539 :param string string: The data to copy in front of incoming data.
Pieter Baauw4d7f7662015-11-08 16:38:08 +01001540 :returns: an integer containing the amount of bytes copied or -1.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001541
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001542.. js:function:: Channel.remove(channel [, offset [, length]])
1543
1544 This function removes **length** bytes of incoming data of the channel buffer,
1545 starting at offset **offset**. This function returns number of bytes removed
1546 on success.
1547
1548 By default, if no length is provided, all incoming data, starting at the given
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02001549 offset, are removed. Not providing an offset is the same as setting it
Ilya Shipitsinff0f2782021-08-22 22:18:07 +05001550 to 0. A positive offset is relative to the beginning of incoming data of the
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02001551 channel buffer while negative offset is relative to the end.
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001552
1553 :param class_channel channel: The manipulated Channel.
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02001554 :param integer offset: *optional* The offset in incoming data where to start
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001555 to remove data. 0 by default. May be negative to
1556 be relative to the end of incoming data.
1557 :param integer length: *optional* The length of data to remove. All incoming
1558 data by default.
1559 :returns: an integer containing the amount of bytes removed.
1560
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001561.. js:function:: Channel.send(channel, string)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001562
Christopher Faulet5a2c6612021-08-15 20:35:25 +02001563 This function requires immediate send of the string **string**. It means the
Ilya Shipitsinff0f2782021-08-22 22:18:07 +05001564 string is copied at the beginning of incoming data of the channel buffer and
Christopher Faulet5a2c6612021-08-15 20:35:25 +02001565 immediately forwarded. Unless if the connection is close, and if called by an
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02001566 action, this function yields to copy and forward all the string.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001567
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001568 :param class_channel channel: The manipulated Channel.
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001569 :param string string: The data to send.
Pieter Baauw4d7f7662015-11-08 16:38:08 +01001570 :returns: an integer containing the amount of bytes copied or -1.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001571
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001572.. js:function:: Channel.set(channel, string [, offset [, length]])
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001573
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02001574 This function replaces **length** bytes of incoming data of the channel buffer,
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001575 starting at offset **offset**, by the string **string**. The function returns
1576 the copied length on success or -1 if data cannot be copied.
1577
1578 By default, if no length is provided, all incoming data, starting at the given
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02001579 offset, are replaced. Not providing an offset is the same as setting it
Ilya Shipitsinff0f2782021-08-22 22:18:07 +05001580 to 0. A positive offset is relative to the beginning of incoming data of the
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02001581 channel buffer while negative offset is relative to the end.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001582
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001583 :param class_channel channel: The manipulated Channel.
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02001584 :param string string: The data to copy into incoming data.
1585 :param integer offset: *optional* The offset in incoming data where to start
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001586 the data replacement. 0 by default. May be negative to
1587 be relative to the end of incoming data.
1588 :param integer length: *optional* The length of data to replace. All incoming
1589 data by default.
1590 :returns: an integer containing the amount of bytes copied or -1.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001591
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001592.. js:function:: Channel.dup(channel)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001593
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001594 **DEPRECATED**
1595
1596 This function returns all incoming data found in the channel buffer. The data
Boyang Li60cfe8b2022-05-10 18:11:00 +00001597 are not removed from the buffer and can be reprocessed later.
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001598
1599 If there is no incoming data and the channel can't receive more data, a 'nil'
1600 value is returned.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001601
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001602 :param class_channel channel: The manipulated Channel.
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001603 :returns: a string containing all data found or nil.
1604
1605 .. warning::
1606 This function is deprecated. :js:func:`Channel.data()` must be used
1607 instead.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001608
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001609.. js:function:: Channel.get(channel)
1610
1611 **DEPRECATED**
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001612
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001613 This function returns all incoming data found in the channel buffer and remove
1614 them from the buffer.
1615
1616 If there is no incoming data and the channel can't receive more data, a 'nil'
1617 value is returned.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001618
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001619 :param class_channel channel: The manipulated Channel.
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001620 :returns: a string containing all the data found or nil.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001621
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001622 .. warning::
1623 This function is deprecated. :js:func:`Channel.data()` must be used to
1624 retrieve data followed by a call to :js:func:`Channel:remove()` to remove
1625 data.
Thierry FOURNIER / OZON.IO65192f32016-11-07 15:28:40 +01001626
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001627 .. code-block:: lua
Thierry FOURNIER / OZON.IO65192f32016-11-07 15:28:40 +01001628
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001629 local data = chn:data()
1630 chn:remove(0, data:len())
1631
1632 ..
1633
1634.. js:function:: Channel.getline(channel)
1635
1636 **DEPRECATED**
1637
1638 This function returns the first line found in incoming data of the channel
Christopher Faulet5a2c6612021-08-15 20:35:25 +02001639 buffer, including the '\\n'. The returned data are removed from the buffer. If
1640 no line is found, and if called by an action, this function yields to wait for
1641 more data, except if the channel can't receive more data. In this case all
1642 data are returned.
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001643
1644 If there is no incoming data and the channel can't receive more data, a 'nil'
1645 value is returned.
1646
1647 :param class_channel channel: The manipulated Channel.
1648 :returns: a string containing the line found or nil.
1649
1650 .. warning::
Boyang Li60cfe8b2022-05-10 18:11:00 +00001651 This function is deprecated. :js:func:`Channel.line()` must be used to
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001652 retrieve a line followed by a call to :js:func:`Channel:remove()` to remove
1653 data.
1654
1655 .. code-block:: lua
1656
1657 local line = chn:line(0, -1)
1658 chn:remove(0, line:len())
1659
1660 ..
1661
1662.. js:function:: Channel.get_in_len(channel)
1663
Boyang Li60cfe8b2022-05-10 18:11:00 +00001664 **DEPRECATED**
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001665
Christopher Faulet5a2c6612021-08-15 20:35:25 +02001666 This function returns the length of the input part of the buffer. When called
1667 by a filter, this value is relative to the filter.
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001668
1669 :param class_channel channel: The manipulated Channel.
1670 :returns: an integer containing the amount of available bytes.
1671
1672 .. warning::
1673 This function is deprecated. :js:func:`Channel.input()` must be used
1674 instead.
1675
1676.. js:function:: Channel.get_out_len(channel)
1677
Boyang Li60cfe8b2022-05-10 18:11:00 +00001678 **DEPRECATED**
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001679
Christopher Faulet5a2c6612021-08-15 20:35:25 +02001680 This function returns the length of the output part of the buffer. When called
1681 by a filter, this value is relative to the filter.
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001682
1683 :param class_channel channel: The manipulated Channel.
1684 :returns: an integer containing the amount of available bytes.
1685
1686 .. warning::
1687 This function is deprecated. :js:func:`Channel.output()` must be used
1688 instead.
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001689
1690.. _http_class:
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001691
1692HTTP class
1693==========
1694
1695.. js:class:: HTTP
1696
1697 This class contain all the HTTP manipulation functions.
1698
Pieter Baauw386a1272015-08-16 15:26:24 +02001699.. js:function:: HTTP.req_get_headers(http)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001700
Patrick Hemmerc6a1d712018-05-01 21:30:41 -04001701 Returns a table containing all the request headers.
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001702
1703 :param class_http http: The related http object.
Patrick Hemmerc6a1d712018-05-01 21:30:41 -04001704 :returns: table of headers.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01001705 :see: :js:func:`HTTP.res_get_headers`
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001706
Patrick Hemmerc6a1d712018-05-01 21:30:41 -04001707 This is the form of the returned table:
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001708
1709.. code-block:: lua
1710
1711 HTTP:req_get_headers()['<header-name>'][<header-index>] = "<header-value>"
1712
1713 local hdr = HTTP:req_get_headers()
1714 hdr["host"][0] = "www.test.com"
1715 hdr["accept"][0] = "audio/basic q=1"
1716 hdr["accept"][1] = "audio/*, q=0.2"
1717 hdr["accept"][2] = "*/*, q=0.1"
1718..
1719
Pieter Baauw386a1272015-08-16 15:26:24 +02001720.. js:function:: HTTP.res_get_headers(http)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001721
Patrick Hemmerc6a1d712018-05-01 21:30:41 -04001722 Returns a table containing all the response headers.
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001723
1724 :param class_http http: The related http object.
Patrick Hemmerc6a1d712018-05-01 21:30:41 -04001725 :returns: table of headers.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01001726 :see: :js:func:`HTTP.req_get_headers`
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001727
Patrick Hemmerc6a1d712018-05-01 21:30:41 -04001728 This is the form of the returned table:
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001729
1730.. code-block:: lua
1731
1732 HTTP:res_get_headers()['<header-name>'][<header-index>] = "<header-value>"
1733
1734 local hdr = HTTP:req_get_headers()
1735 hdr["host"][0] = "www.test.com"
1736 hdr["accept"][0] = "audio/basic q=1"
1737 hdr["accept"][1] = "audio/*, q=0.2"
1738 hdr["accept"][2] = "*.*, q=0.1"
1739..
1740
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001741.. js:function:: HTTP.req_add_header(http, name, value)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001742
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02001743 Appends a HTTP header field in the request whose name is
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001744 specified in "name" and whose value is defined in "value".
1745
1746 :param class_http http: The related http object.
1747 :param string name: The header name.
1748 :param string value: The header value.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01001749 :see: :js:func:`HTTP.res_add_header`
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001750
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001751.. js:function:: HTTP.res_add_header(http, name, value)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001752
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02001753 Appends a HTTP header field in the response whose name is
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001754 specified in "name" and whose value is defined in "value".
1755
1756 :param class_http http: The related http object.
1757 :param string name: The header name.
1758 :param string value: The header value.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01001759 :see: :js:func:`HTTP.req_add_header`
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001760
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001761.. js:function:: HTTP.req_del_header(http, name)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001762
1763 Removes all HTTP header fields in the request whose name is
1764 specified in "name".
1765
1766 :param class_http http: The related http object.
1767 :param string name: The header name.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01001768 :see: :js:func:`HTTP.res_del_header`
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001769
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001770.. js:function:: HTTP.res_del_header(http, name)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001771
1772 Removes all HTTP header fields in the response whose name is
1773 specified in "name".
1774
1775 :param class_http http: The related http object.
1776 :param string name: The header name.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01001777 :see: :js:func:`HTTP.req_del_header`
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001778
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001779.. js:function:: HTTP.req_set_header(http, name, value)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001780
Bertrand Jacquin874a35c2018-09-10 21:26:07 +01001781 This variable replace all occurrence of all header "name", by only
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001782 one containing the "value".
1783
1784 :param class_http http: The related http object.
1785 :param string name: The header name.
1786 :param string value: The header value.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01001787 :see: :js:func:`HTTP.res_set_header`
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001788
Bertrand Jacquin874a35c2018-09-10 21:26:07 +01001789 This function does the same work as the following code:
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001790
1791.. code-block:: lua
1792
1793 function fcn(txn)
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001794 TXN.http:req_del_header("header")
1795 TXN.http:req_add_header("header", "value")
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001796 end
1797..
1798
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001799.. js:function:: HTTP.res_set_header(http, name, value)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001800
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02001801 This function replaces all occurrence of all header "name", by only
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001802 one containing the "value".
1803
1804 :param class_http http: The related http object.
1805 :param string name: The header name.
1806 :param string value: The header value.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01001807 :see: :js:func:`HTTP.req_rep_header()`
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001808
Pieter Baauw386a1272015-08-16 15:26:24 +02001809.. js:function:: HTTP.req_rep_header(http, name, regex, replace)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001810
1811 Matches the regular expression in all occurrences of header field "name"
1812 according to "regex", and replaces them with the "replace" argument. The
1813 replacement value can contain back references like \1, \2, ... This
1814 function works with the request.
1815
1816 :param class_http http: The related http object.
1817 :param string name: The header name.
1818 :param string regex: The match regular expression.
1819 :param string replace: The replacement value.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01001820 :see: :js:func:`HTTP.res_rep_header()`
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001821
Pieter Baauw386a1272015-08-16 15:26:24 +02001822.. js:function:: HTTP.res_rep_header(http, name, regex, string)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001823
1824 Matches the regular expression in all occurrences of header field "name"
1825 according to "regex", and replaces them with the "replace" argument. The
1826 replacement value can contain back references like \1, \2, ... This
1827 function works with the request.
1828
1829 :param class_http http: The related http object.
1830 :param string name: The header name.
1831 :param string regex: The match regular expression.
1832 :param string replace: The replacement value.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01001833 :see: :js:func:`HTTP.req_rep_header()`
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001834
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001835.. js:function:: HTTP.req_set_method(http, method)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001836
1837 Rewrites the request method with the parameter "method".
1838
1839 :param class_http http: The related http object.
1840 :param string method: The new method.
1841
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001842.. js:function:: HTTP.req_set_path(http, path)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001843
1844 Rewrites the request path with the "path" parameter.
1845
1846 :param class_http http: The related http object.
1847 :param string path: The new path.
1848
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001849.. js:function:: HTTP.req_set_query(http, query)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001850
1851 Rewrites the request's query string which appears after the first question
1852 mark ("?") with the parameter "query".
1853
1854 :param class_http http: The related http object.
1855 :param string query: The new query.
1856
Thierry FOURNIER0d79cf62015-08-26 14:20:58 +02001857.. js:function:: HTTP.req_set_uri(http, uri)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001858
1859 Rewrites the request URI with the parameter "uri".
1860
1861 :param class_http http: The related http object.
1862 :param string uri: The new uri.
1863
Robin H. Johnson52f5db22017-01-01 13:10:52 -08001864.. js:function:: HTTP.res_set_status(http, status [, reason])
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +02001865
Robin H. Johnson52f5db22017-01-01 13:10:52 -08001866 Rewrites the response status code with the parameter "code".
1867
1868 If no custom reason is provided, it will be generated from the status.
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +02001869
1870 :param class_http http: The related http object.
1871 :param integer status: The new response status code.
Robin H. Johnson52f5db22017-01-01 13:10:52 -08001872 :param string reason: The new response reason (optional).
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +02001873
William Lallemand00a15022021-11-19 16:02:44 +01001874.. _httpclient_class:
1875
1876HTTPClient class
1877================
1878
1879.. js:class:: HTTPClient
1880
1881 The httpclient class allows issue of outbound HTTP requests through a simple
1882 API without the knowledge of HAProxy internals.
1883
1884.. js:function:: HTTPClient.get(httpclient, request)
1885.. js:function:: HTTPClient.head(httpclient, request)
1886.. js:function:: HTTPClient.put(httpclient, request)
1887.. js:function:: HTTPClient.post(httpclient, request)
1888.. js:function:: HTTPClient.delete(httpclient, request)
1889
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02001890 Send a HTTP request and wait for a response. GET, HEAD PUT, POST and DELETE methods can be used.
1891 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 +01001892
William Lallemanda9256192022-10-21 11:48:24 +02001893 The HTTPClient interface is not able to decompress responses, it is not
1894 recommended to send an Accept-Encoding in the request so the response is
1895 received uncompressed.
William Lallemand00a15022021-11-19 16:02:44 +01001896
1897 :param class httpclient: Is the manipulated HTTPClient.
1898 :param table request: Is a table containing the parameters of the request that will be send.
1899 :param string request.url: Is a mandatory parameter for the request that contains the URL.
1900 :param string request.body: Is an optional parameter for the request that contains the body to send.
1901 :param table request.headers: Is an optional parameter for the request that contains the headers to send.
William Lallemand18340302022-02-23 15:57:45 +01001902 :param string request.dst: Is an optional parameter for the destination in haproxy address format.
William Lallemandb4a4ef62022-02-23 14:18:16 +01001903 :param integer request.timeout: Optional timeout parameter, set a "timeout server" on the connections.
William Lallemand00a15022021-11-19 16:02:44 +01001904 :returns: Lua table containing the response
1905
1906
1907.. code-block:: lua
1908
1909 local httpclient = core.httpclient()
William Lallemand4f4f2b72022-02-17 20:00:23 +01001910 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 +01001911
1912..
1913
1914.. code-block:: lua
1915
1916 response = {
1917 status = 400,
1918 reason = "Bad request",
1919 headers = {
1920 ["content-type"] = { "text/html" },
1921 ["cache-control"] = { "no-cache", "no-store" },
1922 },
William Lallemand4f4f2b72022-02-17 20:00:23 +01001923 body = "<html><body><h1>invalid request<h1></body></html>",
William Lallemand00a15022021-11-19 16:02:44 +01001924 }
1925..
1926
1927
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001928.. _txn_class:
1929
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001930TXN class
1931=========
1932
1933.. js:class:: TXN
1934
1935 The txn class contain all the functions relative to the http or tcp
1936 transaction (Note than a tcp stream is the same than a tcp transaction, but
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02001937 a HTTP transaction is not the same than a tcp stream).
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001938
1939 The usage of this class permits to retrieve data from the requests, alter it
1940 and forward it.
1941
1942 All the functions provided by this class are available in the context
Christopher Faulet5a2c6612021-08-15 20:35:25 +02001943 **sample-fetches**, **actions** and **filters**.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001944
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001945.. js:attribute:: TXN.c
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001946
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001947 :returns: An :ref:`converters_class`.
1948
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001949 This attribute contains a Converters class object.
1950
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001951.. js:attribute:: TXN.sc
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001952
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001953 :returns: An :ref:`converters_class`.
1954
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001955 This attribute contains a Converters class object. The functions of
1956 this object returns always a string.
1957
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001958.. js:attribute:: TXN.f
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001959
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001960 :returns: An :ref:`fetches_class`.
1961
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001962 This attribute contains a Fetches class object.
1963
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001964.. js:attribute:: TXN.sf
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001965
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001966 :returns: An :ref:`fetches_class`.
1967
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001968 This attribute contains a Fetches class object. The functions of
1969 this object returns always a string.
1970
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001971.. js:attribute:: TXN.req
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001972
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001973 :returns: An :ref:`channel_class`.
1974
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001975 This attribute contains a channel class object for the request buffer.
1976
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001977.. js:attribute:: TXN.res
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001978
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001979 :returns: An :ref:`channel_class`.
1980
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001981 This attribute contains a channel class object for the response buffer.
1982
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001983.. js:attribute:: TXN.http
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001984
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001985 :returns: An :ref:`http_class`.
1986
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02001987 This attribute contains a HTTP class object. It is available only if the
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001988 proxy has the "mode http" enabled.
1989
Christopher Faulet5a2c6612021-08-15 20:35:25 +02001990.. js:attribute:: TXN.http_req
1991
1992 :returns: An :ref:`httpmessage_class`.
1993
1994 This attribute contains the request HTTPMessage class object. It is available
1995 only if the proxy has the "mode http" enabled and only in the **filters**
1996 context.
1997
1998.. js:attribute:: TXN.http_res
1999
2000 :returns: An :ref:`httpmessage_class`.
2001
2002 This attribute contains the response HTTPMessage class object. It is available
2003 only if the proxy has the "mode http" enabled and only in the **filters**
2004 context.
2005
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01002006.. js:function:: TXN.log(TXN, loglevel, msg)
2007
2008 This function sends a log. The log is sent, according with the HAProxy
2009 configuration file, on the default syslog server if it is configured and on
2010 the stderr if it is allowed.
2011
2012 :param class_txn txn: The class txn object containing the data.
Bertrand Jacquin874a35c2018-09-10 21:26:07 +01002013 :param integer loglevel: Is the log level associated with the message. It is a
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01002014 number between 0 and 7.
2015 :param string msg: The log content.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01002016 :see: :js:attr:`core.emerg`, :js:attr:`core.alert`, :js:attr:`core.crit`,
2017 :js:attr:`core.err`, :js:attr:`core.warning`, :js:attr:`core.notice`,
2018 :js:attr:`core.info`, :js:attr:`core.debug` (log level definitions)
2019 :see: :js:func:`TXN.deflog`
2020 :see: :js:func:`TXN.Debug`
2021 :see: :js:func:`TXN.Info`
2022 :see: :js:func:`TXN.Warning`
2023 :see: :js:func:`TXN.Alert`
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01002024
2025.. js:function:: TXN.deflog(TXN, msg)
2026
Bertrand Jacquin874a35c2018-09-10 21:26:07 +01002027 Sends a log line with the default loglevel for the proxy associated with the
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01002028 transaction.
2029
2030 :param class_txn txn: The class txn object containing the data.
2031 :param string msg: The log content.
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02002032 :see: :js:func:`TXN.log`
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01002033
2034.. js:function:: TXN.Debug(txn, msg)
2035
2036 :param class_txn txn: The class txn object containing the data.
2037 :param string msg: The log content.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01002038 :see: :js:func:`TXN.log`
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01002039
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02002040 Does the same job as:
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01002041
2042.. code-block:: lua
2043
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002044 function Debug(txn, msg)
2045 TXN.log(txn, core.debug, msg)
2046 end
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01002047..
2048
2049.. js:function:: TXN.Info(txn, msg)
2050
2051 :param class_txn txn: The class txn object containing the data.
2052 :param string msg: The log content.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01002053 :see: :js:func:`TXN.log`
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01002054
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02002055 Does the same job as:
2056
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01002057.. code-block:: lua
2058
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02002059 function Info(txn, msg)
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002060 TXN.log(txn, core.info, msg)
2061 end
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01002062..
2063
2064.. js:function:: TXN.Warning(txn, msg)
2065
2066 :param class_txn txn: The class txn object containing the data.
2067 :param string msg: The log content.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01002068 :see: :js:func:`TXN.log`
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01002069
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02002070 Does the same job as:
2071
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01002072.. code-block:: lua
2073
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02002074 function Warning(txn, msg)
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002075 TXN.log(txn, core.warning, msg)
2076 end
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01002077..
2078
2079.. js:function:: TXN.Alert(txn, msg)
2080
2081 :param class_txn txn: The class txn object containing the data.
2082 :param string msg: The log content.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01002083 :see: :js:func:`TXN.log`
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01002084
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02002085 Does the same job as:
2086
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01002087.. code-block:: lua
2088
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02002089 function Alert(txn, msg)
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002090 TXN.log(txn, core.alert, msg)
2091 end
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01002092..
2093
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002094.. js:function:: TXN.get_priv(txn)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002095
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002096 Return Lua data stored in the current transaction (with the `TXN.set_priv()`)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002097 function. If no data are stored, it returns a nil value.
2098
2099 :param class_txn txn: The class txn object containing the data.
Bertrand Jacquin874a35c2018-09-10 21:26:07 +01002100 :returns: the opaque data previously stored, or nil if nothing is
2101 available.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002102
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002103.. js:function:: TXN.set_priv(txn, data)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002104
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02002105 Store any data in the current HAProxy transaction. This action replaces the
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002106 old stored data.
2107
2108 :param class_txn txn: The class txn object containing the data.
2109 :param opaque data: The data which is stored in the transaction.
2110
Tim Duesterhus4e172c92020-05-19 13:49:42 +02002111.. js:function:: TXN.set_var(TXN, var, value[, ifexist])
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +02002112
David Carlier61fdf8b2015-10-02 11:59:38 +01002113 Converts a Lua type in a HAProxy type and store it in a variable <var>.
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +02002114
2115 :param class_txn txn: The class txn object containing the data.
2116 :param string var: The variable name according with the HAProxy variable syntax.
Thierry FOURNIER / OZON.IOb210bcc2016-12-12 16:24:16 +01002117 :param type value: The value associated to the variable. The type can be string or
2118 integer.
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02002119 :param boolean ifexist: If this parameter is set to true the variable
Tim Duesterhus4e172c92020-05-19 13:49:42 +02002120 will only be set if it was defined elsewhere (i.e. used
Willy Tarreau7978c5c2021-09-07 14:24:07 +02002121 within the configuration). For global variables (using the
2122 "proc" scope), they will only be updated and never created.
2123 It is highly recommended to always set this to true.
Christopher Faulet85d79c92016-11-09 16:54:56 +01002124
2125.. js:function:: TXN.unset_var(TXN, var)
2126
2127 Unset the variable <var>.
2128
2129 :param class_txn txn: The class txn object containing the data.
2130 :param string var: The variable name according with the HAProxy variable syntax.
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +02002131
2132.. js:function:: TXN.get_var(TXN, var)
2133
2134 Returns data stored in the variable <var> converter in Lua type.
2135
2136 :param class_txn txn: The class txn object containing the data.
2137 :param string var: The variable name according with the HAProxy variable syntax.
2138
Christopher Faulet700d9e82020-01-31 12:21:52 +01002139.. js:function:: TXN.reply([reply])
2140
2141 Return a new reply object
2142
2143 :param table reply: A table containing info to initialize the reply fields.
2144 :returns: A :ref:`reply_class` object.
2145
2146 The table used to initialized the reply object may contain following entries :
2147
2148 * status : The reply status code. the code 200 is used by default.
2149 * reason : The reply reason. The reason corresponding to the status code is
2150 used by default.
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02002151 * headers : A list of headers, indexed by header name. Empty by default. For
Christopher Faulet700d9e82020-01-31 12:21:52 +01002152 a given name, multiple values are possible, stored in an ordered list.
2153 * body : The reply body, empty by default.
2154
2155.. code-block:: lua
2156
2157 local reply = txn:reply{
2158 status = 400,
2159 reason = "Bad request",
2160 headers = {
2161 ["content-type"] = { "text/html" },
2162 ["cache-control"] = {"no-cache", "no-store" }
2163 },
2164 body = "<html><body><h1>invalid request<h1></body></html>"
2165 }
2166..
2167 :see: :js:class:`Reply`
2168
2169.. js:function:: TXN.done(txn[, reply])
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002170
Willy Tarreaubc183a62015-08-28 10:39:11 +02002171 This function terminates processing of the transaction and the associated
Christopher Faulet700d9e82020-01-31 12:21:52 +01002172 session and optionally reply to the client for HTTP sessions.
2173
2174 :param class_txn txn: The class txn object containing the data.
2175 :param class_reply reply: The class reply object to return to the client.
2176
2177 This functions can be used when a critical error is detected or to terminate
Willy Tarreaubc183a62015-08-28 10:39:11 +02002178 processing after some data have been returned to the client (eg: a redirect).
Christopher Faulet700d9e82020-01-31 12:21:52 +01002179 To do so, a reply may be provided. This object is optional and may contain a
2180 status code, a reason, a header list and a body. All these fields are
Christopher Faulet7855b192021-11-09 18:39:51 +01002181 optional. When not provided, the default values are used. By default, with an
2182 empty reply object, an empty HTTP 200 response is returned to the client. If
2183 no reply object is provided, the transaction is terminated without any
2184 reply. If a reply object is provided, it must not exceed the buffer size once
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02002185 converted into the internal HTTP representation. Because for now there is no
Christopher Faulet7855b192021-11-09 18:39:51 +01002186 easy way to be sure it fits, it is probably better to keep it reasonably
2187 small.
Christopher Faulet700d9e82020-01-31 12:21:52 +01002188
2189 The reply object may be fully created in lua or the class Reply may be used to
2190 create it.
2191
2192.. code-block:: lua
2193
2194 local reply = txn:reply()
2195 reply:set_status(400, "Bad request")
2196 reply:add_header("content-type", "text/html")
2197 reply:add_header("cache-control", "no-cache")
2198 reply:add_header("cache-control", "no-store")
2199 reply:set_body("<html><body><h1>invalid request<h1></body></html>")
2200 txn:done(reply)
2201..
2202
2203.. code-block:: lua
2204
2205 txn:done{
2206 status = 400,
2207 reason = "Bad request",
2208 headers = {
2209 ["content-type"] = { "text/html" },
2210 ["cache-control"] = { "no-cache", "no-store" },
2211 },
2212 body = "<html><body><h1>invalid request<h1></body></html>"
2213 }
2214..
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002215
Christopher Faulet1e9b1b62021-08-11 10:14:30 +02002216 .. warning::
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02002217 It does not make sense to call this function from sample-fetches. In this case
2218 the behavior is the same than core.done(): it finishes the Lua
Christopher Faulet1e9b1b62021-08-11 10:14:30 +02002219 execution. The transaction is really aborted only from an action registered
2220 function.
Thierry FOURNIERab00df62016-07-14 11:42:37 +02002221
Christopher Faulet700d9e82020-01-31 12:21:52 +01002222 :see: :js:func:`TXN.reply`, :js:class:`Reply`
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002223
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002224.. js:function:: TXN.set_loglevel(txn, loglevel)
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01002225
2226 Is used to change the log level of the current request. The "loglevel" must
2227 be an integer between 0 and 7.
2228
2229 :param class_txn txn: The class txn object containing the data.
2230 :param integer loglevel: The required log level. This variable can be one of
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01002231 :see: :js:attr:`core.emerg`, :js:attr:`core.alert`, :js:attr:`core.crit`,
2232 :js:attr:`core.err`, :js:attr:`core.warning`, :js:attr:`core.notice`,
2233 :js:attr:`core.info`, :js:attr:`core.debug` (log level definitions)
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01002234
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002235.. js:function:: TXN.set_tos(txn, tos)
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01002236
2237 Is used to set the TOS or DSCP field value of packets sent to the client to
2238 the value passed in "tos" on platforms which support this.
2239
2240 :param class_txn txn: The class txn object containing the data.
2241 :param integer tos: The new TOS os DSCP.
2242
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002243.. js:function:: TXN.set_mark(txn, mark)
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01002244
2245 Is used to set the Netfilter MARK on all packets sent to the client to the
2246 value passed in "mark" on platforms which support it.
2247
2248 :param class_txn txn: The class txn object containing the data.
2249 :param integer mark: The mark value.
2250
Patrick Hemmer268a7072018-05-11 12:52:31 -04002251.. js:function:: TXN.set_priority_class(txn, prio)
2252
2253 This function adjusts the priority class of the transaction. The value should
2254 be within the range -2047..2047. Values outside this range will be
2255 truncated.
2256
2257 See the HAProxy configuration.txt file keyword "http-request" action
2258 "set-priority-class" for details.
2259
2260.. js:function:: TXN.set_priority_offset(txn, prio)
2261
2262 This function adjusts the priority offset of the transaction. The value
2263 should be within the range -524287..524287. Values outside this range will be
2264 truncated.
2265
2266 See the HAProxy configuration.txt file keyword "http-request" action
2267 "set-priority-offset" for details.
2268
Christopher Faulet700d9e82020-01-31 12:21:52 +01002269.. _reply_class:
2270
2271Reply class
2272============
2273
2274.. js:class:: Reply
2275
2276 **context**: action
2277
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02002278 This class represents a HTTP response message. It provides some methods to
Christopher Faulet7855b192021-11-09 18:39:51 +01002279 enrich it. Once converted into the internal HTTP representation, the response
2280 message must not exceed the buffer size. Because for now there is no
2281 easy way to be sure it fits, it is probably better to keep it reasonably
2282 small.
2283
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02002284 See tune.bufsize in the configuration manual for details.
Christopher Faulet700d9e82020-01-31 12:21:52 +01002285
2286.. code-block:: lua
2287
2288 local reply = txn:reply({status = 400}) -- default HTTP 400 reason-phase used
2289 reply:add_header("content-type", "text/html")
2290 reply:add_header("cache-control", "no-cache")
2291 reply:add_header("cache-control", "no-store")
2292 reply:set_body("<html><body><h1>invalid request<h1></body></html>")
2293..
2294
2295 :see: :js:func:`TXN.reply`
2296
2297.. js:attribute:: Reply.status
2298
2299 The reply status code. By default, the status code is set to 200.
2300
2301 :returns: integer
2302
2303.. js:attribute:: Reply.reason
2304
2305 The reason string describing the status code.
2306
2307 :returns: string
2308
2309.. js:attribute:: Reply.headers
2310
2311 A table indexing all reply headers by name. To each name is associated an
2312 ordered list of values.
2313
2314 :returns: Lua table
2315
2316.. code-block:: lua
2317
2318 {
2319 ["content-type"] = { "text/html" },
2320 ["cache-control"] = {"no-cache", "no-store" },
2321 x_header_name = { "value1", "value2", ... }
2322 ...
2323 }
2324..
2325
2326.. js:attribute:: Reply.body
2327
2328 The reply payload.
2329
2330 :returns: string
2331
2332.. js:function:: Reply.set_status(REPLY, status[, reason])
2333
2334 Set the reply status code and optionally the reason-phrase. If the reason is
2335 not provided, the default reason corresponding to the status code is used.
2336
2337 :param class_reply reply: The related Reply object.
2338 :param integer status: The reply status code.
2339 :param string reason: The reply status reason (optional).
2340
2341.. js:function:: Reply.add_header(REPLY, name, value)
2342
2343 Add a header to the reply object. If the header does not already exist, a new
2344 entry is created with its name as index and a one-element list containing its
2345 value as value. Otherwise, the header value is appended to the ordered list of
2346 values associated to the header name.
2347
2348 :param class_reply reply: The related Reply object.
2349 :param string name: The header field name.
2350 :param string value: The header field value.
2351
2352.. js:function:: Reply.del_header(REPLY, name)
2353
2354 Remove all occurrences of a header name from the reply object.
2355
2356 :param class_reply reply: The related Reply object.
2357 :param string name: The header field name.
2358
2359.. js:function:: Reply.set_body(REPLY, body)
2360
2361 Set the reply payload.
2362
2363 :param class_reply reply: The related Reply object.
2364 :param string body: The reply payload.
2365
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002366.. _socket_class:
2367
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002368Socket class
2369============
2370
2371.. js:class:: Socket
2372
2373 This class must be compatible with the Lua Socket class. Only the 'client'
2374 functions are available. See the Lua Socket documentation:
2375
2376 `http://w3.impa.br/~diego/software/luasocket/tcp.html
2377 <http://w3.impa.br/~diego/software/luasocket/tcp.html>`_
2378
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002379.. js:function:: Socket.close(socket)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002380
2381 Closes a TCP object. The internal socket used by the object is closed and the
2382 local address to which the object was bound is made available to other
2383 applications. No further operations (except for further calls to the close
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002384 method) are allowed on a closed Socket.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002385
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002386 :param class_socket socket: Is the manipulated Socket.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002387
2388 Note: It is important to close all used sockets once they are not needed,
2389 since, in many systems, each socket uses a file descriptor, which are limited
2390 system resources. Garbage-collected objects are automatically closed before
2391 destruction, though.
2392
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002393.. js:function:: Socket.connect(socket, address[, port])
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002394
2395 Attempts to connect a socket object to a remote host.
2396
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002397
2398 In case of error, the method returns nil followed by a string describing the
2399 error. In case of success, the method returns 1.
2400
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002401 :param class_socket socket: Is the manipulated Socket.
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002402 :param string address: can be an IP address or a host name. See below for more
2403 information.
2404 :param integer port: must be an integer number in the range [1..64K].
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002405 :returns: 1 or nil.
2406
Bertrand Jacquin874a35c2018-09-10 21:26:07 +01002407 An address field extension permits to use the connect() function to connect to
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002408 other stream than TCP. The syntax containing a simpleipv4 or ipv6 address is
2409 the basically expected format. This format requires the port.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002410
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002411 Other format accepted are a socket path like "/socket/path", it permits to
Bertrand Jacquin874a35c2018-09-10 21:26:07 +01002412 connect to a socket. Abstract namespaces are supported with the prefix
Joseph Herlant02cedc42018-11-13 19:45:17 -08002413 "abns@", and finally a file descriptor can be passed with the prefix "fd@".
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002414 The prefix "ipv4@", "ipv6@" and "unix@" are also supported. The port can be
Bertrand Jacquin874a35c2018-09-10 21:26:07 +01002415 passed int the string. The syntax "127.0.0.1:1234" is valid. In this case, the
Tim Duesterhus6edab862018-01-06 19:04:45 +01002416 parameter *port* must not be set.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002417
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002418.. js:function:: Socket.connect_ssl(socket, address, port)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002419
2420 Same behavior than the function socket:connect, but uses SSL.
2421
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002422 :param class_socket socket: Is the manipulated Socket.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002423 :returns: 1 or nil.
2424
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002425.. js:function:: Socket.getpeername(socket)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002426
2427 Returns information about the remote side of a connected client object.
2428
2429 Returns a string with the IP address of the peer, followed by the port number
2430 that peer is using for the connection. In case of error, the method returns
2431 nil.
2432
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002433 :param class_socket socket: Is the manipulated Socket.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002434 :returns: a string containing the server information.
2435
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002436.. js:function:: Socket.getsockname(socket)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002437
2438 Returns the local address information associated to the object.
2439
2440 The method returns a string with local IP address and a number with the port.
2441 In case of error, the method returns nil.
2442
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002443 :param class_socket socket: Is the manipulated Socket.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002444 :returns: a string containing the client information.
2445
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002446.. js:function:: Socket.receive(socket, [pattern [, prefix]])
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002447
2448 Reads data from a client object, according to the specified read pattern.
2449 Patterns follow the Lua file I/O format, and the difference in performance
2450 between all patterns is negligible.
2451
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002452 :param class_socket socket: Is the manipulated Socket.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002453 :param string|integer pattern: Describe what is required (see below).
2454 :param string prefix: A string which will be prefix the returned data.
2455 :returns: a string containing the required data or nil.
2456
2457 Pattern can be any of the following:
2458
2459 * **`*a`**: reads from the socket until the connection is closed. No
2460 end-of-line translation is performed;
2461
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002462 * **`*l`**: reads a line of text from the Socket. The line is terminated by a
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002463 LF character (ASCII 10), optionally preceded by a CR character
2464 (ASCII 13). The CR and LF characters are not included in the
2465 returned line. In fact, all CR characters are ignored by the
2466 pattern. This is the default pattern.
2467
2468 * **number**: causes the method to read a specified number of bytes from the
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002469 Socket. Prefix is an optional string to be concatenated to the
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002470 beginning of any received data before return.
2471
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002472 * **empty**: If the pattern is left empty, the default option is `*l`.
2473
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002474 If successful, the method returns the received pattern. In case of error, the
2475 method returns nil followed by an error message which can be the string
2476 'closed' in case the connection was closed before the transmission was
2477 completed or the string 'timeout' in case there was a timeout during the
2478 operation. Also, after the error message, the function returns the partial
2479 result of the transmission.
2480
2481 Important note: This function was changed severely. It used to support
2482 multiple patterns (but I have never seen this feature used) and now it
2483 doesn't anymore. Partial results used to be returned in the same way as
2484 successful results. This last feature violated the idea that all functions
2485 should return nil on error. Thus it was changed too.
2486
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002487.. js:function:: Socket.send(socket, data [, start [, end ]])
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002488
2489 Sends data through client object.
2490
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002491 :param class_socket socket: Is the manipulated Socket.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002492 :param string data: The data that will be sent.
2493 :param integer start: The start position in the buffer of the data which will
2494 be sent.
2495 :param integer end: The end position in the buffer of the data which will
2496 be sent.
2497 :returns: see below.
2498
2499 Data is the string to be sent. The optional arguments i and j work exactly
2500 like the standard string.sub Lua function to allow the selection of a
2501 substring to be sent.
2502
2503 If successful, the method returns the index of the last byte within [start,
2504 end] that has been sent. Notice that, if start is 1 or absent, this is
2505 effectively the total number of bytes sent. In case of error, the method
2506 returns nil, followed by an error message, followed by the index of the last
2507 byte within [start, end] that has been sent. You might want to try again from
2508 the byte following that. The error message can be 'closed' in case the
2509 connection was closed before the transmission was completed or the string
2510 'timeout' in case there was a timeout during the operation.
2511
2512 Note: Output is not buffered. For small strings, it is always better to
2513 concatenate them in Lua (with the '..' operator) and send the result in one
2514 call instead of calling the method several times.
2515
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002516.. js:function:: Socket.setoption(socket, option [, value])
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002517
2518 Just implemented for compatibility, this cal does nothing.
2519
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002520.. js:function:: Socket.settimeout(socket, value [, mode])
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002521
2522 Changes the timeout values for the object. All I/O operations are blocking.
2523 That is, any call to the methods send, receive, and accept will block
2524 indefinitely, until the operation completes. The settimeout method defines a
2525 limit on the amount of time the I/O methods can block. When a timeout time
2526 has elapsed, the affected methods give up and fail with an error code.
2527
2528 The amount of time to wait is specified as the value parameter, in seconds.
2529
Mark Lakes56cc1252018-03-27 09:48:06 +02002530 The timeout modes are not implemented, the only settable timeout is the
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002531 inactivity time waiting for complete the internal buffer send or waiting for
2532 receive data.
2533
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002534 :param class_socket socket: Is the manipulated Socket.
Bertrand Jacquin874a35c2018-09-10 21:26:07 +01002535 :param float value: The timeout value. Use floating point to specify
Mark Lakes56cc1252018-03-27 09:48:06 +02002536 milliseconds.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002537
Thierry FOURNIER31904272017-10-25 12:59:51 +02002538.. _regex_class:
2539
2540Regex class
2541===========
2542
2543.. js:class:: Regex
2544
2545 This class allows the usage of HAProxy regexes because classic lua doesn't
2546 provides regexes. This class inherits the HAProxy compilation options, so the
2547 regexes can be libc regex, pcre regex or pcre JIT regex.
2548
2549 The expression matching number is limited to 20 per regex. The only available
2550 option is case sensitive.
2551
2552 Because regexes compilation is a heavy process, it is better to define all
2553 your regexes in the **body context** and use it during the runtime.
2554
2555.. code-block:: lua
2556
2557 -- Create the regex
2558 st, regex = Regex.new("needle (..) (...)", true);
2559
2560 -- Check compilation errors
2561 if st == false then
2562 print "error: " .. regex
2563 end
2564
2565 -- Match the regexes
2566 print(regex:exec("Looking for a needle in the haystack")) -- true
2567 print(regex:exec("Lokking for a cat in the haystack")) -- false
2568
2569 -- Extract words
2570 st, list = regex:match("Looking for a needle in the haystack")
2571 print(st) -- true
2572 print(list[1]) -- needle in the
2573 print(list[2]) -- in
2574 print(list[3]) -- the
2575
2576.. js:function:: Regex.new(regex, case_sensitive)
2577
2578 Create and compile a regex.
2579
2580 :param string regex: The regular expression according with the libc or pcre
2581 standard
2582 :param boolean case_sensitive: Match is case sensitive or not.
2583 :returns: boolean status and :ref:`regex_class` or string containing fail reason.
2584
2585.. js:function:: Regex.exec(regex, str)
2586
2587 Execute the regex.
2588
2589 :param class_regex regex: A :ref:`regex_class` object.
2590 :param string str: The input string will be compared with the compiled regex.
2591 :returns: a boolean status according with the match result.
2592
2593.. js:function:: Regex.match(regex, str)
2594
2595 Execute the regex and return matched expressions.
2596
2597 :param class_map map: A :ref:`regex_class` object.
2598 :param string str: The input string will be compared with the compiled regex.
2599 :returns: a boolean status according with the match result, and
2600 a table containing all the string matched in order of declaration.
2601
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002602.. _map_class:
2603
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002604Map class
2605=========
2606
2607.. js:class:: Map
2608
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02002609 This class permits to do some lookups in HAProxy maps. The declared maps can
Bertrand Jacquin874a35c2018-09-10 21:26:07 +01002610 be modified during the runtime through the HAProxy management socket.
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002611
2612.. code-block:: lua
2613
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002614 default = "usa"
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002615
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002616 -- Create and load map
Thierry FOURNIER4dc71972017-01-28 08:33:08 +01002617 geo = Map.new("geo.map", Map._ip);
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002618
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002619 -- Create new fetch that returns the user country
2620 core.register_fetches("country", function(txn)
2621 local src;
2622 local loc;
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002623
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002624 src = txn.f:fhdr("x-forwarded-for");
2625 if (src == nil) then
2626 src = txn.f:src()
2627 if (src == nil) then
2628 return default;
2629 end
2630 end
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002631
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002632 -- Perform lookup
2633 loc = geo:lookup(src);
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002634
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002635 if (loc == nil) then
2636 return default;
2637 end
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002638
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002639 return loc;
2640 end);
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002641
Thierry FOURNIER4dc71972017-01-28 08:33:08 +01002642.. js:attribute:: Map._int
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.int` is also available for compatibility.
2649
2650.. js:attribute:: Map._ip
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.ip` is also available for compatibility.
2657
2658.. js:attribute:: Map._str
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.str` is also available for compatibility.
2665
2666.. js:attribute:: Map._beg
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 Note that :js:attr:`Map.beg` is also available for compatibility.
2673
2674.. js:attribute:: Map._sub
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002675
2676 See the HAProxy configuration.txt file, chapter "Using ACLs and fetching
Ilya Shipitsin2075ca82020-03-06 23:22:22 +05002677 samples" and subchapter "ACL basics" to understand this pattern matching
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002678 method.
2679
Thierry FOURNIER4dc71972017-01-28 08:33:08 +01002680 Note that :js:attr:`Map.sub` is also available for compatibility.
2681
2682.. js:attribute:: Map._dir
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002683
2684 See the HAProxy configuration.txt file, chapter "Using ACLs and fetching
Ilya Shipitsin2075ca82020-03-06 23:22:22 +05002685 samples" and subchapter "ACL basics" to understand this pattern matching
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002686 method.
2687
Thierry FOURNIER4dc71972017-01-28 08:33:08 +01002688 Note that :js:attr:`Map.dir` is also available for compatibility.
2689
2690.. js:attribute:: Map._dom
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002691
2692 See the HAProxy configuration.txt file, chapter "Using ACLs and fetching
Ilya Shipitsin2075ca82020-03-06 23:22:22 +05002693 samples" and subchapter "ACL basics" to understand this pattern matching
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002694 method.
2695
Thierry FOURNIER4dc71972017-01-28 08:33:08 +01002696 Note that :js:attr:`Map.dom` is also available for compatibility.
2697
2698.. js:attribute:: Map._end
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002699
2700 See the HAProxy configuration.txt file, chapter "Using ACLs and fetching
Ilya Shipitsin2075ca82020-03-06 23:22:22 +05002701 samples" and subchapter "ACL basics" to understand this pattern matching
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002702 method.
2703
Thierry FOURNIER4dc71972017-01-28 08:33:08 +01002704.. js:attribute:: Map._reg
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002705
2706 See the HAProxy configuration.txt file, chapter "Using ACLs and fetching
Ilya Shipitsin2075ca82020-03-06 23:22:22 +05002707 samples" and subchapter "ACL basics" to understand this pattern matching
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002708 method.
2709
Thierry FOURNIER4dc71972017-01-28 08:33:08 +01002710 Note that :js:attr:`Map.reg` is also available for compatibility.
2711
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002712
2713.. js:function:: Map.new(file, method)
2714
2715 Creates and load a map.
2716
2717 :param string file: Is the file containing the map.
2718 :param integer method: Is the map pattern matching method. See the attributes
2719 of the Map class.
2720 :returns: a class Map object.
Thierry FOURNIER4dc71972017-01-28 08:33:08 +01002721 :see: The Map attributes: :js:attr:`Map._int`, :js:attr:`Map._ip`,
2722 :js:attr:`Map._str`, :js:attr:`Map._beg`, :js:attr:`Map._sub`,
2723 :js:attr:`Map._dir`, :js:attr:`Map._dom`, :js:attr:`Map._end` and
2724 :js:attr:`Map._reg`.
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002725
2726.. js:function:: Map.lookup(map, str)
2727
2728 Perform a lookup in a map.
2729
2730 :param class_map map: Is the class Map object.
2731 :param string str: Is the string used as key.
2732 :returns: a string containing the result or nil if no match.
2733
2734.. js:function:: Map.slookup(map, str)
2735
2736 Perform a lookup in a map.
2737
2738 :param class_map map: Is the class Map object.
2739 :param string str: Is the string used as key.
2740 :returns: a string containing the result or empty string if no match.
2741
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002742.. _applethttp_class:
2743
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002744AppletHTTP class
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002745================
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002746
2747.. js:class:: AppletHTTP
2748
2749 This class is used with applets that requires the 'http' mode. The http applet
2750 can be registered with the *core.register_service()* function. They are used
2751 for processing an http request like a server in back of HAProxy.
2752
2753 This is an hello world sample code:
2754
2755.. code-block:: lua
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002756
Pieter Baauw4d7f7662015-11-08 16:38:08 +01002757 core.register_service("hello-world", "http", function(applet)
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002758 local response = "Hello World !"
2759 applet:set_status(200)
Pieter Baauw2dcb9bc2015-10-01 22:47:12 +02002760 applet:add_header("content-length", string.len(response))
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002761 applet:add_header("content-type", "text/plain")
Pieter Baauw2dcb9bc2015-10-01 22:47:12 +02002762 applet:start_response()
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002763 applet:send(response)
2764 end)
2765
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002766.. js:attribute:: AppletHTTP.c
2767
2768 :returns: A :ref:`converters_class`
2769
2770 This attribute contains a Converters class object.
2771
2772.. js:attribute:: AppletHTTP.sc
2773
2774 :returns: A :ref:`converters_class`
2775
2776 This attribute contains a Converters class object. The
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02002777 functions of this object always return a string.
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002778
2779.. js:attribute:: AppletHTTP.f
2780
2781 :returns: A :ref:`fetches_class`
2782
2783 This attribute contains a Fetches class object. Note that the
2784 applet execution place cannot access to a valid HAProxy core HTTP
Ilya Shipitsin2075ca82020-03-06 23:22:22 +05002785 transaction, so some sample fetches related to the HTTP dependent
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002786 values (hdr, path, ...) are not available.
2787
2788.. js:attribute:: AppletHTTP.sf
2789
2790 :returns: A :ref:`fetches_class`
2791
2792 This attribute contains a Fetches class object. The functions of
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02002793 this object always return a string. Note that the applet
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002794 execution place cannot access to a valid HAProxy core HTTP
Ilya Shipitsin2075ca82020-03-06 23:22:22 +05002795 transaction, so some sample fetches related to the HTTP dependent
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002796 values (hdr, path, ...) are not available.
2797
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01002798.. js:attribute:: AppletHTTP.method
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002799
2800 :returns: string
2801
2802 The attribute method returns a string containing the HTTP
2803 method.
2804
2805.. js:attribute:: AppletHTTP.version
2806
2807 :returns: string
2808
2809 The attribute version, returns a string containing the HTTP
2810 request version.
2811
2812.. js:attribute:: AppletHTTP.path
2813
2814 :returns: string
2815
2816 The attribute path returns a string containing the HTTP
2817 request path.
2818
2819.. js:attribute:: AppletHTTP.qs
2820
2821 :returns: string
2822
2823 The attribute qs returns a string containing the HTTP
2824 request query string.
2825
2826.. js:attribute:: AppletHTTP.length
2827
2828 :returns: integer
2829
2830 The attribute length returns an integer containing the HTTP
2831 body length.
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002832
Thierry FOURNIER841475e2015-12-11 17:10:09 +01002833.. js:attribute:: AppletHTTP.headers
2834
Patrick Hemmerc6a1d712018-05-01 21:30:41 -04002835 :returns: table
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002836
Patrick Hemmerc6a1d712018-05-01 21:30:41 -04002837 The attribute headers returns a table containing the HTTP
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002838 headers. The header names are always in lower case. As the header name can be
2839 encountered more than once in each request, the value is indexed with 0 as
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02002840 first index value. The table has this form:
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002841
2842.. code-block:: lua
2843
2844 AppletHTTP.headers['<header-name>'][<header-index>] = "<header-value>"
2845
2846 AppletHTTP.headers["host"][0] = "www.test.com"
2847 AppletHTTP.headers["accept"][0] = "audio/basic q=1"
2848 AppletHTTP.headers["accept"][1] = "audio/*, q=0.2"
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01002849 AppletHTTP.headers["accept"][2] = "*/*, q=0.1"
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002850..
2851
Robin H. Johnson52f5db22017-01-01 13:10:52 -08002852.. js:function:: AppletHTTP.set_status(applet, code [, reason])
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002853
2854 This function sets the HTTP status code for the response. The allowed code are
2855 from 100 to 599.
2856
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01002857 :param class_AppletHTTP applet: An :ref:`applethttp_class`
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002858 :param integer code: the status code returned to the client.
Robin H. Johnson52f5db22017-01-01 13:10:52 -08002859 :param string reason: the status reason returned to the client (optional).
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002860
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01002861.. js:function:: AppletHTTP.add_header(applet, name, value)
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002862
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02002863 This function adds a header in the response. Duplicated headers are not
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002864 collapsed. The special header *content-length* is used to determinate the
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02002865 response length. If it does not exist, a *transfer-encoding: chunked* is set, and
Ilya Shipitsin2075ca82020-03-06 23:22:22 +05002866 all the write from the function *AppletHTTP:send()* become a chunk.
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002867
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01002868 :param class_AppletHTTP applet: An :ref:`applethttp_class`
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002869 :param string name: the header name
2870 :param string value: the header value
2871
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01002872.. js:function:: AppletHTTP.start_response(applet)
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002873
2874 This function indicates to the HTTP engine that it can process and send the
2875 response headers. After this called we cannot add headers to the response; We
2876 cannot use the *AppletHTTP:send()* function if the
2877 *AppletHTTP:start_response()* is not called.
2878
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01002879 :param class_AppletHTTP applet: An :ref:`applethttp_class`
2880
2881.. js:function:: AppletHTTP.getline(applet)
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002882
2883 This function returns a string containing one line from the http body. If the
2884 data returned doesn't contains a final '\\n' its assumed than its the last
2885 available data before the end of stream.
2886
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01002887 :param class_AppletHTTP applet: An :ref:`applethttp_class`
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002888 :returns: a string. The string can be empty if we reach the end of the stream.
2889
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01002890.. js:function:: AppletHTTP.receive(applet, [size])
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002891
2892 Reads data from the HTTP body, according to the specified read *size*. If the
2893 *size* is missing, the function tries to read all the content of the stream
2894 until the end. If the *size* is bigger than the http body, it returns the
Bertrand Jacquin874a35c2018-09-10 21:26:07 +01002895 amount of data available.
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002896
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01002897 :param class_AppletHTTP applet: An :ref:`applethttp_class`
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002898 :param integer size: the required read size.
Ilya Shipitsin11057a32020-06-21 21:18:27 +05002899 :returns: always return a string,the string can be empty is the connection is
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002900 closed.
2901
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01002902.. js:function:: AppletHTTP.send(applet, msg)
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002903
2904 Send the message *msg* on the http request body.
2905
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01002906 :param class_AppletHTTP applet: An :ref:`applethttp_class`
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002907 :param string msg: the message to send.
2908
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01002909.. js:function:: AppletHTTP.get_priv(applet)
2910
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01002911 Return Lua data stored in the current transaction. If no data are stored,
2912 it returns a nil value.
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01002913
2914 :param class_AppletHTTP applet: An :ref:`applethttp_class`
Bertrand Jacquin874a35c2018-09-10 21:26:07 +01002915 :returns: the opaque data previously stored, or nil if nothing is
2916 available.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01002917 :see: :js:func:`AppletHTTP.set_priv`
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01002918
2919.. js:function:: AppletHTTP.set_priv(applet, data)
2920
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02002921 Store any data in the current HAProxy transaction. This action replaces the
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01002922 old stored data.
2923
2924 :param class_AppletHTTP applet: An :ref:`applethttp_class`
2925 :param opaque data: The data which is stored in the transaction.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01002926 :see: :js:func:`AppletHTTP.get_priv`
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01002927
Tim Duesterhus4e172c92020-05-19 13:49:42 +02002928.. js:function:: AppletHTTP.set_var(applet, var, value[, ifexist])
Thierry FOURNIER / OZON.IOc1edafe2016-12-12 16:25:30 +01002929
2930 Converts a Lua type in a HAProxy type and store it in a variable <var>.
2931
2932 :param class_AppletHTTP applet: An :ref:`applethttp_class`
2933 :param string var: The variable name according with the HAProxy variable syntax.
2934 :param type value: The value associated to the variable. The type ca be string or
2935 integer.
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02002936 :param boolean ifexist: If this parameter is set to true the variable
Tim Duesterhus4e172c92020-05-19 13:49:42 +02002937 will only be set if it was defined elsewhere (i.e. used
Willy Tarreau7978c5c2021-09-07 14:24:07 +02002938 within the configuration). For global variables (using the
2939 "proc" scope), they will only be updated and never created.
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02002940 It is highly recommended to always set this to true.
2941
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01002942 :see: :js:func:`AppletHTTP.unset_var`
2943 :see: :js:func:`AppletHTTP.get_var`
Thierry FOURNIER / OZON.IOc1edafe2016-12-12 16:25:30 +01002944
2945.. js:function:: AppletHTTP.unset_var(applet, var)
2946
2947 Unset the variable <var>.
2948
2949 :param class_AppletHTTP applet: An :ref:`applethttp_class`
2950 :param string var: The variable name according with the HAProxy variable syntax.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01002951 :see: :js:func:`AppletHTTP.set_var`
2952 :see: :js:func:`AppletHTTP.get_var`
Thierry FOURNIER / OZON.IOc1edafe2016-12-12 16:25:30 +01002953
2954.. js:function:: AppletHTTP.get_var(applet, var)
2955
2956 Returns data stored in the variable <var> converter in Lua type.
2957
2958 :param class_AppletHTTP applet: An :ref:`applethttp_class`
2959 :param string var: The variable name according with the HAProxy variable syntax.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01002960 :see: :js:func:`AppletHTTP.set_var`
2961 :see: :js:func:`AppletHTTP.unset_var`
Thierry FOURNIER / OZON.IOc1edafe2016-12-12 16:25:30 +01002962
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002963.. _applettcp_class:
2964
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002965AppletTCP class
2966===============
2967
2968.. js:class:: AppletTCP
2969
2970 This class is used with applets that requires the 'tcp' mode. The tcp applet
2971 can be registered with the *core.register_service()* function. They are used
2972 for processing a tcp stream like a server in back of HAProxy.
2973
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002974.. js:attribute:: AppletTCP.c
2975
2976 :returns: A :ref:`converters_class`
2977
2978 This attribute contains a Converters class object.
2979
2980.. js:attribute:: AppletTCP.sc
2981
2982 :returns: A :ref:`converters_class`
2983
2984 This attribute contains a Converters class object. The
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02002985 functions of this object always return a string.
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002986
2987.. js:attribute:: AppletTCP.f
2988
2989 :returns: A :ref:`fetches_class`
2990
2991 This attribute contains a Fetches class object.
2992
2993.. js:attribute:: AppletTCP.sf
2994
2995 :returns: A :ref:`fetches_class`
2996
2997 This attribute contains a Fetches class object.
2998
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01002999.. js:function:: AppletTCP.getline(applet)
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02003000
3001 This function returns a string containing one line from the stream. If the
3002 data returned doesn't contains a final '\\n' its assumed than its the last
3003 available data before the end of stream.
3004
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01003005 :param class_AppletTCP applet: An :ref:`applettcp_class`
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02003006 :returns: a string. The string can be empty if we reach the end of the stream.
3007
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01003008.. js:function:: AppletTCP.receive(applet, [size])
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02003009
3010 Reads data from the TCP stream, according to the specified read *size*. If the
3011 *size* is missing, the function tries to read all the content of the stream
3012 until the end.
3013
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01003014 :param class_AppletTCP applet: An :ref:`applettcp_class`
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02003015 :param integer size: the required read size.
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02003016 :returns: always return a string, the string can be empty if the connection is
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02003017 closed.
3018
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01003019.. js:function:: AppletTCP.send(appletmsg)
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02003020
3021 Send the message on the stream.
3022
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01003023 :param class_AppletTCP applet: An :ref:`applettcp_class`
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02003024 :param string msg: the message to send.
3025
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01003026.. js:function:: AppletTCP.get_priv(applet)
3027
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01003028 Return Lua data stored in the current transaction. If no data are stored,
3029 it returns a nil value.
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01003030
3031 :param class_AppletTCP applet: An :ref:`applettcp_class`
Bertrand Jacquin874a35c2018-09-10 21:26:07 +01003032 :returns: the opaque data previously stored, or nil if nothing is
3033 available.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01003034 :see: :js:func:`AppletTCP.set_priv`
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01003035
3036.. js:function:: AppletTCP.set_priv(applet, data)
3037
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02003038 Store any data in the current HAProxy transaction. This action replaces the
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01003039 old stored data.
3040
3041 :param class_AppletTCP applet: An :ref:`applettcp_class`
3042 :param opaque data: The data which is stored in the transaction.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01003043 :see: :js:func:`AppletTCP.get_priv`
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01003044
Tim Duesterhus4e172c92020-05-19 13:49:42 +02003045.. js:function:: AppletTCP.set_var(applet, var, value[, ifexist])
Thierry FOURNIER / OZON.IOc1edafe2016-12-12 16:25:30 +01003046
3047 Converts a Lua type in a HAProxy type and stores it in a variable <var>.
3048
3049 :param class_AppletTCP applet: An :ref:`applettcp_class`
3050 :param string var: The variable name according with the HAProxy variable syntax.
3051 :param type value: The value associated to the variable. The type can be string or
3052 integer.
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02003053 :param boolean ifexist: If this parameter is set to true the variable
Tim Duesterhus4e172c92020-05-19 13:49:42 +02003054 will only be set if it was defined elsewhere (i.e. used
Willy Tarreau7978c5c2021-09-07 14:24:07 +02003055 within the configuration). For global variables (using the
3056 "proc" scope), they will only be updated and never created.
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02003057 It is highly recommended to always set this to true.
3058
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01003059 :see: :js:func:`AppletTCP.unset_var`
3060 :see: :js:func:`AppletTCP.get_var`
Thierry FOURNIER / OZON.IOc1edafe2016-12-12 16:25:30 +01003061
3062.. js:function:: AppletTCP.unset_var(applet, var)
3063
3064 Unsets the variable <var>.
3065
3066 :param class_AppletTCP applet: An :ref:`applettcp_class`
3067 :param string var: The variable name according with the HAProxy variable syntax.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01003068 :see: :js:func:`AppletTCP.unset_var`
3069 :see: :js:func:`AppletTCP.set_var`
Thierry FOURNIER / OZON.IOc1edafe2016-12-12 16:25:30 +01003070
3071.. js:function:: AppletTCP.get_var(applet, var)
3072
3073 Returns data stored in the variable <var> converter in Lua type.
3074
3075 :param class_AppletTCP applet: An :ref:`applettcp_class`
3076 :param string var: The variable name according with the HAProxy variable syntax.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01003077 :see: :js:func:`AppletTCP.unset_var`
3078 :see: :js:func:`AppletTCP.set_var`
Thierry FOURNIER / OZON.IOc1edafe2016-12-12 16:25:30 +01003079
Adis Nezirovic8878f8e2018-07-13 12:18:33 +02003080StickTable class
3081================
3082
3083.. js:class:: StickTable
3084
3085 **context**: task, action, sample-fetch
3086
3087 This class can be used to access the HAProxy stick tables from Lua.
3088
3089.. js:function:: StickTable.info()
3090
3091 Returns stick table attributes as a Lua table. See HAProxy documentation for
Ilya Shipitsin2272d8a2020-12-21 01:22:40 +05003092 "stick-table" for canonical info, or check out example below.
Adis Nezirovic8878f8e2018-07-13 12:18:33 +02003093
3094 :returns: Lua table
3095
3096 Assume our table has IPv4 key and gpc0 and conn_rate "columns":
3097
3098.. code-block:: lua
3099
3100 {
3101 expire=<int>, # Value in ms
3102 size=<int>, # Maximum table size
3103 used=<int>, # Actual number of entries in table
3104 data={ # Data columns, with types as key, and periods as values
3105 (-1 if type is not rate counter)
3106 conn_rate=<int>,
3107 gpc0=-1
3108 },
3109 length=<int>, # max string length for string table keys, key length
3110 # otherwise
3111 nopurge=<boolean>, # purge oldest entries when table is full
3112 type="ip" # can be "ip", "ipv6", "integer", "string", "binary"
3113 }
3114
3115.. js:function:: StickTable.lookup(key)
3116
3117 Returns stick table entry for given <key>
3118
3119 :param string key: Stick table key (IP addresses and strings are supported)
3120 :returns: Lua table
3121
3122.. js:function:: StickTable.dump([filter])
3123
3124 Returns all entries in stick table. An optional filter can be used
3125 to extract entries with specific data values. Filter is a table with valid
3126 comparison operators as keys followed by data type name and value pairs.
3127 Check out the HAProxy docs for "show table" for more details. For the
3128 reference, the supported operators are:
3129 "eq", "ne", "le", "lt", "ge", "gt"
3130
3131 For large tables, execution of this function can take a long time (for
3132 HAProxy standards). That's also true when filter is used, so take care and
3133 measure the impact.
3134
3135 :param table filter: Stick table filter
3136 :returns: Stick table entries (table)
3137
3138 See below for example filter, which contains 4 entries (or comparisons).
3139 (Maximum number of filter entries is 4, defined in the source code)
3140
3141.. code-block:: lua
3142
3143 local filter = {
3144 {"gpc0", "gt", 30}, {"gpc1", "gt", 20}}, {"conn_rate", "le", 10}
3145 }
3146
Christopher Faulet0f3c8902020-01-31 18:57:12 +01003147.. _action_class:
3148
3149Action class
3150=============
3151
3152.. js:class:: Act
3153
3154 **context**: action
3155
3156 This class contains all return codes an action may return. It is the lua
3157 equivalent to HAProxy "ACT_RET_*" code.
3158
3159.. code-block:: lua
3160
3161 core.register_action("deny", { "http-req" }, function (txn)
3162 return act.DENY
3163 end)
3164..
3165.. js:attribute:: act.CONTINUE
3166
3167 This attribute is an integer (0). It instructs HAProxy to continue the current
3168 ruleset processing on the message. It is the default return code for a lua
3169 action.
3170
3171 :returns: integer
3172
3173.. js:attribute:: act.STOP
3174
3175 This attribute is an integer (1). It instructs HAProxy to stop the current
3176 ruleset processing on the message.
3177
3178.. js:attribute:: act.YIELD
3179
3180 This attribute is an integer (2). It instructs HAProxy to temporarily pause
3181 the message processing. It will be resumed later on the same rule. The
3182 corresponding lua script is re-executed for the start.
3183
3184.. js:attribute:: act.ERROR
3185
3186 This attribute is an integer (3). It triggers an internal errors The message
3187 processing is stopped and the transaction is terminated. For HTTP streams, an
3188 HTTP 500 error is returned to the client.
3189
3190 :returns: integer
3191
3192.. js:attribute:: act.DONE
3193
3194 This attribute is an integer (4). It instructs HAProxy to stop the message
3195 processing.
3196
3197 :returns: integer
3198
3199.. js:attribute:: act.DENY
3200
3201 This attribute is an integer (5). It denies the current message. The message
3202 processing is stopped and the transaction is terminated. For HTTP streams, an
3203 HTTP 403 error is returned to the client if the deny is returned during the
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02003204 request analysis. During the response analysis, a HTTP 502 error is returned
Christopher Faulet0f3c8902020-01-31 18:57:12 +01003205 and the server response is discarded.
3206
3207 :returns: integer
3208
3209.. js:attribute:: act.ABORT
3210
3211 This attribute is an integer (6). It aborts the current message. The message
3212 processing is stopped and the transaction is terminated. For HTTP streams,
Willy Tarreau714f3452021-05-09 06:47:26 +02003213 HAProxy assumes a response was already sent to the client. From the Lua
Christopher Faulet0f3c8902020-01-31 18:57:12 +01003214 actions point of view, when this code is used, the transaction is terminated
3215 with no reply.
3216
3217 :returns: integer
3218
3219.. js:attribute:: act.INVALID
3220
3221 This attribute is an integer (7). It triggers an internal errors. The message
3222 processing is stopped and the transaction is terminated. For HTTP streams, an
3223 HTTP 400 error is returned to the client if the error is returned during the
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02003224 request analysis. During the response analysis, a HTTP 502 error is returned
Christopher Faulet0f3c8902020-01-31 18:57:12 +01003225 and the server response is discarded.
3226
3227 :returns: integer
Adis Nezirovic8878f8e2018-07-13 12:18:33 +02003228
Christopher Faulet2c2c2e32020-01-31 19:07:52 +01003229.. js:function:: act:wake_time(milliseconds)
3230
3231 **context**: action
3232
3233 Set the script pause timeout to the specified time, defined in
3234 milliseconds.
3235
3236 :param integer milliseconds: the required milliseconds.
3237
3238 This function may be used when a lua action returns `act.YIELD`, to force its
3239 wake-up at most after the specified number of milliseconds.
3240
Christopher Faulet5a2c6612021-08-15 20:35:25 +02003241.. _filter_class:
3242
3243Filter class
3244=============
3245
3246.. js:class:: filter
3247
3248 **context**: filter
3249
3250 This class contains return codes some filter callback functions may return. It
3251 also contains configuration flags and some helper functions. To understand how
3252 the filter API works, see `doc/internal/filters.txt` documentation.
3253
3254.. js:attribute:: filter.CONTINUE
3255
3256 This attribute is an integer (1). It may be returned by some filter callback
3257 functions to instruct this filtering step is finished for this filter.
3258
3259.. js:attribute:: filter.WAIT
3260
3261 This attribute is an integer (0). It may be returned by some filter callback
3262 functions to instruct the filtering must be paused, waiting for more data or
3263 for an external event depending on this filter.
3264
3265.. js:attribute:: filter.ERROR
3266
3267 This attribute is an integer (-1). It may be returned by some filter callback
3268 functions to trigger an error.
3269
3270.. js:attribute:: filter.FLT_CFG_FL_HTX
3271
3272 This attribute is a flag corresponding to the filter flag FLT_CFG_FL_HTX. When
3273 it is set for a filter, it means the filter is able to filter HTTP streams.
3274
3275.. js:function:: filter.register_data_filter(chn)
3276
3277 **context**: filter
3278
3279 Enable the data filtering on the channel **chn** for the current filter. It
Ilya Shipitsinff0f2782021-08-22 22:18:07 +05003280 may be called at any time from any callback functions proceeding the data
Christopher Faulet5a2c6612021-08-15 20:35:25 +02003281 analysis.
3282
3283 :param class_Channel chn: A :ref:`channel_class`.
3284
3285.. js:function:: filter.unregister_data_filter(chn)
3286
3287 **context**: filter
3288
3289 Disable the data filtering on the channel **chn** for the current filter. It
3290 may be called at any time from any callback functions.
3291
3292 :param class_Channel chn: A :ref:`channel_class`.
3293
3294.. js:function:: filter.wake_time(milliseconds)
3295
3296 **context**: filter
3297
3298 Set the script pause timeout to the specified time, defined in
3299 milliseconds.
3300
3301 :param integer milliseconds: the required milliseconds.
3302
3303 This function may be used from any lua filter callback function to force its
3304 wake-up at most after the specified number of milliseconds. Especially, when
3305 `filter.CONTINUE` is returned.
3306
3307
3308A filters is declared using :js:func:`core.register_filter()` function. The
3309provided class will be used to instantiate filters. It may define following
3310attributes:
3311
3312* id: The filter identifier. It is a string that identifies the filter and is
3313 optional.
3314
3315* flags: The filter flags. Only :js:attr:`filter.FLT_CFG_FL_HTX` may be set for now.
3316
3317Such filter class must also define all required callback functions in the
3318following list. Note that :js:func:`Filter.new()` must be defined otherwise the
Ilya Shipitsinff0f2782021-08-22 22:18:07 +05003319filter is ignored. Others are optional.
Christopher Faulet5a2c6612021-08-15 20:35:25 +02003320
3321* .. js:function:: FILTER.new()
3322
3323 Called to instantiate a new filter. This function must be defined.
3324
3325 :returns: a Lua object that will be used as filter instance for the current
3326 stream.
3327
3328* .. js:function:: FILTER.start_analyze(flt, txn, chn)
3329
3330 Called when the analysis starts on the channel **chn**.
3331
3332* .. js:function:: FILTER.end_analyze(flt, txn, chn)
3333
3334 Called when the analysis ends on the channel **chn**.
3335
3336* .. js:function:: FILTER.http_headers(flt, txn, http_msg)
3337
3338 Called just before the HTTP payload analysis and after any processing on the
3339 HTTP message **http_msg**. This callback functions is only called for HTTP
3340 streams.
3341
3342* .. js:function:: FILTER.http_payload(flt, txn, http_msg)
3343
3344 Called during the HTTP payload analysis on the HTTP message **http_msg**. This
3345 callback functions is only called for HTTP streams.
3346
3347* .. js:function:: FILTER.http_end(flt, txn, http_msg)
3348
3349 Called after the HTTP payload analysis on the HTTP message **http_msg**. This
3350 callback functions is only called for HTTP streams.
3351
3352* .. js:function:: FILTER.tcp_payload(flt, txn, chn)
3353
3354 Called during the TCP payload analysis on the channel **chn**.
3355
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02003356Here is a full example:
Christopher Faulet5a2c6612021-08-15 20:35:25 +02003357
3358.. code-block:: lua
3359
3360 Trace = {}
3361 Trace.id = "Lua trace filter"
3362 Trace.flags = filter.FLT_CFG_FL_HTX;
3363 Trace.__index = Trace
3364
3365 function Trace:new()
3366 local trace = {}
3367 setmetatable(trace, Trace)
3368 trace.req_len = 0
3369 trace.res_len = 0
3370 return trace
3371 end
3372
3373 function Trace:start_analyze(txn, chn)
3374 if chn:is_resp() then
3375 print("Start response analysis")
3376 else
3377 print("Start request analysis")
3378 end
3379 filter.register_data_filter(self, chn)
3380 end
3381
3382 function Trace:end_analyze(txn, chn)
3383 if chn:is_resp() then
3384 print("End response analysis: "..self.res_len.." bytes filtered")
3385 else
3386 print("End request analysis: "..self.req_len.." bytes filtered")
3387 end
3388 end
3389
3390 function Trace:http_headers(txn, http_msg)
3391 stline = http_msg:get_stline()
3392 if http_msg.channel:is_resp() then
3393 print("response:")
3394 print(stline.version.." "..stline.code.." "..stline.reason)
3395 else
3396 print("request:")
3397 print(stline.method.." "..stline.uri.." "..stline.version)
3398 end
3399
3400 for n, hdrs in pairs(http_msg:get_headers()) do
3401 for i,v in pairs(hdrs) do
3402 print(n..": "..v)
3403 end
3404 end
3405 return filter.CONTINUE
3406 end
3407
3408 function Trace:http_payload(txn, http_msg)
3409 body = http_msg:body(-20000)
3410 if http_msg.channel:is_resp() then
3411 self.res_len = self.res_len + body:len()
3412 else
3413 self.req_len = self.req_len + body:len()
3414 end
3415 end
3416
3417 core.register_filter("trace", Trace, function(trace, args)
3418 return trace
3419 end)
3420
3421..
3422
3423.. _httpmessage_class:
3424
3425HTTPMessage class
3426===================
3427
3428.. js:class:: HTTPMessage
3429
3430 **context**: filter
3431
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02003432 This class contains all functions to manipulate a HTTP message. For now, this
Christopher Faulet5a2c6612021-08-15 20:35:25 +02003433 class is only available from a filter context.
3434
3435.. js:function:: HTTPMessage.add_header(http_msg, name, value)
3436
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02003437 Appends a HTTP header field in the HTTP message **http_msg** whose name is
Christopher Faulet5a2c6612021-08-15 20:35:25 +02003438 specified in **name** and whose value is defined in **value**.
3439
3440 :param class_httpmessage http_msg: The manipulated HTTP message.
3441 :param string name: The header name.
3442 :param string value: The header value.
3443
3444.. js:function:: HTTPMessage.append(http_msg, string)
3445
3446 This function copies the string **string** at the end of incoming data of the
3447 HTTP message **http_msg**. The function returns the copied length on success
3448 or -1 if data cannot be copied.
3449
3450 Same that :js:func:`HTTPMessage.insert(http_msg, string, http_msg:input())`.
3451
3452 :param class_httpmessage http_msg: The manipulated HTTP message.
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02003453 :param string string: The data to copy at the end of incoming data.
Christopher Faulet5a2c6612021-08-15 20:35:25 +02003454 :returns: an integer containing the amount of bytes copied or -1.
3455
3456.. js:function:: HTTPMessage.body(http_msgl[, offset[, length]])
3457
3458 This function returns **length** bytes of incoming data from the HTTP message
3459 **http_msg**, starting at the offset **offset**. The data are not removed from
3460 the buffer.
3461
3462 By default, if no length is provided, all incoming data found, starting at the
3463 given offset, are returned. If **length** is set to -1, the function tries to
3464 retrieve a maximum of data. Because it is called in the filter context, it
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02003465 never yield. Not providing an offset is the same as setting it to 0. A
Ilya Shipitsinff0f2782021-08-22 22:18:07 +05003466 positive offset is relative to the beginning of incoming data of the
Christopher Faulet5a2c6612021-08-15 20:35:25 +02003467 http_message buffer while negative offset is relative to their end.
3468
3469 If there is no incoming data and the HTTP message can't receive more data, a 'nil'
3470 value is returned.
3471
3472 :param class_httpmessage http_msg: The manipulated HTTP message.
3473 :param integer offset: *optional* The offset in incoming data to start to get
3474 data. 0 by default. May be negative to be relative to
3475 the end of incoming data.
3476 :param integer length: *optional* The expected length of data to retrieve. All
3477 incoming data by default. May be set to -1 to get a
3478 maximum of data.
3479 :returns: a string containing the data found or nil.
3480
3481.. js:function:: HTTPMessage.eom(http_msg)
3482
3483 This function returns true if the end of message is reached for the HTTP
3484 message **http_msg**.
3485
3486 :param class_httpmessage http_msg: The manipulated HTTP message.
3487 :returns: an integer containing the amount of available bytes.
3488
3489.. js:function:: HTTPMessage.del_header(http_msg, name)
3490
3491 Removes all HTTP header fields in the HTTP message **http_msg** whose name is
3492 specified in **name**.
3493
3494 :param class_httpmessage http_msg: The manipulated http message.
3495 :param string name: The header name.
3496
3497.. js:function:: HTTPMessage.get_headers(http_msg)
3498
3499 Returns a table containing all the headers of the HTTP message **http_msg**.
3500
3501 :param class_httpmessage http_msg: The manipulated http message.
3502 :returns: table of headers.
3503
3504 This is the form of the returned table:
3505
3506.. code-block:: lua
3507
3508 http_msg:get_headers()['<header-name>'][<header-index>] = "<header-value>"
3509
3510 local hdr = http_msg:get_headers()
3511 hdr["host"][0] = "www.test.com"
3512 hdr["accept"][0] = "audio/basic q=1"
3513 hdr["accept"][1] = "audio/*, q=0.2"
3514 hdr["accept"][2] = "*.*, q=0.1"
3515..
3516
3517.. js:function:: HTTPMessage.get_stline(http_msg)
3518
3519 Returns a table containing the start-line of the HTTP message **http_msg**.
3520
3521 :param class_httpmessage http_msg: The manipulated http message.
3522 :returns: the start-line.
3523
3524 This is the form of the returned table:
3525
3526.. code-block:: lua
3527
3528 -- for the request :
3529 {"method" = string, "uri" = string, "version" = string}
3530
3531 -- for the response:
3532 {"version" = string, "code" = string, "reason" = string}
3533..
3534
3535.. js:function:: HTTPMessage.forward(http_msg, length)
3536
3537 This function forwards **length** bytes of data from the HTTP message
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02003538 **http_msg**. Because it is called in the filter context, it never yields. Only
Christopher Faulet5a2c6612021-08-15 20:35:25 +02003539 available incoming data may be forwarded, event if the requested length
3540 exceeds the available amount of incoming data. It returns the amount of data
3541 forwarded.
3542
3543 :param class_httpmessage http_msg: The manipulated HTTP message.
3544 :param integer int: The amount of data to forward.
3545
3546.. js:function:: HTTPMessage.input(http_msg)
3547
3548 This function returns the length of incoming data in the HTTP message
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02003549 **http_msg** from the filter point of view.
Christopher Faulet5a2c6612021-08-15 20:35:25 +02003550
3551 :param class_httpmessage http_msg: The manipulated HTTP message.
3552 :returns: an integer containing the amount of available bytes.
3553
3554.. js:function:: HTTPMessage.insert(http_msg, string[, offset])
3555
3556 This function copies the string **string** at the offset **offset** in
3557 incoming data of the HTTP message **http_msg**. The function returns the
3558 copied length on success or -1 if data cannot be copied.
3559
3560 By default, if no offset is provided, the string is copied in front of
Ilya Shipitsinff0f2782021-08-22 22:18:07 +05003561 incoming data. A positive offset is relative to the beginning of incoming data
Christopher Faulet5a2c6612021-08-15 20:35:25 +02003562 of the HTTP message while negative offset is relative to their end.
3563
3564 :param class_httpmessage http_msg: The manipulated HTTP message.
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02003565 :param string string: The data to copy into incoming data.
3566 :param integer offset: *optional* The offset in incoming data where to copy
Christopher Faulet5a2c6612021-08-15 20:35:25 +02003567 data. 0 by default. May be negative to be relative to
3568 the end of incoming data.
3569 :returns: an integer containing the amount of bytes copied or -1.
3570
3571.. js:function:: HTTPMessage.is_full(http_msg)
3572
3573 This function returns true if the HTTP message **http_msg** is full.
3574
3575 :param class_httpmessage http_msg: The manipulated HTTP message.
3576 :returns: a boolean
3577
3578.. js:function:: HTTPMessage.is_resp(http_msg)
3579
3580 This function returns true if the HTTP message **http_msg** is the response
3581 one.
3582
3583 :param class_httpmessage http_msg: The manipulated HTTP message.
3584 :returns: a boolean
3585
3586.. js:function:: HTTPMessage.may_recv(http_msg)
3587
3588 This function returns true if the HTTP message **http_msg** may still receive
3589 data.
3590
3591 :param class_httpmessage http_msg: The manipulated HTTP message.
3592 :returns: a boolean
3593
3594.. js:function:: HTTPMessage.output(http_msg)
3595
3596 This function returns the length of outgoing data of the HTTP message
3597 **http_msg**.
3598
3599 :param class_httpmessage http_msg: The manipulated HTTP message.
3600 :returns: an integer containing the amount of available bytes.
3601
3602.. js:function:: HTTPMessage.prepend(http_msg, string)
3603
3604 This function copies the string **string** in front of incoming data of the
3605 HTTP message **http_msg**. The function returns the copied length on success
3606 or -1 if data cannot be copied.
3607
3608 Same that :js:func:`HTTPMessage.insert(http_msg, string, 0)`.
3609
3610 :param class_httpmessage http_msg: The manipulated HTTP message.
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02003611 :param string string: The data to copy in front of incoming data.
Christopher Faulet5a2c6612021-08-15 20:35:25 +02003612 :returns: an integer containing the amount of bytes copied or -1.
3613
3614.. js:function:: HTTPMessage.remove(http_msg[, offset[, length]])
3615
3616 This function removes **length** bytes of incoming data of the HTTP message
3617 **http_msg**, starting at offset **offset**. This function returns number of
3618 bytes removed on success.
3619
3620 By default, if no length is provided, all incoming data, starting at the given
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02003621 offset, are removed. Not providing an offset is the same that setting it
Ilya Shipitsinff0f2782021-08-22 22:18:07 +05003622 to 0. A positive offset is relative to the beginning of incoming data of the
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02003623 HTTP message while negative offset is relative to the end.
Christopher Faulet5a2c6612021-08-15 20:35:25 +02003624
3625 :param class_httpmessage http_msg: The manipulated HTTP message.
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02003626 :param integer offset: *optional* The offset in incoming data where to start
Christopher Faulet5a2c6612021-08-15 20:35:25 +02003627 to remove data. 0 by default. May be negative to
3628 be relative to the end of incoming data.
3629 :param integer length: *optional* The length of data to remove. All incoming
3630 data by default.
3631 :returns: an integer containing the amount of bytes removed.
3632
3633.. js:function:: HTTPMessage.rep_header(http_msg, name, regex, replace)
3634
3635 Matches the regular expression in all occurrences of header field **name**
3636 according to regex **regex**, and replaces them with the string **replace**.
3637 The replacement value can contain back references like \1, \2, ... This
3638 function acts on whole header lines, regardless of the number of values they
3639 may contain.
3640
3641 :param class_httpmessage http_msg: The manipulated HTTP message.
3642 :param string name: The header name.
3643 :param string regex: The match regular expression.
3644 :param string replace: The replacement value.
3645
3646.. js:function:: HTTPMessage.rep_value(http_msg, name, regex, replace)
3647
3648 Matches the regular expression on every comma-delimited value of header field
3649 **name** according to regex **regex**, and replaces them with the string
3650 **replace**. The replacement value can contain back references like \1, \2,
3651 ...
3652
3653 :param class_httpmessage http_msg: The manipulated HTTP message.
3654 :param string name: The header name.
3655 :param string regex: The match regular expression.
3656 :param string replace: The replacement value.
3657
3658.. js:function:: HTTPMessage.send(http_msg, string)
3659
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02003660 This function requires immediate send of the string **string**. It means the
Ilya Shipitsinff0f2782021-08-22 22:18:07 +05003661 string is copied at the beginning of incoming data of the HTTP message
Christopher Faulet5a2c6612021-08-15 20:35:25 +02003662 **http_msg** and immediately forwarded. Because it is called in the filter
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02003663 context, it never yields.
Christopher Faulet5a2c6612021-08-15 20:35:25 +02003664
3665 :param class_httpmessage http_msg: The manipulated HTTP message.
3666 :param string string: The data to send.
3667 :returns: an integer containing the amount of bytes copied or -1.
3668
3669.. js:function:: HTTPMessage.set(http_msg, string[, offset[, length]])
3670
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02003671 This function replaces **length** bytes of incoming data of the HTTP message
Christopher Faulet5a2c6612021-08-15 20:35:25 +02003672 **http_msg**, starting at offset **offset**, by the string **string**. The
3673 function returns the copied length on success or -1 if data cannot be copied.
3674
3675 By default, if no length is provided, all incoming data, starting at the given
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02003676 offset, are replaced. Not providing an offset is the same as setting it
Ilya Shipitsinff0f2782021-08-22 22:18:07 +05003677 to 0. A positive offset is relative to the beginning of incoming data of the
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02003678 HTTP message while negative offset is relative to the end.
Christopher Faulet5a2c6612021-08-15 20:35:25 +02003679
3680 :param class_httpmessage http_msg: The manipulated HTTP message.
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02003681 :param string string: The data to copy into incoming data.
3682 :param integer offset: *optional* The offset in incoming data where to start
Christopher Faulet5a2c6612021-08-15 20:35:25 +02003683 the data replacement. 0 by default. May be negative to
3684 be relative to the end of incoming data.
3685 :param integer length: *optional* The length of data to replace. All incoming
3686 data by default.
3687 :returns: an integer containing the amount of bytes copied or -1.
3688
3689.. js:function:: HTTPMessage.set_eom(http_msg)
3690
3691 This function set the end of message for the HTTP message **http_msg**.
3692
3693 :param class_httpmessage http_msg: The manipulated HTTP message.
3694
3695.. js:function:: HTTPMessage.set_header(http_msg, name, value)
3696
3697 This variable replace all occurrence of all header matching the name **name**,
3698 by only one containing the value **value**.
3699
3700 :param class_httpmessage http_msg: The manipulated HTTP message.
3701 :param string name: The header name.
3702 :param string value: The header value.
3703
3704 This function does the same work as the following code:
3705
3706.. code-block:: lua
3707
3708 http_msg:del_header("header")
3709 http_msg:add_header("header", "value")
3710..
3711
3712.. js:function:: HTTPMessage.set_method(http_msg, method)
3713
3714 Rewrites the request method with the string **method**. The HTTP message
3715 **http_msg** must be the request.
3716
3717 :param class_httpmessage http_msg: The manipulated HTTP message.
3718 :param string method: The new method.
3719
3720.. js:function:: HTTPMessage.set_path(http_msg, path)
3721
3722 Rewrites the request path with the string **path**. The HTTP message
3723 **http_msg** must be the request.
3724
3725 :param class_httpmessage http_msg: The manipulated HTTP message.
3726 :param string method: The new method.
3727
3728.. js:function:: HTTPMessage.set_query(http_msg, query)
3729
3730 Rewrites the request's query string which appears after the first question
3731 mark ("?") with the string **query**. The HTTP message **http_msg** must be
3732 the request.
3733
3734 :param class_httpmessage http_msg: The manipulated HTTP message.
3735 :param string query: The new query.
3736
3737.. js:function:: HTTPMessage.set_status(http_msg, status[, reason])
3738
Ilya Shipitsinff0f2782021-08-22 22:18:07 +05003739 Rewrites the response status code with the integer **code** and optional the
Christopher Faulet5a2c6612021-08-15 20:35:25 +02003740 reason **reason**. If no custom reason is provided, it will be generated from
3741 the status. The HTTP message **http_msg** must be the response.
3742
3743 :param class_httpmessage http_msg: The manipulated HTTP message.
3744 :param integer status: The new response status code.
3745 :param string reason: The new response reason (optional).
3746
3747.. js:function:: HTTPMessage.set_uri(http_msg, uri)
3748
3749 Rewrites the request URI with the string **uri**. The HTTP message
3750 **http_msg** must be the request.
3751
3752 :param class_httpmessage http_msg: The manipulated HTTP message.
3753 :param string uri: The new uri.
3754
3755.. js:function:: HTTPMessage.unset_eom(http_msg)
3756
3757 This function remove the end of message for the HTTP message **http_msg**.
3758
3759 :param class_httpmessage http_msg: The manipulated HTTP message.
3760
William Lallemand10cea5c2022-03-30 16:02:43 +02003761.. _CertCache_class:
3762
3763CertCache class
3764================
3765
3766.. js:class:: CertCache
3767
3768 This class allows to update an SSL certificate file in the memory of the
3769 current HAProxy process. It will do the same as "set ssl cert" + "commit ssl
3770 cert" over the HAProxy CLI.
3771
3772.. js:function:: CertCache.set(certificate)
3773
3774 This function updates a certificate in memory.
3775
3776 :param table certificate: A table containing the fields to update.
3777 :param string certificate.filename: The mandatory filename of the certificate
3778 to update, it must already exist in memory.
3779 :param string certificate.crt: A certificate in the PEM format. It can also
3780 contain a private key.
3781 :param string certificate.key: A private key in the PEM format.
3782 :param string certificate.ocsp: An OCSP response in base64. (cf management.txt)
3783 :param string certificate.issuer: The certificate of the OCSP issuer.
3784 :param string certificate.sctl: An SCTL file.
3785
3786.. code-block:: lua
3787
3788 CertCache.set{filename="certs/localhost9994.pem.rsa", crt=crt}
3789
3790
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01003791External Lua libraries
3792======================
3793
3794A lot of useful lua libraries can be found here:
3795
Aurelien DARRAGON846fc7d2022-10-14 08:48:57 +02003796* Lua toolbox has been superseded by `https://luarocks.org/ <https://luarocks.org/>`_
3797 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 +01003798
Ilya Shipitsin2075ca82020-03-06 23:22:22 +05003799Redis client library:
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01003800
3801* `https://github.com/nrk/redis-lua <https://github.com/nrk/redis-lua>`_
3802
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02003803This is an example about the usage of the Redis library within HAProxy. Note that
3804each call to any function of this library can throw an error if the socket
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01003805connection fails.
3806
3807.. code-block:: lua
3808
3809 -- load the redis library
3810 local redis = require("redis");
3811
3812 function do_something(txn)
3813
3814 -- create and connect new tcp socket
3815 local tcp = core.tcp();
3816 tcp:settimeout(1);
3817 tcp:connect("127.0.0.1", 6379);
3818
3819 -- use the redis library with this new socket
3820 local client = redis.connect({socket=tcp});
3821 client:ping();
3822
3823 end
3824
3825OpenSSL:
3826
3827* `http://mkottman.github.io/luacrypto/index.html
3828 <http://mkottman.github.io/luacrypto/index.html>`_
3829
3830* `https://github.com/brunoos/luasec/wiki
3831 <https://github.com/brunoos/luasec/wiki>`_