blob: fb7eb0978caf054909bacb1aa1336f1b3e0576d4 [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
58 **NOTE**: It is possible that this function cannot found the required data
59 in the original HAProxy sample-fetches, in this case, it cannot return the
60 result. This case is not yet supported
61
David Carlier61fdf8b2015-10-02 11:59:38 +0100626. The **converter context**. It is a Lua function that takes a string as input
Thierry FOURNIER17bd1522015-03-11 20:31:00 +010063 and returns another string as output. These types of function are stateless,
64 it cannot access to any context. They don't execute any blocking function.
65 The call prototype is `function string fcn(string)`. This function can be
66 registered with the Lua function `core.register_converters()`. Each declared
67 converter is prefixed by the string "lua.".
68
69HAProxy Lua Hello world
70-----------------------
71
72HAProxy configuration file (`hello_world.conf`):
73
74::
75
76 global
77 lua-load hello_world.lua
78
79 listen proxy
80 bind 127.0.0.1:10001
Thierry FOURNIERa2d02252015-10-01 15:00:42 +020081 tcp-request inspect-delay 1s
82 tcp-request content use-service lua.hello_world
Thierry FOURNIER17bd1522015-03-11 20:31:00 +010083
84HAProxy Lua file (`hello_world.lua`):
85
86.. code-block:: lua
87
Thierry FOURNIERa2d02252015-10-01 15:00:42 +020088 core.register_service("hello_world", "tcp", function(applet)
89 applet:send("hello world\n")
90 end)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +010091
92How to start HAProxy for testing this configuration:
93
94::
95
96 ./haproxy -f hello_world.conf
97
98On other terminal, you can test with telnet:
99
100::
101
102 #:~ telnet 127.0.0.1 10001
103 hello world
104
105Core class
106==========
107
108.. js:class:: core
109
110 The "core" class contains all the HAProxy core functions. These function are
111 useful for the controlling the execution flow, registering hooks, manipulating
112 global maps or ACL, ...
113
114 "core" class is basically provided with HAProxy. No `require` line is
115 required to uses these function.
116
David Carlier61fdf8b2015-10-02 11:59:38 +0100117 The "core" class is static, it is not possible to create a new object of this
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100118 type.
119
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100120.. js:attribute:: core.emerg
121
Thierry FOURNIERdc595002015-12-21 11:13:52 +0100122 :returns: integer
123
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100124 This attribute is an integer, it contains the value of the loglevel "emergency" (0).
125
126.. js:attribute:: core.alert
127
Thierry FOURNIERdc595002015-12-21 11:13:52 +0100128 :returns: integer
129
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100130 This attribute is an integer, it contains the value of the loglevel "alert" (1).
131
132.. js:attribute:: core.crit
133
Thierry FOURNIERdc595002015-12-21 11:13:52 +0100134 :returns: integer
135
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100136 This attribute is an integer, it contains the value of the loglevel "critical" (2).
137
138.. js:attribute:: core.err
139
Thierry FOURNIERdc595002015-12-21 11:13:52 +0100140 :returns: integer
141
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100142 This attribute is an integer, it contains the value of the loglevel "error" (3).
143
144.. js:attribute:: core.warning
145
Thierry FOURNIERdc595002015-12-21 11:13:52 +0100146 :returns: integer
147
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100148 This attribute is an integer, it contains the value of the loglevel "warning" (4).
149
150.. js:attribute:: core.notice
151
Thierry FOURNIERdc595002015-12-21 11:13:52 +0100152 :returns: integer
153
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100154 This attribute is an integer, it contains the value of the loglevel "notice" (5).
155
156.. js:attribute:: core.info
157
Thierry FOURNIERdc595002015-12-21 11:13:52 +0100158 :returns: integer
159
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100160 This attribute is an integer, it contains the value of the loglevel "info" (6).
161
162.. js:attribute:: core.debug
163
Thierry FOURNIERdc595002015-12-21 11:13:52 +0100164 :returns: integer
165
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100166 This attribute is an integer, it contains the value of the loglevel "debug" (7).
167
Thierry FOURNIER12a865d2016-12-14 19:40:37 +0100168.. js:attribute:: core.proxies
169
170 **context**: task, action, sample-fetch, converter
171
Patrick Hemmerc6a1d712018-05-01 21:30:41 -0400172 This attribute is a table of declared proxies (frontend and backends). Each
173 proxy give an access to his list of listeners and servers. The table is
174 indexed by proxy name, and each entry is of type :ref:`proxy_class`.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +0100175
Thierry FOURNIER9b82a582017-07-24 13:30:43 +0200176 Warning, if you are declared frontend and backend with the same name, only one
177 of these are listed.
178
179 :see: :js:attr:`core.backends`
180 :see: :js:attr:`core.frontends`
181
182.. js:attribute:: core.backends
183
184 **context**: task, action, sample-fetch, converter
185
Patrick Hemmerc6a1d712018-05-01 21:30:41 -0400186 This attribute is a table of declared proxies with backend capability. Each
187 proxy give an access to his list of listeners and servers. The table is
188 indexed by the backend name, and each entry is of type :ref:`proxy_class`.
Thierry FOURNIER9b82a582017-07-24 13:30:43 +0200189
190 :see: :js:attr:`core.proxies`
191 :see: :js:attr:`core.frontends`
192
193.. js:attribute:: core.frontends
194
195 **context**: task, action, sample-fetch, converter
196
Patrick Hemmerc6a1d712018-05-01 21:30:41 -0400197 This attribute is a table of declared proxies with frontend capability. Each
198 proxy give an access to his list of listeners and servers. The table is
199 indexed by the frontend name, and each entry is of type :ref:`proxy_class`.
Thierry FOURNIER9b82a582017-07-24 13:30:43 +0200200
201 :see: :js:attr:`core.proxies`
202 :see: :js:attr:`core.backends`
203
Thierry Fournierecb83c22020-11-28 15:49:44 +0100204.. js:attribute:: core.thread
205
206 **context**: task, action, sample-fetch, converter, applet
207
208 This variable contains the executing thread number starting at 1. 0 is a
209 special case for the common lua context. So, if thread is 0, Lua scope is
210 shared by all threads, otherwise the scope is dedicated to a single thread.
211 A program which needs to execute some parts exactly once regardless of the
212 number of threads can check that core.thread is 0 or 1.
213
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100214.. js:function:: core.log(loglevel, msg)
215
216 **context**: body, init, task, action, sample-fetch, converter
217
David Carlier61fdf8b2015-10-02 11:59:38 +0100218 This function sends a log. The log is sent, according with the HAProxy
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100219 configuration file, on the default syslog server if it is configured and on
220 the stderr if it is allowed.
221
Bertrand Jacquin874a35c2018-09-10 21:26:07 +0100222 :param integer loglevel: Is the log level associated with the message. It is a
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100223 number between 0 and 7.
224 :param string msg: The log content.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +0100225 :see: :js:attr:`core.emerg`, :js:attr:`core.alert`, :js:attr:`core.crit`,
226 :js:attr:`core.err`, :js:attr:`core.warning`, :js:attr:`core.notice`,
227 :js:attr:`core.info`, :js:attr:`core.debug` (log level definitions)
228 :see: :js:func:`core.Debug`
229 :see: :js:func:`core.Info`
230 :see: :js:func:`core.Warning`
231 :see: :js:func:`core.Alert`
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100232
233.. js:function:: core.Debug(msg)
234
235 **context**: body, init, task, action, sample-fetch, converter
236
237 :param string msg: The log content.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +0100238 :see: :js:func:`core.log`
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100239
240 Does the same job than:
241
242.. code-block:: lua
243
Thierry FOURNIERdc595002015-12-21 11:13:52 +0100244 function Debug(msg)
245 core.log(core.debug, msg)
246 end
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100247..
248
249.. js:function:: core.Info(msg)
250
251 **context**: body, init, task, action, sample-fetch, converter
252
253 :param string msg: The log content.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +0100254 :see: :js:func:`core.log`
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100255
256.. code-block:: lua
257
Thierry FOURNIERdc595002015-12-21 11:13:52 +0100258 function Info(msg)
259 core.log(core.info, msg)
260 end
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100261..
262
263.. js:function:: core.Warning(msg)
264
265 **context**: body, init, task, action, sample-fetch, converter
266
267 :param string msg: The log content.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +0100268 :see: :js:func:`core.log`
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100269
270.. code-block:: lua
271
Thierry FOURNIERdc595002015-12-21 11:13:52 +0100272 function Warning(msg)
273 core.log(core.warning, msg)
274 end
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100275..
276
277.. js:function:: core.Alert(msg)
278
279 **context**: body, init, task, action, sample-fetch, converter
280
281 :param string msg: The log content.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +0100282 :see: :js:func:`core.log`
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100283
284.. code-block:: lua
285
Thierry FOURNIERdc595002015-12-21 11:13:52 +0100286 function Alert(msg)
287 core.log(core.alert, msg)
288 end
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100289..
290
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100291.. js:function:: core.add_acl(filename, key)
292
293 **context**: init, task, action, sample-fetch, converter
294
295 Add the ACL *key* in the ACLs list referenced by the file *filename*.
296
297 :param string filename: the filename that reference the ACL entries.
298 :param string key: the key which will be added.
299
300.. js:function:: core.del_acl(filename, key)
301
302 **context**: init, task, action, sample-fetch, converter
303
304 Delete the ACL entry referenced by the key *key* in the list of ACLs
305 referenced by *filename*.
306
307 :param string filename: the filename that reference the ACL entries.
308 :param string key: the key which will be deleted.
309
310.. js:function:: core.del_map(filename, key)
311
312 **context**: init, task, action, sample-fetch, converter
313
314 Delete the map entry indexed with the specified key in the list of maps
315 referenced by his filename.
316
317 :param string filename: the filename that reference the map entries.
318 :param string key: the key which will be deleted.
319
Thierry Fourniereea77c02016-03-18 08:47:13 +0100320.. js:function:: core.get_info()
321
322 **context**: body, init, task, action, sample-fetch, converter
323
Ilya Shipitsin2075ca82020-03-06 23:22:22 +0500324 Returns HAProxy core information. We can found information like the uptime,
Thierry Fourniereea77c02016-03-18 08:47:13 +0100325 the pid, memory pool usage, tasks number, ...
326
Bertrand Jacquin874a35c2018-09-10 21:26:07 +0100327 These information are also returned by the management socket via the command
328 "show info". See the management socket documentation for more information
Thierry Fourniereea77c02016-03-18 08:47:13 +0100329 about the content of these variables.
330
331 :returns: an array of values.
332
Thierry Fournierb1f46562016-01-21 09:46:15 +0100333.. js:function:: core.now()
334
335 **context**: body, init, task, action
336
337 This function returns the current time. The time returned is fixed by the
Bertrand Jacquin874a35c2018-09-10 21:26:07 +0100338 HAProxy core and assures than the hour will be monotonic and that the system
Thierry Fournierb1f46562016-01-21 09:46:15 +0100339 call 'gettimeofday' will not be called too. The time is refreshed between each
340 Lua execution or resume, so two consecutive call to the function "now" will
341 probably returns the same result.
342
Patrick Hemmerc6a1d712018-05-01 21:30:41 -0400343 :returns: a table which contains two entries "sec" and "usec". "sec"
Thierry Fournierb1f46562016-01-21 09:46:15 +0100344 contains the current at the epoch format, and "usec" contains the
345 current microseconds.
346
Thierry FOURNIERa78f0372016-12-14 19:04:41 +0100347.. js:function:: core.http_date(date)
348
349 **context**: body, init, task, action
350
Bertrand Jacquin874a35c2018-09-10 21:26:07 +0100351 This function take a string representing http date, and returns an integer
Thierry FOURNIERa78f0372016-12-14 19:04:41 +0100352 containing the corresponding date with a epoch format. A valid http date
353 me respect the format IMF, RFC850 or ASCTIME.
354
355 :param string date: a date http-date formatted
356 :returns: integer containing epoch date
357 :see: :js:func:`core.imf_date`.
358 :see: :js:func:`core.rfc850_date`.
359 :see: :js:func:`core.asctime_date`.
360 :see: https://tools.ietf.org/html/rfc7231#section-7.1.1.1
361
362.. js:function:: core.imf_date(date)
363
364 **context**: body, init, task, action
365
Bertrand Jacquin874a35c2018-09-10 21:26:07 +0100366 This function take a string representing IMF date, and returns an integer
Thierry FOURNIERa78f0372016-12-14 19:04:41 +0100367 containing the corresponding date with a epoch format.
368
369 :param string date: a date IMF formatted
370 :returns: integer containing epoch date
371 :see: https://tools.ietf.org/html/rfc7231#section-7.1.1.1
372
373 The IMF format is like this:
374
375.. code-block:: text
376
377 Sun, 06 Nov 1994 08:49:37 GMT
378..
379
380.. js:function:: core.rfc850_date(date)
381
382 **context**: body, init, task, action
383
Bertrand Jacquin874a35c2018-09-10 21:26:07 +0100384 This function take a string representing RFC850 date, and returns an integer
Thierry FOURNIERa78f0372016-12-14 19:04:41 +0100385 containing the corresponding date with a epoch format.
386
387 :param string date: a date RFC859 formatted
388 :returns: integer containing epoch date
389 :see: https://tools.ietf.org/html/rfc7231#section-7.1.1.1
390
391 The RFC850 format is like this:
392
393.. code-block:: text
394
395 Sunday, 06-Nov-94 08:49:37 GMT
396..
397
398.. js:function:: core.asctime_date(date)
399
400 **context**: body, init, task, action
401
Bertrand Jacquin874a35c2018-09-10 21:26:07 +0100402 This function take a string representing ASCTIME date, and returns an integer
Thierry FOURNIERa78f0372016-12-14 19:04:41 +0100403 containing the corresponding date with a epoch format.
404
405 :param string date: a date ASCTIME formatted
406 :returns: integer containing epoch date
407 :see: https://tools.ietf.org/html/rfc7231#section-7.1.1.1
408
409 The ASCTIME format is like this:
410
411.. code-block:: text
412
413 Sun Nov 6 08:49:37 1994
414..
415
416.. js:function:: core.rfc850_date(date)
417
418 **context**: body, init, task, action
419
Bertrand Jacquin874a35c2018-09-10 21:26:07 +0100420 This function take a string representing http date, and returns an integer
Thierry FOURNIERa78f0372016-12-14 19:04:41 +0100421 containing the corresponding date with a epoch format.
422
423 :param string date: a date http-date formatted
424
425.. js:function:: core.asctime_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
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100434.. js:function:: core.msleep(milliseconds)
435
436 **context**: body, init, task, action
437
438 The `core.msleep()` stops the Lua execution between specified milliseconds.
439
440 :param integer milliseconds: the required milliseconds.
441
Thierry Fournierf61aa632016-02-19 20:56:00 +0100442.. js:attribute:: core.proxies
443
444 **context**: body, init, task, action, sample-fetch, converter
445
Bertrand Jacquin874a35c2018-09-10 21:26:07 +0100446 Proxies is a table containing the list of all proxies declared in the
Patrick Hemmerc6a1d712018-05-01 21:30:41 -0400447 configuration file. The table is indexed by the proxy name, and each entry
448 of the proxies table is an object of type :ref:`proxy_class`.
449
450 Warning, if you have declared a frontend and backend with the same name, only
451 one of these are listed.
Thierry Fournierf61aa632016-02-19 20:56:00 +0100452
Thierry FOURNIERc5d11c62018-02-12 14:46:54 +0100453.. js:function:: core.register_action(name, actions, func [, nb_args])
Thierry FOURNIER8255a752015-09-23 21:03:35 +0200454
455 **context**: body
456
David Carlier61fdf8b2015-10-02 11:59:38 +0100457 Register a Lua function executed as action. All the registered action can be
Thierry FOURNIER8255a752015-09-23 21:03:35 +0200458 used in HAProxy with the prefix "lua.". An action gets a TXN object class as
459 input.
460
461 :param string name: is the name of the converter.
Willy Tarreau61add3c2015-09-28 15:39:10 +0200462 :param table actions: is a table of string describing the HAProxy actions who
Thierry FOURNIER8255a752015-09-23 21:03:35 +0200463 want to register to. The expected actions are 'tcp-req',
464 'tcp-res', 'http-req' or 'http-res'.
Thierry FOURNIERc5d11c62018-02-12 14:46:54 +0100465 :param integer nb_args: is the expected number of argument for the action.
466 By default the value is 0.
Thierry FOURNIER8255a752015-09-23 21:03:35 +0200467 :param function func: is the Lua function called to work as converter.
468
469 The prototype of the Lua function used as argument is:
470
471.. code-block:: lua
472
Thierry FOURNIERc5d11c62018-02-12 14:46:54 +0100473 function(txn [, arg1 [, arg2]])
Thierry FOURNIER8255a752015-09-23 21:03:35 +0200474..
475
Thierry FOURNIERdc595002015-12-21 11:13:52 +0100476 * **txn** (:ref:`txn_class`): this is a TXN object used for manipulating the
Thierry FOURNIER8255a752015-09-23 21:03:35 +0200477 current request or TCP stream.
478
Bertrand Jacquin874a35c2018-09-10 21:26:07 +0100479 * **argX**: this is argument provided through the HAProxy configuration file.
Thierry FOURNIERc5d11c62018-02-12 14:46:54 +0100480
Bertrand Jacquin874a35c2018-09-10 21:26:07 +0100481 Here, an example of action registration. The action just send an 'Hello world'
Thierry FOURNIER8255a752015-09-23 21:03:35 +0200482 in the logs.
483
484.. code-block:: lua
485
486 core.register_action("hello-world", { "tcp-req", "http-req" }, function(txn)
487 txn:Info("Hello world")
488 end)
489..
490
491 This example code is used in HAproxy configuration like this:
492
493::
494
495 frontend tcp_frt
496 mode tcp
497 tcp-request content lua.hello-world
498
499 frontend http_frt
500 mode http
501 http-request lua.hello-world
Thierry FOURNIERc5d11c62018-02-12 14:46:54 +0100502..
503
Bertrand Jacquin874a35c2018-09-10 21:26:07 +0100504 A second example using arguments
Thierry FOURNIERc5d11c62018-02-12 14:46:54 +0100505
506.. code-block:: lua
507
508 function hello_world(txn, arg)
509 txn:Info("Hello world for " .. arg)
510 end
511 core.register_action("hello-world", { "tcp-req", "http-req" }, hello_world, 2)
512..
Thierry FOURNIER8255a752015-09-23 21:03:35 +0200513
Thierry FOURNIERc5d11c62018-02-12 14:46:54 +0100514 This example code is used in HAproxy configuration like this:
515
516::
517
518 frontend tcp_frt
519 mode tcp
520 tcp-request content lua.hello-world everybody
521..
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100522.. js:function:: core.register_converters(name, func)
523
524 **context**: body
525
David Carlier61fdf8b2015-10-02 11:59:38 +0100526 Register a Lua function executed as converter. All the registered converters
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100527 can be used in HAProxy with the prefix "lua.". An converter get a string as
528 input and return a string as output. The registered function can take up to 9
529 values as parameter. All the value are strings.
530
531 :param string name: is the name of the converter.
532 :param function func: is the Lua function called to work as converter.
533
534 The prototype of the Lua function used as argument is:
535
536.. code-block:: lua
537
538 function(str, [p1 [, p2 [, ... [, p5]]]])
539..
540
541 * **str** (*string*): this is the input value automatically converted in
542 string.
543 * **p1** .. **p5** (*string*): this is a list of string arguments declared in
Bertrand Jacquin874a35c2018-09-10 21:26:07 +0100544 the HAProxy configuration file. The number of arguments doesn't exceed 5.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100545 The order and the nature of these is conventionally choose by the
Bertrand Jacquin874a35c2018-09-10 21:26:07 +0100546 developer.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100547
548.. js:function:: core.register_fetches(name, func)
549
550 **context**: body
551
David Carlier61fdf8b2015-10-02 11:59:38 +0100552 Register a Lua function executed as sample fetch. All the registered sample
Bertrand Jacquin874a35c2018-09-10 21:26:07 +0100553 fetch can be used in HAProxy with the prefix "lua.". A Lua sample fetch
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100554 return a string as output. The registered function can take up to 9 values as
555 parameter. All the value are strings.
556
557 :param string name: is the name of the converter.
558 :param function func: is the Lua function called to work as sample fetch.
559
560 The prototype of the Lua function used as argument is:
561
562.. code-block:: lua
563
564 string function(txn, [p1 [, p2 [, ... [, p5]]]])
565..
566
Thierry FOURNIERdc595002015-12-21 11:13:52 +0100567 * **txn** (:ref:`txn_class`): this is the txn object associated with the current
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100568 request.
569 * **p1** .. **p5** (*string*): this is a list of string arguments declared in
Bertrand Jacquin874a35c2018-09-10 21:26:07 +0100570 the HAProxy configuration file. The number of arguments doesn't exceed 5.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100571 The order and the nature of these is conventionally choose by the
Bertrand Jacquin874a35c2018-09-10 21:26:07 +0100572 developer.
573 * **Returns**: A string containing some data, or nil if the value cannot be
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100574 returned now.
575
576 lua example code:
577
578.. code-block:: lua
579
580 core.register_fetches("hello", function(txn)
581 return "hello"
582 end)
583..
584
585 HAProxy example configuration:
586
587::
588
589 frontend example
590 http-request redirect location /%[lua.hello]
591
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +0200592.. js:function:: core.register_service(name, mode, func)
593
594 **context**: body
595
David Carlier61fdf8b2015-10-02 11:59:38 +0100596 Register a Lua function executed as a service. All the registered service can
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +0200597 be used in HAProxy with the prefix "lua.". A service gets an object class as
598 input according with the required mode.
599
600 :param string name: is the name of the converter.
Willy Tarreau61add3c2015-09-28 15:39:10 +0200601 :param string mode: is string describing the required mode. Only 'tcp' or
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +0200602 'http' are allowed.
603 :param function func: is the Lua function called to work as converter.
604
605 The prototype of the Lua function used as argument is:
606
607.. code-block:: lua
608
609 function(applet)
610..
611
Thierry FOURNIERdc595002015-12-21 11:13:52 +0100612 * **applet** *applet* will be a :ref:`applettcp_class` or a
613 :ref:`applethttp_class`. It depends the type of registered applet. An applet
614 registered with the 'http' value for the *mode* parameter will gets a
615 :ref:`applethttp_class`. If the *mode* value is 'tcp', the applet will gets
616 a :ref:`applettcp_class`.
617
618 **warning**: Applets of type 'http' cannot be called from 'tcp-*'
619 rulesets. Only the 'http-*' rulesets are authorized, this means
620 that is not possible to call an HTTP applet from a proxy in tcp
Bertrand Jacquin874a35c2018-09-10 21:26:07 +0100621 mode. Applets of type 'tcp' can be called from anywhere.
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +0200622
Bertrand Jacquin874a35c2018-09-10 21:26:07 +0100623 Here, an example of service registration. The service just send an 'Hello world'
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +0200624 as an http response.
625
626.. code-block:: lua
627
Pieter Baauw4d7f7662015-11-08 16:38:08 +0100628 core.register_service("hello-world", "http", function(applet)
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +0200629 local response = "Hello World !"
630 applet:set_status(200)
Pieter Baauw2dcb9bc2015-10-01 22:47:12 +0200631 applet:add_header("content-length", string.len(response))
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +0200632 applet:add_header("content-type", "text/plain")
Pieter Baauw2dcb9bc2015-10-01 22:47:12 +0200633 applet:start_response()
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +0200634 applet:send(response)
635 end)
636..
637
638 This example code is used in HAproxy configuration like this:
639
640::
641
642 frontend example
643 http-request use-service lua.hello-world
644
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100645.. js:function:: core.register_init(func)
646
647 **context**: body
648
649 Register a function executed after the configuration parsing. This is useful
650 to check any parameters.
651
Pieter Baauw4d7f7662015-11-08 16:38:08 +0100652 :param function func: is the Lua function called to work as initializer.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100653
654 The prototype of the Lua function used as argument is:
655
656.. code-block:: lua
657
658 function()
659..
660
661 It takes no input, and no output is expected.
662
663.. js:function:: core.register_task(func)
664
665 **context**: body, init, task, action, sample-fetch, converter
666
667 Register and start independent task. The task is started when the HAProxy
668 main scheduler starts. For example this type of tasks can be executed to
Thierry FOURNIER2e4893c2015-03-18 13:37:27 +0100669 perform complex health checks.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100670
Pieter Baauw4d7f7662015-11-08 16:38:08 +0100671 :param function func: is the Lua function called to work as initializer.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100672
673 The prototype of the Lua function used as argument is:
674
675.. code-block:: lua
676
677 function()
678..
679
680 It takes no input, and no output is expected.
681
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +0100682.. js:function:: core.register_cli([path], usage, func)
683
684 **context**: body
685
686 Register and start independent task. The task is started when the HAProxy
687 main scheduler starts. For example this type of tasks can be executed to
688 perform complex health checks.
689
690 :param array path: is the sequence of word for which the cli execute the Lua
691 binding.
692 :param string usage: is the usage message displayed in the help.
693 :param function func: is the Lua function called to handle the CLI commands.
694
695 The prototype of the Lua function used as argument is:
696
697.. code-block:: lua
698
699 function(AppletTCP, [arg1, [arg2, [...]]])
700..
701
702 I/O are managed with the :ref:`applettcp_class` object. Args are given as
Bertrand Jacquin874a35c2018-09-10 21:26:07 +0100703 parameter. The args embed the registered path. If the path is declared like
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +0100704 this:
705
706.. code-block:: lua
707
708 core.register_cli({"show", "ssl", "stats"}, "Display SSL stats..", function(applet, arg1, arg2, arg3, arg4, arg5)
709 end)
710..
711
712 And we execute this in the prompt:
713
714.. code-block:: text
715
716 > prompt
717 > show ssl stats all
718..
719
Bertrand Jacquin874a35c2018-09-10 21:26:07 +0100720 Then, arg1, arg2 and arg3 will contains respectively "show", "ssl" and "stats".
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +0100721 arg4 will contain "all". arg5 contains nil.
722
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100723.. js:function:: core.set_nice(nice)
724
725 **context**: task, action, sample-fetch, converter
726
727 Change the nice of the current task or current session.
Thierry FOURNIER2e4893c2015-03-18 13:37:27 +0100728
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100729 :param integer nice: the nice value, it must be between -1024 and 1024.
730
731.. js:function:: core.set_map(filename, key, value)
732
733 **context**: init, task, action, sample-fetch, converter
734
Bertrand Jacquin874a35c2018-09-10 21:26:07 +0100735 Set the value *value* associated to the key *key* in the map referenced by
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100736 *filename*.
737
Thierry FOURNIERdc595002015-12-21 11:13:52 +0100738 :param string filename: the Map reference
739 :param string key: the key to set or replace
740 :param string value: the associated value
741
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100742.. js:function:: core.sleep(int seconds)
743
744 **context**: body, init, task, action
745
746 The `core.sleep()` functions stop the Lua execution between specified seconds.
747
748 :param integer seconds: the required seconds.
749
750.. js:function:: core.tcp()
751
752 **context**: init, task, action
753
754 This function returns a new object of a *socket* class.
755
Thierry FOURNIERdc595002015-12-21 11:13:52 +0100756 :returns: A :ref:`socket_class` object.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100757
Thierry Fournier1de16592016-01-27 09:49:07 +0100758.. js:function:: core.concat()
759
760 **context**: body, init, task, action, sample-fetch, converter
761
Bertrand Jacquin874a35c2018-09-10 21:26:07 +0100762 This function returns a new concat object.
Thierry Fournier1de16592016-01-27 09:49:07 +0100763
764 :returns: A :ref:`concat_class` object.
765
Thierry FOURNIER0a99b892015-08-26 00:14:17 +0200766.. js:function:: core.done(data)
767
768 **context**: body, init, task, action, sample-fetch, converter
769
770 :param any data: Return some data for the caller. It is useful with
771 sample-fetches and sample-converters.
772
773 Immediately stops the current Lua execution and returns to the caller which
774 may be a sample fetch, a converter or an action and returns the specified
Thierry Fournier4234dbd2020-11-28 13:18:23 +0100775 value (ignored for actions and init). It is used when the LUA process finishes
776 its work and wants to give back the control to HAProxy without executing the
Thierry FOURNIER0a99b892015-08-26 00:14:17 +0200777 remaining code. It can be seen as a multi-level "return".
778
Thierry FOURNIER486f5a02015-03-16 15:13:03 +0100779.. js:function:: core.yield()
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100780
781 **context**: task, action, sample-fetch, converter
782
783 Give back the hand at the HAProxy scheduler. It is used when the LUA
784 processing consumes a lot of processing time.
785
Thierry FOURNIER / OZON.IO62fec752016-11-10 20:38:11 +0100786.. js:function:: core.parse_addr(address)
787
788 **context**: body, init, task, action, sample-fetch, converter
789
790 :param network: is a string describing an ipv4 or ipv6 address and optionally
791 its network length, like this: "127.0.0.1/8" or "aaaa::1234/32".
792 :returns: a userdata containing network or nil if an error occurs.
793
Bertrand Jacquin874a35c2018-09-10 21:26:07 +0100794 Parse ipv4 or ipv6 addresses and its facultative associated network.
Thierry FOURNIER / OZON.IO62fec752016-11-10 20:38:11 +0100795
796.. js:function:: core.match_addr(addr1, addr2)
797
798 **context**: body, init, task, action, sample-fetch, converter
799
800 :param addr1: is an address created with "core.parse_addr".
801 :param addr2: is an address created with "core.parse_addr".
Bertrand Jacquin874a35c2018-09-10 21:26:07 +0100802 :returns: boolean, true if the network of the addresses match, else returns
Thierry FOURNIER / OZON.IO62fec752016-11-10 20:38:11 +0100803 false.
804
Ilya Shipitsin2075ca82020-03-06 23:22:22 +0500805 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 +0100806 of network is not important.
807
Thierry FOURNIER / OZON.IO8a1027a2016-11-24 20:48:38 +0100808.. js:function:: core.tokenize(str, separators [, noblank])
809
810 **context**: body, init, task, action, sample-fetch, converter
811
812 This function is useful for tokenizing an entry, or splitting some messages.
813 :param string str: The string which will be split.
814 :param string separators: A string containing a list of separators.
815 :param boolean noblank: Ignore empty entries.
816 :returns: an array of string.
817
818 For example:
819
820.. code-block:: lua
821
822 local array = core.tokenize("This function is useful, for tokenizing an entry.", "., ", true)
823 print_r(array)
824..
825
826 Returns this array:
827
828.. code-block:: text
829
830 (table) table: 0x21c01e0 [
831 1: (string) "This"
832 2: (string) "function"
833 3: (string) "is"
834 4: (string) "useful"
835 5: (string) "for"
836 6: (string) "tokenizing"
837 7: (string) "an"
838 8: (string) "entry"
839 ]
840..
841
Thierry Fournierf61aa632016-02-19 20:56:00 +0100842.. _proxy_class:
843
844Proxy class
845============
846
847.. js:class:: Proxy
848
849 This class provides a way for manipulating proxy and retrieving information
850 like statistics.
851
Thierry FOURNIER817e7592017-07-24 14:35:04 +0200852.. js:attribute:: Proxy.name
853
854 Contain the name of the proxy.
855
Baptiste Assmann46c72552017-10-26 21:51:58 +0200856.. js:attribute:: Proxy.uuid
857
858 Contain the unique identifier of the proxy.
859
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +0100860.. js:attribute:: Proxy.servers
861
Patrick Hemmerc6a1d712018-05-01 21:30:41 -0400862 Contain a table with the attached servers. The table is indexed by server
863 name, and each server entry is an object of type :ref:`server_class`.
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +0100864
Adis Nezirovic8878f8e2018-07-13 12:18:33 +0200865.. js:attribute:: Proxy.stktable
866
867 Contains a stick table object attached to the proxy.
868
Thierry Fournierff480422016-02-25 08:36:46 +0100869.. js:attribute:: Proxy.listeners
870
Patrick Hemmerc6a1d712018-05-01 21:30:41 -0400871 Contain a table with the attached listeners. The table is indexed by listener
872 name, and each each listeners entry is an object of type
873 :ref:`listener_class`.
Thierry Fournierff480422016-02-25 08:36:46 +0100874
Thierry Fournierf61aa632016-02-19 20:56:00 +0100875.. js:function:: Proxy.pause(px)
876
877 Pause the proxy. See the management socket documentation for more information.
878
879 :param class_proxy px: A :ref:`proxy_class` which indicates the manipulated
880 proxy.
881
882.. js:function:: Proxy.resume(px)
883
884 Resume the proxy. See the management socket documentation for more
885 information.
886
887 :param class_proxy px: A :ref:`proxy_class` which indicates the manipulated
888 proxy.
889
890.. js:function:: Proxy.stop(px)
891
892 Stop the proxy. See the management socket documentation for more information.
893
894 :param class_proxy px: A :ref:`proxy_class` which indicates the manipulated
895 proxy.
896
897.. js:function:: Proxy.shut_bcksess(px)
898
899 Kill the session attached to a backup server. See the management socket
900 documentation for more information.
901
902 :param class_proxy px: A :ref:`proxy_class` which indicates the manipulated
903 proxy.
904
905.. js:function:: Proxy.get_cap(px)
906
907 Returns a string describing the capabilities of the proxy.
908
909 :param class_proxy px: A :ref:`proxy_class` which indicates the manipulated
910 proxy.
911 :returns: a string "frontend", "backend", "proxy" or "ruleset".
912
913.. js:function:: Proxy.get_mode(px)
914
915 Returns a string describing the mode of the current proxy.
916
917 :param class_proxy px: A :ref:`proxy_class` which indicates the manipulated
918 proxy.
919 :returns: a string "tcp", "http", "health" or "unknown"
920
921.. js:function:: Proxy.get_stats(px)
922
Bertrand Jacquin874a35c2018-09-10 21:26:07 +0100923 Returns a table containing the proxy statistics. The statistics returned are
Thierry Fournierf61aa632016-02-19 20:56:00 +0100924 not the same if the proxy is frontend or a backend.
925
926 :param class_proxy px: A :ref:`proxy_class` which indicates the manipulated
927 proxy.
Patrick Hemmerc6a1d712018-05-01 21:30:41 -0400928 :returns: a key/value table containing stats
Thierry Fournierf61aa632016-02-19 20:56:00 +0100929
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +0100930.. _server_class:
931
932Server class
933============
934
Patrick Hemmerc6a1d712018-05-01 21:30:41 -0400935.. js:class:: Server
936
937 This class provides a way for manipulating servers and retrieving information.
938
Patrick Hemmera62ae7e2018-04-29 14:23:48 -0400939.. js:attribute:: Server.name
940
941 Contain the name of the server.
942
943.. js:attribute:: Server.puid
944
945 Contain the proxy unique identifier of the server.
946
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +0100947.. js:function:: Server.is_draining(sv)
948
Patrick Hemmerc6a1d712018-05-01 21:30:41 -0400949 Return true if the server is currently draining sticky connections.
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +0100950
951 :param class_server sv: A :ref:`server_class` which indicates the manipulated
952 server.
953 :returns: a boolean
954
Patrick Hemmer32d539f2018-04-29 14:25:46 -0400955.. js:function:: Server.set_maxconn(sv, weight)
956
957 Dynamically change the maximum connections of the server. See the management
958 socket documentation for more information about the format of the string.
959
960 :param class_server sv: A :ref:`server_class` which indicates the manipulated
961 server.
962 :param string maxconn: A string describing the server maximum connections.
963
964.. js:function:: Server.get_maxconn(sv, weight)
965
966 This function returns an integer representing the server maximum connections.
967
968 :param class_server sv: A :ref:`server_class` which indicates the manipulated
969 server.
970 :returns: an integer.
971
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +0100972.. js:function:: Server.set_weight(sv, weight)
973
Patrick Hemmerc6a1d712018-05-01 21:30:41 -0400974 Dynamically change the weight of the server. See the management socket
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +0100975 documentation for more information about the format of the string.
976
977 :param class_server sv: A :ref:`server_class` which indicates the manipulated
978 server.
979 :param string weight: A string describing the server weight.
980
981.. js:function:: Server.get_weight(sv)
982
Patrick Hemmerc6a1d712018-05-01 21:30:41 -0400983 This function returns an integer representing the server weight.
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +0100984
985 :param class_server sv: A :ref:`server_class` which indicates the manipulated
986 server.
987 :returns: an integer.
988
Joseph C. Sible49bbf522020-05-04 22:20:32 -0400989.. js:function:: Server.set_addr(sv, addr[, port])
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +0100990
Patrick Hemmerc6a1d712018-05-01 21:30:41 -0400991 Dynamically change the address of the server. See the management socket
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +0100992 documentation for more information about the format of the string.
993
994 :param class_server sv: A :ref:`server_class` which indicates the manipulated
995 server.
Patrick Hemmerc6a1d712018-05-01 21:30:41 -0400996 :param string addr: A string describing the server address.
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +0100997
998.. js:function:: Server.get_addr(sv)
999
Patrick Hemmerc6a1d712018-05-01 21:30:41 -04001000 Returns a string describing the address of the server.
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001001
1002 :param class_server sv: A :ref:`server_class` which indicates the manipulated
1003 server.
1004 :returns: A string
1005
1006.. js:function:: Server.get_stats(sv)
1007
1008 Returns server statistics.
1009
1010 :param class_server sv: A :ref:`server_class` which indicates the manipulated
1011 server.
Patrick Hemmerc6a1d712018-05-01 21:30:41 -04001012 :returns: a key/value table containing stats
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001013
1014.. js:function:: Server.shut_sess(sv)
1015
1016 Shutdown all the sessions attached to the server. See the management socket
1017 documentation for more information about this function.
1018
1019 :param class_server sv: A :ref:`server_class` which indicates the manipulated
1020 server.
1021
1022.. js:function:: Server.set_drain(sv)
1023
1024 Drain sticky sessions. See the management socket documentation for more
1025 information about this function.
1026
1027 :param class_server sv: A :ref:`server_class` which indicates the manipulated
1028 server.
1029
1030.. js:function:: Server.set_maint(sv)
1031
1032 Set maintenance mode. See the management socket documentation for more
1033 information about this function.
1034
1035 :param class_server sv: A :ref:`server_class` which indicates the manipulated
1036 server.
1037
1038.. js:function:: Server.set_ready(sv)
1039
1040 Set normal mode. See the management socket documentation for more information
1041 about this function.
1042
1043 :param class_server sv: A :ref:`server_class` which indicates the manipulated
1044 server.
1045
1046.. js:function:: Server.check_enable(sv)
1047
1048 Enable health checks. See the management socket documentation for more
1049 information about this function.
1050
1051 :param class_server sv: A :ref:`server_class` which indicates the manipulated
1052 server.
1053
1054.. js:function:: Server.check_disable(sv)
1055
1056 Disable health checks. See the management socket documentation for more
1057 information about this function.
1058
1059 :param class_server sv: A :ref:`server_class` which indicates the manipulated
1060 server.
1061
1062.. js:function:: Server.check_force_up(sv)
1063
1064 Force health-check up. See the management socket documentation for more
1065 information about this function.
1066
1067 :param class_server sv: A :ref:`server_class` which indicates the manipulated
1068 server.
1069
1070.. js:function:: Server.check_force_nolb(sv)
1071
1072 Force health-check nolb mode. See the management socket documentation for more
1073 information about this function.
1074
1075 :param class_server sv: A :ref:`server_class` which indicates the manipulated
1076 server.
1077
1078.. js:function:: Server.check_force_down(sv)
1079
1080 Force health-check down. See the management socket documentation for more
1081 information about this function.
1082
1083 :param class_server sv: A :ref:`server_class` which indicates the manipulated
1084 server.
1085
1086.. js:function:: Server.agent_enable(sv)
1087
1088 Enable agent check. See the management socket documentation for more
1089 information about this function.
1090
1091 :param class_server sv: A :ref:`server_class` which indicates the manipulated
1092 server.
1093
1094.. js:function:: Server.agent_disable(sv)
1095
1096 Disable agent check. See the management socket documentation for more
1097 information about this function.
1098
1099 :param class_server sv: A :ref:`server_class` which indicates the manipulated
1100 server.
1101
1102.. js:function:: Server.agent_force_up(sv)
1103
1104 Force agent check up. See the management socket documentation for more
1105 information about this function.
1106
1107 :param class_server sv: A :ref:`server_class` which indicates the manipulated
1108 server.
1109
1110.. js:function:: Server.agent_force_down(sv)
1111
1112 Force agent check down. See the management socket documentation for more
1113 information about this function.
1114
1115 :param class_server sv: A :ref:`server_class` which indicates the manipulated
1116 server.
1117
Thierry Fournierff480422016-02-25 08:36:46 +01001118.. _listener_class:
1119
1120Listener class
1121==============
1122
1123.. js:function:: Listener.get_stats(ls)
1124
1125 Returns server statistics.
1126
1127 :param class_listener ls: A :ref:`listener_class` which indicates the
1128 manipulated listener.
Patrick Hemmerc6a1d712018-05-01 21:30:41 -04001129 :returns: a key/value table containing stats
Thierry Fournierff480422016-02-25 08:36:46 +01001130
Thierry Fournier1de16592016-01-27 09:49:07 +01001131.. _concat_class:
1132
1133Concat class
1134============
1135
1136.. js:class:: Concat
1137
1138 This class provides a fast way for string concatenation. The way using native
1139 Lua concatenation like the code below is slow for some reasons.
1140
1141.. code-block:: lua
1142
1143 str = "string1"
1144 str = str .. ", string2"
1145 str = str .. ", string3"
1146..
1147
1148 For each concatenation, Lua:
1149 * allocate memory for the result,
Ilya Shipitsin2075ca82020-03-06 23:22:22 +05001150 * catenate the two string copying the strings in the new memory block,
Bertrand Jacquin874a35c2018-09-10 21:26:07 +01001151 * free the old memory block containing the string which is no longer used.
Thierry Fournier1de16592016-01-27 09:49:07 +01001152 This process does many memory move, allocation and free. In addition, the
Bertrand Jacquin874a35c2018-09-10 21:26:07 +01001153 memory is not really freed, it is just mark mark as unused and wait for the
Thierry Fournier1de16592016-01-27 09:49:07 +01001154 garbage collector.
1155
Bertrand Jacquin874a35c2018-09-10 21:26:07 +01001156 The Concat class provide an alternative way to concatenate strings. It uses
Thierry Fournier1de16592016-01-27 09:49:07 +01001157 the internal Lua mechanism (it does not allocate memory), but it doesn't copy
1158 the data more than once.
1159
1160 On my computer, the following loops spends 0.2s for the Concat method and
1161 18.5s for the pure Lua implementation. So, the Concat class is about 1000x
1162 faster than the embedded solution.
1163
1164.. code-block:: lua
1165
1166 for j = 1, 100 do
1167 c = core.concat()
1168 for i = 1, 20000 do
1169 c:add("#####")
1170 end
1171 end
1172..
1173
1174.. code-block:: lua
1175
1176 for j = 1, 100 do
1177 c = ""
1178 for i = 1, 20000 do
1179 c = c .. "#####"
1180 end
1181 end
1182..
1183
1184.. js:function:: Concat.add(concat, string)
1185
1186 This function adds a string to the current concatenated string.
1187
1188 :param class_concat concat: A :ref:`concat_class` which contains the currently
Ilya Shipitsin2075ca82020-03-06 23:22:22 +05001189 built string.
Bertrand Jacquin874a35c2018-09-10 21:26:07 +01001190 :param string string: A new string to concatenate to the current built
Thierry Fournier1de16592016-01-27 09:49:07 +01001191 string.
1192
1193.. js:function:: Concat.dump(concat)
1194
Bertrand Jacquin874a35c2018-09-10 21:26:07 +01001195 This function returns the concatenated string.
Thierry Fournier1de16592016-01-27 09:49:07 +01001196
1197 :param class_concat concat: A :ref:`concat_class` which contains the currently
Ilya Shipitsin2075ca82020-03-06 23:22:22 +05001198 built string.
Thierry Fournier1de16592016-01-27 09:49:07 +01001199 :returns: the concatenated string
1200
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001201.. _fetches_class:
1202
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001203Fetches class
1204=============
1205
1206.. js:class:: Fetches
1207
1208 This class contains a lot of internal HAProxy sample fetches. See the
Thierry FOURNIER2e4893c2015-03-18 13:37:27 +01001209 HAProxy "configuration.txt" documentation for more information about her
Bertrand Jacquin874a35c2018-09-10 21:26:07 +01001210 usage. They are the chapters 7.3.2 to 7.3.6.
Thierry FOURNIER2e4893c2015-03-18 13:37:27 +01001211
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001212 **warning** some sample fetches are not available in some context. These
Patrick Hemmerc6a1d712018-05-01 21:30:41 -04001213 limitations are specified in this documentation when they're useful.
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001214
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01001215 :see: :js:attr:`TXN.f`
1216 :see: :js:attr:`TXN.sf`
Thierry FOURNIER2e4893c2015-03-18 13:37:27 +01001217
1218 Fetches are useful for:
1219
1220 * get system time,
1221 * get environment variable,
1222 * get random numbers,
1223 * known backend status like the number of users in queue or the number of
1224 connections established,
1225 * client information like ip source or destination,
1226 * deal with stick tables,
Ilya Shipitsin2075ca82020-03-06 23:22:22 +05001227 * Established SSL information,
Thierry FOURNIER2e4893c2015-03-18 13:37:27 +01001228 * HTTP information like headers or method.
1229
1230.. code-block:: lua
1231
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001232 function action(txn)
1233 -- Get source IP
1234 local clientip = txn.f:src()
1235 end
Thierry FOURNIER2e4893c2015-03-18 13:37:27 +01001236..
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001237
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001238.. _converters_class:
1239
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001240Converters class
1241================
1242
1243.. js:class:: Converters
1244
1245 This class contains a lot of internal HAProxy sample converters. See the
Thierry FOURNIER2e4893c2015-03-18 13:37:27 +01001246 HAProxy documentation "configuration.txt" for more information about her
1247 usage. Its the chapter 7.3.1.
1248
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01001249 :see: :js:attr:`TXN.c`
1250 :see: :js:attr:`TXN.sc`
Thierry FOURNIER2e4893c2015-03-18 13:37:27 +01001251
1252 Converters provides statefull transformation. They are useful for:
1253
1254 * converting input to base64,
1255 * applying hash on input string (djb2, crc32, sdbm, wt6),
1256 * format date,
1257 * json escape,
Pieter Baauw4d7f7662015-11-08 16:38:08 +01001258 * extracting preferred language comparing two lists,
Thierry FOURNIER2e4893c2015-03-18 13:37:27 +01001259 * turn to lower or upper chars,
1260 * deal with stick tables.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001261
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001262.. _channel_class:
1263
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001264Channel class
1265=============
1266
1267.. js:class:: Channel
1268
1269 HAProxy uses two buffers for the processing of the requests. The first one is
1270 used with the request data (from the client to the server) and the second is
1271 used for the response data (from the server to the client).
1272
1273 Each buffer contains two types of data. The first type is the incoming data
1274 waiting for a processing. The second part is the outgoing data already
1275 processed. Usually, the incoming data is processed, after it is tagged as
1276 outgoing data, and finally it is sent. The following functions provides tools
1277 for manipulating these data in a buffer.
1278
1279 The following diagram shows where the channel class function are applied.
1280
1281 **Warning**: It is not possible to read from the response in request action,
1282 and it is not possible to read for the request channel in response action.
1283
Thierry FOURNIER2e4893c2015-03-18 13:37:27 +01001284.. image:: _static/channel.png
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001285
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001286.. js:function:: Channel.dup(channel)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001287
1288 This function returns a string that contain the entire buffer. The data is
1289 not remove from the buffer and can be reprocessed later.
1290
Ilya Shipitsin2075ca82020-03-06 23:22:22 +05001291 If the buffer can't receive more data, a 'nil' value is returned.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001292
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001293 :param class_channel channel: The manipulated Channel.
Pieter Baauw4d7f7662015-11-08 16:38:08 +01001294 :returns: a string containing all the available data or nil.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001295
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001296.. js:function:: Channel.get(channel)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001297
1298 This function returns a string that contain the entire buffer. The data is
1299 consumed from the buffer.
1300
Ilya Shipitsin2075ca82020-03-06 23:22:22 +05001301 If the buffer can't receive more data, a 'nil' value is returned.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001302
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001303 :param class_channel channel: The manipulated Channel.
Pieter Baauw4d7f7662015-11-08 16:38:08 +01001304 :returns: a string containing all the available data or nil.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001305
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02001306.. js:function:: Channel.getline(channel)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001307
1308 This function returns a string that contain the first line of the buffer. The
1309 data is consumed. If the data returned doesn't contains a final '\n' its
1310 assumed than its the last available data in the buffer.
1311
Ilya Shipitsin2075ca82020-03-06 23:22:22 +05001312 If the buffer can't receive more data, a 'nil' value is returned.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001313
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001314 :param class_channel channel: The manipulated Channel.
Pieter Baauw386a1272015-08-16 15:26:24 +02001315 :returns: a string containing the available line or nil.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001316
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001317.. js:function:: Channel.set(channel, string)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001318
1319 This function replace the content of the buffer by the string. The function
1320 returns the copied length, otherwise, it returns -1.
1321
1322 The data set with this function are not send. They wait for the end of
1323 HAProxy processing, so the buffer can be full.
1324
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001325 :param class_channel channel: The manipulated Channel.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001326 :param string string: The data which will sent.
Pieter Baauw4d7f7662015-11-08 16:38:08 +01001327 :returns: an integer containing the amount of bytes copied or -1.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001328
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001329.. js:function:: Channel.append(channel, string)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001330
1331 This function append the string argument to the content of the buffer. The
1332 function returns the copied length, otherwise, it returns -1.
1333
1334 The data set with this function are not send. They wait for the end of
1335 HAProxy processing, so the buffer can be full.
1336
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001337 :param class_channel channel: The manipulated Channel.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001338 :param string string: The data which will sent.
Pieter Baauw4d7f7662015-11-08 16:38:08 +01001339 :returns: an integer containing the amount of bytes copied or -1.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001340
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001341.. js:function:: Channel.send(channel, string)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001342
1343 This function required immediate send of the data. Unless if the connection
1344 is close, the buffer is regularly flushed and all the string can be sent.
1345
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001346 :param class_channel channel: The manipulated Channel.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001347 :param string string: The data which will sent.
Pieter Baauw4d7f7662015-11-08 16:38:08 +01001348 :returns: an integer containing the amount of bytes copied or -1.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001349
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001350.. js:function:: Channel.get_in_length(channel)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001351
1352 This function returns the length of the input part of the buffer.
1353
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001354 :param class_channel channel: The manipulated Channel.
Pieter Baauw4d7f7662015-11-08 16:38:08 +01001355 :returns: an integer containing the amount of available bytes.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001356
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001357.. js:function:: Channel.get_out_length(channel)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001358
1359 This function returns the length of the output part of the buffer.
1360
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001361 :param class_channel channel: The manipulated Channel.
Pieter Baauw4d7f7662015-11-08 16:38:08 +01001362 :returns: an integer containing the amount of available bytes.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001363
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001364.. js:function:: Channel.forward(channel, int)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001365
1366 This function transfer bytes from the input part of the buffer to the output
1367 part.
1368
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001369 :param class_channel channel: The manipulated Channel.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001370 :param integer int: The amount of data which will be forwarded.
1371
Thierry FOURNIER / OZON.IO65192f32016-11-07 15:28:40 +01001372.. js:function:: Channel.is_full(channel)
1373
1374 This function returns true if the buffer channel is full.
1375
1376 :returns: a boolean
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001377
1378.. _http_class:
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001379
1380HTTP class
1381==========
1382
1383.. js:class:: HTTP
1384
1385 This class contain all the HTTP manipulation functions.
1386
Pieter Baauw386a1272015-08-16 15:26:24 +02001387.. js:function:: HTTP.req_get_headers(http)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001388
Patrick Hemmerc6a1d712018-05-01 21:30:41 -04001389 Returns a table containing all the request headers.
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001390
1391 :param class_http http: The related http object.
Patrick Hemmerc6a1d712018-05-01 21:30:41 -04001392 :returns: table of headers.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01001393 :see: :js:func:`HTTP.res_get_headers`
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001394
Patrick Hemmerc6a1d712018-05-01 21:30:41 -04001395 This is the form of the returned table:
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001396
1397.. code-block:: lua
1398
1399 HTTP:req_get_headers()['<header-name>'][<header-index>] = "<header-value>"
1400
1401 local hdr = HTTP:req_get_headers()
1402 hdr["host"][0] = "www.test.com"
1403 hdr["accept"][0] = "audio/basic q=1"
1404 hdr["accept"][1] = "audio/*, q=0.2"
1405 hdr["accept"][2] = "*/*, q=0.1"
1406..
1407
Pieter Baauw386a1272015-08-16 15:26:24 +02001408.. js:function:: HTTP.res_get_headers(http)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001409
Patrick Hemmerc6a1d712018-05-01 21:30:41 -04001410 Returns a table containing all the response headers.
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001411
1412 :param class_http http: The related http object.
Patrick Hemmerc6a1d712018-05-01 21:30:41 -04001413 :returns: table of headers.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01001414 :see: :js:func:`HTTP.req_get_headers`
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001415
Patrick Hemmerc6a1d712018-05-01 21:30:41 -04001416 This is the form of the returned table:
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001417
1418.. code-block:: lua
1419
1420 HTTP:res_get_headers()['<header-name>'][<header-index>] = "<header-value>"
1421
1422 local hdr = HTTP:req_get_headers()
1423 hdr["host"][0] = "www.test.com"
1424 hdr["accept"][0] = "audio/basic q=1"
1425 hdr["accept"][1] = "audio/*, q=0.2"
1426 hdr["accept"][2] = "*.*, q=0.1"
1427..
1428
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001429.. js:function:: HTTP.req_add_header(http, name, value)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001430
1431 Appends an HTTP header field in the request whose name is
1432 specified in "name" and whose value is defined in "value".
1433
1434 :param class_http http: The related http object.
1435 :param string name: The header name.
1436 :param string value: The header value.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01001437 :see: :js:func:`HTTP.res_add_header`
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001438
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001439.. js:function:: HTTP.res_add_header(http, name, value)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001440
Bertrand Jacquin874a35c2018-09-10 21:26:07 +01001441 Appends an HTTP header field in the response whose name is
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001442 specified in "name" and whose value is defined in "value".
1443
1444 :param class_http http: The related http object.
1445 :param string name: The header name.
1446 :param string value: The header value.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01001447 :see: :js:func:`HTTP.req_add_header`
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001448
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001449.. js:function:: HTTP.req_del_header(http, name)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001450
1451 Removes all HTTP header fields in the request whose name is
1452 specified in "name".
1453
1454 :param class_http http: The related http object.
1455 :param string name: The header name.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01001456 :see: :js:func:`HTTP.res_del_header`
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001457
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001458.. js:function:: HTTP.res_del_header(http, name)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001459
1460 Removes all HTTP header fields in the response whose name is
1461 specified in "name".
1462
1463 :param class_http http: The related http object.
1464 :param string name: The header name.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01001465 :see: :js:func:`HTTP.req_del_header`
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001466
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001467.. js:function:: HTTP.req_set_header(http, name, value)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001468
Bertrand Jacquin874a35c2018-09-10 21:26:07 +01001469 This variable replace all occurrence of all header "name", by only
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001470 one containing the "value".
1471
1472 :param class_http http: The related http object.
1473 :param string name: The header name.
1474 :param string value: The header value.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01001475 :see: :js:func:`HTTP.res_set_header`
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001476
Bertrand Jacquin874a35c2018-09-10 21:26:07 +01001477 This function does the same work as the following code:
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001478
1479.. code-block:: lua
1480
1481 function fcn(txn)
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001482 TXN.http:req_del_header("header")
1483 TXN.http:req_add_header("header", "value")
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001484 end
1485..
1486
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001487.. js:function:: HTTP.res_set_header(http, name, value)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001488
Bertrand Jacquin874a35c2018-09-10 21:26:07 +01001489 This variable replace all occurrence of all header "name", by only
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001490 one containing the "value".
1491
1492 :param class_http http: The related http object.
1493 :param string name: The header name.
1494 :param string value: The header value.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01001495 :see: :js:func:`HTTP.req_rep_header()`
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001496
Pieter Baauw386a1272015-08-16 15:26:24 +02001497.. js:function:: HTTP.req_rep_header(http, name, regex, replace)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001498
1499 Matches the regular expression in all occurrences of header field "name"
1500 according to "regex", and replaces them with the "replace" argument. The
1501 replacement value can contain back references like \1, \2, ... This
1502 function works with the request.
1503
1504 :param class_http http: The related http object.
1505 :param string name: The header name.
1506 :param string regex: The match regular expression.
1507 :param string replace: The replacement value.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01001508 :see: :js:func:`HTTP.res_rep_header()`
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001509
Pieter Baauw386a1272015-08-16 15:26:24 +02001510.. js:function:: HTTP.res_rep_header(http, name, regex, string)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001511
1512 Matches the regular expression in all occurrences of header field "name"
1513 according to "regex", and replaces them with the "replace" argument. The
1514 replacement value can contain back references like \1, \2, ... This
1515 function works with the request.
1516
1517 :param class_http http: The related http object.
1518 :param string name: The header name.
1519 :param string regex: The match regular expression.
1520 :param string replace: The replacement value.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01001521 :see: :js:func:`HTTP.req_rep_header()`
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001522
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001523.. js:function:: HTTP.req_set_method(http, method)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001524
1525 Rewrites the request method with the parameter "method".
1526
1527 :param class_http http: The related http object.
1528 :param string method: The new method.
1529
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001530.. js:function:: HTTP.req_set_path(http, path)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001531
1532 Rewrites the request path with the "path" parameter.
1533
1534 :param class_http http: The related http object.
1535 :param string path: The new path.
1536
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001537.. js:function:: HTTP.req_set_query(http, query)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001538
1539 Rewrites the request's query string which appears after the first question
1540 mark ("?") with the parameter "query".
1541
1542 :param class_http http: The related http object.
1543 :param string query: The new query.
1544
Thierry FOURNIER0d79cf62015-08-26 14:20:58 +02001545.. js:function:: HTTP.req_set_uri(http, uri)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001546
1547 Rewrites the request URI with the parameter "uri".
1548
1549 :param class_http http: The related http object.
1550 :param string uri: The new uri.
1551
Robin H. Johnson52f5db22017-01-01 13:10:52 -08001552.. js:function:: HTTP.res_set_status(http, status [, reason])
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +02001553
Robin H. Johnson52f5db22017-01-01 13:10:52 -08001554 Rewrites the response status code with the parameter "code".
1555
1556 If no custom reason is provided, it will be generated from the status.
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +02001557
1558 :param class_http http: The related http object.
1559 :param integer status: The new response status code.
Robin H. Johnson52f5db22017-01-01 13:10:52 -08001560 :param string reason: The new response reason (optional).
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +02001561
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001562.. _txn_class:
1563
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001564TXN class
1565=========
1566
1567.. js:class:: TXN
1568
1569 The txn class contain all the functions relative to the http or tcp
1570 transaction (Note than a tcp stream is the same than a tcp transaction, but
1571 an HTTP transaction is not the same than a tcp stream).
1572
1573 The usage of this class permits to retrieve data from the requests, alter it
1574 and forward it.
1575
1576 All the functions provided by this class are available in the context
1577 **sample-fetches** and **actions**.
1578
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001579.. js:attribute:: TXN.c
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001580
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001581 :returns: An :ref:`converters_class`.
1582
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001583 This attribute contains a Converters class object.
1584
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001585.. js:attribute:: TXN.sc
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001586
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001587 :returns: An :ref:`converters_class`.
1588
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001589 This attribute contains a Converters class object. The functions of
1590 this object returns always a string.
1591
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001592.. js:attribute:: TXN.f
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001593
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001594 :returns: An :ref:`fetches_class`.
1595
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001596 This attribute contains a Fetches class object.
1597
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001598.. js:attribute:: TXN.sf
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001599
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001600 :returns: An :ref:`fetches_class`.
1601
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001602 This attribute contains a Fetches class object. The functions of
1603 this object returns always a string.
1604
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001605.. js:attribute:: TXN.req
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001606
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001607 :returns: An :ref:`channel_class`.
1608
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001609 This attribute contains a channel class object for the request buffer.
1610
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001611.. js:attribute:: TXN.res
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001612
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001613 :returns: An :ref:`channel_class`.
1614
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001615 This attribute contains a channel class object for the response buffer.
1616
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001617.. js:attribute:: TXN.http
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001618
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001619 :returns: An :ref:`http_class`.
1620
Bertrand Jacquin874a35c2018-09-10 21:26:07 +01001621 This attribute contains an HTTP class object. It is available only if the
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001622 proxy has the "mode http" enabled.
1623
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01001624.. js:function:: TXN.log(TXN, loglevel, msg)
1625
1626 This function sends a log. The log is sent, according with the HAProxy
1627 configuration file, on the default syslog server if it is configured and on
1628 the stderr if it is allowed.
1629
1630 :param class_txn txn: The class txn object containing the data.
Bertrand Jacquin874a35c2018-09-10 21:26:07 +01001631 :param integer loglevel: Is the log level associated with the message. It is a
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01001632 number between 0 and 7.
1633 :param string msg: The log content.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01001634 :see: :js:attr:`core.emerg`, :js:attr:`core.alert`, :js:attr:`core.crit`,
1635 :js:attr:`core.err`, :js:attr:`core.warning`, :js:attr:`core.notice`,
1636 :js:attr:`core.info`, :js:attr:`core.debug` (log level definitions)
1637 :see: :js:func:`TXN.deflog`
1638 :see: :js:func:`TXN.Debug`
1639 :see: :js:func:`TXN.Info`
1640 :see: :js:func:`TXN.Warning`
1641 :see: :js:func:`TXN.Alert`
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01001642
1643.. js:function:: TXN.deflog(TXN, msg)
1644
Bertrand Jacquin874a35c2018-09-10 21:26:07 +01001645 Sends a log line with the default loglevel for the proxy associated with the
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01001646 transaction.
1647
1648 :param class_txn txn: The class txn object containing the data.
1649 :param string msg: The log content.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01001650 :see: :js:func:`TXN.log
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01001651
1652.. js:function:: TXN.Debug(txn, msg)
1653
1654 :param class_txn txn: The class txn object containing the data.
1655 :param string msg: The log content.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01001656 :see: :js:func:`TXN.log`
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01001657
1658 Does the same job than:
1659
1660.. code-block:: lua
1661
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001662 function Debug(txn, msg)
1663 TXN.log(txn, core.debug, msg)
1664 end
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01001665..
1666
1667.. js:function:: TXN.Info(txn, msg)
1668
1669 :param class_txn txn: The class txn object containing the data.
1670 :param string msg: The log content.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01001671 :see: :js:func:`TXN.log`
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01001672
1673.. code-block:: lua
1674
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001675 function Debug(txn, msg)
1676 TXN.log(txn, core.info, msg)
1677 end
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01001678..
1679
1680.. js:function:: TXN.Warning(txn, msg)
1681
1682 :param class_txn txn: The class txn object containing the data.
1683 :param string msg: The log content.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01001684 :see: :js:func:`TXN.log`
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01001685
1686.. code-block:: lua
1687
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001688 function Debug(txn, msg)
1689 TXN.log(txn, core.warning, msg)
1690 end
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01001691..
1692
1693.. js:function:: TXN.Alert(txn, msg)
1694
1695 :param class_txn txn: The class txn object containing the data.
1696 :param string msg: The log content.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01001697 :see: :js:func:`TXN.log`
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01001698
1699.. code-block:: lua
1700
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001701 function Debug(txn, msg)
1702 TXN.log(txn, core.alert, msg)
1703 end
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01001704..
1705
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001706.. js:function:: TXN.get_priv(txn)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001707
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001708 Return Lua data stored in the current transaction (with the `TXN.set_priv()`)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001709 function. If no data are stored, it returns a nil value.
1710
1711 :param class_txn txn: The class txn object containing the data.
Bertrand Jacquin874a35c2018-09-10 21:26:07 +01001712 :returns: the opaque data previously stored, or nil if nothing is
1713 available.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001714
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001715.. js:function:: TXN.set_priv(txn, data)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001716
1717 Store any data in the current HAProxy transaction. This action replace the
1718 old stored data.
1719
1720 :param class_txn txn: The class txn object containing the data.
1721 :param opaque data: The data which is stored in the transaction.
1722
Tim Duesterhus4e172c92020-05-19 13:49:42 +02001723.. js:function:: TXN.set_var(TXN, var, value[, ifexist])
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +02001724
David Carlier61fdf8b2015-10-02 11:59:38 +01001725 Converts a Lua type in a HAProxy type and store it in a variable <var>.
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +02001726
1727 :param class_txn txn: The class txn object containing the data.
1728 :param string var: The variable name according with the HAProxy variable syntax.
Thierry FOURNIER / OZON.IOb210bcc2016-12-12 16:24:16 +01001729 :param type value: The value associated to the variable. The type can be string or
1730 integer.
Tim Duesterhus4e172c92020-05-19 13:49:42 +02001731 :param boolean ifexist: If this parameter is set to a truthy value the variable
1732 will only be set if it was defined elsewhere (i.e. used
1733 within the configuration). It is highly recommended to
1734 always set this to true.
Christopher Faulet85d79c92016-11-09 16:54:56 +01001735
1736.. js:function:: TXN.unset_var(TXN, var)
1737
1738 Unset the variable <var>.
1739
1740 :param class_txn txn: The class txn object containing the data.
1741 :param string var: The variable name according with the HAProxy variable syntax.
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +02001742
1743.. js:function:: TXN.get_var(TXN, var)
1744
1745 Returns data stored in the variable <var> converter in Lua type.
1746
1747 :param class_txn txn: The class txn object containing the data.
1748 :param string var: The variable name according with the HAProxy variable syntax.
1749
Christopher Faulet700d9e82020-01-31 12:21:52 +01001750.. js:function:: TXN.reply([reply])
1751
1752 Return a new reply object
1753
1754 :param table reply: A table containing info to initialize the reply fields.
1755 :returns: A :ref:`reply_class` object.
1756
1757 The table used to initialized the reply object may contain following entries :
1758
1759 * status : The reply status code. the code 200 is used by default.
1760 * reason : The reply reason. The reason corresponding to the status code is
1761 used by default.
1762 * headers : An list of headers, indexed by header name. Empty by default. For
1763 a given name, multiple values are possible, stored in an ordered list.
1764 * body : The reply body, empty by default.
1765
1766.. code-block:: lua
1767
1768 local reply = txn:reply{
1769 status = 400,
1770 reason = "Bad request",
1771 headers = {
1772 ["content-type"] = { "text/html" },
1773 ["cache-control"] = {"no-cache", "no-store" }
1774 },
1775 body = "<html><body><h1>invalid request<h1></body></html>"
1776 }
1777..
1778 :see: :js:class:`Reply`
1779
1780.. js:function:: TXN.done(txn[, reply])
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001781
Willy Tarreaubc183a62015-08-28 10:39:11 +02001782 This function terminates processing of the transaction and the associated
Christopher Faulet700d9e82020-01-31 12:21:52 +01001783 session and optionally reply to the client for HTTP sessions.
1784
1785 :param class_txn txn: The class txn object containing the data.
1786 :param class_reply reply: The class reply object to return to the client.
1787
1788 This functions can be used when a critical error is detected or to terminate
Willy Tarreaubc183a62015-08-28 10:39:11 +02001789 processing after some data have been returned to the client (eg: a redirect).
Christopher Faulet700d9e82020-01-31 12:21:52 +01001790 To do so, a reply may be provided. This object is optional and may contain a
1791 status code, a reason, a header list and a body. All these fields are
1792 optionnals. When not provided, the default values are used. By default, with
1793 an empty reply object, an empty HTTP 200 response is returned to the
1794 client. If no reply object is provided, the transaction is terminated without
1795 any reply.
1796
1797 The reply object may be fully created in lua or the class Reply may be used to
1798 create it.
1799
1800.. code-block:: lua
1801
1802 local reply = txn:reply()
1803 reply:set_status(400, "Bad request")
1804 reply:add_header("content-type", "text/html")
1805 reply:add_header("cache-control", "no-cache")
1806 reply:add_header("cache-control", "no-store")
1807 reply:set_body("<html><body><h1>invalid request<h1></body></html>")
1808 txn:done(reply)
1809..
1810
1811.. code-block:: lua
1812
1813 txn:done{
1814 status = 400,
1815 reason = "Bad request",
1816 headers = {
1817 ["content-type"] = { "text/html" },
1818 ["cache-control"] = { "no-cache", "no-store" },
1819 },
1820 body = "<html><body><h1>invalid request<h1></body></html>"
1821 }
1822..
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001823
Thierry FOURNIERab00df62016-07-14 11:42:37 +02001824 *Warning*: It not make sense to call this function from sample-fetches. In
1825 this case the behaviour of this one is the same than core.done(): it quit
1826 the Lua execution. The transaction is really aborted only from an action
1827 registered function.
1828
Christopher Faulet700d9e82020-01-31 12:21:52 +01001829 :see: :js:func:`TXN.reply`, :js:class:`Reply`
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001830
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001831.. js:function:: TXN.set_loglevel(txn, loglevel)
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01001832
1833 Is used to change the log level of the current request. The "loglevel" must
1834 be an integer between 0 and 7.
1835
1836 :param class_txn txn: The class txn object containing the data.
1837 :param integer loglevel: The required log level. This variable can be one of
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01001838 :see: :js:attr:`core.emerg`, :js:attr:`core.alert`, :js:attr:`core.crit`,
1839 :js:attr:`core.err`, :js:attr:`core.warning`, :js:attr:`core.notice`,
1840 :js:attr:`core.info`, :js:attr:`core.debug` (log level definitions)
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01001841
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001842.. js:function:: TXN.set_tos(txn, tos)
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01001843
1844 Is used to set the TOS or DSCP field value of packets sent to the client to
1845 the value passed in "tos" on platforms which support this.
1846
1847 :param class_txn txn: The class txn object containing the data.
1848 :param integer tos: The new TOS os DSCP.
1849
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001850.. js:function:: TXN.set_mark(txn, mark)
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01001851
1852 Is used to set the Netfilter MARK on all packets sent to the client to the
1853 value passed in "mark" on platforms which support it.
1854
1855 :param class_txn txn: The class txn object containing the data.
1856 :param integer mark: The mark value.
1857
Patrick Hemmer268a7072018-05-11 12:52:31 -04001858.. js:function:: TXN.set_priority_class(txn, prio)
1859
1860 This function adjusts the priority class of the transaction. The value should
1861 be within the range -2047..2047. Values outside this range will be
1862 truncated.
1863
1864 See the HAProxy configuration.txt file keyword "http-request" action
1865 "set-priority-class" for details.
1866
1867.. js:function:: TXN.set_priority_offset(txn, prio)
1868
1869 This function adjusts the priority offset of the transaction. The value
1870 should be within the range -524287..524287. Values outside this range will be
1871 truncated.
1872
1873 See the HAProxy configuration.txt file keyword "http-request" action
1874 "set-priority-offset" for details.
1875
Christopher Faulet700d9e82020-01-31 12:21:52 +01001876.. _reply_class:
1877
1878Reply class
1879============
1880
1881.. js:class:: Reply
1882
1883 **context**: action
1884
1885 This class represents an HTTP response message. It provides some methods to
1886 enrich it.
1887
1888.. code-block:: lua
1889
1890 local reply = txn:reply({status = 400}) -- default HTTP 400 reason-phase used
1891 reply:add_header("content-type", "text/html")
1892 reply:add_header("cache-control", "no-cache")
1893 reply:add_header("cache-control", "no-store")
1894 reply:set_body("<html><body><h1>invalid request<h1></body></html>")
1895..
1896
1897 :see: :js:func:`TXN.reply`
1898
1899.. js:attribute:: Reply.status
1900
1901 The reply status code. By default, the status code is set to 200.
1902
1903 :returns: integer
1904
1905.. js:attribute:: Reply.reason
1906
1907 The reason string describing the status code.
1908
1909 :returns: string
1910
1911.. js:attribute:: Reply.headers
1912
1913 A table indexing all reply headers by name. To each name is associated an
1914 ordered list of values.
1915
1916 :returns: Lua table
1917
1918.. code-block:: lua
1919
1920 {
1921 ["content-type"] = { "text/html" },
1922 ["cache-control"] = {"no-cache", "no-store" },
1923 x_header_name = { "value1", "value2", ... }
1924 ...
1925 }
1926..
1927
1928.. js:attribute:: Reply.body
1929
1930 The reply payload.
1931
1932 :returns: string
1933
1934.. js:function:: Reply.set_status(REPLY, status[, reason])
1935
1936 Set the reply status code and optionally the reason-phrase. If the reason is
1937 not provided, the default reason corresponding to the status code is used.
1938
1939 :param class_reply reply: The related Reply object.
1940 :param integer status: The reply status code.
1941 :param string reason: The reply status reason (optional).
1942
1943.. js:function:: Reply.add_header(REPLY, name, value)
1944
1945 Add a header to the reply object. If the header does not already exist, a new
1946 entry is created with its name as index and a one-element list containing its
1947 value as value. Otherwise, the header value is appended to the ordered list of
1948 values associated to the header name.
1949
1950 :param class_reply reply: The related Reply object.
1951 :param string name: The header field name.
1952 :param string value: The header field value.
1953
1954.. js:function:: Reply.del_header(REPLY, name)
1955
1956 Remove all occurrences of a header name from the reply object.
1957
1958 :param class_reply reply: The related Reply object.
1959 :param string name: The header field name.
1960
1961.. js:function:: Reply.set_body(REPLY, body)
1962
1963 Set the reply payload.
1964
1965 :param class_reply reply: The related Reply object.
1966 :param string body: The reply payload.
1967
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001968.. _socket_class:
1969
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001970Socket class
1971============
1972
1973.. js:class:: Socket
1974
1975 This class must be compatible with the Lua Socket class. Only the 'client'
1976 functions are available. See the Lua Socket documentation:
1977
1978 `http://w3.impa.br/~diego/software/luasocket/tcp.html
1979 <http://w3.impa.br/~diego/software/luasocket/tcp.html>`_
1980
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001981.. js:function:: Socket.close(socket)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001982
1983 Closes a TCP object. The internal socket used by the object is closed and the
1984 local address to which the object was bound is made available to other
1985 applications. No further operations (except for further calls to the close
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001986 method) are allowed on a closed Socket.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001987
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001988 :param class_socket socket: Is the manipulated Socket.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001989
1990 Note: It is important to close all used sockets once they are not needed,
1991 since, in many systems, each socket uses a file descriptor, which are limited
1992 system resources. Garbage-collected objects are automatically closed before
1993 destruction, though.
1994
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02001995.. js:function:: Socket.connect(socket, address[, port])
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001996
1997 Attempts to connect a socket object to a remote host.
1998
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001999
2000 In case of error, the method returns nil followed by a string describing the
2001 error. In case of success, the method returns 1.
2002
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002003 :param class_socket socket: Is the manipulated Socket.
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002004 :param string address: can be an IP address or a host name. See below for more
2005 information.
2006 :param integer port: must be an integer number in the range [1..64K].
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002007 :returns: 1 or nil.
2008
Bertrand Jacquin874a35c2018-09-10 21:26:07 +01002009 An address field extension permits to use the connect() function to connect to
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002010 other stream than TCP. The syntax containing a simpleipv4 or ipv6 address is
2011 the basically expected format. This format requires the port.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002012
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002013 Other format accepted are a socket path like "/socket/path", it permits to
Bertrand Jacquin874a35c2018-09-10 21:26:07 +01002014 connect to a socket. Abstract namespaces are supported with the prefix
Joseph Herlant02cedc42018-11-13 19:45:17 -08002015 "abns@", and finally a file descriptor can be passed with the prefix "fd@".
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002016 The prefix "ipv4@", "ipv6@" and "unix@" are also supported. The port can be
Bertrand Jacquin874a35c2018-09-10 21:26:07 +01002017 passed int the string. The syntax "127.0.0.1:1234" is valid. In this case, the
Tim Duesterhus6edab862018-01-06 19:04:45 +01002018 parameter *port* must not be set.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002019
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002020.. js:function:: Socket.connect_ssl(socket, address, port)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002021
2022 Same behavior than the function socket:connect, but uses SSL.
2023
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002024 :param class_socket socket: Is the manipulated Socket.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002025 :returns: 1 or nil.
2026
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002027.. js:function:: Socket.getpeername(socket)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002028
2029 Returns information about the remote side of a connected client object.
2030
2031 Returns a string with the IP address of the peer, followed by the port number
2032 that peer is using for the connection. In case of error, the method returns
2033 nil.
2034
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002035 :param class_socket socket: Is the manipulated Socket.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002036 :returns: a string containing the server information.
2037
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002038.. js:function:: Socket.getsockname(socket)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002039
2040 Returns the local address information associated to the object.
2041
2042 The method returns a string with local IP address and a number with the port.
2043 In case of error, the method returns nil.
2044
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002045 :param class_socket socket: Is the manipulated Socket.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002046 :returns: a string containing the client information.
2047
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002048.. js:function:: Socket.receive(socket, [pattern [, prefix]])
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002049
2050 Reads data from a client object, according to the specified read pattern.
2051 Patterns follow the Lua file I/O format, and the difference in performance
2052 between all patterns is negligible.
2053
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002054 :param class_socket socket: Is the manipulated Socket.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002055 :param string|integer pattern: Describe what is required (see below).
2056 :param string prefix: A string which will be prefix the returned data.
2057 :returns: a string containing the required data or nil.
2058
2059 Pattern can be any of the following:
2060
2061 * **`*a`**: reads from the socket until the connection is closed. No
2062 end-of-line translation is performed;
2063
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002064 * **`*l`**: reads a line of text from the Socket. The line is terminated by a
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002065 LF character (ASCII 10), optionally preceded by a CR character
2066 (ASCII 13). The CR and LF characters are not included in the
2067 returned line. In fact, all CR characters are ignored by the
2068 pattern. This is the default pattern.
2069
2070 * **number**: causes the method to read a specified number of bytes from the
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002071 Socket. Prefix is an optional string to be concatenated to the
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002072 beginning of any received data before return.
2073
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002074 * **empty**: If the pattern is left empty, the default option is `*l`.
2075
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002076 If successful, the method returns the received pattern. In case of error, the
2077 method returns nil followed by an error message which can be the string
2078 'closed' in case the connection was closed before the transmission was
2079 completed or the string 'timeout' in case there was a timeout during the
2080 operation. Also, after the error message, the function returns the partial
2081 result of the transmission.
2082
2083 Important note: This function was changed severely. It used to support
2084 multiple patterns (but I have never seen this feature used) and now it
2085 doesn't anymore. Partial results used to be returned in the same way as
2086 successful results. This last feature violated the idea that all functions
2087 should return nil on error. Thus it was changed too.
2088
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002089.. js:function:: Socket.send(socket, data [, start [, end ]])
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002090
2091 Sends data through client object.
2092
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002093 :param class_socket socket: Is the manipulated Socket.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002094 :param string data: The data that will be sent.
2095 :param integer start: The start position in the buffer of the data which will
2096 be sent.
2097 :param integer end: The end position in the buffer of the data which will
2098 be sent.
2099 :returns: see below.
2100
2101 Data is the string to be sent. The optional arguments i and j work exactly
2102 like the standard string.sub Lua function to allow the selection of a
2103 substring to be sent.
2104
2105 If successful, the method returns the index of the last byte within [start,
2106 end] that has been sent. Notice that, if start is 1 or absent, this is
2107 effectively the total number of bytes sent. In case of error, the method
2108 returns nil, followed by an error message, followed by the index of the last
2109 byte within [start, end] that has been sent. You might want to try again from
2110 the byte following that. The error message can be 'closed' in case the
2111 connection was closed before the transmission was completed or the string
2112 'timeout' in case there was a timeout during the operation.
2113
2114 Note: Output is not buffered. For small strings, it is always better to
2115 concatenate them in Lua (with the '..' operator) and send the result in one
2116 call instead of calling the method several times.
2117
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002118.. js:function:: Socket.setoption(socket, option [, value])
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002119
2120 Just implemented for compatibility, this cal does nothing.
2121
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002122.. js:function:: Socket.settimeout(socket, value [, mode])
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002123
2124 Changes the timeout values for the object. All I/O operations are blocking.
2125 That is, any call to the methods send, receive, and accept will block
2126 indefinitely, until the operation completes. The settimeout method defines a
2127 limit on the amount of time the I/O methods can block. When a timeout time
2128 has elapsed, the affected methods give up and fail with an error code.
2129
2130 The amount of time to wait is specified as the value parameter, in seconds.
2131
Mark Lakes56cc1252018-03-27 09:48:06 +02002132 The timeout modes are not implemented, the only settable timeout is the
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002133 inactivity time waiting for complete the internal buffer send or waiting for
2134 receive data.
2135
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002136 :param class_socket socket: Is the manipulated Socket.
Bertrand Jacquin874a35c2018-09-10 21:26:07 +01002137 :param float value: The timeout value. Use floating point to specify
Mark Lakes56cc1252018-03-27 09:48:06 +02002138 milliseconds.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002139
Thierry FOURNIER31904272017-10-25 12:59:51 +02002140.. _regex_class:
2141
2142Regex class
2143===========
2144
2145.. js:class:: Regex
2146
2147 This class allows the usage of HAProxy regexes because classic lua doesn't
2148 provides regexes. This class inherits the HAProxy compilation options, so the
2149 regexes can be libc regex, pcre regex or pcre JIT regex.
2150
2151 The expression matching number is limited to 20 per regex. The only available
2152 option is case sensitive.
2153
2154 Because regexes compilation is a heavy process, it is better to define all
2155 your regexes in the **body context** and use it during the runtime.
2156
2157.. code-block:: lua
2158
2159 -- Create the regex
2160 st, regex = Regex.new("needle (..) (...)", true);
2161
2162 -- Check compilation errors
2163 if st == false then
2164 print "error: " .. regex
2165 end
2166
2167 -- Match the regexes
2168 print(regex:exec("Looking for a needle in the haystack")) -- true
2169 print(regex:exec("Lokking for a cat in the haystack")) -- false
2170
2171 -- Extract words
2172 st, list = regex:match("Looking for a needle in the haystack")
2173 print(st) -- true
2174 print(list[1]) -- needle in the
2175 print(list[2]) -- in
2176 print(list[3]) -- the
2177
2178.. js:function:: Regex.new(regex, case_sensitive)
2179
2180 Create and compile a regex.
2181
2182 :param string regex: The regular expression according with the libc or pcre
2183 standard
2184 :param boolean case_sensitive: Match is case sensitive or not.
2185 :returns: boolean status and :ref:`regex_class` or string containing fail reason.
2186
2187.. js:function:: Regex.exec(regex, str)
2188
2189 Execute the regex.
2190
2191 :param class_regex regex: A :ref:`regex_class` object.
2192 :param string str: The input string will be compared with the compiled regex.
2193 :returns: a boolean status according with the match result.
2194
2195.. js:function:: Regex.match(regex, str)
2196
2197 Execute the regex and return matched expressions.
2198
2199 :param class_map map: A :ref:`regex_class` object.
2200 :param string str: The input string will be compared with the compiled regex.
2201 :returns: a boolean status according with the match result, and
2202 a table containing all the string matched in order of declaration.
2203
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002204.. _map_class:
2205
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002206Map class
2207=========
2208
2209.. js:class:: Map
2210
2211 This class permits to do some lookup in HAProxy maps. The declared maps can
Bertrand Jacquin874a35c2018-09-10 21:26:07 +01002212 be modified during the runtime through the HAProxy management socket.
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002213
2214.. code-block:: lua
2215
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002216 default = "usa"
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002217
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002218 -- Create and load map
Thierry FOURNIER4dc71972017-01-28 08:33:08 +01002219 geo = Map.new("geo.map", Map._ip);
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002220
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002221 -- Create new fetch that returns the user country
2222 core.register_fetches("country", function(txn)
2223 local src;
2224 local loc;
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002225
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002226 src = txn.f:fhdr("x-forwarded-for");
2227 if (src == nil) then
2228 src = txn.f:src()
2229 if (src == nil) then
2230 return default;
2231 end
2232 end
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002233
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002234 -- Perform lookup
2235 loc = geo:lookup(src);
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002236
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002237 if (loc == nil) then
2238 return default;
2239 end
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002240
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002241 return loc;
2242 end);
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002243
Thierry FOURNIER4dc71972017-01-28 08:33:08 +01002244.. js:attribute:: Map._int
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002245
2246 See the HAProxy configuration.txt file, chapter "Using ACLs and fetching
Ilya Shipitsin2075ca82020-03-06 23:22:22 +05002247 samples" and subchapter "ACL basics" to understand this pattern matching
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002248 method.
2249
Thierry FOURNIER4dc71972017-01-28 08:33:08 +01002250 Note that :js:attr:`Map.int` is also available for compatibility.
2251
2252.. js:attribute:: Map._ip
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002253
2254 See the HAProxy configuration.txt file, chapter "Using ACLs and fetching
Ilya Shipitsin2075ca82020-03-06 23:22:22 +05002255 samples" and subchapter "ACL basics" to understand this pattern matching
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002256 method.
2257
Thierry FOURNIER4dc71972017-01-28 08:33:08 +01002258 Note that :js:attr:`Map.ip` is also available for compatibility.
2259
2260.. js:attribute:: Map._str
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002261
2262 See the HAProxy configuration.txt file, chapter "Using ACLs and fetching
Ilya Shipitsin2075ca82020-03-06 23:22:22 +05002263 samples" and subchapter "ACL basics" to understand this pattern matching
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002264 method.
2265
Thierry FOURNIER4dc71972017-01-28 08:33:08 +01002266 Note that :js:attr:`Map.str` is also available for compatibility.
2267
2268.. js:attribute:: Map._beg
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002269
2270 See the HAProxy configuration.txt file, chapter "Using ACLs and fetching
Ilya Shipitsin2075ca82020-03-06 23:22:22 +05002271 samples" and subchapter "ACL basics" to understand this pattern matching
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002272 method.
2273
Thierry FOURNIER4dc71972017-01-28 08:33:08 +01002274 Note that :js:attr:`Map.beg` is also available for compatibility.
2275
2276.. js:attribute:: Map._sub
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002277
2278 See the HAProxy configuration.txt file, chapter "Using ACLs and fetching
Ilya Shipitsin2075ca82020-03-06 23:22:22 +05002279 samples" and subchapter "ACL basics" to understand this pattern matching
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002280 method.
2281
Thierry FOURNIER4dc71972017-01-28 08:33:08 +01002282 Note that :js:attr:`Map.sub` is also available for compatibility.
2283
2284.. js:attribute:: Map._dir
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002285
2286 See the HAProxy configuration.txt file, chapter "Using ACLs and fetching
Ilya Shipitsin2075ca82020-03-06 23:22:22 +05002287 samples" and subchapter "ACL basics" to understand this pattern matching
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002288 method.
2289
Thierry FOURNIER4dc71972017-01-28 08:33:08 +01002290 Note that :js:attr:`Map.dir` is also available for compatibility.
2291
2292.. js:attribute:: Map._dom
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002293
2294 See the HAProxy configuration.txt file, chapter "Using ACLs and fetching
Ilya Shipitsin2075ca82020-03-06 23:22:22 +05002295 samples" and subchapter "ACL basics" to understand this pattern matching
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002296 method.
2297
Thierry FOURNIER4dc71972017-01-28 08:33:08 +01002298 Note that :js:attr:`Map.dom` is also available for compatibility.
2299
2300.. js:attribute:: Map._end
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002301
2302 See the HAProxy configuration.txt file, chapter "Using ACLs and fetching
Ilya Shipitsin2075ca82020-03-06 23:22:22 +05002303 samples" and subchapter "ACL basics" to understand this pattern matching
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002304 method.
2305
Thierry FOURNIER4dc71972017-01-28 08:33:08 +01002306.. js:attribute:: Map._reg
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002307
2308 See the HAProxy configuration.txt file, chapter "Using ACLs and fetching
Ilya Shipitsin2075ca82020-03-06 23:22:22 +05002309 samples" and subchapter "ACL basics" to understand this pattern matching
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002310 method.
2311
Thierry FOURNIER4dc71972017-01-28 08:33:08 +01002312 Note that :js:attr:`Map.reg` is also available for compatibility.
2313
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002314
2315.. js:function:: Map.new(file, method)
2316
2317 Creates and load a map.
2318
2319 :param string file: Is the file containing the map.
2320 :param integer method: Is the map pattern matching method. See the attributes
2321 of the Map class.
2322 :returns: a class Map object.
Thierry FOURNIER4dc71972017-01-28 08:33:08 +01002323 :see: The Map attributes: :js:attr:`Map._int`, :js:attr:`Map._ip`,
2324 :js:attr:`Map._str`, :js:attr:`Map._beg`, :js:attr:`Map._sub`,
2325 :js:attr:`Map._dir`, :js:attr:`Map._dom`, :js:attr:`Map._end` and
2326 :js:attr:`Map._reg`.
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002327
2328.. js:function:: Map.lookup(map, str)
2329
2330 Perform a lookup in a map.
2331
2332 :param class_map map: Is the class Map object.
2333 :param string str: Is the string used as key.
2334 :returns: a string containing the result or nil if no match.
2335
2336.. js:function:: Map.slookup(map, str)
2337
2338 Perform a lookup in a map.
2339
2340 :param class_map map: Is the class Map object.
2341 :param string str: Is the string used as key.
2342 :returns: a string containing the result or empty string if no match.
2343
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002344.. _applethttp_class:
2345
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002346AppletHTTP class
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002347================
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002348
2349.. js:class:: AppletHTTP
2350
2351 This class is used with applets that requires the 'http' mode. The http applet
2352 can be registered with the *core.register_service()* function. They are used
2353 for processing an http request like a server in back of HAProxy.
2354
2355 This is an hello world sample code:
2356
2357.. code-block:: lua
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002358
Pieter Baauw4d7f7662015-11-08 16:38:08 +01002359 core.register_service("hello-world", "http", function(applet)
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002360 local response = "Hello World !"
2361 applet:set_status(200)
Pieter Baauw2dcb9bc2015-10-01 22:47:12 +02002362 applet:add_header("content-length", string.len(response))
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002363 applet:add_header("content-type", "text/plain")
Pieter Baauw2dcb9bc2015-10-01 22:47:12 +02002364 applet:start_response()
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002365 applet:send(response)
2366 end)
2367
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002368.. js:attribute:: AppletHTTP.c
2369
2370 :returns: A :ref:`converters_class`
2371
2372 This attribute contains a Converters class object.
2373
2374.. js:attribute:: AppletHTTP.sc
2375
2376 :returns: A :ref:`converters_class`
2377
2378 This attribute contains a Converters class object. The
2379 functions of this object returns always a string.
2380
2381.. js:attribute:: AppletHTTP.f
2382
2383 :returns: A :ref:`fetches_class`
2384
2385 This attribute contains a Fetches class object. Note that the
2386 applet execution place cannot access to a valid HAProxy core HTTP
Ilya Shipitsin2075ca82020-03-06 23:22:22 +05002387 transaction, so some sample fetches related to the HTTP dependent
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002388 values (hdr, path, ...) are not available.
2389
2390.. js:attribute:: AppletHTTP.sf
2391
2392 :returns: A :ref:`fetches_class`
2393
2394 This attribute contains a Fetches class object. The functions of
2395 this object returns always a string. Note that the applet
2396 execution place cannot access to a valid HAProxy core HTTP
Ilya Shipitsin2075ca82020-03-06 23:22:22 +05002397 transaction, so some sample fetches related to the HTTP dependent
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002398 values (hdr, path, ...) are not available.
2399
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01002400.. js:attribute:: AppletHTTP.method
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002401
2402 :returns: string
2403
2404 The attribute method returns a string containing the HTTP
2405 method.
2406
2407.. js:attribute:: AppletHTTP.version
2408
2409 :returns: string
2410
2411 The attribute version, returns a string containing the HTTP
2412 request version.
2413
2414.. js:attribute:: AppletHTTP.path
2415
2416 :returns: string
2417
2418 The attribute path returns a string containing the HTTP
2419 request path.
2420
2421.. js:attribute:: AppletHTTP.qs
2422
2423 :returns: string
2424
2425 The attribute qs returns a string containing the HTTP
2426 request query string.
2427
2428.. js:attribute:: AppletHTTP.length
2429
2430 :returns: integer
2431
2432 The attribute length returns an integer containing the HTTP
2433 body length.
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002434
Thierry FOURNIER841475e2015-12-11 17:10:09 +01002435.. js:attribute:: AppletHTTP.headers
2436
Patrick Hemmerc6a1d712018-05-01 21:30:41 -04002437 :returns: table
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002438
Patrick Hemmerc6a1d712018-05-01 21:30:41 -04002439 The attribute headers returns a table containing the HTTP
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002440 headers. The header names are always in lower case. As the header name can be
2441 encountered more than once in each request, the value is indexed with 0 as
Patrick Hemmerc6a1d712018-05-01 21:30:41 -04002442 first index value. The table have this form:
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002443
2444.. code-block:: lua
2445
2446 AppletHTTP.headers['<header-name>'][<header-index>] = "<header-value>"
2447
2448 AppletHTTP.headers["host"][0] = "www.test.com"
2449 AppletHTTP.headers["accept"][0] = "audio/basic q=1"
2450 AppletHTTP.headers["accept"][1] = "audio/*, q=0.2"
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01002451 AppletHTTP.headers["accept"][2] = "*/*, q=0.1"
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002452..
2453
Robin H. Johnson52f5db22017-01-01 13:10:52 -08002454.. js:function:: AppletHTTP.set_status(applet, code [, reason])
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002455
2456 This function sets the HTTP status code for the response. The allowed code are
2457 from 100 to 599.
2458
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01002459 :param class_AppletHTTP applet: An :ref:`applethttp_class`
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002460 :param integer code: the status code returned to the client.
Robin H. Johnson52f5db22017-01-01 13:10:52 -08002461 :param string reason: the status reason returned to the client (optional).
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002462
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01002463.. js:function:: AppletHTTP.add_header(applet, name, value)
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002464
2465 This function add an header in the response. Duplicated headers are not
2466 collapsed. The special header *content-length* is used to determinate the
2467 response length. If it not exists, a *transfer-encoding: chunked* is set, and
Ilya Shipitsin2075ca82020-03-06 23:22:22 +05002468 all the write from the function *AppletHTTP:send()* become a chunk.
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002469
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01002470 :param class_AppletHTTP applet: An :ref:`applethttp_class`
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002471 :param string name: the header name
2472 :param string value: the header value
2473
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01002474.. js:function:: AppletHTTP.start_response(applet)
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002475
2476 This function indicates to the HTTP engine that it can process and send the
2477 response headers. After this called we cannot add headers to the response; We
2478 cannot use the *AppletHTTP:send()* function if the
2479 *AppletHTTP:start_response()* is not called.
2480
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01002481 :param class_AppletHTTP applet: An :ref:`applethttp_class`
2482
2483.. js:function:: AppletHTTP.getline(applet)
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002484
2485 This function returns a string containing one line from the http body. If the
2486 data returned doesn't contains a final '\\n' its assumed than its the last
2487 available data before the end of stream.
2488
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01002489 :param class_AppletHTTP applet: An :ref:`applethttp_class`
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002490 :returns: a string. The string can be empty if we reach the end of the stream.
2491
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01002492.. js:function:: AppletHTTP.receive(applet, [size])
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002493
2494 Reads data from the HTTP body, according to the specified read *size*. If the
2495 *size* is missing, the function tries to read all the content of the stream
2496 until the end. If the *size* is bigger than the http body, it returns the
Bertrand Jacquin874a35c2018-09-10 21:26:07 +01002497 amount of data available.
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002498
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01002499 :param class_AppletHTTP applet: An :ref:`applethttp_class`
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002500 :param integer size: the required read size.
Ilya Shipitsin11057a32020-06-21 21:18:27 +05002501 :returns: always return a string,the string can be empty is the connection is
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002502 closed.
2503
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01002504.. js:function:: AppletHTTP.send(applet, msg)
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002505
2506 Send the message *msg* on the http request body.
2507
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01002508 :param class_AppletHTTP applet: An :ref:`applethttp_class`
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002509 :param string msg: the message to send.
2510
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01002511.. js:function:: AppletHTTP.get_priv(applet)
2512
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01002513 Return Lua data stored in the current transaction. If no data are stored,
2514 it returns a nil value.
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01002515
2516 :param class_AppletHTTP applet: An :ref:`applethttp_class`
Bertrand Jacquin874a35c2018-09-10 21:26:07 +01002517 :returns: the opaque data previously stored, or nil if nothing is
2518 available.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01002519 :see: :js:func:`AppletHTTP.set_priv`
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01002520
2521.. js:function:: AppletHTTP.set_priv(applet, data)
2522
2523 Store any data in the current HAProxy transaction. This action replace the
2524 old stored data.
2525
2526 :param class_AppletHTTP applet: An :ref:`applethttp_class`
2527 :param opaque data: The data which is stored in the transaction.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01002528 :see: :js:func:`AppletHTTP.get_priv`
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01002529
Tim Duesterhus4e172c92020-05-19 13:49:42 +02002530.. js:function:: AppletHTTP.set_var(applet, var, value[, ifexist])
Thierry FOURNIER / OZON.IOc1edafe2016-12-12 16:25:30 +01002531
2532 Converts a Lua type in a HAProxy type and store it in a variable <var>.
2533
2534 :param class_AppletHTTP applet: An :ref:`applethttp_class`
2535 :param string var: The variable name according with the HAProxy variable syntax.
2536 :param type value: The value associated to the variable. The type ca be string or
2537 integer.
Tim Duesterhus4e172c92020-05-19 13:49:42 +02002538 :param boolean ifexist: If this parameter is set to a truthy value the variable
2539 will only be set if it was defined elsewhere (i.e. used
2540 within the configuration). It is highly recommended to
2541 always set this to true.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01002542 :see: :js:func:`AppletHTTP.unset_var`
2543 :see: :js:func:`AppletHTTP.get_var`
Thierry FOURNIER / OZON.IOc1edafe2016-12-12 16:25:30 +01002544
2545.. js:function:: AppletHTTP.unset_var(applet, var)
2546
2547 Unset the variable <var>.
2548
2549 :param class_AppletHTTP applet: An :ref:`applethttp_class`
2550 :param string var: The variable name according with the HAProxy variable syntax.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01002551 :see: :js:func:`AppletHTTP.set_var`
2552 :see: :js:func:`AppletHTTP.get_var`
Thierry FOURNIER / OZON.IOc1edafe2016-12-12 16:25:30 +01002553
2554.. js:function:: AppletHTTP.get_var(applet, var)
2555
2556 Returns data stored in the variable <var> converter in Lua type.
2557
2558 :param class_AppletHTTP applet: An :ref:`applethttp_class`
2559 :param string var: The variable name according with the HAProxy variable syntax.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01002560 :see: :js:func:`AppletHTTP.set_var`
2561 :see: :js:func:`AppletHTTP.unset_var`
Thierry FOURNIER / OZON.IOc1edafe2016-12-12 16:25:30 +01002562
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002563.. _applettcp_class:
2564
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002565AppletTCP class
2566===============
2567
2568.. js:class:: AppletTCP
2569
2570 This class is used with applets that requires the 'tcp' mode. The tcp applet
2571 can be registered with the *core.register_service()* function. They are used
2572 for processing a tcp stream like a server in back of HAProxy.
2573
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002574.. js:attribute:: AppletTCP.c
2575
2576 :returns: A :ref:`converters_class`
2577
2578 This attribute contains a Converters class object.
2579
2580.. js:attribute:: AppletTCP.sc
2581
2582 :returns: A :ref:`converters_class`
2583
2584 This attribute contains a Converters class object. The
2585 functions of this object returns always a string.
2586
2587.. js:attribute:: AppletTCP.f
2588
2589 :returns: A :ref:`fetches_class`
2590
2591 This attribute contains a Fetches class object.
2592
2593.. js:attribute:: AppletTCP.sf
2594
2595 :returns: A :ref:`fetches_class`
2596
2597 This attribute contains a Fetches class object.
2598
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01002599.. js:function:: AppletTCP.getline(applet)
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002600
2601 This function returns a string containing one line from the stream. If the
2602 data returned doesn't contains a final '\\n' its assumed than its the last
2603 available data before the end of stream.
2604
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01002605 :param class_AppletTCP applet: An :ref:`applettcp_class`
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002606 :returns: a string. The string can be empty if we reach the end of the stream.
2607
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01002608.. js:function:: AppletTCP.receive(applet, [size])
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002609
2610 Reads data from the TCP stream, according to the specified read *size*. If the
2611 *size* is missing, the function tries to read all the content of the stream
2612 until the end.
2613
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01002614 :param class_AppletTCP applet: An :ref:`applettcp_class`
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002615 :param integer size: the required read size.
Ilya Shipitsin11057a32020-06-21 21:18:27 +05002616 :returns: always return a string,the string can be empty is the connection is
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002617 closed.
2618
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01002619.. js:function:: AppletTCP.send(appletmsg)
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002620
2621 Send the message on the stream.
2622
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01002623 :param class_AppletTCP applet: An :ref:`applettcp_class`
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002624 :param string msg: the message to send.
2625
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01002626.. js:function:: AppletTCP.get_priv(applet)
2627
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01002628 Return Lua data stored in the current transaction. If no data are stored,
2629 it returns a nil value.
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01002630
2631 :param class_AppletTCP applet: An :ref:`applettcp_class`
Bertrand Jacquin874a35c2018-09-10 21:26:07 +01002632 :returns: the opaque data previously stored, or nil if nothing is
2633 available.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01002634 :see: :js:func:`AppletTCP.set_priv`
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01002635
2636.. js:function:: AppletTCP.set_priv(applet, data)
2637
2638 Store any data in the current HAProxy transaction. This action replace the
2639 old stored data.
2640
2641 :param class_AppletTCP applet: An :ref:`applettcp_class`
2642 :param opaque data: The data which is stored in the transaction.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01002643 :see: :js:func:`AppletTCP.get_priv`
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01002644
Tim Duesterhus4e172c92020-05-19 13:49:42 +02002645.. js:function:: AppletTCP.set_var(applet, var, value[, ifexist])
Thierry FOURNIER / OZON.IOc1edafe2016-12-12 16:25:30 +01002646
2647 Converts a Lua type in a HAProxy type and stores it in a variable <var>.
2648
2649 :param class_AppletTCP applet: An :ref:`applettcp_class`
2650 :param string var: The variable name according with the HAProxy variable syntax.
2651 :param type value: The value associated to the variable. The type can be string or
2652 integer.
Tim Duesterhus4e172c92020-05-19 13:49:42 +02002653 :param boolean ifexist: If this parameter is set to a truthy value the variable
2654 will only be set if it was defined elsewhere (i.e. used
2655 within the configuration). It is highly recommended to
2656 always set this to true.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01002657 :see: :js:func:`AppletTCP.unset_var`
2658 :see: :js:func:`AppletTCP.get_var`
Thierry FOURNIER / OZON.IOc1edafe2016-12-12 16:25:30 +01002659
2660.. js:function:: AppletTCP.unset_var(applet, var)
2661
2662 Unsets the variable <var>.
2663
2664 :param class_AppletTCP applet: An :ref:`applettcp_class`
2665 :param string var: The variable name according with the HAProxy variable syntax.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01002666 :see: :js:func:`AppletTCP.unset_var`
2667 :see: :js:func:`AppletTCP.set_var`
Thierry FOURNIER / OZON.IOc1edafe2016-12-12 16:25:30 +01002668
2669.. js:function:: AppletTCP.get_var(applet, var)
2670
2671 Returns data stored in the variable <var> converter in Lua type.
2672
2673 :param class_AppletTCP applet: An :ref:`applettcp_class`
2674 :param string var: The variable name according with the HAProxy variable syntax.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01002675 :see: :js:func:`AppletTCP.unset_var`
2676 :see: :js:func:`AppletTCP.set_var`
Thierry FOURNIER / OZON.IOc1edafe2016-12-12 16:25:30 +01002677
Adis Nezirovic8878f8e2018-07-13 12:18:33 +02002678StickTable class
2679================
2680
2681.. js:class:: StickTable
2682
2683 **context**: task, action, sample-fetch
2684
2685 This class can be used to access the HAProxy stick tables from Lua.
2686
2687.. js:function:: StickTable.info()
2688
2689 Returns stick table attributes as a Lua table. See HAProxy documentation for
Ilya Shipitsin2272d8a2020-12-21 01:22:40 +05002690 "stick-table" for canonical info, or check out example below.
Adis Nezirovic8878f8e2018-07-13 12:18:33 +02002691
2692 :returns: Lua table
2693
2694 Assume our table has IPv4 key and gpc0 and conn_rate "columns":
2695
2696.. code-block:: lua
2697
2698 {
2699 expire=<int>, # Value in ms
2700 size=<int>, # Maximum table size
2701 used=<int>, # Actual number of entries in table
2702 data={ # Data columns, with types as key, and periods as values
2703 (-1 if type is not rate counter)
2704 conn_rate=<int>,
2705 gpc0=-1
2706 },
2707 length=<int>, # max string length for string table keys, key length
2708 # otherwise
2709 nopurge=<boolean>, # purge oldest entries when table is full
2710 type="ip" # can be "ip", "ipv6", "integer", "string", "binary"
2711 }
2712
2713.. js:function:: StickTable.lookup(key)
2714
2715 Returns stick table entry for given <key>
2716
2717 :param string key: Stick table key (IP addresses and strings are supported)
2718 :returns: Lua table
2719
2720.. js:function:: StickTable.dump([filter])
2721
2722 Returns all entries in stick table. An optional filter can be used
2723 to extract entries with specific data values. Filter is a table with valid
2724 comparison operators as keys followed by data type name and value pairs.
2725 Check out the HAProxy docs for "show table" for more details. For the
2726 reference, the supported operators are:
2727 "eq", "ne", "le", "lt", "ge", "gt"
2728
2729 For large tables, execution of this function can take a long time (for
2730 HAProxy standards). That's also true when filter is used, so take care and
2731 measure the impact.
2732
2733 :param table filter: Stick table filter
2734 :returns: Stick table entries (table)
2735
2736 See below for example filter, which contains 4 entries (or comparisons).
2737 (Maximum number of filter entries is 4, defined in the source code)
2738
2739.. code-block:: lua
2740
2741 local filter = {
2742 {"gpc0", "gt", 30}, {"gpc1", "gt", 20}}, {"conn_rate", "le", 10}
2743 }
2744
Christopher Faulet0f3c8902020-01-31 18:57:12 +01002745.. _action_class:
2746
2747Action class
2748=============
2749
2750.. js:class:: Act
2751
2752 **context**: action
2753
2754 This class contains all return codes an action may return. It is the lua
2755 equivalent to HAProxy "ACT_RET_*" code.
2756
2757.. code-block:: lua
2758
2759 core.register_action("deny", { "http-req" }, function (txn)
2760 return act.DENY
2761 end)
2762..
2763.. js:attribute:: act.CONTINUE
2764
2765 This attribute is an integer (0). It instructs HAProxy to continue the current
2766 ruleset processing on the message. It is the default return code for a lua
2767 action.
2768
2769 :returns: integer
2770
2771.. js:attribute:: act.STOP
2772
2773 This attribute is an integer (1). It instructs HAProxy to stop the current
2774 ruleset processing on the message.
2775
2776.. js:attribute:: act.YIELD
2777
2778 This attribute is an integer (2). It instructs HAProxy to temporarily pause
2779 the message processing. It will be resumed later on the same rule. The
2780 corresponding lua script is re-executed for the start.
2781
2782.. js:attribute:: act.ERROR
2783
2784 This attribute is an integer (3). It triggers an internal errors The message
2785 processing is stopped and the transaction is terminated. For HTTP streams, an
2786 HTTP 500 error is returned to the client.
2787
2788 :returns: integer
2789
2790.. js:attribute:: act.DONE
2791
2792 This attribute is an integer (4). It instructs HAProxy to stop the message
2793 processing.
2794
2795 :returns: integer
2796
2797.. js:attribute:: act.DENY
2798
2799 This attribute is an integer (5). It denies the current message. The message
2800 processing is stopped and the transaction is terminated. For HTTP streams, an
2801 HTTP 403 error is returned to the client if the deny is returned during the
2802 request analysis. During the response analysis, an HTTP 502 error is returned
2803 and the server response is discarded.
2804
2805 :returns: integer
2806
2807.. js:attribute:: act.ABORT
2808
2809 This attribute is an integer (6). It aborts the current message. The message
2810 processing is stopped and the transaction is terminated. For HTTP streams,
2811 HAproxy assumes a response was already sent to the client. From the Lua
2812 actions point of view, when this code is used, the transaction is terminated
2813 with no reply.
2814
2815 :returns: integer
2816
2817.. js:attribute:: act.INVALID
2818
2819 This attribute is an integer (7). It triggers an internal errors. The message
2820 processing is stopped and the transaction is terminated. For HTTP streams, an
2821 HTTP 400 error is returned to the client if the error is returned during the
2822 request analysis. During the response analysis, an HTTP 502 error is returned
2823 and the server response is discarded.
2824
2825 :returns: integer
Adis Nezirovic8878f8e2018-07-13 12:18:33 +02002826
Christopher Faulet2c2c2e32020-01-31 19:07:52 +01002827.. js:function:: act:wake_time(milliseconds)
2828
2829 **context**: action
2830
2831 Set the script pause timeout to the specified time, defined in
2832 milliseconds.
2833
2834 :param integer milliseconds: the required milliseconds.
2835
2836 This function may be used when a lua action returns `act.YIELD`, to force its
2837 wake-up at most after the specified number of milliseconds.
2838
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002839External Lua libraries
2840======================
2841
2842A lot of useful lua libraries can be found here:
2843
2844* `https://lua-toolbox.com/ <https://lua-toolbox.com/>`_
2845
Ilya Shipitsin2075ca82020-03-06 23:22:22 +05002846Redis client library:
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002847
2848* `https://github.com/nrk/redis-lua <https://github.com/nrk/redis-lua>`_
2849
2850This is an example about the usage of the Redis library with HAProxy. Note that
2851each call of any function of this library can throw an error if the socket
2852connection fails.
2853
2854.. code-block:: lua
2855
2856 -- load the redis library
2857 local redis = require("redis");
2858
2859 function do_something(txn)
2860
2861 -- create and connect new tcp socket
2862 local tcp = core.tcp();
2863 tcp:settimeout(1);
2864 tcp:connect("127.0.0.1", 6379);
2865
2866 -- use the redis library with this new socket
2867 local client = redis.connect({socket=tcp});
2868 client:ping();
2869
2870 end
2871
2872OpenSSL:
2873
2874* `http://mkottman.github.io/luacrypto/index.html
2875 <http://mkottman.github.io/luacrypto/index.html>`_
2876
2877* `https://github.com/brunoos/luasec/wiki
2878 <https://github.com/brunoos/luasec/wiki>`_