blob: 146c05047342bfecdf89ede3bdd85ab66529ca58 [file] [log] [blame]
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001.. toctree::
2 :maxdepth: 2
3
4
5How Lua runs in HAProxy
6=======================
7
8HAProxy Lua running contexts
9----------------------------
10
11The Lua code executed in HAProxy can be processed in 2 main modes. The first one
12is the **initialisation mode**, and the second is the **runtime mode**.
13
14* In the **initialisation mode**, we can perform DNS solves, but we cannot
15 perform socket I/O. In this initialisation mode, HAProxy still blocked during
16 the execution of the Lua program.
17
18* In the **runtime mode**, we cannot perform DNS solves, but we can use sockets.
19 The execution of the Lua code is multiplexed with the requests processing, so
20 the Lua code seems to be run in blocking, but it is not the case.
21
22The Lua code is loaded in one or more files. These files contains main code and
Christopher Faulet5a2c6612021-08-15 20:35:25 +020023functions. Lua have 7 execution context.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +010024
251. The Lua file **body context**. It is executed during the load of the Lua file
26 in the HAProxy `[global]` section with the directive `lua-load`. It is
27 executed in initialisation mode. This section is use for configuring Lua
28 bindings in HAProxy.
29
David Carlier61fdf8b2015-10-02 11:59:38 +0100302. The Lua **init context**. It is a Lua function executed just after the
Thierry FOURNIER17bd1522015-03-11 20:31:00 +010031 HAProxy configuration parsing. The execution is in initialisation mode. In
32 this context the HAProxy environment are already initialized. It is useful to
33 check configuration, or initializing socket connections or tasks. These
34 functions are declared in the body context with the Lua function
35 `core.register_init()`. The prototype of the function is a simple function
36 without return value and without parameters, like this: `function fcn()`.
37
David Carlier61fdf8b2015-10-02 11:59:38 +0100383. The Lua **task context**. It is a Lua function executed after the start
Thierry FOURNIER17bd1522015-03-11 20:31:00 +010039 of the HAProxy scheduler, and just after the declaration of the task with the
40 Lua function `core.register_task()`. This context can be concurrent with the
41 traffic processing. It is executed in runtime mode. The prototype of the
42 function is a simple function without return value and without parameters,
43 like this: `function fcn()`.
44
David Carlier61fdf8b2015-10-02 11:59:38 +0100454. The **action context**. It is a Lua function conditionally executed. These
Thierry FOURNIERa2d02252015-10-01 15:00:42 +020046 actions are registered by the Lua directives "`core.register_action()`". The
47 prototype of the Lua called function is a function with doesn't returns
48 anything and that take an object of class TXN as entry. `function fcn(txn)`.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +010049
505. The **sample-fetch context**. This function takes a TXN object as entry
51 argument and returns a string. These types of function cannot execute any
52 blocking function. They are useful to aggregate some of original HAProxy
53 sample-fetches and return the result. The prototype of the function is
54 `function string fcn(txn)`. These functions can be registered with the Lua
55 function `core.register_fetches()`. Each declared sample-fetch is prefixed by
56 the string "lua.".
57
Christopher Faulet1e9b1b62021-08-11 10:14:30 +020058 .. note::
59 It is possible that this function cannot found the required data in the
60 original HAProxy sample-fetches, in this case, it cannot return the
61 result. This case is not yet supported
Thierry FOURNIER17bd1522015-03-11 20:31:00 +010062
David Carlier61fdf8b2015-10-02 11:59:38 +0100636. The **converter context**. It is a Lua function that takes a string as input
Thierry FOURNIER17bd1522015-03-11 20:31:00 +010064 and returns another string as output. These types of function are stateless,
65 it cannot access to any context. They don't execute any blocking function.
66 The call prototype is `function string fcn(string)`. This function can be
67 registered with the Lua function `core.register_converters()`. Each declared
68 converter is prefixed by the string "lua.".
69
Christopher Faulet5a2c6612021-08-15 20:35:25 +0200707. The **filter context**: It is a Lua object based on a class defining filter
71 callback functions. Lua filters are registered using
72 `core.register_filter()`. Each declared filter is prefixed by the string
73 "lua.".
74
Christopher Faulet5a2c6612021-08-15 20:35:25 +020075
Thierry FOURNIER17bd1522015-03-11 20:31:00 +010076HAProxy Lua Hello world
77-----------------------
78
79HAProxy configuration file (`hello_world.conf`):
80
81::
82
83 global
84 lua-load hello_world.lua
85
86 listen proxy
87 bind 127.0.0.1:10001
Thierry FOURNIERa2d02252015-10-01 15:00:42 +020088 tcp-request inspect-delay 1s
89 tcp-request content use-service lua.hello_world
Thierry FOURNIER17bd1522015-03-11 20:31:00 +010090
91HAProxy Lua file (`hello_world.lua`):
92
93.. code-block:: lua
94
Thierry FOURNIERa2d02252015-10-01 15:00:42 +020095 core.register_service("hello_world", "tcp", function(applet)
96 applet:send("hello world\n")
97 end)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +010098
99How to start HAProxy for testing this configuration:
100
101::
102
103 ./haproxy -f hello_world.conf
104
105On other terminal, you can test with telnet:
106
107::
108
109 #:~ telnet 127.0.0.1 10001
110 hello world
111
Thierry Fournierae6b5682022-09-19 09:04:16 +0200112Usage of load parameters
113------------------------
114
Ilya Shipitsin4a689da2022-10-29 09:34:32 +0500115HAProxy lua-load(-per-thread) directives allow a list of parameters after
Thierry Fournierae6b5682022-09-19 09:04:16 +0200116the lua file name. These parameters are accessible through an array of args
117using this code `local args = table.pack(...)` in the body of loaded file.
118
119Below, a new version of the hello world using load parameters
120
121HAProxy configuration file (`hello_world.conf`):
122
123::
124
125 global
126 lua-load hello_world.lua "this is not an hello world"
127
128 listen proxy
129 bind 127.0.0.1:10001
130 tcp-request inspect-delay 1s
131 tcp-request content use-service lua.hello_world
132
133HAProxy Lua file (`hello_world.lua`):
134
135.. code-block:: lua
136
137 local args = table.pack(...)
138
139 core.register_service("hello_world", "tcp", function(applet)
140 applet:send(args[1] .. "\n")
141 end)
142
143
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100144Core class
145==========
146
147.. js:class:: core
148
149 The "core" class contains all the HAProxy core functions. These function are
Aurelien DARRAGON53901f42022-10-13 19:49:42 +0200150 useful for the controlling of the execution flow, registering hooks, manipulating
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100151 global maps or ACL, ...
152
153 "core" class is basically provided with HAProxy. No `require` line is
154 required to uses these function.
155
David Carlier61fdf8b2015-10-02 11:59:38 +0100156 The "core" class is static, it is not possible to create a new object of this
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100157 type.
158
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100159.. js:attribute:: core.emerg
160
Thierry FOURNIERdc595002015-12-21 11:13:52 +0100161 :returns: integer
162
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100163 This attribute is an integer, it contains the value of the loglevel "emergency" (0).
164
165.. js:attribute:: core.alert
166
Thierry FOURNIERdc595002015-12-21 11:13:52 +0100167 :returns: integer
168
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100169 This attribute is an integer, it contains the value of the loglevel "alert" (1).
170
171.. js:attribute:: core.crit
172
Thierry FOURNIERdc595002015-12-21 11:13:52 +0100173 :returns: integer
174
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100175 This attribute is an integer, it contains the value of the loglevel "critical" (2).
176
177.. js:attribute:: core.err
178
Thierry FOURNIERdc595002015-12-21 11:13:52 +0100179 :returns: integer
180
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100181 This attribute is an integer, it contains the value of the loglevel "error" (3).
182
183.. js:attribute:: core.warning
184
Thierry FOURNIERdc595002015-12-21 11:13:52 +0100185 :returns: integer
186
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100187 This attribute is an integer, it contains the value of the loglevel "warning" (4).
188
189.. js:attribute:: core.notice
190
Thierry FOURNIERdc595002015-12-21 11:13:52 +0100191 :returns: integer
192
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100193 This attribute is an integer, it contains the value of the loglevel "notice" (5).
194
195.. js:attribute:: core.info
196
Thierry FOURNIERdc595002015-12-21 11:13:52 +0100197 :returns: integer
198
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100199 This attribute is an integer, it contains the value of the loglevel "info" (6).
200
201.. js:attribute:: core.debug
202
Thierry FOURNIERdc595002015-12-21 11:13:52 +0100203 :returns: integer
204
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100205 This attribute is an integer, it contains the value of the loglevel "debug" (7).
206
Thierry FOURNIER12a865d2016-12-14 19:40:37 +0100207.. js:attribute:: core.proxies
208
209 **context**: task, action, sample-fetch, converter
210
Patrick Hemmerc6a1d712018-05-01 21:30:41 -0400211 This attribute is a table of declared proxies (frontend and backends). Each
212 proxy give an access to his list of listeners and servers. The table is
213 indexed by proxy name, and each entry is of type :ref:`proxy_class`.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +0100214
Christopher Faulet1e9b1b62021-08-11 10:14:30 +0200215 .. Warning::
Aurelien DARRAGON53901f42022-10-13 19:49:42 +0200216 if you declared a frontend and backend with the same name, only one of
217 them will be listed.
Thierry FOURNIER9b82a582017-07-24 13:30:43 +0200218
219 :see: :js:attr:`core.backends`
220 :see: :js:attr:`core.frontends`
221
222.. js:attribute:: core.backends
223
224 **context**: task, action, sample-fetch, converter
225
Patrick Hemmerc6a1d712018-05-01 21:30:41 -0400226 This attribute is a table of declared proxies with backend capability. Each
227 proxy give an access to his list of listeners and servers. The table is
228 indexed by the backend name, and each entry is of type :ref:`proxy_class`.
Thierry FOURNIER9b82a582017-07-24 13:30:43 +0200229
230 :see: :js:attr:`core.proxies`
231 :see: :js:attr:`core.frontends`
232
233.. js:attribute:: core.frontends
234
235 **context**: task, action, sample-fetch, converter
236
Patrick Hemmerc6a1d712018-05-01 21:30:41 -0400237 This attribute is a table of declared proxies with frontend capability. Each
238 proxy give an access to his list of listeners and servers. The table is
239 indexed by the frontend name, and each entry is of type :ref:`proxy_class`.
Thierry FOURNIER9b82a582017-07-24 13:30:43 +0200240
241 :see: :js:attr:`core.proxies`
242 :see: :js:attr:`core.backends`
243
Thierry Fournierecb83c22020-11-28 15:49:44 +0100244.. js:attribute:: core.thread
245
246 **context**: task, action, sample-fetch, converter, applet
247
248 This variable contains the executing thread number starting at 1. 0 is a
249 special case for the common lua context. So, if thread is 0, Lua scope is
250 shared by all threads, otherwise the scope is dedicated to a single thread.
251 A program which needs to execute some parts exactly once regardless of the
252 number of threads can check that core.thread is 0 or 1.
253
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100254.. js:function:: core.log(loglevel, msg)
255
256 **context**: body, init, task, action, sample-fetch, converter
257
David Carlier61fdf8b2015-10-02 11:59:38 +0100258 This function sends a log. The log is sent, according with the HAProxy
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100259 configuration file, on the default syslog server if it is configured and on
260 the stderr if it is allowed.
261
Bertrand Jacquin874a35c2018-09-10 21:26:07 +0100262 :param integer loglevel: Is the log level associated with the message. It is a
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100263 number between 0 and 7.
264 :param string msg: The log content.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +0100265 :see: :js:attr:`core.emerg`, :js:attr:`core.alert`, :js:attr:`core.crit`,
266 :js:attr:`core.err`, :js:attr:`core.warning`, :js:attr:`core.notice`,
267 :js:attr:`core.info`, :js:attr:`core.debug` (log level definitions)
268 :see: :js:func:`core.Debug`
269 :see: :js:func:`core.Info`
270 :see: :js:func:`core.Warning`
271 :see: :js:func:`core.Alert`
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100272
273.. js:function:: core.Debug(msg)
274
275 **context**: body, init, task, action, sample-fetch, converter
276
277 :param string msg: The log content.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +0100278 :see: :js:func:`core.log`
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100279
280 Does the same job than:
281
282.. code-block:: lua
283
Thierry FOURNIERdc595002015-12-21 11:13:52 +0100284 function Debug(msg)
285 core.log(core.debug, msg)
286 end
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100287..
288
289.. js:function:: core.Info(msg)
290
291 **context**: body, init, task, action, sample-fetch, converter
292
293 :param string msg: The log content.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +0100294 :see: :js:func:`core.log`
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100295
296.. code-block:: lua
297
Thierry FOURNIERdc595002015-12-21 11:13:52 +0100298 function Info(msg)
299 core.log(core.info, msg)
300 end
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100301..
302
303.. js:function:: core.Warning(msg)
304
305 **context**: body, init, task, action, sample-fetch, converter
306
307 :param string msg: The log content.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +0100308 :see: :js:func:`core.log`
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100309
310.. code-block:: lua
311
Thierry FOURNIERdc595002015-12-21 11:13:52 +0100312 function Warning(msg)
313 core.log(core.warning, msg)
314 end
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100315..
316
317.. js:function:: core.Alert(msg)
318
319 **context**: body, init, task, action, sample-fetch, converter
320
321 :param string msg: The log content.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +0100322 :see: :js:func:`core.log`
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100323
324.. code-block:: lua
325
Thierry FOURNIERdc595002015-12-21 11:13:52 +0100326 function Alert(msg)
327 core.log(core.alert, msg)
328 end
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100329..
330
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100331.. js:function:: core.add_acl(filename, key)
332
333 **context**: init, task, action, sample-fetch, converter
334
335 Add the ACL *key* in the ACLs list referenced by the file *filename*.
336
337 :param string filename: the filename that reference the ACL entries.
338 :param string key: the key which will be added.
339
340.. js:function:: core.del_acl(filename, key)
341
342 **context**: init, task, action, sample-fetch, converter
343
344 Delete the ACL entry referenced by the key *key* in the list of ACLs
345 referenced by *filename*.
346
347 :param string filename: the filename that reference the ACL entries.
348 :param string key: the key which will be deleted.
349
350.. js:function:: core.del_map(filename, key)
351
352 **context**: init, task, action, sample-fetch, converter
353
354 Delete the map entry indexed with the specified key in the list of maps
355 referenced by his filename.
356
357 :param string filename: the filename that reference the map entries.
358 :param string key: the key which will be deleted.
359
Thierry Fourniereea77c02016-03-18 08:47:13 +0100360.. js:function:: core.get_info()
361
362 **context**: body, init, task, action, sample-fetch, converter
363
Aurelien DARRAGON53901f42022-10-13 19:49:42 +0200364 Returns HAProxy core information. We can find information like the uptime,
Thierry Fourniereea77c02016-03-18 08:47:13 +0100365 the pid, memory pool usage, tasks number, ...
366
Ilya Shipitsin5fa29b82022-12-07 09:46:19 +0500367 This information is also returned by the management socket via the command
Bertrand Jacquin874a35c2018-09-10 21:26:07 +0100368 "show info". See the management socket documentation for more information
Thierry Fourniereea77c02016-03-18 08:47:13 +0100369 about the content of these variables.
370
371 :returns: an array of values.
372
Thierry Fournierb1f46562016-01-21 09:46:15 +0100373.. js:function:: core.now()
374
375 **context**: body, init, task, action
376
377 This function returns the current time. The time returned is fixed by the
Bertrand Jacquin874a35c2018-09-10 21:26:07 +0100378 HAProxy core and assures than the hour will be monotonic and that the system
Thierry Fournierb1f46562016-01-21 09:46:15 +0100379 call 'gettimeofday' will not be called too. The time is refreshed between each
380 Lua execution or resume, so two consecutive call to the function "now" will
381 probably returns the same result.
382
Patrick Hemmerc6a1d712018-05-01 21:30:41 -0400383 :returns: a table which contains two entries "sec" and "usec". "sec"
Thierry Fournierb1f46562016-01-21 09:46:15 +0100384 contains the current at the epoch format, and "usec" contains the
385 current microseconds.
386
Thierry FOURNIERa78f0372016-12-14 19:04:41 +0100387.. js:function:: core.http_date(date)
388
389 **context**: body, init, task, action
390
Bertrand Jacquin874a35c2018-09-10 21:26:07 +0100391 This function take a string representing http date, and returns an integer
Thierry FOURNIERa78f0372016-12-14 19:04:41 +0100392 containing the corresponding date with a epoch format. A valid http date
393 me respect the format IMF, RFC850 or ASCTIME.
394
395 :param string date: a date http-date formatted
396 :returns: integer containing epoch date
397 :see: :js:func:`core.imf_date`.
398 :see: :js:func:`core.rfc850_date`.
399 :see: :js:func:`core.asctime_date`.
400 :see: https://tools.ietf.org/html/rfc7231#section-7.1.1.1
401
402.. js:function:: core.imf_date(date)
403
404 **context**: body, init, task, action
405
Bertrand Jacquin874a35c2018-09-10 21:26:07 +0100406 This function take a string representing IMF date, and returns an integer
Thierry FOURNIERa78f0372016-12-14 19:04:41 +0100407 containing the corresponding date with a epoch format.
408
409 :param string date: a date IMF formatted
410 :returns: integer containing epoch date
411 :see: https://tools.ietf.org/html/rfc7231#section-7.1.1.1
412
413 The IMF format is like this:
414
415.. code-block:: text
416
417 Sun, 06 Nov 1994 08:49:37 GMT
418..
419
420.. js:function:: core.rfc850_date(date)
421
422 **context**: body, init, task, action
423
Bertrand Jacquin874a35c2018-09-10 21:26:07 +0100424 This function take a string representing RFC850 date, and returns an integer
Thierry FOURNIERa78f0372016-12-14 19:04:41 +0100425 containing the corresponding date with a epoch format.
426
427 :param string date: a date RFC859 formatted
428 :returns: integer containing epoch date
429 :see: https://tools.ietf.org/html/rfc7231#section-7.1.1.1
430
431 The RFC850 format is like this:
432
433.. code-block:: text
434
435 Sunday, 06-Nov-94 08:49:37 GMT
436..
437
438.. js:function:: core.asctime_date(date)
439
440 **context**: body, init, task, action
441
Bertrand Jacquin874a35c2018-09-10 21:26:07 +0100442 This function take a string representing ASCTIME date, and returns an integer
Thierry FOURNIERa78f0372016-12-14 19:04:41 +0100443 containing the corresponding date with a epoch format.
444
445 :param string date: a date ASCTIME formatted
446 :returns: integer containing epoch date
447 :see: https://tools.ietf.org/html/rfc7231#section-7.1.1.1
448
449 The ASCTIME format is like this:
450
451.. code-block:: text
452
453 Sun Nov 6 08:49:37 1994
454..
455
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100456.. js:function:: core.msleep(milliseconds)
457
458 **context**: body, init, task, action
459
460 The `core.msleep()` stops the Lua execution between specified milliseconds.
461
462 :param integer milliseconds: the required milliseconds.
463
Thierry FOURNIERc5d11c62018-02-12 14:46:54 +0100464.. js:function:: core.register_action(name, actions, func [, nb_args])
Thierry FOURNIER8255a752015-09-23 21:03:35 +0200465
466 **context**: body
467
David Carlier61fdf8b2015-10-02 11:59:38 +0100468 Register a Lua function executed as action. All the registered action can be
Thierry FOURNIER8255a752015-09-23 21:03:35 +0200469 used in HAProxy with the prefix "lua.". An action gets a TXN object class as
470 input.
471
472 :param string name: is the name of the converter.
Willy Tarreau61add3c2015-09-28 15:39:10 +0200473 :param table actions: is a table of string describing the HAProxy actions who
Thierry FOURNIER8255a752015-09-23 21:03:35 +0200474 want to register to. The expected actions are 'tcp-req',
475 'tcp-res', 'http-req' or 'http-res'.
Aurelien DARRAGON53901f42022-10-13 19:49:42 +0200476 :param function func: is the Lua function called to work as converter.
Thierry FOURNIERc5d11c62018-02-12 14:46:54 +0100477 :param integer nb_args: is the expected number of argument for the action.
478 By default the value is 0.
Thierry FOURNIER8255a752015-09-23 21:03:35 +0200479
480 The prototype of the Lua function used as argument is:
481
482.. code-block:: lua
483
Thierry FOURNIERc5d11c62018-02-12 14:46:54 +0100484 function(txn [, arg1 [, arg2]])
Thierry FOURNIER8255a752015-09-23 21:03:35 +0200485..
486
Thierry FOURNIERdc595002015-12-21 11:13:52 +0100487 * **txn** (:ref:`txn_class`): this is a TXN object used for manipulating the
Thierry FOURNIER8255a752015-09-23 21:03:35 +0200488 current request or TCP stream.
489
Bertrand Jacquin874a35c2018-09-10 21:26:07 +0100490 * **argX**: this is argument provided through the HAProxy configuration file.
Thierry FOURNIERc5d11c62018-02-12 14:46:54 +0100491
Bertrand Jacquin874a35c2018-09-10 21:26:07 +0100492 Here, an example of action registration. The action just send an 'Hello world'
Thierry FOURNIER8255a752015-09-23 21:03:35 +0200493 in the logs.
494
495.. code-block:: lua
496
497 core.register_action("hello-world", { "tcp-req", "http-req" }, function(txn)
498 txn:Info("Hello world")
499 end)
500..
501
Willy Tarreau714f3452021-05-09 06:47:26 +0200502 This example code is used in HAProxy configuration like this:
Thierry FOURNIER8255a752015-09-23 21:03:35 +0200503
504::
505
506 frontend tcp_frt
507 mode tcp
508 tcp-request content lua.hello-world
509
510 frontend http_frt
511 mode http
512 http-request lua.hello-world
Thierry FOURNIERc5d11c62018-02-12 14:46:54 +0100513..
514
Bertrand Jacquin874a35c2018-09-10 21:26:07 +0100515 A second example using arguments
Thierry FOURNIERc5d11c62018-02-12 14:46:54 +0100516
517.. code-block:: lua
518
519 function hello_world(txn, arg)
520 txn:Info("Hello world for " .. arg)
521 end
522 core.register_action("hello-world", { "tcp-req", "http-req" }, hello_world, 2)
523..
Thierry FOURNIER8255a752015-09-23 21:03:35 +0200524
Willy Tarreau714f3452021-05-09 06:47:26 +0200525 This example code is used in HAProxy configuration like this:
Thierry FOURNIERc5d11c62018-02-12 14:46:54 +0100526
527::
528
529 frontend tcp_frt
530 mode tcp
531 tcp-request content lua.hello-world everybody
532..
Aurelien DARRAGON53901f42022-10-13 19:49:42 +0200533
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100534.. js:function:: core.register_converters(name, func)
535
536 **context**: body
537
David Carlier61fdf8b2015-10-02 11:59:38 +0100538 Register a Lua function executed as converter. All the registered converters
Aurelien DARRAGON53901f42022-10-13 19:49:42 +0200539 can be used in HAProxy with the prefix "lua.". A converter gets a string as
540 input and returns a string as output. The registered function can take up to 9
541 values as parameter. All the values are strings.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100542
543 :param string name: is the name of the converter.
544 :param function func: is the Lua function called to work as converter.
545
546 The prototype of the Lua function used as argument is:
547
548.. code-block:: lua
549
550 function(str, [p1 [, p2 [, ... [, p5]]]])
551..
552
553 * **str** (*string*): this is the input value automatically converted in
554 string.
555 * **p1** .. **p5** (*string*): this is a list of string arguments declared in
Bertrand Jacquin874a35c2018-09-10 21:26:07 +0100556 the HAProxy configuration file. The number of arguments doesn't exceed 5.
Aurelien DARRAGON53901f42022-10-13 19:49:42 +0200557 The order and the nature of these is conventionally chosen by the
Bertrand Jacquin874a35c2018-09-10 21:26:07 +0100558 developer.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100559
560.. js:function:: core.register_fetches(name, func)
561
562 **context**: body
563
David Carlier61fdf8b2015-10-02 11:59:38 +0100564 Register a Lua function executed as sample fetch. All the registered sample
Bertrand Jacquin874a35c2018-09-10 21:26:07 +0100565 fetch can be used in HAProxy with the prefix "lua.". A Lua sample fetch
Aurelien DARRAGON53901f42022-10-13 19:49:42 +0200566 returns a string as output. The registered function can take up to 9 values as
567 parameter. All the values are strings.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100568
Aurelien DARRAGON53901f42022-10-13 19:49:42 +0200569 :param string name: is the name of the sample fetch.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100570 :param function func: is the Lua function called to work as sample fetch.
571
572 The prototype of the Lua function used as argument is:
573
574.. code-block:: lua
575
576 string function(txn, [p1 [, p2 [, ... [, p5]]]])
577..
578
Thierry FOURNIERdc595002015-12-21 11:13:52 +0100579 * **txn** (:ref:`txn_class`): this is the txn object associated with the current
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100580 request.
581 * **p1** .. **p5** (*string*): this is a list of string arguments declared in
Bertrand Jacquin874a35c2018-09-10 21:26:07 +0100582 the HAProxy configuration file. The number of arguments doesn't exceed 5.
Aurelien DARRAGON53901f42022-10-13 19:49:42 +0200583 The order and the nature of these is conventionally chosen by the
Bertrand Jacquin874a35c2018-09-10 21:26:07 +0100584 developer.
585 * **Returns**: A string containing some data, or nil if the value cannot be
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100586 returned now.
587
588 lua example code:
589
590.. code-block:: lua
591
592 core.register_fetches("hello", function(txn)
593 return "hello"
594 end)
595..
596
597 HAProxy example configuration:
598
599::
600
601 frontend example
602 http-request redirect location /%[lua.hello]
603
Christopher Faulet5a2c6612021-08-15 20:35:25 +0200604.. js:function:: core.register_filter(name, Flt, func)
605
606 **context**: body
607
608 Register a Lua function used to declare a filter. All the registered filters
609 can by used in HAProxy with the prefix "lua.".
610
611 :param string name: is the name of the filter.
612 :param table Flt: is a Lua class containing the filter definition (id, flags,
613 callbacks).
614 :param function func: is the Lua function called to create the Lua filter.
615
616 The prototype of the Lua function used as argument is:
617
618.. code-block:: lua
619
620 function(flt, args)
621..
622
623 * **flt** : Is a filter object based on the class provided in
624 :js:func:`core.register_filter()` function.
625
626 * **args**: Is a table of strings containing all arguments provided through
627 the HAProxy configuration file, on the filter line.
628
629 It must return the filter to use or nil to ignore it. Here, an example of
630 filter registration.
631
632.. code-block:: lua
633
634 core.register_filter("my-filter", MyFilter, function(flt, args)
635 flt.args = args -- Save arguments
636 return flt
637 end)
638..
639
640 This example code is used in HAProxy configuration like this:
641
642::
643
644 frontend http
645 mode http
646 filter lua.my-filter arg1 arg2 arg3
647..
648
649 :see: :js:class:`Filter`
650
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +0200651.. js:function:: core.register_service(name, mode, func)
652
653 **context**: body
654
Aurelien DARRAGON53901f42022-10-13 19:49:42 +0200655 Register a Lua function executed as a service. All the registered services can
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +0200656 be used in HAProxy with the prefix "lua.". A service gets an object class as
657 input according with the required mode.
658
Aurelien DARRAGON53901f42022-10-13 19:49:42 +0200659 :param string name: is the name of the service.
Willy Tarreau61add3c2015-09-28 15:39:10 +0200660 :param string mode: is string describing the required mode. Only 'tcp' or
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +0200661 'http' are allowed.
Aurelien DARRAGON53901f42022-10-13 19:49:42 +0200662 :param function func: is the Lua function called to work as service.
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +0200663
664 The prototype of the Lua function used as argument is:
665
666.. code-block:: lua
667
668 function(applet)
669..
670
Thierry FOURNIERdc595002015-12-21 11:13:52 +0100671 * **applet** *applet* will be a :ref:`applettcp_class` or a
672 :ref:`applethttp_class`. It depends the type of registered applet. An applet
673 registered with the 'http' value for the *mode* parameter will gets a
674 :ref:`applethttp_class`. If the *mode* value is 'tcp', the applet will gets
675 a :ref:`applettcp_class`.
676
Christopher Faulet1e9b1b62021-08-11 10:14:30 +0200677 .. warning::
678 Applets of type 'http' cannot be called from 'tcp-*' rulesets. Only the
679 'http-*' rulesets are authorized, this means that is not possible to call
Aurelien DARRAGON53901f42022-10-13 19:49:42 +0200680 a HTTP applet from a proxy in tcp mode. Applets of type 'tcp' can be
Christopher Faulet1e9b1b62021-08-11 10:14:30 +0200681 called from anywhere.
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +0200682
Bertrand Jacquin874a35c2018-09-10 21:26:07 +0100683 Here, an example of service registration. The service just send an 'Hello world'
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +0200684 as an http response.
685
686.. code-block:: lua
687
Pieter Baauw4d7f7662015-11-08 16:38:08 +0100688 core.register_service("hello-world", "http", function(applet)
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +0200689 local response = "Hello World !"
690 applet:set_status(200)
Pieter Baauw2dcb9bc2015-10-01 22:47:12 +0200691 applet:add_header("content-length", string.len(response))
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +0200692 applet:add_header("content-type", "text/plain")
Pieter Baauw2dcb9bc2015-10-01 22:47:12 +0200693 applet:start_response()
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +0200694 applet:send(response)
695 end)
696..
697
Willy Tarreau714f3452021-05-09 06:47:26 +0200698 This example code is used in HAProxy configuration like this:
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +0200699
700::
701
702 frontend example
703 http-request use-service lua.hello-world
704
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100705.. js:function:: core.register_init(func)
706
707 **context**: body
708
709 Register a function executed after the configuration parsing. This is useful
710 to check any parameters.
711
Pieter Baauw4d7f7662015-11-08 16:38:08 +0100712 :param function func: is the Lua function called to work as initializer.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100713
714 The prototype of the Lua function used as argument is:
715
716.. code-block:: lua
717
718 function()
719..
720
721 It takes no input, and no output is expected.
722
723.. js:function:: core.register_task(func)
724
725 **context**: body, init, task, action, sample-fetch, converter
726
727 Register and start independent task. The task is started when the HAProxy
728 main scheduler starts. For example this type of tasks can be executed to
Thierry FOURNIER2e4893c2015-03-18 13:37:27 +0100729 perform complex health checks.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100730
Pieter Baauw4d7f7662015-11-08 16:38:08 +0100731 :param function func: is the Lua function called to work as initializer.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100732
733 The prototype of the Lua function used as argument is:
734
735.. code-block:: lua
736
737 function()
738..
739
740 It takes no input, and no output is expected.
741
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +0100742.. js:function:: core.register_cli([path], usage, func)
743
744 **context**: body
745
Aurelien DARRAGON53901f42022-10-13 19:49:42 +0200746 Register a custom cli that will be available from haproxy stats socket.
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +0100747
748 :param array path: is the sequence of word for which the cli execute the Lua
749 binding.
750 :param string usage: is the usage message displayed in the help.
751 :param function func: is the Lua function called to handle the CLI commands.
752
753 The prototype of the Lua function used as argument is:
754
755.. code-block:: lua
756
757 function(AppletTCP, [arg1, [arg2, [...]]])
758..
759
760 I/O are managed with the :ref:`applettcp_class` object. Args are given as
Bertrand Jacquin874a35c2018-09-10 21:26:07 +0100761 parameter. The args embed the registered path. If the path is declared like
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +0100762 this:
763
764.. code-block:: lua
765
766 core.register_cli({"show", "ssl", "stats"}, "Display SSL stats..", function(applet, arg1, arg2, arg3, arg4, arg5)
767 end)
768..
769
770 And we execute this in the prompt:
771
772.. code-block:: text
773
774 > prompt
775 > show ssl stats all
776..
777
Bertrand Jacquin874a35c2018-09-10 21:26:07 +0100778 Then, arg1, arg2 and arg3 will contains respectively "show", "ssl" and "stats".
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +0100779 arg4 will contain "all". arg5 contains nil.
780
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100781.. js:function:: core.set_nice(nice)
782
783 **context**: task, action, sample-fetch, converter
784
785 Change the nice of the current task or current session.
Thierry FOURNIER2e4893c2015-03-18 13:37:27 +0100786
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100787 :param integer nice: the nice value, it must be between -1024 and 1024.
788
789.. js:function:: core.set_map(filename, key, value)
790
791 **context**: init, task, action, sample-fetch, converter
792
Bertrand Jacquin874a35c2018-09-10 21:26:07 +0100793 Set the value *value* associated to the key *key* in the map referenced by
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100794 *filename*.
795
Thierry FOURNIERdc595002015-12-21 11:13:52 +0100796 :param string filename: the Map reference
797 :param string key: the key to set or replace
798 :param string value: the associated value
799
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100800.. js:function:: core.sleep(int seconds)
801
802 **context**: body, init, task, action
803
804 The `core.sleep()` functions stop the Lua execution between specified seconds.
805
806 :param integer seconds: the required seconds.
807
808.. js:function:: core.tcp()
809
810 **context**: init, task, action
811
812 This function returns a new object of a *socket* class.
813
Thierry FOURNIERdc595002015-12-21 11:13:52 +0100814 :returns: A :ref:`socket_class` object.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100815
William Lallemand00a15022021-11-19 16:02:44 +0100816.. js:function:: core.httpclient()
817
818 **context**: init, task, action
819
820 This function returns a new object of a *httpclient* class.
821
822 :returns: A :ref:`httpclient_class` object.
823
Thierry Fournier1de16592016-01-27 09:49:07 +0100824.. js:function:: core.concat()
825
826 **context**: body, init, task, action, sample-fetch, converter
827
Bertrand Jacquin874a35c2018-09-10 21:26:07 +0100828 This function returns a new concat object.
Thierry Fournier1de16592016-01-27 09:49:07 +0100829
830 :returns: A :ref:`concat_class` object.
831
Thierry FOURNIER0a99b892015-08-26 00:14:17 +0200832.. js:function:: core.done(data)
833
834 **context**: body, init, task, action, sample-fetch, converter
835
836 :param any data: Return some data for the caller. It is useful with
837 sample-fetches and sample-converters.
838
839 Immediately stops the current Lua execution and returns to the caller which
840 may be a sample fetch, a converter or an action and returns the specified
Thierry Fournier4234dbd2020-11-28 13:18:23 +0100841 value (ignored for actions and init). It is used when the LUA process finishes
842 its work and wants to give back the control to HAProxy without executing the
Thierry FOURNIER0a99b892015-08-26 00:14:17 +0200843 remaining code. It can be seen as a multi-level "return".
844
Thierry FOURNIER486f5a02015-03-16 15:13:03 +0100845.. js:function:: core.yield()
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100846
847 **context**: task, action, sample-fetch, converter
848
849 Give back the hand at the HAProxy scheduler. It is used when the LUA
850 processing consumes a lot of processing time.
851
Thierry FOURNIER / OZON.IO62fec752016-11-10 20:38:11 +0100852.. js:function:: core.parse_addr(address)
853
854 **context**: body, init, task, action, sample-fetch, converter
855
856 :param network: is a string describing an ipv4 or ipv6 address and optionally
857 its network length, like this: "127.0.0.1/8" or "aaaa::1234/32".
858 :returns: a userdata containing network or nil if an error occurs.
859
Bertrand Jacquin874a35c2018-09-10 21:26:07 +0100860 Parse ipv4 or ipv6 addresses and its facultative associated network.
Thierry FOURNIER / OZON.IO62fec752016-11-10 20:38:11 +0100861
862.. js:function:: core.match_addr(addr1, addr2)
863
864 **context**: body, init, task, action, sample-fetch, converter
865
866 :param addr1: is an address created with "core.parse_addr".
867 :param addr2: is an address created with "core.parse_addr".
Bertrand Jacquin874a35c2018-09-10 21:26:07 +0100868 :returns: boolean, true if the network of the addresses match, else returns
Thierry FOURNIER / OZON.IO62fec752016-11-10 20:38:11 +0100869 false.
870
Ilya Shipitsin2075ca82020-03-06 23:22:22 +0500871 Match two networks. For example "127.0.0.1/32" matches "127.0.0.0/8". The order
Thierry FOURNIER / OZON.IO62fec752016-11-10 20:38:11 +0100872 of network is not important.
873
Thierry FOURNIER / OZON.IO8a1027a2016-11-24 20:48:38 +0100874.. js:function:: core.tokenize(str, separators [, noblank])
875
876 **context**: body, init, task, action, sample-fetch, converter
877
878 This function is useful for tokenizing an entry, or splitting some messages.
879 :param string str: The string which will be split.
880 :param string separators: A string containing a list of separators.
881 :param boolean noblank: Ignore empty entries.
882 :returns: an array of string.
883
884 For example:
885
886.. code-block:: lua
887
888 local array = core.tokenize("This function is useful, for tokenizing an entry.", "., ", true)
889 print_r(array)
890..
891
892 Returns this array:
893
894.. code-block:: text
895
896 (table) table: 0x21c01e0 [
897 1: (string) "This"
898 2: (string) "function"
899 3: (string) "is"
900 4: (string) "useful"
901 5: (string) "for"
902 6: (string) "tokenizing"
903 7: (string) "an"
904 8: (string) "entry"
905 ]
906..
907
Thierry Fournierf61aa632016-02-19 20:56:00 +0100908.. _proxy_class:
909
910Proxy class
911============
912
913.. js:class:: Proxy
914
915 This class provides a way for manipulating proxy and retrieving information
916 like statistics.
917
Thierry FOURNIER817e7592017-07-24 14:35:04 +0200918.. js:attribute:: Proxy.name
919
920 Contain the name of the proxy.
921
Aurelien DARRAGONc4b24372023-03-02 12:00:06 +0100922 .. warning::
923 This attribute is now deprecated and will eventually be removed.
924 Please use :js:func:`Proxy.get_name()` function instead.
925
Thierry Fournierb0467732022-10-07 12:07:24 +0200926.. js:function:: Proxy.get_name()
927
928 Returns the name of the proxy.
929
Baptiste Assmann46c72552017-10-26 21:51:58 +0200930.. js:attribute:: Proxy.uuid
931
932 Contain the unique identifier of the proxy.
933
Aurelien DARRAGONc4b24372023-03-02 12:00:06 +0100934 .. warning::
935 This attribute is now deprecated and will eventually be removed.
936 Please use :js:func:`Proxy.get_uuid()` function instead.
937
Thierry Fournierb0467732022-10-07 12:07:24 +0200938.. js:function:: Proxy.get_uuid()
939
940 Returns the unique identifier of the proxy.
941
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +0100942.. js:attribute:: Proxy.servers
943
Patrick Hemmerc6a1d712018-05-01 21:30:41 -0400944 Contain a table with the attached servers. The table is indexed by server
945 name, and each server entry is an object of type :ref:`server_class`.
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +0100946
Adis Nezirovic8878f8e2018-07-13 12:18:33 +0200947.. js:attribute:: Proxy.stktable
948
949 Contains a stick table object attached to the proxy.
950
Thierry Fournierff480422016-02-25 08:36:46 +0100951.. js:attribute:: Proxy.listeners
952
Patrick Hemmerc6a1d712018-05-01 21:30:41 -0400953 Contain a table with the attached listeners. The table is indexed by listener
954 name, and each each listeners entry is an object of type
955 :ref:`listener_class`.
Thierry Fournierff480422016-02-25 08:36:46 +0100956
Thierry Fournierf61aa632016-02-19 20:56:00 +0100957.. js:function:: Proxy.pause(px)
958
959 Pause the proxy. See the management socket documentation for more information.
960
961 :param class_proxy px: A :ref:`proxy_class` which indicates the manipulated
962 proxy.
963
964.. js:function:: Proxy.resume(px)
965
966 Resume the proxy. See the management socket documentation for more
967 information.
968
969 :param class_proxy px: A :ref:`proxy_class` which indicates the manipulated
970 proxy.
971
972.. js:function:: Proxy.stop(px)
973
974 Stop the proxy. See the management socket documentation for more information.
975
976 :param class_proxy px: A :ref:`proxy_class` which indicates the manipulated
977 proxy.
978
979.. js:function:: Proxy.shut_bcksess(px)
980
981 Kill the session attached to a backup server. See the management socket
982 documentation for more information.
983
984 :param class_proxy px: A :ref:`proxy_class` which indicates the manipulated
985 proxy.
986
987.. js:function:: Proxy.get_cap(px)
988
989 Returns a string describing the capabilities of the proxy.
990
991 :param class_proxy px: A :ref:`proxy_class` which indicates the manipulated
992 proxy.
993 :returns: a string "frontend", "backend", "proxy" or "ruleset".
994
995.. js:function:: Proxy.get_mode(px)
996
997 Returns a string describing the mode of the current proxy.
998
999 :param class_proxy px: A :ref:`proxy_class` which indicates the manipulated
1000 proxy.
1001 :returns: a string "tcp", "http", "health" or "unknown"
1002
1003.. js:function:: Proxy.get_stats(px)
1004
Bertrand Jacquin874a35c2018-09-10 21:26:07 +01001005 Returns a table containing the proxy statistics. The statistics returned are
Thierry Fournierf61aa632016-02-19 20:56:00 +01001006 not the same if the proxy is frontend or a backend.
1007
1008 :param class_proxy px: A :ref:`proxy_class` which indicates the manipulated
1009 proxy.
Patrick Hemmerc6a1d712018-05-01 21:30:41 -04001010 :returns: a key/value table containing stats
Thierry Fournierf61aa632016-02-19 20:56:00 +01001011
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001012.. _server_class:
1013
1014Server class
1015============
1016
Patrick Hemmerc6a1d712018-05-01 21:30:41 -04001017.. js:class:: Server
1018
1019 This class provides a way for manipulating servers and retrieving information.
1020
Patrick Hemmera62ae7e2018-04-29 14:23:48 -04001021.. js:attribute:: Server.name
1022
1023 Contain the name of the server.
1024
Aurelien DARRAGONc4b24372023-03-02 12:00:06 +01001025 .. warning::
1026 This attribute is now deprecated and will eventually be removed.
1027 Please use :js:func:`Server.get_name()` function instead.
1028
Thierry Fournierb0467732022-10-07 12:07:24 +02001029.. js:function:: Server.get_name(sv)
1030
1031 Returns the name of the server.
1032
Patrick Hemmera62ae7e2018-04-29 14:23:48 -04001033.. js:attribute:: Server.puid
1034
1035 Contain the proxy unique identifier of the server.
1036
Aurelien DARRAGONc4b24372023-03-02 12:00:06 +01001037 .. warning::
1038 This attribute is now deprecated and will eventually be removed.
1039 Please use :js:func:`Server.get_puid()` function instead.
1040
Thierry Fournierb0467732022-10-07 12:07:24 +02001041.. js:function:: Server.get_puid(sv)
1042
1043 Returns the proxy unique identifier of the server.
1044
Aurelien DARRAGON94ee6632023-03-10 15:11:27 +01001045.. js:function:: Server.get_rid(sv)
1046
1047 Returns the rid (revision ID) of the server.
1048 It is an unsigned integer that is set upon server creation. Value is derived
1049 from a global counter that starts at 0 and is incremented each time one or
1050 multiple server deletions are followed by a server addition (meaning that
1051 old name/id reuse could occur).
1052
1053 Combining server name/id with server rid yields a process-wide unique
1054 identifier.
1055
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001056.. js:function:: Server.is_draining(sv)
1057
Patrick Hemmerc6a1d712018-05-01 21:30:41 -04001058 Return true if the server is currently draining sticky connections.
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001059
1060 :param class_server sv: A :ref:`server_class` which indicates the manipulated
1061 server.
1062 :returns: a boolean
1063
Patrick Hemmer32d539f2018-04-29 14:25:46 -04001064.. js:function:: Server.set_maxconn(sv, weight)
1065
1066 Dynamically change the maximum connections of the server. See the management
1067 socket documentation for more information about the format of the string.
1068
1069 :param class_server sv: A :ref:`server_class` which indicates the manipulated
1070 server.
1071 :param string maxconn: A string describing the server maximum connections.
1072
1073.. js:function:: Server.get_maxconn(sv, weight)
1074
1075 This function returns an integer representing the server maximum connections.
1076
1077 :param class_server sv: A :ref:`server_class` which indicates the manipulated
1078 server.
1079 :returns: an integer.
1080
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001081.. js:function:: Server.set_weight(sv, weight)
1082
Patrick Hemmerc6a1d712018-05-01 21:30:41 -04001083 Dynamically change the weight of the server. See the management socket
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001084 documentation for more information about the format of the string.
1085
1086 :param class_server sv: A :ref:`server_class` which indicates the manipulated
1087 server.
1088 :param string weight: A string describing the server weight.
1089
1090.. js:function:: Server.get_weight(sv)
1091
Patrick Hemmerc6a1d712018-05-01 21:30:41 -04001092 This function returns an integer representing the server weight.
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001093
1094 :param class_server sv: A :ref:`server_class` which indicates the manipulated
1095 server.
1096 :returns: an integer.
1097
Joseph C. Sible49bbf522020-05-04 22:20:32 -04001098.. js:function:: Server.set_addr(sv, addr[, port])
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001099
Patrick Hemmerc6a1d712018-05-01 21:30:41 -04001100 Dynamically change the address of the server. See the management socket
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001101 documentation for more information about the format of the string.
1102
1103 :param class_server sv: A :ref:`server_class` which indicates the manipulated
1104 server.
Patrick Hemmerc6a1d712018-05-01 21:30:41 -04001105 :param string addr: A string describing the server address.
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001106
1107.. js:function:: Server.get_addr(sv)
1108
Patrick Hemmerc6a1d712018-05-01 21:30:41 -04001109 Returns a string describing the address of the server.
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001110
1111 :param class_server sv: A :ref:`server_class` which indicates the manipulated
1112 server.
1113 :returns: A string
1114
1115.. js:function:: Server.get_stats(sv)
1116
1117 Returns server statistics.
1118
1119 :param class_server sv: A :ref:`server_class` which indicates the manipulated
1120 server.
Patrick Hemmerc6a1d712018-05-01 21:30:41 -04001121 :returns: a key/value table containing stats
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001122
1123.. js:function:: Server.shut_sess(sv)
1124
1125 Shutdown all the sessions attached to the server. See the management socket
1126 documentation for more information about this function.
1127
1128 :param class_server sv: A :ref:`server_class` which indicates the manipulated
1129 server.
1130
1131.. js:function:: Server.set_drain(sv)
1132
1133 Drain sticky sessions. See the management socket documentation for more
1134 information about this function.
1135
1136 :param class_server sv: A :ref:`server_class` which indicates the manipulated
1137 server.
1138
1139.. js:function:: Server.set_maint(sv)
1140
1141 Set maintenance mode. See the management socket documentation for more
1142 information about this function.
1143
1144 :param class_server sv: A :ref:`server_class` which indicates the manipulated
1145 server.
1146
1147.. js:function:: Server.set_ready(sv)
1148
1149 Set normal mode. See the management socket documentation for more information
1150 about this function.
1151
1152 :param class_server sv: A :ref:`server_class` which indicates the manipulated
1153 server.
1154
1155.. js:function:: Server.check_enable(sv)
1156
1157 Enable health checks. See the management socket documentation for more
1158 information about this function.
1159
1160 :param class_server sv: A :ref:`server_class` which indicates the manipulated
1161 server.
1162
1163.. js:function:: Server.check_disable(sv)
1164
1165 Disable health checks. See the management socket documentation for more
1166 information about this function.
1167
1168 :param class_server sv: A :ref:`server_class` which indicates the manipulated
1169 server.
1170
1171.. js:function:: Server.check_force_up(sv)
1172
1173 Force health-check up. See the management socket documentation for more
1174 information about this function.
1175
1176 :param class_server sv: A :ref:`server_class` which indicates the manipulated
1177 server.
1178
1179.. js:function:: Server.check_force_nolb(sv)
1180
1181 Force health-check nolb mode. See the management socket documentation for more
1182 information about this function.
1183
1184 :param class_server sv: A :ref:`server_class` which indicates the manipulated
1185 server.
1186
1187.. js:function:: Server.check_force_down(sv)
1188
1189 Force health-check down. See the management socket documentation for more
1190 information about this function.
1191
1192 :param class_server sv: A :ref:`server_class` which indicates the manipulated
1193 server.
1194
1195.. js:function:: Server.agent_enable(sv)
1196
1197 Enable agent check. See the management socket documentation for more
1198 information about this function.
1199
1200 :param class_server sv: A :ref:`server_class` which indicates the manipulated
1201 server.
1202
1203.. js:function:: Server.agent_disable(sv)
1204
1205 Disable agent check. See the management socket documentation for more
1206 information about this function.
1207
1208 :param class_server sv: A :ref:`server_class` which indicates the manipulated
1209 server.
1210
1211.. js:function:: Server.agent_force_up(sv)
1212
1213 Force agent check up. See the management socket documentation for more
1214 information about this function.
1215
1216 :param class_server sv: A :ref:`server_class` which indicates the manipulated
1217 server.
1218
1219.. js:function:: Server.agent_force_down(sv)
1220
1221 Force agent check down. See the management socket documentation for more
1222 information about this function.
1223
1224 :param class_server sv: A :ref:`server_class` which indicates the manipulated
1225 server.
1226
Thierry Fournierff480422016-02-25 08:36:46 +01001227.. _listener_class:
1228
1229Listener class
1230==============
1231
1232.. js:function:: Listener.get_stats(ls)
1233
1234 Returns server statistics.
1235
1236 :param class_listener ls: A :ref:`listener_class` which indicates the
1237 manipulated listener.
Patrick Hemmerc6a1d712018-05-01 21:30:41 -04001238 :returns: a key/value table containing stats
Thierry Fournierff480422016-02-25 08:36:46 +01001239
Thierry Fournier1de16592016-01-27 09:49:07 +01001240.. _concat_class:
1241
1242Concat class
1243============
1244
1245.. js:class:: Concat
1246
1247 This class provides a fast way for string concatenation. The way using native
1248 Lua concatenation like the code below is slow for some reasons.
1249
1250.. code-block:: lua
1251
1252 str = "string1"
1253 str = str .. ", string2"
1254 str = str .. ", string3"
1255..
1256
1257 For each concatenation, Lua:
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02001258 - allocates memory for the result,
1259 - catenates the two string copying the strings in the new memory block,
1260 - frees the old memory block containing the string which is no longer used.
1261
Thierry Fournier1de16592016-01-27 09:49:07 +01001262 This process does many memory move, allocation and free. In addition, the
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02001263 memory is not really freed, it is just marked as unused and waits for the
Thierry Fournier1de16592016-01-27 09:49:07 +01001264 garbage collector.
1265
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02001266 The Concat class provides an alternative way to concatenate strings. It uses
Thierry Fournier1de16592016-01-27 09:49:07 +01001267 the internal Lua mechanism (it does not allocate memory), but it doesn't copy
1268 the data more than once.
1269
1270 On my computer, the following loops spends 0.2s for the Concat method and
1271 18.5s for the pure Lua implementation. So, the Concat class is about 1000x
1272 faster than the embedded solution.
1273
1274.. code-block:: lua
1275
1276 for j = 1, 100 do
1277 c = core.concat()
1278 for i = 1, 20000 do
1279 c:add("#####")
1280 end
1281 end
1282..
1283
1284.. code-block:: lua
1285
1286 for j = 1, 100 do
1287 c = ""
1288 for i = 1, 20000 do
1289 c = c .. "#####"
1290 end
1291 end
1292..
1293
1294.. js:function:: Concat.add(concat, string)
1295
1296 This function adds a string to the current concatenated string.
1297
1298 :param class_concat concat: A :ref:`concat_class` which contains the currently
Ilya Shipitsin2075ca82020-03-06 23:22:22 +05001299 built string.
Bertrand Jacquin874a35c2018-09-10 21:26:07 +01001300 :param string string: A new string to concatenate to the current built
Thierry Fournier1de16592016-01-27 09:49:07 +01001301 string.
1302
1303.. js:function:: Concat.dump(concat)
1304
Bertrand Jacquin874a35c2018-09-10 21:26:07 +01001305 This function returns the concatenated string.
Thierry Fournier1de16592016-01-27 09:49:07 +01001306
1307 :param class_concat concat: A :ref:`concat_class` which contains the currently
Ilya Shipitsin2075ca82020-03-06 23:22:22 +05001308 built string.
Thierry Fournier1de16592016-01-27 09:49:07 +01001309 :returns: the concatenated string
1310
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001311.. _fetches_class:
1312
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001313Fetches class
1314=============
1315
1316.. js:class:: Fetches
1317
1318 This class contains a lot of internal HAProxy sample fetches. See the
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02001319 HAProxy "configuration.txt" documentation for more information.
1320 (chapters 7.3.2 to 7.3.6)
Thierry FOURNIER2e4893c2015-03-18 13:37:27 +01001321
Christopher Faulet1e9b1b62021-08-11 10:14:30 +02001322 .. warning::
1323 some sample fetches are not available in some context. These limitations
1324 are specified in this documentation when they're useful.
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001325
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01001326 :see: :js:attr:`TXN.f`
1327 :see: :js:attr:`TXN.sf`
Thierry FOURNIER2e4893c2015-03-18 13:37:27 +01001328
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02001329 Fetches are useful to:
Thierry FOURNIER2e4893c2015-03-18 13:37:27 +01001330
1331 * get system time,
1332 * get environment variable,
1333 * get random numbers,
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02001334 * know backend status like the number of users in queue or the number of
Thierry FOURNIER2e4893c2015-03-18 13:37:27 +01001335 connections established,
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02001336 * get client information like ip source or destination,
Thierry FOURNIER2e4893c2015-03-18 13:37:27 +01001337 * deal with stick tables,
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02001338 * fetch established SSL information,
1339 * fetch HTTP information like headers or method.
Thierry FOURNIER2e4893c2015-03-18 13:37:27 +01001340
1341.. code-block:: lua
1342
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001343 function action(txn)
1344 -- Get source IP
1345 local clientip = txn.f:src()
1346 end
Thierry FOURNIER2e4893c2015-03-18 13:37:27 +01001347..
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001348
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001349.. _converters_class:
1350
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001351Converters class
1352================
1353
1354.. js:class:: Converters
1355
1356 This class contains a lot of internal HAProxy sample converters. See the
Thierry FOURNIER2e4893c2015-03-18 13:37:27 +01001357 HAProxy documentation "configuration.txt" for more information about her
1358 usage. Its the chapter 7.3.1.
1359
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01001360 :see: :js:attr:`TXN.c`
1361 :see: :js:attr:`TXN.sc`
Thierry FOURNIER2e4893c2015-03-18 13:37:27 +01001362
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02001363 Converters provides stateful transformation. They are useful to:
Thierry FOURNIER2e4893c2015-03-18 13:37:27 +01001364
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02001365 * convert input to base64,
1366 * apply hash on input string (djb2, crc32, sdbm, wt6),
Thierry FOURNIER2e4893c2015-03-18 13:37:27 +01001367 * format date,
1368 * json escape,
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02001369 * extract preferred language comparing two lists,
Thierry FOURNIER2e4893c2015-03-18 13:37:27 +01001370 * turn to lower or upper chars,
1371 * deal with stick tables.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001372
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001373.. _channel_class:
1374
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001375Channel class
1376=============
1377
1378.. js:class:: Channel
1379
Christopher Faulet5a2c6612021-08-15 20:35:25 +02001380 **context**: action, sample-fetch, convert, filter
1381
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001382 HAProxy uses two buffers for the processing of the requests. The first one is
1383 used with the request data (from the client to the server) and the second is
1384 used for the response data (from the server to the client).
1385
1386 Each buffer contains two types of data. The first type is the incoming data
1387 waiting for a processing. The second part is the outgoing data already
1388 processed. Usually, the incoming data is processed, after it is tagged as
1389 outgoing data, and finally it is sent. The following functions provides tools
1390 for manipulating these data in a buffer.
1391
1392 The following diagram shows where the channel class function are applied.
1393
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001394 .. image:: _static/channel.png
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001395
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001396 .. warning::
1397 It is not possible to read from the response in request action, and it is
Boyang Li60cfe8b2022-05-10 18:11:00 +00001398 not possible to read from the request channel in response action.
Christopher Faulet09530392021-06-14 11:43:18 +02001399
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001400 .. warning::
1401 It is forbidden to alter the Channels buffer from HTTP contexts. So only
1402 :js:func:`Channel.input`, :js:func:`Channel.output`,
1403 :js:func:`Channel.may_recv`, :js:func:`Channel.is_full` and
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02001404 :js:func:`Channel.is_resp` can be called from a HTTP context.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001405
Christopher Faulet5a2c6612021-08-15 20:35:25 +02001406 All the functions provided by this class are available in the
1407 **sample-fetches**, **actions** and **filters** contexts. For **filters**,
1408 incoming data (offset and length) are relative to the filter. Some functions
Boyang Li60cfe8b2022-05-10 18:11:00 +00001409 may yield, but only for **actions**. Yield is not possible for
Christopher Faulet5a2c6612021-08-15 20:35:25 +02001410 **sample-fetches**, **converters** and **filters**.
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001411
1412.. js:function:: Channel.append(channel, string)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001413
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001414 This function copies the string **string** at the end of incoming data of the
1415 channel buffer. The function returns the copied length on success or -1 if
1416 data cannot be copied.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001417
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001418 Same that :js:func:`Channel.insert(channel, string, channel:input())`.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001419
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001420 :param class_channel channel: The manipulated Channel.
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02001421 :param string string: The data to copy at the end of incoming data.
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001422 :returns: an integer containing the amount of bytes copied or -1.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001423
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001424.. js:function:: Channel.data(channel [, offset [, length]])
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001425
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001426 This function returns **length** bytes of incoming data from the channel
1427 buffer, starting at the offset **offset**. The data are not removed from the
1428 buffer.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001429
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001430 By default, if no length is provided, all incoming data found, starting at the
1431 given offset, are returned. If **length** is set to -1, the function tries to
Christopher Faulet5a2c6612021-08-15 20:35:25 +02001432 retrieve a maximum of data and, if called by an action, it yields if
1433 necessary. It also waits for more data if the requested length exceeds the
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02001434 available amount of incoming data. Not providing an offset is the same as
Ilya Shipitsinff0f2782021-08-22 22:18:07 +05001435 setting it to 0. A positive offset is relative to the beginning of incoming
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02001436 data of the channel buffer while negative offset is relative to the end.
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001437
1438 If there is no incoming data and the channel can't receive more data, a 'nil'
1439 value is returned.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001440
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001441 :param class_channel channel: The manipulated Channel.
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001442 :param integer offset: *optional* The offset in incoming data to start to get
1443 data. 0 by default. May be negative to be relative to
1444 the end of incoming data.
1445 :param integer length: *optional* The expected length of data to retrieve. All
1446 incoming data by default. May be set to -1 to get a
1447 maximum of data.
1448 :returns: a string containing the data found or nil.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001449
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001450.. js:function:: Channel.forward(channel, length)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001451
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001452 This function forwards **length** bytes of data from the channel buffer. If
Christopher Faulet5a2c6612021-08-15 20:35:25 +02001453 the requested length exceeds the available amount of incoming data, and if
1454 called by an action, the function yields, waiting for more data to forward. It
1455 returns the amount of data forwarded.
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001456
1457 :param class_channel channel: The manipulated Channel.
1458 :param integer int: The amount of data to forward.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001459
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001460.. js:function:: Channel.input(channel)
1461
Christopher Faulet5a2c6612021-08-15 20:35:25 +02001462 This function returns the length of incoming data in the channel buffer. When
1463 called by a filter, this value is relative to the filter.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001464
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001465 :param class_channel channel: The manipulated Channel.
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001466 :returns: an integer containing the amount of available bytes.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001467
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001468.. js:function:: Channel.insert(channel, string [, offset])
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001469
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001470 This function copies the string **string** at the offset **offset** in
1471 incoming data of the channel buffer. The function returns the copied length on
1472 success or -1 if data cannot be copied.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001473
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001474 By default, if no offset is provided, the string is copied in front of
Ilya Shipitsinff0f2782021-08-22 22:18:07 +05001475 incoming data. A positive offset is relative to the beginning of incoming data
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001476 of the channel buffer while negative offset is relative to their end.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001477
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001478 :param class_channel channel: The manipulated Channel.
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02001479 :param string string: The data to copy into incoming data.
1480 :param integer offset: *optional* The offset in incoming data where to copy
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001481 data. 0 by default. May be negative to be relative to
1482 the end of incoming data.
Pieter Baauw4d7f7662015-11-08 16:38:08 +01001483 :returns: an integer containing the amount of bytes copied or -1.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001484
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001485.. js:function:: Channel.is_full(channel)
1486
1487 This function returns true if the channel buffer is full.
1488
Christopher Faulet5a2c6612021-08-15 20:35:25 +02001489 :param class_channel channel: The manipulated Channel.
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001490 :returns: a boolean
1491
1492.. js:function:: Channel.is_resp(channel)
1493
1494 This function returns true if the channel is the response one.
1495
Christopher Faulet5a2c6612021-08-15 20:35:25 +02001496 :param class_channel channel: The manipulated Channel.
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001497 :returns: a boolean
1498
1499.. js:function:: Channel.line(channel [, offset [, length]])
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001500
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001501 This function parses **length** bytes of incoming data of the channel buffer,
1502 starting at offset **offset**, and returns the first line found, including the
Christopher Faulet5a2c6612021-08-15 20:35:25 +02001503 '\\n'. The data are not removed from the buffer. If no line is found, all data
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001504 are returned.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001505
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001506 By default, if no length is provided, all incoming data, starting at the given
1507 offset, are evaluated. If **length** is set to -1, the function tries to
Christopher Faulet5a2c6612021-08-15 20:35:25 +02001508 retrieve a maximum of data and, if called by an action, yields if
1509 necessary. It also waits for more data if the requested length exceeds the
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02001510 available amount of incoming data. Not providing an offset is the same as
Ilya Shipitsinff0f2782021-08-22 22:18:07 +05001511 setting it to 0. A positive offset is relative to the beginning of incoming
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02001512 data of the channel buffer while negative offset is relative to the end.
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001513
1514 If there is no incoming data and the channel can't receive more data, a 'nil'
1515 value is returned.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001516
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001517 :param class_channel channel: The manipulated Channel.
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02001518 :param integer offset: *optional* The offset in incoming data to start to
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001519 parse data. 0 by default. May be negative to be
1520 relative to the end of incoming data.
1521 :param integer length: *optional* The length of data to parse. All incoming
1522 data by default. May be set to -1 to get a maximum of
1523 data.
1524 :returns: a string containing the line found or nil.
1525
1526.. js:function:: Channel.may_recv(channel)
1527
1528 This function returns true if the channel may still receive data.
1529
Christopher Faulet5a2c6612021-08-15 20:35:25 +02001530 :param class_channel channel: The manipulated Channel.
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001531 :returns: a boolean
1532
1533.. js:function:: Channel.output(channel)
1534
Christopher Faulet5a2c6612021-08-15 20:35:25 +02001535 This function returns the length of outgoing data of the channel buffer. When
1536 called by a filter, this value is relative to the filter.
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001537
1538 :param class_channel channel: The manipulated Channel.
1539 :returns: an integer containing the amount of available bytes.
1540
1541.. js:function:: Channel.prepend(channel, string)
1542
1543 This function copies the string **string** in front of incoming data of the
1544 channel buffer. The function returns the copied length on success or -1 if
1545 data cannot be copied.
1546
1547 Same that :js:func:`Channel.insert(channel, string, 0)`.
1548
1549 :param class_channel channel: The manipulated Channel.
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02001550 :param string string: The data to copy in front of incoming data.
Pieter Baauw4d7f7662015-11-08 16:38:08 +01001551 :returns: an integer containing the amount of bytes copied or -1.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001552
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001553.. js:function:: Channel.remove(channel [, offset [, length]])
1554
1555 This function removes **length** bytes of incoming data of the channel buffer,
1556 starting at offset **offset**. This function returns number of bytes removed
1557 on success.
1558
1559 By default, if no length is provided, all incoming data, starting at the given
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02001560 offset, are removed. Not providing an offset is the same as setting it
Ilya Shipitsinff0f2782021-08-22 22:18:07 +05001561 to 0. A positive offset is relative to the beginning of incoming data of the
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02001562 channel buffer while negative offset is relative to the end.
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001563
1564 :param class_channel channel: The manipulated Channel.
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02001565 :param integer offset: *optional* The offset in incoming data where to start
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001566 to remove data. 0 by default. May be negative to
1567 be relative to the end of incoming data.
1568 :param integer length: *optional* The length of data to remove. All incoming
1569 data by default.
1570 :returns: an integer containing the amount of bytes removed.
1571
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001572.. js:function:: Channel.send(channel, string)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001573
Christopher Faulet5a2c6612021-08-15 20:35:25 +02001574 This function requires immediate send of the string **string**. It means the
Ilya Shipitsinff0f2782021-08-22 22:18:07 +05001575 string is copied at the beginning of incoming data of the channel buffer and
Christopher Faulet5a2c6612021-08-15 20:35:25 +02001576 immediately forwarded. Unless if the connection is close, and if called by an
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02001577 action, this function yields to copy and forward all the string.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001578
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001579 :param class_channel channel: The manipulated Channel.
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001580 :param string string: The data to send.
Pieter Baauw4d7f7662015-11-08 16:38:08 +01001581 :returns: an integer containing the amount of bytes copied or -1.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001582
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001583.. js:function:: Channel.set(channel, string [, offset [, length]])
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001584
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02001585 This function replaces **length** bytes of incoming data of the channel buffer,
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001586 starting at offset **offset**, by the string **string**. The function returns
1587 the copied length on success or -1 if data cannot be copied.
1588
1589 By default, if no length is provided, all incoming data, starting at the given
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02001590 offset, are replaced. Not providing an offset is the same as setting it
Ilya Shipitsinff0f2782021-08-22 22:18:07 +05001591 to 0. A positive offset is relative to the beginning of incoming data of the
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02001592 channel buffer while negative offset is relative to the end.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001593
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001594 :param class_channel channel: The manipulated Channel.
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02001595 :param string string: The data to copy into incoming data.
1596 :param integer offset: *optional* The offset in incoming data where to start
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001597 the data replacement. 0 by default. May be negative to
1598 be relative to the end of incoming data.
1599 :param integer length: *optional* The length of data to replace. All incoming
1600 data by default.
1601 :returns: an integer containing the amount of bytes copied or -1.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001602
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001603.. js:function:: Channel.dup(channel)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001604
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001605 **DEPRECATED**
1606
1607 This function returns all incoming data found in the channel buffer. The data
Boyang Li60cfe8b2022-05-10 18:11:00 +00001608 are not removed from the buffer and can be reprocessed later.
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001609
1610 If there is no incoming data and the channel can't receive more data, a 'nil'
1611 value is returned.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001612
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001613 :param class_channel channel: The manipulated Channel.
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001614 :returns: a string containing all data found or nil.
1615
1616 .. warning::
1617 This function is deprecated. :js:func:`Channel.data()` must be used
1618 instead.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001619
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001620.. js:function:: Channel.get(channel)
1621
1622 **DEPRECATED**
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001623
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001624 This function returns all incoming data found in the channel buffer and remove
1625 them from the buffer.
1626
1627 If there is no incoming data and the channel can't receive more data, a 'nil'
1628 value is returned.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001629
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001630 :param class_channel channel: The manipulated Channel.
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001631 :returns: a string containing all the data found or nil.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001632
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001633 .. warning::
1634 This function is deprecated. :js:func:`Channel.data()` must be used to
1635 retrieve data followed by a call to :js:func:`Channel:remove()` to remove
1636 data.
Thierry FOURNIER / OZON.IO65192f32016-11-07 15:28:40 +01001637
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001638 .. code-block:: lua
Thierry FOURNIER / OZON.IO65192f32016-11-07 15:28:40 +01001639
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001640 local data = chn:data()
1641 chn:remove(0, data:len())
1642
1643 ..
1644
1645.. js:function:: Channel.getline(channel)
1646
1647 **DEPRECATED**
1648
1649 This function returns the first line found in incoming data of the channel
Christopher Faulet5a2c6612021-08-15 20:35:25 +02001650 buffer, including the '\\n'. The returned data are removed from the buffer. If
1651 no line is found, and if called by an action, this function yields to wait for
1652 more data, except if the channel can't receive more data. In this case all
1653 data are returned.
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001654
1655 If there is no incoming data and the channel can't receive more data, a 'nil'
1656 value is returned.
1657
1658 :param class_channel channel: The manipulated Channel.
1659 :returns: a string containing the line found or nil.
1660
1661 .. warning::
Boyang Li60cfe8b2022-05-10 18:11:00 +00001662 This function is deprecated. :js:func:`Channel.line()` must be used to
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001663 retrieve a line followed by a call to :js:func:`Channel:remove()` to remove
1664 data.
1665
1666 .. code-block:: lua
1667
1668 local line = chn:line(0, -1)
1669 chn:remove(0, line:len())
1670
1671 ..
1672
1673.. js:function:: Channel.get_in_len(channel)
1674
Boyang Li60cfe8b2022-05-10 18:11:00 +00001675 **DEPRECATED**
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001676
Christopher Faulet5a2c6612021-08-15 20:35:25 +02001677 This function returns the length of the input part of the buffer. When called
1678 by a filter, this value is relative to the filter.
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001679
1680 :param class_channel channel: The manipulated Channel.
1681 :returns: an integer containing the amount of available bytes.
1682
1683 .. warning::
1684 This function is deprecated. :js:func:`Channel.input()` must be used
1685 instead.
1686
1687.. js:function:: Channel.get_out_len(channel)
1688
Boyang Li60cfe8b2022-05-10 18:11:00 +00001689 **DEPRECATED**
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001690
Christopher Faulet5a2c6612021-08-15 20:35:25 +02001691 This function returns the length of the output part of the buffer. When called
1692 by a filter, this value is relative to the filter.
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001693
1694 :param class_channel channel: The manipulated Channel.
1695 :returns: an integer containing the amount of available bytes.
1696
1697 .. warning::
1698 This function is deprecated. :js:func:`Channel.output()` must be used
1699 instead.
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001700
1701.. _http_class:
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001702
1703HTTP class
1704==========
1705
1706.. js:class:: HTTP
1707
1708 This class contain all the HTTP manipulation functions.
1709
Pieter Baauw386a1272015-08-16 15:26:24 +02001710.. js:function:: HTTP.req_get_headers(http)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001711
Patrick Hemmerc6a1d712018-05-01 21:30:41 -04001712 Returns a table containing all the request headers.
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001713
1714 :param class_http http: The related http object.
Patrick Hemmerc6a1d712018-05-01 21:30:41 -04001715 :returns: table of headers.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01001716 :see: :js:func:`HTTP.res_get_headers`
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001717
Patrick Hemmerc6a1d712018-05-01 21:30:41 -04001718 This is the form of the returned table:
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001719
1720.. code-block:: lua
1721
1722 HTTP:req_get_headers()['<header-name>'][<header-index>] = "<header-value>"
1723
1724 local hdr = HTTP:req_get_headers()
1725 hdr["host"][0] = "www.test.com"
1726 hdr["accept"][0] = "audio/basic q=1"
1727 hdr["accept"][1] = "audio/*, q=0.2"
1728 hdr["accept"][2] = "*/*, q=0.1"
1729..
1730
Pieter Baauw386a1272015-08-16 15:26:24 +02001731.. js:function:: HTTP.res_get_headers(http)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001732
Patrick Hemmerc6a1d712018-05-01 21:30:41 -04001733 Returns a table containing all the response headers.
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001734
1735 :param class_http http: The related http object.
Patrick Hemmerc6a1d712018-05-01 21:30:41 -04001736 :returns: table of headers.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01001737 :see: :js:func:`HTTP.req_get_headers`
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001738
Patrick Hemmerc6a1d712018-05-01 21:30:41 -04001739 This is the form of the returned table:
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001740
1741.. code-block:: lua
1742
1743 HTTP:res_get_headers()['<header-name>'][<header-index>] = "<header-value>"
1744
1745 local hdr = HTTP:req_get_headers()
1746 hdr["host"][0] = "www.test.com"
1747 hdr["accept"][0] = "audio/basic q=1"
1748 hdr["accept"][1] = "audio/*, q=0.2"
1749 hdr["accept"][2] = "*.*, q=0.1"
1750..
1751
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001752.. js:function:: HTTP.req_add_header(http, name, value)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001753
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02001754 Appends a HTTP header field in the request whose name is
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001755 specified in "name" and whose value is defined in "value".
1756
1757 :param class_http http: The related http object.
1758 :param string name: The header name.
1759 :param string value: The header value.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01001760 :see: :js:func:`HTTP.res_add_header`
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001761
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001762.. js:function:: HTTP.res_add_header(http, name, value)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001763
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02001764 Appends a HTTP header field in the response whose name is
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001765 specified in "name" and whose value is defined in "value".
1766
1767 :param class_http http: The related http object.
1768 :param string name: The header name.
1769 :param string value: The header value.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01001770 :see: :js:func:`HTTP.req_add_header`
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001771
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001772.. js:function:: HTTP.req_del_header(http, name)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001773
1774 Removes all HTTP header fields in the request whose name is
1775 specified in "name".
1776
1777 :param class_http http: The related http object.
1778 :param string name: The header name.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01001779 :see: :js:func:`HTTP.res_del_header`
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001780
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001781.. js:function:: HTTP.res_del_header(http, name)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001782
1783 Removes all HTTP header fields in the response whose name is
1784 specified in "name".
1785
1786 :param class_http http: The related http object.
1787 :param string name: The header name.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01001788 :see: :js:func:`HTTP.req_del_header`
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001789
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001790.. js:function:: HTTP.req_set_header(http, name, value)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001791
Bertrand Jacquin874a35c2018-09-10 21:26:07 +01001792 This variable replace all occurrence of all header "name", by only
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001793 one containing the "value".
1794
1795 :param class_http http: The related http object.
1796 :param string name: The header name.
1797 :param string value: The header value.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01001798 :see: :js:func:`HTTP.res_set_header`
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001799
Bertrand Jacquin874a35c2018-09-10 21:26:07 +01001800 This function does the same work as the following code:
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001801
1802.. code-block:: lua
1803
1804 function fcn(txn)
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001805 TXN.http:req_del_header("header")
1806 TXN.http:req_add_header("header", "value")
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001807 end
1808..
1809
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001810.. js:function:: HTTP.res_set_header(http, name, value)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001811
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02001812 This function replaces all occurrence of all header "name", by only
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001813 one containing the "value".
1814
1815 :param class_http http: The related http object.
1816 :param string name: The header name.
1817 :param string value: The header value.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01001818 :see: :js:func:`HTTP.req_rep_header()`
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001819
Pieter Baauw386a1272015-08-16 15:26:24 +02001820.. js:function:: HTTP.req_rep_header(http, name, regex, replace)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001821
1822 Matches the regular expression in all occurrences of header field "name"
1823 according to "regex", and replaces them with the "replace" argument. The
1824 replacement value can contain back references like \1, \2, ... This
1825 function works with the request.
1826
1827 :param class_http http: The related http object.
1828 :param string name: The header name.
1829 :param string regex: The match regular expression.
1830 :param string replace: The replacement value.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01001831 :see: :js:func:`HTTP.res_rep_header()`
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001832
Pieter Baauw386a1272015-08-16 15:26:24 +02001833.. js:function:: HTTP.res_rep_header(http, name, regex, string)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001834
1835 Matches the regular expression in all occurrences of header field "name"
1836 according to "regex", and replaces them with the "replace" argument. The
1837 replacement value can contain back references like \1, \2, ... This
1838 function works with the request.
1839
1840 :param class_http http: The related http object.
1841 :param string name: The header name.
1842 :param string regex: The match regular expression.
1843 :param string replace: The replacement value.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01001844 :see: :js:func:`HTTP.req_rep_header()`
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001845
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001846.. js:function:: HTTP.req_set_method(http, method)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001847
1848 Rewrites the request method with the parameter "method".
1849
1850 :param class_http http: The related http object.
1851 :param string method: The new method.
1852
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001853.. js:function:: HTTP.req_set_path(http, path)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001854
1855 Rewrites the request path with the "path" parameter.
1856
1857 :param class_http http: The related http object.
1858 :param string path: The new path.
1859
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001860.. js:function:: HTTP.req_set_query(http, query)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001861
1862 Rewrites the request's query string which appears after the first question
1863 mark ("?") with the parameter "query".
1864
1865 :param class_http http: The related http object.
1866 :param string query: The new query.
1867
Thierry FOURNIER0d79cf62015-08-26 14:20:58 +02001868.. js:function:: HTTP.req_set_uri(http, uri)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001869
1870 Rewrites the request URI with the parameter "uri".
1871
1872 :param class_http http: The related http object.
1873 :param string uri: The new uri.
1874
Robin H. Johnson52f5db22017-01-01 13:10:52 -08001875.. js:function:: HTTP.res_set_status(http, status [, reason])
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +02001876
Robin H. Johnson52f5db22017-01-01 13:10:52 -08001877 Rewrites the response status code with the parameter "code".
1878
1879 If no custom reason is provided, it will be generated from the status.
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +02001880
1881 :param class_http http: The related http object.
1882 :param integer status: The new response status code.
Robin H. Johnson52f5db22017-01-01 13:10:52 -08001883 :param string reason: The new response reason (optional).
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +02001884
William Lallemand00a15022021-11-19 16:02:44 +01001885.. _httpclient_class:
1886
1887HTTPClient class
1888================
1889
1890.. js:class:: HTTPClient
1891
1892 The httpclient class allows issue of outbound HTTP requests through a simple
1893 API without the knowledge of HAProxy internals.
1894
1895.. js:function:: HTTPClient.get(httpclient, request)
1896.. js:function:: HTTPClient.head(httpclient, request)
1897.. js:function:: HTTPClient.put(httpclient, request)
1898.. js:function:: HTTPClient.post(httpclient, request)
1899.. js:function:: HTTPClient.delete(httpclient, request)
1900
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02001901 Send a HTTP request and wait for a response. GET, HEAD PUT, POST and DELETE methods can be used.
1902 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 +01001903
William Lallemanda9256192022-10-21 11:48:24 +02001904 The HTTPClient interface is not able to decompress responses, it is not
1905 recommended to send an Accept-Encoding in the request so the response is
1906 received uncompressed.
William Lallemand00a15022021-11-19 16:02:44 +01001907
1908 :param class httpclient: Is the manipulated HTTPClient.
1909 :param table request: Is a table containing the parameters of the request that will be send.
1910 :param string request.url: Is a mandatory parameter for the request that contains the URL.
1911 :param string request.body: Is an optional parameter for the request that contains the body to send.
1912 :param table request.headers: Is an optional parameter for the request that contains the headers to send.
William Lallemand18340302022-02-23 15:57:45 +01001913 :param string request.dst: Is an optional parameter for the destination in haproxy address format.
William Lallemandb4a4ef62022-02-23 14:18:16 +01001914 :param integer request.timeout: Optional timeout parameter, set a "timeout server" on the connections.
William Lallemand00a15022021-11-19 16:02:44 +01001915 :returns: Lua table containing the response
1916
1917
1918.. code-block:: lua
1919
1920 local httpclient = core.httpclient()
William Lallemand4f4f2b72022-02-17 20:00:23 +01001921 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 +01001922
1923..
1924
1925.. code-block:: lua
1926
1927 response = {
1928 status = 400,
1929 reason = "Bad request",
1930 headers = {
1931 ["content-type"] = { "text/html" },
1932 ["cache-control"] = { "no-cache", "no-store" },
1933 },
William Lallemand4f4f2b72022-02-17 20:00:23 +01001934 body = "<html><body><h1>invalid request<h1></body></html>",
William Lallemand00a15022021-11-19 16:02:44 +01001935 }
1936..
1937
1938
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001939.. _txn_class:
1940
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001941TXN class
1942=========
1943
1944.. js:class:: TXN
1945
1946 The txn class contain all the functions relative to the http or tcp
1947 transaction (Note than a tcp stream is the same than a tcp transaction, but
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02001948 a HTTP transaction is not the same than a tcp stream).
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001949
1950 The usage of this class permits to retrieve data from the requests, alter it
1951 and forward it.
1952
1953 All the functions provided by this class are available in the context
Christopher Faulet5a2c6612021-08-15 20:35:25 +02001954 **sample-fetches**, **actions** and **filters**.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001955
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001956.. js:attribute:: TXN.c
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001957
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001958 :returns: An :ref:`converters_class`.
1959
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001960 This attribute contains a Converters class object.
1961
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001962.. js:attribute:: TXN.sc
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001963
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001964 :returns: An :ref:`converters_class`.
1965
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001966 This attribute contains a Converters class object. The functions of
1967 this object returns always a string.
1968
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001969.. js:attribute:: TXN.f
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001970
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001971 :returns: An :ref:`fetches_class`.
1972
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001973 This attribute contains a Fetches class object.
1974
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001975.. js:attribute:: TXN.sf
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001976
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001977 :returns: An :ref:`fetches_class`.
1978
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001979 This attribute contains a Fetches class object. The functions of
1980 this object returns always a string.
1981
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001982.. js:attribute:: TXN.req
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001983
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001984 :returns: An :ref:`channel_class`.
1985
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001986 This attribute contains a channel class object for the request buffer.
1987
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001988.. js:attribute:: TXN.res
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001989
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001990 :returns: An :ref:`channel_class`.
1991
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001992 This attribute contains a channel class object for the response buffer.
1993
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001994.. js:attribute:: TXN.http
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001995
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001996 :returns: An :ref:`http_class`.
1997
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02001998 This attribute contains a HTTP class object. It is available only if the
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001999 proxy has the "mode http" enabled.
2000
Christopher Faulet5a2c6612021-08-15 20:35:25 +02002001.. js:attribute:: TXN.http_req
2002
2003 :returns: An :ref:`httpmessage_class`.
2004
2005 This attribute contains the request HTTPMessage class object. It is available
2006 only if the proxy has the "mode http" enabled and only in the **filters**
2007 context.
2008
2009.. js:attribute:: TXN.http_res
2010
2011 :returns: An :ref:`httpmessage_class`.
2012
2013 This attribute contains the response HTTPMessage class object. It is available
2014 only if the proxy has the "mode http" enabled and only in the **filters**
2015 context.
2016
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01002017.. js:function:: TXN.log(TXN, loglevel, msg)
2018
2019 This function sends a log. The log is sent, according with the HAProxy
2020 configuration file, on the default syslog server if it is configured and on
2021 the stderr if it is allowed.
2022
2023 :param class_txn txn: The class txn object containing the data.
Bertrand Jacquin874a35c2018-09-10 21:26:07 +01002024 :param integer loglevel: Is the log level associated with the message. It is a
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01002025 number between 0 and 7.
2026 :param string msg: The log content.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01002027 :see: :js:attr:`core.emerg`, :js:attr:`core.alert`, :js:attr:`core.crit`,
2028 :js:attr:`core.err`, :js:attr:`core.warning`, :js:attr:`core.notice`,
2029 :js:attr:`core.info`, :js:attr:`core.debug` (log level definitions)
2030 :see: :js:func:`TXN.deflog`
2031 :see: :js:func:`TXN.Debug`
2032 :see: :js:func:`TXN.Info`
2033 :see: :js:func:`TXN.Warning`
2034 :see: :js:func:`TXN.Alert`
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01002035
2036.. js:function:: TXN.deflog(TXN, msg)
2037
Bertrand Jacquin874a35c2018-09-10 21:26:07 +01002038 Sends a log line with the default loglevel for the proxy associated with the
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01002039 transaction.
2040
2041 :param class_txn txn: The class txn object containing the data.
2042 :param string msg: The log content.
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02002043 :see: :js:func:`TXN.log`
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01002044
2045.. js:function:: TXN.Debug(txn, msg)
2046
2047 :param class_txn txn: The class txn object containing the data.
2048 :param string msg: The log content.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01002049 :see: :js:func:`TXN.log`
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01002050
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02002051 Does the same job as:
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01002052
2053.. code-block:: lua
2054
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002055 function Debug(txn, msg)
2056 TXN.log(txn, core.debug, msg)
2057 end
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01002058..
2059
2060.. js:function:: TXN.Info(txn, msg)
2061
2062 :param class_txn txn: The class txn object containing the data.
2063 :param string msg: The log content.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01002064 :see: :js:func:`TXN.log`
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01002065
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02002066 Does the same job as:
2067
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01002068.. code-block:: lua
2069
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02002070 function Info(txn, msg)
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002071 TXN.log(txn, core.info, msg)
2072 end
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01002073..
2074
2075.. js:function:: TXN.Warning(txn, msg)
2076
2077 :param class_txn txn: The class txn object containing the data.
2078 :param string msg: The log content.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01002079 :see: :js:func:`TXN.log`
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01002080
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02002081 Does the same job as:
2082
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01002083.. code-block:: lua
2084
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02002085 function Warning(txn, msg)
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002086 TXN.log(txn, core.warning, msg)
2087 end
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01002088..
2089
2090.. js:function:: TXN.Alert(txn, msg)
2091
2092 :param class_txn txn: The class txn object containing the data.
2093 :param string msg: The log content.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01002094 :see: :js:func:`TXN.log`
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01002095
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02002096 Does the same job as:
2097
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01002098.. code-block:: lua
2099
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02002100 function Alert(txn, msg)
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002101 TXN.log(txn, core.alert, msg)
2102 end
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01002103..
2104
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002105.. js:function:: TXN.get_priv(txn)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002106
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002107 Return Lua data stored in the current transaction (with the `TXN.set_priv()`)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002108 function. If no data are stored, it returns a nil value.
2109
2110 :param class_txn txn: The class txn object containing the data.
Bertrand Jacquin874a35c2018-09-10 21:26:07 +01002111 :returns: the opaque data previously stored, or nil if nothing is
2112 available.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002113
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002114.. js:function:: TXN.set_priv(txn, data)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002115
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02002116 Store any data in the current HAProxy transaction. This action replaces the
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002117 old stored data.
2118
2119 :param class_txn txn: The class txn object containing the data.
2120 :param opaque data: The data which is stored in the transaction.
2121
Tim Duesterhus4e172c92020-05-19 13:49:42 +02002122.. js:function:: TXN.set_var(TXN, var, value[, ifexist])
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +02002123
David Carlier61fdf8b2015-10-02 11:59:38 +01002124 Converts a Lua type in a HAProxy type and store it in a variable <var>.
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +02002125
2126 :param class_txn txn: The class txn object containing the data.
2127 :param string var: The variable name according with the HAProxy variable syntax.
Thierry FOURNIER / OZON.IOb210bcc2016-12-12 16:24:16 +01002128 :param type value: The value associated to the variable. The type can be string or
2129 integer.
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02002130 :param boolean ifexist: If this parameter is set to true the variable
Tim Duesterhus4e172c92020-05-19 13:49:42 +02002131 will only be set if it was defined elsewhere (i.e. used
Willy Tarreau7978c5c2021-09-07 14:24:07 +02002132 within the configuration). For global variables (using the
2133 "proc" scope), they will only be updated and never created.
2134 It is highly recommended to always set this to true.
Christopher Faulet85d79c92016-11-09 16:54:56 +01002135
2136.. js:function:: TXN.unset_var(TXN, var)
2137
2138 Unset the variable <var>.
2139
2140 :param class_txn txn: The class txn object containing the data.
2141 :param string var: The variable name according with the HAProxy variable syntax.
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +02002142
2143.. js:function:: TXN.get_var(TXN, var)
2144
2145 Returns data stored in the variable <var> converter in Lua type.
2146
2147 :param class_txn txn: The class txn object containing the data.
2148 :param string var: The variable name according with the HAProxy variable syntax.
2149
Christopher Faulet700d9e82020-01-31 12:21:52 +01002150.. js:function:: TXN.reply([reply])
2151
2152 Return a new reply object
2153
2154 :param table reply: A table containing info to initialize the reply fields.
2155 :returns: A :ref:`reply_class` object.
2156
2157 The table used to initialized the reply object may contain following entries :
2158
2159 * status : The reply status code. the code 200 is used by default.
2160 * reason : The reply reason. The reason corresponding to the status code is
2161 used by default.
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02002162 * headers : A list of headers, indexed by header name. Empty by default. For
Christopher Faulet700d9e82020-01-31 12:21:52 +01002163 a given name, multiple values are possible, stored in an ordered list.
2164 * body : The reply body, empty by default.
2165
2166.. code-block:: lua
2167
2168 local reply = txn:reply{
2169 status = 400,
2170 reason = "Bad request",
2171 headers = {
2172 ["content-type"] = { "text/html" },
2173 ["cache-control"] = {"no-cache", "no-store" }
2174 },
2175 body = "<html><body><h1>invalid request<h1></body></html>"
2176 }
2177..
2178 :see: :js:class:`Reply`
2179
2180.. js:function:: TXN.done(txn[, reply])
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002181
Willy Tarreaubc183a62015-08-28 10:39:11 +02002182 This function terminates processing of the transaction and the associated
Christopher Faulet700d9e82020-01-31 12:21:52 +01002183 session and optionally reply to the client for HTTP sessions.
2184
2185 :param class_txn txn: The class txn object containing the data.
2186 :param class_reply reply: The class reply object to return to the client.
2187
2188 This functions can be used when a critical error is detected or to terminate
Willy Tarreaubc183a62015-08-28 10:39:11 +02002189 processing after some data have been returned to the client (eg: a redirect).
Christopher Faulet700d9e82020-01-31 12:21:52 +01002190 To do so, a reply may be provided. This object is optional and may contain a
2191 status code, a reason, a header list and a body. All these fields are
Christopher Faulet7855b192021-11-09 18:39:51 +01002192 optional. When not provided, the default values are used. By default, with an
2193 empty reply object, an empty HTTP 200 response is returned to the client. If
2194 no reply object is provided, the transaction is terminated without any
2195 reply. If a reply object is provided, it must not exceed the buffer size once
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02002196 converted into the internal HTTP representation. Because for now there is no
Christopher Faulet7855b192021-11-09 18:39:51 +01002197 easy way to be sure it fits, it is probably better to keep it reasonably
2198 small.
Christopher Faulet700d9e82020-01-31 12:21:52 +01002199
2200 The reply object may be fully created in lua or the class Reply may be used to
2201 create it.
2202
2203.. code-block:: lua
2204
2205 local reply = txn:reply()
2206 reply:set_status(400, "Bad request")
2207 reply:add_header("content-type", "text/html")
2208 reply:add_header("cache-control", "no-cache")
2209 reply:add_header("cache-control", "no-store")
2210 reply:set_body("<html><body><h1>invalid request<h1></body></html>")
2211 txn:done(reply)
2212..
2213
2214.. code-block:: lua
2215
2216 txn:done{
2217 status = 400,
2218 reason = "Bad request",
2219 headers = {
2220 ["content-type"] = { "text/html" },
2221 ["cache-control"] = { "no-cache", "no-store" },
2222 },
2223 body = "<html><body><h1>invalid request<h1></body></html>"
2224 }
2225..
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002226
Christopher Faulet1e9b1b62021-08-11 10:14:30 +02002227 .. warning::
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02002228 It does not make sense to call this function from sample-fetches. In this case
2229 the behavior is the same than core.done(): it finishes the Lua
Christopher Faulet1e9b1b62021-08-11 10:14:30 +02002230 execution. The transaction is really aborted only from an action registered
2231 function.
Thierry FOURNIERab00df62016-07-14 11:42:37 +02002232
Christopher Faulet700d9e82020-01-31 12:21:52 +01002233 :see: :js:func:`TXN.reply`, :js:class:`Reply`
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002234
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002235.. js:function:: TXN.set_loglevel(txn, loglevel)
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01002236
2237 Is used to change the log level of the current request. The "loglevel" must
2238 be an integer between 0 and 7.
2239
2240 :param class_txn txn: The class txn object containing the data.
2241 :param integer loglevel: The required log level. This variable can be one of
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01002242 :see: :js:attr:`core.emerg`, :js:attr:`core.alert`, :js:attr:`core.crit`,
2243 :js:attr:`core.err`, :js:attr:`core.warning`, :js:attr:`core.notice`,
2244 :js:attr:`core.info`, :js:attr:`core.debug` (log level definitions)
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01002245
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002246.. js:function:: TXN.set_tos(txn, tos)
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01002247
2248 Is used to set the TOS or DSCP field value of packets sent to the client to
2249 the value passed in "tos" on platforms which support this.
2250
2251 :param class_txn txn: The class txn object containing the data.
2252 :param integer tos: The new TOS os DSCP.
2253
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002254.. js:function:: TXN.set_mark(txn, mark)
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01002255
2256 Is used to set the Netfilter MARK on all packets sent to the client to the
2257 value passed in "mark" on platforms which support it.
2258
2259 :param class_txn txn: The class txn object containing the data.
2260 :param integer mark: The mark value.
2261
Patrick Hemmer268a7072018-05-11 12:52:31 -04002262.. js:function:: TXN.set_priority_class(txn, prio)
2263
2264 This function adjusts the priority class of the transaction. The value should
2265 be within the range -2047..2047. Values outside this range will be
2266 truncated.
2267
2268 See the HAProxy configuration.txt file keyword "http-request" action
2269 "set-priority-class" for details.
2270
2271.. js:function:: TXN.set_priority_offset(txn, prio)
2272
2273 This function adjusts the priority offset of the transaction. The value
2274 should be within the range -524287..524287. Values outside this range will be
2275 truncated.
2276
2277 See the HAProxy configuration.txt file keyword "http-request" action
2278 "set-priority-offset" for details.
2279
Christopher Faulet700d9e82020-01-31 12:21:52 +01002280.. _reply_class:
2281
2282Reply class
2283============
2284
2285.. js:class:: Reply
2286
2287 **context**: action
2288
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02002289 This class represents a HTTP response message. It provides some methods to
Christopher Faulet7855b192021-11-09 18:39:51 +01002290 enrich it. Once converted into the internal HTTP representation, the response
2291 message must not exceed the buffer size. Because for now there is no
2292 easy way to be sure it fits, it is probably better to keep it reasonably
2293 small.
2294
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02002295 See tune.bufsize in the configuration manual for details.
Christopher Faulet700d9e82020-01-31 12:21:52 +01002296
2297.. code-block:: lua
2298
2299 local reply = txn:reply({status = 400}) -- default HTTP 400 reason-phase used
2300 reply:add_header("content-type", "text/html")
2301 reply:add_header("cache-control", "no-cache")
2302 reply:add_header("cache-control", "no-store")
2303 reply:set_body("<html><body><h1>invalid request<h1></body></html>")
2304..
2305
2306 :see: :js:func:`TXN.reply`
2307
2308.. js:attribute:: Reply.status
2309
2310 The reply status code. By default, the status code is set to 200.
2311
2312 :returns: integer
2313
2314.. js:attribute:: Reply.reason
2315
2316 The reason string describing the status code.
2317
2318 :returns: string
2319
2320.. js:attribute:: Reply.headers
2321
2322 A table indexing all reply headers by name. To each name is associated an
2323 ordered list of values.
2324
2325 :returns: Lua table
2326
2327.. code-block:: lua
2328
2329 {
2330 ["content-type"] = { "text/html" },
2331 ["cache-control"] = {"no-cache", "no-store" },
2332 x_header_name = { "value1", "value2", ... }
2333 ...
2334 }
2335..
2336
2337.. js:attribute:: Reply.body
2338
2339 The reply payload.
2340
2341 :returns: string
2342
2343.. js:function:: Reply.set_status(REPLY, status[, reason])
2344
2345 Set the reply status code and optionally the reason-phrase. If the reason is
2346 not provided, the default reason corresponding to the status code is used.
2347
2348 :param class_reply reply: The related Reply object.
2349 :param integer status: The reply status code.
2350 :param string reason: The reply status reason (optional).
2351
2352.. js:function:: Reply.add_header(REPLY, name, value)
2353
2354 Add a header to the reply object. If the header does not already exist, a new
2355 entry is created with its name as index and a one-element list containing its
2356 value as value. Otherwise, the header value is appended to the ordered list of
2357 values associated to the header name.
2358
2359 :param class_reply reply: The related Reply object.
2360 :param string name: The header field name.
2361 :param string value: The header field value.
2362
2363.. js:function:: Reply.del_header(REPLY, name)
2364
2365 Remove all occurrences of a header name from the reply object.
2366
2367 :param class_reply reply: The related Reply object.
2368 :param string name: The header field name.
2369
2370.. js:function:: Reply.set_body(REPLY, body)
2371
2372 Set the reply payload.
2373
2374 :param class_reply reply: The related Reply object.
2375 :param string body: The reply payload.
2376
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002377.. _socket_class:
2378
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002379Socket class
2380============
2381
2382.. js:class:: Socket
2383
2384 This class must be compatible with the Lua Socket class. Only the 'client'
2385 functions are available. See the Lua Socket documentation:
2386
2387 `http://w3.impa.br/~diego/software/luasocket/tcp.html
2388 <http://w3.impa.br/~diego/software/luasocket/tcp.html>`_
2389
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002390.. js:function:: Socket.close(socket)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002391
2392 Closes a TCP object. The internal socket used by the object is closed and the
2393 local address to which the object was bound is made available to other
2394 applications. No further operations (except for further calls to the close
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002395 method) are allowed on a closed Socket.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002396
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002397 :param class_socket socket: Is the manipulated Socket.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002398
2399 Note: It is important to close all used sockets once they are not needed,
2400 since, in many systems, each socket uses a file descriptor, which are limited
2401 system resources. Garbage-collected objects are automatically closed before
2402 destruction, though.
2403
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002404.. js:function:: Socket.connect(socket, address[, port])
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002405
2406 Attempts to connect a socket object to a remote host.
2407
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002408
2409 In case of error, the method returns nil followed by a string describing the
2410 error. In case of success, the method returns 1.
2411
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002412 :param class_socket socket: Is the manipulated Socket.
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002413 :param string address: can be an IP address or a host name. See below for more
2414 information.
2415 :param integer port: must be an integer number in the range [1..64K].
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002416 :returns: 1 or nil.
2417
Bertrand Jacquin874a35c2018-09-10 21:26:07 +01002418 An address field extension permits to use the connect() function to connect to
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002419 other stream than TCP. The syntax containing a simpleipv4 or ipv6 address is
2420 the basically expected format. This format requires the port.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002421
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002422 Other format accepted are a socket path like "/socket/path", it permits to
Bertrand Jacquin874a35c2018-09-10 21:26:07 +01002423 connect to a socket. Abstract namespaces are supported with the prefix
Joseph Herlant02cedc42018-11-13 19:45:17 -08002424 "abns@", and finally a file descriptor can be passed with the prefix "fd@".
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002425 The prefix "ipv4@", "ipv6@" and "unix@" are also supported. The port can be
Bertrand Jacquin874a35c2018-09-10 21:26:07 +01002426 passed int the string. The syntax "127.0.0.1:1234" is valid. In this case, the
Tim Duesterhus6edab862018-01-06 19:04:45 +01002427 parameter *port* must not be set.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002428
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002429.. js:function:: Socket.connect_ssl(socket, address, port)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002430
2431 Same behavior than the function socket:connect, but uses SSL.
2432
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002433 :param class_socket socket: Is the manipulated Socket.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002434 :returns: 1 or nil.
2435
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002436.. js:function:: Socket.getpeername(socket)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002437
2438 Returns information about the remote side of a connected client object.
2439
2440 Returns a string with the IP address of the peer, followed by the port number
2441 that peer is using for the connection. In case of error, the method returns
2442 nil.
2443
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002444 :param class_socket socket: Is the manipulated Socket.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002445 :returns: a string containing the server information.
2446
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002447.. js:function:: Socket.getsockname(socket)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002448
2449 Returns the local address information associated to the object.
2450
2451 The method returns a string with local IP address and a number with the port.
2452 In case of error, the method returns nil.
2453
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002454 :param class_socket socket: Is the manipulated Socket.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002455 :returns: a string containing the client information.
2456
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002457.. js:function:: Socket.receive(socket, [pattern [, prefix]])
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002458
2459 Reads data from a client object, according to the specified read pattern.
2460 Patterns follow the Lua file I/O format, and the difference in performance
2461 between all patterns is negligible.
2462
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002463 :param class_socket socket: Is the manipulated Socket.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002464 :param string|integer pattern: Describe what is required (see below).
2465 :param string prefix: A string which will be prefix the returned data.
2466 :returns: a string containing the required data or nil.
2467
2468 Pattern can be any of the following:
2469
2470 * **`*a`**: reads from the socket until the connection is closed. No
2471 end-of-line translation is performed;
2472
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002473 * **`*l`**: reads a line of text from the Socket. The line is terminated by a
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002474 LF character (ASCII 10), optionally preceded by a CR character
2475 (ASCII 13). The CR and LF characters are not included in the
2476 returned line. In fact, all CR characters are ignored by the
2477 pattern. This is the default pattern.
2478
2479 * **number**: causes the method to read a specified number of bytes from the
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002480 Socket. Prefix is an optional string to be concatenated to the
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002481 beginning of any received data before return.
2482
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002483 * **empty**: If the pattern is left empty, the default option is `*l`.
2484
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002485 If successful, the method returns the received pattern. In case of error, the
2486 method returns nil followed by an error message which can be the string
2487 'closed' in case the connection was closed before the transmission was
2488 completed or the string 'timeout' in case there was a timeout during the
2489 operation. Also, after the error message, the function returns the partial
2490 result of the transmission.
2491
2492 Important note: This function was changed severely. It used to support
2493 multiple patterns (but I have never seen this feature used) and now it
2494 doesn't anymore. Partial results used to be returned in the same way as
2495 successful results. This last feature violated the idea that all functions
2496 should return nil on error. Thus it was changed too.
2497
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002498.. js:function:: Socket.send(socket, data [, start [, end ]])
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002499
2500 Sends data through client object.
2501
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002502 :param class_socket socket: Is the manipulated Socket.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002503 :param string data: The data that will be sent.
2504 :param integer start: The start position in the buffer of the data which will
2505 be sent.
2506 :param integer end: The end position in the buffer of the data which will
2507 be sent.
2508 :returns: see below.
2509
2510 Data is the string to be sent. The optional arguments i and j work exactly
2511 like the standard string.sub Lua function to allow the selection of a
2512 substring to be sent.
2513
2514 If successful, the method returns the index of the last byte within [start,
2515 end] that has been sent. Notice that, if start is 1 or absent, this is
2516 effectively the total number of bytes sent. In case of error, the method
2517 returns nil, followed by an error message, followed by the index of the last
2518 byte within [start, end] that has been sent. You might want to try again from
2519 the byte following that. The error message can be 'closed' in case the
2520 connection was closed before the transmission was completed or the string
2521 'timeout' in case there was a timeout during the operation.
2522
2523 Note: Output is not buffered. For small strings, it is always better to
2524 concatenate them in Lua (with the '..' operator) and send the result in one
2525 call instead of calling the method several times.
2526
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002527.. js:function:: Socket.setoption(socket, option [, value])
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002528
2529 Just implemented for compatibility, this cal does nothing.
2530
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002531.. js:function:: Socket.settimeout(socket, value [, mode])
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002532
2533 Changes the timeout values for the object. All I/O operations are blocking.
2534 That is, any call to the methods send, receive, and accept will block
2535 indefinitely, until the operation completes. The settimeout method defines a
2536 limit on the amount of time the I/O methods can block. When a timeout time
2537 has elapsed, the affected methods give up and fail with an error code.
2538
2539 The amount of time to wait is specified as the value parameter, in seconds.
2540
Mark Lakes56cc1252018-03-27 09:48:06 +02002541 The timeout modes are not implemented, the only settable timeout is the
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002542 inactivity time waiting for complete the internal buffer send or waiting for
2543 receive data.
2544
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002545 :param class_socket socket: Is the manipulated Socket.
Bertrand Jacquin874a35c2018-09-10 21:26:07 +01002546 :param float value: The timeout value. Use floating point to specify
Mark Lakes56cc1252018-03-27 09:48:06 +02002547 milliseconds.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002548
Thierry FOURNIER31904272017-10-25 12:59:51 +02002549.. _regex_class:
2550
2551Regex class
2552===========
2553
2554.. js:class:: Regex
2555
2556 This class allows the usage of HAProxy regexes because classic lua doesn't
2557 provides regexes. This class inherits the HAProxy compilation options, so the
2558 regexes can be libc regex, pcre regex or pcre JIT regex.
2559
2560 The expression matching number is limited to 20 per regex. The only available
2561 option is case sensitive.
2562
2563 Because regexes compilation is a heavy process, it is better to define all
2564 your regexes in the **body context** and use it during the runtime.
2565
2566.. code-block:: lua
2567
2568 -- Create the regex
2569 st, regex = Regex.new("needle (..) (...)", true);
2570
2571 -- Check compilation errors
2572 if st == false then
2573 print "error: " .. regex
2574 end
2575
2576 -- Match the regexes
2577 print(regex:exec("Looking for a needle in the haystack")) -- true
2578 print(regex:exec("Lokking for a cat in the haystack")) -- false
2579
2580 -- Extract words
2581 st, list = regex:match("Looking for a needle in the haystack")
2582 print(st) -- true
2583 print(list[1]) -- needle in the
2584 print(list[2]) -- in
2585 print(list[3]) -- the
2586
2587.. js:function:: Regex.new(regex, case_sensitive)
2588
2589 Create and compile a regex.
2590
2591 :param string regex: The regular expression according with the libc or pcre
2592 standard
2593 :param boolean case_sensitive: Match is case sensitive or not.
2594 :returns: boolean status and :ref:`regex_class` or string containing fail reason.
2595
2596.. js:function:: Regex.exec(regex, str)
2597
2598 Execute the regex.
2599
2600 :param class_regex regex: A :ref:`regex_class` object.
2601 :param string str: The input string will be compared with the compiled regex.
2602 :returns: a boolean status according with the match result.
2603
2604.. js:function:: Regex.match(regex, str)
2605
2606 Execute the regex and return matched expressions.
2607
2608 :param class_map map: A :ref:`regex_class` object.
2609 :param string str: The input string will be compared with the compiled regex.
2610 :returns: a boolean status according with the match result, and
2611 a table containing all the string matched in order of declaration.
2612
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002613.. _map_class:
2614
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002615Map class
2616=========
2617
2618.. js:class:: Map
2619
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02002620 This class permits to do some lookups in HAProxy maps. The declared maps can
Bertrand Jacquin874a35c2018-09-10 21:26:07 +01002621 be modified during the runtime through the HAProxy management socket.
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002622
2623.. code-block:: lua
2624
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002625 default = "usa"
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002626
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002627 -- Create and load map
Thierry FOURNIER4dc71972017-01-28 08:33:08 +01002628 geo = Map.new("geo.map", Map._ip);
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002629
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002630 -- Create new fetch that returns the user country
2631 core.register_fetches("country", function(txn)
2632 local src;
2633 local loc;
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002634
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002635 src = txn.f:fhdr("x-forwarded-for");
2636 if (src == nil) then
2637 src = txn.f:src()
2638 if (src == nil) then
2639 return default;
2640 end
2641 end
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002642
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002643 -- Perform lookup
2644 loc = geo:lookup(src);
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002645
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002646 if (loc == nil) then
2647 return default;
2648 end
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002649
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002650 return loc;
2651 end);
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002652
Thierry FOURNIER4dc71972017-01-28 08:33:08 +01002653.. js:attribute:: Map._int
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002654
2655 See the HAProxy configuration.txt file, chapter "Using ACLs and fetching
Ilya Shipitsin2075ca82020-03-06 23:22:22 +05002656 samples" and subchapter "ACL basics" to understand this pattern matching
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002657 method.
2658
Thierry FOURNIER4dc71972017-01-28 08:33:08 +01002659 Note that :js:attr:`Map.int` is also available for compatibility.
2660
2661.. js:attribute:: Map._ip
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002662
2663 See the HAProxy configuration.txt file, chapter "Using ACLs and fetching
Ilya Shipitsin2075ca82020-03-06 23:22:22 +05002664 samples" and subchapter "ACL basics" to understand this pattern matching
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002665 method.
2666
Thierry FOURNIER4dc71972017-01-28 08:33:08 +01002667 Note that :js:attr:`Map.ip` is also available for compatibility.
2668
2669.. js:attribute:: Map._str
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002670
2671 See the HAProxy configuration.txt file, chapter "Using ACLs and fetching
Ilya Shipitsin2075ca82020-03-06 23:22:22 +05002672 samples" and subchapter "ACL basics" to understand this pattern matching
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002673 method.
2674
Thierry FOURNIER4dc71972017-01-28 08:33:08 +01002675 Note that :js:attr:`Map.str` is also available for compatibility.
2676
2677.. js:attribute:: Map._beg
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002678
2679 See the HAProxy configuration.txt file, chapter "Using ACLs and fetching
Ilya Shipitsin2075ca82020-03-06 23:22:22 +05002680 samples" and subchapter "ACL basics" to understand this pattern matching
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002681 method.
2682
Thierry FOURNIER4dc71972017-01-28 08:33:08 +01002683 Note that :js:attr:`Map.beg` is also available for compatibility.
2684
2685.. js:attribute:: Map._sub
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002686
2687 See the HAProxy configuration.txt file, chapter "Using ACLs and fetching
Ilya Shipitsin2075ca82020-03-06 23:22:22 +05002688 samples" and subchapter "ACL basics" to understand this pattern matching
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002689 method.
2690
Thierry FOURNIER4dc71972017-01-28 08:33:08 +01002691 Note that :js:attr:`Map.sub` is also available for compatibility.
2692
2693.. js:attribute:: Map._dir
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002694
2695 See the HAProxy configuration.txt file, chapter "Using ACLs and fetching
Ilya Shipitsin2075ca82020-03-06 23:22:22 +05002696 samples" and subchapter "ACL basics" to understand this pattern matching
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002697 method.
2698
Thierry FOURNIER4dc71972017-01-28 08:33:08 +01002699 Note that :js:attr:`Map.dir` is also available for compatibility.
2700
2701.. js:attribute:: Map._dom
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002702
2703 See the HAProxy configuration.txt file, chapter "Using ACLs and fetching
Ilya Shipitsin2075ca82020-03-06 23:22:22 +05002704 samples" and subchapter "ACL basics" to understand this pattern matching
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002705 method.
2706
Thierry FOURNIER4dc71972017-01-28 08:33:08 +01002707 Note that :js:attr:`Map.dom` is also available for compatibility.
2708
2709.. js:attribute:: Map._end
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002710
2711 See the HAProxy configuration.txt file, chapter "Using ACLs and fetching
Ilya Shipitsin2075ca82020-03-06 23:22:22 +05002712 samples" and subchapter "ACL basics" to understand this pattern matching
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002713 method.
2714
Thierry FOURNIER4dc71972017-01-28 08:33:08 +01002715.. js:attribute:: Map._reg
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002716
2717 See the HAProxy configuration.txt file, chapter "Using ACLs and fetching
Ilya Shipitsin2075ca82020-03-06 23:22:22 +05002718 samples" and subchapter "ACL basics" to understand this pattern matching
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002719 method.
2720
Thierry FOURNIER4dc71972017-01-28 08:33:08 +01002721 Note that :js:attr:`Map.reg` is also available for compatibility.
2722
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002723
2724.. js:function:: Map.new(file, method)
2725
2726 Creates and load a map.
2727
2728 :param string file: Is the file containing the map.
2729 :param integer method: Is the map pattern matching method. See the attributes
2730 of the Map class.
2731 :returns: a class Map object.
Thierry FOURNIER4dc71972017-01-28 08:33:08 +01002732 :see: The Map attributes: :js:attr:`Map._int`, :js:attr:`Map._ip`,
2733 :js:attr:`Map._str`, :js:attr:`Map._beg`, :js:attr:`Map._sub`,
2734 :js:attr:`Map._dir`, :js:attr:`Map._dom`, :js:attr:`Map._end` and
2735 :js:attr:`Map._reg`.
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002736
2737.. js:function:: Map.lookup(map, str)
2738
2739 Perform a lookup in a map.
2740
2741 :param class_map map: Is the class Map object.
2742 :param string str: Is the string used as key.
2743 :returns: a string containing the result or nil if no match.
2744
2745.. js:function:: Map.slookup(map, str)
2746
2747 Perform a lookup in a map.
2748
2749 :param class_map map: Is the class Map object.
2750 :param string str: Is the string used as key.
2751 :returns: a string containing the result or empty string if no match.
2752
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002753.. _applethttp_class:
2754
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002755AppletHTTP class
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002756================
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002757
2758.. js:class:: AppletHTTP
2759
2760 This class is used with applets that requires the 'http' mode. The http applet
2761 can be registered with the *core.register_service()* function. They are used
2762 for processing an http request like a server in back of HAProxy.
2763
2764 This is an hello world sample code:
2765
2766.. code-block:: lua
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002767
Pieter Baauw4d7f7662015-11-08 16:38:08 +01002768 core.register_service("hello-world", "http", function(applet)
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002769 local response = "Hello World !"
2770 applet:set_status(200)
Pieter Baauw2dcb9bc2015-10-01 22:47:12 +02002771 applet:add_header("content-length", string.len(response))
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002772 applet:add_header("content-type", "text/plain")
Pieter Baauw2dcb9bc2015-10-01 22:47:12 +02002773 applet:start_response()
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002774 applet:send(response)
2775 end)
2776
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002777.. js:attribute:: AppletHTTP.c
2778
2779 :returns: A :ref:`converters_class`
2780
2781 This attribute contains a Converters class object.
2782
2783.. js:attribute:: AppletHTTP.sc
2784
2785 :returns: A :ref:`converters_class`
2786
2787 This attribute contains a Converters class object. The
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02002788 functions of this object always return a string.
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002789
2790.. js:attribute:: AppletHTTP.f
2791
2792 :returns: A :ref:`fetches_class`
2793
2794 This attribute contains a Fetches class object. Note that the
2795 applet execution place cannot access to a valid HAProxy core HTTP
Ilya Shipitsin2075ca82020-03-06 23:22:22 +05002796 transaction, so some sample fetches related to the HTTP dependent
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002797 values (hdr, path, ...) are not available.
2798
2799.. js:attribute:: AppletHTTP.sf
2800
2801 :returns: A :ref:`fetches_class`
2802
2803 This attribute contains a Fetches class object. The functions of
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02002804 this object always return a string. Note that the applet
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002805 execution place cannot access to a valid HAProxy core HTTP
Ilya Shipitsin2075ca82020-03-06 23:22:22 +05002806 transaction, so some sample fetches related to the HTTP dependent
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002807 values (hdr, path, ...) are not available.
2808
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01002809.. js:attribute:: AppletHTTP.method
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002810
2811 :returns: string
2812
2813 The attribute method returns a string containing the HTTP
2814 method.
2815
2816.. js:attribute:: AppletHTTP.version
2817
2818 :returns: string
2819
2820 The attribute version, returns a string containing the HTTP
2821 request version.
2822
2823.. js:attribute:: AppletHTTP.path
2824
2825 :returns: string
2826
2827 The attribute path returns a string containing the HTTP
2828 request path.
2829
2830.. js:attribute:: AppletHTTP.qs
2831
2832 :returns: string
2833
2834 The attribute qs returns a string containing the HTTP
2835 request query string.
2836
2837.. js:attribute:: AppletHTTP.length
2838
2839 :returns: integer
2840
2841 The attribute length returns an integer containing the HTTP
2842 body length.
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002843
Thierry FOURNIER841475e2015-12-11 17:10:09 +01002844.. js:attribute:: AppletHTTP.headers
2845
Patrick Hemmerc6a1d712018-05-01 21:30:41 -04002846 :returns: table
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002847
Patrick Hemmerc6a1d712018-05-01 21:30:41 -04002848 The attribute headers returns a table containing the HTTP
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002849 headers. The header names are always in lower case. As the header name can be
2850 encountered more than once in each request, the value is indexed with 0 as
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02002851 first index value. The table has this form:
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002852
2853.. code-block:: lua
2854
2855 AppletHTTP.headers['<header-name>'][<header-index>] = "<header-value>"
2856
2857 AppletHTTP.headers["host"][0] = "www.test.com"
2858 AppletHTTP.headers["accept"][0] = "audio/basic q=1"
2859 AppletHTTP.headers["accept"][1] = "audio/*, q=0.2"
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01002860 AppletHTTP.headers["accept"][2] = "*/*, q=0.1"
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002861..
2862
Robin H. Johnson52f5db22017-01-01 13:10:52 -08002863.. js:function:: AppletHTTP.set_status(applet, code [, reason])
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002864
2865 This function sets the HTTP status code for the response. The allowed code are
2866 from 100 to 599.
2867
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01002868 :param class_AppletHTTP applet: An :ref:`applethttp_class`
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002869 :param integer code: the status code returned to the client.
Robin H. Johnson52f5db22017-01-01 13:10:52 -08002870 :param string reason: the status reason returned to the client (optional).
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002871
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01002872.. js:function:: AppletHTTP.add_header(applet, name, value)
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002873
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02002874 This function adds a header in the response. Duplicated headers are not
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002875 collapsed. The special header *content-length* is used to determinate the
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02002876 response length. If it does not exist, a *transfer-encoding: chunked* is set, and
Ilya Shipitsin2075ca82020-03-06 23:22:22 +05002877 all the write from the function *AppletHTTP:send()* become a chunk.
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002878
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01002879 :param class_AppletHTTP applet: An :ref:`applethttp_class`
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002880 :param string name: the header name
2881 :param string value: the header value
2882
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01002883.. js:function:: AppletHTTP.start_response(applet)
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002884
2885 This function indicates to the HTTP engine that it can process and send the
2886 response headers. After this called we cannot add headers to the response; We
2887 cannot use the *AppletHTTP:send()* function if the
2888 *AppletHTTP:start_response()* is not called.
2889
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01002890 :param class_AppletHTTP applet: An :ref:`applethttp_class`
2891
2892.. js:function:: AppletHTTP.getline(applet)
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002893
2894 This function returns a string containing one line from the http body. If the
2895 data returned doesn't contains a final '\\n' its assumed than its the last
2896 available data before the end of stream.
2897
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01002898 :param class_AppletHTTP applet: An :ref:`applethttp_class`
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002899 :returns: a string. The string can be empty if we reach the end of the stream.
2900
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01002901.. js:function:: AppletHTTP.receive(applet, [size])
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002902
2903 Reads data from the HTTP body, according to the specified read *size*. If the
2904 *size* is missing, the function tries to read all the content of the stream
2905 until the end. If the *size* is bigger than the http body, it returns the
Bertrand Jacquin874a35c2018-09-10 21:26:07 +01002906 amount of data available.
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002907
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01002908 :param class_AppletHTTP applet: An :ref:`applethttp_class`
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002909 :param integer size: the required read size.
Ilya Shipitsin11057a32020-06-21 21:18:27 +05002910 :returns: always return a string,the string can be empty is the connection is
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002911 closed.
2912
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01002913.. js:function:: AppletHTTP.send(applet, msg)
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002914
2915 Send the message *msg* on the http request body.
2916
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01002917 :param class_AppletHTTP applet: An :ref:`applethttp_class`
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002918 :param string msg: the message to send.
2919
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01002920.. js:function:: AppletHTTP.get_priv(applet)
2921
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01002922 Return Lua data stored in the current transaction. If no data are stored,
2923 it returns a nil value.
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01002924
2925 :param class_AppletHTTP applet: An :ref:`applethttp_class`
Bertrand Jacquin874a35c2018-09-10 21:26:07 +01002926 :returns: the opaque data previously stored, or nil if nothing is
2927 available.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01002928 :see: :js:func:`AppletHTTP.set_priv`
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01002929
2930.. js:function:: AppletHTTP.set_priv(applet, data)
2931
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02002932 Store any data in the current HAProxy transaction. This action replaces the
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01002933 old stored data.
2934
2935 :param class_AppletHTTP applet: An :ref:`applethttp_class`
2936 :param opaque data: The data which is stored in the transaction.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01002937 :see: :js:func:`AppletHTTP.get_priv`
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01002938
Tim Duesterhus4e172c92020-05-19 13:49:42 +02002939.. js:function:: AppletHTTP.set_var(applet, var, value[, ifexist])
Thierry FOURNIER / OZON.IOc1edafe2016-12-12 16:25:30 +01002940
2941 Converts a Lua type in a HAProxy type and store it in a variable <var>.
2942
2943 :param class_AppletHTTP applet: An :ref:`applethttp_class`
2944 :param string var: The variable name according with the HAProxy variable syntax.
2945 :param type value: The value associated to the variable. The type ca be string or
2946 integer.
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02002947 :param boolean ifexist: If this parameter is set to true the variable
Tim Duesterhus4e172c92020-05-19 13:49:42 +02002948 will only be set if it was defined elsewhere (i.e. used
Willy Tarreau7978c5c2021-09-07 14:24:07 +02002949 within the configuration). For global variables (using the
2950 "proc" scope), they will only be updated and never created.
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02002951 It is highly recommended to always set this to true.
2952
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01002953 :see: :js:func:`AppletHTTP.unset_var`
2954 :see: :js:func:`AppletHTTP.get_var`
Thierry FOURNIER / OZON.IOc1edafe2016-12-12 16:25:30 +01002955
2956.. js:function:: AppletHTTP.unset_var(applet, var)
2957
2958 Unset the variable <var>.
2959
2960 :param class_AppletHTTP applet: An :ref:`applethttp_class`
2961 :param string var: The variable name according with the HAProxy variable syntax.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01002962 :see: :js:func:`AppletHTTP.set_var`
2963 :see: :js:func:`AppletHTTP.get_var`
Thierry FOURNIER / OZON.IOc1edafe2016-12-12 16:25:30 +01002964
2965.. js:function:: AppletHTTP.get_var(applet, var)
2966
2967 Returns data stored in the variable <var> converter in Lua type.
2968
2969 :param class_AppletHTTP applet: An :ref:`applethttp_class`
2970 :param string var: The variable name according with the HAProxy variable syntax.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01002971 :see: :js:func:`AppletHTTP.set_var`
2972 :see: :js:func:`AppletHTTP.unset_var`
Thierry FOURNIER / OZON.IOc1edafe2016-12-12 16:25:30 +01002973
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002974.. _applettcp_class:
2975
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002976AppletTCP class
2977===============
2978
2979.. js:class:: AppletTCP
2980
2981 This class is used with applets that requires the 'tcp' mode. The tcp applet
2982 can be registered with the *core.register_service()* function. They are used
2983 for processing a tcp stream like a server in back of HAProxy.
2984
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002985.. js:attribute:: AppletTCP.c
2986
2987 :returns: A :ref:`converters_class`
2988
2989 This attribute contains a Converters class object.
2990
2991.. js:attribute:: AppletTCP.sc
2992
2993 :returns: A :ref:`converters_class`
2994
2995 This attribute contains a Converters class object. The
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02002996 functions of this object always return a string.
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002997
2998.. js:attribute:: AppletTCP.f
2999
3000 :returns: A :ref:`fetches_class`
3001
3002 This attribute contains a Fetches class object.
3003
3004.. js:attribute:: AppletTCP.sf
3005
3006 :returns: A :ref:`fetches_class`
3007
3008 This attribute contains a Fetches class object.
3009
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01003010.. js:function:: AppletTCP.getline(applet)
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02003011
3012 This function returns a string containing one line from the stream. If the
3013 data returned doesn't contains a final '\\n' its assumed than its the last
3014 available data before the end of stream.
3015
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01003016 :param class_AppletTCP applet: An :ref:`applettcp_class`
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02003017 :returns: a string. The string can be empty if we reach the end of the stream.
3018
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01003019.. js:function:: AppletTCP.receive(applet, [size])
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02003020
3021 Reads data from the TCP stream, according to the specified read *size*. If the
3022 *size* is missing, the function tries to read all the content of the stream
3023 until the end.
3024
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01003025 :param class_AppletTCP applet: An :ref:`applettcp_class`
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02003026 :param integer size: the required read size.
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02003027 :returns: always return a string, the string can be empty if the connection is
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02003028 closed.
3029
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01003030.. js:function:: AppletTCP.send(appletmsg)
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02003031
3032 Send the message on the stream.
3033
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01003034 :param class_AppletTCP applet: An :ref:`applettcp_class`
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02003035 :param string msg: the message to send.
3036
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01003037.. js:function:: AppletTCP.get_priv(applet)
3038
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01003039 Return Lua data stored in the current transaction. If no data are stored,
3040 it returns a nil value.
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01003041
3042 :param class_AppletTCP applet: An :ref:`applettcp_class`
Bertrand Jacquin874a35c2018-09-10 21:26:07 +01003043 :returns: the opaque data previously stored, or nil if nothing is
3044 available.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01003045 :see: :js:func:`AppletTCP.set_priv`
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01003046
3047.. js:function:: AppletTCP.set_priv(applet, data)
3048
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02003049 Store any data in the current HAProxy transaction. This action replaces the
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01003050 old stored data.
3051
3052 :param class_AppletTCP applet: An :ref:`applettcp_class`
3053 :param opaque data: The data which is stored in the transaction.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01003054 :see: :js:func:`AppletTCP.get_priv`
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01003055
Tim Duesterhus4e172c92020-05-19 13:49:42 +02003056.. js:function:: AppletTCP.set_var(applet, var, value[, ifexist])
Thierry FOURNIER / OZON.IOc1edafe2016-12-12 16:25:30 +01003057
3058 Converts a Lua type in a HAProxy type and stores it in a variable <var>.
3059
3060 :param class_AppletTCP applet: An :ref:`applettcp_class`
3061 :param string var: The variable name according with the HAProxy variable syntax.
3062 :param type value: The value associated to the variable. The type can be string or
3063 integer.
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02003064 :param boolean ifexist: If this parameter is set to true the variable
Tim Duesterhus4e172c92020-05-19 13:49:42 +02003065 will only be set if it was defined elsewhere (i.e. used
Willy Tarreau7978c5c2021-09-07 14:24:07 +02003066 within the configuration). For global variables (using the
3067 "proc" scope), they will only be updated and never created.
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02003068 It is highly recommended to always set this to true.
3069
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01003070 :see: :js:func:`AppletTCP.unset_var`
3071 :see: :js:func:`AppletTCP.get_var`
Thierry FOURNIER / OZON.IOc1edafe2016-12-12 16:25:30 +01003072
3073.. js:function:: AppletTCP.unset_var(applet, var)
3074
3075 Unsets the variable <var>.
3076
3077 :param class_AppletTCP applet: An :ref:`applettcp_class`
3078 :param string var: The variable name according with the HAProxy variable syntax.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01003079 :see: :js:func:`AppletTCP.unset_var`
3080 :see: :js:func:`AppletTCP.set_var`
Thierry FOURNIER / OZON.IOc1edafe2016-12-12 16:25:30 +01003081
3082.. js:function:: AppletTCP.get_var(applet, var)
3083
3084 Returns data stored in the variable <var> converter in Lua type.
3085
3086 :param class_AppletTCP applet: An :ref:`applettcp_class`
3087 :param string var: The variable name according with the HAProxy variable syntax.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01003088 :see: :js:func:`AppletTCP.unset_var`
3089 :see: :js:func:`AppletTCP.set_var`
Thierry FOURNIER / OZON.IOc1edafe2016-12-12 16:25:30 +01003090
Adis Nezirovic8878f8e2018-07-13 12:18:33 +02003091StickTable class
3092================
3093
3094.. js:class:: StickTable
3095
3096 **context**: task, action, sample-fetch
3097
3098 This class can be used to access the HAProxy stick tables from Lua.
3099
3100.. js:function:: StickTable.info()
3101
3102 Returns stick table attributes as a Lua table. See HAProxy documentation for
Ilya Shipitsin2272d8a2020-12-21 01:22:40 +05003103 "stick-table" for canonical info, or check out example below.
Adis Nezirovic8878f8e2018-07-13 12:18:33 +02003104
3105 :returns: Lua table
3106
3107 Assume our table has IPv4 key and gpc0 and conn_rate "columns":
3108
3109.. code-block:: lua
3110
3111 {
3112 expire=<int>, # Value in ms
3113 size=<int>, # Maximum table size
3114 used=<int>, # Actual number of entries in table
3115 data={ # Data columns, with types as key, and periods as values
3116 (-1 if type is not rate counter)
3117 conn_rate=<int>,
3118 gpc0=-1
3119 },
3120 length=<int>, # max string length for string table keys, key length
3121 # otherwise
3122 nopurge=<boolean>, # purge oldest entries when table is full
3123 type="ip" # can be "ip", "ipv6", "integer", "string", "binary"
3124 }
3125
3126.. js:function:: StickTable.lookup(key)
3127
3128 Returns stick table entry for given <key>
3129
3130 :param string key: Stick table key (IP addresses and strings are supported)
3131 :returns: Lua table
3132
3133.. js:function:: StickTable.dump([filter])
3134
3135 Returns all entries in stick table. An optional filter can be used
3136 to extract entries with specific data values. Filter is a table with valid
3137 comparison operators as keys followed by data type name and value pairs.
3138 Check out the HAProxy docs for "show table" for more details. For the
3139 reference, the supported operators are:
3140 "eq", "ne", "le", "lt", "ge", "gt"
3141
3142 For large tables, execution of this function can take a long time (for
3143 HAProxy standards). That's also true when filter is used, so take care and
3144 measure the impact.
3145
3146 :param table filter: Stick table filter
3147 :returns: Stick table entries (table)
3148
3149 See below for example filter, which contains 4 entries (or comparisons).
3150 (Maximum number of filter entries is 4, defined in the source code)
3151
3152.. code-block:: lua
3153
3154 local filter = {
3155 {"gpc0", "gt", 30}, {"gpc1", "gt", 20}}, {"conn_rate", "le", 10}
3156 }
3157
Christopher Faulet0f3c8902020-01-31 18:57:12 +01003158.. _action_class:
3159
3160Action class
3161=============
3162
3163.. js:class:: Act
3164
3165 **context**: action
3166
3167 This class contains all return codes an action may return. It is the lua
3168 equivalent to HAProxy "ACT_RET_*" code.
3169
3170.. code-block:: lua
3171
3172 core.register_action("deny", { "http-req" }, function (txn)
3173 return act.DENY
3174 end)
3175..
3176.. js:attribute:: act.CONTINUE
3177
3178 This attribute is an integer (0). It instructs HAProxy to continue the current
3179 ruleset processing on the message. It is the default return code for a lua
3180 action.
3181
3182 :returns: integer
3183
3184.. js:attribute:: act.STOP
3185
3186 This attribute is an integer (1). It instructs HAProxy to stop the current
3187 ruleset processing on the message.
3188
3189.. js:attribute:: act.YIELD
3190
3191 This attribute is an integer (2). It instructs HAProxy to temporarily pause
3192 the message processing. It will be resumed later on the same rule. The
3193 corresponding lua script is re-executed for the start.
3194
3195.. js:attribute:: act.ERROR
3196
3197 This attribute is an integer (3). It triggers an internal errors The message
3198 processing is stopped and the transaction is terminated. For HTTP streams, an
3199 HTTP 500 error is returned to the client.
3200
3201 :returns: integer
3202
3203.. js:attribute:: act.DONE
3204
3205 This attribute is an integer (4). It instructs HAProxy to stop the message
3206 processing.
3207
3208 :returns: integer
3209
3210.. js:attribute:: act.DENY
3211
3212 This attribute is an integer (5). It denies the current message. The message
3213 processing is stopped and the transaction is terminated. For HTTP streams, an
3214 HTTP 403 error is returned to the client if the deny is returned during the
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02003215 request analysis. During the response analysis, a HTTP 502 error is returned
Christopher Faulet0f3c8902020-01-31 18:57:12 +01003216 and the server response is discarded.
3217
3218 :returns: integer
3219
3220.. js:attribute:: act.ABORT
3221
3222 This attribute is an integer (6). It aborts the current message. The message
3223 processing is stopped and the transaction is terminated. For HTTP streams,
Willy Tarreau714f3452021-05-09 06:47:26 +02003224 HAProxy assumes a response was already sent to the client. From the Lua
Christopher Faulet0f3c8902020-01-31 18:57:12 +01003225 actions point of view, when this code is used, the transaction is terminated
3226 with no reply.
3227
3228 :returns: integer
3229
3230.. js:attribute:: act.INVALID
3231
3232 This attribute is an integer (7). It triggers an internal errors. The message
3233 processing is stopped and the transaction is terminated. For HTTP streams, an
3234 HTTP 400 error is returned to the client if the error is returned during the
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02003235 request analysis. During the response analysis, a HTTP 502 error is returned
Christopher Faulet0f3c8902020-01-31 18:57:12 +01003236 and the server response is discarded.
3237
3238 :returns: integer
Adis Nezirovic8878f8e2018-07-13 12:18:33 +02003239
Christopher Faulet2c2c2e32020-01-31 19:07:52 +01003240.. js:function:: act:wake_time(milliseconds)
3241
3242 **context**: action
3243
3244 Set the script pause timeout to the specified time, defined in
3245 milliseconds.
3246
3247 :param integer milliseconds: the required milliseconds.
3248
3249 This function may be used when a lua action returns `act.YIELD`, to force its
3250 wake-up at most after the specified number of milliseconds.
3251
Christopher Faulet5a2c6612021-08-15 20:35:25 +02003252.. _filter_class:
3253
3254Filter class
3255=============
3256
3257.. js:class:: filter
3258
3259 **context**: filter
3260
3261 This class contains return codes some filter callback functions may return. It
3262 also contains configuration flags and some helper functions. To understand how
3263 the filter API works, see `doc/internal/filters.txt` documentation.
3264
3265.. js:attribute:: filter.CONTINUE
3266
3267 This attribute is an integer (1). It may be returned by some filter callback
3268 functions to instruct this filtering step is finished for this filter.
3269
3270.. js:attribute:: filter.WAIT
3271
3272 This attribute is an integer (0). It may be returned by some filter callback
3273 functions to instruct the filtering must be paused, waiting for more data or
3274 for an external event depending on this filter.
3275
3276.. js:attribute:: filter.ERROR
3277
3278 This attribute is an integer (-1). It may be returned by some filter callback
3279 functions to trigger an error.
3280
3281.. js:attribute:: filter.FLT_CFG_FL_HTX
3282
3283 This attribute is a flag corresponding to the filter flag FLT_CFG_FL_HTX. When
3284 it is set for a filter, it means the filter is able to filter HTTP streams.
3285
3286.. js:function:: filter.register_data_filter(chn)
3287
3288 **context**: filter
3289
3290 Enable the data filtering on the channel **chn** for the current filter. It
Ilya Shipitsinff0f2782021-08-22 22:18:07 +05003291 may be called at any time from any callback functions proceeding the data
Christopher Faulet5a2c6612021-08-15 20:35:25 +02003292 analysis.
3293
3294 :param class_Channel chn: A :ref:`channel_class`.
3295
3296.. js:function:: filter.unregister_data_filter(chn)
3297
3298 **context**: filter
3299
3300 Disable the data filtering on the channel **chn** for the current filter. It
3301 may be called at any time from any callback functions.
3302
3303 :param class_Channel chn: A :ref:`channel_class`.
3304
3305.. js:function:: filter.wake_time(milliseconds)
3306
3307 **context**: filter
3308
3309 Set the script pause timeout to the specified time, defined in
3310 milliseconds.
3311
3312 :param integer milliseconds: the required milliseconds.
3313
3314 This function may be used from any lua filter callback function to force its
3315 wake-up at most after the specified number of milliseconds. Especially, when
3316 `filter.CONTINUE` is returned.
3317
3318
3319A filters is declared using :js:func:`core.register_filter()` function. The
3320provided class will be used to instantiate filters. It may define following
3321attributes:
3322
3323* id: The filter identifier. It is a string that identifies the filter and is
3324 optional.
3325
3326* flags: The filter flags. Only :js:attr:`filter.FLT_CFG_FL_HTX` may be set for now.
3327
3328Such filter class must also define all required callback functions in the
3329following list. Note that :js:func:`Filter.new()` must be defined otherwise the
Ilya Shipitsinff0f2782021-08-22 22:18:07 +05003330filter is ignored. Others are optional.
Christopher Faulet5a2c6612021-08-15 20:35:25 +02003331
3332* .. js:function:: FILTER.new()
3333
3334 Called to instantiate a new filter. This function must be defined.
3335
3336 :returns: a Lua object that will be used as filter instance for the current
3337 stream.
3338
3339* .. js:function:: FILTER.start_analyze(flt, txn, chn)
3340
3341 Called when the analysis starts on the channel **chn**.
3342
3343* .. js:function:: FILTER.end_analyze(flt, txn, chn)
3344
3345 Called when the analysis ends on the channel **chn**.
3346
3347* .. js:function:: FILTER.http_headers(flt, txn, http_msg)
3348
3349 Called just before the HTTP payload analysis and after any processing on the
3350 HTTP message **http_msg**. This callback functions is only called for HTTP
3351 streams.
3352
3353* .. js:function:: FILTER.http_payload(flt, txn, http_msg)
3354
3355 Called during the HTTP payload analysis on the HTTP message **http_msg**. This
3356 callback functions is only called for HTTP streams.
3357
3358* .. js:function:: FILTER.http_end(flt, txn, http_msg)
3359
3360 Called after the HTTP payload analysis on the HTTP message **http_msg**. This
3361 callback functions is only called for HTTP streams.
3362
3363* .. js:function:: FILTER.tcp_payload(flt, txn, chn)
3364
3365 Called during the TCP payload analysis on the channel **chn**.
3366
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02003367Here is a full example:
Christopher Faulet5a2c6612021-08-15 20:35:25 +02003368
3369.. code-block:: lua
3370
3371 Trace = {}
3372 Trace.id = "Lua trace filter"
3373 Trace.flags = filter.FLT_CFG_FL_HTX;
3374 Trace.__index = Trace
3375
3376 function Trace:new()
3377 local trace = {}
3378 setmetatable(trace, Trace)
3379 trace.req_len = 0
3380 trace.res_len = 0
3381 return trace
3382 end
3383
3384 function Trace:start_analyze(txn, chn)
3385 if chn:is_resp() then
3386 print("Start response analysis")
3387 else
3388 print("Start request analysis")
3389 end
3390 filter.register_data_filter(self, chn)
3391 end
3392
3393 function Trace:end_analyze(txn, chn)
3394 if chn:is_resp() then
3395 print("End response analysis: "..self.res_len.." bytes filtered")
3396 else
3397 print("End request analysis: "..self.req_len.." bytes filtered")
3398 end
3399 end
3400
3401 function Trace:http_headers(txn, http_msg)
3402 stline = http_msg:get_stline()
3403 if http_msg.channel:is_resp() then
3404 print("response:")
3405 print(stline.version.." "..stline.code.." "..stline.reason)
3406 else
3407 print("request:")
3408 print(stline.method.." "..stline.uri.." "..stline.version)
3409 end
3410
3411 for n, hdrs in pairs(http_msg:get_headers()) do
3412 for i,v in pairs(hdrs) do
3413 print(n..": "..v)
3414 end
3415 end
3416 return filter.CONTINUE
3417 end
3418
3419 function Trace:http_payload(txn, http_msg)
3420 body = http_msg:body(-20000)
3421 if http_msg.channel:is_resp() then
3422 self.res_len = self.res_len + body:len()
3423 else
3424 self.req_len = self.req_len + body:len()
3425 end
3426 end
3427
3428 core.register_filter("trace", Trace, function(trace, args)
3429 return trace
3430 end)
3431
3432..
3433
3434.. _httpmessage_class:
3435
3436HTTPMessage class
3437===================
3438
3439.. js:class:: HTTPMessage
3440
3441 **context**: filter
3442
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02003443 This class contains all functions to manipulate a HTTP message. For now, this
Christopher Faulet5a2c6612021-08-15 20:35:25 +02003444 class is only available from a filter context.
3445
3446.. js:function:: HTTPMessage.add_header(http_msg, name, value)
3447
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02003448 Appends a HTTP header field in the HTTP message **http_msg** whose name is
Christopher Faulet5a2c6612021-08-15 20:35:25 +02003449 specified in **name** and whose value is defined in **value**.
3450
3451 :param class_httpmessage http_msg: The manipulated HTTP message.
3452 :param string name: The header name.
3453 :param string value: The header value.
3454
3455.. js:function:: HTTPMessage.append(http_msg, string)
3456
3457 This function copies the string **string** at the end of incoming data of the
3458 HTTP message **http_msg**. The function returns the copied length on success
3459 or -1 if data cannot be copied.
3460
3461 Same that :js:func:`HTTPMessage.insert(http_msg, string, http_msg:input())`.
3462
3463 :param class_httpmessage http_msg: The manipulated HTTP message.
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02003464 :param string string: The data to copy at the end of incoming data.
Christopher Faulet5a2c6612021-08-15 20:35:25 +02003465 :returns: an integer containing the amount of bytes copied or -1.
3466
3467.. js:function:: HTTPMessage.body(http_msgl[, offset[, length]])
3468
3469 This function returns **length** bytes of incoming data from the HTTP message
3470 **http_msg**, starting at the offset **offset**. The data are not removed from
3471 the buffer.
3472
3473 By default, if no length is provided, all incoming data found, starting at the
3474 given offset, are returned. If **length** is set to -1, the function tries to
3475 retrieve a maximum of data. Because it is called in the filter context, it
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02003476 never yield. Not providing an offset is the same as setting it to 0. A
Ilya Shipitsinff0f2782021-08-22 22:18:07 +05003477 positive offset is relative to the beginning of incoming data of the
Christopher Faulet5a2c6612021-08-15 20:35:25 +02003478 http_message buffer while negative offset is relative to their end.
3479
3480 If there is no incoming data and the HTTP message can't receive more data, a 'nil'
3481 value is returned.
3482
3483 :param class_httpmessage http_msg: The manipulated HTTP message.
3484 :param integer offset: *optional* The offset in incoming data to start to get
3485 data. 0 by default. May be negative to be relative to
3486 the end of incoming data.
3487 :param integer length: *optional* The expected length of data to retrieve. All
3488 incoming data by default. May be set to -1 to get a
3489 maximum of data.
3490 :returns: a string containing the data found or nil.
3491
3492.. js:function:: HTTPMessage.eom(http_msg)
3493
3494 This function returns true if the end of message is reached for the HTTP
3495 message **http_msg**.
3496
3497 :param class_httpmessage http_msg: The manipulated HTTP message.
3498 :returns: an integer containing the amount of available bytes.
3499
3500.. js:function:: HTTPMessage.del_header(http_msg, name)
3501
3502 Removes all HTTP header fields in the HTTP message **http_msg** whose name is
3503 specified in **name**.
3504
3505 :param class_httpmessage http_msg: The manipulated http message.
3506 :param string name: The header name.
3507
3508.. js:function:: HTTPMessage.get_headers(http_msg)
3509
3510 Returns a table containing all the headers of the HTTP message **http_msg**.
3511
3512 :param class_httpmessage http_msg: The manipulated http message.
3513 :returns: table of headers.
3514
3515 This is the form of the returned table:
3516
3517.. code-block:: lua
3518
3519 http_msg:get_headers()['<header-name>'][<header-index>] = "<header-value>"
3520
3521 local hdr = http_msg:get_headers()
3522 hdr["host"][0] = "www.test.com"
3523 hdr["accept"][0] = "audio/basic q=1"
3524 hdr["accept"][1] = "audio/*, q=0.2"
3525 hdr["accept"][2] = "*.*, q=0.1"
3526..
3527
3528.. js:function:: HTTPMessage.get_stline(http_msg)
3529
3530 Returns a table containing the start-line of the HTTP message **http_msg**.
3531
3532 :param class_httpmessage http_msg: The manipulated http message.
3533 :returns: the start-line.
3534
3535 This is the form of the returned table:
3536
3537.. code-block:: lua
3538
3539 -- for the request :
3540 {"method" = string, "uri" = string, "version" = string}
3541
3542 -- for the response:
3543 {"version" = string, "code" = string, "reason" = string}
3544..
3545
3546.. js:function:: HTTPMessage.forward(http_msg, length)
3547
3548 This function forwards **length** bytes of data from the HTTP message
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02003549 **http_msg**. Because it is called in the filter context, it never yields. Only
Christopher Faulet5a2c6612021-08-15 20:35:25 +02003550 available incoming data may be forwarded, event if the requested length
3551 exceeds the available amount of incoming data. It returns the amount of data
3552 forwarded.
3553
3554 :param class_httpmessage http_msg: The manipulated HTTP message.
3555 :param integer int: The amount of data to forward.
3556
3557.. js:function:: HTTPMessage.input(http_msg)
3558
3559 This function returns the length of incoming data in the HTTP message
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02003560 **http_msg** from the filter point of view.
Christopher Faulet5a2c6612021-08-15 20:35:25 +02003561
3562 :param class_httpmessage http_msg: The manipulated HTTP message.
3563 :returns: an integer containing the amount of available bytes.
3564
3565.. js:function:: HTTPMessage.insert(http_msg, string[, offset])
3566
3567 This function copies the string **string** at the offset **offset** in
3568 incoming data of the HTTP message **http_msg**. The function returns the
3569 copied length on success or -1 if data cannot be copied.
3570
3571 By default, if no offset is provided, the string is copied in front of
Ilya Shipitsinff0f2782021-08-22 22:18:07 +05003572 incoming data. A positive offset is relative to the beginning of incoming data
Christopher Faulet5a2c6612021-08-15 20:35:25 +02003573 of the HTTP message while negative offset is relative to their end.
3574
3575 :param class_httpmessage http_msg: The manipulated HTTP message.
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02003576 :param string string: The data to copy into incoming data.
3577 :param integer offset: *optional* The offset in incoming data where to copy
Christopher Faulet5a2c6612021-08-15 20:35:25 +02003578 data. 0 by default. May be negative to be relative to
3579 the end of incoming data.
3580 :returns: an integer containing the amount of bytes copied or -1.
3581
3582.. js:function:: HTTPMessage.is_full(http_msg)
3583
3584 This function returns true if the HTTP message **http_msg** is full.
3585
3586 :param class_httpmessage http_msg: The manipulated HTTP message.
3587 :returns: a boolean
3588
3589.. js:function:: HTTPMessage.is_resp(http_msg)
3590
3591 This function returns true if the HTTP message **http_msg** is the response
3592 one.
3593
3594 :param class_httpmessage http_msg: The manipulated HTTP message.
3595 :returns: a boolean
3596
3597.. js:function:: HTTPMessage.may_recv(http_msg)
3598
3599 This function returns true if the HTTP message **http_msg** may still receive
3600 data.
3601
3602 :param class_httpmessage http_msg: The manipulated HTTP message.
3603 :returns: a boolean
3604
3605.. js:function:: HTTPMessage.output(http_msg)
3606
3607 This function returns the length of outgoing data of the HTTP message
3608 **http_msg**.
3609
3610 :param class_httpmessage http_msg: The manipulated HTTP message.
3611 :returns: an integer containing the amount of available bytes.
3612
3613.. js:function:: HTTPMessage.prepend(http_msg, string)
3614
3615 This function copies the string **string** in front of incoming data of the
3616 HTTP message **http_msg**. The function returns the copied length on success
3617 or -1 if data cannot be copied.
3618
3619 Same that :js:func:`HTTPMessage.insert(http_msg, string, 0)`.
3620
3621 :param class_httpmessage http_msg: The manipulated HTTP message.
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02003622 :param string string: The data to copy in front of incoming data.
Christopher Faulet5a2c6612021-08-15 20:35:25 +02003623 :returns: an integer containing the amount of bytes copied or -1.
3624
3625.. js:function:: HTTPMessage.remove(http_msg[, offset[, length]])
3626
3627 This function removes **length** bytes of incoming data of the HTTP message
3628 **http_msg**, starting at offset **offset**. This function returns number of
3629 bytes removed on success.
3630
3631 By default, if no length is provided, all incoming data, starting at the given
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02003632 offset, are removed. Not providing an offset is the same that setting it
Ilya Shipitsinff0f2782021-08-22 22:18:07 +05003633 to 0. A positive offset is relative to the beginning of incoming data of the
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02003634 HTTP message while negative offset is relative to the end.
Christopher Faulet5a2c6612021-08-15 20:35:25 +02003635
3636 :param class_httpmessage http_msg: The manipulated HTTP message.
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02003637 :param integer offset: *optional* The offset in incoming data where to start
Christopher Faulet5a2c6612021-08-15 20:35:25 +02003638 to remove data. 0 by default. May be negative to
3639 be relative to the end of incoming data.
3640 :param integer length: *optional* The length of data to remove. All incoming
3641 data by default.
3642 :returns: an integer containing the amount of bytes removed.
3643
3644.. js:function:: HTTPMessage.rep_header(http_msg, name, regex, replace)
3645
3646 Matches the regular expression in all occurrences of header field **name**
3647 according to regex **regex**, and replaces them with the string **replace**.
3648 The replacement value can contain back references like \1, \2, ... This
3649 function acts on whole header lines, regardless of the number of values they
3650 may contain.
3651
3652 :param class_httpmessage http_msg: The manipulated HTTP message.
3653 :param string name: The header name.
3654 :param string regex: The match regular expression.
3655 :param string replace: The replacement value.
3656
3657.. js:function:: HTTPMessage.rep_value(http_msg, name, regex, replace)
3658
3659 Matches the regular expression on every comma-delimited value of header field
3660 **name** according to regex **regex**, and replaces them with the string
3661 **replace**. The replacement value can contain back references like \1, \2,
3662 ...
3663
3664 :param class_httpmessage http_msg: The manipulated HTTP message.
3665 :param string name: The header name.
3666 :param string regex: The match regular expression.
3667 :param string replace: The replacement value.
3668
3669.. js:function:: HTTPMessage.send(http_msg, string)
3670
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02003671 This function requires immediate send of the string **string**. It means the
Ilya Shipitsinff0f2782021-08-22 22:18:07 +05003672 string is copied at the beginning of incoming data of the HTTP message
Christopher Faulet5a2c6612021-08-15 20:35:25 +02003673 **http_msg** and immediately forwarded. Because it is called in the filter
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02003674 context, it never yields.
Christopher Faulet5a2c6612021-08-15 20:35:25 +02003675
3676 :param class_httpmessage http_msg: The manipulated HTTP message.
3677 :param string string: The data to send.
3678 :returns: an integer containing the amount of bytes copied or -1.
3679
3680.. js:function:: HTTPMessage.set(http_msg, string[, offset[, length]])
3681
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02003682 This function replaces **length** bytes of incoming data of the HTTP message
Christopher Faulet5a2c6612021-08-15 20:35:25 +02003683 **http_msg**, starting at offset **offset**, by the string **string**. The
3684 function returns the copied length on success or -1 if data cannot be copied.
3685
3686 By default, if no length is provided, all incoming data, starting at the given
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02003687 offset, are replaced. Not providing an offset is the same as setting it
Ilya Shipitsinff0f2782021-08-22 22:18:07 +05003688 to 0. A positive offset is relative to the beginning of incoming data of the
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02003689 HTTP message while negative offset is relative to the end.
Christopher Faulet5a2c6612021-08-15 20:35:25 +02003690
3691 :param class_httpmessage http_msg: The manipulated HTTP message.
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02003692 :param string string: The data to copy into incoming data.
3693 :param integer offset: *optional* The offset in incoming data where to start
Christopher Faulet5a2c6612021-08-15 20:35:25 +02003694 the data replacement. 0 by default. May be negative to
3695 be relative to the end of incoming data.
3696 :param integer length: *optional* The length of data to replace. All incoming
3697 data by default.
3698 :returns: an integer containing the amount of bytes copied or -1.
3699
3700.. js:function:: HTTPMessage.set_eom(http_msg)
3701
3702 This function set the end of message for the HTTP message **http_msg**.
3703
3704 :param class_httpmessage http_msg: The manipulated HTTP message.
3705
3706.. js:function:: HTTPMessage.set_header(http_msg, name, value)
3707
3708 This variable replace all occurrence of all header matching the name **name**,
3709 by only one containing the value **value**.
3710
3711 :param class_httpmessage http_msg: The manipulated HTTP message.
3712 :param string name: The header name.
3713 :param string value: The header value.
3714
3715 This function does the same work as the following code:
3716
3717.. code-block:: lua
3718
3719 http_msg:del_header("header")
3720 http_msg:add_header("header", "value")
3721..
3722
3723.. js:function:: HTTPMessage.set_method(http_msg, method)
3724
3725 Rewrites the request method with the string **method**. The HTTP message
3726 **http_msg** must be the request.
3727
3728 :param class_httpmessage http_msg: The manipulated HTTP message.
3729 :param string method: The new method.
3730
3731.. js:function:: HTTPMessage.set_path(http_msg, path)
3732
3733 Rewrites the request path with the string **path**. The HTTP message
3734 **http_msg** must be the request.
3735
3736 :param class_httpmessage http_msg: The manipulated HTTP message.
3737 :param string method: The new method.
3738
3739.. js:function:: HTTPMessage.set_query(http_msg, query)
3740
3741 Rewrites the request's query string which appears after the first question
3742 mark ("?") with the string **query**. The HTTP message **http_msg** must be
3743 the request.
3744
3745 :param class_httpmessage http_msg: The manipulated HTTP message.
3746 :param string query: The new query.
3747
3748.. js:function:: HTTPMessage.set_status(http_msg, status[, reason])
3749
Ilya Shipitsinff0f2782021-08-22 22:18:07 +05003750 Rewrites the response status code with the integer **code** and optional the
Christopher Faulet5a2c6612021-08-15 20:35:25 +02003751 reason **reason**. If no custom reason is provided, it will be generated from
3752 the status. The HTTP message **http_msg** must be the response.
3753
3754 :param class_httpmessage http_msg: The manipulated HTTP message.
3755 :param integer status: The new response status code.
3756 :param string reason: The new response reason (optional).
3757
3758.. js:function:: HTTPMessage.set_uri(http_msg, uri)
3759
3760 Rewrites the request URI with the string **uri**. The HTTP message
3761 **http_msg** must be the request.
3762
3763 :param class_httpmessage http_msg: The manipulated HTTP message.
3764 :param string uri: The new uri.
3765
3766.. js:function:: HTTPMessage.unset_eom(http_msg)
3767
3768 This function remove the end of message for the HTTP message **http_msg**.
3769
3770 :param class_httpmessage http_msg: The manipulated HTTP message.
3771
William Lallemand10cea5c2022-03-30 16:02:43 +02003772.. _CertCache_class:
3773
3774CertCache class
3775================
3776
3777.. js:class:: CertCache
3778
3779 This class allows to update an SSL certificate file in the memory of the
3780 current HAProxy process. It will do the same as "set ssl cert" + "commit ssl
3781 cert" over the HAProxy CLI.
3782
3783.. js:function:: CertCache.set(certificate)
3784
3785 This function updates a certificate in memory.
3786
3787 :param table certificate: A table containing the fields to update.
3788 :param string certificate.filename: The mandatory filename of the certificate
3789 to update, it must already exist in memory.
3790 :param string certificate.crt: A certificate in the PEM format. It can also
3791 contain a private key.
3792 :param string certificate.key: A private key in the PEM format.
3793 :param string certificate.ocsp: An OCSP response in base64. (cf management.txt)
3794 :param string certificate.issuer: The certificate of the OCSP issuer.
3795 :param string certificate.sctl: An SCTL file.
3796
3797.. code-block:: lua
3798
3799 CertCache.set{filename="certs/localhost9994.pem.rsa", crt=crt}
3800
3801
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01003802External Lua libraries
3803======================
3804
3805A lot of useful lua libraries can be found here:
3806
Aurelien DARRAGON846fc7d2022-10-14 08:48:57 +02003807* Lua toolbox has been superseded by `https://luarocks.org/ <https://luarocks.org/>`_
3808 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 +01003809
Ilya Shipitsin2075ca82020-03-06 23:22:22 +05003810Redis client library:
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01003811
3812* `https://github.com/nrk/redis-lua <https://github.com/nrk/redis-lua>`_
3813
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02003814This is an example about the usage of the Redis library within HAProxy. Note that
3815each call to any function of this library can throw an error if the socket
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01003816connection fails.
3817
3818.. code-block:: lua
3819
3820 -- load the redis library
3821 local redis = require("redis");
3822
3823 function do_something(txn)
3824
3825 -- create and connect new tcp socket
3826 local tcp = core.tcp();
3827 tcp:settimeout(1);
3828 tcp:connect("127.0.0.1", 6379);
3829
3830 -- use the redis library with this new socket
3831 local client = redis.connect({socket=tcp});
3832 client:ping();
3833
3834 end
3835
3836OpenSSL:
3837
3838* `http://mkottman.github.io/luacrypto/index.html
3839 <http://mkottman.github.io/luacrypto/index.html>`_
3840
3841* `https://github.com/brunoos/luasec/wiki
3842 <https://github.com/brunoos/luasec/wiki>`_