blob: 61557c76410206db083e14835c31786e44599379 [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
23functions. Lua have 6 execution context.
24
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 Fauletfce62942021-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
70HAProxy Lua Hello world
71-----------------------
72
73HAProxy configuration file (`hello_world.conf`):
74
75::
76
77 global
78 lua-load hello_world.lua
79
80 listen proxy
81 bind 127.0.0.1:10001
Thierry FOURNIERa2d02252015-10-01 15:00:42 +020082 tcp-request inspect-delay 1s
83 tcp-request content use-service lua.hello_world
Thierry FOURNIER17bd1522015-03-11 20:31:00 +010084
85HAProxy Lua file (`hello_world.lua`):
86
87.. code-block:: lua
88
Thierry FOURNIERa2d02252015-10-01 15:00:42 +020089 core.register_service("hello_world", "tcp", function(applet)
90 applet:send("hello world\n")
91 end)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +010092
93How to start HAProxy for testing this configuration:
94
95::
96
97 ./haproxy -f hello_world.conf
98
99On other terminal, you can test with telnet:
100
101::
102
103 #:~ telnet 127.0.0.1 10001
104 hello world
105
106Core class
107==========
108
109.. js:class:: core
110
111 The "core" class contains all the HAProxy core functions. These function are
112 useful for the controlling the execution flow, registering hooks, manipulating
113 global maps or ACL, ...
114
115 "core" class is basically provided with HAProxy. No `require` line is
116 required to uses these function.
117
David Carlier61fdf8b2015-10-02 11:59:38 +0100118 The "core" class is static, it is not possible to create a new object of this
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100119 type.
120
Christopher Faulet8263e942024-02-29 15:41:17 +0100121.. js:attribute:: core.silent
122
123 :returns: integer
124
125 This attribute is an integer, it contains the value -1. It is a special value
126 used to disable logging.
127
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100128.. js:attribute:: core.emerg
129
Thierry FOURNIERdc595002015-12-21 11:13:52 +0100130 :returns: integer
131
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100132 This attribute is an integer, it contains the value of the loglevel "emergency" (0).
133
134.. js:attribute:: core.alert
135
Thierry FOURNIERdc595002015-12-21 11:13:52 +0100136 :returns: integer
137
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100138 This attribute is an integer, it contains the value of the loglevel "alert" (1).
139
140.. js:attribute:: core.crit
141
Thierry FOURNIERdc595002015-12-21 11:13:52 +0100142 :returns: integer
143
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100144 This attribute is an integer, it contains the value of the loglevel "critical" (2).
145
146.. js:attribute:: core.err
147
Thierry FOURNIERdc595002015-12-21 11:13:52 +0100148 :returns: integer
149
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100150 This attribute is an integer, it contains the value of the loglevel "error" (3).
151
152.. js:attribute:: core.warning
153
Thierry FOURNIERdc595002015-12-21 11:13:52 +0100154 :returns: integer
155
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100156 This attribute is an integer, it contains the value of the loglevel "warning" (4).
157
158.. js:attribute:: core.notice
159
Thierry FOURNIERdc595002015-12-21 11:13:52 +0100160 :returns: integer
161
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100162 This attribute is an integer, it contains the value of the loglevel "notice" (5).
163
164.. js:attribute:: core.info
165
Thierry FOURNIERdc595002015-12-21 11:13:52 +0100166 :returns: integer
167
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100168 This attribute is an integer, it contains the value of the loglevel "info" (6).
169
170.. js:attribute:: core.debug
171
Thierry FOURNIERdc595002015-12-21 11:13:52 +0100172 :returns: integer
173
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100174 This attribute is an integer, it contains the value of the loglevel "debug" (7).
175
Thierry FOURNIER12a865d2016-12-14 19:40:37 +0100176.. js:attribute:: core.proxies
177
178 **context**: task, action, sample-fetch, converter
179
Patrick Hemmerc6a1d712018-05-01 21:30:41 -0400180 This attribute is a table of declared proxies (frontend and backends). Each
181 proxy give an access to his list of listeners and servers. The table is
182 indexed by proxy name, and each entry is of type :ref:`proxy_class`.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +0100183
Christopher Fauletfce62942021-08-11 10:14:30 +0200184 .. Warning::
185 if you are declared frontend and backend with the same name, only one of
186 these are listed.
Thierry FOURNIER9b82a582017-07-24 13:30:43 +0200187
188 :see: :js:attr:`core.backends`
189 :see: :js:attr:`core.frontends`
190
191.. js:attribute:: core.backends
192
193 **context**: task, action, sample-fetch, converter
194
Patrick Hemmerc6a1d712018-05-01 21:30:41 -0400195 This attribute is a table of declared proxies with backend capability. Each
196 proxy give an access to his list of listeners and servers. The table is
197 indexed by the backend name, and each entry is of type :ref:`proxy_class`.
Thierry FOURNIER9b82a582017-07-24 13:30:43 +0200198
199 :see: :js:attr:`core.proxies`
200 :see: :js:attr:`core.frontends`
201
202.. js:attribute:: core.frontends
203
204 **context**: task, action, sample-fetch, converter
205
Patrick Hemmerc6a1d712018-05-01 21:30:41 -0400206 This attribute is a table of declared proxies with frontend capability. Each
207 proxy give an access to his list of listeners and servers. The table is
208 indexed by the frontend name, and each entry is of type :ref:`proxy_class`.
Thierry FOURNIER9b82a582017-07-24 13:30:43 +0200209
210 :see: :js:attr:`core.proxies`
211 :see: :js:attr:`core.backends`
212
Thierry Fournierecb83c22020-11-28 15:49:44 +0100213.. js:attribute:: core.thread
214
215 **context**: task, action, sample-fetch, converter, applet
216
217 This variable contains the executing thread number starting at 1. 0 is a
218 special case for the common lua context. So, if thread is 0, Lua scope is
219 shared by all threads, otherwise the scope is dedicated to a single thread.
220 A program which needs to execute some parts exactly once regardless of the
221 number of threads can check that core.thread is 0 or 1.
222
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100223.. js:function:: core.log(loglevel, msg)
224
225 **context**: body, init, task, action, sample-fetch, converter
226
David Carlier61fdf8b2015-10-02 11:59:38 +0100227 This function sends a log. The log is sent, according with the HAProxy
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100228 configuration file, on the default syslog server if it is configured and on
229 the stderr if it is allowed.
230
Bertrand Jacquin874a35c2018-09-10 21:26:07 +0100231 :param integer loglevel: Is the log level associated with the message. It is a
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100232 number between 0 and 7.
233 :param string msg: The log content.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +0100234 :see: :js:attr:`core.emerg`, :js:attr:`core.alert`, :js:attr:`core.crit`,
235 :js:attr:`core.err`, :js:attr:`core.warning`, :js:attr:`core.notice`,
236 :js:attr:`core.info`, :js:attr:`core.debug` (log level definitions)
237 :see: :js:func:`core.Debug`
238 :see: :js:func:`core.Info`
239 :see: :js:func:`core.Warning`
240 :see: :js:func:`core.Alert`
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100241
242.. js:function:: core.Debug(msg)
243
244 **context**: body, init, task, action, sample-fetch, converter
245
246 :param string msg: The log content.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +0100247 :see: :js:func:`core.log`
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100248
249 Does the same job than:
250
251.. code-block:: lua
252
Thierry FOURNIERdc595002015-12-21 11:13:52 +0100253 function Debug(msg)
254 core.log(core.debug, msg)
255 end
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100256..
257
258.. js:function:: core.Info(msg)
259
260 **context**: body, init, task, action, sample-fetch, converter
261
262 :param string msg: The log content.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +0100263 :see: :js:func:`core.log`
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100264
265.. code-block:: lua
266
Thierry FOURNIERdc595002015-12-21 11:13:52 +0100267 function Info(msg)
268 core.log(core.info, msg)
269 end
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100270..
271
272.. js:function:: core.Warning(msg)
273
274 **context**: body, init, task, action, sample-fetch, converter
275
276 :param string msg: The log content.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +0100277 :see: :js:func:`core.log`
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100278
279.. code-block:: lua
280
Thierry FOURNIERdc595002015-12-21 11:13:52 +0100281 function Warning(msg)
282 core.log(core.warning, msg)
283 end
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100284..
285
286.. js:function:: core.Alert(msg)
287
288 **context**: body, init, task, action, sample-fetch, converter
289
290 :param string msg: The log content.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +0100291 :see: :js:func:`core.log`
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100292
293.. code-block:: lua
294
Thierry FOURNIERdc595002015-12-21 11:13:52 +0100295 function Alert(msg)
296 core.log(core.alert, msg)
297 end
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100298..
299
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100300.. js:function:: core.add_acl(filename, key)
301
302 **context**: init, task, action, sample-fetch, converter
303
304 Add the ACL *key* in the ACLs list referenced by the file *filename*.
305
306 :param string filename: the filename that reference the ACL entries.
307 :param string key: the key which will be added.
308
309.. js:function:: core.del_acl(filename, key)
310
311 **context**: init, task, action, sample-fetch, converter
312
313 Delete the ACL entry referenced by the key *key* in the list of ACLs
314 referenced by *filename*.
315
316 :param string filename: the filename that reference the ACL entries.
317 :param string key: the key which will be deleted.
318
319.. js:function:: core.del_map(filename, key)
320
321 **context**: init, task, action, sample-fetch, converter
322
323 Delete the map entry indexed with the specified key in the list of maps
324 referenced by his filename.
325
326 :param string filename: the filename that reference the map entries.
327 :param string key: the key which will be deleted.
328
Thierry Fourniereea77c02016-03-18 08:47:13 +0100329.. js:function:: core.get_info()
330
331 **context**: body, init, task, action, sample-fetch, converter
332
Ilya Shipitsin2075ca82020-03-06 23:22:22 +0500333 Returns HAProxy core information. We can found information like the uptime,
Thierry Fourniereea77c02016-03-18 08:47:13 +0100334 the pid, memory pool usage, tasks number, ...
335
Bertrand Jacquin874a35c2018-09-10 21:26:07 +0100336 These information are also returned by the management socket via the command
337 "show info". See the management socket documentation for more information
Thierry Fourniereea77c02016-03-18 08:47:13 +0100338 about the content of these variables.
339
340 :returns: an array of values.
341
Thierry Fournierb1f46562016-01-21 09:46:15 +0100342.. js:function:: core.now()
343
344 **context**: body, init, task, action
345
346 This function returns the current time. The time returned is fixed by the
Bertrand Jacquin874a35c2018-09-10 21:26:07 +0100347 HAProxy core and assures than the hour will be monotonic and that the system
Thierry Fournierb1f46562016-01-21 09:46:15 +0100348 call 'gettimeofday' will not be called too. The time is refreshed between each
349 Lua execution or resume, so two consecutive call to the function "now" will
350 probably returns the same result.
351
Patrick Hemmerc6a1d712018-05-01 21:30:41 -0400352 :returns: a table which contains two entries "sec" and "usec". "sec"
Thierry Fournierb1f46562016-01-21 09:46:15 +0100353 contains the current at the epoch format, and "usec" contains the
354 current microseconds.
355
Thierry FOURNIERa78f0372016-12-14 19:04:41 +0100356.. js:function:: core.http_date(date)
357
358 **context**: body, init, task, action
359
Bertrand Jacquin874a35c2018-09-10 21:26:07 +0100360 This function take a string representing http date, and returns an integer
Thierry FOURNIERa78f0372016-12-14 19:04:41 +0100361 containing the corresponding date with a epoch format. A valid http date
362 me respect the format IMF, RFC850 or ASCTIME.
363
364 :param string date: a date http-date formatted
365 :returns: integer containing epoch date
366 :see: :js:func:`core.imf_date`.
367 :see: :js:func:`core.rfc850_date`.
368 :see: :js:func:`core.asctime_date`.
369 :see: https://tools.ietf.org/html/rfc7231#section-7.1.1.1
370
371.. js:function:: core.imf_date(date)
372
373 **context**: body, init, task, action
374
Bertrand Jacquin874a35c2018-09-10 21:26:07 +0100375 This function take a string representing IMF date, and returns an integer
Thierry FOURNIERa78f0372016-12-14 19:04:41 +0100376 containing the corresponding date with a epoch format.
377
378 :param string date: a date IMF formatted
379 :returns: integer containing epoch date
380 :see: https://tools.ietf.org/html/rfc7231#section-7.1.1.1
381
382 The IMF format is like this:
383
384.. code-block:: text
385
386 Sun, 06 Nov 1994 08:49:37 GMT
387..
388
389.. js:function:: core.rfc850_date(date)
390
391 **context**: body, init, task, action
392
Bertrand Jacquin874a35c2018-09-10 21:26:07 +0100393 This function take a string representing RFC850 date, and returns an integer
Thierry FOURNIERa78f0372016-12-14 19:04:41 +0100394 containing the corresponding date with a epoch format.
395
396 :param string date: a date RFC859 formatted
397 :returns: integer containing epoch date
398 :see: https://tools.ietf.org/html/rfc7231#section-7.1.1.1
399
400 The RFC850 format is like this:
401
402.. code-block:: text
403
404 Sunday, 06-Nov-94 08:49:37 GMT
405..
406
407.. js:function:: core.asctime_date(date)
408
409 **context**: body, init, task, action
410
Bertrand Jacquin874a35c2018-09-10 21:26:07 +0100411 This function take a string representing ASCTIME date, and returns an integer
Thierry FOURNIERa78f0372016-12-14 19:04:41 +0100412 containing the corresponding date with a epoch format.
413
414 :param string date: a date ASCTIME formatted
415 :returns: integer containing epoch date
416 :see: https://tools.ietf.org/html/rfc7231#section-7.1.1.1
417
418 The ASCTIME format is like this:
419
420.. code-block:: text
421
422 Sun Nov 6 08:49:37 1994
423..
424
425.. js:function:: core.rfc850_date(date)
426
427 **context**: body, init, task, action
428
Bertrand Jacquin874a35c2018-09-10 21:26:07 +0100429 This function take a string representing http date, and returns an integer
Thierry FOURNIERa78f0372016-12-14 19:04:41 +0100430 containing the corresponding date with a epoch format.
431
432 :param string date: a date http-date formatted
433
434.. js:function:: core.asctime_date(date)
435
436 **context**: body, init, task, action
437
Bertrand Jacquin874a35c2018-09-10 21:26:07 +0100438 This function take a string representing http date, and returns an integer
Thierry FOURNIERa78f0372016-12-14 19:04:41 +0100439 containing the corresponding date with a epoch format.
440
441 :param string date: a date http-date formatted
442
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100443.. js:function:: core.msleep(milliseconds)
444
445 **context**: body, init, task, action
446
447 The `core.msleep()` stops the Lua execution between specified milliseconds.
448
449 :param integer milliseconds: the required milliseconds.
450
Thierry Fournierf61aa632016-02-19 20:56:00 +0100451.. js:attribute:: core.proxies
452
453 **context**: body, init, task, action, sample-fetch, converter
454
Bertrand Jacquin874a35c2018-09-10 21:26:07 +0100455 Proxies is a table containing the list of all proxies declared in the
Patrick Hemmerc6a1d712018-05-01 21:30:41 -0400456 configuration file. The table is indexed by the proxy name, and each entry
457 of the proxies table is an object of type :ref:`proxy_class`.
458
Christopher Fauletfce62942021-08-11 10:14:30 +0200459 .. warning::
460 if you have declared a frontend and backend with the same name, only one of
461 these are listed.
Thierry Fournierf61aa632016-02-19 20:56:00 +0100462
Thierry FOURNIERc5d11c62018-02-12 14:46:54 +0100463.. js:function:: core.register_action(name, actions, func [, nb_args])
Thierry FOURNIER8255a752015-09-23 21:03:35 +0200464
465 **context**: body
466
David Carlier61fdf8b2015-10-02 11:59:38 +0100467 Register a Lua function executed as action. All the registered action can be
Thierry FOURNIER8255a752015-09-23 21:03:35 +0200468 used in HAProxy with the prefix "lua.". An action gets a TXN object class as
469 input.
470
Aurelien DARRAGON6a083c72023-08-23 17:38:42 +0200471 :param string name: is the name of the action.
472 :param table actions: is a table of string describing the HAProxy actions
473 facilities where to expose the new action. Expected facilities are:
474 'tcp-req', 'tcp-res', 'http-req' or 'http-res'.
475 :param function func: is the Lua function called to work as an action.
Thierry FOURNIERc5d11c62018-02-12 14:46:54 +0100476 :param integer nb_args: is the expected number of argument for the action.
477 By default the value is 0.
Thierry FOURNIER8255a752015-09-23 21:03:35 +0200478
479 The prototype of the Lua function used as argument is:
480
481.. code-block:: lua
482
Thierry FOURNIERc5d11c62018-02-12 14:46:54 +0100483 function(txn [, arg1 [, arg2]])
Thierry FOURNIER8255a752015-09-23 21:03:35 +0200484..
485
Thierry FOURNIERdc595002015-12-21 11:13:52 +0100486 * **txn** (:ref:`txn_class`): this is a TXN object used for manipulating the
Thierry FOURNIER8255a752015-09-23 21:03:35 +0200487 current request or TCP stream.
488
Bertrand Jacquin874a35c2018-09-10 21:26:07 +0100489 * **argX**: this is argument provided through the HAProxy configuration file.
Thierry FOURNIERc5d11c62018-02-12 14:46:54 +0100490
Bertrand Jacquin874a35c2018-09-10 21:26:07 +0100491 Here, an example of action registration. The action just send an 'Hello world'
Thierry FOURNIER8255a752015-09-23 21:03:35 +0200492 in the logs.
493
494.. code-block:: lua
495
496 core.register_action("hello-world", { "tcp-req", "http-req" }, function(txn)
497 txn:Info("Hello world")
498 end)
499..
500
Willy Tarreau714f3452021-05-09 06:47:26 +0200501 This example code is used in HAProxy configuration like this:
Thierry FOURNIER8255a752015-09-23 21:03:35 +0200502
503::
504
505 frontend tcp_frt
506 mode tcp
507 tcp-request content lua.hello-world
508
509 frontend http_frt
510 mode http
511 http-request lua.hello-world
Thierry FOURNIERc5d11c62018-02-12 14:46:54 +0100512..
513
Bertrand Jacquin874a35c2018-09-10 21:26:07 +0100514 A second example using arguments
Thierry FOURNIERc5d11c62018-02-12 14:46:54 +0100515
516.. code-block:: lua
517
518 function hello_world(txn, arg)
519 txn:Info("Hello world for " .. arg)
520 end
521 core.register_action("hello-world", { "tcp-req", "http-req" }, hello_world, 2)
522..
Thierry FOURNIER8255a752015-09-23 21:03:35 +0200523
Willy Tarreau714f3452021-05-09 06:47:26 +0200524 This example code is used in HAProxy configuration like this:
Thierry FOURNIERc5d11c62018-02-12 14:46:54 +0100525
526::
527
528 frontend tcp_frt
529 mode tcp
530 tcp-request content lua.hello-world everybody
531..
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100532.. js:function:: core.register_converters(name, func)
533
534 **context**: body
535
David Carlier61fdf8b2015-10-02 11:59:38 +0100536 Register a Lua function executed as converter. All the registered converters
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100537 can be used in HAProxy with the prefix "lua.". An converter get a string as
538 input and return a string as output. The registered function can take up to 9
539 values as parameter. All the value are strings.
540
541 :param string name: is the name of the converter.
542 :param function func: is the Lua function called to work as converter.
543
544 The prototype of the Lua function used as argument is:
545
546.. code-block:: lua
547
548 function(str, [p1 [, p2 [, ... [, p5]]]])
549..
550
551 * **str** (*string*): this is the input value automatically converted in
552 string.
553 * **p1** .. **p5** (*string*): this is a list of string arguments declared in
Bertrand Jacquin874a35c2018-09-10 21:26:07 +0100554 the HAProxy configuration file. The number of arguments doesn't exceed 5.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100555 The order and the nature of these is conventionally choose by the
Bertrand Jacquin874a35c2018-09-10 21:26:07 +0100556 developer.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100557
558.. js:function:: core.register_fetches(name, func)
559
560 **context**: body
561
David Carlier61fdf8b2015-10-02 11:59:38 +0100562 Register a Lua function executed as sample fetch. All the registered sample
Bertrand Jacquin874a35c2018-09-10 21:26:07 +0100563 fetch can be used in HAProxy with the prefix "lua.". A Lua sample fetch
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100564 return a string as output. The registered function can take up to 9 values as
565 parameter. All the value are strings.
566
567 :param string name: is the name of the converter.
568 :param function func: is the Lua function called to work as sample fetch.
569
570 The prototype of the Lua function used as argument is:
571
572.. code-block:: lua
573
574 string function(txn, [p1 [, p2 [, ... [, p5]]]])
575..
576
Thierry FOURNIERdc595002015-12-21 11:13:52 +0100577 * **txn** (:ref:`txn_class`): this is the txn object associated with the current
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100578 request.
579 * **p1** .. **p5** (*string*): this is a list of string arguments declared in
Bertrand Jacquin874a35c2018-09-10 21:26:07 +0100580 the HAProxy configuration file. The number of arguments doesn't exceed 5.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100581 The order and the nature of these is conventionally choose by the
Bertrand Jacquin874a35c2018-09-10 21:26:07 +0100582 developer.
583 * **Returns**: A string containing some data, or nil if the value cannot be
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100584 returned now.
585
586 lua example code:
587
588.. code-block:: lua
589
590 core.register_fetches("hello", function(txn)
591 return "hello"
592 end)
593..
594
595 HAProxy example configuration:
596
597::
598
599 frontend example
600 http-request redirect location /%[lua.hello]
601
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +0200602.. js:function:: core.register_service(name, mode, func)
603
604 **context**: body
605
David Carlier61fdf8b2015-10-02 11:59:38 +0100606 Register a Lua function executed as a service. All the registered service can
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +0200607 be used in HAProxy with the prefix "lua.". A service gets an object class as
608 input according with the required mode.
609
610 :param string name: is the name of the converter.
Willy Tarreau61add3c2015-09-28 15:39:10 +0200611 :param string mode: is string describing the required mode. Only 'tcp' or
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +0200612 'http' are allowed.
613 :param function func: is the Lua function called to work as converter.
614
615 The prototype of the Lua function used as argument is:
616
617.. code-block:: lua
618
619 function(applet)
620..
621
Thierry FOURNIERdc595002015-12-21 11:13:52 +0100622 * **applet** *applet* will be a :ref:`applettcp_class` or a
623 :ref:`applethttp_class`. It depends the type of registered applet. An applet
624 registered with the 'http' value for the *mode* parameter will gets a
625 :ref:`applethttp_class`. If the *mode* value is 'tcp', the applet will gets
626 a :ref:`applettcp_class`.
627
Christopher Fauletfce62942021-08-11 10:14:30 +0200628 .. warning::
629 Applets of type 'http' cannot be called from 'tcp-*' rulesets. Only the
630 'http-*' rulesets are authorized, this means that is not possible to call
631 an HTTP applet from a proxy in tcp mode. Applets of type 'tcp' can be
632 called from anywhere.
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +0200633
Bertrand Jacquin874a35c2018-09-10 21:26:07 +0100634 Here, an example of service registration. The service just send an 'Hello world'
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +0200635 as an http response.
636
637.. code-block:: lua
638
Pieter Baauw4d7f7662015-11-08 16:38:08 +0100639 core.register_service("hello-world", "http", function(applet)
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +0200640 local response = "Hello World !"
641 applet:set_status(200)
Pieter Baauw2dcb9bc2015-10-01 22:47:12 +0200642 applet:add_header("content-length", string.len(response))
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +0200643 applet:add_header("content-type", "text/plain")
Pieter Baauw2dcb9bc2015-10-01 22:47:12 +0200644 applet:start_response()
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +0200645 applet:send(response)
646 end)
647..
648
Willy Tarreau714f3452021-05-09 06:47:26 +0200649 This example code is used in HAProxy configuration like this:
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +0200650
651::
652
653 frontend example
654 http-request use-service lua.hello-world
655
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100656.. js:function:: core.register_init(func)
657
658 **context**: body
659
660 Register a function executed after the configuration parsing. This is useful
661 to check any parameters.
662
Pieter Baauw4d7f7662015-11-08 16:38:08 +0100663 :param function func: is the Lua function called to work as initializer.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100664
665 The prototype of the Lua function used as argument is:
666
667.. code-block:: lua
668
669 function()
670..
671
672 It takes no input, and no output is expected.
673
674.. js:function:: core.register_task(func)
675
676 **context**: body, init, task, action, sample-fetch, converter
677
678 Register and start independent task. The task is started when the HAProxy
679 main scheduler starts. For example this type of tasks can be executed to
Thierry FOURNIER2e4893c2015-03-18 13:37:27 +0100680 perform complex health checks.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100681
Pieter Baauw4d7f7662015-11-08 16:38:08 +0100682 :param function func: is the Lua function called to work as initializer.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100683
684 The prototype of the Lua function used as argument is:
685
686.. code-block:: lua
687
688 function()
689..
690
691 It takes no input, and no output is expected.
692
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +0100693.. js:function:: core.register_cli([path], usage, func)
694
695 **context**: body
696
697 Register and start independent task. The task is started when the HAProxy
698 main scheduler starts. For example this type of tasks can be executed to
699 perform complex health checks.
700
701 :param array path: is the sequence of word for which the cli execute the Lua
702 binding.
703 :param string usage: is the usage message displayed in the help.
704 :param function func: is the Lua function called to handle the CLI commands.
705
706 The prototype of the Lua function used as argument is:
707
708.. code-block:: lua
709
710 function(AppletTCP, [arg1, [arg2, [...]]])
711..
712
713 I/O are managed with the :ref:`applettcp_class` object. Args are given as
Bertrand Jacquin874a35c2018-09-10 21:26:07 +0100714 parameter. The args embed the registered path. If the path is declared like
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +0100715 this:
716
717.. code-block:: lua
718
719 core.register_cli({"show", "ssl", "stats"}, "Display SSL stats..", function(applet, arg1, arg2, arg3, arg4, arg5)
720 end)
721..
722
723 And we execute this in the prompt:
724
725.. code-block:: text
726
727 > prompt
728 > show ssl stats all
729..
730
Bertrand Jacquin874a35c2018-09-10 21:26:07 +0100731 Then, arg1, arg2 and arg3 will contains respectively "show", "ssl" and "stats".
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +0100732 arg4 will contain "all". arg5 contains nil.
733
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100734.. js:function:: core.set_nice(nice)
735
736 **context**: task, action, sample-fetch, converter
737
738 Change the nice of the current task or current session.
Thierry FOURNIER2e4893c2015-03-18 13:37:27 +0100739
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100740 :param integer nice: the nice value, it must be between -1024 and 1024.
741
742.. js:function:: core.set_map(filename, key, value)
743
744 **context**: init, task, action, sample-fetch, converter
745
Bertrand Jacquin874a35c2018-09-10 21:26:07 +0100746 Set the value *value* associated to the key *key* in the map referenced by
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100747 *filename*.
748
Thierry FOURNIERdc595002015-12-21 11:13:52 +0100749 :param string filename: the Map reference
750 :param string key: the key to set or replace
751 :param string value: the associated value
752
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100753.. js:function:: core.sleep(int seconds)
754
755 **context**: body, init, task, action
756
757 The `core.sleep()` functions stop the Lua execution between specified seconds.
758
759 :param integer seconds: the required seconds.
760
761.. js:function:: core.tcp()
762
763 **context**: init, task, action
764
765 This function returns a new object of a *socket* class.
766
Thierry FOURNIERdc595002015-12-21 11:13:52 +0100767 :returns: A :ref:`socket_class` object.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100768
Thierry Fournier1de16592016-01-27 09:49:07 +0100769.. js:function:: core.concat()
770
771 **context**: body, init, task, action, sample-fetch, converter
772
Bertrand Jacquin874a35c2018-09-10 21:26:07 +0100773 This function returns a new concat object.
Thierry Fournier1de16592016-01-27 09:49:07 +0100774
775 :returns: A :ref:`concat_class` object.
776
Thierry FOURNIER0a99b892015-08-26 00:14:17 +0200777.. js:function:: core.done(data)
778
779 **context**: body, init, task, action, sample-fetch, converter
780
781 :param any data: Return some data for the caller. It is useful with
782 sample-fetches and sample-converters.
783
784 Immediately stops the current Lua execution and returns to the caller which
785 may be a sample fetch, a converter or an action and returns the specified
Thierry Fournier4234dbd2020-11-28 13:18:23 +0100786 value (ignored for actions and init). It is used when the LUA process finishes
787 its work and wants to give back the control to HAProxy without executing the
Thierry FOURNIER0a99b892015-08-26 00:14:17 +0200788 remaining code. It can be seen as a multi-level "return".
789
Thierry FOURNIER486f5a02015-03-16 15:13:03 +0100790.. js:function:: core.yield()
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100791
792 **context**: task, action, sample-fetch, converter
793
794 Give back the hand at the HAProxy scheduler. It is used when the LUA
795 processing consumes a lot of processing time.
796
Thierry FOURNIER / OZON.IO62fec752016-11-10 20:38:11 +0100797.. js:function:: core.parse_addr(address)
798
799 **context**: body, init, task, action, sample-fetch, converter
800
801 :param network: is a string describing an ipv4 or ipv6 address and optionally
802 its network length, like this: "127.0.0.1/8" or "aaaa::1234/32".
803 :returns: a userdata containing network or nil if an error occurs.
804
Bertrand Jacquin874a35c2018-09-10 21:26:07 +0100805 Parse ipv4 or ipv6 addresses and its facultative associated network.
Thierry FOURNIER / OZON.IO62fec752016-11-10 20:38:11 +0100806
807.. js:function:: core.match_addr(addr1, addr2)
808
809 **context**: body, init, task, action, sample-fetch, converter
810
811 :param addr1: is an address created with "core.parse_addr".
812 :param addr2: is an address created with "core.parse_addr".
Bertrand Jacquin874a35c2018-09-10 21:26:07 +0100813 :returns: boolean, true if the network of the addresses match, else returns
Thierry FOURNIER / OZON.IO62fec752016-11-10 20:38:11 +0100814 false.
815
Ilya Shipitsin2075ca82020-03-06 23:22:22 +0500816 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 +0100817 of network is not important.
818
Thierry FOURNIER / OZON.IO8a1027a2016-11-24 20:48:38 +0100819.. js:function:: core.tokenize(str, separators [, noblank])
820
821 **context**: body, init, task, action, sample-fetch, converter
822
823 This function is useful for tokenizing an entry, or splitting some messages.
824 :param string str: The string which will be split.
825 :param string separators: A string containing a list of separators.
826 :param boolean noblank: Ignore empty entries.
827 :returns: an array of string.
828
829 For example:
830
831.. code-block:: lua
832
833 local array = core.tokenize("This function is useful, for tokenizing an entry.", "., ", true)
834 print_r(array)
835..
836
837 Returns this array:
838
839.. code-block:: text
840
841 (table) table: 0x21c01e0 [
842 1: (string) "This"
843 2: (string) "function"
844 3: (string) "is"
845 4: (string) "useful"
846 5: (string) "for"
847 6: (string) "tokenizing"
848 7: (string) "an"
849 8: (string) "entry"
850 ]
851..
852
Thierry Fournierf61aa632016-02-19 20:56:00 +0100853.. _proxy_class:
854
855Proxy class
856============
857
858.. js:class:: Proxy
859
860 This class provides a way for manipulating proxy and retrieving information
861 like statistics.
862
Thierry FOURNIER817e7592017-07-24 14:35:04 +0200863.. js:attribute:: Proxy.name
864
865 Contain the name of the proxy.
866
Baptiste Assmann46c72552017-10-26 21:51:58 +0200867.. js:attribute:: Proxy.uuid
868
869 Contain the unique identifier of the proxy.
870
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +0100871.. js:attribute:: Proxy.servers
872
Patrick Hemmerc6a1d712018-05-01 21:30:41 -0400873 Contain a table with the attached servers. The table is indexed by server
874 name, and each server entry is an object of type :ref:`server_class`.
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +0100875
Adis Nezirovic8878f8e2018-07-13 12:18:33 +0200876.. js:attribute:: Proxy.stktable
877
Aurelien DARRAGON0c951622023-11-23 13:47:54 +0100878 Contains a stick table object of type :ref:`sticktable_class` attached to the
879 proxy.
Adis Nezirovic8878f8e2018-07-13 12:18:33 +0200880
Thierry Fournierff480422016-02-25 08:36:46 +0100881.. js:attribute:: Proxy.listeners
882
Patrick Hemmerc6a1d712018-05-01 21:30:41 -0400883 Contain a table with the attached listeners. The table is indexed by listener
884 name, and each each listeners entry is an object of type
885 :ref:`listener_class`.
Thierry Fournierff480422016-02-25 08:36:46 +0100886
Thierry Fournierf61aa632016-02-19 20:56:00 +0100887.. js:function:: Proxy.pause(px)
888
889 Pause the proxy. See the management socket documentation for more information.
890
891 :param class_proxy px: A :ref:`proxy_class` which indicates the manipulated
892 proxy.
893
894.. js:function:: Proxy.resume(px)
895
896 Resume the proxy. See the management socket documentation for more
897 information.
898
899 :param class_proxy px: A :ref:`proxy_class` which indicates the manipulated
900 proxy.
901
902.. js:function:: Proxy.stop(px)
903
904 Stop the proxy. See the management socket documentation for more information.
905
906 :param class_proxy px: A :ref:`proxy_class` which indicates the manipulated
907 proxy.
908
909.. js:function:: Proxy.shut_bcksess(px)
910
911 Kill the session attached to a backup server. See the management socket
912 documentation for more information.
913
914 :param class_proxy px: A :ref:`proxy_class` which indicates the manipulated
915 proxy.
916
917.. js:function:: Proxy.get_cap(px)
918
919 Returns a string describing the capabilities of the proxy.
920
921 :param class_proxy px: A :ref:`proxy_class` which indicates the manipulated
922 proxy.
923 :returns: a string "frontend", "backend", "proxy" or "ruleset".
924
925.. js:function:: Proxy.get_mode(px)
926
927 Returns a string describing the mode of the current proxy.
928
929 :param class_proxy px: A :ref:`proxy_class` which indicates the manipulated
Aurelien DARRAGON8b2a0052023-11-23 16:02:14 +0100930 proxy.
931 :returns: a string "tcp", "http" or "unknown"
Thierry Fournierf61aa632016-02-19 20:56:00 +0100932
933.. js:function:: Proxy.get_stats(px)
934
Bertrand Jacquin874a35c2018-09-10 21:26:07 +0100935 Returns a table containing the proxy statistics. The statistics returned are
Thierry Fournierf61aa632016-02-19 20:56:00 +0100936 not the same if the proxy is frontend or a backend.
937
938 :param class_proxy px: A :ref:`proxy_class` which indicates the manipulated
939 proxy.
Patrick Hemmerc6a1d712018-05-01 21:30:41 -0400940 :returns: a key/value table containing stats
Thierry Fournierf61aa632016-02-19 20:56:00 +0100941
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +0100942.. _server_class:
943
944Server class
945============
946
Patrick Hemmerc6a1d712018-05-01 21:30:41 -0400947.. js:class:: Server
948
949 This class provides a way for manipulating servers and retrieving information.
950
Patrick Hemmera62ae7e2018-04-29 14:23:48 -0400951.. js:attribute:: Server.name
952
953 Contain the name of the server.
954
955.. js:attribute:: Server.puid
956
957 Contain the proxy unique identifier of the server.
958
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +0100959.. js:function:: Server.is_draining(sv)
960
Patrick Hemmerc6a1d712018-05-01 21:30:41 -0400961 Return true if the server is currently draining sticky connections.
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +0100962
963 :param class_server sv: A :ref:`server_class` which indicates the manipulated
964 server.
965 :returns: a boolean
966
Patrick Hemmer32d539f2018-04-29 14:25:46 -0400967.. js:function:: Server.set_maxconn(sv, weight)
968
969 Dynamically change the maximum connections of the server. See the management
970 socket documentation for more information about the format of the string.
971
972 :param class_server sv: A :ref:`server_class` which indicates the manipulated
973 server.
974 :param string maxconn: A string describing the server maximum connections.
975
976.. js:function:: Server.get_maxconn(sv, weight)
977
978 This function returns an integer representing the server maximum connections.
979
980 :param class_server sv: A :ref:`server_class` which indicates the manipulated
981 server.
982 :returns: an integer.
983
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +0100984.. js:function:: Server.set_weight(sv, weight)
985
Patrick Hemmerc6a1d712018-05-01 21:30:41 -0400986 Dynamically change the weight of the server. See the management socket
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +0100987 documentation for more information about the format of the string.
988
989 :param class_server sv: A :ref:`server_class` which indicates the manipulated
990 server.
991 :param string weight: A string describing the server weight.
992
993.. js:function:: Server.get_weight(sv)
994
Patrick Hemmerc6a1d712018-05-01 21:30:41 -0400995 This function returns an integer representing the server weight.
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +0100996
997 :param class_server sv: A :ref:`server_class` which indicates the manipulated
998 server.
999 :returns: an integer.
1000
Joseph C. Sible49bbf522020-05-04 22:20:32 -04001001.. js:function:: Server.set_addr(sv, addr[, port])
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001002
Patrick Hemmerc6a1d712018-05-01 21:30:41 -04001003 Dynamically change the address of the server. See the management socket
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001004 documentation for more information about the format of the string.
1005
1006 :param class_server sv: A :ref:`server_class` which indicates the manipulated
1007 server.
Patrick Hemmerc6a1d712018-05-01 21:30:41 -04001008 :param string addr: A string describing the server address.
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001009
1010.. js:function:: Server.get_addr(sv)
1011
Patrick Hemmerc6a1d712018-05-01 21:30:41 -04001012 Returns a string describing the address of the server.
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001013
1014 :param class_server sv: A :ref:`server_class` which indicates the manipulated
1015 server.
1016 :returns: A string
1017
1018.. js:function:: Server.get_stats(sv)
1019
1020 Returns server statistics.
1021
1022 :param class_server sv: A :ref:`server_class` which indicates the manipulated
1023 server.
Patrick Hemmerc6a1d712018-05-01 21:30:41 -04001024 :returns: a key/value table containing stats
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001025
1026.. js:function:: Server.shut_sess(sv)
1027
1028 Shutdown all the sessions attached to the server. See the management socket
1029 documentation for more information about this function.
1030
1031 :param class_server sv: A :ref:`server_class` which indicates the manipulated
1032 server.
1033
1034.. js:function:: Server.set_drain(sv)
1035
1036 Drain sticky sessions. See the management socket documentation for more
1037 information about this function.
1038
1039 :param class_server sv: A :ref:`server_class` which indicates the manipulated
1040 server.
1041
1042.. js:function:: Server.set_maint(sv)
1043
1044 Set maintenance mode. See the management socket documentation for more
1045 information about this function.
1046
1047 :param class_server sv: A :ref:`server_class` which indicates the manipulated
1048 server.
1049
1050.. js:function:: Server.set_ready(sv)
1051
1052 Set normal mode. See the management socket documentation for more information
1053 about this function.
1054
1055 :param class_server sv: A :ref:`server_class` which indicates the manipulated
1056 server.
1057
1058.. js:function:: Server.check_enable(sv)
1059
1060 Enable health checks. See the management socket documentation for more
1061 information about this function.
1062
1063 :param class_server sv: A :ref:`server_class` which indicates the manipulated
1064 server.
1065
1066.. js:function:: Server.check_disable(sv)
1067
1068 Disable health checks. See the management socket documentation for more
1069 information about this function.
1070
1071 :param class_server sv: A :ref:`server_class` which indicates the manipulated
1072 server.
1073
1074.. js:function:: Server.check_force_up(sv)
1075
1076 Force health-check up. See the management socket documentation for more
1077 information about this function.
1078
1079 :param class_server sv: A :ref:`server_class` which indicates the manipulated
1080 server.
1081
1082.. js:function:: Server.check_force_nolb(sv)
1083
1084 Force health-check nolb mode. See the management socket documentation for more
1085 information about this function.
1086
1087 :param class_server sv: A :ref:`server_class` which indicates the manipulated
1088 server.
1089
1090.. js:function:: Server.check_force_down(sv)
1091
1092 Force health-check down. See the management socket documentation for more
1093 information about this function.
1094
1095 :param class_server sv: A :ref:`server_class` which indicates the manipulated
1096 server.
1097
1098.. js:function:: Server.agent_enable(sv)
1099
1100 Enable agent check. See the management socket documentation for more
1101 information about this function.
1102
1103 :param class_server sv: A :ref:`server_class` which indicates the manipulated
1104 server.
1105
1106.. js:function:: Server.agent_disable(sv)
1107
1108 Disable agent check. See the management socket documentation for more
1109 information about this function.
1110
1111 :param class_server sv: A :ref:`server_class` which indicates the manipulated
1112 server.
1113
1114.. js:function:: Server.agent_force_up(sv)
1115
1116 Force agent check up. See the management socket documentation for more
1117 information about this function.
1118
1119 :param class_server sv: A :ref:`server_class` which indicates the manipulated
1120 server.
1121
1122.. js:function:: Server.agent_force_down(sv)
1123
1124 Force agent check down. See the management socket documentation for more
1125 information about this function.
1126
1127 :param class_server sv: A :ref:`server_class` which indicates the manipulated
1128 server.
1129
Thierry Fournierff480422016-02-25 08:36:46 +01001130.. _listener_class:
1131
1132Listener class
1133==============
1134
1135.. js:function:: Listener.get_stats(ls)
1136
1137 Returns server statistics.
1138
1139 :param class_listener ls: A :ref:`listener_class` which indicates the
1140 manipulated listener.
Patrick Hemmerc6a1d712018-05-01 21:30:41 -04001141 :returns: a key/value table containing stats
Thierry Fournierff480422016-02-25 08:36:46 +01001142
Thierry Fournier1de16592016-01-27 09:49:07 +01001143.. _concat_class:
1144
1145Concat class
1146============
1147
1148.. js:class:: Concat
1149
1150 This class provides a fast way for string concatenation. The way using native
1151 Lua concatenation like the code below is slow for some reasons.
1152
1153.. code-block:: lua
1154
1155 str = "string1"
1156 str = str .. ", string2"
1157 str = str .. ", string3"
1158..
1159
1160 For each concatenation, Lua:
1161 * allocate memory for the result,
Ilya Shipitsin2075ca82020-03-06 23:22:22 +05001162 * catenate the two string copying the strings in the new memory block,
Bertrand Jacquin874a35c2018-09-10 21:26:07 +01001163 * free the old memory block containing the string which is no longer used.
Thierry Fournier1de16592016-01-27 09:49:07 +01001164 This process does many memory move, allocation and free. In addition, the
Bertrand Jacquin874a35c2018-09-10 21:26:07 +01001165 memory is not really freed, it is just mark mark as unused and wait for the
Thierry Fournier1de16592016-01-27 09:49:07 +01001166 garbage collector.
1167
Bertrand Jacquin874a35c2018-09-10 21:26:07 +01001168 The Concat class provide an alternative way to concatenate strings. It uses
Thierry Fournier1de16592016-01-27 09:49:07 +01001169 the internal Lua mechanism (it does not allocate memory), but it doesn't copy
1170 the data more than once.
1171
1172 On my computer, the following loops spends 0.2s for the Concat method and
1173 18.5s for the pure Lua implementation. So, the Concat class is about 1000x
1174 faster than the embedded solution.
1175
1176.. code-block:: lua
1177
1178 for j = 1, 100 do
1179 c = core.concat()
1180 for i = 1, 20000 do
1181 c:add("#####")
1182 end
1183 end
1184..
1185
1186.. code-block:: lua
1187
1188 for j = 1, 100 do
1189 c = ""
1190 for i = 1, 20000 do
1191 c = c .. "#####"
1192 end
1193 end
1194..
1195
1196.. js:function:: Concat.add(concat, string)
1197
1198 This function adds a string to the current concatenated string.
1199
1200 :param class_concat concat: A :ref:`concat_class` which contains the currently
Ilya Shipitsin2075ca82020-03-06 23:22:22 +05001201 built string.
Bertrand Jacquin874a35c2018-09-10 21:26:07 +01001202 :param string string: A new string to concatenate to the current built
Thierry Fournier1de16592016-01-27 09:49:07 +01001203 string.
1204
1205.. js:function:: Concat.dump(concat)
1206
Bertrand Jacquin874a35c2018-09-10 21:26:07 +01001207 This function returns the concatenated string.
Thierry Fournier1de16592016-01-27 09:49:07 +01001208
1209 :param class_concat concat: A :ref:`concat_class` which contains the currently
Ilya Shipitsin2075ca82020-03-06 23:22:22 +05001210 built string.
Thierry Fournier1de16592016-01-27 09:49:07 +01001211 :returns: the concatenated string
1212
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001213.. _fetches_class:
1214
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001215Fetches class
1216=============
1217
1218.. js:class:: Fetches
1219
1220 This class contains a lot of internal HAProxy sample fetches. See the
Thierry FOURNIER2e4893c2015-03-18 13:37:27 +01001221 HAProxy "configuration.txt" documentation for more information about her
Bertrand Jacquin874a35c2018-09-10 21:26:07 +01001222 usage. They are the chapters 7.3.2 to 7.3.6.
Thierry FOURNIER2e4893c2015-03-18 13:37:27 +01001223
Christopher Fauletfce62942021-08-11 10:14:30 +02001224 .. warning::
1225 some sample fetches are not available in some context. These limitations
1226 are specified in this documentation when they're useful.
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001227
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01001228 :see: :js:attr:`TXN.f`
1229 :see: :js:attr:`TXN.sf`
Thierry FOURNIER2e4893c2015-03-18 13:37:27 +01001230
1231 Fetches are useful for:
1232
1233 * get system time,
1234 * get environment variable,
1235 * get random numbers,
1236 * known backend status like the number of users in queue or the number of
1237 connections established,
1238 * client information like ip source or destination,
1239 * deal with stick tables,
Ilya Shipitsin2075ca82020-03-06 23:22:22 +05001240 * Established SSL information,
Thierry FOURNIER2e4893c2015-03-18 13:37:27 +01001241 * HTTP information like headers or method.
1242
1243.. code-block:: lua
1244
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001245 function action(txn)
1246 -- Get source IP
1247 local clientip = txn.f:src()
1248 end
Thierry FOURNIER2e4893c2015-03-18 13:37:27 +01001249..
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001250
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001251.. _converters_class:
1252
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001253Converters class
1254================
1255
1256.. js:class:: Converters
1257
1258 This class contains a lot of internal HAProxy sample converters. See the
Thierry FOURNIER2e4893c2015-03-18 13:37:27 +01001259 HAProxy documentation "configuration.txt" for more information about her
1260 usage. Its the chapter 7.3.1.
1261
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01001262 :see: :js:attr:`TXN.c`
1263 :see: :js:attr:`TXN.sc`
Thierry FOURNIER2e4893c2015-03-18 13:37:27 +01001264
1265 Converters provides statefull transformation. They are useful for:
1266
1267 * converting input to base64,
1268 * applying hash on input string (djb2, crc32, sdbm, wt6),
1269 * format date,
1270 * json escape,
Pieter Baauw4d7f7662015-11-08 16:38:08 +01001271 * extracting preferred language comparing two lists,
Thierry FOURNIER2e4893c2015-03-18 13:37:27 +01001272 * turn to lower or upper chars,
1273 * deal with stick tables.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001274
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001275.. _channel_class:
1276
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001277Channel class
1278=============
1279
1280.. js:class:: Channel
1281
1282 HAProxy uses two buffers for the processing of the requests. The first one is
1283 used with the request data (from the client to the server) and the second is
1284 used for the response data (from the server to the client).
1285
1286 Each buffer contains two types of data. The first type is the incoming data
1287 waiting for a processing. The second part is the outgoing data already
1288 processed. Usually, the incoming data is processed, after it is tagged as
1289 outgoing data, and finally it is sent. The following functions provides tools
1290 for manipulating these data in a buffer.
1291
1292 The following diagram shows where the channel class function are applied.
1293
1294 **Warning**: It is not possible to read from the response in request action,
1295 and it is not possible to read for the request channel in response action.
1296
Christopher Faulet1cda6c82021-06-14 11:43:18 +02001297 **Warning**: It is forbidden to alter the Channels buffer from HTTP contexts.
1298 So only :js:func:`Channel.get_in_length`, :js:func:`Channel.get_out_length`
1299 and :js:func:`Channel.is_full` can be called from an HTTP conetext.
1300
Thierry FOURNIER2e4893c2015-03-18 13:37:27 +01001301.. image:: _static/channel.png
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001302
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001303.. js:function:: Channel.dup(channel)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001304
1305 This function returns a string that contain the entire buffer. The data is
1306 not remove from the buffer and can be reprocessed later.
1307
Ilya Shipitsin2075ca82020-03-06 23:22:22 +05001308 If the buffer can't receive more data, a 'nil' value is returned.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001309
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001310 :param class_channel channel: The manipulated Channel.
Pieter Baauw4d7f7662015-11-08 16:38:08 +01001311 :returns: a string containing all the available data or nil.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001312
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001313.. js:function:: Channel.get(channel)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001314
1315 This function returns a string that contain the entire buffer. The data is
1316 consumed from the buffer.
1317
Ilya Shipitsin2075ca82020-03-06 23:22:22 +05001318 If the buffer can't receive more data, a 'nil' value is returned.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001319
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001320 :param class_channel channel: The manipulated Channel.
Pieter Baauw4d7f7662015-11-08 16:38:08 +01001321 :returns: a string containing all the available data or nil.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001322
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02001323.. js:function:: Channel.getline(channel)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001324
1325 This function returns a string that contain the first line of the buffer. The
1326 data is consumed. If the data returned doesn't contains a final '\n' its
1327 assumed than its the last available data in the buffer.
1328
Ilya Shipitsin2075ca82020-03-06 23:22:22 +05001329 If the buffer can't receive more data, a 'nil' value is returned.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001330
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001331 :param class_channel channel: The manipulated Channel.
Pieter Baauw386a1272015-08-16 15:26:24 +02001332 :returns: a string containing the available line or nil.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001333
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001334.. js:function:: Channel.set(channel, string)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001335
1336 This function replace the content of the buffer by the string. The function
1337 returns the copied length, otherwise, it returns -1.
1338
1339 The data set with this function are not send. They wait for the end of
1340 HAProxy processing, so the buffer can be full.
1341
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001342 :param class_channel channel: The manipulated Channel.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001343 :param string string: The data which will sent.
Pieter Baauw4d7f7662015-11-08 16:38:08 +01001344 :returns: an integer containing the amount of bytes copied or -1.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001345
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001346.. js:function:: Channel.append(channel, string)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001347
1348 This function append the string argument to the content of the buffer. The
1349 function returns the copied length, otherwise, it returns -1.
1350
1351 The data set with this function are not send. They wait for the end of
1352 HAProxy processing, so the buffer can be full.
1353
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001354 :param class_channel channel: The manipulated Channel.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001355 :param string string: The data which will sent.
Pieter Baauw4d7f7662015-11-08 16:38:08 +01001356 :returns: an integer containing the amount of bytes copied or -1.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001357
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001358.. js:function:: Channel.send(channel, string)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001359
1360 This function required immediate send of the data. Unless if the connection
1361 is close, the buffer is regularly flushed and all the string can be sent.
1362
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001363 :param class_channel channel: The manipulated Channel.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001364 :param string string: The data which will sent.
Pieter Baauw4d7f7662015-11-08 16:38:08 +01001365 :returns: an integer containing the amount of bytes copied or -1.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001366
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001367.. js:function:: Channel.get_in_length(channel)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001368
1369 This function returns the length of the input part of the buffer.
1370
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001371 :param class_channel channel: The manipulated Channel.
Pieter Baauw4d7f7662015-11-08 16:38:08 +01001372 :returns: an integer containing the amount of available bytes.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001373
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001374.. js:function:: Channel.get_out_length(channel)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001375
1376 This function returns the length of the output part of the buffer.
1377
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001378 :param class_channel channel: The manipulated Channel.
Pieter Baauw4d7f7662015-11-08 16:38:08 +01001379 :returns: an integer containing the amount of available bytes.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001380
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001381.. js:function:: Channel.forward(channel, int)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001382
1383 This function transfer bytes from the input part of the buffer to the output
1384 part.
1385
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001386 :param class_channel channel: The manipulated Channel.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001387 :param integer int: The amount of data which will be forwarded.
1388
Thierry FOURNIER / OZON.IO65192f32016-11-07 15:28:40 +01001389.. js:function:: Channel.is_full(channel)
1390
1391 This function returns true if the buffer channel is full.
1392
1393 :returns: a boolean
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001394
1395.. _http_class:
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001396
1397HTTP class
1398==========
1399
1400.. js:class:: HTTP
1401
1402 This class contain all the HTTP manipulation functions.
1403
Pieter Baauw386a1272015-08-16 15:26:24 +02001404.. js:function:: HTTP.req_get_headers(http)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001405
Patrick Hemmerc6a1d712018-05-01 21:30:41 -04001406 Returns a table containing all the request headers.
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001407
1408 :param class_http http: The related http object.
Patrick Hemmerc6a1d712018-05-01 21:30:41 -04001409 :returns: table of headers.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01001410 :see: :js:func:`HTTP.res_get_headers`
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001411
Patrick Hemmerc6a1d712018-05-01 21:30:41 -04001412 This is the form of the returned table:
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001413
1414.. code-block:: lua
1415
1416 HTTP:req_get_headers()['<header-name>'][<header-index>] = "<header-value>"
1417
1418 local hdr = HTTP:req_get_headers()
1419 hdr["host"][0] = "www.test.com"
1420 hdr["accept"][0] = "audio/basic q=1"
1421 hdr["accept"][1] = "audio/*, q=0.2"
1422 hdr["accept"][2] = "*/*, q=0.1"
1423..
1424
Pieter Baauw386a1272015-08-16 15:26:24 +02001425.. js:function:: HTTP.res_get_headers(http)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001426
Patrick Hemmerc6a1d712018-05-01 21:30:41 -04001427 Returns a table containing all the response headers.
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001428
1429 :param class_http http: The related http object.
Patrick Hemmerc6a1d712018-05-01 21:30:41 -04001430 :returns: table of headers.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01001431 :see: :js:func:`HTTP.req_get_headers`
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001432
Patrick Hemmerc6a1d712018-05-01 21:30:41 -04001433 This is the form of the returned table:
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001434
1435.. code-block:: lua
1436
1437 HTTP:res_get_headers()['<header-name>'][<header-index>] = "<header-value>"
1438
1439 local hdr = HTTP:req_get_headers()
1440 hdr["host"][0] = "www.test.com"
1441 hdr["accept"][0] = "audio/basic q=1"
1442 hdr["accept"][1] = "audio/*, q=0.2"
1443 hdr["accept"][2] = "*.*, q=0.1"
1444..
1445
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001446.. js:function:: HTTP.req_add_header(http, name, value)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001447
1448 Appends an HTTP header field in the request whose name is
1449 specified in "name" and whose value is defined in "value".
1450
1451 :param class_http http: The related http object.
1452 :param string name: The header name.
1453 :param string value: The header value.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01001454 :see: :js:func:`HTTP.res_add_header`
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001455
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001456.. js:function:: HTTP.res_add_header(http, name, value)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001457
Bertrand Jacquin874a35c2018-09-10 21:26:07 +01001458 Appends an HTTP header field in the response whose name is
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001459 specified in "name" and whose value is defined in "value".
1460
1461 :param class_http http: The related http object.
1462 :param string name: The header name.
1463 :param string value: The header value.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01001464 :see: :js:func:`HTTP.req_add_header`
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001465
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001466.. js:function:: HTTP.req_del_header(http, name)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001467
1468 Removes all HTTP header fields in the request whose name is
1469 specified in "name".
1470
1471 :param class_http http: The related http object.
1472 :param string name: The header name.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01001473 :see: :js:func:`HTTP.res_del_header`
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001474
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001475.. js:function:: HTTP.res_del_header(http, name)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001476
1477 Removes all HTTP header fields in the response whose name is
1478 specified in "name".
1479
1480 :param class_http http: The related http object.
1481 :param string name: The header name.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01001482 :see: :js:func:`HTTP.req_del_header`
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001483
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001484.. js:function:: HTTP.req_set_header(http, name, value)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001485
Bertrand Jacquin874a35c2018-09-10 21:26:07 +01001486 This variable replace all occurrence of all header "name", by only
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001487 one containing the "value".
1488
1489 :param class_http http: The related http object.
1490 :param string name: The header name.
1491 :param string value: The header value.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01001492 :see: :js:func:`HTTP.res_set_header`
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001493
Bertrand Jacquin874a35c2018-09-10 21:26:07 +01001494 This function does the same work as the following code:
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001495
1496.. code-block:: lua
1497
1498 function fcn(txn)
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001499 TXN.http:req_del_header("header")
1500 TXN.http:req_add_header("header", "value")
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001501 end
1502..
1503
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001504.. js:function:: HTTP.res_set_header(http, name, value)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001505
Bertrand Jacquin874a35c2018-09-10 21:26:07 +01001506 This variable replace all occurrence of all header "name", by only
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001507 one containing the "value".
1508
1509 :param class_http http: The related http object.
1510 :param string name: The header name.
1511 :param string value: The header value.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01001512 :see: :js:func:`HTTP.req_rep_header()`
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001513
Pieter Baauw386a1272015-08-16 15:26:24 +02001514.. js:function:: HTTP.req_rep_header(http, name, regex, replace)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001515
1516 Matches the regular expression in all occurrences of header field "name"
1517 according to "regex", and replaces them with the "replace" argument. The
1518 replacement value can contain back references like \1, \2, ... This
1519 function works with the request.
1520
1521 :param class_http http: The related http object.
1522 :param string name: The header name.
1523 :param string regex: The match regular expression.
1524 :param string replace: The replacement value.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01001525 :see: :js:func:`HTTP.res_rep_header()`
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001526
Pieter Baauw386a1272015-08-16 15:26:24 +02001527.. js:function:: HTTP.res_rep_header(http, name, regex, string)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001528
1529 Matches the regular expression in all occurrences of header field "name"
1530 according to "regex", and replaces them with the "replace" argument. The
1531 replacement value can contain back references like \1, \2, ... This
1532 function works with the request.
1533
1534 :param class_http http: The related http object.
1535 :param string name: The header name.
1536 :param string regex: The match regular expression.
1537 :param string replace: The replacement value.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01001538 :see: :js:func:`HTTP.req_rep_header()`
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001539
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001540.. js:function:: HTTP.req_set_method(http, method)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001541
1542 Rewrites the request method with the parameter "method".
1543
1544 :param class_http http: The related http object.
1545 :param string method: The new method.
1546
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001547.. js:function:: HTTP.req_set_path(http, path)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001548
1549 Rewrites the request path with the "path" parameter.
1550
1551 :param class_http http: The related http object.
1552 :param string path: The new path.
1553
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001554.. js:function:: HTTP.req_set_query(http, query)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001555
1556 Rewrites the request's query string which appears after the first question
1557 mark ("?") with the parameter "query".
1558
1559 :param class_http http: The related http object.
1560 :param string query: The new query.
1561
Thierry FOURNIER0d79cf62015-08-26 14:20:58 +02001562.. js:function:: HTTP.req_set_uri(http, uri)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001563
1564 Rewrites the request URI with the parameter "uri".
1565
1566 :param class_http http: The related http object.
1567 :param string uri: The new uri.
1568
Robin H. Johnson52f5db22017-01-01 13:10:52 -08001569.. js:function:: HTTP.res_set_status(http, status [, reason])
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +02001570
Robin H. Johnson52f5db22017-01-01 13:10:52 -08001571 Rewrites the response status code with the parameter "code".
1572
1573 If no custom reason is provided, it will be generated from the status.
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +02001574
1575 :param class_http http: The related http object.
1576 :param integer status: The new response status code.
Robin H. Johnson52f5db22017-01-01 13:10:52 -08001577 :param string reason: The new response reason (optional).
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +02001578
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001579.. _txn_class:
1580
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001581TXN class
1582=========
1583
1584.. js:class:: TXN
1585
1586 The txn class contain all the functions relative to the http or tcp
1587 transaction (Note than a tcp stream is the same than a tcp transaction, but
1588 an HTTP transaction is not the same than a tcp stream).
1589
1590 The usage of this class permits to retrieve data from the requests, alter it
1591 and forward it.
1592
1593 All the functions provided by this class are available in the context
1594 **sample-fetches** and **actions**.
1595
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001596.. js:attribute:: TXN.c
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001597
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001598 :returns: An :ref:`converters_class`.
1599
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001600 This attribute contains a Converters class object.
1601
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001602.. js:attribute:: TXN.sc
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001603
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001604 :returns: An :ref:`converters_class`.
1605
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001606 This attribute contains a Converters class object. The functions of
1607 this object returns always a string.
1608
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001609.. js:attribute:: TXN.f
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001610
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001611 :returns: An :ref:`fetches_class`.
1612
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001613 This attribute contains a Fetches class object.
1614
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001615.. js:attribute:: TXN.sf
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001616
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001617 :returns: An :ref:`fetches_class`.
1618
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001619 This attribute contains a Fetches class object. The functions of
1620 this object returns always a string.
1621
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001622.. js:attribute:: TXN.req
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001623
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001624 :returns: An :ref:`channel_class`.
1625
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001626 This attribute contains a channel class object for the request buffer.
1627
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001628.. js:attribute:: TXN.res
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001629
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001630 :returns: An :ref:`channel_class`.
1631
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001632 This attribute contains a channel class object for the response buffer.
1633
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001634.. js:attribute:: TXN.http
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001635
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001636 :returns: An :ref:`http_class`.
1637
Bertrand Jacquin874a35c2018-09-10 21:26:07 +01001638 This attribute contains an HTTP class object. It is available only if the
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001639 proxy has the "mode http" enabled.
1640
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01001641.. js:function:: TXN.log(TXN, loglevel, msg)
1642
1643 This function sends a log. The log is sent, according with the HAProxy
1644 configuration file, on the default syslog server if it is configured and on
1645 the stderr if it is allowed.
1646
1647 :param class_txn txn: The class txn object containing the data.
Bertrand Jacquin874a35c2018-09-10 21:26:07 +01001648 :param integer loglevel: Is the log level associated with the message. It is a
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01001649 number between 0 and 7.
1650 :param string msg: The log content.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01001651 :see: :js:attr:`core.emerg`, :js:attr:`core.alert`, :js:attr:`core.crit`,
1652 :js:attr:`core.err`, :js:attr:`core.warning`, :js:attr:`core.notice`,
1653 :js:attr:`core.info`, :js:attr:`core.debug` (log level definitions)
1654 :see: :js:func:`TXN.deflog`
1655 :see: :js:func:`TXN.Debug`
1656 :see: :js:func:`TXN.Info`
1657 :see: :js:func:`TXN.Warning`
1658 :see: :js:func:`TXN.Alert`
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01001659
1660.. js:function:: TXN.deflog(TXN, msg)
1661
Bertrand Jacquin874a35c2018-09-10 21:26:07 +01001662 Sends a log line with the default loglevel for the proxy associated with the
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01001663 transaction.
1664
1665 :param class_txn txn: The class txn object containing the data.
1666 :param string msg: The log content.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01001667 :see: :js:func:`TXN.log
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01001668
1669.. js:function:: TXN.Debug(txn, msg)
1670
1671 :param class_txn txn: The class txn object containing the data.
1672 :param string msg: The log content.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01001673 :see: :js:func:`TXN.log`
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01001674
1675 Does the same job than:
1676
1677.. code-block:: lua
1678
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001679 function Debug(txn, msg)
1680 TXN.log(txn, core.debug, msg)
1681 end
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01001682..
1683
1684.. js:function:: TXN.Info(txn, msg)
1685
1686 :param class_txn txn: The class txn object containing the data.
1687 :param string msg: The log content.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01001688 :see: :js:func:`TXN.log`
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01001689
1690.. code-block:: lua
1691
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001692 function Debug(txn, msg)
1693 TXN.log(txn, core.info, msg)
1694 end
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01001695..
1696
1697.. js:function:: TXN.Warning(txn, msg)
1698
1699 :param class_txn txn: The class txn object containing the data.
1700 :param string msg: The log content.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01001701 :see: :js:func:`TXN.log`
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01001702
1703.. code-block:: lua
1704
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001705 function Debug(txn, msg)
1706 TXN.log(txn, core.warning, msg)
1707 end
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01001708..
1709
1710.. js:function:: TXN.Alert(txn, msg)
1711
1712 :param class_txn txn: The class txn object containing the data.
1713 :param string msg: The log content.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01001714 :see: :js:func:`TXN.log`
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01001715
1716.. code-block:: lua
1717
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001718 function Debug(txn, msg)
1719 TXN.log(txn, core.alert, msg)
1720 end
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01001721..
1722
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001723.. js:function:: TXN.get_priv(txn)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001724
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001725 Return Lua data stored in the current transaction (with the `TXN.set_priv()`)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001726 function. If no data are stored, it returns a nil value.
1727
1728 :param class_txn txn: The class txn object containing the data.
Bertrand Jacquin874a35c2018-09-10 21:26:07 +01001729 :returns: the opaque data previously stored, or nil if nothing is
1730 available.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001731
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001732.. js:function:: TXN.set_priv(txn, data)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001733
1734 Store any data in the current HAProxy transaction. This action replace the
1735 old stored data.
1736
1737 :param class_txn txn: The class txn object containing the data.
1738 :param opaque data: The data which is stored in the transaction.
1739
Tim Duesterhus4e172c92020-05-19 13:49:42 +02001740.. js:function:: TXN.set_var(TXN, var, value[, ifexist])
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +02001741
David Carlier61fdf8b2015-10-02 11:59:38 +01001742 Converts a Lua type in a HAProxy type and store it in a variable <var>.
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +02001743
1744 :param class_txn txn: The class txn object containing the data.
1745 :param string var: The variable name according with the HAProxy variable syntax.
Thierry FOURNIER / OZON.IOb210bcc2016-12-12 16:24:16 +01001746 :param type value: The value associated to the variable. The type can be string or
1747 integer.
Tim Duesterhus4e172c92020-05-19 13:49:42 +02001748 :param boolean ifexist: If this parameter is set to a truthy value the variable
1749 will only be set if it was defined elsewhere (i.e. used
1750 within the configuration). It is highly recommended to
1751 always set this to true.
Christopher Faulet85d79c92016-11-09 16:54:56 +01001752
1753.. js:function:: TXN.unset_var(TXN, var)
1754
1755 Unset the variable <var>.
1756
1757 :param class_txn txn: The class txn object containing the data.
1758 :param string var: The variable name according with the HAProxy variable syntax.
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +02001759
1760.. js:function:: TXN.get_var(TXN, var)
1761
1762 Returns data stored in the variable <var> converter in Lua type.
1763
1764 :param class_txn txn: The class txn object containing the data.
1765 :param string var: The variable name according with the HAProxy variable syntax.
1766
Christopher Faulet700d9e82020-01-31 12:21:52 +01001767.. js:function:: TXN.reply([reply])
1768
1769 Return a new reply object
1770
1771 :param table reply: A table containing info to initialize the reply fields.
1772 :returns: A :ref:`reply_class` object.
1773
1774 The table used to initialized the reply object may contain following entries :
1775
1776 * status : The reply status code. the code 200 is used by default.
1777 * reason : The reply reason. The reason corresponding to the status code is
1778 used by default.
1779 * headers : An list of headers, indexed by header name. Empty by default. For
1780 a given name, multiple values are possible, stored in an ordered list.
1781 * body : The reply body, empty by default.
1782
1783.. code-block:: lua
1784
1785 local reply = txn:reply{
1786 status = 400,
1787 reason = "Bad request",
1788 headers = {
1789 ["content-type"] = { "text/html" },
1790 ["cache-control"] = {"no-cache", "no-store" }
1791 },
1792 body = "<html><body><h1>invalid request<h1></body></html>"
1793 }
1794..
1795 :see: :js:class:`Reply`
1796
1797.. js:function:: TXN.done(txn[, reply])
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001798
Willy Tarreaubc183a62015-08-28 10:39:11 +02001799 This function terminates processing of the transaction and the associated
Christopher Faulet700d9e82020-01-31 12:21:52 +01001800 session and optionally reply to the client for HTTP sessions.
1801
1802 :param class_txn txn: The class txn object containing the data.
1803 :param class_reply reply: The class reply object to return to the client.
1804
1805 This functions can be used when a critical error is detected or to terminate
Willy Tarreaubc183a62015-08-28 10:39:11 +02001806 processing after some data have been returned to the client (eg: a redirect).
Christopher Faulet700d9e82020-01-31 12:21:52 +01001807 To do so, a reply may be provided. This object is optional and may contain a
1808 status code, a reason, a header list and a body. All these fields are
Christopher Faulet8cee9152021-11-09 18:39:51 +01001809 optional. When not provided, the default values are used. By default, with an
1810 empty reply object, an empty HTTP 200 response is returned to the client. If
1811 no reply object is provided, the transaction is terminated without any
1812 reply. If a reply object is provided, it must not exceed the buffer size once
1813 converted into the internal HTTP representing. Because for now there is no
1814 easy way to be sure it fits, it is probably better to keep it reasonably
1815 small.
Christopher Faulet700d9e82020-01-31 12:21:52 +01001816
1817 The reply object may be fully created in lua or the class Reply may be used to
1818 create it.
1819
1820.. code-block:: lua
1821
1822 local reply = txn:reply()
1823 reply:set_status(400, "Bad request")
1824 reply:add_header("content-type", "text/html")
1825 reply:add_header("cache-control", "no-cache")
1826 reply:add_header("cache-control", "no-store")
1827 reply:set_body("<html><body><h1>invalid request<h1></body></html>")
1828 txn:done(reply)
1829..
1830
1831.. code-block:: lua
1832
1833 txn:done{
1834 status = 400,
1835 reason = "Bad request",
1836 headers = {
1837 ["content-type"] = { "text/html" },
1838 ["cache-control"] = { "no-cache", "no-store" },
1839 },
1840 body = "<html><body><h1>invalid request<h1></body></html>"
1841 }
1842..
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001843
Christopher Fauletfce62942021-08-11 10:14:30 +02001844 .. warning::
1845 It not make sense to call this function from sample-fetches. In this case
1846 the behaviour of this one is the same than core.done(): it quit the Lua
1847 execution. The transaction is really aborted only from an action registered
1848 function.
Thierry FOURNIERab00df62016-07-14 11:42:37 +02001849
Christopher Faulet700d9e82020-01-31 12:21:52 +01001850 :see: :js:func:`TXN.reply`, :js:class:`Reply`
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001851
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001852.. js:function:: TXN.set_loglevel(txn, loglevel)
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01001853
1854 Is used to change the log level of the current request. The "loglevel" must
Christopher Faulet8263e942024-02-29 15:41:17 +01001855 be an integer between 0 and 7 or the special value -1 to disable logging.
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01001856
1857 :param class_txn txn: The class txn object containing the data.
1858 :param integer loglevel: The required log level. This variable can be one of
Christopher Faulet8263e942024-02-29 15:41:17 +01001859 :see: :js:attr:`core.silent`, :js:attr:`core.emerg`, :js:attr:`core.alert`,
1860 :js:attr:`core.crit`, :js:attr:`core.err`, :js:attr:`core.warning`, :js:attr:`core.notice`,
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01001861 :js:attr:`core.info`, :js:attr:`core.debug` (log level definitions)
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01001862
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001863.. js:function:: TXN.set_tos(txn, tos)
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01001864
1865 Is used to set the TOS or DSCP field value of packets sent to the client to
1866 the value passed in "tos" on platforms which support this.
1867
1868 :param class_txn txn: The class txn object containing the data.
1869 :param integer tos: The new TOS os DSCP.
1870
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001871.. js:function:: TXN.set_mark(txn, mark)
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01001872
1873 Is used to set the Netfilter MARK on all packets sent to the client to the
1874 value passed in "mark" on platforms which support it.
1875
1876 :param class_txn txn: The class txn object containing the data.
1877 :param integer mark: The mark value.
1878
Patrick Hemmer268a7072018-05-11 12:52:31 -04001879.. js:function:: TXN.set_priority_class(txn, prio)
1880
1881 This function adjusts the priority class of the transaction. The value should
1882 be within the range -2047..2047. Values outside this range will be
1883 truncated.
1884
1885 See the HAProxy configuration.txt file keyword "http-request" action
1886 "set-priority-class" for details.
1887
1888.. js:function:: TXN.set_priority_offset(txn, prio)
1889
1890 This function adjusts the priority offset of the transaction. The value
1891 should be within the range -524287..524287. Values outside this range will be
1892 truncated.
1893
1894 See the HAProxy configuration.txt file keyword "http-request" action
1895 "set-priority-offset" for details.
1896
Christopher Faulet700d9e82020-01-31 12:21:52 +01001897.. _reply_class:
1898
1899Reply class
1900============
1901
1902.. js:class:: Reply
1903
1904 **context**: action
1905
1906 This class represents an HTTP response message. It provides some methods to
Christopher Faulet8cee9152021-11-09 18:39:51 +01001907 enrich it. Once converted into the internal HTTP representation, the response
1908 message must not exceed the buffer size. Because for now there is no
1909 easy way to be sure it fits, it is probably better to keep it reasonably
1910 small.
1911
1912 See tune.bufsize in the configuration manual for dettails.
Christopher Faulet700d9e82020-01-31 12:21:52 +01001913
1914.. code-block:: lua
1915
1916 local reply = txn:reply({status = 400}) -- default HTTP 400 reason-phase used
1917 reply:add_header("content-type", "text/html")
1918 reply:add_header("cache-control", "no-cache")
1919 reply:add_header("cache-control", "no-store")
1920 reply:set_body("<html><body><h1>invalid request<h1></body></html>")
1921..
1922
1923 :see: :js:func:`TXN.reply`
1924
1925.. js:attribute:: Reply.status
1926
1927 The reply status code. By default, the status code is set to 200.
1928
1929 :returns: integer
1930
1931.. js:attribute:: Reply.reason
1932
1933 The reason string describing the status code.
1934
1935 :returns: string
1936
1937.. js:attribute:: Reply.headers
1938
1939 A table indexing all reply headers by name. To each name is associated an
1940 ordered list of values.
1941
1942 :returns: Lua table
1943
1944.. code-block:: lua
1945
1946 {
1947 ["content-type"] = { "text/html" },
1948 ["cache-control"] = {"no-cache", "no-store" },
1949 x_header_name = { "value1", "value2", ... }
1950 ...
1951 }
1952..
1953
1954.. js:attribute:: Reply.body
1955
1956 The reply payload.
1957
1958 :returns: string
1959
1960.. js:function:: Reply.set_status(REPLY, status[, reason])
1961
1962 Set the reply status code and optionally the reason-phrase. If the reason is
1963 not provided, the default reason corresponding to the status code is used.
1964
1965 :param class_reply reply: The related Reply object.
1966 :param integer status: The reply status code.
1967 :param string reason: The reply status reason (optional).
1968
1969.. js:function:: Reply.add_header(REPLY, name, value)
1970
1971 Add a header to the reply object. If the header does not already exist, a new
1972 entry is created with its name as index and a one-element list containing its
1973 value as value. Otherwise, the header value is appended to the ordered list of
1974 values associated to the header name.
1975
1976 :param class_reply reply: The related Reply object.
1977 :param string name: The header field name.
1978 :param string value: The header field value.
1979
1980.. js:function:: Reply.del_header(REPLY, name)
1981
1982 Remove all occurrences of a header name from the reply object.
1983
1984 :param class_reply reply: The related Reply object.
1985 :param string name: The header field name.
1986
1987.. js:function:: Reply.set_body(REPLY, body)
1988
1989 Set the reply payload.
1990
1991 :param class_reply reply: The related Reply object.
1992 :param string body: The reply payload.
1993
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001994.. _socket_class:
1995
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001996Socket class
1997============
1998
1999.. js:class:: Socket
2000
2001 This class must be compatible with the Lua Socket class. Only the 'client'
2002 functions are available. See the Lua Socket documentation:
2003
2004 `http://w3.impa.br/~diego/software/luasocket/tcp.html
2005 <http://w3.impa.br/~diego/software/luasocket/tcp.html>`_
2006
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002007.. js:function:: Socket.close(socket)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002008
2009 Closes a TCP object. The internal socket used by the object is closed and the
2010 local address to which the object was bound is made available to other
2011 applications. No further operations (except for further calls to the close
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002012 method) are allowed on a closed Socket.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002013
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002014 :param class_socket socket: Is the manipulated Socket.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002015
2016 Note: It is important to close all used sockets once they are not needed,
2017 since, in many systems, each socket uses a file descriptor, which are limited
2018 system resources. Garbage-collected objects are automatically closed before
2019 destruction, though.
2020
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002021.. js:function:: Socket.connect(socket, address[, port])
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002022
2023 Attempts to connect a socket object to a remote host.
2024
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002025
2026 In case of error, the method returns nil followed by a string describing the
2027 error. In case of success, the method returns 1.
2028
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002029 :param class_socket socket: Is the manipulated Socket.
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002030 :param string address: can be an IP address or a host name. See below for more
2031 information.
2032 :param integer port: must be an integer number in the range [1..64K].
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002033 :returns: 1 or nil.
2034
Bertrand Jacquin874a35c2018-09-10 21:26:07 +01002035 An address field extension permits to use the connect() function to connect to
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002036 other stream than TCP. The syntax containing a simpleipv4 or ipv6 address is
2037 the basically expected format. This format requires the port.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002038
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002039 Other format accepted are a socket path like "/socket/path", it permits to
Bertrand Jacquin874a35c2018-09-10 21:26:07 +01002040 connect to a socket. Abstract namespaces are supported with the prefix
Joseph Herlant02cedc42018-11-13 19:45:17 -08002041 "abns@", and finally a file descriptor can be passed with the prefix "fd@".
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002042 The prefix "ipv4@", "ipv6@" and "unix@" are also supported. The port can be
Bertrand Jacquin874a35c2018-09-10 21:26:07 +01002043 passed int the string. The syntax "127.0.0.1:1234" is valid. In this case, the
Tim Duesterhus6edab862018-01-06 19:04:45 +01002044 parameter *port* must not be set.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002045
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002046.. js:function:: Socket.connect_ssl(socket, address, port)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002047
2048 Same behavior than the function socket:connect, but uses SSL.
2049
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002050 :param class_socket socket: Is the manipulated Socket.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002051 :returns: 1 or nil.
2052
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002053.. js:function:: Socket.getpeername(socket)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002054
2055 Returns information about the remote side of a connected client object.
2056
2057 Returns a string with the IP address of the peer, followed by the port number
2058 that peer is using for the connection. In case of error, the method returns
2059 nil.
2060
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002061 :param class_socket socket: Is the manipulated Socket.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002062 :returns: a string containing the server information.
2063
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002064.. js:function:: Socket.getsockname(socket)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002065
2066 Returns the local address information associated to the object.
2067
2068 The method returns a string with local IP address and a number with the port.
2069 In case of error, the method returns nil.
2070
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002071 :param class_socket socket: Is the manipulated Socket.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002072 :returns: a string containing the client information.
2073
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002074.. js:function:: Socket.receive(socket, [pattern [, prefix]])
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002075
2076 Reads data from a client object, according to the specified read pattern.
2077 Patterns follow the Lua file I/O format, and the difference in performance
2078 between all patterns is negligible.
2079
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002080 :param class_socket socket: Is the manipulated Socket.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002081 :param string|integer pattern: Describe what is required (see below).
2082 :param string prefix: A string which will be prefix the returned data.
2083 :returns: a string containing the required data or nil.
2084
2085 Pattern can be any of the following:
2086
2087 * **`*a`**: reads from the socket until the connection is closed. No
2088 end-of-line translation is performed;
2089
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002090 * **`*l`**: reads a line of text from the Socket. The line is terminated by a
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002091 LF character (ASCII 10), optionally preceded by a CR character
2092 (ASCII 13). The CR and LF characters are not included in the
2093 returned line. In fact, all CR characters are ignored by the
2094 pattern. This is the default pattern.
2095
2096 * **number**: causes the method to read a specified number of bytes from the
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002097 Socket. Prefix is an optional string to be concatenated to the
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002098 beginning of any received data before return.
2099
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002100 * **empty**: If the pattern is left empty, the default option is `*l`.
2101
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002102 If successful, the method returns the received pattern. In case of error, the
2103 method returns nil followed by an error message which can be the string
2104 'closed' in case the connection was closed before the transmission was
2105 completed or the string 'timeout' in case there was a timeout during the
2106 operation. Also, after the error message, the function returns the partial
2107 result of the transmission.
2108
2109 Important note: This function was changed severely. It used to support
2110 multiple patterns (but I have never seen this feature used) and now it
2111 doesn't anymore. Partial results used to be returned in the same way as
2112 successful results. This last feature violated the idea that all functions
2113 should return nil on error. Thus it was changed too.
2114
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002115.. js:function:: Socket.send(socket, data [, start [, end ]])
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002116
2117 Sends data through client object.
2118
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002119 :param class_socket socket: Is the manipulated Socket.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002120 :param string data: The data that will be sent.
2121 :param integer start: The start position in the buffer of the data which will
2122 be sent.
2123 :param integer end: The end position in the buffer of the data which will
2124 be sent.
2125 :returns: see below.
2126
2127 Data is the string to be sent. The optional arguments i and j work exactly
2128 like the standard string.sub Lua function to allow the selection of a
2129 substring to be sent.
2130
2131 If successful, the method returns the index of the last byte within [start,
2132 end] that has been sent. Notice that, if start is 1 or absent, this is
2133 effectively the total number of bytes sent. In case of error, the method
2134 returns nil, followed by an error message, followed by the index of the last
2135 byte within [start, end] that has been sent. You might want to try again from
2136 the byte following that. The error message can be 'closed' in case the
2137 connection was closed before the transmission was completed or the string
2138 'timeout' in case there was a timeout during the operation.
2139
2140 Note: Output is not buffered. For small strings, it is always better to
2141 concatenate them in Lua (with the '..' operator) and send the result in one
2142 call instead of calling the method several times.
2143
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002144.. js:function:: Socket.setoption(socket, option [, value])
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002145
2146 Just implemented for compatibility, this cal does nothing.
2147
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002148.. js:function:: Socket.settimeout(socket, value [, mode])
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002149
2150 Changes the timeout values for the object. All I/O operations are blocking.
2151 That is, any call to the methods send, receive, and accept will block
2152 indefinitely, until the operation completes. The settimeout method defines a
2153 limit on the amount of time the I/O methods can block. When a timeout time
2154 has elapsed, the affected methods give up and fail with an error code.
2155
2156 The amount of time to wait is specified as the value parameter, in seconds.
2157
Mark Lakes56cc1252018-03-27 09:48:06 +02002158 The timeout modes are not implemented, the only settable timeout is the
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002159 inactivity time waiting for complete the internal buffer send or waiting for
2160 receive data.
2161
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002162 :param class_socket socket: Is the manipulated Socket.
Bertrand Jacquin874a35c2018-09-10 21:26:07 +01002163 :param float value: The timeout value. Use floating point to specify
Mark Lakes56cc1252018-03-27 09:48:06 +02002164 milliseconds.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002165
Thierry FOURNIER31904272017-10-25 12:59:51 +02002166.. _regex_class:
2167
2168Regex class
2169===========
2170
2171.. js:class:: Regex
2172
2173 This class allows the usage of HAProxy regexes because classic lua doesn't
2174 provides regexes. This class inherits the HAProxy compilation options, so the
2175 regexes can be libc regex, pcre regex or pcre JIT regex.
2176
2177 The expression matching number is limited to 20 per regex. The only available
2178 option is case sensitive.
2179
2180 Because regexes compilation is a heavy process, it is better to define all
2181 your regexes in the **body context** and use it during the runtime.
2182
2183.. code-block:: lua
2184
2185 -- Create the regex
2186 st, regex = Regex.new("needle (..) (...)", true);
2187
2188 -- Check compilation errors
2189 if st == false then
2190 print "error: " .. regex
2191 end
2192
2193 -- Match the regexes
2194 print(regex:exec("Looking for a needle in the haystack")) -- true
2195 print(regex:exec("Lokking for a cat in the haystack")) -- false
2196
2197 -- Extract words
2198 st, list = regex:match("Looking for a needle in the haystack")
2199 print(st) -- true
2200 print(list[1]) -- needle in the
2201 print(list[2]) -- in
2202 print(list[3]) -- the
2203
2204.. js:function:: Regex.new(regex, case_sensitive)
2205
2206 Create and compile a regex.
2207
2208 :param string regex: The regular expression according with the libc or pcre
2209 standard
2210 :param boolean case_sensitive: Match is case sensitive or not.
2211 :returns: boolean status and :ref:`regex_class` or string containing fail reason.
2212
2213.. js:function:: Regex.exec(regex, str)
2214
2215 Execute the regex.
2216
2217 :param class_regex regex: A :ref:`regex_class` object.
2218 :param string str: The input string will be compared with the compiled regex.
2219 :returns: a boolean status according with the match result.
2220
2221.. js:function:: Regex.match(regex, str)
2222
2223 Execute the regex and return matched expressions.
2224
2225 :param class_map map: A :ref:`regex_class` object.
2226 :param string str: The input string will be compared with the compiled regex.
2227 :returns: a boolean status according with the match result, and
2228 a table containing all the string matched in order of declaration.
2229
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002230.. _map_class:
2231
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002232Map class
2233=========
2234
2235.. js:class:: Map
2236
2237 This class permits to do some lookup in HAProxy maps. The declared maps can
Bertrand Jacquin874a35c2018-09-10 21:26:07 +01002238 be modified during the runtime through the HAProxy management socket.
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002239
2240.. code-block:: lua
2241
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002242 default = "usa"
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002243
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002244 -- Create and load map
Thierry FOURNIER4dc71972017-01-28 08:33:08 +01002245 geo = Map.new("geo.map", Map._ip);
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002246
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002247 -- Create new fetch that returns the user country
2248 core.register_fetches("country", function(txn)
2249 local src;
2250 local loc;
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002251
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002252 src = txn.f:fhdr("x-forwarded-for");
2253 if (src == nil) then
2254 src = txn.f:src()
2255 if (src == nil) then
2256 return default;
2257 end
2258 end
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002259
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002260 -- Perform lookup
2261 loc = geo:lookup(src);
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002262
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002263 if (loc == nil) then
2264 return default;
2265 end
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002266
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002267 return loc;
2268 end);
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002269
Thierry FOURNIER4dc71972017-01-28 08:33:08 +01002270.. js:attribute:: Map._int
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002271
2272 See the HAProxy configuration.txt file, chapter "Using ACLs and fetching
Ilya Shipitsin2075ca82020-03-06 23:22:22 +05002273 samples" and subchapter "ACL basics" to understand this pattern matching
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002274 method.
2275
Thierry FOURNIER4dc71972017-01-28 08:33:08 +01002276 Note that :js:attr:`Map.int` is also available for compatibility.
2277
2278.. js:attribute:: Map._ip
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002279
2280 See the HAProxy configuration.txt file, chapter "Using ACLs and fetching
Ilya Shipitsin2075ca82020-03-06 23:22:22 +05002281 samples" and subchapter "ACL basics" to understand this pattern matching
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002282 method.
2283
Thierry FOURNIER4dc71972017-01-28 08:33:08 +01002284 Note that :js:attr:`Map.ip` is also available for compatibility.
2285
2286.. js:attribute:: Map._str
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002287
2288 See the HAProxy configuration.txt file, chapter "Using ACLs and fetching
Ilya Shipitsin2075ca82020-03-06 23:22:22 +05002289 samples" and subchapter "ACL basics" to understand this pattern matching
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002290 method.
2291
Thierry FOURNIER4dc71972017-01-28 08:33:08 +01002292 Note that :js:attr:`Map.str` is also available for compatibility.
2293
2294.. js:attribute:: Map._beg
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002295
2296 See the HAProxy configuration.txt file, chapter "Using ACLs and fetching
Ilya Shipitsin2075ca82020-03-06 23:22:22 +05002297 samples" and subchapter "ACL basics" to understand this pattern matching
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002298 method.
2299
Thierry FOURNIER4dc71972017-01-28 08:33:08 +01002300 Note that :js:attr:`Map.beg` is also available for compatibility.
2301
2302.. js:attribute:: Map._sub
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002303
2304 See the HAProxy configuration.txt file, chapter "Using ACLs and fetching
Ilya Shipitsin2075ca82020-03-06 23:22:22 +05002305 samples" and subchapter "ACL basics" to understand this pattern matching
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002306 method.
2307
Thierry FOURNIER4dc71972017-01-28 08:33:08 +01002308 Note that :js:attr:`Map.sub` is also available for compatibility.
2309
2310.. js:attribute:: Map._dir
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002311
2312 See the HAProxy configuration.txt file, chapter "Using ACLs and fetching
Ilya Shipitsin2075ca82020-03-06 23:22:22 +05002313 samples" and subchapter "ACL basics" to understand this pattern matching
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002314 method.
2315
Thierry FOURNIER4dc71972017-01-28 08:33:08 +01002316 Note that :js:attr:`Map.dir` is also available for compatibility.
2317
2318.. js:attribute:: Map._dom
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002319
2320 See the HAProxy configuration.txt file, chapter "Using ACLs and fetching
Ilya Shipitsin2075ca82020-03-06 23:22:22 +05002321 samples" and subchapter "ACL basics" to understand this pattern matching
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002322 method.
2323
Thierry FOURNIER4dc71972017-01-28 08:33:08 +01002324 Note that :js:attr:`Map.dom` is also available for compatibility.
2325
2326.. js:attribute:: Map._end
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002327
2328 See the HAProxy configuration.txt file, chapter "Using ACLs and fetching
Ilya Shipitsin2075ca82020-03-06 23:22:22 +05002329 samples" and subchapter "ACL basics" to understand this pattern matching
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002330 method.
2331
Thierry FOURNIER4dc71972017-01-28 08:33:08 +01002332.. js:attribute:: Map._reg
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002333
2334 See the HAProxy configuration.txt file, chapter "Using ACLs and fetching
Ilya Shipitsin2075ca82020-03-06 23:22:22 +05002335 samples" and subchapter "ACL basics" to understand this pattern matching
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002336 method.
2337
Thierry FOURNIER4dc71972017-01-28 08:33:08 +01002338 Note that :js:attr:`Map.reg` is also available for compatibility.
2339
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002340
2341.. js:function:: Map.new(file, method)
2342
2343 Creates and load a map.
2344
2345 :param string file: Is the file containing the map.
2346 :param integer method: Is the map pattern matching method. See the attributes
2347 of the Map class.
2348 :returns: a class Map object.
Thierry FOURNIER4dc71972017-01-28 08:33:08 +01002349 :see: The Map attributes: :js:attr:`Map._int`, :js:attr:`Map._ip`,
2350 :js:attr:`Map._str`, :js:attr:`Map._beg`, :js:attr:`Map._sub`,
2351 :js:attr:`Map._dir`, :js:attr:`Map._dom`, :js:attr:`Map._end` and
2352 :js:attr:`Map._reg`.
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002353
2354.. js:function:: Map.lookup(map, str)
2355
2356 Perform a lookup in a map.
2357
2358 :param class_map map: Is the class Map object.
2359 :param string str: Is the string used as key.
2360 :returns: a string containing the result or nil if no match.
2361
2362.. js:function:: Map.slookup(map, str)
2363
2364 Perform a lookup in a map.
2365
2366 :param class_map map: Is the class Map object.
2367 :param string str: Is the string used as key.
2368 :returns: a string containing the result or empty string if no match.
2369
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002370.. _applethttp_class:
2371
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002372AppletHTTP class
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002373================
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002374
2375.. js:class:: AppletHTTP
2376
2377 This class is used with applets that requires the 'http' mode. The http applet
2378 can be registered with the *core.register_service()* function. They are used
2379 for processing an http request like a server in back of HAProxy.
2380
2381 This is an hello world sample code:
2382
2383.. code-block:: lua
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002384
Pieter Baauw4d7f7662015-11-08 16:38:08 +01002385 core.register_service("hello-world", "http", function(applet)
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002386 local response = "Hello World !"
2387 applet:set_status(200)
Pieter Baauw2dcb9bc2015-10-01 22:47:12 +02002388 applet:add_header("content-length", string.len(response))
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002389 applet:add_header("content-type", "text/plain")
Pieter Baauw2dcb9bc2015-10-01 22:47:12 +02002390 applet:start_response()
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002391 applet:send(response)
2392 end)
2393
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002394.. js:attribute:: AppletHTTP.c
2395
2396 :returns: A :ref:`converters_class`
2397
2398 This attribute contains a Converters class object.
2399
2400.. js:attribute:: AppletHTTP.sc
2401
2402 :returns: A :ref:`converters_class`
2403
2404 This attribute contains a Converters class object. The
2405 functions of this object returns always a string.
2406
2407.. js:attribute:: AppletHTTP.f
2408
2409 :returns: A :ref:`fetches_class`
2410
2411 This attribute contains a Fetches class object. Note that the
2412 applet execution place cannot access to a valid HAProxy core HTTP
Ilya Shipitsin2075ca82020-03-06 23:22:22 +05002413 transaction, so some sample fetches related to the HTTP dependent
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002414 values (hdr, path, ...) are not available.
2415
2416.. js:attribute:: AppletHTTP.sf
2417
2418 :returns: A :ref:`fetches_class`
2419
2420 This attribute contains a Fetches class object. The functions of
2421 this object returns always a string. Note that the applet
2422 execution place cannot access to a valid HAProxy core HTTP
Ilya Shipitsin2075ca82020-03-06 23:22:22 +05002423 transaction, so some sample fetches related to the HTTP dependent
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002424 values (hdr, path, ...) are not available.
2425
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01002426.. js:attribute:: AppletHTTP.method
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002427
2428 :returns: string
2429
2430 The attribute method returns a string containing the HTTP
2431 method.
2432
2433.. js:attribute:: AppletHTTP.version
2434
2435 :returns: string
2436
2437 The attribute version, returns a string containing the HTTP
2438 request version.
2439
2440.. js:attribute:: AppletHTTP.path
2441
2442 :returns: string
2443
2444 The attribute path returns a string containing the HTTP
2445 request path.
2446
2447.. js:attribute:: AppletHTTP.qs
2448
2449 :returns: string
2450
2451 The attribute qs returns a string containing the HTTP
2452 request query string.
2453
2454.. js:attribute:: AppletHTTP.length
2455
2456 :returns: integer
2457
2458 The attribute length returns an integer containing the HTTP
2459 body length.
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002460
Thierry FOURNIER841475e2015-12-11 17:10:09 +01002461.. js:attribute:: AppletHTTP.headers
2462
Patrick Hemmerc6a1d712018-05-01 21:30:41 -04002463 :returns: table
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002464
Patrick Hemmerc6a1d712018-05-01 21:30:41 -04002465 The attribute headers returns a table containing the HTTP
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002466 headers. The header names are always in lower case. As the header name can be
2467 encountered more than once in each request, the value is indexed with 0 as
Patrick Hemmerc6a1d712018-05-01 21:30:41 -04002468 first index value. The table have this form:
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002469
2470.. code-block:: lua
2471
2472 AppletHTTP.headers['<header-name>'][<header-index>] = "<header-value>"
2473
2474 AppletHTTP.headers["host"][0] = "www.test.com"
2475 AppletHTTP.headers["accept"][0] = "audio/basic q=1"
2476 AppletHTTP.headers["accept"][1] = "audio/*, q=0.2"
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01002477 AppletHTTP.headers["accept"][2] = "*/*, q=0.1"
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002478..
2479
Robin H. Johnson52f5db22017-01-01 13:10:52 -08002480.. js:function:: AppletHTTP.set_status(applet, code [, reason])
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002481
2482 This function sets the HTTP status code for the response. The allowed code are
2483 from 100 to 599.
2484
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01002485 :param class_AppletHTTP applet: An :ref:`applethttp_class`
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002486 :param integer code: the status code returned to the client.
Robin H. Johnson52f5db22017-01-01 13:10:52 -08002487 :param string reason: the status reason returned to the client (optional).
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002488
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01002489.. js:function:: AppletHTTP.add_header(applet, name, value)
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002490
2491 This function add an header in the response. Duplicated headers are not
2492 collapsed. The special header *content-length* is used to determinate the
2493 response length. If it not exists, a *transfer-encoding: chunked* is set, and
Ilya Shipitsin2075ca82020-03-06 23:22:22 +05002494 all the write from the function *AppletHTTP:send()* become a chunk.
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002495
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01002496 :param class_AppletHTTP applet: An :ref:`applethttp_class`
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002497 :param string name: the header name
2498 :param string value: the header value
2499
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01002500.. js:function:: AppletHTTP.start_response(applet)
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002501
2502 This function indicates to the HTTP engine that it can process and send the
2503 response headers. After this called we cannot add headers to the response; We
2504 cannot use the *AppletHTTP:send()* function if the
2505 *AppletHTTP:start_response()* is not called.
2506
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01002507 :param class_AppletHTTP applet: An :ref:`applethttp_class`
2508
2509.. js:function:: AppletHTTP.getline(applet)
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002510
2511 This function returns a string containing one line from the http body. If the
2512 data returned doesn't contains a final '\\n' its assumed than its the last
2513 available data before the end of stream.
2514
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01002515 :param class_AppletHTTP applet: An :ref:`applethttp_class`
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002516 :returns: a string. The string can be empty if we reach the end of the stream.
2517
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01002518.. js:function:: AppletHTTP.receive(applet, [size])
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002519
2520 Reads data from the HTTP body, according to the specified read *size*. If the
2521 *size* is missing, the function tries to read all the content of the stream
2522 until the end. If the *size* is bigger than the http body, it returns the
Bertrand Jacquin874a35c2018-09-10 21:26:07 +01002523 amount of data available.
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002524
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01002525 :param class_AppletHTTP applet: An :ref:`applethttp_class`
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002526 :param integer size: the required read size.
Ilya Shipitsin11057a32020-06-21 21:18:27 +05002527 :returns: always return a string,the string can be empty is the connection is
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002528 closed.
2529
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01002530.. js:function:: AppletHTTP.send(applet, msg)
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002531
2532 Send the message *msg* on the http request body.
2533
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01002534 :param class_AppletHTTP applet: An :ref:`applethttp_class`
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002535 :param string msg: the message to send.
2536
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01002537.. js:function:: AppletHTTP.get_priv(applet)
2538
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01002539 Return Lua data stored in the current transaction. If no data are stored,
2540 it returns a nil value.
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01002541
2542 :param class_AppletHTTP applet: An :ref:`applethttp_class`
Bertrand Jacquin874a35c2018-09-10 21:26:07 +01002543 :returns: the opaque data previously stored, or nil if nothing is
2544 available.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01002545 :see: :js:func:`AppletHTTP.set_priv`
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01002546
2547.. js:function:: AppletHTTP.set_priv(applet, data)
2548
2549 Store any data in the current HAProxy transaction. This action replace the
2550 old stored data.
2551
2552 :param class_AppletHTTP applet: An :ref:`applethttp_class`
2553 :param opaque data: The data which is stored in the transaction.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01002554 :see: :js:func:`AppletHTTP.get_priv`
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01002555
Tim Duesterhus4e172c92020-05-19 13:49:42 +02002556.. js:function:: AppletHTTP.set_var(applet, var, value[, ifexist])
Thierry FOURNIER / OZON.IOc1edafe2016-12-12 16:25:30 +01002557
2558 Converts a Lua type in a HAProxy type and store it in a variable <var>.
2559
2560 :param class_AppletHTTP applet: An :ref:`applethttp_class`
2561 :param string var: The variable name according with the HAProxy variable syntax.
2562 :param type value: The value associated to the variable. The type ca be string or
2563 integer.
Tim Duesterhus4e172c92020-05-19 13:49:42 +02002564 :param boolean ifexist: If this parameter is set to a truthy value the variable
2565 will only be set if it was defined elsewhere (i.e. used
2566 within the configuration). It is highly recommended to
2567 always set this to true.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01002568 :see: :js:func:`AppletHTTP.unset_var`
2569 :see: :js:func:`AppletHTTP.get_var`
Thierry FOURNIER / OZON.IOc1edafe2016-12-12 16:25:30 +01002570
2571.. js:function:: AppletHTTP.unset_var(applet, var)
2572
2573 Unset the variable <var>.
2574
2575 :param class_AppletHTTP applet: An :ref:`applethttp_class`
2576 :param string var: The variable name according with the HAProxy variable syntax.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01002577 :see: :js:func:`AppletHTTP.set_var`
2578 :see: :js:func:`AppletHTTP.get_var`
Thierry FOURNIER / OZON.IOc1edafe2016-12-12 16:25:30 +01002579
2580.. js:function:: AppletHTTP.get_var(applet, var)
2581
2582 Returns data stored in the variable <var> converter in Lua type.
2583
2584 :param class_AppletHTTP applet: An :ref:`applethttp_class`
2585 :param string var: The variable name according with the HAProxy variable syntax.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01002586 :see: :js:func:`AppletHTTP.set_var`
2587 :see: :js:func:`AppletHTTP.unset_var`
Thierry FOURNIER / OZON.IOc1edafe2016-12-12 16:25:30 +01002588
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002589.. _applettcp_class:
2590
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002591AppletTCP class
2592===============
2593
2594.. js:class:: AppletTCP
2595
2596 This class is used with applets that requires the 'tcp' mode. The tcp applet
2597 can be registered with the *core.register_service()* function. They are used
2598 for processing a tcp stream like a server in back of HAProxy.
2599
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002600.. js:attribute:: AppletTCP.c
2601
2602 :returns: A :ref:`converters_class`
2603
2604 This attribute contains a Converters class object.
2605
2606.. js:attribute:: AppletTCP.sc
2607
2608 :returns: A :ref:`converters_class`
2609
2610 This attribute contains a Converters class object. The
2611 functions of this object returns always a string.
2612
2613.. js:attribute:: AppletTCP.f
2614
2615 :returns: A :ref:`fetches_class`
2616
2617 This attribute contains a Fetches class object.
2618
2619.. js:attribute:: AppletTCP.sf
2620
2621 :returns: A :ref:`fetches_class`
2622
2623 This attribute contains a Fetches class object.
2624
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01002625.. js:function:: AppletTCP.getline(applet)
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002626
2627 This function returns a string containing one line from the stream. If the
2628 data returned doesn't contains a final '\\n' its assumed than its the last
2629 available data before the end of stream.
2630
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01002631 :param class_AppletTCP applet: An :ref:`applettcp_class`
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002632 :returns: a string. The string can be empty if we reach the end of the stream.
2633
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01002634.. js:function:: AppletTCP.receive(applet, [size])
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002635
2636 Reads data from the TCP stream, according to the specified read *size*. If the
2637 *size* is missing, the function tries to read all the content of the stream
2638 until the end.
2639
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01002640 :param class_AppletTCP applet: An :ref:`applettcp_class`
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002641 :param integer size: the required read size.
Ilya Shipitsin11057a32020-06-21 21:18:27 +05002642 :returns: always return a string,the string can be empty is the connection is
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002643 closed.
2644
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01002645.. js:function:: AppletTCP.send(appletmsg)
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002646
2647 Send the message on the stream.
2648
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01002649 :param class_AppletTCP applet: An :ref:`applettcp_class`
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002650 :param string msg: the message to send.
2651
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01002652.. js:function:: AppletTCP.get_priv(applet)
2653
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01002654 Return Lua data stored in the current transaction. If no data are stored,
2655 it returns a nil value.
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01002656
2657 :param class_AppletTCP applet: An :ref:`applettcp_class`
Bertrand Jacquin874a35c2018-09-10 21:26:07 +01002658 :returns: the opaque data previously stored, or nil if nothing is
2659 available.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01002660 :see: :js:func:`AppletTCP.set_priv`
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01002661
2662.. js:function:: AppletTCP.set_priv(applet, data)
2663
2664 Store any data in the current HAProxy transaction. This action replace the
2665 old stored data.
2666
2667 :param class_AppletTCP applet: An :ref:`applettcp_class`
2668 :param opaque data: The data which is stored in the transaction.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01002669 :see: :js:func:`AppletTCP.get_priv`
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01002670
Tim Duesterhus4e172c92020-05-19 13:49:42 +02002671.. js:function:: AppletTCP.set_var(applet, var, value[, ifexist])
Thierry FOURNIER / OZON.IOc1edafe2016-12-12 16:25:30 +01002672
2673 Converts a Lua type in a HAProxy type and stores it in a variable <var>.
2674
2675 :param class_AppletTCP applet: An :ref:`applettcp_class`
2676 :param string var: The variable name according with the HAProxy variable syntax.
2677 :param type value: The value associated to the variable. The type can be string or
2678 integer.
Tim Duesterhus4e172c92020-05-19 13:49:42 +02002679 :param boolean ifexist: If this parameter is set to a truthy value the variable
2680 will only be set if it was defined elsewhere (i.e. used
2681 within the configuration). It is highly recommended to
2682 always set this to true.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01002683 :see: :js:func:`AppletTCP.unset_var`
2684 :see: :js:func:`AppletTCP.get_var`
Thierry FOURNIER / OZON.IOc1edafe2016-12-12 16:25:30 +01002685
2686.. js:function:: AppletTCP.unset_var(applet, var)
2687
2688 Unsets the variable <var>.
2689
2690 :param class_AppletTCP applet: An :ref:`applettcp_class`
2691 :param string var: The variable name according with the HAProxy variable syntax.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01002692 :see: :js:func:`AppletTCP.unset_var`
2693 :see: :js:func:`AppletTCP.set_var`
Thierry FOURNIER / OZON.IOc1edafe2016-12-12 16:25:30 +01002694
2695.. js:function:: AppletTCP.get_var(applet, var)
2696
2697 Returns data stored in the variable <var> converter in Lua type.
2698
2699 :param class_AppletTCP applet: An :ref:`applettcp_class`
2700 :param string var: The variable name according with the HAProxy variable syntax.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01002701 :see: :js:func:`AppletTCP.unset_var`
2702 :see: :js:func:`AppletTCP.set_var`
Thierry FOURNIER / OZON.IOc1edafe2016-12-12 16:25:30 +01002703
Aurelien DARRAGON0c951622023-11-23 13:47:54 +01002704.. _sticktable_class:
2705
Adis Nezirovic8878f8e2018-07-13 12:18:33 +02002706StickTable class
2707================
2708
2709.. js:class:: StickTable
2710
2711 **context**: task, action, sample-fetch
2712
2713 This class can be used to access the HAProxy stick tables from Lua.
2714
2715.. js:function:: StickTable.info()
2716
2717 Returns stick table attributes as a Lua table. See HAProxy documentation for
Ilya Shipitsin2272d8a2020-12-21 01:22:40 +05002718 "stick-table" for canonical info, or check out example below.
Adis Nezirovic8878f8e2018-07-13 12:18:33 +02002719
2720 :returns: Lua table
2721
2722 Assume our table has IPv4 key and gpc0 and conn_rate "columns":
2723
2724.. code-block:: lua
2725
2726 {
2727 expire=<int>, # Value in ms
2728 size=<int>, # Maximum table size
2729 used=<int>, # Actual number of entries in table
2730 data={ # Data columns, with types as key, and periods as values
2731 (-1 if type is not rate counter)
2732 conn_rate=<int>,
2733 gpc0=-1
2734 },
2735 length=<int>, # max string length for string table keys, key length
2736 # otherwise
2737 nopurge=<boolean>, # purge oldest entries when table is full
2738 type="ip" # can be "ip", "ipv6", "integer", "string", "binary"
2739 }
2740
2741.. js:function:: StickTable.lookup(key)
2742
2743 Returns stick table entry for given <key>
2744
2745 :param string key: Stick table key (IP addresses and strings are supported)
2746 :returns: Lua table
2747
2748.. js:function:: StickTable.dump([filter])
2749
2750 Returns all entries in stick table. An optional filter can be used
2751 to extract entries with specific data values. Filter is a table with valid
2752 comparison operators as keys followed by data type name and value pairs.
2753 Check out the HAProxy docs for "show table" for more details. For the
2754 reference, the supported operators are:
2755 "eq", "ne", "le", "lt", "ge", "gt"
2756
2757 For large tables, execution of this function can take a long time (for
2758 HAProxy standards). That's also true when filter is used, so take care and
2759 measure the impact.
2760
2761 :param table filter: Stick table filter
2762 :returns: Stick table entries (table)
2763
2764 See below for example filter, which contains 4 entries (or comparisons).
2765 (Maximum number of filter entries is 4, defined in the source code)
2766
2767.. code-block:: lua
2768
2769 local filter = {
2770 {"gpc0", "gt", 30}, {"gpc1", "gt", 20}}, {"conn_rate", "le", 10}
2771 }
2772
Christopher Faulet0f3c8902020-01-31 18:57:12 +01002773.. _action_class:
2774
2775Action class
2776=============
2777
2778.. js:class:: Act
2779
2780 **context**: action
2781
2782 This class contains all return codes an action may return. It is the lua
2783 equivalent to HAProxy "ACT_RET_*" code.
2784
2785.. code-block:: lua
2786
2787 core.register_action("deny", { "http-req" }, function (txn)
2788 return act.DENY
2789 end)
2790..
2791.. js:attribute:: act.CONTINUE
2792
2793 This attribute is an integer (0). It instructs HAProxy to continue the current
2794 ruleset processing on the message. It is the default return code for a lua
2795 action.
2796
2797 :returns: integer
2798
2799.. js:attribute:: act.STOP
2800
2801 This attribute is an integer (1). It instructs HAProxy to stop the current
2802 ruleset processing on the message.
2803
2804.. js:attribute:: act.YIELD
2805
2806 This attribute is an integer (2). It instructs HAProxy to temporarily pause
2807 the message processing. It will be resumed later on the same rule. The
2808 corresponding lua script is re-executed for the start.
2809
2810.. js:attribute:: act.ERROR
2811
2812 This attribute is an integer (3). It triggers an internal errors The message
2813 processing is stopped and the transaction is terminated. For HTTP streams, an
2814 HTTP 500 error is returned to the client.
2815
2816 :returns: integer
2817
2818.. js:attribute:: act.DONE
2819
2820 This attribute is an integer (4). It instructs HAProxy to stop the message
2821 processing.
2822
2823 :returns: integer
2824
2825.. js:attribute:: act.DENY
2826
2827 This attribute is an integer (5). It denies the current message. The message
2828 processing is stopped and the transaction is terminated. For HTTP streams, an
2829 HTTP 403 error is returned to the client if the deny is returned during the
2830 request analysis. During the response analysis, an HTTP 502 error is returned
2831 and the server response is discarded.
2832
2833 :returns: integer
2834
2835.. js:attribute:: act.ABORT
2836
2837 This attribute is an integer (6). It aborts the current message. The message
2838 processing is stopped and the transaction is terminated. For HTTP streams,
Willy Tarreau714f3452021-05-09 06:47:26 +02002839 HAProxy assumes a response was already sent to the client. From the Lua
Christopher Faulet0f3c8902020-01-31 18:57:12 +01002840 actions point of view, when this code is used, the transaction is terminated
2841 with no reply.
2842
2843 :returns: integer
2844
2845.. js:attribute:: act.INVALID
2846
2847 This attribute is an integer (7). It triggers an internal errors. The message
2848 processing is stopped and the transaction is terminated. For HTTP streams, an
2849 HTTP 400 error is returned to the client if the error is returned during the
2850 request analysis. During the response analysis, an HTTP 502 error is returned
2851 and the server response is discarded.
2852
2853 :returns: integer
Adis Nezirovic8878f8e2018-07-13 12:18:33 +02002854
Christopher Faulet2c2c2e32020-01-31 19:07:52 +01002855.. js:function:: act:wake_time(milliseconds)
2856
2857 **context**: action
2858
2859 Set the script pause timeout to the specified time, defined in
2860 milliseconds.
2861
2862 :param integer milliseconds: the required milliseconds.
2863
2864 This function may be used when a lua action returns `act.YIELD`, to force its
2865 wake-up at most after the specified number of milliseconds.
2866
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002867External Lua libraries
2868======================
2869
2870A lot of useful lua libraries can be found here:
2871
2872* `https://lua-toolbox.com/ <https://lua-toolbox.com/>`_
2873
Ilya Shipitsin2075ca82020-03-06 23:22:22 +05002874Redis client library:
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002875
2876* `https://github.com/nrk/redis-lua <https://github.com/nrk/redis-lua>`_
2877
2878This is an example about the usage of the Redis library with HAProxy. Note that
2879each call of any function of this library can throw an error if the socket
2880connection fails.
2881
2882.. code-block:: lua
2883
2884 -- load the redis library
2885 local redis = require("redis");
2886
2887 function do_something(txn)
2888
2889 -- create and connect new tcp socket
2890 local tcp = core.tcp();
2891 tcp:settimeout(1);
2892 tcp:connect("127.0.0.1", 6379);
2893
2894 -- use the redis library with this new socket
2895 local client = redis.connect({socket=tcp});
2896 client:ping();
2897
2898 end
2899
2900OpenSSL:
2901
2902* `http://mkottman.github.io/luacrypto/index.html
2903 <http://mkottman.github.io/luacrypto/index.html>`_
2904
2905* `https://github.com/brunoos/luasec/wiki
2906 <https://github.com/brunoos/luasec/wiki>`_