blob: 917b68481a82b26a4ce005e81b1f17c31d7967ca [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
Aurelien DARRAGONb8038992023-03-09 16:48:30 +0100723.. js:function:: core.register_task(func[, arg1[, arg2[, ...[, arg4]]]])
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100724
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
Aurelien DARRAGONb8038992023-03-09 16:48:30 +0100731 :param function func: is the Lua function called to work as an async task.
732
733 Up to 4 optional arguments (all types supported) may be passed to the function.
734 (They will be passed as-is to the task function)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100735
736 The prototype of the Lua function used as argument is:
737
738.. code-block:: lua
739
Aurelien DARRAGONb8038992023-03-09 16:48:30 +0100740 function([arg1[, arg2[, ...[, arg4]]]])
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100741..
742
Aurelien DARRAGONb8038992023-03-09 16:48:30 +0100743 It takes up to 4 optional arguments (provided when registering), and no output is expected.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100744
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +0100745.. js:function:: core.register_cli([path], usage, func)
746
747 **context**: body
748
Aurelien DARRAGON53901f42022-10-13 19:49:42 +0200749 Register a custom cli that will be available from haproxy stats socket.
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +0100750
751 :param array path: is the sequence of word for which the cli execute the Lua
752 binding.
753 :param string usage: is the usage message displayed in the help.
754 :param function func: is the Lua function called to handle the CLI commands.
755
756 The prototype of the Lua function used as argument is:
757
758.. code-block:: lua
759
760 function(AppletTCP, [arg1, [arg2, [...]]])
761..
762
763 I/O are managed with the :ref:`applettcp_class` object. Args are given as
Bertrand Jacquin874a35c2018-09-10 21:26:07 +0100764 parameter. The args embed the registered path. If the path is declared like
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +0100765 this:
766
767.. code-block:: lua
768
769 core.register_cli({"show", "ssl", "stats"}, "Display SSL stats..", function(applet, arg1, arg2, arg3, arg4, arg5)
770 end)
771..
772
773 And we execute this in the prompt:
774
775.. code-block:: text
776
777 > prompt
778 > show ssl stats all
779..
780
Bertrand Jacquin874a35c2018-09-10 21:26:07 +0100781 Then, arg1, arg2 and arg3 will contains respectively "show", "ssl" and "stats".
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +0100782 arg4 will contain "all". arg5 contains nil.
783
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100784.. js:function:: core.set_nice(nice)
785
786 **context**: task, action, sample-fetch, converter
787
788 Change the nice of the current task or current session.
Thierry FOURNIER2e4893c2015-03-18 13:37:27 +0100789
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100790 :param integer nice: the nice value, it must be between -1024 and 1024.
791
792.. js:function:: core.set_map(filename, key, value)
793
794 **context**: init, task, action, sample-fetch, converter
795
Bertrand Jacquin874a35c2018-09-10 21:26:07 +0100796 Set the value *value* associated to the key *key* in the map referenced by
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100797 *filename*.
798
Thierry FOURNIERdc595002015-12-21 11:13:52 +0100799 :param string filename: the Map reference
800 :param string key: the key to set or replace
801 :param string value: the associated value
802
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100803.. js:function:: core.sleep(int seconds)
804
805 **context**: body, init, task, action
806
807 The `core.sleep()` functions stop the Lua execution between specified seconds.
808
809 :param integer seconds: the required seconds.
810
811.. js:function:: core.tcp()
812
813 **context**: init, task, action
814
815 This function returns a new object of a *socket* class.
816
Thierry FOURNIERdc595002015-12-21 11:13:52 +0100817 :returns: A :ref:`socket_class` object.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100818
William Lallemand00a15022021-11-19 16:02:44 +0100819.. js:function:: core.httpclient()
820
821 **context**: init, task, action
822
823 This function returns a new object of a *httpclient* class.
824
825 :returns: A :ref:`httpclient_class` object.
826
Thierry Fournier1de16592016-01-27 09:49:07 +0100827.. js:function:: core.concat()
828
829 **context**: body, init, task, action, sample-fetch, converter
830
Bertrand Jacquin874a35c2018-09-10 21:26:07 +0100831 This function returns a new concat object.
Thierry Fournier1de16592016-01-27 09:49:07 +0100832
833 :returns: A :ref:`concat_class` object.
834
Thierry FOURNIER0a99b892015-08-26 00:14:17 +0200835.. js:function:: core.done(data)
836
837 **context**: body, init, task, action, sample-fetch, converter
838
839 :param any data: Return some data for the caller. It is useful with
840 sample-fetches and sample-converters.
841
842 Immediately stops the current Lua execution and returns to the caller which
843 may be a sample fetch, a converter or an action and returns the specified
Thierry Fournier4234dbd2020-11-28 13:18:23 +0100844 value (ignored for actions and init). It is used when the LUA process finishes
845 its work and wants to give back the control to HAProxy without executing the
Thierry FOURNIER0a99b892015-08-26 00:14:17 +0200846 remaining code. It can be seen as a multi-level "return".
847
Thierry FOURNIER486f5a02015-03-16 15:13:03 +0100848.. js:function:: core.yield()
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100849
850 **context**: task, action, sample-fetch, converter
851
852 Give back the hand at the HAProxy scheduler. It is used when the LUA
853 processing consumes a lot of processing time.
854
Thierry FOURNIER / OZON.IO62fec752016-11-10 20:38:11 +0100855.. js:function:: core.parse_addr(address)
856
857 **context**: body, init, task, action, sample-fetch, converter
858
859 :param network: is a string describing an ipv4 or ipv6 address and optionally
860 its network length, like this: "127.0.0.1/8" or "aaaa::1234/32".
861 :returns: a userdata containing network or nil if an error occurs.
862
Bertrand Jacquin874a35c2018-09-10 21:26:07 +0100863 Parse ipv4 or ipv6 addresses and its facultative associated network.
Thierry FOURNIER / OZON.IO62fec752016-11-10 20:38:11 +0100864
865.. js:function:: core.match_addr(addr1, addr2)
866
867 **context**: body, init, task, action, sample-fetch, converter
868
869 :param addr1: is an address created with "core.parse_addr".
870 :param addr2: is an address created with "core.parse_addr".
Bertrand Jacquin874a35c2018-09-10 21:26:07 +0100871 :returns: boolean, true if the network of the addresses match, else returns
Thierry FOURNIER / OZON.IO62fec752016-11-10 20:38:11 +0100872 false.
873
Ilya Shipitsin2075ca82020-03-06 23:22:22 +0500874 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 +0100875 of network is not important.
876
Thierry FOURNIER / OZON.IO8a1027a2016-11-24 20:48:38 +0100877.. js:function:: core.tokenize(str, separators [, noblank])
878
879 **context**: body, init, task, action, sample-fetch, converter
880
881 This function is useful for tokenizing an entry, or splitting some messages.
882 :param string str: The string which will be split.
883 :param string separators: A string containing a list of separators.
884 :param boolean noblank: Ignore empty entries.
885 :returns: an array of string.
886
887 For example:
888
889.. code-block:: lua
890
891 local array = core.tokenize("This function is useful, for tokenizing an entry.", "., ", true)
892 print_r(array)
893..
894
895 Returns this array:
896
897.. code-block:: text
898
899 (table) table: 0x21c01e0 [
900 1: (string) "This"
901 2: (string) "function"
902 3: (string) "is"
903 4: (string) "useful"
904 5: (string) "for"
905 6: (string) "tokenizing"
906 7: (string) "an"
907 8: (string) "entry"
908 ]
909..
910
Thierry Fournierf61aa632016-02-19 20:56:00 +0100911.. _proxy_class:
912
913Proxy class
914============
915
916.. js:class:: Proxy
917
918 This class provides a way for manipulating proxy and retrieving information
919 like statistics.
920
Thierry FOURNIER817e7592017-07-24 14:35:04 +0200921.. js:attribute:: Proxy.name
922
923 Contain the name of the proxy.
924
Aurelien DARRAGONc4b24372023-03-02 12:00:06 +0100925 .. warning::
926 This attribute is now deprecated and will eventually be removed.
927 Please use :js:func:`Proxy.get_name()` function instead.
928
Thierry Fournierb0467732022-10-07 12:07:24 +0200929.. js:function:: Proxy.get_name()
930
931 Returns the name of the proxy.
932
Baptiste Assmann46c72552017-10-26 21:51:58 +0200933.. js:attribute:: Proxy.uuid
934
935 Contain the unique identifier of the proxy.
936
Aurelien DARRAGONc4b24372023-03-02 12:00:06 +0100937 .. warning::
938 This attribute is now deprecated and will eventually be removed.
939 Please use :js:func:`Proxy.get_uuid()` function instead.
940
Thierry Fournierb0467732022-10-07 12:07:24 +0200941.. js:function:: Proxy.get_uuid()
942
943 Returns the unique identifier of the proxy.
944
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +0100945.. js:attribute:: Proxy.servers
946
Patrick Hemmerc6a1d712018-05-01 21:30:41 -0400947 Contain a table with the attached servers. The table is indexed by server
948 name, and each server entry is an object of type :ref:`server_class`.
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +0100949
Adis Nezirovic8878f8e2018-07-13 12:18:33 +0200950.. js:attribute:: Proxy.stktable
951
952 Contains a stick table object attached to the proxy.
953
Thierry Fournierff480422016-02-25 08:36:46 +0100954.. js:attribute:: Proxy.listeners
955
Patrick Hemmerc6a1d712018-05-01 21:30:41 -0400956 Contain a table with the attached listeners. The table is indexed by listener
957 name, and each each listeners entry is an object of type
958 :ref:`listener_class`.
Thierry Fournierff480422016-02-25 08:36:46 +0100959
Thierry Fournierf61aa632016-02-19 20:56:00 +0100960.. js:function:: Proxy.pause(px)
961
962 Pause the proxy. See the management socket documentation for more information.
963
964 :param class_proxy px: A :ref:`proxy_class` which indicates the manipulated
965 proxy.
966
967.. js:function:: Proxy.resume(px)
968
969 Resume the proxy. See the management socket documentation for more
970 information.
971
972 :param class_proxy px: A :ref:`proxy_class` which indicates the manipulated
973 proxy.
974
975.. js:function:: Proxy.stop(px)
976
977 Stop the proxy. See the management socket documentation for more information.
978
979 :param class_proxy px: A :ref:`proxy_class` which indicates the manipulated
980 proxy.
981
982.. js:function:: Proxy.shut_bcksess(px)
983
984 Kill the session attached to a backup server. See the management socket
985 documentation for more information.
986
987 :param class_proxy px: A :ref:`proxy_class` which indicates the manipulated
988 proxy.
989
990.. js:function:: Proxy.get_cap(px)
991
992 Returns a string describing the capabilities of the proxy.
993
994 :param class_proxy px: A :ref:`proxy_class` which indicates the manipulated
995 proxy.
996 :returns: a string "frontend", "backend", "proxy" or "ruleset".
997
998.. js:function:: Proxy.get_mode(px)
999
1000 Returns a string describing the mode of the current proxy.
1001
1002 :param class_proxy px: A :ref:`proxy_class` which indicates the manipulated
1003 proxy.
1004 :returns: a string "tcp", "http", "health" or "unknown"
1005
1006.. js:function:: Proxy.get_stats(px)
1007
Bertrand Jacquin874a35c2018-09-10 21:26:07 +01001008 Returns a table containing the proxy statistics. The statistics returned are
Thierry Fournierf61aa632016-02-19 20:56:00 +01001009 not the same if the proxy is frontend or a backend.
1010
1011 :param class_proxy px: A :ref:`proxy_class` which indicates the manipulated
1012 proxy.
Patrick Hemmerc6a1d712018-05-01 21:30:41 -04001013 :returns: a key/value table containing stats
Thierry Fournierf61aa632016-02-19 20:56:00 +01001014
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001015.. _server_class:
1016
1017Server class
1018============
1019
Patrick Hemmerc6a1d712018-05-01 21:30:41 -04001020.. js:class:: Server
1021
1022 This class provides a way for manipulating servers and retrieving information.
1023
Patrick Hemmera62ae7e2018-04-29 14:23:48 -04001024.. js:attribute:: Server.name
1025
1026 Contain the name of the server.
1027
Aurelien DARRAGONc4b24372023-03-02 12:00:06 +01001028 .. warning::
1029 This attribute is now deprecated and will eventually be removed.
1030 Please use :js:func:`Server.get_name()` function instead.
1031
Thierry Fournierb0467732022-10-07 12:07:24 +02001032.. js:function:: Server.get_name(sv)
1033
1034 Returns the name of the server.
1035
Patrick Hemmera62ae7e2018-04-29 14:23:48 -04001036.. js:attribute:: Server.puid
1037
1038 Contain the proxy unique identifier of the server.
1039
Aurelien DARRAGONc4b24372023-03-02 12:00:06 +01001040 .. warning::
1041 This attribute is now deprecated and will eventually be removed.
1042 Please use :js:func:`Server.get_puid()` function instead.
1043
Thierry Fournierb0467732022-10-07 12:07:24 +02001044.. js:function:: Server.get_puid(sv)
1045
1046 Returns the proxy unique identifier of the server.
1047
Aurelien DARRAGON94ee6632023-03-10 15:11:27 +01001048.. js:function:: Server.get_rid(sv)
1049
1050 Returns the rid (revision ID) of the server.
1051 It is an unsigned integer that is set upon server creation. Value is derived
1052 from a global counter that starts at 0 and is incremented each time one or
1053 multiple server deletions are followed by a server addition (meaning that
1054 old name/id reuse could occur).
1055
1056 Combining server name/id with server rid yields a process-wide unique
1057 identifier.
1058
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001059.. js:function:: Server.is_draining(sv)
1060
Patrick Hemmerc6a1d712018-05-01 21:30:41 -04001061 Return true if the server is currently draining sticky connections.
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001062
1063 :param class_server sv: A :ref:`server_class` which indicates the manipulated
1064 server.
1065 :returns: a boolean
1066
Patrick Hemmer32d539f2018-04-29 14:25:46 -04001067.. js:function:: Server.set_maxconn(sv, weight)
1068
1069 Dynamically change the maximum connections of the server. See the management
1070 socket documentation for more information about the format of the string.
1071
1072 :param class_server sv: A :ref:`server_class` which indicates the manipulated
1073 server.
1074 :param string maxconn: A string describing the server maximum connections.
1075
1076.. js:function:: Server.get_maxconn(sv, weight)
1077
1078 This function returns an integer representing the server maximum connections.
1079
1080 :param class_server sv: A :ref:`server_class` which indicates the manipulated
1081 server.
1082 :returns: an integer.
1083
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001084.. js:function:: Server.set_weight(sv, weight)
1085
Patrick Hemmerc6a1d712018-05-01 21:30:41 -04001086 Dynamically change the weight of the server. See the management socket
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001087 documentation for more information about the format of the string.
1088
1089 :param class_server sv: A :ref:`server_class` which indicates the manipulated
1090 server.
1091 :param string weight: A string describing the server weight.
1092
1093.. js:function:: Server.get_weight(sv)
1094
Patrick Hemmerc6a1d712018-05-01 21:30:41 -04001095 This function returns an integer representing the server weight.
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001096
1097 :param class_server sv: A :ref:`server_class` which indicates the manipulated
1098 server.
1099 :returns: an integer.
1100
Joseph C. Sible49bbf522020-05-04 22:20:32 -04001101.. js:function:: Server.set_addr(sv, addr[, port])
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001102
Patrick Hemmerc6a1d712018-05-01 21:30:41 -04001103 Dynamically change the address of the server. See the management socket
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001104 documentation for more information about the format of the string.
1105
1106 :param class_server sv: A :ref:`server_class` which indicates the manipulated
1107 server.
Patrick Hemmerc6a1d712018-05-01 21:30:41 -04001108 :param string addr: A string describing the server address.
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001109
1110.. js:function:: Server.get_addr(sv)
1111
Patrick Hemmerc6a1d712018-05-01 21:30:41 -04001112 Returns a string describing the address of the server.
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001113
1114 :param class_server sv: A :ref:`server_class` which indicates the manipulated
1115 server.
1116 :returns: A string
1117
1118.. js:function:: Server.get_stats(sv)
1119
1120 Returns server statistics.
1121
1122 :param class_server sv: A :ref:`server_class` which indicates the manipulated
1123 server.
Patrick Hemmerc6a1d712018-05-01 21:30:41 -04001124 :returns: a key/value table containing stats
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001125
1126.. js:function:: Server.shut_sess(sv)
1127
1128 Shutdown all the sessions attached to the server. See the management socket
1129 documentation for more information about this function.
1130
1131 :param class_server sv: A :ref:`server_class` which indicates the manipulated
1132 server.
1133
1134.. js:function:: Server.set_drain(sv)
1135
1136 Drain sticky sessions. See the management socket documentation for more
1137 information about this function.
1138
1139 :param class_server sv: A :ref:`server_class` which indicates the manipulated
1140 server.
1141
1142.. js:function:: Server.set_maint(sv)
1143
1144 Set maintenance mode. See the management socket documentation for more
1145 information about this function.
1146
1147 :param class_server sv: A :ref:`server_class` which indicates the manipulated
1148 server.
1149
1150.. js:function:: Server.set_ready(sv)
1151
1152 Set normal mode. See the management socket documentation for more information
1153 about this function.
1154
1155 :param class_server sv: A :ref:`server_class` which indicates the manipulated
1156 server.
1157
1158.. js:function:: Server.check_enable(sv)
1159
1160 Enable health checks. See the management socket documentation for more
1161 information about this function.
1162
1163 :param class_server sv: A :ref:`server_class` which indicates the manipulated
1164 server.
1165
1166.. js:function:: Server.check_disable(sv)
1167
1168 Disable health checks. See the management socket documentation for more
1169 information about this function.
1170
1171 :param class_server sv: A :ref:`server_class` which indicates the manipulated
1172 server.
1173
1174.. js:function:: Server.check_force_up(sv)
1175
1176 Force health-check up. See the management socket documentation for more
1177 information about this function.
1178
1179 :param class_server sv: A :ref:`server_class` which indicates the manipulated
1180 server.
1181
1182.. js:function:: Server.check_force_nolb(sv)
1183
1184 Force health-check nolb mode. See the management socket documentation for more
1185 information about this function.
1186
1187 :param class_server sv: A :ref:`server_class` which indicates the manipulated
1188 server.
1189
1190.. js:function:: Server.check_force_down(sv)
1191
1192 Force health-check down. See the management socket documentation for more
1193 information about this function.
1194
1195 :param class_server sv: A :ref:`server_class` which indicates the manipulated
1196 server.
1197
1198.. js:function:: Server.agent_enable(sv)
1199
1200 Enable agent check. See the management socket documentation for more
1201 information about this function.
1202
1203 :param class_server sv: A :ref:`server_class` which indicates the manipulated
1204 server.
1205
1206.. js:function:: Server.agent_disable(sv)
1207
1208 Disable agent check. See the management socket documentation for more
1209 information about this function.
1210
1211 :param class_server sv: A :ref:`server_class` which indicates the manipulated
1212 server.
1213
1214.. js:function:: Server.agent_force_up(sv)
1215
1216 Force agent check up. See the management socket documentation for more
1217 information about this function.
1218
1219 :param class_server sv: A :ref:`server_class` which indicates the manipulated
1220 server.
1221
1222.. js:function:: Server.agent_force_down(sv)
1223
1224 Force agent check down. See the management socket documentation for more
1225 information about this function.
1226
1227 :param class_server sv: A :ref:`server_class` which indicates the manipulated
1228 server.
1229
Thierry Fournierff480422016-02-25 08:36:46 +01001230.. _listener_class:
1231
1232Listener class
1233==============
1234
1235.. js:function:: Listener.get_stats(ls)
1236
1237 Returns server statistics.
1238
1239 :param class_listener ls: A :ref:`listener_class` which indicates the
1240 manipulated listener.
Patrick Hemmerc6a1d712018-05-01 21:30:41 -04001241 :returns: a key/value table containing stats
Thierry Fournierff480422016-02-25 08:36:46 +01001242
Thierry Fournier1de16592016-01-27 09:49:07 +01001243.. _concat_class:
1244
1245Concat class
1246============
1247
1248.. js:class:: Concat
1249
1250 This class provides a fast way for string concatenation. The way using native
1251 Lua concatenation like the code below is slow for some reasons.
1252
1253.. code-block:: lua
1254
1255 str = "string1"
1256 str = str .. ", string2"
1257 str = str .. ", string3"
1258..
1259
1260 For each concatenation, Lua:
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02001261 - allocates memory for the result,
1262 - catenates the two string copying the strings in the new memory block,
1263 - frees the old memory block containing the string which is no longer used.
1264
Thierry Fournier1de16592016-01-27 09:49:07 +01001265 This process does many memory move, allocation and free. In addition, the
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02001266 memory is not really freed, it is just marked as unused and waits for the
Thierry Fournier1de16592016-01-27 09:49:07 +01001267 garbage collector.
1268
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02001269 The Concat class provides an alternative way to concatenate strings. It uses
Thierry Fournier1de16592016-01-27 09:49:07 +01001270 the internal Lua mechanism (it does not allocate memory), but it doesn't copy
1271 the data more than once.
1272
1273 On my computer, the following loops spends 0.2s for the Concat method and
1274 18.5s for the pure Lua implementation. So, the Concat class is about 1000x
1275 faster than the embedded solution.
1276
1277.. code-block:: lua
1278
1279 for j = 1, 100 do
1280 c = core.concat()
1281 for i = 1, 20000 do
1282 c:add("#####")
1283 end
1284 end
1285..
1286
1287.. code-block:: lua
1288
1289 for j = 1, 100 do
1290 c = ""
1291 for i = 1, 20000 do
1292 c = c .. "#####"
1293 end
1294 end
1295..
1296
1297.. js:function:: Concat.add(concat, string)
1298
1299 This function adds a string to the current concatenated string.
1300
1301 :param class_concat concat: A :ref:`concat_class` which contains the currently
Ilya Shipitsin2075ca82020-03-06 23:22:22 +05001302 built string.
Bertrand Jacquin874a35c2018-09-10 21:26:07 +01001303 :param string string: A new string to concatenate to the current built
Thierry Fournier1de16592016-01-27 09:49:07 +01001304 string.
1305
1306.. js:function:: Concat.dump(concat)
1307
Bertrand Jacquin874a35c2018-09-10 21:26:07 +01001308 This function returns the concatenated string.
Thierry Fournier1de16592016-01-27 09:49:07 +01001309
1310 :param class_concat concat: A :ref:`concat_class` which contains the currently
Ilya Shipitsin2075ca82020-03-06 23:22:22 +05001311 built string.
Thierry Fournier1de16592016-01-27 09:49:07 +01001312 :returns: the concatenated string
1313
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001314.. _fetches_class:
1315
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001316Fetches class
1317=============
1318
1319.. js:class:: Fetches
1320
1321 This class contains a lot of internal HAProxy sample fetches. See the
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02001322 HAProxy "configuration.txt" documentation for more information.
1323 (chapters 7.3.2 to 7.3.6)
Thierry FOURNIER2e4893c2015-03-18 13:37:27 +01001324
Christopher Faulet1e9b1b62021-08-11 10:14:30 +02001325 .. warning::
1326 some sample fetches are not available in some context. These limitations
1327 are specified in this documentation when they're useful.
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001328
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01001329 :see: :js:attr:`TXN.f`
1330 :see: :js:attr:`TXN.sf`
Thierry FOURNIER2e4893c2015-03-18 13:37:27 +01001331
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02001332 Fetches are useful to:
Thierry FOURNIER2e4893c2015-03-18 13:37:27 +01001333
1334 * get system time,
1335 * get environment variable,
1336 * get random numbers,
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02001337 * know backend status like the number of users in queue or the number of
Thierry FOURNIER2e4893c2015-03-18 13:37:27 +01001338 connections established,
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02001339 * get client information like ip source or destination,
Thierry FOURNIER2e4893c2015-03-18 13:37:27 +01001340 * deal with stick tables,
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02001341 * fetch established SSL information,
1342 * fetch HTTP information like headers or method.
Thierry FOURNIER2e4893c2015-03-18 13:37:27 +01001343
1344.. code-block:: lua
1345
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001346 function action(txn)
1347 -- Get source IP
1348 local clientip = txn.f:src()
1349 end
Thierry FOURNIER2e4893c2015-03-18 13:37:27 +01001350..
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001351
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001352.. _converters_class:
1353
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001354Converters class
1355================
1356
1357.. js:class:: Converters
1358
1359 This class contains a lot of internal HAProxy sample converters. See the
Thierry FOURNIER2e4893c2015-03-18 13:37:27 +01001360 HAProxy documentation "configuration.txt" for more information about her
1361 usage. Its the chapter 7.3.1.
1362
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01001363 :see: :js:attr:`TXN.c`
1364 :see: :js:attr:`TXN.sc`
Thierry FOURNIER2e4893c2015-03-18 13:37:27 +01001365
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02001366 Converters provides stateful transformation. They are useful to:
Thierry FOURNIER2e4893c2015-03-18 13:37:27 +01001367
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02001368 * convert input to base64,
1369 * apply hash on input string (djb2, crc32, sdbm, wt6),
Thierry FOURNIER2e4893c2015-03-18 13:37:27 +01001370 * format date,
1371 * json escape,
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02001372 * extract preferred language comparing two lists,
Thierry FOURNIER2e4893c2015-03-18 13:37:27 +01001373 * turn to lower or upper chars,
1374 * deal with stick tables.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001375
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001376.. _channel_class:
1377
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001378Channel class
1379=============
1380
1381.. js:class:: Channel
1382
Christopher Faulet5a2c6612021-08-15 20:35:25 +02001383 **context**: action, sample-fetch, convert, filter
1384
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001385 HAProxy uses two buffers for the processing of the requests. The first one is
1386 used with the request data (from the client to the server) and the second is
1387 used for the response data (from the server to the client).
1388
1389 Each buffer contains two types of data. The first type is the incoming data
1390 waiting for a processing. The second part is the outgoing data already
1391 processed. Usually, the incoming data is processed, after it is tagged as
1392 outgoing data, and finally it is sent. The following functions provides tools
1393 for manipulating these data in a buffer.
1394
1395 The following diagram shows where the channel class function are applied.
1396
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001397 .. image:: _static/channel.png
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001398
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001399 .. warning::
1400 It is not possible to read from the response in request action, and it is
Boyang Li60cfe8b2022-05-10 18:11:00 +00001401 not possible to read from the request channel in response action.
Christopher Faulet09530392021-06-14 11:43:18 +02001402
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001403 .. warning::
1404 It is forbidden to alter the Channels buffer from HTTP contexts. So only
1405 :js:func:`Channel.input`, :js:func:`Channel.output`,
1406 :js:func:`Channel.may_recv`, :js:func:`Channel.is_full` and
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02001407 :js:func:`Channel.is_resp` can be called from a HTTP context.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001408
Christopher Faulet5a2c6612021-08-15 20:35:25 +02001409 All the functions provided by this class are available in the
1410 **sample-fetches**, **actions** and **filters** contexts. For **filters**,
1411 incoming data (offset and length) are relative to the filter. Some functions
Boyang Li60cfe8b2022-05-10 18:11:00 +00001412 may yield, but only for **actions**. Yield is not possible for
Christopher Faulet5a2c6612021-08-15 20:35:25 +02001413 **sample-fetches**, **converters** and **filters**.
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001414
1415.. js:function:: Channel.append(channel, string)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001416
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001417 This function copies the string **string** at the end of incoming data of the
1418 channel buffer. The function returns the copied length on success or -1 if
1419 data cannot be copied.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001420
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001421 Same that :js:func:`Channel.insert(channel, string, channel:input())`.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001422
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001423 :param class_channel channel: The manipulated Channel.
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02001424 :param string string: The data to copy at the end of incoming data.
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001425 :returns: an integer containing the amount of bytes copied or -1.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001426
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001427.. js:function:: Channel.data(channel [, offset [, length]])
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001428
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001429 This function returns **length** bytes of incoming data from the channel
1430 buffer, starting at the offset **offset**. The data are not removed from the
1431 buffer.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001432
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001433 By default, if no length is provided, all incoming data found, starting at the
1434 given offset, are returned. If **length** is set to -1, the function tries to
Christopher Faulet5a2c6612021-08-15 20:35:25 +02001435 retrieve a maximum of data and, if called by an action, it yields if
1436 necessary. It also waits for more data if the requested length exceeds the
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02001437 available amount of incoming data. Not providing an offset is the same as
Ilya Shipitsinff0f2782021-08-22 22:18:07 +05001438 setting it to 0. A positive offset is relative to the beginning of incoming
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02001439 data of the channel buffer while negative offset is relative to the end.
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001440
1441 If there is no incoming data and the channel can't receive more data, a 'nil'
1442 value is returned.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001443
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001444 :param class_channel channel: The manipulated Channel.
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001445 :param integer offset: *optional* The offset in incoming data to start to get
1446 data. 0 by default. May be negative to be relative to
1447 the end of incoming data.
1448 :param integer length: *optional* The expected length of data to retrieve. All
1449 incoming data by default. May be set to -1 to get a
1450 maximum of data.
1451 :returns: a string containing the data found or nil.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001452
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001453.. js:function:: Channel.forward(channel, length)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001454
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001455 This function forwards **length** bytes of data from the channel buffer. If
Christopher Faulet5a2c6612021-08-15 20:35:25 +02001456 the requested length exceeds the available amount of incoming data, and if
1457 called by an action, the function yields, waiting for more data to forward. It
1458 returns the amount of data forwarded.
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001459
1460 :param class_channel channel: The manipulated Channel.
1461 :param integer int: The amount of data to forward.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001462
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001463.. js:function:: Channel.input(channel)
1464
Christopher Faulet5a2c6612021-08-15 20:35:25 +02001465 This function returns the length of incoming data in the channel buffer. When
1466 called by a filter, this value is relative to the filter.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001467
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001468 :param class_channel channel: The manipulated Channel.
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001469 :returns: an integer containing the amount of available bytes.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001470
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001471.. js:function:: Channel.insert(channel, string [, offset])
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001472
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001473 This function copies the string **string** at the offset **offset** in
1474 incoming data of the channel buffer. The function returns the copied length on
1475 success or -1 if data cannot be copied.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001476
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001477 By default, if no offset is provided, the string is copied in front of
Ilya Shipitsinff0f2782021-08-22 22:18:07 +05001478 incoming data. A positive offset is relative to the beginning of incoming data
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001479 of the channel buffer while negative offset is relative to their end.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001480
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001481 :param class_channel channel: The manipulated Channel.
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02001482 :param string string: The data to copy into incoming data.
1483 :param integer offset: *optional* The offset in incoming data where to copy
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001484 data. 0 by default. May be negative to be relative to
1485 the end of incoming data.
Pieter Baauw4d7f7662015-11-08 16:38:08 +01001486 :returns: an integer containing the amount of bytes copied or -1.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001487
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001488.. js:function:: Channel.is_full(channel)
1489
1490 This function returns true if the channel buffer is full.
1491
Christopher Faulet5a2c6612021-08-15 20:35:25 +02001492 :param class_channel channel: The manipulated Channel.
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001493 :returns: a boolean
1494
1495.. js:function:: Channel.is_resp(channel)
1496
1497 This function returns true if the channel is the response one.
1498
Christopher Faulet5a2c6612021-08-15 20:35:25 +02001499 :param class_channel channel: The manipulated Channel.
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001500 :returns: a boolean
1501
1502.. js:function:: Channel.line(channel [, offset [, length]])
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001503
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001504 This function parses **length** bytes of incoming data of the channel buffer,
1505 starting at offset **offset**, and returns the first line found, including the
Christopher Faulet5a2c6612021-08-15 20:35:25 +02001506 '\\n'. The data are not removed from the buffer. If no line is found, all data
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001507 are returned.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001508
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001509 By default, if no length is provided, all incoming data, starting at the given
1510 offset, are evaluated. If **length** is set to -1, the function tries to
Christopher Faulet5a2c6612021-08-15 20:35:25 +02001511 retrieve a maximum of data and, if called by an action, yields if
1512 necessary. It also waits for more data if the requested length exceeds the
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02001513 available amount of incoming data. Not providing an offset is the same as
Ilya Shipitsinff0f2782021-08-22 22:18:07 +05001514 setting it to 0. A positive offset is relative to the beginning of incoming
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02001515 data of the channel buffer while negative offset is relative to the end.
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001516
1517 If there is no incoming data and the channel can't receive more data, a 'nil'
1518 value is returned.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001519
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001520 :param class_channel channel: The manipulated Channel.
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02001521 :param integer offset: *optional* The offset in incoming data to start to
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001522 parse data. 0 by default. May be negative to be
1523 relative to the end of incoming data.
1524 :param integer length: *optional* The length of data to parse. All incoming
1525 data by default. May be set to -1 to get a maximum of
1526 data.
1527 :returns: a string containing the line found or nil.
1528
1529.. js:function:: Channel.may_recv(channel)
1530
1531 This function returns true if the channel may still receive data.
1532
Christopher Faulet5a2c6612021-08-15 20:35:25 +02001533 :param class_channel channel: The manipulated Channel.
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001534 :returns: a boolean
1535
1536.. js:function:: Channel.output(channel)
1537
Christopher Faulet5a2c6612021-08-15 20:35:25 +02001538 This function returns the length of outgoing data of the channel buffer. When
1539 called by a filter, this value is relative to the filter.
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001540
1541 :param class_channel channel: The manipulated Channel.
1542 :returns: an integer containing the amount of available bytes.
1543
1544.. js:function:: Channel.prepend(channel, string)
1545
1546 This function copies the string **string** in front of incoming data of the
1547 channel buffer. The function returns the copied length on success or -1 if
1548 data cannot be copied.
1549
1550 Same that :js:func:`Channel.insert(channel, string, 0)`.
1551
1552 :param class_channel channel: The manipulated Channel.
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02001553 :param string string: The data to copy in front of incoming data.
Pieter Baauw4d7f7662015-11-08 16:38:08 +01001554 :returns: an integer containing the amount of bytes copied or -1.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001555
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001556.. js:function:: Channel.remove(channel [, offset [, length]])
1557
1558 This function removes **length** bytes of incoming data of the channel buffer,
1559 starting at offset **offset**. This function returns number of bytes removed
1560 on success.
1561
1562 By default, if no length is provided, all incoming data, starting at the given
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02001563 offset, are removed. Not providing an offset is the same as setting it
Ilya Shipitsinff0f2782021-08-22 22:18:07 +05001564 to 0. A positive offset is relative to the beginning of incoming data of the
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02001565 channel buffer while negative offset is relative to the end.
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001566
1567 :param class_channel channel: The manipulated Channel.
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02001568 :param integer offset: *optional* The offset in incoming data where to start
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001569 to remove data. 0 by default. May be negative to
1570 be relative to the end of incoming data.
1571 :param integer length: *optional* The length of data to remove. All incoming
1572 data by default.
1573 :returns: an integer containing the amount of bytes removed.
1574
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001575.. js:function:: Channel.send(channel, string)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001576
Christopher Faulet5a2c6612021-08-15 20:35:25 +02001577 This function requires immediate send of the string **string**. It means the
Ilya Shipitsinff0f2782021-08-22 22:18:07 +05001578 string is copied at the beginning of incoming data of the channel buffer and
Christopher Faulet5a2c6612021-08-15 20:35:25 +02001579 immediately forwarded. Unless if the connection is close, and if called by an
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02001580 action, this function yields to copy and forward all the string.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001581
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001582 :param class_channel channel: The manipulated Channel.
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001583 :param string string: The data to send.
Pieter Baauw4d7f7662015-11-08 16:38:08 +01001584 :returns: an integer containing the amount of bytes copied or -1.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001585
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001586.. js:function:: Channel.set(channel, string [, offset [, length]])
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001587
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02001588 This function replaces **length** bytes of incoming data of the channel buffer,
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001589 starting at offset **offset**, by the string **string**. The function returns
1590 the copied length on success or -1 if data cannot be copied.
1591
1592 By default, if no length is provided, all incoming data, starting at the given
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02001593 offset, are replaced. Not providing an offset is the same as setting it
Ilya Shipitsinff0f2782021-08-22 22:18:07 +05001594 to 0. A positive offset is relative to the beginning of incoming data of the
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02001595 channel buffer while negative offset is relative to the end.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001596
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001597 :param class_channel channel: The manipulated Channel.
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02001598 :param string string: The data to copy into incoming data.
1599 :param integer offset: *optional* The offset in incoming data where to start
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001600 the data replacement. 0 by default. May be negative to
1601 be relative to the end of incoming data.
1602 :param integer length: *optional* The length of data to replace. All incoming
1603 data by default.
1604 :returns: an integer containing the amount of bytes copied or -1.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001605
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001606.. js:function:: Channel.dup(channel)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001607
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001608 **DEPRECATED**
1609
1610 This function returns all incoming data found in the channel buffer. The data
Boyang Li60cfe8b2022-05-10 18:11:00 +00001611 are not removed from the buffer and can be reprocessed later.
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001612
1613 If there is no incoming data and the channel can't receive more data, a 'nil'
1614 value is returned.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001615
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001616 :param class_channel channel: The manipulated Channel.
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001617 :returns: a string containing all data found or nil.
1618
1619 .. warning::
1620 This function is deprecated. :js:func:`Channel.data()` must be used
1621 instead.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001622
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001623.. js:function:: Channel.get(channel)
1624
1625 **DEPRECATED**
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001626
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001627 This function returns all incoming data found in the channel buffer and remove
1628 them from the buffer.
1629
1630 If there is no incoming data and the channel can't receive more data, a 'nil'
1631 value is returned.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001632
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001633 :param class_channel channel: The manipulated Channel.
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001634 :returns: a string containing all the data found or nil.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001635
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001636 .. warning::
1637 This function is deprecated. :js:func:`Channel.data()` must be used to
1638 retrieve data followed by a call to :js:func:`Channel:remove()` to remove
1639 data.
Thierry FOURNIER / OZON.IO65192f32016-11-07 15:28:40 +01001640
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001641 .. code-block:: lua
Thierry FOURNIER / OZON.IO65192f32016-11-07 15:28:40 +01001642
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001643 local data = chn:data()
1644 chn:remove(0, data:len())
1645
1646 ..
1647
1648.. js:function:: Channel.getline(channel)
1649
1650 **DEPRECATED**
1651
1652 This function returns the first line found in incoming data of the channel
Christopher Faulet5a2c6612021-08-15 20:35:25 +02001653 buffer, including the '\\n'. The returned data are removed from the buffer. If
1654 no line is found, and if called by an action, this function yields to wait for
1655 more data, except if the channel can't receive more data. In this case all
1656 data are returned.
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001657
1658 If there is no incoming data and the channel can't receive more data, a 'nil'
1659 value is returned.
1660
1661 :param class_channel channel: The manipulated Channel.
1662 :returns: a string containing the line found or nil.
1663
1664 .. warning::
Boyang Li60cfe8b2022-05-10 18:11:00 +00001665 This function is deprecated. :js:func:`Channel.line()` must be used to
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001666 retrieve a line followed by a call to :js:func:`Channel:remove()` to remove
1667 data.
1668
1669 .. code-block:: lua
1670
1671 local line = chn:line(0, -1)
1672 chn:remove(0, line:len())
1673
1674 ..
1675
1676.. js:function:: Channel.get_in_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 input 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.input()` must be used
1688 instead.
1689
1690.. js:function:: Channel.get_out_len(channel)
1691
Boyang Li60cfe8b2022-05-10 18:11:00 +00001692 **DEPRECATED**
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001693
Christopher Faulet5a2c6612021-08-15 20:35:25 +02001694 This function returns the length of the output part of the buffer. When called
1695 by a filter, this value is relative to the filter.
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001696
1697 :param class_channel channel: The manipulated Channel.
1698 :returns: an integer containing the amount of available bytes.
1699
1700 .. warning::
1701 This function is deprecated. :js:func:`Channel.output()` must be used
1702 instead.
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001703
1704.. _http_class:
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001705
1706HTTP class
1707==========
1708
1709.. js:class:: HTTP
1710
1711 This class contain all the HTTP manipulation functions.
1712
Pieter Baauw386a1272015-08-16 15:26:24 +02001713.. js:function:: HTTP.req_get_headers(http)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001714
Patrick Hemmerc6a1d712018-05-01 21:30:41 -04001715 Returns a table containing all the request headers.
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001716
1717 :param class_http http: The related http object.
Patrick Hemmerc6a1d712018-05-01 21:30:41 -04001718 :returns: table of headers.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01001719 :see: :js:func:`HTTP.res_get_headers`
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001720
Patrick Hemmerc6a1d712018-05-01 21:30:41 -04001721 This is the form of the returned table:
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001722
1723.. code-block:: lua
1724
1725 HTTP:req_get_headers()['<header-name>'][<header-index>] = "<header-value>"
1726
1727 local hdr = HTTP:req_get_headers()
1728 hdr["host"][0] = "www.test.com"
1729 hdr["accept"][0] = "audio/basic q=1"
1730 hdr["accept"][1] = "audio/*, q=0.2"
1731 hdr["accept"][2] = "*/*, q=0.1"
1732..
1733
Pieter Baauw386a1272015-08-16 15:26:24 +02001734.. js:function:: HTTP.res_get_headers(http)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001735
Patrick Hemmerc6a1d712018-05-01 21:30:41 -04001736 Returns a table containing all the response headers.
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001737
1738 :param class_http http: The related http object.
Patrick Hemmerc6a1d712018-05-01 21:30:41 -04001739 :returns: table of headers.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01001740 :see: :js:func:`HTTP.req_get_headers`
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001741
Patrick Hemmerc6a1d712018-05-01 21:30:41 -04001742 This is the form of the returned table:
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001743
1744.. code-block:: lua
1745
1746 HTTP:res_get_headers()['<header-name>'][<header-index>] = "<header-value>"
1747
1748 local hdr = HTTP:req_get_headers()
1749 hdr["host"][0] = "www.test.com"
1750 hdr["accept"][0] = "audio/basic q=1"
1751 hdr["accept"][1] = "audio/*, q=0.2"
1752 hdr["accept"][2] = "*.*, q=0.1"
1753..
1754
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001755.. js:function:: HTTP.req_add_header(http, name, value)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001756
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02001757 Appends a HTTP header field in the request whose name is
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001758 specified in "name" and whose value is defined in "value".
1759
1760 :param class_http http: The related http object.
1761 :param string name: The header name.
1762 :param string value: The header value.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01001763 :see: :js:func:`HTTP.res_add_header`
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001764
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001765.. js:function:: HTTP.res_add_header(http, name, value)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001766
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02001767 Appends a HTTP header field in the response whose name is
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001768 specified in "name" and whose value is defined in "value".
1769
1770 :param class_http http: The related http object.
1771 :param string name: The header name.
1772 :param string value: The header value.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01001773 :see: :js:func:`HTTP.req_add_header`
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001774
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001775.. js:function:: HTTP.req_del_header(http, name)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001776
1777 Removes all HTTP header fields in the request whose name is
1778 specified in "name".
1779
1780 :param class_http http: The related http object.
1781 :param string name: The header name.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01001782 :see: :js:func:`HTTP.res_del_header`
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001783
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001784.. js:function:: HTTP.res_del_header(http, name)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001785
1786 Removes all HTTP header fields in the response whose name is
1787 specified in "name".
1788
1789 :param class_http http: The related http object.
1790 :param string name: The header name.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01001791 :see: :js:func:`HTTP.req_del_header`
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001792
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001793.. js:function:: HTTP.req_set_header(http, name, value)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001794
Bertrand Jacquin874a35c2018-09-10 21:26:07 +01001795 This variable replace all occurrence of all header "name", by only
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001796 one containing the "value".
1797
1798 :param class_http http: The related http object.
1799 :param string name: The header name.
1800 :param string value: The header value.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01001801 :see: :js:func:`HTTP.res_set_header`
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001802
Bertrand Jacquin874a35c2018-09-10 21:26:07 +01001803 This function does the same work as the following code:
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001804
1805.. code-block:: lua
1806
1807 function fcn(txn)
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001808 TXN.http:req_del_header("header")
1809 TXN.http:req_add_header("header", "value")
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001810 end
1811..
1812
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001813.. js:function:: HTTP.res_set_header(http, name, value)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001814
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02001815 This function replaces all occurrence of all header "name", by only
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001816 one containing the "value".
1817
1818 :param class_http http: The related http object.
1819 :param string name: The header name.
1820 :param string value: The header value.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01001821 :see: :js:func:`HTTP.req_rep_header()`
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001822
Pieter Baauw386a1272015-08-16 15:26:24 +02001823.. js:function:: HTTP.req_rep_header(http, name, regex, replace)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001824
1825 Matches the regular expression in all occurrences of header field "name"
1826 according to "regex", and replaces them with the "replace" argument. The
1827 replacement value can contain back references like \1, \2, ... This
1828 function works with the request.
1829
1830 :param class_http http: The related http object.
1831 :param string name: The header name.
1832 :param string regex: The match regular expression.
1833 :param string replace: The replacement value.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01001834 :see: :js:func:`HTTP.res_rep_header()`
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001835
Pieter Baauw386a1272015-08-16 15:26:24 +02001836.. js:function:: HTTP.res_rep_header(http, name, regex, string)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001837
1838 Matches the regular expression in all occurrences of header field "name"
1839 according to "regex", and replaces them with the "replace" argument. The
1840 replacement value can contain back references like \1, \2, ... This
1841 function works with the request.
1842
1843 :param class_http http: The related http object.
1844 :param string name: The header name.
1845 :param string regex: The match regular expression.
1846 :param string replace: The replacement value.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01001847 :see: :js:func:`HTTP.req_rep_header()`
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001848
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001849.. js:function:: HTTP.req_set_method(http, method)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001850
1851 Rewrites the request method with the parameter "method".
1852
1853 :param class_http http: The related http object.
1854 :param string method: The new method.
1855
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001856.. js:function:: HTTP.req_set_path(http, path)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001857
1858 Rewrites the request path with the "path" parameter.
1859
1860 :param class_http http: The related http object.
1861 :param string path: The new path.
1862
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001863.. js:function:: HTTP.req_set_query(http, query)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001864
1865 Rewrites the request's query string which appears after the first question
1866 mark ("?") with the parameter "query".
1867
1868 :param class_http http: The related http object.
1869 :param string query: The new query.
1870
Thierry FOURNIER0d79cf62015-08-26 14:20:58 +02001871.. js:function:: HTTP.req_set_uri(http, uri)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001872
1873 Rewrites the request URI with the parameter "uri".
1874
1875 :param class_http http: The related http object.
1876 :param string uri: The new uri.
1877
Robin H. Johnson52f5db22017-01-01 13:10:52 -08001878.. js:function:: HTTP.res_set_status(http, status [, reason])
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +02001879
Robin H. Johnson52f5db22017-01-01 13:10:52 -08001880 Rewrites the response status code with the parameter "code".
1881
1882 If no custom reason is provided, it will be generated from the status.
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +02001883
1884 :param class_http http: The related http object.
1885 :param integer status: The new response status code.
Robin H. Johnson52f5db22017-01-01 13:10:52 -08001886 :param string reason: The new response reason (optional).
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +02001887
William Lallemand00a15022021-11-19 16:02:44 +01001888.. _httpclient_class:
1889
1890HTTPClient class
1891================
1892
1893.. js:class:: HTTPClient
1894
1895 The httpclient class allows issue of outbound HTTP requests through a simple
1896 API without the knowledge of HAProxy internals.
1897
1898.. js:function:: HTTPClient.get(httpclient, request)
1899.. js:function:: HTTPClient.head(httpclient, request)
1900.. js:function:: HTTPClient.put(httpclient, request)
1901.. js:function:: HTTPClient.post(httpclient, request)
1902.. js:function:: HTTPClient.delete(httpclient, request)
1903
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02001904 Send a HTTP request and wait for a response. GET, HEAD PUT, POST and DELETE methods can be used.
1905 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 +01001906
William Lallemanda9256192022-10-21 11:48:24 +02001907 The HTTPClient interface is not able to decompress responses, it is not
1908 recommended to send an Accept-Encoding in the request so the response is
1909 received uncompressed.
William Lallemand00a15022021-11-19 16:02:44 +01001910
1911 :param class httpclient: Is the manipulated HTTPClient.
1912 :param table request: Is a table containing the parameters of the request that will be send.
1913 :param string request.url: Is a mandatory parameter for the request that contains the URL.
1914 :param string request.body: Is an optional parameter for the request that contains the body to send.
1915 :param table request.headers: Is an optional parameter for the request that contains the headers to send.
William Lallemand18340302022-02-23 15:57:45 +01001916 :param string request.dst: Is an optional parameter for the destination in haproxy address format.
William Lallemandb4a4ef62022-02-23 14:18:16 +01001917 :param integer request.timeout: Optional timeout parameter, set a "timeout server" on the connections.
William Lallemand00a15022021-11-19 16:02:44 +01001918 :returns: Lua table containing the response
1919
1920
1921.. code-block:: lua
1922
1923 local httpclient = core.httpclient()
William Lallemand4f4f2b72022-02-17 20:00:23 +01001924 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 +01001925
1926..
1927
1928.. code-block:: lua
1929
1930 response = {
1931 status = 400,
1932 reason = "Bad request",
1933 headers = {
1934 ["content-type"] = { "text/html" },
1935 ["cache-control"] = { "no-cache", "no-store" },
1936 },
William Lallemand4f4f2b72022-02-17 20:00:23 +01001937 body = "<html><body><h1>invalid request<h1></body></html>",
William Lallemand00a15022021-11-19 16:02:44 +01001938 }
1939..
1940
1941
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001942.. _txn_class:
1943
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001944TXN class
1945=========
1946
1947.. js:class:: TXN
1948
1949 The txn class contain all the functions relative to the http or tcp
1950 transaction (Note than a tcp stream is the same than a tcp transaction, but
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02001951 a HTTP transaction is not the same than a tcp stream).
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001952
1953 The usage of this class permits to retrieve data from the requests, alter it
1954 and forward it.
1955
1956 All the functions provided by this class are available in the context
Christopher Faulet5a2c6612021-08-15 20:35:25 +02001957 **sample-fetches**, **actions** and **filters**.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001958
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001959.. js:attribute:: TXN.c
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001960
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001961 :returns: An :ref:`converters_class`.
1962
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001963 This attribute contains a Converters class object.
1964
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001965.. js:attribute:: TXN.sc
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001966
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001967 :returns: An :ref:`converters_class`.
1968
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001969 This attribute contains a Converters class object. The functions of
1970 this object returns always a string.
1971
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001972.. js:attribute:: TXN.f
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001973
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001974 :returns: An :ref:`fetches_class`.
1975
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001976 This attribute contains a Fetches class object.
1977
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001978.. js:attribute:: TXN.sf
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001979
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001980 :returns: An :ref:`fetches_class`.
1981
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001982 This attribute contains a Fetches class object. The functions of
1983 this object returns always a string.
1984
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001985.. js:attribute:: TXN.req
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001986
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001987 :returns: An :ref:`channel_class`.
1988
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001989 This attribute contains a channel class object for the request buffer.
1990
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001991.. js:attribute:: TXN.res
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001992
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001993 :returns: An :ref:`channel_class`.
1994
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001995 This attribute contains a channel class object for the response buffer.
1996
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001997.. js:attribute:: TXN.http
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001998
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001999 :returns: An :ref:`http_class`.
2000
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02002001 This attribute contains a HTTP class object. It is available only if the
Thierry FOURNIER08504f42015-03-16 14:17:08 +01002002 proxy has the "mode http" enabled.
2003
Christopher Faulet5a2c6612021-08-15 20:35:25 +02002004.. js:attribute:: TXN.http_req
2005
2006 :returns: An :ref:`httpmessage_class`.
2007
2008 This attribute contains the request HTTPMessage class object. It is available
2009 only if the proxy has the "mode http" enabled and only in the **filters**
2010 context.
2011
2012.. js:attribute:: TXN.http_res
2013
2014 :returns: An :ref:`httpmessage_class`.
2015
2016 This attribute contains the response HTTPMessage class object. It is available
2017 only if the proxy has the "mode http" enabled and only in the **filters**
2018 context.
2019
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01002020.. js:function:: TXN.log(TXN, loglevel, msg)
2021
2022 This function sends a log. The log is sent, according with the HAProxy
2023 configuration file, on the default syslog server if it is configured and on
2024 the stderr if it is allowed.
2025
2026 :param class_txn txn: The class txn object containing the data.
Bertrand Jacquin874a35c2018-09-10 21:26:07 +01002027 :param integer loglevel: Is the log level associated with the message. It is a
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01002028 number between 0 and 7.
2029 :param string msg: The log content.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01002030 :see: :js:attr:`core.emerg`, :js:attr:`core.alert`, :js:attr:`core.crit`,
2031 :js:attr:`core.err`, :js:attr:`core.warning`, :js:attr:`core.notice`,
2032 :js:attr:`core.info`, :js:attr:`core.debug` (log level definitions)
2033 :see: :js:func:`TXN.deflog`
2034 :see: :js:func:`TXN.Debug`
2035 :see: :js:func:`TXN.Info`
2036 :see: :js:func:`TXN.Warning`
2037 :see: :js:func:`TXN.Alert`
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01002038
2039.. js:function:: TXN.deflog(TXN, msg)
2040
Bertrand Jacquin874a35c2018-09-10 21:26:07 +01002041 Sends a log line with the default loglevel for the proxy associated with the
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01002042 transaction.
2043
2044 :param class_txn txn: The class txn object containing the data.
2045 :param string msg: The log content.
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02002046 :see: :js:func:`TXN.log`
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01002047
2048.. js:function:: TXN.Debug(txn, msg)
2049
2050 :param class_txn txn: The class txn object containing the data.
2051 :param string msg: The log content.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01002052 :see: :js:func:`TXN.log`
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01002053
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02002054 Does the same job as:
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01002055
2056.. code-block:: lua
2057
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002058 function Debug(txn, msg)
2059 TXN.log(txn, core.debug, msg)
2060 end
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01002061..
2062
2063.. js:function:: TXN.Info(txn, msg)
2064
2065 :param class_txn txn: The class txn object containing the data.
2066 :param string msg: The log content.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01002067 :see: :js:func:`TXN.log`
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01002068
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02002069 Does the same job as:
2070
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01002071.. code-block:: lua
2072
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02002073 function Info(txn, msg)
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002074 TXN.log(txn, core.info, msg)
2075 end
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01002076..
2077
2078.. js:function:: TXN.Warning(txn, msg)
2079
2080 :param class_txn txn: The class txn object containing the data.
2081 :param string msg: The log content.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01002082 :see: :js:func:`TXN.log`
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01002083
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02002084 Does the same job as:
2085
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01002086.. code-block:: lua
2087
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02002088 function Warning(txn, msg)
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002089 TXN.log(txn, core.warning, msg)
2090 end
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01002091..
2092
2093.. js:function:: TXN.Alert(txn, msg)
2094
2095 :param class_txn txn: The class txn object containing the data.
2096 :param string msg: The log content.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01002097 :see: :js:func:`TXN.log`
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01002098
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02002099 Does the same job as:
2100
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01002101.. code-block:: lua
2102
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02002103 function Alert(txn, msg)
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002104 TXN.log(txn, core.alert, msg)
2105 end
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01002106..
2107
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002108.. js:function:: TXN.get_priv(txn)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002109
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002110 Return Lua data stored in the current transaction (with the `TXN.set_priv()`)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002111 function. If no data are stored, it returns a nil value.
2112
2113 :param class_txn txn: The class txn object containing the data.
Bertrand Jacquin874a35c2018-09-10 21:26:07 +01002114 :returns: the opaque data previously stored, or nil if nothing is
2115 available.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002116
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002117.. js:function:: TXN.set_priv(txn, data)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002118
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02002119 Store any data in the current HAProxy transaction. This action replaces the
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002120 old stored data.
2121
2122 :param class_txn txn: The class txn object containing the data.
2123 :param opaque data: The data which is stored in the transaction.
2124
Tim Duesterhus4e172c92020-05-19 13:49:42 +02002125.. js:function:: TXN.set_var(TXN, var, value[, ifexist])
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +02002126
David Carlier61fdf8b2015-10-02 11:59:38 +01002127 Converts a Lua type in a HAProxy type and store it in a variable <var>.
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +02002128
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 FOURNIER / OZON.IOb210bcc2016-12-12 16:24:16 +01002131 :param type value: The value associated to the variable. The type can be string or
2132 integer.
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02002133 :param boolean ifexist: If this parameter is set to true the variable
Tim Duesterhus4e172c92020-05-19 13:49:42 +02002134 will only be set if it was defined elsewhere (i.e. used
Willy Tarreau7978c5c2021-09-07 14:24:07 +02002135 within the configuration). For global variables (using the
2136 "proc" scope), they will only be updated and never created.
2137 It is highly recommended to always set this to true.
Christopher Faulet85d79c92016-11-09 16:54:56 +01002138
2139.. js:function:: TXN.unset_var(TXN, var)
2140
2141 Unset the variable <var>.
2142
2143 :param class_txn txn: The class txn object containing the data.
2144 :param string var: The variable name according with the HAProxy variable syntax.
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +02002145
2146.. js:function:: TXN.get_var(TXN, var)
2147
2148 Returns data stored in the variable <var> converter in Lua type.
2149
2150 :param class_txn txn: The class txn object containing the data.
2151 :param string var: The variable name according with the HAProxy variable syntax.
2152
Christopher Faulet700d9e82020-01-31 12:21:52 +01002153.. js:function:: TXN.reply([reply])
2154
2155 Return a new reply object
2156
2157 :param table reply: A table containing info to initialize the reply fields.
2158 :returns: A :ref:`reply_class` object.
2159
2160 The table used to initialized the reply object may contain following entries :
2161
2162 * status : The reply status code. the code 200 is used by default.
2163 * reason : The reply reason. The reason corresponding to the status code is
2164 used by default.
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02002165 * headers : A list of headers, indexed by header name. Empty by default. For
Christopher Faulet700d9e82020-01-31 12:21:52 +01002166 a given name, multiple values are possible, stored in an ordered list.
2167 * body : The reply body, empty by default.
2168
2169.. code-block:: lua
2170
2171 local reply = txn:reply{
2172 status = 400,
2173 reason = "Bad request",
2174 headers = {
2175 ["content-type"] = { "text/html" },
2176 ["cache-control"] = {"no-cache", "no-store" }
2177 },
2178 body = "<html><body><h1>invalid request<h1></body></html>"
2179 }
2180..
2181 :see: :js:class:`Reply`
2182
2183.. js:function:: TXN.done(txn[, reply])
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002184
Willy Tarreaubc183a62015-08-28 10:39:11 +02002185 This function terminates processing of the transaction and the associated
Christopher Faulet700d9e82020-01-31 12:21:52 +01002186 session and optionally reply to the client for HTTP sessions.
2187
2188 :param class_txn txn: The class txn object containing the data.
2189 :param class_reply reply: The class reply object to return to the client.
2190
2191 This functions can be used when a critical error is detected or to terminate
Willy Tarreaubc183a62015-08-28 10:39:11 +02002192 processing after some data have been returned to the client (eg: a redirect).
Christopher Faulet700d9e82020-01-31 12:21:52 +01002193 To do so, a reply may be provided. This object is optional and may contain a
2194 status code, a reason, a header list and a body. All these fields are
Christopher Faulet7855b192021-11-09 18:39:51 +01002195 optional. When not provided, the default values are used. By default, with an
2196 empty reply object, an empty HTTP 200 response is returned to the client. If
2197 no reply object is provided, the transaction is terminated without any
2198 reply. If a reply object is provided, it must not exceed the buffer size once
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02002199 converted into the internal HTTP representation. Because for now there is no
Christopher Faulet7855b192021-11-09 18:39:51 +01002200 easy way to be sure it fits, it is probably better to keep it reasonably
2201 small.
Christopher Faulet700d9e82020-01-31 12:21:52 +01002202
2203 The reply object may be fully created in lua or the class Reply may be used to
2204 create it.
2205
2206.. code-block:: lua
2207
2208 local reply = txn:reply()
2209 reply:set_status(400, "Bad request")
2210 reply:add_header("content-type", "text/html")
2211 reply:add_header("cache-control", "no-cache")
2212 reply:add_header("cache-control", "no-store")
2213 reply:set_body("<html><body><h1>invalid request<h1></body></html>")
2214 txn:done(reply)
2215..
2216
2217.. code-block:: lua
2218
2219 txn:done{
2220 status = 400,
2221 reason = "Bad request",
2222 headers = {
2223 ["content-type"] = { "text/html" },
2224 ["cache-control"] = { "no-cache", "no-store" },
2225 },
2226 body = "<html><body><h1>invalid request<h1></body></html>"
2227 }
2228..
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002229
Christopher Faulet1e9b1b62021-08-11 10:14:30 +02002230 .. warning::
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02002231 It does not make sense to call this function from sample-fetches. In this case
2232 the behavior is the same than core.done(): it finishes the Lua
Christopher Faulet1e9b1b62021-08-11 10:14:30 +02002233 execution. The transaction is really aborted only from an action registered
2234 function.
Thierry FOURNIERab00df62016-07-14 11:42:37 +02002235
Christopher Faulet700d9e82020-01-31 12:21:52 +01002236 :see: :js:func:`TXN.reply`, :js:class:`Reply`
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002237
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002238.. js:function:: TXN.set_loglevel(txn, loglevel)
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01002239
2240 Is used to change the log level of the current request. The "loglevel" must
2241 be an integer between 0 and 7.
2242
2243 :param class_txn txn: The class txn object containing the data.
2244 :param integer loglevel: The required log level. This variable can be one of
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01002245 :see: :js:attr:`core.emerg`, :js:attr:`core.alert`, :js:attr:`core.crit`,
2246 :js:attr:`core.err`, :js:attr:`core.warning`, :js:attr:`core.notice`,
2247 :js:attr:`core.info`, :js:attr:`core.debug` (log level definitions)
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01002248
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002249.. js:function:: TXN.set_tos(txn, tos)
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01002250
2251 Is used to set the TOS or DSCP field value of packets sent to the client to
2252 the value passed in "tos" on platforms which support this.
2253
2254 :param class_txn txn: The class txn object containing the data.
2255 :param integer tos: The new TOS os DSCP.
2256
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002257.. js:function:: TXN.set_mark(txn, mark)
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01002258
2259 Is used to set the Netfilter MARK on all packets sent to the client to the
2260 value passed in "mark" on platforms which support it.
2261
2262 :param class_txn txn: The class txn object containing the data.
2263 :param integer mark: The mark value.
2264
Patrick Hemmer268a7072018-05-11 12:52:31 -04002265.. js:function:: TXN.set_priority_class(txn, prio)
2266
2267 This function adjusts the priority class of the transaction. The value should
2268 be within the range -2047..2047. Values outside this range will be
2269 truncated.
2270
2271 See the HAProxy configuration.txt file keyword "http-request" action
2272 "set-priority-class" for details.
2273
2274.. js:function:: TXN.set_priority_offset(txn, prio)
2275
2276 This function adjusts the priority offset of the transaction. The value
2277 should be within the range -524287..524287. Values outside this range will be
2278 truncated.
2279
2280 See the HAProxy configuration.txt file keyword "http-request" action
2281 "set-priority-offset" for details.
2282
Christopher Faulet700d9e82020-01-31 12:21:52 +01002283.. _reply_class:
2284
2285Reply class
2286============
2287
2288.. js:class:: Reply
2289
2290 **context**: action
2291
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02002292 This class represents a HTTP response message. It provides some methods to
Christopher Faulet7855b192021-11-09 18:39:51 +01002293 enrich it. Once converted into the internal HTTP representation, the response
2294 message must not exceed the buffer size. Because for now there is no
2295 easy way to be sure it fits, it is probably better to keep it reasonably
2296 small.
2297
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02002298 See tune.bufsize in the configuration manual for details.
Christopher Faulet700d9e82020-01-31 12:21:52 +01002299
2300.. code-block:: lua
2301
2302 local reply = txn:reply({status = 400}) -- default HTTP 400 reason-phase used
2303 reply:add_header("content-type", "text/html")
2304 reply:add_header("cache-control", "no-cache")
2305 reply:add_header("cache-control", "no-store")
2306 reply:set_body("<html><body><h1>invalid request<h1></body></html>")
2307..
2308
2309 :see: :js:func:`TXN.reply`
2310
2311.. js:attribute:: Reply.status
2312
2313 The reply status code. By default, the status code is set to 200.
2314
2315 :returns: integer
2316
2317.. js:attribute:: Reply.reason
2318
2319 The reason string describing the status code.
2320
2321 :returns: string
2322
2323.. js:attribute:: Reply.headers
2324
2325 A table indexing all reply headers by name. To each name is associated an
2326 ordered list of values.
2327
2328 :returns: Lua table
2329
2330.. code-block:: lua
2331
2332 {
2333 ["content-type"] = { "text/html" },
2334 ["cache-control"] = {"no-cache", "no-store" },
2335 x_header_name = { "value1", "value2", ... }
2336 ...
2337 }
2338..
2339
2340.. js:attribute:: Reply.body
2341
2342 The reply payload.
2343
2344 :returns: string
2345
2346.. js:function:: Reply.set_status(REPLY, status[, reason])
2347
2348 Set the reply status code and optionally the reason-phrase. If the reason is
2349 not provided, the default reason corresponding to the status code is used.
2350
2351 :param class_reply reply: The related Reply object.
2352 :param integer status: The reply status code.
2353 :param string reason: The reply status reason (optional).
2354
2355.. js:function:: Reply.add_header(REPLY, name, value)
2356
2357 Add a header to the reply object. If the header does not already exist, a new
2358 entry is created with its name as index and a one-element list containing its
2359 value as value. Otherwise, the header value is appended to the ordered list of
2360 values associated to the header name.
2361
2362 :param class_reply reply: The related Reply object.
2363 :param string name: The header field name.
2364 :param string value: The header field value.
2365
2366.. js:function:: Reply.del_header(REPLY, name)
2367
2368 Remove all occurrences of a header name from the reply object.
2369
2370 :param class_reply reply: The related Reply object.
2371 :param string name: The header field name.
2372
2373.. js:function:: Reply.set_body(REPLY, body)
2374
2375 Set the reply payload.
2376
2377 :param class_reply reply: The related Reply object.
2378 :param string body: The reply payload.
2379
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002380.. _socket_class:
2381
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002382Socket class
2383============
2384
2385.. js:class:: Socket
2386
2387 This class must be compatible with the Lua Socket class. Only the 'client'
2388 functions are available. See the Lua Socket documentation:
2389
2390 `http://w3.impa.br/~diego/software/luasocket/tcp.html
2391 <http://w3.impa.br/~diego/software/luasocket/tcp.html>`_
2392
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002393.. js:function:: Socket.close(socket)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002394
2395 Closes a TCP object. The internal socket used by the object is closed and the
2396 local address to which the object was bound is made available to other
2397 applications. No further operations (except for further calls to the close
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002398 method) are allowed on a closed Socket.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002399
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002400 :param class_socket socket: Is the manipulated Socket.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002401
2402 Note: It is important to close all used sockets once they are not needed,
2403 since, in many systems, each socket uses a file descriptor, which are limited
2404 system resources. Garbage-collected objects are automatically closed before
2405 destruction, though.
2406
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002407.. js:function:: Socket.connect(socket, address[, port])
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002408
2409 Attempts to connect a socket object to a remote host.
2410
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002411
2412 In case of error, the method returns nil followed by a string describing the
2413 error. In case of success, the method returns 1.
2414
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002415 :param class_socket socket: Is the manipulated Socket.
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002416 :param string address: can be an IP address or a host name. See below for more
2417 information.
2418 :param integer port: must be an integer number in the range [1..64K].
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002419 :returns: 1 or nil.
2420
Bertrand Jacquin874a35c2018-09-10 21:26:07 +01002421 An address field extension permits to use the connect() function to connect to
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002422 other stream than TCP. The syntax containing a simpleipv4 or ipv6 address is
2423 the basically expected format. This format requires the port.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002424
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002425 Other format accepted are a socket path like "/socket/path", it permits to
Bertrand Jacquin874a35c2018-09-10 21:26:07 +01002426 connect to a socket. Abstract namespaces are supported with the prefix
Joseph Herlant02cedc42018-11-13 19:45:17 -08002427 "abns@", and finally a file descriptor can be passed with the prefix "fd@".
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002428 The prefix "ipv4@", "ipv6@" and "unix@" are also supported. The port can be
Bertrand Jacquin874a35c2018-09-10 21:26:07 +01002429 passed int the string. The syntax "127.0.0.1:1234" is valid. In this case, the
Tim Duesterhus6edab862018-01-06 19:04:45 +01002430 parameter *port* must not be set.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002431
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002432.. js:function:: Socket.connect_ssl(socket, address, port)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002433
2434 Same behavior than the function socket:connect, but uses SSL.
2435
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002436 :param class_socket socket: Is the manipulated Socket.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002437 :returns: 1 or nil.
2438
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002439.. js:function:: Socket.getpeername(socket)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002440
2441 Returns information about the remote side of a connected client object.
2442
2443 Returns a string with the IP address of the peer, followed by the port number
2444 that peer is using for the connection. In case of error, the method returns
2445 nil.
2446
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002447 :param class_socket socket: Is the manipulated Socket.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002448 :returns: a string containing the server information.
2449
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002450.. js:function:: Socket.getsockname(socket)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002451
2452 Returns the local address information associated to the object.
2453
2454 The method returns a string with local IP address and a number with the port.
2455 In case of error, the method returns nil.
2456
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002457 :param class_socket socket: Is the manipulated Socket.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002458 :returns: a string containing the client information.
2459
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002460.. js:function:: Socket.receive(socket, [pattern [, prefix]])
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002461
2462 Reads data from a client object, according to the specified read pattern.
2463 Patterns follow the Lua file I/O format, and the difference in performance
2464 between all patterns is negligible.
2465
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002466 :param class_socket socket: Is the manipulated Socket.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002467 :param string|integer pattern: Describe what is required (see below).
2468 :param string prefix: A string which will be prefix the returned data.
2469 :returns: a string containing the required data or nil.
2470
2471 Pattern can be any of the following:
2472
2473 * **`*a`**: reads from the socket until the connection is closed. No
2474 end-of-line translation is performed;
2475
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002476 * **`*l`**: reads a line of text from the Socket. The line is terminated by a
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002477 LF character (ASCII 10), optionally preceded by a CR character
2478 (ASCII 13). The CR and LF characters are not included in the
2479 returned line. In fact, all CR characters are ignored by the
2480 pattern. This is the default pattern.
2481
2482 * **number**: causes the method to read a specified number of bytes from the
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002483 Socket. Prefix is an optional string to be concatenated to the
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002484 beginning of any received data before return.
2485
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002486 * **empty**: If the pattern is left empty, the default option is `*l`.
2487
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002488 If successful, the method returns the received pattern. In case of error, the
2489 method returns nil followed by an error message which can be the string
2490 'closed' in case the connection was closed before the transmission was
2491 completed or the string 'timeout' in case there was a timeout during the
2492 operation. Also, after the error message, the function returns the partial
2493 result of the transmission.
2494
2495 Important note: This function was changed severely. It used to support
2496 multiple patterns (but I have never seen this feature used) and now it
2497 doesn't anymore. Partial results used to be returned in the same way as
2498 successful results. This last feature violated the idea that all functions
2499 should return nil on error. Thus it was changed too.
2500
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002501.. js:function:: Socket.send(socket, data [, start [, end ]])
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002502
2503 Sends data through client object.
2504
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002505 :param class_socket socket: Is the manipulated Socket.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002506 :param string data: The data that will be sent.
2507 :param integer start: The start position in the buffer of the data which will
2508 be sent.
2509 :param integer end: The end position in the buffer of the data which will
2510 be sent.
2511 :returns: see below.
2512
2513 Data is the string to be sent. The optional arguments i and j work exactly
2514 like the standard string.sub Lua function to allow the selection of a
2515 substring to be sent.
2516
2517 If successful, the method returns the index of the last byte within [start,
2518 end] that has been sent. Notice that, if start is 1 or absent, this is
2519 effectively the total number of bytes sent. In case of error, the method
2520 returns nil, followed by an error message, followed by the index of the last
2521 byte within [start, end] that has been sent. You might want to try again from
2522 the byte following that. The error message can be 'closed' in case the
2523 connection was closed before the transmission was completed or the string
2524 'timeout' in case there was a timeout during the operation.
2525
2526 Note: Output is not buffered. For small strings, it is always better to
2527 concatenate them in Lua (with the '..' operator) and send the result in one
2528 call instead of calling the method several times.
2529
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002530.. js:function:: Socket.setoption(socket, option [, value])
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002531
2532 Just implemented for compatibility, this cal does nothing.
2533
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002534.. js:function:: Socket.settimeout(socket, value [, mode])
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002535
2536 Changes the timeout values for the object. All I/O operations are blocking.
2537 That is, any call to the methods send, receive, and accept will block
2538 indefinitely, until the operation completes. The settimeout method defines a
2539 limit on the amount of time the I/O methods can block. When a timeout time
2540 has elapsed, the affected methods give up and fail with an error code.
2541
2542 The amount of time to wait is specified as the value parameter, in seconds.
2543
Mark Lakes56cc1252018-03-27 09:48:06 +02002544 The timeout modes are not implemented, the only settable timeout is the
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002545 inactivity time waiting for complete the internal buffer send or waiting for
2546 receive data.
2547
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002548 :param class_socket socket: Is the manipulated Socket.
Bertrand Jacquin874a35c2018-09-10 21:26:07 +01002549 :param float value: The timeout value. Use floating point to specify
Mark Lakes56cc1252018-03-27 09:48:06 +02002550 milliseconds.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002551
Thierry FOURNIER31904272017-10-25 12:59:51 +02002552.. _regex_class:
2553
2554Regex class
2555===========
2556
2557.. js:class:: Regex
2558
2559 This class allows the usage of HAProxy regexes because classic lua doesn't
2560 provides regexes. This class inherits the HAProxy compilation options, so the
2561 regexes can be libc regex, pcre regex or pcre JIT regex.
2562
2563 The expression matching number is limited to 20 per regex. The only available
2564 option is case sensitive.
2565
2566 Because regexes compilation is a heavy process, it is better to define all
2567 your regexes in the **body context** and use it during the runtime.
2568
2569.. code-block:: lua
2570
2571 -- Create the regex
2572 st, regex = Regex.new("needle (..) (...)", true);
2573
2574 -- Check compilation errors
2575 if st == false then
2576 print "error: " .. regex
2577 end
2578
2579 -- Match the regexes
2580 print(regex:exec("Looking for a needle in the haystack")) -- true
2581 print(regex:exec("Lokking for a cat in the haystack")) -- false
2582
2583 -- Extract words
2584 st, list = regex:match("Looking for a needle in the haystack")
2585 print(st) -- true
2586 print(list[1]) -- needle in the
2587 print(list[2]) -- in
2588 print(list[3]) -- the
2589
2590.. js:function:: Regex.new(regex, case_sensitive)
2591
2592 Create and compile a regex.
2593
2594 :param string regex: The regular expression according with the libc or pcre
2595 standard
2596 :param boolean case_sensitive: Match is case sensitive or not.
2597 :returns: boolean status and :ref:`regex_class` or string containing fail reason.
2598
2599.. js:function:: Regex.exec(regex, str)
2600
2601 Execute the regex.
2602
2603 :param class_regex regex: A :ref:`regex_class` object.
2604 :param string str: The input string will be compared with the compiled regex.
2605 :returns: a boolean status according with the match result.
2606
2607.. js:function:: Regex.match(regex, str)
2608
2609 Execute the regex and return matched expressions.
2610
2611 :param class_map map: A :ref:`regex_class` object.
2612 :param string str: The input string will be compared with the compiled regex.
2613 :returns: a boolean status according with the match result, and
2614 a table containing all the string matched in order of declaration.
2615
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002616.. _map_class:
2617
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002618Map class
2619=========
2620
2621.. js:class:: Map
2622
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02002623 This class permits to do some lookups in HAProxy maps. The declared maps can
Bertrand Jacquin874a35c2018-09-10 21:26:07 +01002624 be modified during the runtime through the HAProxy management socket.
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002625
2626.. code-block:: lua
2627
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002628 default = "usa"
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002629
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002630 -- Create and load map
Thierry FOURNIER4dc71972017-01-28 08:33:08 +01002631 geo = Map.new("geo.map", Map._ip);
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002632
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002633 -- Create new fetch that returns the user country
2634 core.register_fetches("country", function(txn)
2635 local src;
2636 local loc;
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002637
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002638 src = txn.f:fhdr("x-forwarded-for");
2639 if (src == nil) then
2640 src = txn.f:src()
2641 if (src == nil) then
2642 return default;
2643 end
2644 end
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002645
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002646 -- Perform lookup
2647 loc = geo:lookup(src);
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002648
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002649 if (loc == nil) then
2650 return default;
2651 end
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002652
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002653 return loc;
2654 end);
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002655
Thierry FOURNIER4dc71972017-01-28 08:33:08 +01002656.. js:attribute:: Map._int
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002657
2658 See the HAProxy configuration.txt file, chapter "Using ACLs and fetching
Ilya Shipitsin2075ca82020-03-06 23:22:22 +05002659 samples" and subchapter "ACL basics" to understand this pattern matching
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002660 method.
2661
Thierry FOURNIER4dc71972017-01-28 08:33:08 +01002662 Note that :js:attr:`Map.int` is also available for compatibility.
2663
2664.. js:attribute:: Map._ip
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002665
2666 See the HAProxy configuration.txt file, chapter "Using ACLs and fetching
Ilya Shipitsin2075ca82020-03-06 23:22:22 +05002667 samples" and subchapter "ACL basics" to understand this pattern matching
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002668 method.
2669
Thierry FOURNIER4dc71972017-01-28 08:33:08 +01002670 Note that :js:attr:`Map.ip` is also available for compatibility.
2671
2672.. js:attribute:: Map._str
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002673
2674 See the HAProxy configuration.txt file, chapter "Using ACLs and fetching
Ilya Shipitsin2075ca82020-03-06 23:22:22 +05002675 samples" and subchapter "ACL basics" to understand this pattern matching
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002676 method.
2677
Thierry FOURNIER4dc71972017-01-28 08:33:08 +01002678 Note that :js:attr:`Map.str` is also available for compatibility.
2679
2680.. js:attribute:: Map._beg
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002681
2682 See the HAProxy configuration.txt file, chapter "Using ACLs and fetching
Ilya Shipitsin2075ca82020-03-06 23:22:22 +05002683 samples" and subchapter "ACL basics" to understand this pattern matching
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002684 method.
2685
Thierry FOURNIER4dc71972017-01-28 08:33:08 +01002686 Note that :js:attr:`Map.beg` is also available for compatibility.
2687
2688.. js:attribute:: Map._sub
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002689
2690 See the HAProxy configuration.txt file, chapter "Using ACLs and fetching
Ilya Shipitsin2075ca82020-03-06 23:22:22 +05002691 samples" and subchapter "ACL basics" to understand this pattern matching
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002692 method.
2693
Thierry FOURNIER4dc71972017-01-28 08:33:08 +01002694 Note that :js:attr:`Map.sub` is also available for compatibility.
2695
2696.. js:attribute:: Map._dir
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002697
2698 See the HAProxy configuration.txt file, chapter "Using ACLs and fetching
Ilya Shipitsin2075ca82020-03-06 23:22:22 +05002699 samples" and subchapter "ACL basics" to understand this pattern matching
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002700 method.
2701
Thierry FOURNIER4dc71972017-01-28 08:33:08 +01002702 Note that :js:attr:`Map.dir` is also available for compatibility.
2703
2704.. js:attribute:: Map._dom
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.dom` is also available for compatibility.
2711
2712.. js:attribute:: Map._end
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002713
2714 See the HAProxy configuration.txt file, chapter "Using ACLs and fetching
Ilya Shipitsin2075ca82020-03-06 23:22:22 +05002715 samples" and subchapter "ACL basics" to understand this pattern matching
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002716 method.
2717
Thierry FOURNIER4dc71972017-01-28 08:33:08 +01002718.. js:attribute:: Map._reg
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002719
2720 See the HAProxy configuration.txt file, chapter "Using ACLs and fetching
Ilya Shipitsin2075ca82020-03-06 23:22:22 +05002721 samples" and subchapter "ACL basics" to understand this pattern matching
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002722 method.
2723
Thierry FOURNIER4dc71972017-01-28 08:33:08 +01002724 Note that :js:attr:`Map.reg` is also available for compatibility.
2725
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002726
2727.. js:function:: Map.new(file, method)
2728
2729 Creates and load a map.
2730
2731 :param string file: Is the file containing the map.
2732 :param integer method: Is the map pattern matching method. See the attributes
2733 of the Map class.
2734 :returns: a class Map object.
Thierry FOURNIER4dc71972017-01-28 08:33:08 +01002735 :see: The Map attributes: :js:attr:`Map._int`, :js:attr:`Map._ip`,
2736 :js:attr:`Map._str`, :js:attr:`Map._beg`, :js:attr:`Map._sub`,
2737 :js:attr:`Map._dir`, :js:attr:`Map._dom`, :js:attr:`Map._end` and
2738 :js:attr:`Map._reg`.
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002739
2740.. js:function:: Map.lookup(map, str)
2741
2742 Perform a lookup in a map.
2743
2744 :param class_map map: Is the class Map object.
2745 :param string str: Is the string used as key.
2746 :returns: a string containing the result or nil if no match.
2747
2748.. js:function:: Map.slookup(map, str)
2749
2750 Perform a lookup in a map.
2751
2752 :param class_map map: Is the class Map object.
2753 :param string str: Is the string used as key.
2754 :returns: a string containing the result or empty string if no match.
2755
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002756.. _applethttp_class:
2757
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002758AppletHTTP class
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002759================
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002760
2761.. js:class:: AppletHTTP
2762
2763 This class is used with applets that requires the 'http' mode. The http applet
2764 can be registered with the *core.register_service()* function. They are used
2765 for processing an http request like a server in back of HAProxy.
2766
2767 This is an hello world sample code:
2768
2769.. code-block:: lua
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002770
Pieter Baauw4d7f7662015-11-08 16:38:08 +01002771 core.register_service("hello-world", "http", function(applet)
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002772 local response = "Hello World !"
2773 applet:set_status(200)
Pieter Baauw2dcb9bc2015-10-01 22:47:12 +02002774 applet:add_header("content-length", string.len(response))
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002775 applet:add_header("content-type", "text/plain")
Pieter Baauw2dcb9bc2015-10-01 22:47:12 +02002776 applet:start_response()
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002777 applet:send(response)
2778 end)
2779
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002780.. js:attribute:: AppletHTTP.c
2781
2782 :returns: A :ref:`converters_class`
2783
2784 This attribute contains a Converters class object.
2785
2786.. js:attribute:: AppletHTTP.sc
2787
2788 :returns: A :ref:`converters_class`
2789
2790 This attribute contains a Converters class object. The
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02002791 functions of this object always return a string.
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002792
2793.. js:attribute:: AppletHTTP.f
2794
2795 :returns: A :ref:`fetches_class`
2796
2797 This attribute contains a Fetches class object. Note that the
2798 applet execution place cannot access to a valid HAProxy core HTTP
Ilya Shipitsin2075ca82020-03-06 23:22:22 +05002799 transaction, so some sample fetches related to the HTTP dependent
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002800 values (hdr, path, ...) are not available.
2801
2802.. js:attribute:: AppletHTTP.sf
2803
2804 :returns: A :ref:`fetches_class`
2805
2806 This attribute contains a Fetches class object. The functions of
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02002807 this object always return a string. Note that the applet
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002808 execution place cannot access to a valid HAProxy core HTTP
Ilya Shipitsin2075ca82020-03-06 23:22:22 +05002809 transaction, so some sample fetches related to the HTTP dependent
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002810 values (hdr, path, ...) are not available.
2811
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01002812.. js:attribute:: AppletHTTP.method
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002813
2814 :returns: string
2815
2816 The attribute method returns a string containing the HTTP
2817 method.
2818
2819.. js:attribute:: AppletHTTP.version
2820
2821 :returns: string
2822
2823 The attribute version, returns a string containing the HTTP
2824 request version.
2825
2826.. js:attribute:: AppletHTTP.path
2827
2828 :returns: string
2829
2830 The attribute path returns a string containing the HTTP
2831 request path.
2832
2833.. js:attribute:: AppletHTTP.qs
2834
2835 :returns: string
2836
2837 The attribute qs returns a string containing the HTTP
2838 request query string.
2839
2840.. js:attribute:: AppletHTTP.length
2841
2842 :returns: integer
2843
2844 The attribute length returns an integer containing the HTTP
2845 body length.
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002846
Thierry FOURNIER841475e2015-12-11 17:10:09 +01002847.. js:attribute:: AppletHTTP.headers
2848
Patrick Hemmerc6a1d712018-05-01 21:30:41 -04002849 :returns: table
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002850
Patrick Hemmerc6a1d712018-05-01 21:30:41 -04002851 The attribute headers returns a table containing the HTTP
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002852 headers. The header names are always in lower case. As the header name can be
2853 encountered more than once in each request, the value is indexed with 0 as
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02002854 first index value. The table has this form:
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002855
2856.. code-block:: lua
2857
2858 AppletHTTP.headers['<header-name>'][<header-index>] = "<header-value>"
2859
2860 AppletHTTP.headers["host"][0] = "www.test.com"
2861 AppletHTTP.headers["accept"][0] = "audio/basic q=1"
2862 AppletHTTP.headers["accept"][1] = "audio/*, q=0.2"
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01002863 AppletHTTP.headers["accept"][2] = "*/*, q=0.1"
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002864..
2865
Robin H. Johnson52f5db22017-01-01 13:10:52 -08002866.. js:function:: AppletHTTP.set_status(applet, code [, reason])
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002867
2868 This function sets the HTTP status code for the response. The allowed code are
2869 from 100 to 599.
2870
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01002871 :param class_AppletHTTP applet: An :ref:`applethttp_class`
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002872 :param integer code: the status code returned to the client.
Robin H. Johnson52f5db22017-01-01 13:10:52 -08002873 :param string reason: the status reason returned to the client (optional).
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002874
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01002875.. js:function:: AppletHTTP.add_header(applet, name, value)
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002876
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02002877 This function adds a header in the response. Duplicated headers are not
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002878 collapsed. The special header *content-length* is used to determinate the
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02002879 response length. If it does not exist, a *transfer-encoding: chunked* is set, and
Ilya Shipitsin2075ca82020-03-06 23:22:22 +05002880 all the write from the function *AppletHTTP:send()* become a chunk.
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002881
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01002882 :param class_AppletHTTP applet: An :ref:`applethttp_class`
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002883 :param string name: the header name
2884 :param string value: the header value
2885
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01002886.. js:function:: AppletHTTP.start_response(applet)
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002887
2888 This function indicates to the HTTP engine that it can process and send the
2889 response headers. After this called we cannot add headers to the response; We
2890 cannot use the *AppletHTTP:send()* function if the
2891 *AppletHTTP:start_response()* is not called.
2892
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01002893 :param class_AppletHTTP applet: An :ref:`applethttp_class`
2894
2895.. js:function:: AppletHTTP.getline(applet)
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002896
2897 This function returns a string containing one line from the http body. If the
2898 data returned doesn't contains a final '\\n' its assumed than its the last
2899 available data before the end of stream.
2900
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01002901 :param class_AppletHTTP applet: An :ref:`applethttp_class`
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002902 :returns: a string. The string can be empty if we reach the end of the stream.
2903
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01002904.. js:function:: AppletHTTP.receive(applet, [size])
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002905
2906 Reads data from the HTTP body, according to the specified read *size*. If the
2907 *size* is missing, the function tries to read all the content of the stream
2908 until the end. If the *size* is bigger than the http body, it returns the
Bertrand Jacquin874a35c2018-09-10 21:26:07 +01002909 amount of data available.
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002910
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01002911 :param class_AppletHTTP applet: An :ref:`applethttp_class`
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002912 :param integer size: the required read size.
Ilya Shipitsin11057a32020-06-21 21:18:27 +05002913 :returns: always return a string,the string can be empty is the connection is
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002914 closed.
2915
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01002916.. js:function:: AppletHTTP.send(applet, msg)
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002917
2918 Send the message *msg* on the http request body.
2919
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01002920 :param class_AppletHTTP applet: An :ref:`applethttp_class`
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002921 :param string msg: the message to send.
2922
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01002923.. js:function:: AppletHTTP.get_priv(applet)
2924
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01002925 Return Lua data stored in the current transaction. If no data are stored,
2926 it returns a nil value.
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01002927
2928 :param class_AppletHTTP applet: An :ref:`applethttp_class`
Bertrand Jacquin874a35c2018-09-10 21:26:07 +01002929 :returns: the opaque data previously stored, or nil if nothing is
2930 available.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01002931 :see: :js:func:`AppletHTTP.set_priv`
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01002932
2933.. js:function:: AppletHTTP.set_priv(applet, data)
2934
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02002935 Store any data in the current HAProxy transaction. This action replaces the
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01002936 old stored data.
2937
2938 :param class_AppletHTTP applet: An :ref:`applethttp_class`
2939 :param opaque data: The data which is stored in the transaction.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01002940 :see: :js:func:`AppletHTTP.get_priv`
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01002941
Tim Duesterhus4e172c92020-05-19 13:49:42 +02002942.. js:function:: AppletHTTP.set_var(applet, var, value[, ifexist])
Thierry FOURNIER / OZON.IOc1edafe2016-12-12 16:25:30 +01002943
2944 Converts a Lua type in a HAProxy type and store it in a variable <var>.
2945
2946 :param class_AppletHTTP applet: An :ref:`applethttp_class`
2947 :param string var: The variable name according with the HAProxy variable syntax.
2948 :param type value: The value associated to the variable. The type ca be string or
2949 integer.
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02002950 :param boolean ifexist: If this parameter is set to true the variable
Tim Duesterhus4e172c92020-05-19 13:49:42 +02002951 will only be set if it was defined elsewhere (i.e. used
Willy Tarreau7978c5c2021-09-07 14:24:07 +02002952 within the configuration). For global variables (using the
2953 "proc" scope), they will only be updated and never created.
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02002954 It is highly recommended to always set this to true.
2955
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01002956 :see: :js:func:`AppletHTTP.unset_var`
2957 :see: :js:func:`AppletHTTP.get_var`
Thierry FOURNIER / OZON.IOc1edafe2016-12-12 16:25:30 +01002958
2959.. js:function:: AppletHTTP.unset_var(applet, var)
2960
2961 Unset the variable <var>.
2962
2963 :param class_AppletHTTP applet: An :ref:`applethttp_class`
2964 :param string var: The variable name according with the HAProxy variable syntax.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01002965 :see: :js:func:`AppletHTTP.set_var`
2966 :see: :js:func:`AppletHTTP.get_var`
Thierry FOURNIER / OZON.IOc1edafe2016-12-12 16:25:30 +01002967
2968.. js:function:: AppletHTTP.get_var(applet, var)
2969
2970 Returns data stored in the variable <var> converter in Lua type.
2971
2972 :param class_AppletHTTP applet: An :ref:`applethttp_class`
2973 :param string var: The variable name according with the HAProxy variable syntax.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01002974 :see: :js:func:`AppletHTTP.set_var`
2975 :see: :js:func:`AppletHTTP.unset_var`
Thierry FOURNIER / OZON.IOc1edafe2016-12-12 16:25:30 +01002976
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002977.. _applettcp_class:
2978
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002979AppletTCP class
2980===============
2981
2982.. js:class:: AppletTCP
2983
2984 This class is used with applets that requires the 'tcp' mode. The tcp applet
2985 can be registered with the *core.register_service()* function. They are used
2986 for processing a tcp stream like a server in back of HAProxy.
2987
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002988.. js:attribute:: AppletTCP.c
2989
2990 :returns: A :ref:`converters_class`
2991
2992 This attribute contains a Converters class object.
2993
2994.. js:attribute:: AppletTCP.sc
2995
2996 :returns: A :ref:`converters_class`
2997
2998 This attribute contains a Converters class object. The
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02002999 functions of this object always return a string.
Thierry FOURNIERdc595002015-12-21 11:13:52 +01003000
3001.. js:attribute:: AppletTCP.f
3002
3003 :returns: A :ref:`fetches_class`
3004
3005 This attribute contains a Fetches class object.
3006
3007.. js:attribute:: AppletTCP.sf
3008
3009 :returns: A :ref:`fetches_class`
3010
3011 This attribute contains a Fetches class object.
3012
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01003013.. js:function:: AppletTCP.getline(applet)
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02003014
3015 This function returns a string containing one line from the stream. If the
3016 data returned doesn't contains a final '\\n' its assumed than its the last
3017 available data before the end of stream.
3018
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01003019 :param class_AppletTCP applet: An :ref:`applettcp_class`
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02003020 :returns: a string. The string can be empty if we reach the end of the stream.
3021
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01003022.. js:function:: AppletTCP.receive(applet, [size])
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02003023
3024 Reads data from the TCP stream, according to the specified read *size*. If the
3025 *size* is missing, the function tries to read all the content of the stream
3026 until the end.
3027
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01003028 :param class_AppletTCP applet: An :ref:`applettcp_class`
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02003029 :param integer size: the required read size.
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02003030 :returns: always return a string, the string can be empty if the connection is
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02003031 closed.
3032
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01003033.. js:function:: AppletTCP.send(appletmsg)
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02003034
3035 Send the message on the stream.
3036
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01003037 :param class_AppletTCP applet: An :ref:`applettcp_class`
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02003038 :param string msg: the message to send.
3039
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01003040.. js:function:: AppletTCP.get_priv(applet)
3041
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01003042 Return Lua data stored in the current transaction. If no data are stored,
3043 it returns a nil value.
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01003044
3045 :param class_AppletTCP applet: An :ref:`applettcp_class`
Bertrand Jacquin874a35c2018-09-10 21:26:07 +01003046 :returns: the opaque data previously stored, or nil if nothing is
3047 available.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01003048 :see: :js:func:`AppletTCP.set_priv`
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01003049
3050.. js:function:: AppletTCP.set_priv(applet, data)
3051
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02003052 Store any data in the current HAProxy transaction. This action replaces the
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01003053 old stored data.
3054
3055 :param class_AppletTCP applet: An :ref:`applettcp_class`
3056 :param opaque data: The data which is stored in the transaction.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01003057 :see: :js:func:`AppletTCP.get_priv`
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01003058
Tim Duesterhus4e172c92020-05-19 13:49:42 +02003059.. js:function:: AppletTCP.set_var(applet, var, value[, ifexist])
Thierry FOURNIER / OZON.IOc1edafe2016-12-12 16:25:30 +01003060
3061 Converts a Lua type in a HAProxy type and stores it in a variable <var>.
3062
3063 :param class_AppletTCP applet: An :ref:`applettcp_class`
3064 :param string var: The variable name according with the HAProxy variable syntax.
3065 :param type value: The value associated to the variable. The type can be string or
3066 integer.
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02003067 :param boolean ifexist: If this parameter is set to true the variable
Tim Duesterhus4e172c92020-05-19 13:49:42 +02003068 will only be set if it was defined elsewhere (i.e. used
Willy Tarreau7978c5c2021-09-07 14:24:07 +02003069 within the configuration). For global variables (using the
3070 "proc" scope), they will only be updated and never created.
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02003071 It is highly recommended to always set this to true.
3072
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01003073 :see: :js:func:`AppletTCP.unset_var`
3074 :see: :js:func:`AppletTCP.get_var`
Thierry FOURNIER / OZON.IOc1edafe2016-12-12 16:25:30 +01003075
3076.. js:function:: AppletTCP.unset_var(applet, var)
3077
3078 Unsets the variable <var>.
3079
3080 :param class_AppletTCP applet: An :ref:`applettcp_class`
3081 :param string var: The variable name according with the HAProxy variable syntax.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01003082 :see: :js:func:`AppletTCP.unset_var`
3083 :see: :js:func:`AppletTCP.set_var`
Thierry FOURNIER / OZON.IOc1edafe2016-12-12 16:25:30 +01003084
3085.. js:function:: AppletTCP.get_var(applet, var)
3086
3087 Returns data stored in the variable <var> converter in Lua type.
3088
3089 :param class_AppletTCP applet: An :ref:`applettcp_class`
3090 :param string var: The variable name according with the HAProxy variable syntax.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01003091 :see: :js:func:`AppletTCP.unset_var`
3092 :see: :js:func:`AppletTCP.set_var`
Thierry FOURNIER / OZON.IOc1edafe2016-12-12 16:25:30 +01003093
Adis Nezirovic8878f8e2018-07-13 12:18:33 +02003094StickTable class
3095================
3096
3097.. js:class:: StickTable
3098
3099 **context**: task, action, sample-fetch
3100
3101 This class can be used to access the HAProxy stick tables from Lua.
3102
3103.. js:function:: StickTable.info()
3104
3105 Returns stick table attributes as a Lua table. See HAProxy documentation for
Ilya Shipitsin2272d8a2020-12-21 01:22:40 +05003106 "stick-table" for canonical info, or check out example below.
Adis Nezirovic8878f8e2018-07-13 12:18:33 +02003107
3108 :returns: Lua table
3109
3110 Assume our table has IPv4 key and gpc0 and conn_rate "columns":
3111
3112.. code-block:: lua
3113
3114 {
3115 expire=<int>, # Value in ms
3116 size=<int>, # Maximum table size
3117 used=<int>, # Actual number of entries in table
3118 data={ # Data columns, with types as key, and periods as values
3119 (-1 if type is not rate counter)
3120 conn_rate=<int>,
3121 gpc0=-1
3122 },
3123 length=<int>, # max string length for string table keys, key length
3124 # otherwise
3125 nopurge=<boolean>, # purge oldest entries when table is full
3126 type="ip" # can be "ip", "ipv6", "integer", "string", "binary"
3127 }
3128
3129.. js:function:: StickTable.lookup(key)
3130
3131 Returns stick table entry for given <key>
3132
3133 :param string key: Stick table key (IP addresses and strings are supported)
3134 :returns: Lua table
3135
3136.. js:function:: StickTable.dump([filter])
3137
3138 Returns all entries in stick table. An optional filter can be used
3139 to extract entries with specific data values. Filter is a table with valid
3140 comparison operators as keys followed by data type name and value pairs.
3141 Check out the HAProxy docs for "show table" for more details. For the
3142 reference, the supported operators are:
3143 "eq", "ne", "le", "lt", "ge", "gt"
3144
3145 For large tables, execution of this function can take a long time (for
3146 HAProxy standards). That's also true when filter is used, so take care and
3147 measure the impact.
3148
3149 :param table filter: Stick table filter
3150 :returns: Stick table entries (table)
3151
3152 See below for example filter, which contains 4 entries (or comparisons).
3153 (Maximum number of filter entries is 4, defined in the source code)
3154
3155.. code-block:: lua
3156
3157 local filter = {
3158 {"gpc0", "gt", 30}, {"gpc1", "gt", 20}}, {"conn_rate", "le", 10}
3159 }
3160
Christopher Faulet0f3c8902020-01-31 18:57:12 +01003161.. _action_class:
3162
3163Action class
3164=============
3165
3166.. js:class:: Act
3167
3168 **context**: action
3169
3170 This class contains all return codes an action may return. It is the lua
3171 equivalent to HAProxy "ACT_RET_*" code.
3172
3173.. code-block:: lua
3174
3175 core.register_action("deny", { "http-req" }, function (txn)
3176 return act.DENY
3177 end)
3178..
3179.. js:attribute:: act.CONTINUE
3180
3181 This attribute is an integer (0). It instructs HAProxy to continue the current
3182 ruleset processing on the message. It is the default return code for a lua
3183 action.
3184
3185 :returns: integer
3186
3187.. js:attribute:: act.STOP
3188
3189 This attribute is an integer (1). It instructs HAProxy to stop the current
3190 ruleset processing on the message.
3191
3192.. js:attribute:: act.YIELD
3193
3194 This attribute is an integer (2). It instructs HAProxy to temporarily pause
3195 the message processing. It will be resumed later on the same rule. The
3196 corresponding lua script is re-executed for the start.
3197
3198.. js:attribute:: act.ERROR
3199
3200 This attribute is an integer (3). It triggers an internal errors The message
3201 processing is stopped and the transaction is terminated. For HTTP streams, an
3202 HTTP 500 error is returned to the client.
3203
3204 :returns: integer
3205
3206.. js:attribute:: act.DONE
3207
3208 This attribute is an integer (4). It instructs HAProxy to stop the message
3209 processing.
3210
3211 :returns: integer
3212
3213.. js:attribute:: act.DENY
3214
3215 This attribute is an integer (5). It denies the current message. The message
3216 processing is stopped and the transaction is terminated. For HTTP streams, an
3217 HTTP 403 error is returned to the client if the deny is returned during the
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02003218 request analysis. During the response analysis, a HTTP 502 error is returned
Christopher Faulet0f3c8902020-01-31 18:57:12 +01003219 and the server response is discarded.
3220
3221 :returns: integer
3222
3223.. js:attribute:: act.ABORT
3224
3225 This attribute is an integer (6). It aborts the current message. The message
3226 processing is stopped and the transaction is terminated. For HTTP streams,
Willy Tarreau714f3452021-05-09 06:47:26 +02003227 HAProxy assumes a response was already sent to the client. From the Lua
Christopher Faulet0f3c8902020-01-31 18:57:12 +01003228 actions point of view, when this code is used, the transaction is terminated
3229 with no reply.
3230
3231 :returns: integer
3232
3233.. js:attribute:: act.INVALID
3234
3235 This attribute is an integer (7). It triggers an internal errors. The message
3236 processing is stopped and the transaction is terminated. For HTTP streams, an
3237 HTTP 400 error is returned to the client if the error is returned during the
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02003238 request analysis. During the response analysis, a HTTP 502 error is returned
Christopher Faulet0f3c8902020-01-31 18:57:12 +01003239 and the server response is discarded.
3240
3241 :returns: integer
Adis Nezirovic8878f8e2018-07-13 12:18:33 +02003242
Christopher Faulet2c2c2e32020-01-31 19:07:52 +01003243.. js:function:: act:wake_time(milliseconds)
3244
3245 **context**: action
3246
3247 Set the script pause timeout to the specified time, defined in
3248 milliseconds.
3249
3250 :param integer milliseconds: the required milliseconds.
3251
3252 This function may be used when a lua action returns `act.YIELD`, to force its
3253 wake-up at most after the specified number of milliseconds.
3254
Christopher Faulet5a2c6612021-08-15 20:35:25 +02003255.. _filter_class:
3256
3257Filter class
3258=============
3259
3260.. js:class:: filter
3261
3262 **context**: filter
3263
3264 This class contains return codes some filter callback functions may return. It
3265 also contains configuration flags and some helper functions. To understand how
3266 the filter API works, see `doc/internal/filters.txt` documentation.
3267
3268.. js:attribute:: filter.CONTINUE
3269
3270 This attribute is an integer (1). It may be returned by some filter callback
3271 functions to instruct this filtering step is finished for this filter.
3272
3273.. js:attribute:: filter.WAIT
3274
3275 This attribute is an integer (0). It may be returned by some filter callback
3276 functions to instruct the filtering must be paused, waiting for more data or
3277 for an external event depending on this filter.
3278
3279.. js:attribute:: filter.ERROR
3280
3281 This attribute is an integer (-1). It may be returned by some filter callback
3282 functions to trigger an error.
3283
3284.. js:attribute:: filter.FLT_CFG_FL_HTX
3285
3286 This attribute is a flag corresponding to the filter flag FLT_CFG_FL_HTX. When
3287 it is set for a filter, it means the filter is able to filter HTTP streams.
3288
3289.. js:function:: filter.register_data_filter(chn)
3290
3291 **context**: filter
3292
3293 Enable the data filtering on the channel **chn** for the current filter. It
Ilya Shipitsinff0f2782021-08-22 22:18:07 +05003294 may be called at any time from any callback functions proceeding the data
Christopher Faulet5a2c6612021-08-15 20:35:25 +02003295 analysis.
3296
3297 :param class_Channel chn: A :ref:`channel_class`.
3298
3299.. js:function:: filter.unregister_data_filter(chn)
3300
3301 **context**: filter
3302
3303 Disable the data filtering on the channel **chn** for the current filter. It
3304 may be called at any time from any callback functions.
3305
3306 :param class_Channel chn: A :ref:`channel_class`.
3307
3308.. js:function:: filter.wake_time(milliseconds)
3309
3310 **context**: filter
3311
3312 Set the script pause timeout to the specified time, defined in
3313 milliseconds.
3314
3315 :param integer milliseconds: the required milliseconds.
3316
3317 This function may be used from any lua filter callback function to force its
3318 wake-up at most after the specified number of milliseconds. Especially, when
3319 `filter.CONTINUE` is returned.
3320
3321
3322A filters is declared using :js:func:`core.register_filter()` function. The
3323provided class will be used to instantiate filters. It may define following
3324attributes:
3325
3326* id: The filter identifier. It is a string that identifies the filter and is
3327 optional.
3328
3329* flags: The filter flags. Only :js:attr:`filter.FLT_CFG_FL_HTX` may be set for now.
3330
3331Such filter class must also define all required callback functions in the
3332following list. Note that :js:func:`Filter.new()` must be defined otherwise the
Ilya Shipitsinff0f2782021-08-22 22:18:07 +05003333filter is ignored. Others are optional.
Christopher Faulet5a2c6612021-08-15 20:35:25 +02003334
3335* .. js:function:: FILTER.new()
3336
3337 Called to instantiate a new filter. This function must be defined.
3338
3339 :returns: a Lua object that will be used as filter instance for the current
3340 stream.
3341
3342* .. js:function:: FILTER.start_analyze(flt, txn, chn)
3343
3344 Called when the analysis starts on the channel **chn**.
3345
3346* .. js:function:: FILTER.end_analyze(flt, txn, chn)
3347
3348 Called when the analysis ends on the channel **chn**.
3349
3350* .. js:function:: FILTER.http_headers(flt, txn, http_msg)
3351
3352 Called just before the HTTP payload analysis and after any processing on the
3353 HTTP message **http_msg**. This callback functions is only called for HTTP
3354 streams.
3355
3356* .. js:function:: FILTER.http_payload(flt, txn, http_msg)
3357
3358 Called during the HTTP payload analysis on the HTTP message **http_msg**. This
3359 callback functions is only called for HTTP streams.
3360
3361* .. js:function:: FILTER.http_end(flt, txn, http_msg)
3362
3363 Called after the HTTP payload analysis on the HTTP message **http_msg**. This
3364 callback functions is only called for HTTP streams.
3365
3366* .. js:function:: FILTER.tcp_payload(flt, txn, chn)
3367
3368 Called during the TCP payload analysis on the channel **chn**.
3369
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02003370Here is a full example:
Christopher Faulet5a2c6612021-08-15 20:35:25 +02003371
3372.. code-block:: lua
3373
3374 Trace = {}
3375 Trace.id = "Lua trace filter"
3376 Trace.flags = filter.FLT_CFG_FL_HTX;
3377 Trace.__index = Trace
3378
3379 function Trace:new()
3380 local trace = {}
3381 setmetatable(trace, Trace)
3382 trace.req_len = 0
3383 trace.res_len = 0
3384 return trace
3385 end
3386
3387 function Trace:start_analyze(txn, chn)
3388 if chn:is_resp() then
3389 print("Start response analysis")
3390 else
3391 print("Start request analysis")
3392 end
3393 filter.register_data_filter(self, chn)
3394 end
3395
3396 function Trace:end_analyze(txn, chn)
3397 if chn:is_resp() then
3398 print("End response analysis: "..self.res_len.." bytes filtered")
3399 else
3400 print("End request analysis: "..self.req_len.." bytes filtered")
3401 end
3402 end
3403
3404 function Trace:http_headers(txn, http_msg)
3405 stline = http_msg:get_stline()
3406 if http_msg.channel:is_resp() then
3407 print("response:")
3408 print(stline.version.." "..stline.code.." "..stline.reason)
3409 else
3410 print("request:")
3411 print(stline.method.." "..stline.uri.." "..stline.version)
3412 end
3413
3414 for n, hdrs in pairs(http_msg:get_headers()) do
3415 for i,v in pairs(hdrs) do
3416 print(n..": "..v)
3417 end
3418 end
3419 return filter.CONTINUE
3420 end
3421
3422 function Trace:http_payload(txn, http_msg)
3423 body = http_msg:body(-20000)
3424 if http_msg.channel:is_resp() then
3425 self.res_len = self.res_len + body:len()
3426 else
3427 self.req_len = self.req_len + body:len()
3428 end
3429 end
3430
3431 core.register_filter("trace", Trace, function(trace, args)
3432 return trace
3433 end)
3434
3435..
3436
3437.. _httpmessage_class:
3438
3439HTTPMessage class
3440===================
3441
3442.. js:class:: HTTPMessage
3443
3444 **context**: filter
3445
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02003446 This class contains all functions to manipulate a HTTP message. For now, this
Christopher Faulet5a2c6612021-08-15 20:35:25 +02003447 class is only available from a filter context.
3448
3449.. js:function:: HTTPMessage.add_header(http_msg, name, value)
3450
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02003451 Appends a HTTP header field in the HTTP message **http_msg** whose name is
Christopher Faulet5a2c6612021-08-15 20:35:25 +02003452 specified in **name** and whose value is defined in **value**.
3453
3454 :param class_httpmessage http_msg: The manipulated HTTP message.
3455 :param string name: The header name.
3456 :param string value: The header value.
3457
3458.. js:function:: HTTPMessage.append(http_msg, string)
3459
3460 This function copies the string **string** at the end of incoming data of the
3461 HTTP message **http_msg**. The function returns the copied length on success
3462 or -1 if data cannot be copied.
3463
3464 Same that :js:func:`HTTPMessage.insert(http_msg, string, http_msg:input())`.
3465
3466 :param class_httpmessage http_msg: The manipulated HTTP message.
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02003467 :param string string: The data to copy at the end of incoming data.
Christopher Faulet5a2c6612021-08-15 20:35:25 +02003468 :returns: an integer containing the amount of bytes copied or -1.
3469
3470.. js:function:: HTTPMessage.body(http_msgl[, offset[, length]])
3471
3472 This function returns **length** bytes of incoming data from the HTTP message
3473 **http_msg**, starting at the offset **offset**. The data are not removed from
3474 the buffer.
3475
3476 By default, if no length is provided, all incoming data found, starting at the
3477 given offset, are returned. If **length** is set to -1, the function tries to
3478 retrieve a maximum of data. Because it is called in the filter context, it
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02003479 never yield. Not providing an offset is the same as setting it to 0. A
Ilya Shipitsinff0f2782021-08-22 22:18:07 +05003480 positive offset is relative to the beginning of incoming data of the
Christopher Faulet5a2c6612021-08-15 20:35:25 +02003481 http_message buffer while negative offset is relative to their end.
3482
3483 If there is no incoming data and the HTTP message can't receive more data, a 'nil'
3484 value is returned.
3485
3486 :param class_httpmessage http_msg: The manipulated HTTP message.
3487 :param integer offset: *optional* The offset in incoming data to start to get
3488 data. 0 by default. May be negative to be relative to
3489 the end of incoming data.
3490 :param integer length: *optional* The expected length of data to retrieve. All
3491 incoming data by default. May be set to -1 to get a
3492 maximum of data.
3493 :returns: a string containing the data found or nil.
3494
3495.. js:function:: HTTPMessage.eom(http_msg)
3496
3497 This function returns true if the end of message is reached for the HTTP
3498 message **http_msg**.
3499
3500 :param class_httpmessage http_msg: The manipulated HTTP message.
3501 :returns: an integer containing the amount of available bytes.
3502
3503.. js:function:: HTTPMessage.del_header(http_msg, name)
3504
3505 Removes all HTTP header fields in the HTTP message **http_msg** whose name is
3506 specified in **name**.
3507
3508 :param class_httpmessage http_msg: The manipulated http message.
3509 :param string name: The header name.
3510
3511.. js:function:: HTTPMessage.get_headers(http_msg)
3512
3513 Returns a table containing all the headers of the HTTP message **http_msg**.
3514
3515 :param class_httpmessage http_msg: The manipulated http message.
3516 :returns: table of headers.
3517
3518 This is the form of the returned table:
3519
3520.. code-block:: lua
3521
3522 http_msg:get_headers()['<header-name>'][<header-index>] = "<header-value>"
3523
3524 local hdr = http_msg:get_headers()
3525 hdr["host"][0] = "www.test.com"
3526 hdr["accept"][0] = "audio/basic q=1"
3527 hdr["accept"][1] = "audio/*, q=0.2"
3528 hdr["accept"][2] = "*.*, q=0.1"
3529..
3530
3531.. js:function:: HTTPMessage.get_stline(http_msg)
3532
3533 Returns a table containing the start-line of the HTTP message **http_msg**.
3534
3535 :param class_httpmessage http_msg: The manipulated http message.
3536 :returns: the start-line.
3537
3538 This is the form of the returned table:
3539
3540.. code-block:: lua
3541
3542 -- for the request :
3543 {"method" = string, "uri" = string, "version" = string}
3544
3545 -- for the response:
3546 {"version" = string, "code" = string, "reason" = string}
3547..
3548
3549.. js:function:: HTTPMessage.forward(http_msg, length)
3550
3551 This function forwards **length** bytes of data from the HTTP message
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02003552 **http_msg**. Because it is called in the filter context, it never yields. Only
Christopher Faulet5a2c6612021-08-15 20:35:25 +02003553 available incoming data may be forwarded, event if the requested length
3554 exceeds the available amount of incoming data. It returns the amount of data
3555 forwarded.
3556
3557 :param class_httpmessage http_msg: The manipulated HTTP message.
3558 :param integer int: The amount of data to forward.
3559
3560.. js:function:: HTTPMessage.input(http_msg)
3561
3562 This function returns the length of incoming data in the HTTP message
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02003563 **http_msg** from the filter point of view.
Christopher Faulet5a2c6612021-08-15 20:35:25 +02003564
3565 :param class_httpmessage http_msg: The manipulated HTTP message.
3566 :returns: an integer containing the amount of available bytes.
3567
3568.. js:function:: HTTPMessage.insert(http_msg, string[, offset])
3569
3570 This function copies the string **string** at the offset **offset** in
3571 incoming data of the HTTP message **http_msg**. The function returns the
3572 copied length on success or -1 if data cannot be copied.
3573
3574 By default, if no offset is provided, the string is copied in front of
Ilya Shipitsinff0f2782021-08-22 22:18:07 +05003575 incoming data. A positive offset is relative to the beginning of incoming data
Christopher Faulet5a2c6612021-08-15 20:35:25 +02003576 of the HTTP message while negative offset is relative to their end.
3577
3578 :param class_httpmessage http_msg: The manipulated HTTP message.
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02003579 :param string string: The data to copy into incoming data.
3580 :param integer offset: *optional* The offset in incoming data where to copy
Christopher Faulet5a2c6612021-08-15 20:35:25 +02003581 data. 0 by default. May be negative to be relative to
3582 the end of incoming data.
3583 :returns: an integer containing the amount of bytes copied or -1.
3584
3585.. js:function:: HTTPMessage.is_full(http_msg)
3586
3587 This function returns true if the HTTP message **http_msg** is full.
3588
3589 :param class_httpmessage http_msg: The manipulated HTTP message.
3590 :returns: a boolean
3591
3592.. js:function:: HTTPMessage.is_resp(http_msg)
3593
3594 This function returns true if the HTTP message **http_msg** is the response
3595 one.
3596
3597 :param class_httpmessage http_msg: The manipulated HTTP message.
3598 :returns: a boolean
3599
3600.. js:function:: HTTPMessage.may_recv(http_msg)
3601
3602 This function returns true if the HTTP message **http_msg** may still receive
3603 data.
3604
3605 :param class_httpmessage http_msg: The manipulated HTTP message.
3606 :returns: a boolean
3607
3608.. js:function:: HTTPMessage.output(http_msg)
3609
3610 This function returns the length of outgoing data of the HTTP message
3611 **http_msg**.
3612
3613 :param class_httpmessage http_msg: The manipulated HTTP message.
3614 :returns: an integer containing the amount of available bytes.
3615
3616.. js:function:: HTTPMessage.prepend(http_msg, string)
3617
3618 This function copies the string **string** in front of incoming data of the
3619 HTTP message **http_msg**. The function returns the copied length on success
3620 or -1 if data cannot be copied.
3621
3622 Same that :js:func:`HTTPMessage.insert(http_msg, string, 0)`.
3623
3624 :param class_httpmessage http_msg: The manipulated HTTP message.
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02003625 :param string string: The data to copy in front of incoming data.
Christopher Faulet5a2c6612021-08-15 20:35:25 +02003626 :returns: an integer containing the amount of bytes copied or -1.
3627
3628.. js:function:: HTTPMessage.remove(http_msg[, offset[, length]])
3629
3630 This function removes **length** bytes of incoming data of the HTTP message
3631 **http_msg**, starting at offset **offset**. This function returns number of
3632 bytes removed on success.
3633
3634 By default, if no length is provided, all incoming data, starting at the given
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02003635 offset, are removed. Not providing an offset is the same that setting it
Ilya Shipitsinff0f2782021-08-22 22:18:07 +05003636 to 0. A positive offset is relative to the beginning of incoming data of the
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02003637 HTTP message while negative offset is relative to the end.
Christopher Faulet5a2c6612021-08-15 20:35:25 +02003638
3639 :param class_httpmessage http_msg: The manipulated HTTP message.
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02003640 :param integer offset: *optional* The offset in incoming data where to start
Christopher Faulet5a2c6612021-08-15 20:35:25 +02003641 to remove data. 0 by default. May be negative to
3642 be relative to the end of incoming data.
3643 :param integer length: *optional* The length of data to remove. All incoming
3644 data by default.
3645 :returns: an integer containing the amount of bytes removed.
3646
3647.. js:function:: HTTPMessage.rep_header(http_msg, name, regex, replace)
3648
3649 Matches the regular expression in all occurrences of header field **name**
3650 according to regex **regex**, and replaces them with the string **replace**.
3651 The replacement value can contain back references like \1, \2, ... This
3652 function acts on whole header lines, regardless of the number of values they
3653 may contain.
3654
3655 :param class_httpmessage http_msg: The manipulated HTTP message.
3656 :param string name: The header name.
3657 :param string regex: The match regular expression.
3658 :param string replace: The replacement value.
3659
3660.. js:function:: HTTPMessage.rep_value(http_msg, name, regex, replace)
3661
3662 Matches the regular expression on every comma-delimited value of header field
3663 **name** according to regex **regex**, and replaces them with the string
3664 **replace**. The replacement value can contain back references like \1, \2,
3665 ...
3666
3667 :param class_httpmessage http_msg: The manipulated HTTP message.
3668 :param string name: The header name.
3669 :param string regex: The match regular expression.
3670 :param string replace: The replacement value.
3671
3672.. js:function:: HTTPMessage.send(http_msg, string)
3673
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02003674 This function requires immediate send of the string **string**. It means the
Ilya Shipitsinff0f2782021-08-22 22:18:07 +05003675 string is copied at the beginning of incoming data of the HTTP message
Christopher Faulet5a2c6612021-08-15 20:35:25 +02003676 **http_msg** and immediately forwarded. Because it is called in the filter
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02003677 context, it never yields.
Christopher Faulet5a2c6612021-08-15 20:35:25 +02003678
3679 :param class_httpmessage http_msg: The manipulated HTTP message.
3680 :param string string: The data to send.
3681 :returns: an integer containing the amount of bytes copied or -1.
3682
3683.. js:function:: HTTPMessage.set(http_msg, string[, offset[, length]])
3684
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02003685 This function replaces **length** bytes of incoming data of the HTTP message
Christopher Faulet5a2c6612021-08-15 20:35:25 +02003686 **http_msg**, starting at offset **offset**, by the string **string**. The
3687 function returns the copied length on success or -1 if data cannot be copied.
3688
3689 By default, if no length is provided, all incoming data, starting at the given
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02003690 offset, are replaced. Not providing an offset is the same as setting it
Ilya Shipitsinff0f2782021-08-22 22:18:07 +05003691 to 0. A positive offset is relative to the beginning of incoming data of the
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02003692 HTTP message while negative offset is relative to the end.
Christopher Faulet5a2c6612021-08-15 20:35:25 +02003693
3694 :param class_httpmessage http_msg: The manipulated HTTP message.
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02003695 :param string string: The data to copy into incoming data.
3696 :param integer offset: *optional* The offset in incoming data where to start
Christopher Faulet5a2c6612021-08-15 20:35:25 +02003697 the data replacement. 0 by default. May be negative to
3698 be relative to the end of incoming data.
3699 :param integer length: *optional* The length of data to replace. All incoming
3700 data by default.
3701 :returns: an integer containing the amount of bytes copied or -1.
3702
3703.. js:function:: HTTPMessage.set_eom(http_msg)
3704
3705 This function set the end of message for the HTTP message **http_msg**.
3706
3707 :param class_httpmessage http_msg: The manipulated HTTP message.
3708
3709.. js:function:: HTTPMessage.set_header(http_msg, name, value)
3710
3711 This variable replace all occurrence of all header matching the name **name**,
3712 by only one containing the value **value**.
3713
3714 :param class_httpmessage http_msg: The manipulated HTTP message.
3715 :param string name: The header name.
3716 :param string value: The header value.
3717
3718 This function does the same work as the following code:
3719
3720.. code-block:: lua
3721
3722 http_msg:del_header("header")
3723 http_msg:add_header("header", "value")
3724..
3725
3726.. js:function:: HTTPMessage.set_method(http_msg, method)
3727
3728 Rewrites the request method with the string **method**. The HTTP message
3729 **http_msg** must be the request.
3730
3731 :param class_httpmessage http_msg: The manipulated HTTP message.
3732 :param string method: The new method.
3733
3734.. js:function:: HTTPMessage.set_path(http_msg, path)
3735
3736 Rewrites the request path with the string **path**. The HTTP message
3737 **http_msg** must be the request.
3738
3739 :param class_httpmessage http_msg: The manipulated HTTP message.
3740 :param string method: The new method.
3741
3742.. js:function:: HTTPMessage.set_query(http_msg, query)
3743
3744 Rewrites the request's query string which appears after the first question
3745 mark ("?") with the string **query**. The HTTP message **http_msg** must be
3746 the request.
3747
3748 :param class_httpmessage http_msg: The manipulated HTTP message.
3749 :param string query: The new query.
3750
3751.. js:function:: HTTPMessage.set_status(http_msg, status[, reason])
3752
Ilya Shipitsinff0f2782021-08-22 22:18:07 +05003753 Rewrites the response status code with the integer **code** and optional the
Christopher Faulet5a2c6612021-08-15 20:35:25 +02003754 reason **reason**. If no custom reason is provided, it will be generated from
3755 the status. The HTTP message **http_msg** must be the response.
3756
3757 :param class_httpmessage http_msg: The manipulated HTTP message.
3758 :param integer status: The new response status code.
3759 :param string reason: The new response reason (optional).
3760
3761.. js:function:: HTTPMessage.set_uri(http_msg, uri)
3762
3763 Rewrites the request URI with the string **uri**. The HTTP message
3764 **http_msg** must be the request.
3765
3766 :param class_httpmessage http_msg: The manipulated HTTP message.
3767 :param string uri: The new uri.
3768
3769.. js:function:: HTTPMessage.unset_eom(http_msg)
3770
3771 This function remove the end of message for the HTTP message **http_msg**.
3772
3773 :param class_httpmessage http_msg: The manipulated HTTP message.
3774
William Lallemand10cea5c2022-03-30 16:02:43 +02003775.. _CertCache_class:
3776
3777CertCache class
3778================
3779
3780.. js:class:: CertCache
3781
3782 This class allows to update an SSL certificate file in the memory of the
3783 current HAProxy process. It will do the same as "set ssl cert" + "commit ssl
3784 cert" over the HAProxy CLI.
3785
3786.. js:function:: CertCache.set(certificate)
3787
3788 This function updates a certificate in memory.
3789
3790 :param table certificate: A table containing the fields to update.
3791 :param string certificate.filename: The mandatory filename of the certificate
3792 to update, it must already exist in memory.
3793 :param string certificate.crt: A certificate in the PEM format. It can also
3794 contain a private key.
3795 :param string certificate.key: A private key in the PEM format.
3796 :param string certificate.ocsp: An OCSP response in base64. (cf management.txt)
3797 :param string certificate.issuer: The certificate of the OCSP issuer.
3798 :param string certificate.sctl: An SCTL file.
3799
3800.. code-block:: lua
3801
3802 CertCache.set{filename="certs/localhost9994.pem.rsa", crt=crt}
3803
3804
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01003805External Lua libraries
3806======================
3807
3808A lot of useful lua libraries can be found here:
3809
Aurelien DARRAGON846fc7d2022-10-14 08:48:57 +02003810* Lua toolbox has been superseded by `https://luarocks.org/ <https://luarocks.org/>`_
3811 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 +01003812
Ilya Shipitsin2075ca82020-03-06 23:22:22 +05003813Redis client library:
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01003814
3815* `https://github.com/nrk/redis-lua <https://github.com/nrk/redis-lua>`_
3816
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02003817This is an example about the usage of the Redis library within HAProxy. Note that
3818each call to any function of this library can throw an error if the socket
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01003819connection fails.
3820
3821.. code-block:: lua
3822
3823 -- load the redis library
3824 local redis = require("redis");
3825
3826 function do_something(txn)
3827
3828 -- create and connect new tcp socket
3829 local tcp = core.tcp();
3830 tcp:settimeout(1);
3831 tcp:connect("127.0.0.1", 6379);
3832
3833 -- use the redis library with this new socket
3834 local client = redis.connect({socket=tcp});
3835 client:ping();
3836
3837 end
3838
3839OpenSSL:
3840
3841* `http://mkottman.github.io/luacrypto/index.html
3842 <http://mkottman.github.io/luacrypto/index.html>`_
3843
3844* `https://github.com/brunoos/luasec/wiki
3845 <https://github.com/brunoos/luasec/wiki>`_