blob: 038400a079c8436c7d55e2ae347a937152cbd448 [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
Aurelien DARRAGONd5c80d72023-03-13 19:36:13 +0100513
Thierry FOURNIERc5d11c62018-02-12 14:46:54 +0100514..
515
Bertrand Jacquin874a35c2018-09-10 21:26:07 +0100516 A second example using arguments
Thierry FOURNIERc5d11c62018-02-12 14:46:54 +0100517
518.. code-block:: lua
519
520 function hello_world(txn, arg)
521 txn:Info("Hello world for " .. arg)
522 end
523 core.register_action("hello-world", { "tcp-req", "http-req" }, hello_world, 2)
Aurelien DARRAGONd5c80d72023-03-13 19:36:13 +0100524
Thierry FOURNIERc5d11c62018-02-12 14:46:54 +0100525..
Thierry FOURNIER8255a752015-09-23 21:03:35 +0200526
Willy Tarreau714f3452021-05-09 06:47:26 +0200527 This example code is used in HAProxy configuration like this:
Thierry FOURNIERc5d11c62018-02-12 14:46:54 +0100528
529::
530
531 frontend tcp_frt
532 mode tcp
533 tcp-request content lua.hello-world everybody
Aurelien DARRAGONd5c80d72023-03-13 19:36:13 +0100534
Thierry FOURNIERc5d11c62018-02-12 14:46:54 +0100535..
Aurelien DARRAGON53901f42022-10-13 19:49:42 +0200536
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100537.. js:function:: core.register_converters(name, func)
538
539 **context**: body
540
David Carlier61fdf8b2015-10-02 11:59:38 +0100541 Register a Lua function executed as converter. All the registered converters
Aurelien DARRAGON53901f42022-10-13 19:49:42 +0200542 can be used in HAProxy with the prefix "lua.". A converter gets a string as
543 input and returns a string as output. The registered function can take up to 9
544 values as parameter. All the values are strings.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100545
546 :param string name: is the name of the converter.
547 :param function func: is the Lua function called to work as converter.
548
549 The prototype of the Lua function used as argument is:
550
551.. code-block:: lua
552
553 function(str, [p1 [, p2 [, ... [, p5]]]])
554..
555
556 * **str** (*string*): this is the input value automatically converted in
557 string.
558 * **p1** .. **p5** (*string*): this is a list of string arguments declared in
Bertrand Jacquin874a35c2018-09-10 21:26:07 +0100559 the HAProxy configuration file. The number of arguments doesn't exceed 5.
Aurelien DARRAGON53901f42022-10-13 19:49:42 +0200560 The order and the nature of these is conventionally chosen by the
Bertrand Jacquin874a35c2018-09-10 21:26:07 +0100561 developer.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100562
563.. js:function:: core.register_fetches(name, func)
564
565 **context**: body
566
David Carlier61fdf8b2015-10-02 11:59:38 +0100567 Register a Lua function executed as sample fetch. All the registered sample
Bertrand Jacquin874a35c2018-09-10 21:26:07 +0100568 fetch can be used in HAProxy with the prefix "lua.". A Lua sample fetch
Aurelien DARRAGON53901f42022-10-13 19:49:42 +0200569 returns a string as output. The registered function can take up to 9 values as
570 parameter. All the values are strings.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100571
Aurelien DARRAGON53901f42022-10-13 19:49:42 +0200572 :param string name: is the name of the sample fetch.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100573 :param function func: is the Lua function called to work as sample fetch.
574
575 The prototype of the Lua function used as argument is:
576
577.. code-block:: lua
578
579 string function(txn, [p1 [, p2 [, ... [, p5]]]])
580..
581
Thierry FOURNIERdc595002015-12-21 11:13:52 +0100582 * **txn** (:ref:`txn_class`): this is the txn object associated with the current
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100583 request.
584 * **p1** .. **p5** (*string*): this is a list of string arguments declared in
Bertrand Jacquin874a35c2018-09-10 21:26:07 +0100585 the HAProxy configuration file. The number of arguments doesn't exceed 5.
Aurelien DARRAGON53901f42022-10-13 19:49:42 +0200586 The order and the nature of these is conventionally chosen by the
Bertrand Jacquin874a35c2018-09-10 21:26:07 +0100587 developer.
588 * **Returns**: A string containing some data, or nil if the value cannot be
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100589 returned now.
590
591 lua example code:
592
593.. code-block:: lua
594
595 core.register_fetches("hello", function(txn)
596 return "hello"
597 end)
598..
599
600 HAProxy example configuration:
601
602::
603
604 frontend example
605 http-request redirect location /%[lua.hello]
606
Christopher Faulet5a2c6612021-08-15 20:35:25 +0200607.. js:function:: core.register_filter(name, Flt, func)
608
609 **context**: body
610
611 Register a Lua function used to declare a filter. All the registered filters
612 can by used in HAProxy with the prefix "lua.".
613
614 :param string name: is the name of the filter.
615 :param table Flt: is a Lua class containing the filter definition (id, flags,
616 callbacks).
617 :param function func: is the Lua function called to create the Lua filter.
618
619 The prototype of the Lua function used as argument is:
620
621.. code-block:: lua
622
623 function(flt, args)
624..
625
626 * **flt** : Is a filter object based on the class provided in
627 :js:func:`core.register_filter()` function.
628
629 * **args**: Is a table of strings containing all arguments provided through
630 the HAProxy configuration file, on the filter line.
631
632 It must return the filter to use or nil to ignore it. Here, an example of
633 filter registration.
634
635.. code-block:: lua
636
637 core.register_filter("my-filter", MyFilter, function(flt, args)
638 flt.args = args -- Save arguments
639 return flt
640 end)
641..
642
643 This example code is used in HAProxy configuration like this:
644
645::
646
647 frontend http
648 mode http
649 filter lua.my-filter arg1 arg2 arg3
Aurelien DARRAGONd5c80d72023-03-13 19:36:13 +0100650
Christopher Faulet5a2c6612021-08-15 20:35:25 +0200651..
652
653 :see: :js:class:`Filter`
654
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +0200655.. js:function:: core.register_service(name, mode, func)
656
657 **context**: body
658
Aurelien DARRAGON53901f42022-10-13 19:49:42 +0200659 Register a Lua function executed as a service. All the registered services can
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +0200660 be used in HAProxy with the prefix "lua.". A service gets an object class as
661 input according with the required mode.
662
Aurelien DARRAGON53901f42022-10-13 19:49:42 +0200663 :param string name: is the name of the service.
Willy Tarreau61add3c2015-09-28 15:39:10 +0200664 :param string mode: is string describing the required mode. Only 'tcp' or
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +0200665 'http' are allowed.
Aurelien DARRAGON53901f42022-10-13 19:49:42 +0200666 :param function func: is the Lua function called to work as service.
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +0200667
668 The prototype of the Lua function used as argument is:
669
670.. code-block:: lua
671
672 function(applet)
673..
674
Thierry FOURNIERdc595002015-12-21 11:13:52 +0100675 * **applet** *applet* will be a :ref:`applettcp_class` or a
676 :ref:`applethttp_class`. It depends the type of registered applet. An applet
677 registered with the 'http' value for the *mode* parameter will gets a
678 :ref:`applethttp_class`. If the *mode* value is 'tcp', the applet will gets
679 a :ref:`applettcp_class`.
680
Christopher Faulet1e9b1b62021-08-11 10:14:30 +0200681 .. warning::
682 Applets of type 'http' cannot be called from 'tcp-*' rulesets. Only the
683 'http-*' rulesets are authorized, this means that is not possible to call
Aurelien DARRAGON53901f42022-10-13 19:49:42 +0200684 a HTTP applet from a proxy in tcp mode. Applets of type 'tcp' can be
Christopher Faulet1e9b1b62021-08-11 10:14:30 +0200685 called from anywhere.
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +0200686
Bertrand Jacquin874a35c2018-09-10 21:26:07 +0100687 Here, an example of service registration. The service just send an 'Hello world'
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +0200688 as an http response.
689
690.. code-block:: lua
691
Pieter Baauw4d7f7662015-11-08 16:38:08 +0100692 core.register_service("hello-world", "http", function(applet)
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +0200693 local response = "Hello World !"
694 applet:set_status(200)
Pieter Baauw2dcb9bc2015-10-01 22:47:12 +0200695 applet:add_header("content-length", string.len(response))
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +0200696 applet:add_header("content-type", "text/plain")
Pieter Baauw2dcb9bc2015-10-01 22:47:12 +0200697 applet:start_response()
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +0200698 applet:send(response)
699 end)
700..
701
Willy Tarreau714f3452021-05-09 06:47:26 +0200702 This example code is used in HAProxy configuration like this:
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +0200703
704::
705
706 frontend example
707 http-request use-service lua.hello-world
708
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100709.. js:function:: core.register_init(func)
710
711 **context**: body
712
713 Register a function executed after the configuration parsing. This is useful
714 to check any parameters.
715
Pieter Baauw4d7f7662015-11-08 16:38:08 +0100716 :param function func: is the Lua function called to work as initializer.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100717
718 The prototype of the Lua function used as argument is:
719
720.. code-block:: lua
721
722 function()
723..
724
725 It takes no input, and no output is expected.
726
Aurelien DARRAGONb8038992023-03-09 16:48:30 +0100727.. js:function:: core.register_task(func[, arg1[, arg2[, ...[, arg4]]]])
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100728
729 **context**: body, init, task, action, sample-fetch, converter
730
731 Register and start independent task. The task is started when the HAProxy
732 main scheduler starts. For example this type of tasks can be executed to
Thierry FOURNIER2e4893c2015-03-18 13:37:27 +0100733 perform complex health checks.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100734
Aurelien DARRAGONb8038992023-03-09 16:48:30 +0100735 :param function func: is the Lua function called to work as an async task.
736
737 Up to 4 optional arguments (all types supported) may be passed to the function.
738 (They will be passed as-is to the task function)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100739
740 The prototype of the Lua function used as argument is:
741
742.. code-block:: lua
743
Aurelien DARRAGONb8038992023-03-09 16:48:30 +0100744 function([arg1[, arg2[, ...[, arg4]]]])
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100745..
746
Aurelien DARRAGONb8038992023-03-09 16:48:30 +0100747 It takes up to 4 optional arguments (provided when registering), and no output is expected.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100748
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +0100749.. js:function:: core.register_cli([path], usage, func)
750
751 **context**: body
752
Aurelien DARRAGON53901f42022-10-13 19:49:42 +0200753 Register a custom cli that will be available from haproxy stats socket.
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +0100754
755 :param array path: is the sequence of word for which the cli execute the Lua
756 binding.
757 :param string usage: is the usage message displayed in the help.
758 :param function func: is the Lua function called to handle the CLI commands.
759
760 The prototype of the Lua function used as argument is:
761
762.. code-block:: lua
763
764 function(AppletTCP, [arg1, [arg2, [...]]])
765..
766
767 I/O are managed with the :ref:`applettcp_class` object. Args are given as
Bertrand Jacquin874a35c2018-09-10 21:26:07 +0100768 parameter. The args embed the registered path. If the path is declared like
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +0100769 this:
770
771.. code-block:: lua
772
773 core.register_cli({"show", "ssl", "stats"}, "Display SSL stats..", function(applet, arg1, arg2, arg3, arg4, arg5)
774 end)
775..
776
777 And we execute this in the prompt:
778
779.. code-block:: text
780
781 > prompt
782 > show ssl stats all
783..
784
Bertrand Jacquin874a35c2018-09-10 21:26:07 +0100785 Then, arg1, arg2 and arg3 will contains respectively "show", "ssl" and "stats".
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +0100786 arg4 will contain "all". arg5 contains nil.
787
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100788.. js:function:: core.set_nice(nice)
789
790 **context**: task, action, sample-fetch, converter
791
792 Change the nice of the current task or current session.
Thierry FOURNIER2e4893c2015-03-18 13:37:27 +0100793
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100794 :param integer nice: the nice value, it must be between -1024 and 1024.
795
796.. js:function:: core.set_map(filename, key, value)
797
798 **context**: init, task, action, sample-fetch, converter
799
Bertrand Jacquin874a35c2018-09-10 21:26:07 +0100800 Set the value *value* associated to the key *key* in the map referenced by
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100801 *filename*.
802
Thierry FOURNIERdc595002015-12-21 11:13:52 +0100803 :param string filename: the Map reference
804 :param string key: the key to set or replace
805 :param string value: the associated value
806
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100807.. js:function:: core.sleep(int seconds)
808
809 **context**: body, init, task, action
810
811 The `core.sleep()` functions stop the Lua execution between specified seconds.
812
813 :param integer seconds: the required seconds.
814
815.. js:function:: core.tcp()
816
817 **context**: init, task, action
818
819 This function returns a new object of a *socket* class.
820
Thierry FOURNIERdc595002015-12-21 11:13:52 +0100821 :returns: A :ref:`socket_class` object.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100822
William Lallemand00a15022021-11-19 16:02:44 +0100823.. js:function:: core.httpclient()
824
825 **context**: init, task, action
826
827 This function returns a new object of a *httpclient* class.
828
829 :returns: A :ref:`httpclient_class` object.
830
Thierry Fournier1de16592016-01-27 09:49:07 +0100831.. js:function:: core.concat()
832
833 **context**: body, init, task, action, sample-fetch, converter
834
Bertrand Jacquin874a35c2018-09-10 21:26:07 +0100835 This function returns a new concat object.
Thierry Fournier1de16592016-01-27 09:49:07 +0100836
837 :returns: A :ref:`concat_class` object.
838
Thierry FOURNIER0a99b892015-08-26 00:14:17 +0200839.. js:function:: core.done(data)
840
841 **context**: body, init, task, action, sample-fetch, converter
842
843 :param any data: Return some data for the caller. It is useful with
844 sample-fetches and sample-converters.
845
846 Immediately stops the current Lua execution and returns to the caller which
847 may be a sample fetch, a converter or an action and returns the specified
Thierry Fournier4234dbd2020-11-28 13:18:23 +0100848 value (ignored for actions and init). It is used when the LUA process finishes
849 its work and wants to give back the control to HAProxy without executing the
Thierry FOURNIER0a99b892015-08-26 00:14:17 +0200850 remaining code. It can be seen as a multi-level "return".
851
Thierry FOURNIER486f5a02015-03-16 15:13:03 +0100852.. js:function:: core.yield()
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100853
854 **context**: task, action, sample-fetch, converter
855
856 Give back the hand at the HAProxy scheduler. It is used when the LUA
857 processing consumes a lot of processing time.
858
Thierry FOURNIER / OZON.IO62fec752016-11-10 20:38:11 +0100859.. js:function:: core.parse_addr(address)
860
861 **context**: body, init, task, action, sample-fetch, converter
862
863 :param network: is a string describing an ipv4 or ipv6 address and optionally
864 its network length, like this: "127.0.0.1/8" or "aaaa::1234/32".
865 :returns: a userdata containing network or nil if an error occurs.
866
Bertrand Jacquin874a35c2018-09-10 21:26:07 +0100867 Parse ipv4 or ipv6 addresses and its facultative associated network.
Thierry FOURNIER / OZON.IO62fec752016-11-10 20:38:11 +0100868
869.. js:function:: core.match_addr(addr1, addr2)
870
871 **context**: body, init, task, action, sample-fetch, converter
872
873 :param addr1: is an address created with "core.parse_addr".
874 :param addr2: is an address created with "core.parse_addr".
Bertrand Jacquin874a35c2018-09-10 21:26:07 +0100875 :returns: boolean, true if the network of the addresses match, else returns
Thierry FOURNIER / OZON.IO62fec752016-11-10 20:38:11 +0100876 false.
877
Ilya Shipitsin2075ca82020-03-06 23:22:22 +0500878 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 +0100879 of network is not important.
880
Thierry FOURNIER / OZON.IO8a1027a2016-11-24 20:48:38 +0100881.. js:function:: core.tokenize(str, separators [, noblank])
882
883 **context**: body, init, task, action, sample-fetch, converter
884
885 This function is useful for tokenizing an entry, or splitting some messages.
886 :param string str: The string which will be split.
887 :param string separators: A string containing a list of separators.
888 :param boolean noblank: Ignore empty entries.
889 :returns: an array of string.
890
891 For example:
892
893.. code-block:: lua
894
895 local array = core.tokenize("This function is useful, for tokenizing an entry.", "., ", true)
896 print_r(array)
897..
898
899 Returns this array:
900
901.. code-block:: text
902
903 (table) table: 0x21c01e0 [
904 1: (string) "This"
905 2: (string) "function"
906 3: (string) "is"
907 4: (string) "useful"
908 5: (string) "for"
909 6: (string) "tokenizing"
910 7: (string) "an"
911 8: (string) "entry"
912 ]
913..
914
Thierry Fournierf61aa632016-02-19 20:56:00 +0100915.. _proxy_class:
916
917Proxy class
918============
919
920.. js:class:: Proxy
921
922 This class provides a way for manipulating proxy and retrieving information
923 like statistics.
924
Thierry FOURNIER817e7592017-07-24 14:35:04 +0200925.. js:attribute:: Proxy.name
926
927 Contain the name of the proxy.
928
Aurelien DARRAGONc4b24372023-03-02 12:00:06 +0100929 .. warning::
930 This attribute is now deprecated and will eventually be removed.
931 Please use :js:func:`Proxy.get_name()` function instead.
932
Thierry Fournierb0467732022-10-07 12:07:24 +0200933.. js:function:: Proxy.get_name()
934
935 Returns the name of the proxy.
936
Baptiste Assmann46c72552017-10-26 21:51:58 +0200937.. js:attribute:: Proxy.uuid
938
939 Contain the unique identifier of the proxy.
940
Aurelien DARRAGONc4b24372023-03-02 12:00:06 +0100941 .. warning::
942 This attribute is now deprecated and will eventually be removed.
943 Please use :js:func:`Proxy.get_uuid()` function instead.
944
Thierry Fournierb0467732022-10-07 12:07:24 +0200945.. js:function:: Proxy.get_uuid()
946
947 Returns the unique identifier of the proxy.
948
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +0100949.. js:attribute:: Proxy.servers
950
Patrick Hemmerc6a1d712018-05-01 21:30:41 -0400951 Contain a table with the attached servers. The table is indexed by server
952 name, and each server entry is an object of type :ref:`server_class`.
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +0100953
Adis Nezirovic8878f8e2018-07-13 12:18:33 +0200954.. js:attribute:: Proxy.stktable
955
956 Contains a stick table object attached to the proxy.
957
Thierry Fournierff480422016-02-25 08:36:46 +0100958.. js:attribute:: Proxy.listeners
959
Patrick Hemmerc6a1d712018-05-01 21:30:41 -0400960 Contain a table with the attached listeners. The table is indexed by listener
961 name, and each each listeners entry is an object of type
962 :ref:`listener_class`.
Thierry Fournierff480422016-02-25 08:36:46 +0100963
Thierry Fournierf61aa632016-02-19 20:56:00 +0100964.. js:function:: Proxy.pause(px)
965
966 Pause the proxy. See the management socket documentation for more information.
967
968 :param class_proxy px: A :ref:`proxy_class` which indicates the manipulated
969 proxy.
970
971.. js:function:: Proxy.resume(px)
972
973 Resume the proxy. See the management socket documentation for more
974 information.
975
976 :param class_proxy px: A :ref:`proxy_class` which indicates the manipulated
977 proxy.
978
979.. js:function:: Proxy.stop(px)
980
981 Stop the proxy. See the management socket documentation for more information.
982
983 :param class_proxy px: A :ref:`proxy_class` which indicates the manipulated
984 proxy.
985
986.. js:function:: Proxy.shut_bcksess(px)
987
988 Kill the session attached to a backup server. See the management socket
989 documentation for more information.
990
991 :param class_proxy px: A :ref:`proxy_class` which indicates the manipulated
992 proxy.
993
994.. js:function:: Proxy.get_cap(px)
995
996 Returns a string describing the capabilities of the proxy.
997
998 :param class_proxy px: A :ref:`proxy_class` which indicates the manipulated
999 proxy.
1000 :returns: a string "frontend", "backend", "proxy" or "ruleset".
1001
1002.. js:function:: Proxy.get_mode(px)
1003
1004 Returns a string describing the mode of the current proxy.
1005
1006 :param class_proxy px: A :ref:`proxy_class` which indicates the manipulated
1007 proxy.
1008 :returns: a string "tcp", "http", "health" or "unknown"
1009
1010.. js:function:: Proxy.get_stats(px)
1011
Bertrand Jacquin874a35c2018-09-10 21:26:07 +01001012 Returns a table containing the proxy statistics. The statistics returned are
Thierry Fournierf61aa632016-02-19 20:56:00 +01001013 not the same if the proxy is frontend or a backend.
1014
1015 :param class_proxy px: A :ref:`proxy_class` which indicates the manipulated
1016 proxy.
Patrick Hemmerc6a1d712018-05-01 21:30:41 -04001017 :returns: a key/value table containing stats
Thierry Fournierf61aa632016-02-19 20:56:00 +01001018
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001019.. _server_class:
1020
1021Server class
1022============
1023
Patrick Hemmerc6a1d712018-05-01 21:30:41 -04001024.. js:class:: Server
1025
1026 This class provides a way for manipulating servers and retrieving information.
1027
Patrick Hemmera62ae7e2018-04-29 14:23:48 -04001028.. js:attribute:: Server.name
1029
1030 Contain the name of the server.
1031
Aurelien DARRAGONc4b24372023-03-02 12:00:06 +01001032 .. warning::
1033 This attribute is now deprecated and will eventually be removed.
1034 Please use :js:func:`Server.get_name()` function instead.
1035
Thierry Fournierb0467732022-10-07 12:07:24 +02001036.. js:function:: Server.get_name(sv)
1037
1038 Returns the name of the server.
1039
Patrick Hemmera62ae7e2018-04-29 14:23:48 -04001040.. js:attribute:: Server.puid
1041
1042 Contain the proxy unique identifier of the server.
1043
Aurelien DARRAGONc4b24372023-03-02 12:00:06 +01001044 .. warning::
1045 This attribute is now deprecated and will eventually be removed.
1046 Please use :js:func:`Server.get_puid()` function instead.
1047
Thierry Fournierb0467732022-10-07 12:07:24 +02001048.. js:function:: Server.get_puid(sv)
1049
1050 Returns the proxy unique identifier of the server.
1051
Aurelien DARRAGON94ee6632023-03-10 15:11:27 +01001052.. js:function:: Server.get_rid(sv)
1053
1054 Returns the rid (revision ID) of the server.
1055 It is an unsigned integer that is set upon server creation. Value is derived
1056 from a global counter that starts at 0 and is incremented each time one or
1057 multiple server deletions are followed by a server addition (meaning that
1058 old name/id reuse could occur).
1059
1060 Combining server name/id with server rid yields a process-wide unique
1061 identifier.
1062
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001063.. js:function:: Server.is_draining(sv)
1064
Patrick Hemmerc6a1d712018-05-01 21:30:41 -04001065 Return true if the server is currently draining sticky connections.
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001066
1067 :param class_server sv: A :ref:`server_class` which indicates the manipulated
1068 server.
1069 :returns: a boolean
1070
Patrick Hemmer32d539f2018-04-29 14:25:46 -04001071.. js:function:: Server.set_maxconn(sv, weight)
1072
1073 Dynamically change the maximum connections of the server. See the management
1074 socket documentation for more information about the format of the string.
1075
1076 :param class_server sv: A :ref:`server_class` which indicates the manipulated
1077 server.
1078 :param string maxconn: A string describing the server maximum connections.
1079
1080.. js:function:: Server.get_maxconn(sv, weight)
1081
1082 This function returns an integer representing the server maximum connections.
1083
1084 :param class_server sv: A :ref:`server_class` which indicates the manipulated
1085 server.
1086 :returns: an integer.
1087
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001088.. js:function:: Server.set_weight(sv, weight)
1089
Patrick Hemmerc6a1d712018-05-01 21:30:41 -04001090 Dynamically change the weight of the server. See the management socket
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001091 documentation for more information about the format of the string.
1092
1093 :param class_server sv: A :ref:`server_class` which indicates the manipulated
1094 server.
1095 :param string weight: A string describing the server weight.
1096
1097.. js:function:: Server.get_weight(sv)
1098
Patrick Hemmerc6a1d712018-05-01 21:30:41 -04001099 This function returns an integer representing the server weight.
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001100
1101 :param class_server sv: A :ref:`server_class` which indicates the manipulated
1102 server.
1103 :returns: an integer.
1104
Joseph C. Sible49bbf522020-05-04 22:20:32 -04001105.. js:function:: Server.set_addr(sv, addr[, port])
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001106
Patrick Hemmerc6a1d712018-05-01 21:30:41 -04001107 Dynamically change the address of the server. See the management socket
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001108 documentation for more information about the format of the string.
1109
1110 :param class_server sv: A :ref:`server_class` which indicates the manipulated
1111 server.
Patrick Hemmerc6a1d712018-05-01 21:30:41 -04001112 :param string addr: A string describing the server address.
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001113
1114.. js:function:: Server.get_addr(sv)
1115
Patrick Hemmerc6a1d712018-05-01 21:30:41 -04001116 Returns a string describing the address of the server.
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001117
1118 :param class_server sv: A :ref:`server_class` which indicates the manipulated
1119 server.
1120 :returns: A string
1121
1122.. js:function:: Server.get_stats(sv)
1123
1124 Returns server statistics.
1125
1126 :param class_server sv: A :ref:`server_class` which indicates the manipulated
1127 server.
Patrick Hemmerc6a1d712018-05-01 21:30:41 -04001128 :returns: a key/value table containing stats
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001129
1130.. js:function:: Server.shut_sess(sv)
1131
1132 Shutdown all the sessions attached to the server. See the management socket
1133 documentation for more information about this function.
1134
1135 :param class_server sv: A :ref:`server_class` which indicates the manipulated
1136 server.
1137
1138.. js:function:: Server.set_drain(sv)
1139
1140 Drain sticky sessions. See the management socket documentation for more
1141 information about this function.
1142
1143 :param class_server sv: A :ref:`server_class` which indicates the manipulated
1144 server.
1145
1146.. js:function:: Server.set_maint(sv)
1147
1148 Set maintenance mode. See the management socket documentation for more
1149 information about this function.
1150
1151 :param class_server sv: A :ref:`server_class` which indicates the manipulated
1152 server.
1153
1154.. js:function:: Server.set_ready(sv)
1155
1156 Set normal mode. See the management socket documentation for more information
1157 about this function.
1158
1159 :param class_server sv: A :ref:`server_class` which indicates the manipulated
1160 server.
1161
1162.. js:function:: Server.check_enable(sv)
1163
1164 Enable health checks. See the management socket documentation for more
1165 information about this function.
1166
1167 :param class_server sv: A :ref:`server_class` which indicates the manipulated
1168 server.
1169
1170.. js:function:: Server.check_disable(sv)
1171
1172 Disable health checks. See the management socket documentation for more
1173 information about this function.
1174
1175 :param class_server sv: A :ref:`server_class` which indicates the manipulated
1176 server.
1177
1178.. js:function:: Server.check_force_up(sv)
1179
1180 Force health-check up. See the management socket documentation for more
1181 information about this function.
1182
1183 :param class_server sv: A :ref:`server_class` which indicates the manipulated
1184 server.
1185
1186.. js:function:: Server.check_force_nolb(sv)
1187
1188 Force health-check nolb mode. See the management socket documentation for more
1189 information about this function.
1190
1191 :param class_server sv: A :ref:`server_class` which indicates the manipulated
1192 server.
1193
1194.. js:function:: Server.check_force_down(sv)
1195
1196 Force health-check down. See the management socket documentation for more
1197 information about this function.
1198
1199 :param class_server sv: A :ref:`server_class` which indicates the manipulated
1200 server.
1201
1202.. js:function:: Server.agent_enable(sv)
1203
1204 Enable agent check. See the management socket documentation for more
1205 information about this function.
1206
1207 :param class_server sv: A :ref:`server_class` which indicates the manipulated
1208 server.
1209
1210.. js:function:: Server.agent_disable(sv)
1211
1212 Disable agent check. See the management socket documentation for more
1213 information about this function.
1214
1215 :param class_server sv: A :ref:`server_class` which indicates the manipulated
1216 server.
1217
1218.. js:function:: Server.agent_force_up(sv)
1219
1220 Force agent check up. See the management socket documentation for more
1221 information about this function.
1222
1223 :param class_server sv: A :ref:`server_class` which indicates the manipulated
1224 server.
1225
1226.. js:function:: Server.agent_force_down(sv)
1227
1228 Force agent check down. See the management socket documentation for more
1229 information about this function.
1230
1231 :param class_server sv: A :ref:`server_class` which indicates the manipulated
1232 server.
1233
Thierry Fournierff480422016-02-25 08:36:46 +01001234.. _listener_class:
1235
1236Listener class
1237==============
1238
1239.. js:function:: Listener.get_stats(ls)
1240
1241 Returns server statistics.
1242
1243 :param class_listener ls: A :ref:`listener_class` which indicates the
1244 manipulated listener.
Patrick Hemmerc6a1d712018-05-01 21:30:41 -04001245 :returns: a key/value table containing stats
Thierry Fournierff480422016-02-25 08:36:46 +01001246
Thierry Fournier1de16592016-01-27 09:49:07 +01001247.. _concat_class:
1248
1249Concat class
1250============
1251
1252.. js:class:: Concat
1253
1254 This class provides a fast way for string concatenation. The way using native
1255 Lua concatenation like the code below is slow for some reasons.
1256
1257.. code-block:: lua
1258
1259 str = "string1"
1260 str = str .. ", string2"
1261 str = str .. ", string3"
1262..
1263
1264 For each concatenation, Lua:
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02001265 - allocates memory for the result,
1266 - catenates the two string copying the strings in the new memory block,
1267 - frees the old memory block containing the string which is no longer used.
1268
Thierry Fournier1de16592016-01-27 09:49:07 +01001269 This process does many memory move, allocation and free. In addition, the
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02001270 memory is not really freed, it is just marked as unused and waits for the
Thierry Fournier1de16592016-01-27 09:49:07 +01001271 garbage collector.
1272
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02001273 The Concat class provides an alternative way to concatenate strings. It uses
Thierry Fournier1de16592016-01-27 09:49:07 +01001274 the internal Lua mechanism (it does not allocate memory), but it doesn't copy
1275 the data more than once.
1276
1277 On my computer, the following loops spends 0.2s for the Concat method and
1278 18.5s for the pure Lua implementation. So, the Concat class is about 1000x
1279 faster than the embedded solution.
1280
1281.. code-block:: lua
1282
1283 for j = 1, 100 do
1284 c = core.concat()
1285 for i = 1, 20000 do
1286 c:add("#####")
1287 end
1288 end
1289..
1290
1291.. code-block:: lua
1292
1293 for j = 1, 100 do
1294 c = ""
1295 for i = 1, 20000 do
1296 c = c .. "#####"
1297 end
1298 end
1299..
1300
1301.. js:function:: Concat.add(concat, string)
1302
1303 This function adds a string to the current concatenated string.
1304
1305 :param class_concat concat: A :ref:`concat_class` which contains the currently
Ilya Shipitsin2075ca82020-03-06 23:22:22 +05001306 built string.
Bertrand Jacquin874a35c2018-09-10 21:26:07 +01001307 :param string string: A new string to concatenate to the current built
Thierry Fournier1de16592016-01-27 09:49:07 +01001308 string.
1309
1310.. js:function:: Concat.dump(concat)
1311
Bertrand Jacquin874a35c2018-09-10 21:26:07 +01001312 This function returns the concatenated string.
Thierry Fournier1de16592016-01-27 09:49:07 +01001313
1314 :param class_concat concat: A :ref:`concat_class` which contains the currently
Ilya Shipitsin2075ca82020-03-06 23:22:22 +05001315 built string.
Thierry Fournier1de16592016-01-27 09:49:07 +01001316 :returns: the concatenated string
1317
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001318.. _fetches_class:
1319
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001320Fetches class
1321=============
1322
1323.. js:class:: Fetches
1324
1325 This class contains a lot of internal HAProxy sample fetches. See the
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02001326 HAProxy "configuration.txt" documentation for more information.
1327 (chapters 7.3.2 to 7.3.6)
Thierry FOURNIER2e4893c2015-03-18 13:37:27 +01001328
Christopher Faulet1e9b1b62021-08-11 10:14:30 +02001329 .. warning::
1330 some sample fetches are not available in some context. These limitations
1331 are specified in this documentation when they're useful.
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001332
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01001333 :see: :js:attr:`TXN.f`
1334 :see: :js:attr:`TXN.sf`
Thierry FOURNIER2e4893c2015-03-18 13:37:27 +01001335
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02001336 Fetches are useful to:
Thierry FOURNIER2e4893c2015-03-18 13:37:27 +01001337
1338 * get system time,
1339 * get environment variable,
1340 * get random numbers,
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02001341 * know backend status like the number of users in queue or the number of
Thierry FOURNIER2e4893c2015-03-18 13:37:27 +01001342 connections established,
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02001343 * get client information like ip source or destination,
Thierry FOURNIER2e4893c2015-03-18 13:37:27 +01001344 * deal with stick tables,
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02001345 * fetch established SSL information,
1346 * fetch HTTP information like headers or method.
Thierry FOURNIER2e4893c2015-03-18 13:37:27 +01001347
1348.. code-block:: lua
1349
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001350 function action(txn)
1351 -- Get source IP
1352 local clientip = txn.f:src()
1353 end
Thierry FOURNIER2e4893c2015-03-18 13:37:27 +01001354..
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001355
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001356.. _converters_class:
1357
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001358Converters class
1359================
1360
1361.. js:class:: Converters
1362
1363 This class contains a lot of internal HAProxy sample converters. See the
Thierry FOURNIER2e4893c2015-03-18 13:37:27 +01001364 HAProxy documentation "configuration.txt" for more information about her
1365 usage. Its the chapter 7.3.1.
1366
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01001367 :see: :js:attr:`TXN.c`
1368 :see: :js:attr:`TXN.sc`
Thierry FOURNIER2e4893c2015-03-18 13:37:27 +01001369
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02001370 Converters provides stateful transformation. They are useful to:
Thierry FOURNIER2e4893c2015-03-18 13:37:27 +01001371
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02001372 * convert input to base64,
1373 * apply hash on input string (djb2, crc32, sdbm, wt6),
Thierry FOURNIER2e4893c2015-03-18 13:37:27 +01001374 * format date,
1375 * json escape,
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02001376 * extract preferred language comparing two lists,
Thierry FOURNIER2e4893c2015-03-18 13:37:27 +01001377 * turn to lower or upper chars,
1378 * deal with stick tables.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001379
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001380.. _channel_class:
1381
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001382Channel class
1383=============
1384
1385.. js:class:: Channel
1386
Christopher Faulet5a2c6612021-08-15 20:35:25 +02001387 **context**: action, sample-fetch, convert, filter
1388
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001389 HAProxy uses two buffers for the processing of the requests. The first one is
1390 used with the request data (from the client to the server) and the second is
1391 used for the response data (from the server to the client).
1392
1393 Each buffer contains two types of data. The first type is the incoming data
1394 waiting for a processing. The second part is the outgoing data already
1395 processed. Usually, the incoming data is processed, after it is tagged as
1396 outgoing data, and finally it is sent. The following functions provides tools
1397 for manipulating these data in a buffer.
1398
1399 The following diagram shows where the channel class function are applied.
1400
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001401 .. image:: _static/channel.png
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001402
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001403 .. warning::
1404 It is not possible to read from the response in request action, and it is
Boyang Li60cfe8b2022-05-10 18:11:00 +00001405 not possible to read from the request channel in response action.
Christopher Faulet09530392021-06-14 11:43:18 +02001406
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001407 .. warning::
1408 It is forbidden to alter the Channels buffer from HTTP contexts. So only
1409 :js:func:`Channel.input`, :js:func:`Channel.output`,
1410 :js:func:`Channel.may_recv`, :js:func:`Channel.is_full` and
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02001411 :js:func:`Channel.is_resp` can be called from a HTTP context.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001412
Christopher Faulet5a2c6612021-08-15 20:35:25 +02001413 All the functions provided by this class are available in the
1414 **sample-fetches**, **actions** and **filters** contexts. For **filters**,
1415 incoming data (offset and length) are relative to the filter. Some functions
Boyang Li60cfe8b2022-05-10 18:11:00 +00001416 may yield, but only for **actions**. Yield is not possible for
Christopher Faulet5a2c6612021-08-15 20:35:25 +02001417 **sample-fetches**, **converters** and **filters**.
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001418
1419.. js:function:: Channel.append(channel, string)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001420
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001421 This function copies the string **string** at the end of incoming data of the
1422 channel buffer. The function returns the copied length on success or -1 if
1423 data cannot be copied.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001424
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001425 Same that :js:func:`Channel.insert(channel, string, channel:input())`.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001426
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001427 :param class_channel channel: The manipulated Channel.
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02001428 :param string string: The data to copy at the end of incoming data.
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001429 :returns: an integer containing the amount of bytes copied or -1.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001430
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001431.. js:function:: Channel.data(channel [, offset [, length]])
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001432
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001433 This function returns **length** bytes of incoming data from the channel
1434 buffer, starting at the offset **offset**. The data are not removed from the
1435 buffer.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001436
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001437 By default, if no length is provided, all incoming data found, starting at the
1438 given offset, are returned. If **length** is set to -1, the function tries to
Christopher Faulet5a2c6612021-08-15 20:35:25 +02001439 retrieve a maximum of data and, if called by an action, it yields if
1440 necessary. It also waits for more data if the requested length exceeds the
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02001441 available amount of incoming data. Not providing an offset is the same as
Ilya Shipitsinff0f2782021-08-22 22:18:07 +05001442 setting it to 0. A positive offset is relative to the beginning of incoming
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02001443 data of the channel buffer while negative offset is relative to the end.
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001444
1445 If there is no incoming data and the channel can't receive more data, a 'nil'
1446 value is returned.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001447
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001448 :param class_channel channel: The manipulated Channel.
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001449 :param integer offset: *optional* The offset in incoming data to start to get
1450 data. 0 by default. May be negative to be relative to
1451 the end of incoming data.
1452 :param integer length: *optional* The expected length of data to retrieve. All
1453 incoming data by default. May be set to -1 to get a
1454 maximum of data.
1455 :returns: a string containing the data found or nil.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001456
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001457.. js:function:: Channel.forward(channel, length)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001458
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001459 This function forwards **length** bytes of data from the channel buffer. If
Christopher Faulet5a2c6612021-08-15 20:35:25 +02001460 the requested length exceeds the available amount of incoming data, and if
1461 called by an action, the function yields, waiting for more data to forward. It
1462 returns the amount of data forwarded.
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001463
1464 :param class_channel channel: The manipulated Channel.
1465 :param integer int: The amount of data to forward.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001466
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001467.. js:function:: Channel.input(channel)
1468
Christopher Faulet5a2c6612021-08-15 20:35:25 +02001469 This function returns the length of incoming data in the channel buffer. When
1470 called by a filter, this value is relative to the filter.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001471
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001472 :param class_channel channel: The manipulated Channel.
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001473 :returns: an integer containing the amount of available bytes.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001474
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001475.. js:function:: Channel.insert(channel, string [, offset])
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001476
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001477 This function copies the string **string** at the offset **offset** in
1478 incoming data of the channel buffer. The function returns the copied length on
1479 success or -1 if data cannot be copied.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001480
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001481 By default, if no offset is provided, the string is copied in front of
Ilya Shipitsinff0f2782021-08-22 22:18:07 +05001482 incoming data. A positive offset is relative to the beginning of incoming data
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001483 of the channel buffer while negative offset is relative to their end.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001484
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001485 :param class_channel channel: The manipulated Channel.
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02001486 :param string string: The data to copy into incoming data.
1487 :param integer offset: *optional* The offset in incoming data where to copy
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001488 data. 0 by default. May be negative to be relative to
1489 the end of incoming data.
Pieter Baauw4d7f7662015-11-08 16:38:08 +01001490 :returns: an integer containing the amount of bytes copied or -1.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001491
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001492.. js:function:: Channel.is_full(channel)
1493
1494 This function returns true if the channel buffer is full.
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.is_resp(channel)
1500
1501 This function returns true if the channel is the response one.
1502
Christopher Faulet5a2c6612021-08-15 20:35:25 +02001503 :param class_channel channel: The manipulated Channel.
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001504 :returns: a boolean
1505
1506.. js:function:: Channel.line(channel [, offset [, length]])
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001507
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001508 This function parses **length** bytes of incoming data of the channel buffer,
1509 starting at offset **offset**, and returns the first line found, including the
Christopher Faulet5a2c6612021-08-15 20:35:25 +02001510 '\\n'. The data are not removed from the buffer. If no line is found, all data
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001511 are returned.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001512
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001513 By default, if no length is provided, all incoming data, starting at the given
1514 offset, are evaluated. If **length** is set to -1, the function tries to
Christopher Faulet5a2c6612021-08-15 20:35:25 +02001515 retrieve a maximum of data and, if called by an action, yields if
1516 necessary. It also waits for more data if the requested length exceeds the
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02001517 available amount of incoming data. Not providing an offset is the same as
Ilya Shipitsinff0f2782021-08-22 22:18:07 +05001518 setting it to 0. A positive offset is relative to the beginning of incoming
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02001519 data of the channel buffer while negative offset is relative to the end.
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001520
1521 If there is no incoming data and the channel can't receive more data, a 'nil'
1522 value is returned.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001523
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001524 :param class_channel channel: The manipulated Channel.
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02001525 :param integer offset: *optional* The offset in incoming data to start to
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001526 parse data. 0 by default. May be negative to be
1527 relative to the end of incoming data.
1528 :param integer length: *optional* The length of data to parse. All incoming
1529 data by default. May be set to -1 to get a maximum of
1530 data.
1531 :returns: a string containing the line found or nil.
1532
1533.. js:function:: Channel.may_recv(channel)
1534
1535 This function returns true if the channel may still receive data.
1536
Christopher Faulet5a2c6612021-08-15 20:35:25 +02001537 :param class_channel channel: The manipulated Channel.
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001538 :returns: a boolean
1539
1540.. js:function:: Channel.output(channel)
1541
Christopher Faulet5a2c6612021-08-15 20:35:25 +02001542 This function returns the length of outgoing data of the channel buffer. When
1543 called by a filter, this value is relative to the filter.
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001544
1545 :param class_channel channel: The manipulated Channel.
1546 :returns: an integer containing the amount of available bytes.
1547
1548.. js:function:: Channel.prepend(channel, string)
1549
1550 This function copies the string **string** in front of incoming data of the
1551 channel buffer. The function returns the copied length on success or -1 if
1552 data cannot be copied.
1553
1554 Same that :js:func:`Channel.insert(channel, string, 0)`.
1555
1556 :param class_channel channel: The manipulated Channel.
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02001557 :param string string: The data to copy in front of incoming data.
Pieter Baauw4d7f7662015-11-08 16:38:08 +01001558 :returns: an integer containing the amount of bytes copied or -1.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001559
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001560.. js:function:: Channel.remove(channel [, offset [, length]])
1561
1562 This function removes **length** bytes of incoming data of the channel buffer,
1563 starting at offset **offset**. This function returns number of bytes removed
1564 on success.
1565
1566 By default, if no length is provided, all incoming data, starting at the given
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02001567 offset, are removed. Not providing an offset is the same as setting it
Ilya Shipitsinff0f2782021-08-22 22:18:07 +05001568 to 0. A positive offset is relative to the beginning of incoming data of the
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02001569 channel buffer while negative offset is relative to the end.
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001570
1571 :param class_channel channel: The manipulated Channel.
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02001572 :param integer offset: *optional* The offset in incoming data where to start
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001573 to remove data. 0 by default. May be negative to
1574 be relative to the end of incoming data.
1575 :param integer length: *optional* The length of data to remove. All incoming
1576 data by default.
1577 :returns: an integer containing the amount of bytes removed.
1578
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001579.. js:function:: Channel.send(channel, string)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001580
Christopher Faulet5a2c6612021-08-15 20:35:25 +02001581 This function requires immediate send of the string **string**. It means the
Ilya Shipitsinff0f2782021-08-22 22:18:07 +05001582 string is copied at the beginning of incoming data of the channel buffer and
Christopher Faulet5a2c6612021-08-15 20:35:25 +02001583 immediately forwarded. Unless if the connection is close, and if called by an
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02001584 action, this function yields to copy and forward all the string.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001585
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001586 :param class_channel channel: The manipulated Channel.
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001587 :param string string: The data to send.
Pieter Baauw4d7f7662015-11-08 16:38:08 +01001588 :returns: an integer containing the amount of bytes copied or -1.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001589
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001590.. js:function:: Channel.set(channel, string [, offset [, length]])
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001591
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02001592 This function replaces **length** bytes of incoming data of the channel buffer,
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001593 starting at offset **offset**, by the string **string**. The function returns
1594 the copied length on success or -1 if data cannot be copied.
1595
1596 By default, if no length is provided, all incoming data, starting at the given
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02001597 offset, are replaced. Not providing an offset is the same as setting it
Ilya Shipitsinff0f2782021-08-22 22:18:07 +05001598 to 0. A positive offset is relative to the beginning of incoming data of the
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02001599 channel buffer while negative offset is relative to the end.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001600
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001601 :param class_channel channel: The manipulated Channel.
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02001602 :param string string: The data to copy into incoming data.
1603 :param integer offset: *optional* The offset in incoming data where to start
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001604 the data replacement. 0 by default. May be negative to
1605 be relative to the end of incoming data.
1606 :param integer length: *optional* The length of data to replace. All incoming
1607 data by default.
1608 :returns: an integer containing the amount of bytes copied or -1.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001609
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001610.. js:function:: Channel.dup(channel)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001611
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001612 **DEPRECATED**
1613
1614 This function returns all incoming data found in the channel buffer. The data
Boyang Li60cfe8b2022-05-10 18:11:00 +00001615 are not removed from the buffer and can be reprocessed later.
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001616
1617 If there is no incoming data and the channel can't receive more data, a 'nil'
1618 value is returned.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001619
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001620 :param class_channel channel: The manipulated Channel.
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001621 :returns: a string containing all data found or nil.
1622
1623 .. warning::
1624 This function is deprecated. :js:func:`Channel.data()` must be used
1625 instead.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001626
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001627.. js:function:: Channel.get(channel)
1628
1629 **DEPRECATED**
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001630
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001631 This function returns all incoming data found in the channel buffer and remove
1632 them from the buffer.
1633
1634 If there is no incoming data and the channel can't receive more data, a 'nil'
1635 value is returned.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001636
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001637 :param class_channel channel: The manipulated Channel.
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001638 :returns: a string containing all the data found or nil.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001639
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001640 .. warning::
1641 This function is deprecated. :js:func:`Channel.data()` must be used to
1642 retrieve data followed by a call to :js:func:`Channel:remove()` to remove
1643 data.
Thierry FOURNIER / OZON.IO65192f32016-11-07 15:28:40 +01001644
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001645 .. code-block:: lua
Thierry FOURNIER / OZON.IO65192f32016-11-07 15:28:40 +01001646
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001647 local data = chn:data()
1648 chn:remove(0, data:len())
1649
1650 ..
1651
1652.. js:function:: Channel.getline(channel)
1653
1654 **DEPRECATED**
1655
1656 This function returns the first line found in incoming data of the channel
Christopher Faulet5a2c6612021-08-15 20:35:25 +02001657 buffer, including the '\\n'. The returned data are removed from the buffer. If
1658 no line is found, and if called by an action, this function yields to wait for
1659 more data, except if the channel can't receive more data. In this case all
1660 data are returned.
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001661
1662 If there is no incoming data and the channel can't receive more data, a 'nil'
1663 value is returned.
1664
1665 :param class_channel channel: The manipulated Channel.
1666 :returns: a string containing the line found or nil.
1667
1668 .. warning::
Boyang Li60cfe8b2022-05-10 18:11:00 +00001669 This function is deprecated. :js:func:`Channel.line()` must be used to
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001670 retrieve a line followed by a call to :js:func:`Channel:remove()` to remove
1671 data.
1672
1673 .. code-block:: lua
1674
1675 local line = chn:line(0, -1)
1676 chn:remove(0, line:len())
1677
1678 ..
1679
1680.. js:function:: Channel.get_in_len(channel)
1681
Boyang Li60cfe8b2022-05-10 18:11:00 +00001682 **DEPRECATED**
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001683
Christopher Faulet5a2c6612021-08-15 20:35:25 +02001684 This function returns the length of the input part of the buffer. When called
1685 by a filter, this value is relative to the filter.
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001686
1687 :param class_channel channel: The manipulated Channel.
1688 :returns: an integer containing the amount of available bytes.
1689
1690 .. warning::
1691 This function is deprecated. :js:func:`Channel.input()` must be used
1692 instead.
1693
1694.. js:function:: Channel.get_out_len(channel)
1695
Boyang Li60cfe8b2022-05-10 18:11:00 +00001696 **DEPRECATED**
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001697
Christopher Faulet5a2c6612021-08-15 20:35:25 +02001698 This function returns the length of the output part of the buffer. When called
1699 by a filter, this value is relative to the filter.
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001700
1701 :param class_channel channel: The manipulated Channel.
1702 :returns: an integer containing the amount of available bytes.
1703
1704 .. warning::
1705 This function is deprecated. :js:func:`Channel.output()` must be used
1706 instead.
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001707
1708.. _http_class:
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001709
1710HTTP class
1711==========
1712
1713.. js:class:: HTTP
1714
1715 This class contain all the HTTP manipulation functions.
1716
Pieter Baauw386a1272015-08-16 15:26:24 +02001717.. js:function:: HTTP.req_get_headers(http)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001718
Patrick Hemmerc6a1d712018-05-01 21:30:41 -04001719 Returns a table containing all the request headers.
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001720
1721 :param class_http http: The related http object.
Patrick Hemmerc6a1d712018-05-01 21:30:41 -04001722 :returns: table of headers.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01001723 :see: :js:func:`HTTP.res_get_headers`
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001724
Patrick Hemmerc6a1d712018-05-01 21:30:41 -04001725 This is the form of the returned table:
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001726
1727.. code-block:: lua
1728
1729 HTTP:req_get_headers()['<header-name>'][<header-index>] = "<header-value>"
1730
1731 local hdr = HTTP:req_get_headers()
1732 hdr["host"][0] = "www.test.com"
1733 hdr["accept"][0] = "audio/basic q=1"
1734 hdr["accept"][1] = "audio/*, q=0.2"
1735 hdr["accept"][2] = "*/*, q=0.1"
1736..
1737
Pieter Baauw386a1272015-08-16 15:26:24 +02001738.. js:function:: HTTP.res_get_headers(http)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001739
Patrick Hemmerc6a1d712018-05-01 21:30:41 -04001740 Returns a table containing all the response headers.
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001741
1742 :param class_http http: The related http object.
Patrick Hemmerc6a1d712018-05-01 21:30:41 -04001743 :returns: table of headers.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01001744 :see: :js:func:`HTTP.req_get_headers`
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001745
Patrick Hemmerc6a1d712018-05-01 21:30:41 -04001746 This is the form of the returned table:
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001747
1748.. code-block:: lua
1749
1750 HTTP:res_get_headers()['<header-name>'][<header-index>] = "<header-value>"
1751
1752 local hdr = HTTP:req_get_headers()
1753 hdr["host"][0] = "www.test.com"
1754 hdr["accept"][0] = "audio/basic q=1"
1755 hdr["accept"][1] = "audio/*, q=0.2"
1756 hdr["accept"][2] = "*.*, q=0.1"
1757..
1758
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001759.. js:function:: HTTP.req_add_header(http, name, value)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001760
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02001761 Appends a HTTP header field in the request whose name is
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001762 specified in "name" and whose value is defined in "value".
1763
1764 :param class_http http: The related http object.
1765 :param string name: The header name.
1766 :param string value: The header value.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01001767 :see: :js:func:`HTTP.res_add_header`
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001768
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001769.. js:function:: HTTP.res_add_header(http, name, value)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001770
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02001771 Appends a HTTP header field in the response whose name is
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001772 specified in "name" and whose value is defined in "value".
1773
1774 :param class_http http: The related http object.
1775 :param string name: The header name.
1776 :param string value: The header value.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01001777 :see: :js:func:`HTTP.req_add_header`
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001778
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001779.. js:function:: HTTP.req_del_header(http, name)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001780
1781 Removes all HTTP header fields in the request whose name is
1782 specified in "name".
1783
1784 :param class_http http: The related http object.
1785 :param string name: The header name.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01001786 :see: :js:func:`HTTP.res_del_header`
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001787
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001788.. js:function:: HTTP.res_del_header(http, name)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001789
1790 Removes all HTTP header fields in the response whose name is
1791 specified in "name".
1792
1793 :param class_http http: The related http object.
1794 :param string name: The header name.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01001795 :see: :js:func:`HTTP.req_del_header`
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001796
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001797.. js:function:: HTTP.req_set_header(http, name, value)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001798
Bertrand Jacquin874a35c2018-09-10 21:26:07 +01001799 This variable replace all occurrence of all header "name", by only
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001800 one containing the "value".
1801
1802 :param class_http http: The related http object.
1803 :param string name: The header name.
1804 :param string value: The header value.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01001805 :see: :js:func:`HTTP.res_set_header`
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001806
Bertrand Jacquin874a35c2018-09-10 21:26:07 +01001807 This function does the same work as the following code:
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001808
1809.. code-block:: lua
1810
1811 function fcn(txn)
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001812 TXN.http:req_del_header("header")
1813 TXN.http:req_add_header("header", "value")
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001814 end
1815..
1816
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001817.. js:function:: HTTP.res_set_header(http, name, value)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001818
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02001819 This function replaces all occurrence of all header "name", by only
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001820 one containing the "value".
1821
1822 :param class_http http: The related http object.
1823 :param string name: The header name.
1824 :param string value: The header value.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01001825 :see: :js:func:`HTTP.req_rep_header()`
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001826
Pieter Baauw386a1272015-08-16 15:26:24 +02001827.. js:function:: HTTP.req_rep_header(http, name, regex, replace)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001828
1829 Matches the regular expression in all occurrences of header field "name"
1830 according to "regex", and replaces them with the "replace" argument. The
1831 replacement value can contain back references like \1, \2, ... This
1832 function works with the request.
1833
1834 :param class_http http: The related http object.
1835 :param string name: The header name.
1836 :param string regex: The match regular expression.
1837 :param string replace: The replacement value.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01001838 :see: :js:func:`HTTP.res_rep_header()`
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001839
Pieter Baauw386a1272015-08-16 15:26:24 +02001840.. js:function:: HTTP.res_rep_header(http, name, regex, string)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001841
1842 Matches the regular expression in all occurrences of header field "name"
1843 according to "regex", and replaces them with the "replace" argument. The
1844 replacement value can contain back references like \1, \2, ... This
1845 function works with the request.
1846
1847 :param class_http http: The related http object.
1848 :param string name: The header name.
1849 :param string regex: The match regular expression.
1850 :param string replace: The replacement value.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01001851 :see: :js:func:`HTTP.req_rep_header()`
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001852
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001853.. js:function:: HTTP.req_set_method(http, method)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001854
1855 Rewrites the request method with the parameter "method".
1856
1857 :param class_http http: The related http object.
1858 :param string method: The new method.
1859
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001860.. js:function:: HTTP.req_set_path(http, path)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001861
1862 Rewrites the request path with the "path" parameter.
1863
1864 :param class_http http: The related http object.
1865 :param string path: The new path.
1866
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001867.. js:function:: HTTP.req_set_query(http, query)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001868
1869 Rewrites the request's query string which appears after the first question
1870 mark ("?") with the parameter "query".
1871
1872 :param class_http http: The related http object.
1873 :param string query: The new query.
1874
Thierry FOURNIER0d79cf62015-08-26 14:20:58 +02001875.. js:function:: HTTP.req_set_uri(http, uri)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001876
1877 Rewrites the request URI with the parameter "uri".
1878
1879 :param class_http http: The related http object.
1880 :param string uri: The new uri.
1881
Robin H. Johnson52f5db22017-01-01 13:10:52 -08001882.. js:function:: HTTP.res_set_status(http, status [, reason])
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +02001883
Robin H. Johnson52f5db22017-01-01 13:10:52 -08001884 Rewrites the response status code with the parameter "code".
1885
1886 If no custom reason is provided, it will be generated from the status.
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +02001887
1888 :param class_http http: The related http object.
1889 :param integer status: The new response status code.
Robin H. Johnson52f5db22017-01-01 13:10:52 -08001890 :param string reason: The new response reason (optional).
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +02001891
William Lallemand00a15022021-11-19 16:02:44 +01001892.. _httpclient_class:
1893
1894HTTPClient class
1895================
1896
1897.. js:class:: HTTPClient
1898
1899 The httpclient class allows issue of outbound HTTP requests through a simple
1900 API without the knowledge of HAProxy internals.
1901
1902.. js:function:: HTTPClient.get(httpclient, request)
1903.. js:function:: HTTPClient.head(httpclient, request)
1904.. js:function:: HTTPClient.put(httpclient, request)
1905.. js:function:: HTTPClient.post(httpclient, request)
1906.. js:function:: HTTPClient.delete(httpclient, request)
1907
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02001908 Send a HTTP request and wait for a response. GET, HEAD PUT, POST and DELETE methods can be used.
1909 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 +01001910
William Lallemanda9256192022-10-21 11:48:24 +02001911 The HTTPClient interface is not able to decompress responses, it is not
1912 recommended to send an Accept-Encoding in the request so the response is
1913 received uncompressed.
William Lallemand00a15022021-11-19 16:02:44 +01001914
1915 :param class httpclient: Is the manipulated HTTPClient.
1916 :param table request: Is a table containing the parameters of the request that will be send.
1917 :param string request.url: Is a mandatory parameter for the request that contains the URL.
1918 :param string request.body: Is an optional parameter for the request that contains the body to send.
1919 :param table request.headers: Is an optional parameter for the request that contains the headers to send.
William Lallemand18340302022-02-23 15:57:45 +01001920 :param string request.dst: Is an optional parameter for the destination in haproxy address format.
William Lallemandb4a4ef62022-02-23 14:18:16 +01001921 :param integer request.timeout: Optional timeout parameter, set a "timeout server" on the connections.
William Lallemand00a15022021-11-19 16:02:44 +01001922 :returns: Lua table containing the response
1923
1924
1925.. code-block:: lua
1926
1927 local httpclient = core.httpclient()
William Lallemand4f4f2b72022-02-17 20:00:23 +01001928 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 +01001929
1930..
1931
1932.. code-block:: lua
1933
1934 response = {
1935 status = 400,
1936 reason = "Bad request",
1937 headers = {
1938 ["content-type"] = { "text/html" },
1939 ["cache-control"] = { "no-cache", "no-store" },
1940 },
William Lallemand4f4f2b72022-02-17 20:00:23 +01001941 body = "<html><body><h1>invalid request<h1></body></html>",
William Lallemand00a15022021-11-19 16:02:44 +01001942 }
1943..
1944
1945
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001946.. _txn_class:
1947
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001948TXN class
1949=========
1950
1951.. js:class:: TXN
1952
1953 The txn class contain all the functions relative to the http or tcp
1954 transaction (Note than a tcp stream is the same than a tcp transaction, but
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02001955 a HTTP transaction is not the same than a tcp stream).
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001956
1957 The usage of this class permits to retrieve data from the requests, alter it
1958 and forward it.
1959
1960 All the functions provided by this class are available in the context
Christopher Faulet5a2c6612021-08-15 20:35:25 +02001961 **sample-fetches**, **actions** and **filters**.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001962
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001963.. js:attribute:: TXN.c
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001964
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001965 :returns: An :ref:`converters_class`.
1966
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001967 This attribute contains a Converters class object.
1968
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001969.. js:attribute:: TXN.sc
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001970
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001971 :returns: An :ref:`converters_class`.
1972
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001973 This attribute contains a Converters class object. The functions of
1974 this object returns always a string.
1975
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001976.. js:attribute:: TXN.f
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001977
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001978 :returns: An :ref:`fetches_class`.
1979
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001980 This attribute contains a Fetches class object.
1981
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001982.. js:attribute:: TXN.sf
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001983
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001984 :returns: An :ref:`fetches_class`.
1985
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001986 This attribute contains a Fetches class object. The functions of
1987 this object returns always a string.
1988
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001989.. js:attribute:: TXN.req
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001990
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001991 :returns: An :ref:`channel_class`.
1992
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001993 This attribute contains a channel class object for the request buffer.
1994
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001995.. js:attribute:: TXN.res
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001996
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001997 :returns: An :ref:`channel_class`.
1998
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001999 This attribute contains a channel class object for the response buffer.
2000
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002001.. js:attribute:: TXN.http
Thierry FOURNIER08504f42015-03-16 14:17:08 +01002002
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002003 :returns: An :ref:`http_class`.
2004
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02002005 This attribute contains a HTTP class object. It is available only if the
Thierry FOURNIER08504f42015-03-16 14:17:08 +01002006 proxy has the "mode http" enabled.
2007
Christopher Faulet5a2c6612021-08-15 20:35:25 +02002008.. js:attribute:: TXN.http_req
2009
2010 :returns: An :ref:`httpmessage_class`.
2011
2012 This attribute contains the request HTTPMessage class object. It is available
2013 only if the proxy has the "mode http" enabled and only in the **filters**
2014 context.
2015
2016.. js:attribute:: TXN.http_res
2017
2018 :returns: An :ref:`httpmessage_class`.
2019
2020 This attribute contains the response HTTPMessage class object. It is available
2021 only if the proxy has the "mode http" enabled and only in the **filters**
2022 context.
2023
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01002024.. js:function:: TXN.log(TXN, loglevel, msg)
2025
2026 This function sends a log. The log is sent, according with the HAProxy
2027 configuration file, on the default syslog server if it is configured and on
2028 the stderr if it is allowed.
2029
2030 :param class_txn txn: The class txn object containing the data.
Bertrand Jacquin874a35c2018-09-10 21:26:07 +01002031 :param integer loglevel: Is the log level associated with the message. It is a
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01002032 number between 0 and 7.
2033 :param string msg: The log content.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01002034 :see: :js:attr:`core.emerg`, :js:attr:`core.alert`, :js:attr:`core.crit`,
2035 :js:attr:`core.err`, :js:attr:`core.warning`, :js:attr:`core.notice`,
2036 :js:attr:`core.info`, :js:attr:`core.debug` (log level definitions)
2037 :see: :js:func:`TXN.deflog`
2038 :see: :js:func:`TXN.Debug`
2039 :see: :js:func:`TXN.Info`
2040 :see: :js:func:`TXN.Warning`
2041 :see: :js:func:`TXN.Alert`
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01002042
2043.. js:function:: TXN.deflog(TXN, msg)
2044
Bertrand Jacquin874a35c2018-09-10 21:26:07 +01002045 Sends a log line with the default loglevel for the proxy associated with the
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01002046 transaction.
2047
2048 :param class_txn txn: The class txn object containing the data.
2049 :param string msg: The log content.
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02002050 :see: :js:func:`TXN.log`
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01002051
2052.. js:function:: TXN.Debug(txn, msg)
2053
2054 :param class_txn txn: The class txn object containing the data.
2055 :param string msg: The log content.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01002056 :see: :js:func:`TXN.log`
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01002057
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02002058 Does the same job as:
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01002059
2060.. code-block:: lua
2061
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002062 function Debug(txn, msg)
2063 TXN.log(txn, core.debug, msg)
2064 end
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01002065..
2066
2067.. js:function:: TXN.Info(txn, msg)
2068
2069 :param class_txn txn: The class txn object containing the data.
2070 :param string msg: The log content.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01002071 :see: :js:func:`TXN.log`
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01002072
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02002073 Does the same job as:
2074
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01002075.. code-block:: lua
2076
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02002077 function Info(txn, msg)
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002078 TXN.log(txn, core.info, msg)
2079 end
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01002080..
2081
2082.. js:function:: TXN.Warning(txn, msg)
2083
2084 :param class_txn txn: The class txn object containing the data.
2085 :param string msg: The log content.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01002086 :see: :js:func:`TXN.log`
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01002087
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02002088 Does the same job as:
2089
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01002090.. code-block:: lua
2091
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02002092 function Warning(txn, msg)
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002093 TXN.log(txn, core.warning, msg)
2094 end
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01002095..
2096
2097.. js:function:: TXN.Alert(txn, msg)
2098
2099 :param class_txn txn: The class txn object containing the data.
2100 :param string msg: The log content.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01002101 :see: :js:func:`TXN.log`
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01002102
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02002103 Does the same job as:
2104
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01002105.. code-block:: lua
2106
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02002107 function Alert(txn, msg)
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002108 TXN.log(txn, core.alert, msg)
2109 end
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01002110..
2111
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002112.. js:function:: TXN.get_priv(txn)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002113
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002114 Return Lua data stored in the current transaction (with the `TXN.set_priv()`)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002115 function. If no data are stored, it returns a nil value.
2116
2117 :param class_txn txn: The class txn object containing the data.
Bertrand Jacquin874a35c2018-09-10 21:26:07 +01002118 :returns: the opaque data previously stored, or nil if nothing is
2119 available.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002120
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002121.. js:function:: TXN.set_priv(txn, data)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002122
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02002123 Store any data in the current HAProxy transaction. This action replaces the
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002124 old stored data.
2125
2126 :param class_txn txn: The class txn object containing the data.
2127 :param opaque data: The data which is stored in the transaction.
2128
Tim Duesterhus4e172c92020-05-19 13:49:42 +02002129.. js:function:: TXN.set_var(TXN, var, value[, ifexist])
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +02002130
David Carlier61fdf8b2015-10-02 11:59:38 +01002131 Converts a Lua type in a HAProxy type and store it in a variable <var>.
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +02002132
2133 :param class_txn txn: The class txn object containing the data.
2134 :param string var: The variable name according with the HAProxy variable syntax.
Thierry FOURNIER / OZON.IOb210bcc2016-12-12 16:24:16 +01002135 :param type value: The value associated to the variable. The type can be string or
2136 integer.
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02002137 :param boolean ifexist: If this parameter is set to true the variable
Tim Duesterhus4e172c92020-05-19 13:49:42 +02002138 will only be set if it was defined elsewhere (i.e. used
Willy Tarreau7978c5c2021-09-07 14:24:07 +02002139 within the configuration). For global variables (using the
2140 "proc" scope), they will only be updated and never created.
2141 It is highly recommended to always set this to true.
Christopher Faulet85d79c92016-11-09 16:54:56 +01002142
2143.. js:function:: TXN.unset_var(TXN, var)
2144
2145 Unset the variable <var>.
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.
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +02002149
2150.. js:function:: TXN.get_var(TXN, var)
2151
2152 Returns data stored in the variable <var> converter in Lua type.
2153
2154 :param class_txn txn: The class txn object containing the data.
2155 :param string var: The variable name according with the HAProxy variable syntax.
2156
Christopher Faulet700d9e82020-01-31 12:21:52 +01002157.. js:function:: TXN.reply([reply])
2158
2159 Return a new reply object
2160
2161 :param table reply: A table containing info to initialize the reply fields.
2162 :returns: A :ref:`reply_class` object.
2163
2164 The table used to initialized the reply object may contain following entries :
2165
2166 * status : The reply status code. the code 200 is used by default.
2167 * reason : The reply reason. The reason corresponding to the status code is
2168 used by default.
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02002169 * headers : A list of headers, indexed by header name. Empty by default. For
Christopher Faulet700d9e82020-01-31 12:21:52 +01002170 a given name, multiple values are possible, stored in an ordered list.
2171 * body : The reply body, empty by default.
2172
2173.. code-block:: lua
2174
2175 local reply = txn:reply{
2176 status = 400,
2177 reason = "Bad request",
2178 headers = {
2179 ["content-type"] = { "text/html" },
2180 ["cache-control"] = {"no-cache", "no-store" }
2181 },
2182 body = "<html><body><h1>invalid request<h1></body></html>"
2183 }
2184..
2185 :see: :js:class:`Reply`
2186
2187.. js:function:: TXN.done(txn[, reply])
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002188
Willy Tarreaubc183a62015-08-28 10:39:11 +02002189 This function terminates processing of the transaction and the associated
Christopher Faulet700d9e82020-01-31 12:21:52 +01002190 session and optionally reply to the client for HTTP sessions.
2191
2192 :param class_txn txn: The class txn object containing the data.
2193 :param class_reply reply: The class reply object to return to the client.
2194
2195 This functions can be used when a critical error is detected or to terminate
Willy Tarreaubc183a62015-08-28 10:39:11 +02002196 processing after some data have been returned to the client (eg: a redirect).
Christopher Faulet700d9e82020-01-31 12:21:52 +01002197 To do so, a reply may be provided. This object is optional and may contain a
2198 status code, a reason, a header list and a body. All these fields are
Christopher Faulet7855b192021-11-09 18:39:51 +01002199 optional. When not provided, the default values are used. By default, with an
2200 empty reply object, an empty HTTP 200 response is returned to the client. If
2201 no reply object is provided, the transaction is terminated without any
2202 reply. If a reply object is provided, it must not exceed the buffer size once
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02002203 converted into the internal HTTP representation. Because for now there is no
Christopher Faulet7855b192021-11-09 18:39:51 +01002204 easy way to be sure it fits, it is probably better to keep it reasonably
2205 small.
Christopher Faulet700d9e82020-01-31 12:21:52 +01002206
2207 The reply object may be fully created in lua or the class Reply may be used to
2208 create it.
2209
2210.. code-block:: lua
2211
2212 local reply = txn:reply()
2213 reply:set_status(400, "Bad request")
2214 reply:add_header("content-type", "text/html")
2215 reply:add_header("cache-control", "no-cache")
2216 reply:add_header("cache-control", "no-store")
2217 reply:set_body("<html><body><h1>invalid request<h1></body></html>")
2218 txn:done(reply)
2219..
2220
2221.. code-block:: lua
2222
2223 txn:done{
2224 status = 400,
2225 reason = "Bad request",
2226 headers = {
2227 ["content-type"] = { "text/html" },
2228 ["cache-control"] = { "no-cache", "no-store" },
2229 },
2230 body = "<html><body><h1>invalid request<h1></body></html>"
2231 }
2232..
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002233
Christopher Faulet1e9b1b62021-08-11 10:14:30 +02002234 .. warning::
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02002235 It does not make sense to call this function from sample-fetches. In this case
2236 the behavior is the same than core.done(): it finishes the Lua
Christopher Faulet1e9b1b62021-08-11 10:14:30 +02002237 execution. The transaction is really aborted only from an action registered
2238 function.
Thierry FOURNIERab00df62016-07-14 11:42:37 +02002239
Christopher Faulet700d9e82020-01-31 12:21:52 +01002240 :see: :js:func:`TXN.reply`, :js:class:`Reply`
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002241
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002242.. js:function:: TXN.set_loglevel(txn, loglevel)
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01002243
2244 Is used to change the log level of the current request. The "loglevel" must
2245 be an integer between 0 and 7.
2246
2247 :param class_txn txn: The class txn object containing the data.
2248 :param integer loglevel: The required log level. This variable can be one of
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01002249 :see: :js:attr:`core.emerg`, :js:attr:`core.alert`, :js:attr:`core.crit`,
2250 :js:attr:`core.err`, :js:attr:`core.warning`, :js:attr:`core.notice`,
2251 :js:attr:`core.info`, :js:attr:`core.debug` (log level definitions)
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01002252
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002253.. js:function:: TXN.set_tos(txn, tos)
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01002254
2255 Is used to set the TOS or DSCP field value of packets sent to the client to
2256 the value passed in "tos" on platforms which support this.
2257
2258 :param class_txn txn: The class txn object containing the data.
2259 :param integer tos: The new TOS os DSCP.
2260
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002261.. js:function:: TXN.set_mark(txn, mark)
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01002262
2263 Is used to set the Netfilter MARK on all packets sent to the client to the
2264 value passed in "mark" on platforms which support it.
2265
2266 :param class_txn txn: The class txn object containing the data.
2267 :param integer mark: The mark value.
2268
Patrick Hemmer268a7072018-05-11 12:52:31 -04002269.. js:function:: TXN.set_priority_class(txn, prio)
2270
2271 This function adjusts the priority class of the transaction. The value should
2272 be within the range -2047..2047. Values outside this range will be
2273 truncated.
2274
2275 See the HAProxy configuration.txt file keyword "http-request" action
2276 "set-priority-class" for details.
2277
2278.. js:function:: TXN.set_priority_offset(txn, prio)
2279
2280 This function adjusts the priority offset of the transaction. The value
2281 should be within the range -524287..524287. Values outside this range will be
2282 truncated.
2283
2284 See the HAProxy configuration.txt file keyword "http-request" action
2285 "set-priority-offset" for details.
2286
Christopher Faulet700d9e82020-01-31 12:21:52 +01002287.. _reply_class:
2288
2289Reply class
2290============
2291
2292.. js:class:: Reply
2293
2294 **context**: action
2295
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02002296 This class represents a HTTP response message. It provides some methods to
Christopher Faulet7855b192021-11-09 18:39:51 +01002297 enrich it. Once converted into the internal HTTP representation, the response
2298 message must not exceed the buffer size. Because for now there is no
2299 easy way to be sure it fits, it is probably better to keep it reasonably
2300 small.
2301
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02002302 See tune.bufsize in the configuration manual for details.
Christopher Faulet700d9e82020-01-31 12:21:52 +01002303
2304.. code-block:: lua
2305
2306 local reply = txn:reply({status = 400}) -- default HTTP 400 reason-phase used
2307 reply:add_header("content-type", "text/html")
2308 reply:add_header("cache-control", "no-cache")
2309 reply:add_header("cache-control", "no-store")
2310 reply:set_body("<html><body><h1>invalid request<h1></body></html>")
2311..
2312
2313 :see: :js:func:`TXN.reply`
2314
2315.. js:attribute:: Reply.status
2316
2317 The reply status code. By default, the status code is set to 200.
2318
2319 :returns: integer
2320
2321.. js:attribute:: Reply.reason
2322
2323 The reason string describing the status code.
2324
2325 :returns: string
2326
2327.. js:attribute:: Reply.headers
2328
2329 A table indexing all reply headers by name. To each name is associated an
2330 ordered list of values.
2331
2332 :returns: Lua table
2333
2334.. code-block:: lua
2335
2336 {
2337 ["content-type"] = { "text/html" },
2338 ["cache-control"] = {"no-cache", "no-store" },
2339 x_header_name = { "value1", "value2", ... }
2340 ...
2341 }
2342..
2343
2344.. js:attribute:: Reply.body
2345
2346 The reply payload.
2347
2348 :returns: string
2349
2350.. js:function:: Reply.set_status(REPLY, status[, reason])
2351
2352 Set the reply status code and optionally the reason-phrase. If the reason is
2353 not provided, the default reason corresponding to the status code is used.
2354
2355 :param class_reply reply: The related Reply object.
2356 :param integer status: The reply status code.
2357 :param string reason: The reply status reason (optional).
2358
2359.. js:function:: Reply.add_header(REPLY, name, value)
2360
2361 Add a header to the reply object. If the header does not already exist, a new
2362 entry is created with its name as index and a one-element list containing its
2363 value as value. Otherwise, the header value is appended to the ordered list of
2364 values associated to the header name.
2365
2366 :param class_reply reply: The related Reply object.
2367 :param string name: The header field name.
2368 :param string value: The header field value.
2369
2370.. js:function:: Reply.del_header(REPLY, name)
2371
2372 Remove all occurrences of a header name from the reply object.
2373
2374 :param class_reply reply: The related Reply object.
2375 :param string name: The header field name.
2376
2377.. js:function:: Reply.set_body(REPLY, body)
2378
2379 Set the reply payload.
2380
2381 :param class_reply reply: The related Reply object.
2382 :param string body: The reply payload.
2383
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002384.. _socket_class:
2385
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002386Socket class
2387============
2388
2389.. js:class:: Socket
2390
2391 This class must be compatible with the Lua Socket class. Only the 'client'
2392 functions are available. See the Lua Socket documentation:
2393
2394 `http://w3.impa.br/~diego/software/luasocket/tcp.html
2395 <http://w3.impa.br/~diego/software/luasocket/tcp.html>`_
2396
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002397.. js:function:: Socket.close(socket)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002398
2399 Closes a TCP object. The internal socket used by the object is closed and the
2400 local address to which the object was bound is made available to other
2401 applications. No further operations (except for further calls to the close
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002402 method) are allowed on a closed Socket.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002403
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002404 :param class_socket socket: Is the manipulated Socket.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002405
2406 Note: It is important to close all used sockets once they are not needed,
2407 since, in many systems, each socket uses a file descriptor, which are limited
2408 system resources. Garbage-collected objects are automatically closed before
2409 destruction, though.
2410
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002411.. js:function:: Socket.connect(socket, address[, port])
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002412
2413 Attempts to connect a socket object to a remote host.
2414
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002415
2416 In case of error, the method returns nil followed by a string describing the
2417 error. In case of success, the method returns 1.
2418
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002419 :param class_socket socket: Is the manipulated Socket.
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002420 :param string address: can be an IP address or a host name. See below for more
2421 information.
2422 :param integer port: must be an integer number in the range [1..64K].
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002423 :returns: 1 or nil.
2424
Bertrand Jacquin874a35c2018-09-10 21:26:07 +01002425 An address field extension permits to use the connect() function to connect to
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002426 other stream than TCP. The syntax containing a simpleipv4 or ipv6 address is
2427 the basically expected format. This format requires the port.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002428
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002429 Other format accepted are a socket path like "/socket/path", it permits to
Bertrand Jacquin874a35c2018-09-10 21:26:07 +01002430 connect to a socket. Abstract namespaces are supported with the prefix
Joseph Herlant02cedc42018-11-13 19:45:17 -08002431 "abns@", and finally a file descriptor can be passed with the prefix "fd@".
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002432 The prefix "ipv4@", "ipv6@" and "unix@" are also supported. The port can be
Bertrand Jacquin874a35c2018-09-10 21:26:07 +01002433 passed int the string. The syntax "127.0.0.1:1234" is valid. In this case, the
Tim Duesterhus6edab862018-01-06 19:04:45 +01002434 parameter *port* must not be set.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002435
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002436.. js:function:: Socket.connect_ssl(socket, address, port)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002437
2438 Same behavior than the function socket:connect, but uses SSL.
2439
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002440 :param class_socket socket: Is the manipulated Socket.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002441 :returns: 1 or nil.
2442
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002443.. js:function:: Socket.getpeername(socket)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002444
2445 Returns information about the remote side of a connected client object.
2446
2447 Returns a string with the IP address of the peer, followed by the port number
2448 that peer is using for the connection. In case of error, the method returns
2449 nil.
2450
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002451 :param class_socket socket: Is the manipulated Socket.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002452 :returns: a string containing the server information.
2453
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002454.. js:function:: Socket.getsockname(socket)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002455
2456 Returns the local address information associated to the object.
2457
2458 The method returns a string with local IP address and a number with the port.
2459 In case of error, the method returns nil.
2460
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002461 :param class_socket socket: Is the manipulated Socket.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002462 :returns: a string containing the client information.
2463
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002464.. js:function:: Socket.receive(socket, [pattern [, prefix]])
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002465
2466 Reads data from a client object, according to the specified read pattern.
2467 Patterns follow the Lua file I/O format, and the difference in performance
2468 between all patterns is negligible.
2469
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002470 :param class_socket socket: Is the manipulated Socket.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002471 :param string|integer pattern: Describe what is required (see below).
2472 :param string prefix: A string which will be prefix the returned data.
2473 :returns: a string containing the required data or nil.
2474
2475 Pattern can be any of the following:
2476
2477 * **`*a`**: reads from the socket until the connection is closed. No
2478 end-of-line translation is performed;
2479
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002480 * **`*l`**: reads a line of text from the Socket. The line is terminated by a
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002481 LF character (ASCII 10), optionally preceded by a CR character
2482 (ASCII 13). The CR and LF characters are not included in the
2483 returned line. In fact, all CR characters are ignored by the
2484 pattern. This is the default pattern.
2485
2486 * **number**: causes the method to read a specified number of bytes from the
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002487 Socket. Prefix is an optional string to be concatenated to the
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002488 beginning of any received data before return.
2489
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002490 * **empty**: If the pattern is left empty, the default option is `*l`.
2491
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002492 If successful, the method returns the received pattern. In case of error, the
2493 method returns nil followed by an error message which can be the string
2494 'closed' in case the connection was closed before the transmission was
2495 completed or the string 'timeout' in case there was a timeout during the
2496 operation. Also, after the error message, the function returns the partial
2497 result of the transmission.
2498
2499 Important note: This function was changed severely. It used to support
2500 multiple patterns (but I have never seen this feature used) and now it
2501 doesn't anymore. Partial results used to be returned in the same way as
2502 successful results. This last feature violated the idea that all functions
2503 should return nil on error. Thus it was changed too.
2504
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002505.. js:function:: Socket.send(socket, data [, start [, end ]])
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002506
2507 Sends data through client object.
2508
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002509 :param class_socket socket: Is the manipulated Socket.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002510 :param string data: The data that will be sent.
2511 :param integer start: The start position in the buffer of the data which will
2512 be sent.
2513 :param integer end: The end position in the buffer of the data which will
2514 be sent.
2515 :returns: see below.
2516
2517 Data is the string to be sent. The optional arguments i and j work exactly
2518 like the standard string.sub Lua function to allow the selection of a
2519 substring to be sent.
2520
2521 If successful, the method returns the index of the last byte within [start,
2522 end] that has been sent. Notice that, if start is 1 or absent, this is
2523 effectively the total number of bytes sent. In case of error, the method
2524 returns nil, followed by an error message, followed by the index of the last
2525 byte within [start, end] that has been sent. You might want to try again from
2526 the byte following that. The error message can be 'closed' in case the
2527 connection was closed before the transmission was completed or the string
2528 'timeout' in case there was a timeout during the operation.
2529
2530 Note: Output is not buffered. For small strings, it is always better to
2531 concatenate them in Lua (with the '..' operator) and send the result in one
2532 call instead of calling the method several times.
2533
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002534.. js:function:: Socket.setoption(socket, option [, value])
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002535
2536 Just implemented for compatibility, this cal does nothing.
2537
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002538.. js:function:: Socket.settimeout(socket, value [, mode])
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002539
2540 Changes the timeout values for the object. All I/O operations are blocking.
2541 That is, any call to the methods send, receive, and accept will block
2542 indefinitely, until the operation completes. The settimeout method defines a
2543 limit on the amount of time the I/O methods can block. When a timeout time
2544 has elapsed, the affected methods give up and fail with an error code.
2545
2546 The amount of time to wait is specified as the value parameter, in seconds.
2547
Mark Lakes56cc1252018-03-27 09:48:06 +02002548 The timeout modes are not implemented, the only settable timeout is the
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002549 inactivity time waiting for complete the internal buffer send or waiting for
2550 receive data.
2551
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002552 :param class_socket socket: Is the manipulated Socket.
Bertrand Jacquin874a35c2018-09-10 21:26:07 +01002553 :param float value: The timeout value. Use floating point to specify
Mark Lakes56cc1252018-03-27 09:48:06 +02002554 milliseconds.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002555
Thierry FOURNIER31904272017-10-25 12:59:51 +02002556.. _regex_class:
2557
2558Regex class
2559===========
2560
2561.. js:class:: Regex
2562
2563 This class allows the usage of HAProxy regexes because classic lua doesn't
2564 provides regexes. This class inherits the HAProxy compilation options, so the
2565 regexes can be libc regex, pcre regex or pcre JIT regex.
2566
2567 The expression matching number is limited to 20 per regex. The only available
2568 option is case sensitive.
2569
2570 Because regexes compilation is a heavy process, it is better to define all
2571 your regexes in the **body context** and use it during the runtime.
2572
2573.. code-block:: lua
2574
2575 -- Create the regex
2576 st, regex = Regex.new("needle (..) (...)", true);
2577
2578 -- Check compilation errors
2579 if st == false then
2580 print "error: " .. regex
2581 end
2582
2583 -- Match the regexes
2584 print(regex:exec("Looking for a needle in the haystack")) -- true
2585 print(regex:exec("Lokking for a cat in the haystack")) -- false
2586
2587 -- Extract words
2588 st, list = regex:match("Looking for a needle in the haystack")
2589 print(st) -- true
2590 print(list[1]) -- needle in the
2591 print(list[2]) -- in
2592 print(list[3]) -- the
2593
2594.. js:function:: Regex.new(regex, case_sensitive)
2595
2596 Create and compile a regex.
2597
2598 :param string regex: The regular expression according with the libc or pcre
2599 standard
2600 :param boolean case_sensitive: Match is case sensitive or not.
2601 :returns: boolean status and :ref:`regex_class` or string containing fail reason.
2602
2603.. js:function:: Regex.exec(regex, str)
2604
2605 Execute the regex.
2606
2607 :param class_regex regex: A :ref:`regex_class` object.
2608 :param string str: The input string will be compared with the compiled regex.
2609 :returns: a boolean status according with the match result.
2610
2611.. js:function:: Regex.match(regex, str)
2612
2613 Execute the regex and return matched expressions.
2614
2615 :param class_map map: A :ref:`regex_class` object.
2616 :param string str: The input string will be compared with the compiled regex.
2617 :returns: a boolean status according with the match result, and
2618 a table containing all the string matched in order of declaration.
2619
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002620.. _map_class:
2621
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002622Map class
2623=========
2624
2625.. js:class:: Map
2626
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02002627 This class permits to do some lookups in HAProxy maps. The declared maps can
Bertrand Jacquin874a35c2018-09-10 21:26:07 +01002628 be modified during the runtime through the HAProxy management socket.
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002629
2630.. code-block:: lua
2631
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002632 default = "usa"
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002633
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002634 -- Create and load map
Thierry FOURNIER4dc71972017-01-28 08:33:08 +01002635 geo = Map.new("geo.map", Map._ip);
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002636
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002637 -- Create new fetch that returns the user country
2638 core.register_fetches("country", function(txn)
2639 local src;
2640 local loc;
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002641
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002642 src = txn.f:fhdr("x-forwarded-for");
2643 if (src == nil) then
2644 src = txn.f:src()
2645 if (src == nil) then
2646 return default;
2647 end
2648 end
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002649
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002650 -- Perform lookup
2651 loc = geo:lookup(src);
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002652
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002653 if (loc == nil) then
2654 return default;
2655 end
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002656
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002657 return loc;
2658 end);
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002659
Thierry FOURNIER4dc71972017-01-28 08:33:08 +01002660.. js:attribute:: Map._int
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002661
2662 See the HAProxy configuration.txt file, chapter "Using ACLs and fetching
Ilya Shipitsin2075ca82020-03-06 23:22:22 +05002663 samples" and subchapter "ACL basics" to understand this pattern matching
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002664 method.
2665
Thierry FOURNIER4dc71972017-01-28 08:33:08 +01002666 Note that :js:attr:`Map.int` is also available for compatibility.
2667
2668.. js:attribute:: Map._ip
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002669
2670 See the HAProxy configuration.txt file, chapter "Using ACLs and fetching
Ilya Shipitsin2075ca82020-03-06 23:22:22 +05002671 samples" and subchapter "ACL basics" to understand this pattern matching
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002672 method.
2673
Thierry FOURNIER4dc71972017-01-28 08:33:08 +01002674 Note that :js:attr:`Map.ip` is also available for compatibility.
2675
2676.. js:attribute:: Map._str
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002677
2678 See the HAProxy configuration.txt file, chapter "Using ACLs and fetching
Ilya Shipitsin2075ca82020-03-06 23:22:22 +05002679 samples" and subchapter "ACL basics" to understand this pattern matching
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002680 method.
2681
Thierry FOURNIER4dc71972017-01-28 08:33:08 +01002682 Note that :js:attr:`Map.str` is also available for compatibility.
2683
2684.. js:attribute:: Map._beg
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002685
2686 See the HAProxy configuration.txt file, chapter "Using ACLs and fetching
Ilya Shipitsin2075ca82020-03-06 23:22:22 +05002687 samples" and subchapter "ACL basics" to understand this pattern matching
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002688 method.
2689
Thierry FOURNIER4dc71972017-01-28 08:33:08 +01002690 Note that :js:attr:`Map.beg` is also available for compatibility.
2691
2692.. js:attribute:: Map._sub
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002693
2694 See the HAProxy configuration.txt file, chapter "Using ACLs and fetching
Ilya Shipitsin2075ca82020-03-06 23:22:22 +05002695 samples" and subchapter "ACL basics" to understand this pattern matching
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002696 method.
2697
Thierry FOURNIER4dc71972017-01-28 08:33:08 +01002698 Note that :js:attr:`Map.sub` is also available for compatibility.
2699
2700.. js:attribute:: Map._dir
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002701
2702 See the HAProxy configuration.txt file, chapter "Using ACLs and fetching
Ilya Shipitsin2075ca82020-03-06 23:22:22 +05002703 samples" and subchapter "ACL basics" to understand this pattern matching
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002704 method.
2705
Thierry FOURNIER4dc71972017-01-28 08:33:08 +01002706 Note that :js:attr:`Map.dir` is also available for compatibility.
2707
2708.. js:attribute:: Map._dom
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002709
2710 See the HAProxy configuration.txt file, chapter "Using ACLs and fetching
Ilya Shipitsin2075ca82020-03-06 23:22:22 +05002711 samples" and subchapter "ACL basics" to understand this pattern matching
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002712 method.
2713
Thierry FOURNIER4dc71972017-01-28 08:33:08 +01002714 Note that :js:attr:`Map.dom` is also available for compatibility.
2715
2716.. js:attribute:: Map._end
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002717
2718 See the HAProxy configuration.txt file, chapter "Using ACLs and fetching
Ilya Shipitsin2075ca82020-03-06 23:22:22 +05002719 samples" and subchapter "ACL basics" to understand this pattern matching
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002720 method.
2721
Thierry FOURNIER4dc71972017-01-28 08:33:08 +01002722.. js:attribute:: Map._reg
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002723
2724 See the HAProxy configuration.txt file, chapter "Using ACLs and fetching
Ilya Shipitsin2075ca82020-03-06 23:22:22 +05002725 samples" and subchapter "ACL basics" to understand this pattern matching
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002726 method.
2727
Thierry FOURNIER4dc71972017-01-28 08:33:08 +01002728 Note that :js:attr:`Map.reg` is also available for compatibility.
2729
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002730
2731.. js:function:: Map.new(file, method)
2732
2733 Creates and load a map.
2734
2735 :param string file: Is the file containing the map.
2736 :param integer method: Is the map pattern matching method. See the attributes
2737 of the Map class.
2738 :returns: a class Map object.
Thierry FOURNIER4dc71972017-01-28 08:33:08 +01002739 :see: The Map attributes: :js:attr:`Map._int`, :js:attr:`Map._ip`,
2740 :js:attr:`Map._str`, :js:attr:`Map._beg`, :js:attr:`Map._sub`,
2741 :js:attr:`Map._dir`, :js:attr:`Map._dom`, :js:attr:`Map._end` and
2742 :js:attr:`Map._reg`.
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002743
2744.. js:function:: Map.lookup(map, str)
2745
2746 Perform a lookup in a map.
2747
2748 :param class_map map: Is the class Map object.
2749 :param string str: Is the string used as key.
2750 :returns: a string containing the result or nil if no match.
2751
2752.. js:function:: Map.slookup(map, str)
2753
2754 Perform a lookup in a map.
2755
2756 :param class_map map: Is the class Map object.
2757 :param string str: Is the string used as key.
2758 :returns: a string containing the result or empty string if no match.
2759
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002760.. _applethttp_class:
2761
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002762AppletHTTP class
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002763================
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002764
2765.. js:class:: AppletHTTP
2766
2767 This class is used with applets that requires the 'http' mode. The http applet
2768 can be registered with the *core.register_service()* function. They are used
2769 for processing an http request like a server in back of HAProxy.
2770
2771 This is an hello world sample code:
2772
2773.. code-block:: lua
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002774
Pieter Baauw4d7f7662015-11-08 16:38:08 +01002775 core.register_service("hello-world", "http", function(applet)
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002776 local response = "Hello World !"
2777 applet:set_status(200)
Pieter Baauw2dcb9bc2015-10-01 22:47:12 +02002778 applet:add_header("content-length", string.len(response))
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002779 applet:add_header("content-type", "text/plain")
Pieter Baauw2dcb9bc2015-10-01 22:47:12 +02002780 applet:start_response()
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002781 applet:send(response)
2782 end)
2783
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002784.. js:attribute:: AppletHTTP.c
2785
2786 :returns: A :ref:`converters_class`
2787
2788 This attribute contains a Converters class object.
2789
2790.. js:attribute:: AppletHTTP.sc
2791
2792 :returns: A :ref:`converters_class`
2793
2794 This attribute contains a Converters class object. The
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02002795 functions of this object always return a string.
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002796
2797.. js:attribute:: AppletHTTP.f
2798
2799 :returns: A :ref:`fetches_class`
2800
2801 This attribute contains a Fetches class object. Note that the
2802 applet execution place cannot access to a valid HAProxy core HTTP
Ilya Shipitsin2075ca82020-03-06 23:22:22 +05002803 transaction, so some sample fetches related to the HTTP dependent
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002804 values (hdr, path, ...) are not available.
2805
2806.. js:attribute:: AppletHTTP.sf
2807
2808 :returns: A :ref:`fetches_class`
2809
2810 This attribute contains a Fetches class object. The functions of
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02002811 this object always return a string. Note that the applet
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002812 execution place cannot access to a valid HAProxy core HTTP
Ilya Shipitsin2075ca82020-03-06 23:22:22 +05002813 transaction, so some sample fetches related to the HTTP dependent
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002814 values (hdr, path, ...) are not available.
2815
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01002816.. js:attribute:: AppletHTTP.method
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002817
2818 :returns: string
2819
2820 The attribute method returns a string containing the HTTP
2821 method.
2822
2823.. js:attribute:: AppletHTTP.version
2824
2825 :returns: string
2826
2827 The attribute version, returns a string containing the HTTP
2828 request version.
2829
2830.. js:attribute:: AppletHTTP.path
2831
2832 :returns: string
2833
2834 The attribute path returns a string containing the HTTP
2835 request path.
2836
2837.. js:attribute:: AppletHTTP.qs
2838
2839 :returns: string
2840
2841 The attribute qs returns a string containing the HTTP
2842 request query string.
2843
2844.. js:attribute:: AppletHTTP.length
2845
2846 :returns: integer
2847
2848 The attribute length returns an integer containing the HTTP
2849 body length.
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002850
Thierry FOURNIER841475e2015-12-11 17:10:09 +01002851.. js:attribute:: AppletHTTP.headers
2852
Patrick Hemmerc6a1d712018-05-01 21:30:41 -04002853 :returns: table
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002854
Patrick Hemmerc6a1d712018-05-01 21:30:41 -04002855 The attribute headers returns a table containing the HTTP
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002856 headers. The header names are always in lower case. As the header name can be
2857 encountered more than once in each request, the value is indexed with 0 as
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02002858 first index value. The table has this form:
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002859
2860.. code-block:: lua
2861
2862 AppletHTTP.headers['<header-name>'][<header-index>] = "<header-value>"
2863
2864 AppletHTTP.headers["host"][0] = "www.test.com"
2865 AppletHTTP.headers["accept"][0] = "audio/basic q=1"
2866 AppletHTTP.headers["accept"][1] = "audio/*, q=0.2"
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01002867 AppletHTTP.headers["accept"][2] = "*/*, q=0.1"
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002868..
2869
Robin H. Johnson52f5db22017-01-01 13:10:52 -08002870.. js:function:: AppletHTTP.set_status(applet, code [, reason])
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002871
2872 This function sets the HTTP status code for the response. The allowed code are
2873 from 100 to 599.
2874
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01002875 :param class_AppletHTTP applet: An :ref:`applethttp_class`
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002876 :param integer code: the status code returned to the client.
Robin H. Johnson52f5db22017-01-01 13:10:52 -08002877 :param string reason: the status reason returned to the client (optional).
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002878
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01002879.. js:function:: AppletHTTP.add_header(applet, name, value)
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002880
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02002881 This function adds a header in the response. Duplicated headers are not
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002882 collapsed. The special header *content-length* is used to determinate the
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02002883 response length. If it does not exist, a *transfer-encoding: chunked* is set, and
Ilya Shipitsin2075ca82020-03-06 23:22:22 +05002884 all the write from the function *AppletHTTP:send()* become a chunk.
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002885
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01002886 :param class_AppletHTTP applet: An :ref:`applethttp_class`
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002887 :param string name: the header name
2888 :param string value: the header value
2889
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01002890.. js:function:: AppletHTTP.start_response(applet)
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002891
2892 This function indicates to the HTTP engine that it can process and send the
2893 response headers. After this called we cannot add headers to the response; We
2894 cannot use the *AppletHTTP:send()* function if the
2895 *AppletHTTP:start_response()* is not called.
2896
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01002897 :param class_AppletHTTP applet: An :ref:`applethttp_class`
2898
2899.. js:function:: AppletHTTP.getline(applet)
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002900
2901 This function returns a string containing one line from the http body. If the
2902 data returned doesn't contains a final '\\n' its assumed than its the last
2903 available data before the end of stream.
2904
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01002905 :param class_AppletHTTP applet: An :ref:`applethttp_class`
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002906 :returns: a string. The string can be empty if we reach the end of the stream.
2907
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01002908.. js:function:: AppletHTTP.receive(applet, [size])
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002909
2910 Reads data from the HTTP body, according to the specified read *size*. If the
2911 *size* is missing, the function tries to read all the content of the stream
2912 until the end. If the *size* is bigger than the http body, it returns the
Bertrand Jacquin874a35c2018-09-10 21:26:07 +01002913 amount of data available.
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002914
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01002915 :param class_AppletHTTP applet: An :ref:`applethttp_class`
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002916 :param integer size: the required read size.
Ilya Shipitsin11057a32020-06-21 21:18:27 +05002917 :returns: always return a string,the string can be empty is the connection is
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002918 closed.
2919
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01002920.. js:function:: AppletHTTP.send(applet, msg)
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002921
2922 Send the message *msg* on the http request body.
2923
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01002924 :param class_AppletHTTP applet: An :ref:`applethttp_class`
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002925 :param string msg: the message to send.
2926
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01002927.. js:function:: AppletHTTP.get_priv(applet)
2928
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01002929 Return Lua data stored in the current transaction. If no data are stored,
2930 it returns a nil value.
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01002931
2932 :param class_AppletHTTP applet: An :ref:`applethttp_class`
Bertrand Jacquin874a35c2018-09-10 21:26:07 +01002933 :returns: the opaque data previously stored, or nil if nothing is
2934 available.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01002935 :see: :js:func:`AppletHTTP.set_priv`
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01002936
2937.. js:function:: AppletHTTP.set_priv(applet, data)
2938
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02002939 Store any data in the current HAProxy transaction. This action replaces the
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01002940 old stored data.
2941
2942 :param class_AppletHTTP applet: An :ref:`applethttp_class`
2943 :param opaque data: The data which is stored in the transaction.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01002944 :see: :js:func:`AppletHTTP.get_priv`
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01002945
Tim Duesterhus4e172c92020-05-19 13:49:42 +02002946.. js:function:: AppletHTTP.set_var(applet, var, value[, ifexist])
Thierry FOURNIER / OZON.IOc1edafe2016-12-12 16:25:30 +01002947
2948 Converts a Lua type in a HAProxy type and store it in a variable <var>.
2949
2950 :param class_AppletHTTP applet: An :ref:`applethttp_class`
2951 :param string var: The variable name according with the HAProxy variable syntax.
2952 :param type value: The value associated to the variable. The type ca be string or
2953 integer.
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02002954 :param boolean ifexist: If this parameter is set to true the variable
Tim Duesterhus4e172c92020-05-19 13:49:42 +02002955 will only be set if it was defined elsewhere (i.e. used
Willy Tarreau7978c5c2021-09-07 14:24:07 +02002956 within the configuration). For global variables (using the
2957 "proc" scope), they will only be updated and never created.
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02002958 It is highly recommended to always set this to true.
2959
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01002960 :see: :js:func:`AppletHTTP.unset_var`
2961 :see: :js:func:`AppletHTTP.get_var`
Thierry FOURNIER / OZON.IOc1edafe2016-12-12 16:25:30 +01002962
2963.. js:function:: AppletHTTP.unset_var(applet, var)
2964
2965 Unset the variable <var>.
2966
2967 :param class_AppletHTTP applet: An :ref:`applethttp_class`
2968 :param string var: The variable name according with the HAProxy variable syntax.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01002969 :see: :js:func:`AppletHTTP.set_var`
2970 :see: :js:func:`AppletHTTP.get_var`
Thierry FOURNIER / OZON.IOc1edafe2016-12-12 16:25:30 +01002971
2972.. js:function:: AppletHTTP.get_var(applet, var)
2973
2974 Returns data stored in the variable <var> converter in Lua type.
2975
2976 :param class_AppletHTTP applet: An :ref:`applethttp_class`
2977 :param string var: The variable name according with the HAProxy variable syntax.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01002978 :see: :js:func:`AppletHTTP.set_var`
2979 :see: :js:func:`AppletHTTP.unset_var`
Thierry FOURNIER / OZON.IOc1edafe2016-12-12 16:25:30 +01002980
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002981.. _applettcp_class:
2982
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002983AppletTCP class
2984===============
2985
2986.. js:class:: AppletTCP
2987
2988 This class is used with applets that requires the 'tcp' mode. The tcp applet
2989 can be registered with the *core.register_service()* function. They are used
2990 for processing a tcp stream like a server in back of HAProxy.
2991
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002992.. js:attribute:: AppletTCP.c
2993
2994 :returns: A :ref:`converters_class`
2995
2996 This attribute contains a Converters class object.
2997
2998.. js:attribute:: AppletTCP.sc
2999
3000 :returns: A :ref:`converters_class`
3001
3002 This attribute contains a Converters class object. The
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02003003 functions of this object always return a string.
Thierry FOURNIERdc595002015-12-21 11:13:52 +01003004
3005.. js:attribute:: AppletTCP.f
3006
3007 :returns: A :ref:`fetches_class`
3008
3009 This attribute contains a Fetches class object.
3010
3011.. js:attribute:: AppletTCP.sf
3012
3013 :returns: A :ref:`fetches_class`
3014
3015 This attribute contains a Fetches class object.
3016
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01003017.. js:function:: AppletTCP.getline(applet)
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02003018
3019 This function returns a string containing one line from the stream. If the
3020 data returned doesn't contains a final '\\n' its assumed than its the last
3021 available data before the end of stream.
3022
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01003023 :param class_AppletTCP applet: An :ref:`applettcp_class`
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02003024 :returns: a string. The string can be empty if we reach the end of the stream.
3025
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01003026.. js:function:: AppletTCP.receive(applet, [size])
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02003027
3028 Reads data from the TCP stream, according to the specified read *size*. If the
3029 *size* is missing, the function tries to read all the content of the stream
3030 until the end.
3031
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01003032 :param class_AppletTCP applet: An :ref:`applettcp_class`
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02003033 :param integer size: the required read size.
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02003034 :returns: always return a string, the string can be empty if the connection is
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02003035 closed.
3036
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01003037.. js:function:: AppletTCP.send(appletmsg)
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02003038
3039 Send the message on the stream.
3040
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01003041 :param class_AppletTCP applet: An :ref:`applettcp_class`
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02003042 :param string msg: the message to send.
3043
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01003044.. js:function:: AppletTCP.get_priv(applet)
3045
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01003046 Return Lua data stored in the current transaction. If no data are stored,
3047 it returns a nil value.
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01003048
3049 :param class_AppletTCP applet: An :ref:`applettcp_class`
Bertrand Jacquin874a35c2018-09-10 21:26:07 +01003050 :returns: the opaque data previously stored, or nil if nothing is
3051 available.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01003052 :see: :js:func:`AppletTCP.set_priv`
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01003053
3054.. js:function:: AppletTCP.set_priv(applet, data)
3055
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02003056 Store any data in the current HAProxy transaction. This action replaces the
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01003057 old stored data.
3058
3059 :param class_AppletTCP applet: An :ref:`applettcp_class`
3060 :param opaque data: The data which is stored in the transaction.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01003061 :see: :js:func:`AppletTCP.get_priv`
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01003062
Tim Duesterhus4e172c92020-05-19 13:49:42 +02003063.. js:function:: AppletTCP.set_var(applet, var, value[, ifexist])
Thierry FOURNIER / OZON.IOc1edafe2016-12-12 16:25:30 +01003064
3065 Converts a Lua type in a HAProxy type and stores it in a variable <var>.
3066
3067 :param class_AppletTCP applet: An :ref:`applettcp_class`
3068 :param string var: The variable name according with the HAProxy variable syntax.
3069 :param type value: The value associated to the variable. The type can be string or
3070 integer.
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02003071 :param boolean ifexist: If this parameter is set to true the variable
Tim Duesterhus4e172c92020-05-19 13:49:42 +02003072 will only be set if it was defined elsewhere (i.e. used
Willy Tarreau7978c5c2021-09-07 14:24:07 +02003073 within the configuration). For global variables (using the
3074 "proc" scope), they will only be updated and never created.
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02003075 It is highly recommended to always set this to true.
3076
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01003077 :see: :js:func:`AppletTCP.unset_var`
3078 :see: :js:func:`AppletTCP.get_var`
Thierry FOURNIER / OZON.IOc1edafe2016-12-12 16:25:30 +01003079
3080.. js:function:: AppletTCP.unset_var(applet, var)
3081
3082 Unsets the variable <var>.
3083
3084 :param class_AppletTCP applet: An :ref:`applettcp_class`
3085 :param string var: The variable name according with the HAProxy variable syntax.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01003086 :see: :js:func:`AppletTCP.unset_var`
3087 :see: :js:func:`AppletTCP.set_var`
Thierry FOURNIER / OZON.IOc1edafe2016-12-12 16:25:30 +01003088
3089.. js:function:: AppletTCP.get_var(applet, var)
3090
3091 Returns data stored in the variable <var> converter in Lua type.
3092
3093 :param class_AppletTCP applet: An :ref:`applettcp_class`
3094 :param string var: The variable name according with the HAProxy variable syntax.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01003095 :see: :js:func:`AppletTCP.unset_var`
3096 :see: :js:func:`AppletTCP.set_var`
Thierry FOURNIER / OZON.IOc1edafe2016-12-12 16:25:30 +01003097
Adis Nezirovic8878f8e2018-07-13 12:18:33 +02003098StickTable class
3099================
3100
3101.. js:class:: StickTable
3102
3103 **context**: task, action, sample-fetch
3104
3105 This class can be used to access the HAProxy stick tables from Lua.
3106
3107.. js:function:: StickTable.info()
3108
3109 Returns stick table attributes as a Lua table. See HAProxy documentation for
Ilya Shipitsin2272d8a2020-12-21 01:22:40 +05003110 "stick-table" for canonical info, or check out example below.
Adis Nezirovic8878f8e2018-07-13 12:18:33 +02003111
3112 :returns: Lua table
3113
3114 Assume our table has IPv4 key and gpc0 and conn_rate "columns":
3115
3116.. code-block:: lua
3117
3118 {
3119 expire=<int>, # Value in ms
3120 size=<int>, # Maximum table size
3121 used=<int>, # Actual number of entries in table
3122 data={ # Data columns, with types as key, and periods as values
3123 (-1 if type is not rate counter)
3124 conn_rate=<int>,
3125 gpc0=-1
3126 },
3127 length=<int>, # max string length for string table keys, key length
3128 # otherwise
3129 nopurge=<boolean>, # purge oldest entries when table is full
3130 type="ip" # can be "ip", "ipv6", "integer", "string", "binary"
3131 }
3132
3133.. js:function:: StickTable.lookup(key)
3134
3135 Returns stick table entry for given <key>
3136
3137 :param string key: Stick table key (IP addresses and strings are supported)
3138 :returns: Lua table
3139
3140.. js:function:: StickTable.dump([filter])
3141
3142 Returns all entries in stick table. An optional filter can be used
3143 to extract entries with specific data values. Filter is a table with valid
3144 comparison operators as keys followed by data type name and value pairs.
3145 Check out the HAProxy docs for "show table" for more details. For the
3146 reference, the supported operators are:
3147 "eq", "ne", "le", "lt", "ge", "gt"
3148
3149 For large tables, execution of this function can take a long time (for
3150 HAProxy standards). That's also true when filter is used, so take care and
3151 measure the impact.
3152
3153 :param table filter: Stick table filter
3154 :returns: Stick table entries (table)
3155
3156 See below for example filter, which contains 4 entries (or comparisons).
3157 (Maximum number of filter entries is 4, defined in the source code)
3158
3159.. code-block:: lua
3160
3161 local filter = {
3162 {"gpc0", "gt", 30}, {"gpc1", "gt", 20}}, {"conn_rate", "le", 10}
3163 }
3164
Christopher Faulet0f3c8902020-01-31 18:57:12 +01003165.. _action_class:
3166
3167Action class
3168=============
3169
3170.. js:class:: Act
3171
3172 **context**: action
3173
3174 This class contains all return codes an action may return. It is the lua
3175 equivalent to HAProxy "ACT_RET_*" code.
3176
3177.. code-block:: lua
3178
3179 core.register_action("deny", { "http-req" }, function (txn)
3180 return act.DENY
3181 end)
3182..
3183.. js:attribute:: act.CONTINUE
3184
3185 This attribute is an integer (0). It instructs HAProxy to continue the current
3186 ruleset processing on the message. It is the default return code for a lua
3187 action.
3188
3189 :returns: integer
3190
3191.. js:attribute:: act.STOP
3192
3193 This attribute is an integer (1). It instructs HAProxy to stop the current
3194 ruleset processing on the message.
3195
3196.. js:attribute:: act.YIELD
3197
3198 This attribute is an integer (2). It instructs HAProxy to temporarily pause
3199 the message processing. It will be resumed later on the same rule. The
3200 corresponding lua script is re-executed for the start.
3201
3202.. js:attribute:: act.ERROR
3203
3204 This attribute is an integer (3). It triggers an internal errors The message
3205 processing is stopped and the transaction is terminated. For HTTP streams, an
3206 HTTP 500 error is returned to the client.
3207
3208 :returns: integer
3209
3210.. js:attribute:: act.DONE
3211
3212 This attribute is an integer (4). It instructs HAProxy to stop the message
3213 processing.
3214
3215 :returns: integer
3216
3217.. js:attribute:: act.DENY
3218
3219 This attribute is an integer (5). It denies the current message. The message
3220 processing is stopped and the transaction is terminated. For HTTP streams, an
3221 HTTP 403 error is returned to the client if the deny is returned during the
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02003222 request analysis. During the response analysis, a HTTP 502 error is returned
Christopher Faulet0f3c8902020-01-31 18:57:12 +01003223 and the server response is discarded.
3224
3225 :returns: integer
3226
3227.. js:attribute:: act.ABORT
3228
3229 This attribute is an integer (6). It aborts the current message. The message
3230 processing is stopped and the transaction is terminated. For HTTP streams,
Willy Tarreau714f3452021-05-09 06:47:26 +02003231 HAProxy assumes a response was already sent to the client. From the Lua
Christopher Faulet0f3c8902020-01-31 18:57:12 +01003232 actions point of view, when this code is used, the transaction is terminated
3233 with no reply.
3234
3235 :returns: integer
3236
3237.. js:attribute:: act.INVALID
3238
3239 This attribute is an integer (7). It triggers an internal errors. The message
3240 processing is stopped and the transaction is terminated. For HTTP streams, an
3241 HTTP 400 error is returned to the client if the error is returned during the
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02003242 request analysis. During the response analysis, a HTTP 502 error is returned
Christopher Faulet0f3c8902020-01-31 18:57:12 +01003243 and the server response is discarded.
3244
3245 :returns: integer
Adis Nezirovic8878f8e2018-07-13 12:18:33 +02003246
Christopher Faulet2c2c2e32020-01-31 19:07:52 +01003247.. js:function:: act:wake_time(milliseconds)
3248
3249 **context**: action
3250
3251 Set the script pause timeout to the specified time, defined in
3252 milliseconds.
3253
3254 :param integer milliseconds: the required milliseconds.
3255
3256 This function may be used when a lua action returns `act.YIELD`, to force its
3257 wake-up at most after the specified number of milliseconds.
3258
Christopher Faulet5a2c6612021-08-15 20:35:25 +02003259.. _filter_class:
3260
3261Filter class
3262=============
3263
3264.. js:class:: filter
3265
3266 **context**: filter
3267
3268 This class contains return codes some filter callback functions may return. It
3269 also contains configuration flags and some helper functions. To understand how
3270 the filter API works, see `doc/internal/filters.txt` documentation.
3271
3272.. js:attribute:: filter.CONTINUE
3273
3274 This attribute is an integer (1). It may be returned by some filter callback
3275 functions to instruct this filtering step is finished for this filter.
3276
3277.. js:attribute:: filter.WAIT
3278
3279 This attribute is an integer (0). It may be returned by some filter callback
3280 functions to instruct the filtering must be paused, waiting for more data or
3281 for an external event depending on this filter.
3282
3283.. js:attribute:: filter.ERROR
3284
3285 This attribute is an integer (-1). It may be returned by some filter callback
3286 functions to trigger an error.
3287
3288.. js:attribute:: filter.FLT_CFG_FL_HTX
3289
3290 This attribute is a flag corresponding to the filter flag FLT_CFG_FL_HTX. When
3291 it is set for a filter, it means the filter is able to filter HTTP streams.
3292
3293.. js:function:: filter.register_data_filter(chn)
3294
3295 **context**: filter
3296
3297 Enable the data filtering on the channel **chn** for the current filter. It
Ilya Shipitsinff0f2782021-08-22 22:18:07 +05003298 may be called at any time from any callback functions proceeding the data
Christopher Faulet5a2c6612021-08-15 20:35:25 +02003299 analysis.
3300
3301 :param class_Channel chn: A :ref:`channel_class`.
3302
3303.. js:function:: filter.unregister_data_filter(chn)
3304
3305 **context**: filter
3306
3307 Disable the data filtering on the channel **chn** for the current filter. It
3308 may be called at any time from any callback functions.
3309
3310 :param class_Channel chn: A :ref:`channel_class`.
3311
3312.. js:function:: filter.wake_time(milliseconds)
3313
3314 **context**: filter
3315
3316 Set the script pause timeout to the specified time, defined in
3317 milliseconds.
3318
3319 :param integer milliseconds: the required milliseconds.
3320
3321 This function may be used from any lua filter callback function to force its
3322 wake-up at most after the specified number of milliseconds. Especially, when
3323 `filter.CONTINUE` is returned.
3324
3325
3326A filters is declared using :js:func:`core.register_filter()` function. The
3327provided class will be used to instantiate filters. It may define following
3328attributes:
3329
3330* id: The filter identifier. It is a string that identifies the filter and is
3331 optional.
3332
3333* flags: The filter flags. Only :js:attr:`filter.FLT_CFG_FL_HTX` may be set for now.
3334
3335Such filter class must also define all required callback functions in the
3336following list. Note that :js:func:`Filter.new()` must be defined otherwise the
Ilya Shipitsinff0f2782021-08-22 22:18:07 +05003337filter is ignored. Others are optional.
Christopher Faulet5a2c6612021-08-15 20:35:25 +02003338
3339* .. js:function:: FILTER.new()
3340
3341 Called to instantiate a new filter. This function must be defined.
3342
3343 :returns: a Lua object that will be used as filter instance for the current
3344 stream.
3345
3346* .. js:function:: FILTER.start_analyze(flt, txn, chn)
3347
3348 Called when the analysis starts on the channel **chn**.
3349
3350* .. js:function:: FILTER.end_analyze(flt, txn, chn)
3351
3352 Called when the analysis ends on the channel **chn**.
3353
3354* .. js:function:: FILTER.http_headers(flt, txn, http_msg)
3355
3356 Called just before the HTTP payload analysis and after any processing on the
3357 HTTP message **http_msg**. This callback functions is only called for HTTP
3358 streams.
3359
3360* .. js:function:: FILTER.http_payload(flt, txn, http_msg)
3361
3362 Called during the HTTP payload analysis on the HTTP message **http_msg**. This
3363 callback functions is only called for HTTP streams.
3364
3365* .. js:function:: FILTER.http_end(flt, txn, http_msg)
3366
3367 Called after the HTTP payload analysis on the HTTP message **http_msg**. This
3368 callback functions is only called for HTTP streams.
3369
3370* .. js:function:: FILTER.tcp_payload(flt, txn, chn)
3371
3372 Called during the TCP payload analysis on the channel **chn**.
3373
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02003374Here is a full example:
Christopher Faulet5a2c6612021-08-15 20:35:25 +02003375
3376.. code-block:: lua
3377
3378 Trace = {}
3379 Trace.id = "Lua trace filter"
3380 Trace.flags = filter.FLT_CFG_FL_HTX;
3381 Trace.__index = Trace
3382
3383 function Trace:new()
3384 local trace = {}
3385 setmetatable(trace, Trace)
3386 trace.req_len = 0
3387 trace.res_len = 0
3388 return trace
3389 end
3390
3391 function Trace:start_analyze(txn, chn)
3392 if chn:is_resp() then
3393 print("Start response analysis")
3394 else
3395 print("Start request analysis")
3396 end
3397 filter.register_data_filter(self, chn)
3398 end
3399
3400 function Trace:end_analyze(txn, chn)
3401 if chn:is_resp() then
3402 print("End response analysis: "..self.res_len.." bytes filtered")
3403 else
3404 print("End request analysis: "..self.req_len.." bytes filtered")
3405 end
3406 end
3407
3408 function Trace:http_headers(txn, http_msg)
3409 stline = http_msg:get_stline()
3410 if http_msg.channel:is_resp() then
3411 print("response:")
3412 print(stline.version.." "..stline.code.." "..stline.reason)
3413 else
3414 print("request:")
3415 print(stline.method.." "..stline.uri.." "..stline.version)
3416 end
3417
3418 for n, hdrs in pairs(http_msg:get_headers()) do
3419 for i,v in pairs(hdrs) do
3420 print(n..": "..v)
3421 end
3422 end
3423 return filter.CONTINUE
3424 end
3425
3426 function Trace:http_payload(txn, http_msg)
3427 body = http_msg:body(-20000)
3428 if http_msg.channel:is_resp() then
3429 self.res_len = self.res_len + body:len()
3430 else
3431 self.req_len = self.req_len + body:len()
3432 end
3433 end
3434
3435 core.register_filter("trace", Trace, function(trace, args)
3436 return trace
3437 end)
3438
3439..
3440
3441.. _httpmessage_class:
3442
3443HTTPMessage class
3444===================
3445
3446.. js:class:: HTTPMessage
3447
3448 **context**: filter
3449
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02003450 This class contains all functions to manipulate a HTTP message. For now, this
Christopher Faulet5a2c6612021-08-15 20:35:25 +02003451 class is only available from a filter context.
3452
3453.. js:function:: HTTPMessage.add_header(http_msg, name, value)
3454
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02003455 Appends a HTTP header field in the HTTP message **http_msg** whose name is
Christopher Faulet5a2c6612021-08-15 20:35:25 +02003456 specified in **name** and whose value is defined in **value**.
3457
3458 :param class_httpmessage http_msg: The manipulated HTTP message.
3459 :param string name: The header name.
3460 :param string value: The header value.
3461
3462.. js:function:: HTTPMessage.append(http_msg, string)
3463
3464 This function copies the string **string** at the end of incoming data of the
3465 HTTP message **http_msg**. The function returns the copied length on success
3466 or -1 if data cannot be copied.
3467
3468 Same that :js:func:`HTTPMessage.insert(http_msg, string, http_msg:input())`.
3469
3470 :param class_httpmessage http_msg: The manipulated HTTP message.
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02003471 :param string string: The data to copy at the end of incoming data.
Christopher Faulet5a2c6612021-08-15 20:35:25 +02003472 :returns: an integer containing the amount of bytes copied or -1.
3473
3474.. js:function:: HTTPMessage.body(http_msgl[, offset[, length]])
3475
3476 This function returns **length** bytes of incoming data from the HTTP message
3477 **http_msg**, starting at the offset **offset**. The data are not removed from
3478 the buffer.
3479
3480 By default, if no length is provided, all incoming data found, starting at the
3481 given offset, are returned. If **length** is set to -1, the function tries to
3482 retrieve a maximum of data. Because it is called in the filter context, it
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02003483 never yield. Not providing an offset is the same as setting it to 0. A
Ilya Shipitsinff0f2782021-08-22 22:18:07 +05003484 positive offset is relative to the beginning of incoming data of the
Christopher Faulet5a2c6612021-08-15 20:35:25 +02003485 http_message buffer while negative offset is relative to their end.
3486
3487 If there is no incoming data and the HTTP message can't receive more data, a 'nil'
3488 value is returned.
3489
3490 :param class_httpmessage http_msg: The manipulated HTTP message.
3491 :param integer offset: *optional* The offset in incoming data to start to get
3492 data. 0 by default. May be negative to be relative to
3493 the end of incoming data.
3494 :param integer length: *optional* The expected length of data to retrieve. All
3495 incoming data by default. May be set to -1 to get a
3496 maximum of data.
3497 :returns: a string containing the data found or nil.
3498
3499.. js:function:: HTTPMessage.eom(http_msg)
3500
3501 This function returns true if the end of message is reached for the HTTP
3502 message **http_msg**.
3503
3504 :param class_httpmessage http_msg: The manipulated HTTP message.
3505 :returns: an integer containing the amount of available bytes.
3506
3507.. js:function:: HTTPMessage.del_header(http_msg, name)
3508
3509 Removes all HTTP header fields in the HTTP message **http_msg** whose name is
3510 specified in **name**.
3511
3512 :param class_httpmessage http_msg: The manipulated http message.
3513 :param string name: The header name.
3514
3515.. js:function:: HTTPMessage.get_headers(http_msg)
3516
3517 Returns a table containing all the headers of the HTTP message **http_msg**.
3518
3519 :param class_httpmessage http_msg: The manipulated http message.
3520 :returns: table of headers.
3521
3522 This is the form of the returned table:
3523
3524.. code-block:: lua
3525
3526 http_msg:get_headers()['<header-name>'][<header-index>] = "<header-value>"
3527
3528 local hdr = http_msg:get_headers()
3529 hdr["host"][0] = "www.test.com"
3530 hdr["accept"][0] = "audio/basic q=1"
3531 hdr["accept"][1] = "audio/*, q=0.2"
3532 hdr["accept"][2] = "*.*, q=0.1"
3533..
3534
3535.. js:function:: HTTPMessage.get_stline(http_msg)
3536
3537 Returns a table containing the start-line of the HTTP message **http_msg**.
3538
3539 :param class_httpmessage http_msg: The manipulated http message.
3540 :returns: the start-line.
3541
3542 This is the form of the returned table:
3543
3544.. code-block:: lua
3545
3546 -- for the request :
3547 {"method" = string, "uri" = string, "version" = string}
3548
3549 -- for the response:
3550 {"version" = string, "code" = string, "reason" = string}
3551..
3552
3553.. js:function:: HTTPMessage.forward(http_msg, length)
3554
3555 This function forwards **length** bytes of data from the HTTP message
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02003556 **http_msg**. Because it is called in the filter context, it never yields. Only
Christopher Faulet5a2c6612021-08-15 20:35:25 +02003557 available incoming data may be forwarded, event if the requested length
3558 exceeds the available amount of incoming data. It returns the amount of data
3559 forwarded.
3560
3561 :param class_httpmessage http_msg: The manipulated HTTP message.
3562 :param integer int: The amount of data to forward.
3563
3564.. js:function:: HTTPMessage.input(http_msg)
3565
3566 This function returns the length of incoming data in the HTTP message
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02003567 **http_msg** from the filter point of view.
Christopher Faulet5a2c6612021-08-15 20:35:25 +02003568
3569 :param class_httpmessage http_msg: The manipulated HTTP message.
3570 :returns: an integer containing the amount of available bytes.
3571
3572.. js:function:: HTTPMessage.insert(http_msg, string[, offset])
3573
3574 This function copies the string **string** at the offset **offset** in
3575 incoming data of the HTTP message **http_msg**. The function returns the
3576 copied length on success or -1 if data cannot be copied.
3577
3578 By default, if no offset is provided, the string is copied in front of
Ilya Shipitsinff0f2782021-08-22 22:18:07 +05003579 incoming data. A positive offset is relative to the beginning of incoming data
Christopher Faulet5a2c6612021-08-15 20:35:25 +02003580 of the HTTP message while negative offset is relative to their end.
3581
3582 :param class_httpmessage http_msg: The manipulated HTTP message.
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02003583 :param string string: The data to copy into incoming data.
3584 :param integer offset: *optional* The offset in incoming data where to copy
Christopher Faulet5a2c6612021-08-15 20:35:25 +02003585 data. 0 by default. May be negative to be relative to
3586 the end of incoming data.
3587 :returns: an integer containing the amount of bytes copied or -1.
3588
3589.. js:function:: HTTPMessage.is_full(http_msg)
3590
3591 This function returns true if the HTTP message **http_msg** is full.
3592
3593 :param class_httpmessage http_msg: The manipulated HTTP message.
3594 :returns: a boolean
3595
3596.. js:function:: HTTPMessage.is_resp(http_msg)
3597
3598 This function returns true if the HTTP message **http_msg** is the response
3599 one.
3600
3601 :param class_httpmessage http_msg: The manipulated HTTP message.
3602 :returns: a boolean
3603
3604.. js:function:: HTTPMessage.may_recv(http_msg)
3605
3606 This function returns true if the HTTP message **http_msg** may still receive
3607 data.
3608
3609 :param class_httpmessage http_msg: The manipulated HTTP message.
3610 :returns: a boolean
3611
3612.. js:function:: HTTPMessage.output(http_msg)
3613
3614 This function returns the length of outgoing data of the HTTP message
3615 **http_msg**.
3616
3617 :param class_httpmessage http_msg: The manipulated HTTP message.
3618 :returns: an integer containing the amount of available bytes.
3619
3620.. js:function:: HTTPMessage.prepend(http_msg, string)
3621
3622 This function copies the string **string** in front of incoming data of the
3623 HTTP message **http_msg**. The function returns the copied length on success
3624 or -1 if data cannot be copied.
3625
3626 Same that :js:func:`HTTPMessage.insert(http_msg, string, 0)`.
3627
3628 :param class_httpmessage http_msg: The manipulated HTTP message.
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02003629 :param string string: The data to copy in front of incoming data.
Christopher Faulet5a2c6612021-08-15 20:35:25 +02003630 :returns: an integer containing the amount of bytes copied or -1.
3631
3632.. js:function:: HTTPMessage.remove(http_msg[, offset[, length]])
3633
3634 This function removes **length** bytes of incoming data of the HTTP message
3635 **http_msg**, starting at offset **offset**. This function returns number of
3636 bytes removed on success.
3637
3638 By default, if no length is provided, all incoming data, starting at the given
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02003639 offset, are removed. Not providing an offset is the same that setting it
Ilya Shipitsinff0f2782021-08-22 22:18:07 +05003640 to 0. A positive offset is relative to the beginning of incoming data of the
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02003641 HTTP message while negative offset is relative to the end.
Christopher Faulet5a2c6612021-08-15 20:35:25 +02003642
3643 :param class_httpmessage http_msg: The manipulated HTTP message.
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02003644 :param integer offset: *optional* The offset in incoming data where to start
Christopher Faulet5a2c6612021-08-15 20:35:25 +02003645 to remove data. 0 by default. May be negative to
3646 be relative to the end of incoming data.
3647 :param integer length: *optional* The length of data to remove. All incoming
3648 data by default.
3649 :returns: an integer containing the amount of bytes removed.
3650
3651.. js:function:: HTTPMessage.rep_header(http_msg, name, regex, replace)
3652
3653 Matches the regular expression in all occurrences of header field **name**
3654 according to regex **regex**, and replaces them with the string **replace**.
3655 The replacement value can contain back references like \1, \2, ... This
3656 function acts on whole header lines, regardless of the number of values they
3657 may contain.
3658
3659 :param class_httpmessage http_msg: The manipulated HTTP message.
3660 :param string name: The header name.
3661 :param string regex: The match regular expression.
3662 :param string replace: The replacement value.
3663
3664.. js:function:: HTTPMessage.rep_value(http_msg, name, regex, replace)
3665
3666 Matches the regular expression on every comma-delimited value of header field
3667 **name** according to regex **regex**, and replaces them with the string
3668 **replace**. The replacement value can contain back references like \1, \2,
3669 ...
3670
3671 :param class_httpmessage http_msg: The manipulated HTTP message.
3672 :param string name: The header name.
3673 :param string regex: The match regular expression.
3674 :param string replace: The replacement value.
3675
3676.. js:function:: HTTPMessage.send(http_msg, string)
3677
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02003678 This function requires immediate send of the string **string**. It means the
Ilya Shipitsinff0f2782021-08-22 22:18:07 +05003679 string is copied at the beginning of incoming data of the HTTP message
Christopher Faulet5a2c6612021-08-15 20:35:25 +02003680 **http_msg** and immediately forwarded. Because it is called in the filter
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02003681 context, it never yields.
Christopher Faulet5a2c6612021-08-15 20:35:25 +02003682
3683 :param class_httpmessage http_msg: The manipulated HTTP message.
3684 :param string string: The data to send.
3685 :returns: an integer containing the amount of bytes copied or -1.
3686
3687.. js:function:: HTTPMessage.set(http_msg, string[, offset[, length]])
3688
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02003689 This function replaces **length** bytes of incoming data of the HTTP message
Christopher Faulet5a2c6612021-08-15 20:35:25 +02003690 **http_msg**, starting at offset **offset**, by the string **string**. The
3691 function returns the copied length on success or -1 if data cannot be copied.
3692
3693 By default, if no length is provided, all incoming data, starting at the given
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02003694 offset, are replaced. Not providing an offset is the same as setting it
Ilya Shipitsinff0f2782021-08-22 22:18:07 +05003695 to 0. A positive offset is relative to the beginning of incoming data of the
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02003696 HTTP message while negative offset is relative to the end.
Christopher Faulet5a2c6612021-08-15 20:35:25 +02003697
3698 :param class_httpmessage http_msg: The manipulated HTTP message.
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02003699 :param string string: The data to copy into incoming data.
3700 :param integer offset: *optional* The offset in incoming data where to start
Christopher Faulet5a2c6612021-08-15 20:35:25 +02003701 the data replacement. 0 by default. May be negative to
3702 be relative to the end of incoming data.
3703 :param integer length: *optional* The length of data to replace. All incoming
3704 data by default.
3705 :returns: an integer containing the amount of bytes copied or -1.
3706
3707.. js:function:: HTTPMessage.set_eom(http_msg)
3708
3709 This function set the end of message for the HTTP message **http_msg**.
3710
3711 :param class_httpmessage http_msg: The manipulated HTTP message.
3712
3713.. js:function:: HTTPMessage.set_header(http_msg, name, value)
3714
3715 This variable replace all occurrence of all header matching the name **name**,
3716 by only one containing the value **value**.
3717
3718 :param class_httpmessage http_msg: The manipulated HTTP message.
3719 :param string name: The header name.
3720 :param string value: The header value.
3721
3722 This function does the same work as the following code:
3723
3724.. code-block:: lua
3725
3726 http_msg:del_header("header")
3727 http_msg:add_header("header", "value")
3728..
3729
3730.. js:function:: HTTPMessage.set_method(http_msg, method)
3731
3732 Rewrites the request method with the string **method**. The HTTP message
3733 **http_msg** must be the request.
3734
3735 :param class_httpmessage http_msg: The manipulated HTTP message.
3736 :param string method: The new method.
3737
3738.. js:function:: HTTPMessage.set_path(http_msg, path)
3739
3740 Rewrites the request path with the string **path**. The HTTP message
3741 **http_msg** must be the request.
3742
3743 :param class_httpmessage http_msg: The manipulated HTTP message.
3744 :param string method: The new method.
3745
3746.. js:function:: HTTPMessage.set_query(http_msg, query)
3747
3748 Rewrites the request's query string which appears after the first question
3749 mark ("?") with the string **query**. The HTTP message **http_msg** must be
3750 the request.
3751
3752 :param class_httpmessage http_msg: The manipulated HTTP message.
3753 :param string query: The new query.
3754
3755.. js:function:: HTTPMessage.set_status(http_msg, status[, reason])
3756
Ilya Shipitsinff0f2782021-08-22 22:18:07 +05003757 Rewrites the response status code with the integer **code** and optional the
Christopher Faulet5a2c6612021-08-15 20:35:25 +02003758 reason **reason**. If no custom reason is provided, it will be generated from
3759 the status. The HTTP message **http_msg** must be the response.
3760
3761 :param class_httpmessage http_msg: The manipulated HTTP message.
3762 :param integer status: The new response status code.
3763 :param string reason: The new response reason (optional).
3764
3765.. js:function:: HTTPMessage.set_uri(http_msg, uri)
3766
3767 Rewrites the request URI with the string **uri**. The HTTP message
3768 **http_msg** must be the request.
3769
3770 :param class_httpmessage http_msg: The manipulated HTTP message.
3771 :param string uri: The new uri.
3772
3773.. js:function:: HTTPMessage.unset_eom(http_msg)
3774
3775 This function remove the end of message for the HTTP message **http_msg**.
3776
3777 :param class_httpmessage http_msg: The manipulated HTTP message.
3778
William Lallemand10cea5c2022-03-30 16:02:43 +02003779.. _CertCache_class:
3780
3781CertCache class
3782================
3783
3784.. js:class:: CertCache
3785
3786 This class allows to update an SSL certificate file in the memory of the
3787 current HAProxy process. It will do the same as "set ssl cert" + "commit ssl
3788 cert" over the HAProxy CLI.
3789
3790.. js:function:: CertCache.set(certificate)
3791
3792 This function updates a certificate in memory.
3793
3794 :param table certificate: A table containing the fields to update.
3795 :param string certificate.filename: The mandatory filename of the certificate
3796 to update, it must already exist in memory.
3797 :param string certificate.crt: A certificate in the PEM format. It can also
3798 contain a private key.
3799 :param string certificate.key: A private key in the PEM format.
3800 :param string certificate.ocsp: An OCSP response in base64. (cf management.txt)
3801 :param string certificate.issuer: The certificate of the OCSP issuer.
3802 :param string certificate.sctl: An SCTL file.
3803
3804.. code-block:: lua
3805
3806 CertCache.set{filename="certs/localhost9994.pem.rsa", crt=crt}
3807
3808
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01003809External Lua libraries
3810======================
3811
3812A lot of useful lua libraries can be found here:
3813
Aurelien DARRAGON846fc7d2022-10-14 08:48:57 +02003814* Lua toolbox has been superseded by `https://luarocks.org/ <https://luarocks.org/>`_
3815 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 +01003816
Ilya Shipitsin2075ca82020-03-06 23:22:22 +05003817Redis client library:
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01003818
3819* `https://github.com/nrk/redis-lua <https://github.com/nrk/redis-lua>`_
3820
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02003821This is an example about the usage of the Redis library within HAProxy. Note that
3822each call to any function of this library can throw an error if the socket
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01003823connection fails.
3824
3825.. code-block:: lua
3826
3827 -- load the redis library
3828 local redis = require("redis");
3829
3830 function do_something(txn)
3831
3832 -- create and connect new tcp socket
3833 local tcp = core.tcp();
3834 tcp:settimeout(1);
3835 tcp:connect("127.0.0.1", 6379);
3836
3837 -- use the redis library with this new socket
3838 local client = redis.connect({socket=tcp});
3839 client:ping();
3840
3841 end
3842
3843OpenSSL:
3844
3845* `http://mkottman.github.io/luacrypto/index.html
3846 <http://mkottman.github.io/luacrypto/index.html>`_
3847
3848* `https://github.com/brunoos/luasec/wiki
3849 <https://github.com/brunoos/luasec/wiki>`_