blob: 9b6d18f46262fc5c2f31e50c85fd8d8e073d099d [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 FOURNIERc798b5d2015-03-17 01:09:57 +0100204.. js:function:: core.log(loglevel, msg)
205
206 **context**: body, init, task, action, sample-fetch, converter
207
David Carlier61fdf8b2015-10-02 11:59:38 +0100208 This function sends a log. The log is sent, according with the HAProxy
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100209 configuration file, on the default syslog server if it is configured and on
210 the stderr if it is allowed.
211
212 :param integer loglevel: Is the log level asociated with the message. It is a
213 number between 0 and 7.
214 :param string msg: The log content.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +0100215 :see: :js:attr:`core.emerg`, :js:attr:`core.alert`, :js:attr:`core.crit`,
216 :js:attr:`core.err`, :js:attr:`core.warning`, :js:attr:`core.notice`,
217 :js:attr:`core.info`, :js:attr:`core.debug` (log level definitions)
218 :see: :js:func:`core.Debug`
219 :see: :js:func:`core.Info`
220 :see: :js:func:`core.Warning`
221 :see: :js:func:`core.Alert`
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100222
223.. js:function:: core.Debug(msg)
224
225 **context**: body, init, task, action, sample-fetch, converter
226
227 :param string msg: The log content.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +0100228 :see: :js:func:`core.log`
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100229
230 Does the same job than:
231
232.. code-block:: lua
233
Thierry FOURNIERdc595002015-12-21 11:13:52 +0100234 function Debug(msg)
235 core.log(core.debug, msg)
236 end
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100237..
238
239.. js:function:: core.Info(msg)
240
241 **context**: body, init, task, action, sample-fetch, converter
242
243 :param string msg: The log content.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +0100244 :see: :js:func:`core.log`
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100245
246.. code-block:: lua
247
Thierry FOURNIERdc595002015-12-21 11:13:52 +0100248 function Info(msg)
249 core.log(core.info, msg)
250 end
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100251..
252
253.. js:function:: core.Warning(msg)
254
255 **context**: body, init, task, action, sample-fetch, converter
256
257 :param string msg: The log content.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +0100258 :see: :js:func:`core.log`
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100259
260.. code-block:: lua
261
Thierry FOURNIERdc595002015-12-21 11:13:52 +0100262 function Warning(msg)
263 core.log(core.warning, msg)
264 end
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100265..
266
267.. js:function:: core.Alert(msg)
268
269 **context**: body, init, task, action, sample-fetch, converter
270
271 :param string msg: The log content.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +0100272 :see: :js:func:`core.log`
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100273
274.. code-block:: lua
275
Thierry FOURNIERdc595002015-12-21 11:13:52 +0100276 function Alert(msg)
277 core.log(core.alert, msg)
278 end
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100279..
280
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100281.. js:function:: core.add_acl(filename, key)
282
283 **context**: init, task, action, sample-fetch, converter
284
285 Add the ACL *key* in the ACLs list referenced by the file *filename*.
286
287 :param string filename: the filename that reference the ACL entries.
288 :param string key: the key which will be added.
289
290.. js:function:: core.del_acl(filename, key)
291
292 **context**: init, task, action, sample-fetch, converter
293
294 Delete the ACL entry referenced by the key *key* in the list of ACLs
295 referenced by *filename*.
296
297 :param string filename: the filename that reference the ACL entries.
298 :param string key: the key which will be deleted.
299
300.. js:function:: core.del_map(filename, key)
301
302 **context**: init, task, action, sample-fetch, converter
303
304 Delete the map entry indexed with the specified key in the list of maps
305 referenced by his filename.
306
307 :param string filename: the filename that reference the map entries.
308 :param string key: the key which will be deleted.
309
Thierry Fourniereea77c02016-03-18 08:47:13 +0100310.. js:function:: core.get_info()
311
312 **context**: body, init, task, action, sample-fetch, converter
313
314 Returns HAProxy core informations. We can found information like the uptime,
315 the pid, memory pool usage, tasks number, ...
316
317 These information are also returned by the management sockat via the command
318 "show info". See the management socket documentation fpor more information
319 about the content of these variables.
320
321 :returns: an array of values.
322
Thierry Fournierb1f46562016-01-21 09:46:15 +0100323.. js:function:: core.now()
324
325 **context**: body, init, task, action
326
327 This function returns the current time. The time returned is fixed by the
328 HAProxy core and assures than the hour will be monotnic and that the system
329 call 'gettimeofday' will not be called too. The time is refreshed between each
330 Lua execution or resume, so two consecutive call to the function "now" will
331 probably returns the same result.
332
Patrick Hemmerc6a1d712018-05-01 21:30:41 -0400333 :returns: a table which contains two entries "sec" and "usec". "sec"
Thierry Fournierb1f46562016-01-21 09:46:15 +0100334 contains the current at the epoch format, and "usec" contains the
335 current microseconds.
336
Thierry FOURNIERa78f0372016-12-14 19:04:41 +0100337.. js:function:: core.http_date(date)
338
339 **context**: body, init, task, action
340
341 This function take a string repsenting http date, and returns an integer
342 containing the corresponding date with a epoch format. A valid http date
343 me respect the format IMF, RFC850 or ASCTIME.
344
345 :param string date: a date http-date formatted
346 :returns: integer containing epoch date
347 :see: :js:func:`core.imf_date`.
348 :see: :js:func:`core.rfc850_date`.
349 :see: :js:func:`core.asctime_date`.
350 :see: https://tools.ietf.org/html/rfc7231#section-7.1.1.1
351
352.. js:function:: core.imf_date(date)
353
354 **context**: body, init, task, action
355
356 This function take a string repsenting IMF date, and returns an integer
357 containing the corresponding date with a epoch format.
358
359 :param string date: a date IMF formatted
360 :returns: integer containing epoch date
361 :see: https://tools.ietf.org/html/rfc7231#section-7.1.1.1
362
363 The IMF format is like this:
364
365.. code-block:: text
366
367 Sun, 06 Nov 1994 08:49:37 GMT
368..
369
370.. js:function:: core.rfc850_date(date)
371
372 **context**: body, init, task, action
373
374 This function take a string repsenting RFC850 date, and returns an integer
375 containing the corresponding date with a epoch format.
376
377 :param string date: a date RFC859 formatted
378 :returns: integer containing epoch date
379 :see: https://tools.ietf.org/html/rfc7231#section-7.1.1.1
380
381 The RFC850 format is like this:
382
383.. code-block:: text
384
385 Sunday, 06-Nov-94 08:49:37 GMT
386..
387
388.. js:function:: core.asctime_date(date)
389
390 **context**: body, init, task, action
391
392 This function take a string repsenting ASCTIME date, and returns an integer
393 containing the corresponding date with a epoch format.
394
395 :param string date: a date ASCTIME formatted
396 :returns: integer containing epoch date
397 :see: https://tools.ietf.org/html/rfc7231#section-7.1.1.1
398
399 The ASCTIME format is like this:
400
401.. code-block:: text
402
403 Sun Nov 6 08:49:37 1994
404..
405
406.. js:function:: core.rfc850_date(date)
407
408 **context**: body, init, task, action
409
410 This function take a string repsenting http date, and returns an integer
411 containing the corresponding date with a epoch format.
412
413 :param string date: a date http-date formatted
414
415.. js:function:: core.asctime_date(date)
416
417 **context**: body, init, task, action
418
419 This function take a string repsenting http date, and returns an integer
420 containing the corresponding date with a epoch format.
421
422 :param string date: a date http-date formatted
423
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100424.. js:function:: core.msleep(milliseconds)
425
426 **context**: body, init, task, action
427
428 The `core.msleep()` stops the Lua execution between specified milliseconds.
429
430 :param integer milliseconds: the required milliseconds.
431
Thierry Fournierf61aa632016-02-19 20:56:00 +0100432.. js:attribute:: core.proxies
433
434 **context**: body, init, task, action, sample-fetch, converter
435
Patrick Hemmerc6a1d712018-05-01 21:30:41 -0400436 proxies is a table containing the list of all proxies declared in the
437 configuration file. The table is indexed by the proxy name, and each entry
438 of the proxies table is an object of type :ref:`proxy_class`.
439
440 Warning, if you have declared a frontend and backend with the same name, only
441 one of these are listed.
Thierry Fournierf61aa632016-02-19 20:56:00 +0100442
Thierry FOURNIERc5d11c62018-02-12 14:46:54 +0100443.. js:function:: core.register_action(name, actions, func [, nb_args])
Thierry FOURNIER8255a752015-09-23 21:03:35 +0200444
445 **context**: body
446
David Carlier61fdf8b2015-10-02 11:59:38 +0100447 Register a Lua function executed as action. All the registered action can be
Thierry FOURNIER8255a752015-09-23 21:03:35 +0200448 used in HAProxy with the prefix "lua.". An action gets a TXN object class as
449 input.
450
451 :param string name: is the name of the converter.
Willy Tarreau61add3c2015-09-28 15:39:10 +0200452 :param table actions: is a table of string describing the HAProxy actions who
Thierry FOURNIER8255a752015-09-23 21:03:35 +0200453 want to register to. The expected actions are 'tcp-req',
454 'tcp-res', 'http-req' or 'http-res'.
Thierry FOURNIERc5d11c62018-02-12 14:46:54 +0100455 :param integer nb_args: is the expected number of argument for the action.
456 By default the value is 0.
Thierry FOURNIER8255a752015-09-23 21:03:35 +0200457 :param function func: is the Lua function called to work as converter.
458
459 The prototype of the Lua function used as argument is:
460
461.. code-block:: lua
462
Thierry FOURNIERc5d11c62018-02-12 14:46:54 +0100463 function(txn [, arg1 [, arg2]])
Thierry FOURNIER8255a752015-09-23 21:03:35 +0200464..
465
Thierry FOURNIERdc595002015-12-21 11:13:52 +0100466 * **txn** (:ref:`txn_class`): this is a TXN object used for manipulating the
Thierry FOURNIER8255a752015-09-23 21:03:35 +0200467 current request or TCP stream.
468
Thierry FOURNIERc5d11c62018-02-12 14:46:54 +0100469 * **argX**: this is argument provided throught the HAProxy configuration file.
470
Willy Tarreau61add3c2015-09-28 15:39:10 +0200471 Here, an exemple of action registration. the action juste send an 'Hello world'
Thierry FOURNIER8255a752015-09-23 21:03:35 +0200472 in the logs.
473
474.. code-block:: lua
475
476 core.register_action("hello-world", { "tcp-req", "http-req" }, function(txn)
477 txn:Info("Hello world")
478 end)
479..
480
481 This example code is used in HAproxy configuration like this:
482
483::
484
485 frontend tcp_frt
486 mode tcp
487 tcp-request content lua.hello-world
488
489 frontend http_frt
490 mode http
491 http-request lua.hello-world
Thierry FOURNIERc5d11c62018-02-12 14:46:54 +0100492..
493
494 A second example using aruments
495
496.. code-block:: lua
497
498 function hello_world(txn, arg)
499 txn:Info("Hello world for " .. arg)
500 end
501 core.register_action("hello-world", { "tcp-req", "http-req" }, hello_world, 2)
502..
Thierry FOURNIER8255a752015-09-23 21:03:35 +0200503
Thierry FOURNIERc5d11c62018-02-12 14:46:54 +0100504 This example code is used in HAproxy configuration like this:
505
506::
507
508 frontend tcp_frt
509 mode tcp
510 tcp-request content lua.hello-world everybody
511..
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100512.. js:function:: core.register_converters(name, func)
513
514 **context**: body
515
David Carlier61fdf8b2015-10-02 11:59:38 +0100516 Register a Lua function executed as converter. All the registered converters
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100517 can be used in HAProxy with the prefix "lua.". An converter get a string as
518 input and return a string as output. The registered function can take up to 9
519 values as parameter. All the value are strings.
520
521 :param string name: is the name of the converter.
522 :param function func: is the Lua function called to work as converter.
523
524 The prototype of the Lua function used as argument is:
525
526.. code-block:: lua
527
528 function(str, [p1 [, p2 [, ... [, p5]]]])
529..
530
531 * **str** (*string*): this is the input value automatically converted in
532 string.
533 * **p1** .. **p5** (*string*): this is a list of string arguments declared in
534 the haroxy configuration file. The number of arguments doesn't exceed 5.
535 The order and the nature of these is conventionally choose by the
536 developper.
537
538.. js:function:: core.register_fetches(name, func)
539
540 **context**: body
541
David Carlier61fdf8b2015-10-02 11:59:38 +0100542 Register a Lua function executed as sample fetch. All the registered sample
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100543 fetchs can be used in HAProxy with the prefix "lua.". A Lua sample fetch
544 return a string as output. The registered function can take up to 9 values as
545 parameter. All the value are strings.
546
547 :param string name: is the name of the converter.
548 :param function func: is the Lua function called to work as sample fetch.
549
550 The prototype of the Lua function used as argument is:
551
552.. code-block:: lua
553
554 string function(txn, [p1 [, p2 [, ... [, p5]]]])
555..
556
Thierry FOURNIERdc595002015-12-21 11:13:52 +0100557 * **txn** (:ref:`txn_class`): this is the txn object associated with the current
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100558 request.
559 * **p1** .. **p5** (*string*): this is a list of string arguments declared in
560 the haroxy configuration file. The number of arguments doesn't exceed 5.
561 The order and the nature of these is conventionally choose by the
562 developper.
563 * **Returns**: A string containing some data, ot nil if the value cannot be
564 returned now.
565
566 lua example code:
567
568.. code-block:: lua
569
570 core.register_fetches("hello", function(txn)
571 return "hello"
572 end)
573..
574
575 HAProxy example configuration:
576
577::
578
579 frontend example
580 http-request redirect location /%[lua.hello]
581
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +0200582.. js:function:: core.register_service(name, mode, func)
583
584 **context**: body
585
David Carlier61fdf8b2015-10-02 11:59:38 +0100586 Register a Lua function executed as a service. All the registered service can
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +0200587 be used in HAProxy with the prefix "lua.". A service gets an object class as
588 input according with the required mode.
589
590 :param string name: is the name of the converter.
Willy Tarreau61add3c2015-09-28 15:39:10 +0200591 :param string mode: is string describing the required mode. Only 'tcp' or
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +0200592 'http' are allowed.
593 :param function func: is the Lua function called to work as converter.
594
595 The prototype of the Lua function used as argument is:
596
597.. code-block:: lua
598
599 function(applet)
600..
601
Thierry FOURNIERdc595002015-12-21 11:13:52 +0100602 * **applet** *applet* will be a :ref:`applettcp_class` or a
603 :ref:`applethttp_class`. It depends the type of registered applet. An applet
604 registered with the 'http' value for the *mode* parameter will gets a
605 :ref:`applethttp_class`. If the *mode* value is 'tcp', the applet will gets
606 a :ref:`applettcp_class`.
607
608 **warning**: Applets of type 'http' cannot be called from 'tcp-*'
609 rulesets. Only the 'http-*' rulesets are authorized, this means
610 that is not possible to call an HTTP applet from a proxy in tcp
611 mode. Applets of type 'tcp' can be called from anywhre.
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +0200612
Willy Tarreau61add3c2015-09-28 15:39:10 +0200613 Here, an exemple of service registration. the service just send an 'Hello world'
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +0200614 as an http response.
615
616.. code-block:: lua
617
Pieter Baauw4d7f7662015-11-08 16:38:08 +0100618 core.register_service("hello-world", "http", function(applet)
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +0200619 local response = "Hello World !"
620 applet:set_status(200)
Pieter Baauw2dcb9bc2015-10-01 22:47:12 +0200621 applet:add_header("content-length", string.len(response))
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +0200622 applet:add_header("content-type", "text/plain")
Pieter Baauw2dcb9bc2015-10-01 22:47:12 +0200623 applet:start_response()
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +0200624 applet:send(response)
625 end)
626..
627
628 This example code is used in HAproxy configuration like this:
629
630::
631
632 frontend example
633 http-request use-service lua.hello-world
634
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100635.. js:function:: core.register_init(func)
636
637 **context**: body
638
639 Register a function executed after the configuration parsing. This is useful
640 to check any parameters.
641
Pieter Baauw4d7f7662015-11-08 16:38:08 +0100642 :param function func: is the Lua function called to work as initializer.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100643
644 The prototype of the Lua function used as argument is:
645
646.. code-block:: lua
647
648 function()
649..
650
651 It takes no input, and no output is expected.
652
653.. js:function:: core.register_task(func)
654
655 **context**: body, init, task, action, sample-fetch, converter
656
657 Register and start independent task. The task is started when the HAProxy
658 main scheduler starts. For example this type of tasks can be executed to
Thierry FOURNIER2e4893c2015-03-18 13:37:27 +0100659 perform complex health checks.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100660
Pieter Baauw4d7f7662015-11-08 16:38:08 +0100661 :param function func: is the Lua function called to work as initializer.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100662
663 The prototype of the Lua function used as argument is:
664
665.. code-block:: lua
666
667 function()
668..
669
670 It takes no input, and no output is expected.
671
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +0100672.. js:function:: core.register_cli([path], usage, func)
673
674 **context**: body
675
676 Register and start independent task. The task is started when the HAProxy
677 main scheduler starts. For example this type of tasks can be executed to
678 perform complex health checks.
679
680 :param array path: is the sequence of word for which the cli execute the Lua
681 binding.
682 :param string usage: is the usage message displayed in the help.
683 :param function func: is the Lua function called to handle the CLI commands.
684
685 The prototype of the Lua function used as argument is:
686
687.. code-block:: lua
688
689 function(AppletTCP, [arg1, [arg2, [...]]])
690..
691
692 I/O are managed with the :ref:`applettcp_class` object. Args are given as
693 paramter. The args embbed the registred path. If the path is declared like
694 this:
695
696.. code-block:: lua
697
698 core.register_cli({"show", "ssl", "stats"}, "Display SSL stats..", function(applet, arg1, arg2, arg3, arg4, arg5)
699 end)
700..
701
702 And we execute this in the prompt:
703
704.. code-block:: text
705
706 > prompt
707 > show ssl stats all
708..
709
710 Then, arg1, arg2 and arg3 will contains respectivey "show", "ssl" and "stats".
711 arg4 will contain "all". arg5 contains nil.
712
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100713.. js:function:: core.set_nice(nice)
714
715 **context**: task, action, sample-fetch, converter
716
717 Change the nice of the current task or current session.
Thierry FOURNIER2e4893c2015-03-18 13:37:27 +0100718
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100719 :param integer nice: the nice value, it must be between -1024 and 1024.
720
721.. js:function:: core.set_map(filename, key, value)
722
723 **context**: init, task, action, sample-fetch, converter
724
725 set the value *value* associated to the key *key* in the map referenced by
726 *filename*.
727
Thierry FOURNIERdc595002015-12-21 11:13:52 +0100728 :param string filename: the Map reference
729 :param string key: the key to set or replace
730 :param string value: the associated value
731
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100732.. js:function:: core.sleep(int seconds)
733
734 **context**: body, init, task, action
735
736 The `core.sleep()` functions stop the Lua execution between specified seconds.
737
738 :param integer seconds: the required seconds.
739
740.. js:function:: core.tcp()
741
742 **context**: init, task, action
743
744 This function returns a new object of a *socket* class.
745
Thierry FOURNIERdc595002015-12-21 11:13:52 +0100746 :returns: A :ref:`socket_class` object.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100747
Thierry Fournier1de16592016-01-27 09:49:07 +0100748.. js:function:: core.concat()
749
750 **context**: body, init, task, action, sample-fetch, converter
751
752 This function retruns a new concat object.
753
754 :returns: A :ref:`concat_class` object.
755
Thierry FOURNIER0a99b892015-08-26 00:14:17 +0200756.. js:function:: core.done(data)
757
758 **context**: body, init, task, action, sample-fetch, converter
759
760 :param any data: Return some data for the caller. It is useful with
761 sample-fetches and sample-converters.
762
763 Immediately stops the current Lua execution and returns to the caller which
764 may be a sample fetch, a converter or an action and returns the specified
765 value (ignored for actions). It is used when the LUA process finishes its
766 work and wants to give back the control to HAProxy without executing the
767 remaining code. It can be seen as a multi-level "return".
768
Thierry FOURNIER486f5a02015-03-16 15:13:03 +0100769.. js:function:: core.yield()
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100770
771 **context**: task, action, sample-fetch, converter
772
773 Give back the hand at the HAProxy scheduler. It is used when the LUA
774 processing consumes a lot of processing time.
775
Thierry FOURNIER / OZON.IO62fec752016-11-10 20:38:11 +0100776.. js:function:: core.parse_addr(address)
777
778 **context**: body, init, task, action, sample-fetch, converter
779
780 :param network: is a string describing an ipv4 or ipv6 address and optionally
781 its network length, like this: "127.0.0.1/8" or "aaaa::1234/32".
782 :returns: a userdata containing network or nil if an error occurs.
783
784 Parse ipv4 or ipv6 adresses and its facultative associated network.
785
786.. js:function:: core.match_addr(addr1, addr2)
787
788 **context**: body, init, task, action, sample-fetch, converter
789
790 :param addr1: is an address created with "core.parse_addr".
791 :param addr2: is an address created with "core.parse_addr".
792 :returns: boolean, true if the network of the addresses matche, else returns
793 false.
794
795 Match two networks. For example "127.0.0.1/32" matchs "127.0.0.0/8". The order
796 of network is not important.
797
Thierry FOURNIER / OZON.IO8a1027a2016-11-24 20:48:38 +0100798.. js:function:: core.tokenize(str, separators [, noblank])
799
800 **context**: body, init, task, action, sample-fetch, converter
801
802 This function is useful for tokenizing an entry, or splitting some messages.
803 :param string str: The string which will be split.
804 :param string separators: A string containing a list of separators.
805 :param boolean noblank: Ignore empty entries.
806 :returns: an array of string.
807
808 For example:
809
810.. code-block:: lua
811
812 local array = core.tokenize("This function is useful, for tokenizing an entry.", "., ", true)
813 print_r(array)
814..
815
816 Returns this array:
817
818.. code-block:: text
819
820 (table) table: 0x21c01e0 [
821 1: (string) "This"
822 2: (string) "function"
823 3: (string) "is"
824 4: (string) "useful"
825 5: (string) "for"
826 6: (string) "tokenizing"
827 7: (string) "an"
828 8: (string) "entry"
829 ]
830..
831
Thierry Fournierf61aa632016-02-19 20:56:00 +0100832.. _proxy_class:
833
834Proxy class
835============
836
837.. js:class:: Proxy
838
839 This class provides a way for manipulating proxy and retrieving information
840 like statistics.
841
Thierry FOURNIER817e7592017-07-24 14:35:04 +0200842.. js:attribute:: Proxy.name
843
844 Contain the name of the proxy.
845
Baptiste Assmann46c72552017-10-26 21:51:58 +0200846.. js:attribute:: Proxy.uuid
847
848 Contain the unique identifier of the proxy.
849
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +0100850.. js:attribute:: Proxy.servers
851
Patrick Hemmerc6a1d712018-05-01 21:30:41 -0400852 Contain a table with the attached servers. The table is indexed by server
853 name, and each server entry is an object of type :ref:`server_class`.
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +0100854
Thierry Fournierff480422016-02-25 08:36:46 +0100855.. js:attribute:: Proxy.listeners
856
Patrick Hemmerc6a1d712018-05-01 21:30:41 -0400857 Contain a table with the attached listeners. The table is indexed by listener
858 name, and each each listeners entry is an object of type
859 :ref:`listener_class`.
Thierry Fournierff480422016-02-25 08:36:46 +0100860
Thierry Fournierf61aa632016-02-19 20:56:00 +0100861.. js:function:: Proxy.pause(px)
862
863 Pause the proxy. See the management socket documentation for more information.
864
865 :param class_proxy px: A :ref:`proxy_class` which indicates the manipulated
866 proxy.
867
868.. js:function:: Proxy.resume(px)
869
870 Resume the proxy. See the management socket documentation for more
871 information.
872
873 :param class_proxy px: A :ref:`proxy_class` which indicates the manipulated
874 proxy.
875
876.. js:function:: Proxy.stop(px)
877
878 Stop the proxy. See the management socket documentation for more information.
879
880 :param class_proxy px: A :ref:`proxy_class` which indicates the manipulated
881 proxy.
882
883.. js:function:: Proxy.shut_bcksess(px)
884
885 Kill the session attached to a backup server. See the management socket
886 documentation for more information.
887
888 :param class_proxy px: A :ref:`proxy_class` which indicates the manipulated
889 proxy.
890
891.. js:function:: Proxy.get_cap(px)
892
893 Returns a string describing the capabilities of the proxy.
894
895 :param class_proxy px: A :ref:`proxy_class` which indicates the manipulated
896 proxy.
897 :returns: a string "frontend", "backend", "proxy" or "ruleset".
898
899.. js:function:: Proxy.get_mode(px)
900
901 Returns a string describing the mode of the current proxy.
902
903 :param class_proxy px: A :ref:`proxy_class` which indicates the manipulated
904 proxy.
905 :returns: a string "tcp", "http", "health" or "unknown"
906
907.. js:function:: Proxy.get_stats(px)
908
Patrick Hemmerc6a1d712018-05-01 21:30:41 -0400909 Returns a table containg the proxy statistics. The statistics returned are
Thierry Fournierf61aa632016-02-19 20:56:00 +0100910 not the same if the proxy is frontend or a backend.
911
912 :param class_proxy px: A :ref:`proxy_class` which indicates the manipulated
913 proxy.
Patrick Hemmerc6a1d712018-05-01 21:30:41 -0400914 :returns: a key/value table containing stats
Thierry Fournierf61aa632016-02-19 20:56:00 +0100915
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +0100916.. _server_class:
917
918Server class
919============
920
Patrick Hemmerc6a1d712018-05-01 21:30:41 -0400921.. js:class:: Server
922
923 This class provides a way for manipulating servers and retrieving information.
924
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +0100925.. js:function:: Server.is_draining(sv)
926
Patrick Hemmerc6a1d712018-05-01 21:30:41 -0400927 Return true if the server is currently draining sticky connections.
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +0100928
929 :param class_server sv: A :ref:`server_class` which indicates the manipulated
930 server.
931 :returns: a boolean
932
933.. js:function:: Server.set_weight(sv, weight)
934
Patrick Hemmerc6a1d712018-05-01 21:30:41 -0400935 Dynamically change the weight of the server. See the management socket
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +0100936 documentation for more information about the format of the string.
937
938 :param class_server sv: A :ref:`server_class` which indicates the manipulated
939 server.
940 :param string weight: A string describing the server weight.
941
942.. js:function:: Server.get_weight(sv)
943
Patrick Hemmerc6a1d712018-05-01 21:30:41 -0400944 This function returns an integer representing the server weight.
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +0100945
946 :param class_server sv: A :ref:`server_class` which indicates the manipulated
947 server.
948 :returns: an integer.
949
950.. js:function:: Server.set_addr(sv, addr)
951
Patrick Hemmerc6a1d712018-05-01 21:30:41 -0400952 Dynamically change the address of the server. See the management socket
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +0100953 documentation for more information about the format of the string.
954
955 :param class_server sv: A :ref:`server_class` which indicates the manipulated
956 server.
Patrick Hemmerc6a1d712018-05-01 21:30:41 -0400957 :param string addr: A string describing the server address.
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +0100958
959.. js:function:: Server.get_addr(sv)
960
Patrick Hemmerc6a1d712018-05-01 21:30:41 -0400961 Returns a string describing the address of the server.
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +0100962
963 :param class_server sv: A :ref:`server_class` which indicates the manipulated
964 server.
965 :returns: A string
966
967.. js:function:: Server.get_stats(sv)
968
969 Returns server statistics.
970
971 :param class_server sv: A :ref:`server_class` which indicates the manipulated
972 server.
Patrick Hemmerc6a1d712018-05-01 21:30:41 -0400973 :returns: a key/value table containing stats
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +0100974
975.. js:function:: Server.shut_sess(sv)
976
977 Shutdown all the sessions attached to the server. See the management socket
978 documentation for more information about this function.
979
980 :param class_server sv: A :ref:`server_class` which indicates the manipulated
981 server.
982
983.. js:function:: Server.set_drain(sv)
984
985 Drain sticky sessions. See the management socket documentation for more
986 information about this function.
987
988 :param class_server sv: A :ref:`server_class` which indicates the manipulated
989 server.
990
991.. js:function:: Server.set_maint(sv)
992
993 Set maintenance mode. See the management socket documentation for more
994 information about this function.
995
996 :param class_server sv: A :ref:`server_class` which indicates the manipulated
997 server.
998
999.. js:function:: Server.set_ready(sv)
1000
1001 Set normal mode. See the management socket documentation for more information
1002 about this function.
1003
1004 :param class_server sv: A :ref:`server_class` which indicates the manipulated
1005 server.
1006
1007.. js:function:: Server.check_enable(sv)
1008
1009 Enable health checks. See the management socket documentation for more
1010 information about this function.
1011
1012 :param class_server sv: A :ref:`server_class` which indicates the manipulated
1013 server.
1014
1015.. js:function:: Server.check_disable(sv)
1016
1017 Disable health checks. See the management socket documentation for more
1018 information about this function.
1019
1020 :param class_server sv: A :ref:`server_class` which indicates the manipulated
1021 server.
1022
1023.. js:function:: Server.check_force_up(sv)
1024
1025 Force health-check up. See the management socket documentation for more
1026 information about this function.
1027
1028 :param class_server sv: A :ref:`server_class` which indicates the manipulated
1029 server.
1030
1031.. js:function:: Server.check_force_nolb(sv)
1032
1033 Force health-check nolb mode. See the management socket documentation for more
1034 information about this function.
1035
1036 :param class_server sv: A :ref:`server_class` which indicates the manipulated
1037 server.
1038
1039.. js:function:: Server.check_force_down(sv)
1040
1041 Force health-check down. See the management socket documentation for more
1042 information about this function.
1043
1044 :param class_server sv: A :ref:`server_class` which indicates the manipulated
1045 server.
1046
1047.. js:function:: Server.agent_enable(sv)
1048
1049 Enable agent check. See the management socket documentation for more
1050 information about this function.
1051
1052 :param class_server sv: A :ref:`server_class` which indicates the manipulated
1053 server.
1054
1055.. js:function:: Server.agent_disable(sv)
1056
1057 Disable agent check. See the management socket documentation for more
1058 information about this function.
1059
1060 :param class_server sv: A :ref:`server_class` which indicates the manipulated
1061 server.
1062
1063.. js:function:: Server.agent_force_up(sv)
1064
1065 Force agent check up. See the management socket documentation for more
1066 information about this function.
1067
1068 :param class_server sv: A :ref:`server_class` which indicates the manipulated
1069 server.
1070
1071.. js:function:: Server.agent_force_down(sv)
1072
1073 Force agent check down. See the management socket documentation for more
1074 information about this function.
1075
1076 :param class_server sv: A :ref:`server_class` which indicates the manipulated
1077 server.
1078
Thierry Fournierff480422016-02-25 08:36:46 +01001079.. _listener_class:
1080
1081Listener class
1082==============
1083
1084.. js:function:: Listener.get_stats(ls)
1085
1086 Returns server statistics.
1087
1088 :param class_listener ls: A :ref:`listener_class` which indicates the
1089 manipulated listener.
Patrick Hemmerc6a1d712018-05-01 21:30:41 -04001090 :returns: a key/value table containing stats
Thierry Fournierff480422016-02-25 08:36:46 +01001091
Thierry Fournier1de16592016-01-27 09:49:07 +01001092.. _concat_class:
1093
1094Concat class
1095============
1096
1097.. js:class:: Concat
1098
1099 This class provides a fast way for string concatenation. The way using native
1100 Lua concatenation like the code below is slow for some reasons.
1101
1102.. code-block:: lua
1103
1104 str = "string1"
1105 str = str .. ", string2"
1106 str = str .. ", string3"
1107..
1108
1109 For each concatenation, Lua:
1110 * allocate memory for the result,
1111 * catenate the two string copying the strings in the new memory bloc,
1112 * free the old memory block containing the string whoch is no longer used.
1113 This process does many memory move, allocation and free. In addition, the
1114 memory is not really freed, it is just mark mark as unsused and wait for the
1115 garbage collector.
1116
1117 The Concat class provide an alternative way for catenating strings. It uses
1118 the internal Lua mechanism (it does not allocate memory), but it doesn't copy
1119 the data more than once.
1120
1121 On my computer, the following loops spends 0.2s for the Concat method and
1122 18.5s for the pure Lua implementation. So, the Concat class is about 1000x
1123 faster than the embedded solution.
1124
1125.. code-block:: lua
1126
1127 for j = 1, 100 do
1128 c = core.concat()
1129 for i = 1, 20000 do
1130 c:add("#####")
1131 end
1132 end
1133..
1134
1135.. code-block:: lua
1136
1137 for j = 1, 100 do
1138 c = ""
1139 for i = 1, 20000 do
1140 c = c .. "#####"
1141 end
1142 end
1143..
1144
1145.. js:function:: Concat.add(concat, string)
1146
1147 This function adds a string to the current concatenated string.
1148
1149 :param class_concat concat: A :ref:`concat_class` which contains the currently
1150 builded string.
1151 :param string string: A new string to concatenate to the current builded
1152 string.
1153
1154.. js:function:: Concat.dump(concat)
1155
1156 This function returns the concanated string.
1157
1158 :param class_concat concat: A :ref:`concat_class` which contains the currently
1159 builded string.
1160 :returns: the concatenated string
1161
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001162.. _fetches_class:
1163
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001164Fetches class
1165=============
1166
1167.. js:class:: Fetches
1168
1169 This class contains a lot of internal HAProxy sample fetches. See the
Thierry FOURNIER2e4893c2015-03-18 13:37:27 +01001170 HAProxy "configuration.txt" documentation for more information about her
1171 usage. they are the chapters 7.3.2 to 7.3.6.
1172
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001173 **warning** some sample fetches are not available in some context. These
Patrick Hemmerc6a1d712018-05-01 21:30:41 -04001174 limitations are specified in this documentation when they're useful.
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001175
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01001176 :see: :js:attr:`TXN.f`
1177 :see: :js:attr:`TXN.sf`
Thierry FOURNIER2e4893c2015-03-18 13:37:27 +01001178
1179 Fetches are useful for:
1180
1181 * get system time,
1182 * get environment variable,
1183 * get random numbers,
1184 * known backend status like the number of users in queue or the number of
1185 connections established,
1186 * client information like ip source or destination,
1187 * deal with stick tables,
1188 * Established SSL informations,
1189 * HTTP information like headers or method.
1190
1191.. code-block:: lua
1192
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001193 function action(txn)
1194 -- Get source IP
1195 local clientip = txn.f:src()
1196 end
Thierry FOURNIER2e4893c2015-03-18 13:37:27 +01001197..
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001198
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001199.. _converters_class:
1200
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001201Converters class
1202================
1203
1204.. js:class:: Converters
1205
1206 This class contains a lot of internal HAProxy sample converters. See the
Thierry FOURNIER2e4893c2015-03-18 13:37:27 +01001207 HAProxy documentation "configuration.txt" for more information about her
1208 usage. Its the chapter 7.3.1.
1209
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01001210 :see: :js:attr:`TXN.c`
1211 :see: :js:attr:`TXN.sc`
Thierry FOURNIER2e4893c2015-03-18 13:37:27 +01001212
1213 Converters provides statefull transformation. They are useful for:
1214
1215 * converting input to base64,
1216 * applying hash on input string (djb2, crc32, sdbm, wt6),
1217 * format date,
1218 * json escape,
Pieter Baauw4d7f7662015-11-08 16:38:08 +01001219 * extracting preferred language comparing two lists,
Thierry FOURNIER2e4893c2015-03-18 13:37:27 +01001220 * turn to lower or upper chars,
1221 * deal with stick tables.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001222
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001223.. _channel_class:
1224
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001225Channel class
1226=============
1227
1228.. js:class:: Channel
1229
1230 HAProxy uses two buffers for the processing of the requests. The first one is
1231 used with the request data (from the client to the server) and the second is
1232 used for the response data (from the server to the client).
1233
1234 Each buffer contains two types of data. The first type is the incoming data
1235 waiting for a processing. The second part is the outgoing data already
1236 processed. Usually, the incoming data is processed, after it is tagged as
1237 outgoing data, and finally it is sent. The following functions provides tools
1238 for manipulating these data in a buffer.
1239
1240 The following diagram shows where the channel class function are applied.
1241
1242 **Warning**: It is not possible to read from the response in request action,
1243 and it is not possible to read for the request channel in response action.
1244
Thierry FOURNIER2e4893c2015-03-18 13:37:27 +01001245.. image:: _static/channel.png
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001246
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001247.. js:function:: Channel.dup(channel)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001248
1249 This function returns a string that contain the entire buffer. The data is
1250 not remove from the buffer and can be reprocessed later.
1251
1252 If the buffer cant receive more data, a 'nil' value is returned.
1253
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001254 :param class_channel channel: The manipulated Channel.
Pieter Baauw4d7f7662015-11-08 16:38:08 +01001255 :returns: a string containing all the available data or nil.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001256
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001257.. js:function:: Channel.get(channel)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001258
1259 This function returns a string that contain the entire buffer. The data is
1260 consumed from the buffer.
1261
1262 If the buffer cant receive more data, a 'nil' value is returned.
1263
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001264 :param class_channel channel: The manipulated Channel.
Pieter Baauw4d7f7662015-11-08 16:38:08 +01001265 :returns: a string containing all the available data or nil.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001266
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02001267.. js:function:: Channel.getline(channel)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001268
1269 This function returns a string that contain the first line of the buffer. The
1270 data is consumed. If the data returned doesn't contains a final '\n' its
1271 assumed than its the last available data in the buffer.
1272
1273 If the buffer cant receive more data, a 'nil' value is returned.
1274
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001275 :param class_channel channel: The manipulated Channel.
Pieter Baauw386a1272015-08-16 15:26:24 +02001276 :returns: a string containing the available line or nil.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001277
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001278.. js:function:: Channel.set(channel, string)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001279
1280 This function replace the content of the buffer by the string. The function
1281 returns the copied length, otherwise, it returns -1.
1282
1283 The data set with this function are not send. They wait for the end of
1284 HAProxy processing, so the buffer can be full.
1285
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001286 :param class_channel channel: The manipulated Channel.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001287 :param string string: The data which will sent.
Pieter Baauw4d7f7662015-11-08 16:38:08 +01001288 :returns: an integer containing the amount of bytes copied or -1.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001289
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001290.. js:function:: Channel.append(channel, string)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001291
1292 This function append the string argument to the content of the buffer. The
1293 function returns the copied length, otherwise, it returns -1.
1294
1295 The data set with this function are not send. They wait for the end of
1296 HAProxy processing, so the buffer can be full.
1297
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001298 :param class_channel channel: The manipulated Channel.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001299 :param string string: The data which will sent.
Pieter Baauw4d7f7662015-11-08 16:38:08 +01001300 :returns: an integer containing the amount of bytes copied or -1.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001301
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001302.. js:function:: Channel.send(channel, string)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001303
1304 This function required immediate send of the data. Unless if the connection
1305 is close, the buffer is regularly flushed and all the string can be sent.
1306
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001307 :param class_channel channel: The manipulated Channel.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001308 :param string string: The data which will sent.
Pieter Baauw4d7f7662015-11-08 16:38:08 +01001309 :returns: an integer containing the amount of bytes copied or -1.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001310
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001311.. js:function:: Channel.get_in_length(channel)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001312
1313 This function returns the length of the input part of the buffer.
1314
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001315 :param class_channel channel: The manipulated Channel.
Pieter Baauw4d7f7662015-11-08 16:38:08 +01001316 :returns: an integer containing the amount of available bytes.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001317
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001318.. js:function:: Channel.get_out_length(channel)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001319
1320 This function returns the length of the output part of the buffer.
1321
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001322 :param class_channel channel: The manipulated Channel.
Pieter Baauw4d7f7662015-11-08 16:38:08 +01001323 :returns: an integer containing the amount of available bytes.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001324
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001325.. js:function:: Channel.forward(channel, int)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001326
1327 This function transfer bytes from the input part of the buffer to the output
1328 part.
1329
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001330 :param class_channel channel: The manipulated Channel.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001331 :param integer int: The amount of data which will be forwarded.
1332
Thierry FOURNIER / OZON.IO65192f32016-11-07 15:28:40 +01001333.. js:function:: Channel.is_full(channel)
1334
1335 This function returns true if the buffer channel is full.
1336
1337 :returns: a boolean
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001338
1339.. _http_class:
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001340
1341HTTP class
1342==========
1343
1344.. js:class:: HTTP
1345
1346 This class contain all the HTTP manipulation functions.
1347
Pieter Baauw386a1272015-08-16 15:26:24 +02001348.. js:function:: HTTP.req_get_headers(http)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001349
Patrick Hemmerc6a1d712018-05-01 21:30:41 -04001350 Returns a table containing all the request headers.
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001351
1352 :param class_http http: The related http object.
Patrick Hemmerc6a1d712018-05-01 21:30:41 -04001353 :returns: table of headers.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01001354 :see: :js:func:`HTTP.res_get_headers`
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001355
Patrick Hemmerc6a1d712018-05-01 21:30:41 -04001356 This is the form of the returned table:
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001357
1358.. code-block:: lua
1359
1360 HTTP:req_get_headers()['<header-name>'][<header-index>] = "<header-value>"
1361
1362 local hdr = HTTP:req_get_headers()
1363 hdr["host"][0] = "www.test.com"
1364 hdr["accept"][0] = "audio/basic q=1"
1365 hdr["accept"][1] = "audio/*, q=0.2"
1366 hdr["accept"][2] = "*/*, q=0.1"
1367..
1368
Pieter Baauw386a1272015-08-16 15:26:24 +02001369.. js:function:: HTTP.res_get_headers(http)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001370
Patrick Hemmerc6a1d712018-05-01 21:30:41 -04001371 Returns a table containing all the response headers.
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001372
1373 :param class_http http: The related http object.
Patrick Hemmerc6a1d712018-05-01 21:30:41 -04001374 :returns: table of headers.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01001375 :see: :js:func:`HTTP.req_get_headers`
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001376
Patrick Hemmerc6a1d712018-05-01 21:30:41 -04001377 This is the form of the returned table:
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001378
1379.. code-block:: lua
1380
1381 HTTP:res_get_headers()['<header-name>'][<header-index>] = "<header-value>"
1382
1383 local hdr = HTTP:req_get_headers()
1384 hdr["host"][0] = "www.test.com"
1385 hdr["accept"][0] = "audio/basic q=1"
1386 hdr["accept"][1] = "audio/*, q=0.2"
1387 hdr["accept"][2] = "*.*, q=0.1"
1388..
1389
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001390.. js:function:: HTTP.req_add_header(http, name, value)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001391
1392 Appends an HTTP header field in the request whose name is
1393 specified in "name" and whose value is defined in "value".
1394
1395 :param class_http http: The related http object.
1396 :param string name: The header name.
1397 :param string value: The header value.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01001398 :see: :js:func:`HTTP.res_add_header`
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001399
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001400.. js:function:: HTTP.res_add_header(http, name, value)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001401
1402 appends an HTTP header field in the response whose name is
1403 specified in "name" and whose value is defined in "value".
1404
1405 :param class_http http: The related http object.
1406 :param string name: The header name.
1407 :param string value: The header value.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01001408 :see: :js:func:`HTTP.req_add_header`
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001409
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001410.. js:function:: HTTP.req_del_header(http, name)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001411
1412 Removes all HTTP header fields in the request whose name is
1413 specified in "name".
1414
1415 :param class_http http: The related http object.
1416 :param string name: The header name.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01001417 :see: :js:func:`HTTP.res_del_header`
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001418
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001419.. js:function:: HTTP.res_del_header(http, name)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001420
1421 Removes all HTTP header fields in the response whose name is
1422 specified in "name".
1423
1424 :param class_http http: The related http object.
1425 :param string name: The header name.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01001426 :see: :js:func:`HTTP.req_del_header`
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001427
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001428.. js:function:: HTTP.req_set_header(http, name, value)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001429
1430 This variable replace all occurence of all header "name", by only
1431 one containing the "value".
1432
1433 :param class_http http: The related http object.
1434 :param string name: The header name.
1435 :param string value: The header value.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01001436 :see: :js:func:`HTTP.res_set_header`
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001437
1438 This function does the same work as the folowwing code:
1439
1440.. code-block:: lua
1441
1442 function fcn(txn)
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001443 TXN.http:req_del_header("header")
1444 TXN.http:req_add_header("header", "value")
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001445 end
1446..
1447
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001448.. js:function:: HTTP.res_set_header(http, name, value)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001449
1450 This variable replace all occurence of all header "name", by only
1451 one containing the "value".
1452
1453 :param class_http http: The related http object.
1454 :param string name: The header name.
1455 :param string value: The header value.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01001456 :see: :js:func:`HTTP.req_rep_header()`
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001457
Pieter Baauw386a1272015-08-16 15:26:24 +02001458.. js:function:: HTTP.req_rep_header(http, name, regex, replace)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001459
1460 Matches the regular expression in all occurrences of header field "name"
1461 according to "regex", and replaces them with the "replace" argument. The
1462 replacement value can contain back references like \1, \2, ... This
1463 function works with the request.
1464
1465 :param class_http http: The related http object.
1466 :param string name: The header name.
1467 :param string regex: The match regular expression.
1468 :param string replace: The replacement value.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01001469 :see: :js:func:`HTTP.res_rep_header()`
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001470
Pieter Baauw386a1272015-08-16 15:26:24 +02001471.. js:function:: HTTP.res_rep_header(http, name, regex, string)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001472
1473 Matches the regular expression in all occurrences of header field "name"
1474 according to "regex", and replaces them with the "replace" argument. The
1475 replacement value can contain back references like \1, \2, ... This
1476 function works with the request.
1477
1478 :param class_http http: The related http object.
1479 :param string name: The header name.
1480 :param string regex: The match regular expression.
1481 :param string replace: The replacement value.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01001482 :see: :js:func:`HTTP.req_rep_header()`
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001483
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001484.. js:function:: HTTP.req_set_method(http, method)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001485
1486 Rewrites the request method with the parameter "method".
1487
1488 :param class_http http: The related http object.
1489 :param string method: The new method.
1490
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001491.. js:function:: HTTP.req_set_path(http, path)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001492
1493 Rewrites the request path with the "path" parameter.
1494
1495 :param class_http http: The related http object.
1496 :param string path: The new path.
1497
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001498.. js:function:: HTTP.req_set_query(http, query)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001499
1500 Rewrites the request's query string which appears after the first question
1501 mark ("?") with the parameter "query".
1502
1503 :param class_http http: The related http object.
1504 :param string query: The new query.
1505
Thierry FOURNIER0d79cf62015-08-26 14:20:58 +02001506.. js:function:: HTTP.req_set_uri(http, uri)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001507
1508 Rewrites the request URI with the parameter "uri".
1509
1510 :param class_http http: The related http object.
1511 :param string uri: The new uri.
1512
Robin H. Johnson52f5db22017-01-01 13:10:52 -08001513.. js:function:: HTTP.res_set_status(http, status [, reason])
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +02001514
Robin H. Johnson52f5db22017-01-01 13:10:52 -08001515 Rewrites the response status code with the parameter "code".
1516
1517 If no custom reason is provided, it will be generated from the status.
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +02001518
1519 :param class_http http: The related http object.
1520 :param integer status: The new response status code.
Robin H. Johnson52f5db22017-01-01 13:10:52 -08001521 :param string reason: The new response reason (optional).
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +02001522
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001523.. _txn_class:
1524
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001525TXN class
1526=========
1527
1528.. js:class:: TXN
1529
1530 The txn class contain all the functions relative to the http or tcp
1531 transaction (Note than a tcp stream is the same than a tcp transaction, but
1532 an HTTP transaction is not the same than a tcp stream).
1533
1534 The usage of this class permits to retrieve data from the requests, alter it
1535 and forward it.
1536
1537 All the functions provided by this class are available in the context
1538 **sample-fetches** and **actions**.
1539
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001540.. js:attribute:: TXN.c
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001541
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001542 :returns: An :ref:`converters_class`.
1543
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001544 This attribute contains a Converters class object.
1545
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001546.. js:attribute:: TXN.sc
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001547
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001548 :returns: An :ref:`converters_class`.
1549
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001550 This attribute contains a Converters class object. The functions of
1551 this object returns always a string.
1552
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001553.. js:attribute:: TXN.f
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001554
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001555 :returns: An :ref:`fetches_class`.
1556
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001557 This attribute contains a Fetches class object.
1558
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001559.. js:attribute:: TXN.sf
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001560
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001561 :returns: An :ref:`fetches_class`.
1562
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001563 This attribute contains a Fetches class object. The functions of
1564 this object returns always a string.
1565
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001566.. js:attribute:: TXN.req
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001567
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001568 :returns: An :ref:`channel_class`.
1569
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001570 This attribute contains a channel class object for the request buffer.
1571
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001572.. js:attribute:: TXN.res
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001573
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001574 :returns: An :ref:`channel_class`.
1575
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001576 This attribute contains a channel class object for the response buffer.
1577
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001578.. js:attribute:: TXN.http
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001579
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001580 :returns: An :ref:`http_class`.
1581
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001582 This attribute contains an HTTP class object. It is avalaible only if the
1583 proxy has the "mode http" enabled.
1584
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01001585.. js:function:: TXN.log(TXN, loglevel, msg)
1586
1587 This function sends a log. The log is sent, according with the HAProxy
1588 configuration file, on the default syslog server if it is configured and on
1589 the stderr if it is allowed.
1590
1591 :param class_txn txn: The class txn object containing the data.
1592 :param integer loglevel: Is the log level asociated with the message. It is a
1593 number between 0 and 7.
1594 :param string msg: The log content.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01001595 :see: :js:attr:`core.emerg`, :js:attr:`core.alert`, :js:attr:`core.crit`,
1596 :js:attr:`core.err`, :js:attr:`core.warning`, :js:attr:`core.notice`,
1597 :js:attr:`core.info`, :js:attr:`core.debug` (log level definitions)
1598 :see: :js:func:`TXN.deflog`
1599 :see: :js:func:`TXN.Debug`
1600 :see: :js:func:`TXN.Info`
1601 :see: :js:func:`TXN.Warning`
1602 :see: :js:func:`TXN.Alert`
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01001603
1604.. js:function:: TXN.deflog(TXN, msg)
1605
1606 Sends a log line with the default loglevel for the proxy ssociated with the
1607 transaction.
1608
1609 :param class_txn txn: The class txn object containing the data.
1610 :param string msg: The log content.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01001611 :see: :js:func:`TXN.log
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01001612
1613.. js:function:: TXN.Debug(txn, msg)
1614
1615 :param class_txn txn: The class txn object containing the data.
1616 :param string msg: The log content.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01001617 :see: :js:func:`TXN.log`
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01001618
1619 Does the same job than:
1620
1621.. code-block:: lua
1622
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001623 function Debug(txn, msg)
1624 TXN.log(txn, core.debug, msg)
1625 end
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01001626..
1627
1628.. js:function:: TXN.Info(txn, msg)
1629
1630 :param class_txn txn: The class txn object containing the data.
1631 :param string msg: The log content.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01001632 :see: :js:func:`TXN.log`
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01001633
1634.. code-block:: lua
1635
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001636 function Debug(txn, msg)
1637 TXN.log(txn, core.info, msg)
1638 end
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01001639..
1640
1641.. js:function:: TXN.Warning(txn, msg)
1642
1643 :param class_txn txn: The class txn object containing the data.
1644 :param string msg: The log content.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01001645 :see: :js:func:`TXN.log`
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01001646
1647.. code-block:: lua
1648
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001649 function Debug(txn, msg)
1650 TXN.log(txn, core.warning, msg)
1651 end
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01001652..
1653
1654.. js:function:: TXN.Alert(txn, msg)
1655
1656 :param class_txn txn: The class txn object containing the data.
1657 :param string msg: The log content.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01001658 :see: :js:func:`TXN.log`
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01001659
1660.. code-block:: lua
1661
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001662 function Debug(txn, msg)
1663 TXN.log(txn, core.alert, msg)
1664 end
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01001665..
1666
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001667.. js:function:: TXN.get_priv(txn)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001668
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001669 Return Lua data stored in the current transaction (with the `TXN.set_priv()`)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001670 function. If no data are stored, it returns a nil value.
1671
1672 :param class_txn txn: The class txn object containing the data.
1673 :returns: the opaque data previsously stored, or nil if nothing is
1674 avalaible.
1675
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001676.. js:function:: TXN.set_priv(txn, data)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001677
1678 Store any data in the current HAProxy transaction. This action replace the
1679 old stored data.
1680
1681 :param class_txn txn: The class txn object containing the data.
1682 :param opaque data: The data which is stored in the transaction.
1683
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +02001684.. js:function:: TXN.set_var(TXN, var, value)
1685
David Carlier61fdf8b2015-10-02 11:59:38 +01001686 Converts a Lua type in a HAProxy type and store it in a variable <var>.
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +02001687
1688 :param class_txn txn: The class txn object containing the data.
1689 :param string var: The variable name according with the HAProxy variable syntax.
Thierry FOURNIER / OZON.IOb210bcc2016-12-12 16:24:16 +01001690 :param type value: The value associated to the variable. The type can be string or
1691 integer.
Christopher Faulet85d79c92016-11-09 16:54:56 +01001692
1693.. js:function:: TXN.unset_var(TXN, var)
1694
1695 Unset the variable <var>.
1696
1697 :param class_txn txn: The class txn object containing the data.
1698 :param string var: The variable name according with the HAProxy variable syntax.
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +02001699
1700.. js:function:: TXN.get_var(TXN, var)
1701
1702 Returns data stored in the variable <var> converter in Lua type.
1703
1704 :param class_txn txn: The class txn object containing the data.
1705 :param string var: The variable name according with the HAProxy variable syntax.
1706
Willy Tarreaubc183a62015-08-28 10:39:11 +02001707.. js:function:: TXN.done(txn)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001708
Willy Tarreaubc183a62015-08-28 10:39:11 +02001709 This function terminates processing of the transaction and the associated
1710 session. It can be used when a critical error is detected or to terminate
1711 processing after some data have been returned to the client (eg: a redirect).
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001712
Thierry FOURNIERab00df62016-07-14 11:42:37 +02001713 *Warning*: It not make sense to call this function from sample-fetches. In
1714 this case the behaviour of this one is the same than core.done(): it quit
1715 the Lua execution. The transaction is really aborted only from an action
1716 registered function.
1717
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001718 :param class_txn txn: The class txn object containing the data.
1719
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001720.. js:function:: TXN.set_loglevel(txn, loglevel)
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01001721
1722 Is used to change the log level of the current request. The "loglevel" must
1723 be an integer between 0 and 7.
1724
1725 :param class_txn txn: The class txn object containing the data.
1726 :param integer loglevel: The required log level. This variable can be one of
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01001727 :see: :js:attr:`core.emerg`, :js:attr:`core.alert`, :js:attr:`core.crit`,
1728 :js:attr:`core.err`, :js:attr:`core.warning`, :js:attr:`core.notice`,
1729 :js:attr:`core.info`, :js:attr:`core.debug` (log level definitions)
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01001730
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001731.. js:function:: TXN.set_tos(txn, tos)
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01001732
1733 Is used to set the TOS or DSCP field value of packets sent to the client to
1734 the value passed in "tos" on platforms which support this.
1735
1736 :param class_txn txn: The class txn object containing the data.
1737 :param integer tos: The new TOS os DSCP.
1738
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001739.. js:function:: TXN.set_mark(txn, mark)
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01001740
1741 Is used to set the Netfilter MARK on all packets sent to the client to the
1742 value passed in "mark" on platforms which support it.
1743
1744 :param class_txn txn: The class txn object containing the data.
1745 :param integer mark: The mark value.
1746
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001747.. _socket_class:
1748
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001749Socket class
1750============
1751
1752.. js:class:: Socket
1753
1754 This class must be compatible with the Lua Socket class. Only the 'client'
1755 functions are available. See the Lua Socket documentation:
1756
1757 `http://w3.impa.br/~diego/software/luasocket/tcp.html
1758 <http://w3.impa.br/~diego/software/luasocket/tcp.html>`_
1759
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001760.. js:function:: Socket.close(socket)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001761
1762 Closes a TCP object. The internal socket used by the object is closed and the
1763 local address to which the object was bound is made available to other
1764 applications. No further operations (except for further calls to the close
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001765 method) are allowed on a closed Socket.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001766
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001767 :param class_socket socket: Is the manipulated Socket.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001768
1769 Note: It is important to close all used sockets once they are not needed,
1770 since, in many systems, each socket uses a file descriptor, which are limited
1771 system resources. Garbage-collected objects are automatically closed before
1772 destruction, though.
1773
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02001774.. js:function:: Socket.connect(socket, address[, port])
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001775
1776 Attempts to connect a socket object to a remote host.
1777
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001778
1779 In case of error, the method returns nil followed by a string describing the
1780 error. In case of success, the method returns 1.
1781
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001782 :param class_socket socket: Is the manipulated Socket.
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02001783 :param string address: can be an IP address or a host name. See below for more
1784 information.
1785 :param integer port: must be an integer number in the range [1..64K].
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001786 :returns: 1 or nil.
1787
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02001788 an address field extension permits to use the connect() function to connect to
1789 other stream than TCP. The syntax containing a simpleipv4 or ipv6 address is
1790 the basically expected format. This format requires the port.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001791
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02001792 Other format accepted are a socket path like "/socket/path", it permits to
1793 connect to a socket. abstract namespaces are supported with the prefix
1794 "abns@", and finaly a filedescriotr can be passed with the prefix "fd@".
1795 The prefix "ipv4@", "ipv6@" and "unix@" are also supported. The port can be
1796 passed int the string. The syntax "127.0.0.1:1234" is valid. in this case, the
Tim Duesterhus6edab862018-01-06 19:04:45 +01001797 parameter *port* must not be set.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001798
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001799.. js:function:: Socket.connect_ssl(socket, address, port)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001800
1801 Same behavior than the function socket:connect, but uses SSL.
1802
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001803 :param class_socket socket: Is the manipulated Socket.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001804 :returns: 1 or nil.
1805
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001806.. js:function:: Socket.getpeername(socket)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001807
1808 Returns information about the remote side of a connected client object.
1809
1810 Returns a string with the IP address of the peer, followed by the port number
1811 that peer is using for the connection. In case of error, the method returns
1812 nil.
1813
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001814 :param class_socket socket: Is the manipulated Socket.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001815 :returns: a string containing the server information.
1816
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001817.. js:function:: Socket.getsockname(socket)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001818
1819 Returns the local address information associated to the object.
1820
1821 The method returns a string with local IP address and a number with the port.
1822 In case of error, the method returns nil.
1823
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001824 :param class_socket socket: Is the manipulated Socket.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001825 :returns: a string containing the client information.
1826
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001827.. js:function:: Socket.receive(socket, [pattern [, prefix]])
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001828
1829 Reads data from a client object, according to the specified read pattern.
1830 Patterns follow the Lua file I/O format, and the difference in performance
1831 between all patterns is negligible.
1832
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001833 :param class_socket socket: Is the manipulated Socket.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001834 :param string|integer pattern: Describe what is required (see below).
1835 :param string prefix: A string which will be prefix the returned data.
1836 :returns: a string containing the required data or nil.
1837
1838 Pattern can be any of the following:
1839
1840 * **`*a`**: reads from the socket until the connection is closed. No
1841 end-of-line translation is performed;
1842
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001843 * **`*l`**: reads a line of text from the Socket. The line is terminated by a
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001844 LF character (ASCII 10), optionally preceded by a CR character
1845 (ASCII 13). The CR and LF characters are not included in the
1846 returned line. In fact, all CR characters are ignored by the
1847 pattern. This is the default pattern.
1848
1849 * **number**: causes the method to read a specified number of bytes from the
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001850 Socket. Prefix is an optional string to be concatenated to the
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001851 beginning of any received data before return.
1852
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02001853 * **empty**: If the pattern is left empty, the default option is `*l`.
1854
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001855 If successful, the method returns the received pattern. In case of error, the
1856 method returns nil followed by an error message which can be the string
1857 'closed' in case the connection was closed before the transmission was
1858 completed or the string 'timeout' in case there was a timeout during the
1859 operation. Also, after the error message, the function returns the partial
1860 result of the transmission.
1861
1862 Important note: This function was changed severely. It used to support
1863 multiple patterns (but I have never seen this feature used) and now it
1864 doesn't anymore. Partial results used to be returned in the same way as
1865 successful results. This last feature violated the idea that all functions
1866 should return nil on error. Thus it was changed too.
1867
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001868.. js:function:: Socket.send(socket, data [, start [, end ]])
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001869
1870 Sends data through client object.
1871
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001872 :param class_socket socket: Is the manipulated Socket.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001873 :param string data: The data that will be sent.
1874 :param integer start: The start position in the buffer of the data which will
1875 be sent.
1876 :param integer end: The end position in the buffer of the data which will
1877 be sent.
1878 :returns: see below.
1879
1880 Data is the string to be sent. The optional arguments i and j work exactly
1881 like the standard string.sub Lua function to allow the selection of a
1882 substring to be sent.
1883
1884 If successful, the method returns the index of the last byte within [start,
1885 end] that has been sent. Notice that, if start is 1 or absent, this is
1886 effectively the total number of bytes sent. In case of error, the method
1887 returns nil, followed by an error message, followed by the index of the last
1888 byte within [start, end] that has been sent. You might want to try again from
1889 the byte following that. The error message can be 'closed' in case the
1890 connection was closed before the transmission was completed or the string
1891 'timeout' in case there was a timeout during the operation.
1892
1893 Note: Output is not buffered. For small strings, it is always better to
1894 concatenate them in Lua (with the '..' operator) and send the result in one
1895 call instead of calling the method several times.
1896
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001897.. js:function:: Socket.setoption(socket, option [, value])
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001898
1899 Just implemented for compatibility, this cal does nothing.
1900
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001901.. js:function:: Socket.settimeout(socket, value [, mode])
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001902
1903 Changes the timeout values for the object. All I/O operations are blocking.
1904 That is, any call to the methods send, receive, and accept will block
1905 indefinitely, until the operation completes. The settimeout method defines a
1906 limit on the amount of time the I/O methods can block. When a timeout time
1907 has elapsed, the affected methods give up and fail with an error code.
1908
1909 The amount of time to wait is specified as the value parameter, in seconds.
1910
Mark Lakes56cc1252018-03-27 09:48:06 +02001911 The timeout modes are not implemented, the only settable timeout is the
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001912 inactivity time waiting for complete the internal buffer send or waiting for
1913 receive data.
1914
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001915 :param class_socket socket: Is the manipulated Socket.
Mark Lakes56cc1252018-03-27 09:48:06 +02001916 :param float value: The timeout value. Use flotting point to specify
1917 milliseconds.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001918
Thierry FOURNIER31904272017-10-25 12:59:51 +02001919.. _regex_class:
1920
1921Regex class
1922===========
1923
1924.. js:class:: Regex
1925
1926 This class allows the usage of HAProxy regexes because classic lua doesn't
1927 provides regexes. This class inherits the HAProxy compilation options, so the
1928 regexes can be libc regex, pcre regex or pcre JIT regex.
1929
1930 The expression matching number is limited to 20 per regex. The only available
1931 option is case sensitive.
1932
1933 Because regexes compilation is a heavy process, it is better to define all
1934 your regexes in the **body context** and use it during the runtime.
1935
1936.. code-block:: lua
1937
1938 -- Create the regex
1939 st, regex = Regex.new("needle (..) (...)", true);
1940
1941 -- Check compilation errors
1942 if st == false then
1943 print "error: " .. regex
1944 end
1945
1946 -- Match the regexes
1947 print(regex:exec("Looking for a needle in the haystack")) -- true
1948 print(regex:exec("Lokking for a cat in the haystack")) -- false
1949
1950 -- Extract words
1951 st, list = regex:match("Looking for a needle in the haystack")
1952 print(st) -- true
1953 print(list[1]) -- needle in the
1954 print(list[2]) -- in
1955 print(list[3]) -- the
1956
1957.. js:function:: Regex.new(regex, case_sensitive)
1958
1959 Create and compile a regex.
1960
1961 :param string regex: The regular expression according with the libc or pcre
1962 standard
1963 :param boolean case_sensitive: Match is case sensitive or not.
1964 :returns: boolean status and :ref:`regex_class` or string containing fail reason.
1965
1966.. js:function:: Regex.exec(regex, str)
1967
1968 Execute the regex.
1969
1970 :param class_regex regex: A :ref:`regex_class` object.
1971 :param string str: The input string will be compared with the compiled regex.
1972 :returns: a boolean status according with the match result.
1973
1974.. js:function:: Regex.match(regex, str)
1975
1976 Execute the regex and return matched expressions.
1977
1978 :param class_map map: A :ref:`regex_class` object.
1979 :param string str: The input string will be compared with the compiled regex.
1980 :returns: a boolean status according with the match result, and
1981 a table containing all the string matched in order of declaration.
1982
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001983.. _map_class:
1984
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001985Map class
1986=========
1987
1988.. js:class:: Map
1989
1990 This class permits to do some lookup in HAProxy maps. The declared maps can
1991 be modified during the runtime throught the HAProxy management socket.
1992
1993.. code-block:: lua
1994
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001995 default = "usa"
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001996
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001997 -- Create and load map
Thierry FOURNIER4dc71972017-01-28 08:33:08 +01001998 geo = Map.new("geo.map", Map._ip);
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001999
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002000 -- Create new fetch that returns the user country
2001 core.register_fetches("country", function(txn)
2002 local src;
2003 local loc;
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002004
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002005 src = txn.f:fhdr("x-forwarded-for");
2006 if (src == nil) then
2007 src = txn.f:src()
2008 if (src == nil) then
2009 return default;
2010 end
2011 end
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002012
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002013 -- Perform lookup
2014 loc = geo:lookup(src);
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002015
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002016 if (loc == nil) then
2017 return default;
2018 end
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002019
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002020 return loc;
2021 end);
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002022
Thierry FOURNIER4dc71972017-01-28 08:33:08 +01002023.. js:attribute:: Map._int
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002024
2025 See the HAProxy configuration.txt file, chapter "Using ACLs and fetching
2026 samples" ans subchapter "ACL basics" to understand this pattern matching
2027 method.
2028
Thierry FOURNIER4dc71972017-01-28 08:33:08 +01002029 Note that :js:attr:`Map.int` is also available for compatibility.
2030
2031.. js:attribute:: Map._ip
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002032
2033 See the HAProxy configuration.txt file, chapter "Using ACLs and fetching
2034 samples" ans subchapter "ACL basics" to understand this pattern matching
2035 method.
2036
Thierry FOURNIER4dc71972017-01-28 08:33:08 +01002037 Note that :js:attr:`Map.ip` is also available for compatibility.
2038
2039.. js:attribute:: Map._str
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002040
2041 See the HAProxy configuration.txt file, chapter "Using ACLs and fetching
2042 samples" ans subchapter "ACL basics" to understand this pattern matching
2043 method.
2044
Thierry FOURNIER4dc71972017-01-28 08:33:08 +01002045 Note that :js:attr:`Map.str` is also available for compatibility.
2046
2047.. js:attribute:: Map._beg
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002048
2049 See the HAProxy configuration.txt file, chapter "Using ACLs and fetching
2050 samples" ans subchapter "ACL basics" to understand this pattern matching
2051 method.
2052
Thierry FOURNIER4dc71972017-01-28 08:33:08 +01002053 Note that :js:attr:`Map.beg` is also available for compatibility.
2054
2055.. js:attribute:: Map._sub
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002056
2057 See the HAProxy configuration.txt file, chapter "Using ACLs and fetching
2058 samples" ans subchapter "ACL basics" to understand this pattern matching
2059 method.
2060
Thierry FOURNIER4dc71972017-01-28 08:33:08 +01002061 Note that :js:attr:`Map.sub` is also available for compatibility.
2062
2063.. js:attribute:: Map._dir
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002064
2065 See the HAProxy configuration.txt file, chapter "Using ACLs and fetching
2066 samples" ans subchapter "ACL basics" to understand this pattern matching
2067 method.
2068
Thierry FOURNIER4dc71972017-01-28 08:33:08 +01002069 Note that :js:attr:`Map.dir` is also available for compatibility.
2070
2071.. js:attribute:: Map._dom
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002072
2073 See the HAProxy configuration.txt file, chapter "Using ACLs and fetching
2074 samples" ans subchapter "ACL basics" to understand this pattern matching
2075 method.
2076
Thierry FOURNIER4dc71972017-01-28 08:33:08 +01002077 Note that :js:attr:`Map.dom` is also available for compatibility.
2078
2079.. js:attribute:: Map._end
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002080
2081 See the HAProxy configuration.txt file, chapter "Using ACLs and fetching
2082 samples" ans subchapter "ACL basics" to understand this pattern matching
2083 method.
2084
Thierry FOURNIER4dc71972017-01-28 08:33:08 +01002085.. js:attribute:: Map._reg
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002086
2087 See the HAProxy configuration.txt file, chapter "Using ACLs and fetching
2088 samples" ans subchapter "ACL basics" to understand this pattern matching
2089 method.
2090
Thierry FOURNIER4dc71972017-01-28 08:33:08 +01002091 Note that :js:attr:`Map.reg` is also available for compatibility.
2092
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002093
2094.. js:function:: Map.new(file, method)
2095
2096 Creates and load a map.
2097
2098 :param string file: Is the file containing the map.
2099 :param integer method: Is the map pattern matching method. See the attributes
2100 of the Map class.
2101 :returns: a class Map object.
Thierry FOURNIER4dc71972017-01-28 08:33:08 +01002102 :see: The Map attributes: :js:attr:`Map._int`, :js:attr:`Map._ip`,
2103 :js:attr:`Map._str`, :js:attr:`Map._beg`, :js:attr:`Map._sub`,
2104 :js:attr:`Map._dir`, :js:attr:`Map._dom`, :js:attr:`Map._end` and
2105 :js:attr:`Map._reg`.
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002106
2107.. js:function:: Map.lookup(map, str)
2108
2109 Perform a lookup in a map.
2110
2111 :param class_map map: Is the class Map object.
2112 :param string str: Is the string used as key.
2113 :returns: a string containing the result or nil if no match.
2114
2115.. js:function:: Map.slookup(map, str)
2116
2117 Perform a lookup in a map.
2118
2119 :param class_map map: Is the class Map object.
2120 :param string str: Is the string used as key.
2121 :returns: a string containing the result or empty string if no match.
2122
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002123.. _applethttp_class:
2124
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002125AppletHTTP class
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002126================
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002127
2128.. js:class:: AppletHTTP
2129
2130 This class is used with applets that requires the 'http' mode. The http applet
2131 can be registered with the *core.register_service()* function. They are used
2132 for processing an http request like a server in back of HAProxy.
2133
2134 This is an hello world sample code:
2135
2136.. code-block:: lua
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002137
Pieter Baauw4d7f7662015-11-08 16:38:08 +01002138 core.register_service("hello-world", "http", function(applet)
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002139 local response = "Hello World !"
2140 applet:set_status(200)
Pieter Baauw2dcb9bc2015-10-01 22:47:12 +02002141 applet:add_header("content-length", string.len(response))
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002142 applet:add_header("content-type", "text/plain")
Pieter Baauw2dcb9bc2015-10-01 22:47:12 +02002143 applet:start_response()
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002144 applet:send(response)
2145 end)
2146
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002147.. js:attribute:: AppletHTTP.c
2148
2149 :returns: A :ref:`converters_class`
2150
2151 This attribute contains a Converters class object.
2152
2153.. js:attribute:: AppletHTTP.sc
2154
2155 :returns: A :ref:`converters_class`
2156
2157 This attribute contains a Converters class object. The
2158 functions of this object returns always a string.
2159
2160.. js:attribute:: AppletHTTP.f
2161
2162 :returns: A :ref:`fetches_class`
2163
2164 This attribute contains a Fetches class object. Note that the
2165 applet execution place cannot access to a valid HAProxy core HTTP
2166 transaction, so some sample fecthes related to the HTTP dependant
2167 values (hdr, path, ...) are not available.
2168
2169.. js:attribute:: AppletHTTP.sf
2170
2171 :returns: A :ref:`fetches_class`
2172
2173 This attribute contains a Fetches class object. The functions of
2174 this object returns always a string. Note that the applet
2175 execution place cannot access to a valid HAProxy core HTTP
2176 transaction, so some sample fecthes related to the HTTP dependant
2177 values (hdr, path, ...) are not available.
2178
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01002179.. js:attribute:: AppletHTTP.method
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002180
2181 :returns: string
2182
2183 The attribute method returns a string containing the HTTP
2184 method.
2185
2186.. js:attribute:: AppletHTTP.version
2187
2188 :returns: string
2189
2190 The attribute version, returns a string containing the HTTP
2191 request version.
2192
2193.. js:attribute:: AppletHTTP.path
2194
2195 :returns: string
2196
2197 The attribute path returns a string containing the HTTP
2198 request path.
2199
2200.. js:attribute:: AppletHTTP.qs
2201
2202 :returns: string
2203
2204 The attribute qs returns a string containing the HTTP
2205 request query string.
2206
2207.. js:attribute:: AppletHTTP.length
2208
2209 :returns: integer
2210
2211 The attribute length returns an integer containing the HTTP
2212 body length.
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002213
Thierry FOURNIER841475e2015-12-11 17:10:09 +01002214.. js:attribute:: AppletHTTP.headers
2215
Patrick Hemmerc6a1d712018-05-01 21:30:41 -04002216 :returns: table
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002217
Patrick Hemmerc6a1d712018-05-01 21:30:41 -04002218 The attribute headers returns a table containing the HTTP
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002219 headers. The header names are always in lower case. As the header name can be
2220 encountered more than once in each request, the value is indexed with 0 as
Patrick Hemmerc6a1d712018-05-01 21:30:41 -04002221 first index value. The table have this form:
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002222
2223.. code-block:: lua
2224
2225 AppletHTTP.headers['<header-name>'][<header-index>] = "<header-value>"
2226
2227 AppletHTTP.headers["host"][0] = "www.test.com"
2228 AppletHTTP.headers["accept"][0] = "audio/basic q=1"
2229 AppletHTTP.headers["accept"][1] = "audio/*, q=0.2"
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01002230 AppletHTTP.headers["accept"][2] = "*/*, q=0.1"
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002231..
2232
Robin H. Johnson52f5db22017-01-01 13:10:52 -08002233.. js:function:: AppletHTTP.set_status(applet, code [, reason])
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002234
2235 This function sets the HTTP status code for the response. The allowed code are
2236 from 100 to 599.
2237
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01002238 :param class_AppletHTTP applet: An :ref:`applethttp_class`
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002239 :param integer code: the status code returned to the client.
Robin H. Johnson52f5db22017-01-01 13:10:52 -08002240 :param string reason: the status reason returned to the client (optional).
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002241
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01002242.. js:function:: AppletHTTP.add_header(applet, name, value)
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002243
2244 This function add an header in the response. Duplicated headers are not
2245 collapsed. The special header *content-length* is used to determinate the
2246 response length. If it not exists, a *transfer-encoding: chunked* is set, and
2247 all the write from the funcion *AppletHTTP:send()* become a chunk.
2248
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01002249 :param class_AppletHTTP applet: An :ref:`applethttp_class`
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002250 :param string name: the header name
2251 :param string value: the header value
2252
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01002253.. js:function:: AppletHTTP.start_response(applet)
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002254
2255 This function indicates to the HTTP engine that it can process and send the
2256 response headers. After this called we cannot add headers to the response; We
2257 cannot use the *AppletHTTP:send()* function if the
2258 *AppletHTTP:start_response()* is not called.
2259
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01002260 :param class_AppletHTTP applet: An :ref:`applethttp_class`
2261
2262.. js:function:: AppletHTTP.getline(applet)
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002263
2264 This function returns a string containing one line from the http body. If the
2265 data returned doesn't contains a final '\\n' its assumed than its the last
2266 available data before the end of stream.
2267
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01002268 :param class_AppletHTTP applet: An :ref:`applethttp_class`
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002269 :returns: a string. The string can be empty if we reach the end of the stream.
2270
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01002271.. js:function:: AppletHTTP.receive(applet, [size])
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002272
2273 Reads data from the HTTP body, according to the specified read *size*. If the
2274 *size* is missing, the function tries to read all the content of the stream
2275 until the end. If the *size* is bigger than the http body, it returns the
2276 amount of data avalaible.
2277
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01002278 :param class_AppletHTTP applet: An :ref:`applethttp_class`
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002279 :param integer size: the required read size.
2280 :returns: always return a string,the string can be empty is the connexion is
2281 closed.
2282
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01002283.. js:function:: AppletHTTP.send(applet, msg)
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002284
2285 Send the message *msg* on the http request body.
2286
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01002287 :param class_AppletHTTP applet: An :ref:`applethttp_class`
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002288 :param string msg: the message to send.
2289
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01002290.. js:function:: AppletHTTP.get_priv(applet)
2291
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01002292 Return Lua data stored in the current transaction. If no data are stored,
2293 it returns a nil value.
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01002294
2295 :param class_AppletHTTP applet: An :ref:`applethttp_class`
2296 :returns: the opaque data previsously stored, or nil if nothing is
2297 avalaible.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01002298 :see: :js:func:`AppletHTTP.set_priv`
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01002299
2300.. js:function:: AppletHTTP.set_priv(applet, data)
2301
2302 Store any data in the current HAProxy transaction. This action replace the
2303 old stored data.
2304
2305 :param class_AppletHTTP applet: An :ref:`applethttp_class`
2306 :param opaque data: The data which is stored in the transaction.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01002307 :see: :js:func:`AppletHTTP.get_priv`
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01002308
Thierry FOURNIER / OZON.IOc1edafe2016-12-12 16:25:30 +01002309.. js:function:: AppletHTTP.set_var(applet, var, value)
2310
2311 Converts a Lua type in a HAProxy type and store it in a variable <var>.
2312
2313 :param class_AppletHTTP applet: An :ref:`applethttp_class`
2314 :param string var: The variable name according with the HAProxy variable syntax.
2315 :param type value: The value associated to the variable. The type ca be string or
2316 integer.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01002317 :see: :js:func:`AppletHTTP.unset_var`
2318 :see: :js:func:`AppletHTTP.get_var`
Thierry FOURNIER / OZON.IOc1edafe2016-12-12 16:25:30 +01002319
2320.. js:function:: AppletHTTP.unset_var(applet, var)
2321
2322 Unset the variable <var>.
2323
2324 :param class_AppletHTTP applet: An :ref:`applethttp_class`
2325 :param string var: The variable name according with the HAProxy variable syntax.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01002326 :see: :js:func:`AppletHTTP.set_var`
2327 :see: :js:func:`AppletHTTP.get_var`
Thierry FOURNIER / OZON.IOc1edafe2016-12-12 16:25:30 +01002328
2329.. js:function:: AppletHTTP.get_var(applet, var)
2330
2331 Returns data stored in the variable <var> converter in Lua type.
2332
2333 :param class_AppletHTTP applet: An :ref:`applethttp_class`
2334 :param string var: The variable name according with the HAProxy variable syntax.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01002335 :see: :js:func:`AppletHTTP.set_var`
2336 :see: :js:func:`AppletHTTP.unset_var`
Thierry FOURNIER / OZON.IOc1edafe2016-12-12 16:25:30 +01002337
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002338.. _applettcp_class:
2339
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002340AppletTCP class
2341===============
2342
2343.. js:class:: AppletTCP
2344
2345 This class is used with applets that requires the 'tcp' mode. The tcp applet
2346 can be registered with the *core.register_service()* function. They are used
2347 for processing a tcp stream like a server in back of HAProxy.
2348
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002349.. js:attribute:: AppletTCP.c
2350
2351 :returns: A :ref:`converters_class`
2352
2353 This attribute contains a Converters class object.
2354
2355.. js:attribute:: AppletTCP.sc
2356
2357 :returns: A :ref:`converters_class`
2358
2359 This attribute contains a Converters class object. The
2360 functions of this object returns always a string.
2361
2362.. js:attribute:: AppletTCP.f
2363
2364 :returns: A :ref:`fetches_class`
2365
2366 This attribute contains a Fetches class object.
2367
2368.. js:attribute:: AppletTCP.sf
2369
2370 :returns: A :ref:`fetches_class`
2371
2372 This attribute contains a Fetches class object.
2373
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01002374.. js:function:: AppletTCP.getline(applet)
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002375
2376 This function returns a string containing one line from the stream. If the
2377 data returned doesn't contains a final '\\n' its assumed than its the last
2378 available data before the end of stream.
2379
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01002380 :param class_AppletTCP applet: An :ref:`applettcp_class`
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002381 :returns: a string. The string can be empty if we reach the end of the stream.
2382
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01002383.. js:function:: AppletTCP.receive(applet, [size])
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002384
2385 Reads data from the TCP stream, according to the specified read *size*. If the
2386 *size* is missing, the function tries to read all the content of the stream
2387 until the end.
2388
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01002389 :param class_AppletTCP applet: An :ref:`applettcp_class`
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002390 :param integer size: the required read size.
2391 :returns: always return a string,the string can be empty is the connexion is
2392 closed.
2393
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01002394.. js:function:: AppletTCP.send(appletmsg)
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002395
2396 Send the message on the stream.
2397
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01002398 :param class_AppletTCP applet: An :ref:`applettcp_class`
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002399 :param string msg: the message to send.
2400
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01002401.. js:function:: AppletTCP.get_priv(applet)
2402
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01002403 Return Lua data stored in the current transaction. If no data are stored,
2404 it returns a nil value.
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01002405
2406 :param class_AppletTCP applet: An :ref:`applettcp_class`
2407 :returns: the opaque data previsously stored, or nil if nothing is
2408 avalaible.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01002409 :see: :js:func:`AppletTCP.set_priv`
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01002410
2411.. js:function:: AppletTCP.set_priv(applet, data)
2412
2413 Store any data in the current HAProxy transaction. This action replace the
2414 old stored data.
2415
2416 :param class_AppletTCP applet: An :ref:`applettcp_class`
2417 :param opaque data: The data which is stored in the transaction.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01002418 :see: :js:func:`AppletTCP.get_priv`
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01002419
Thierry FOURNIER / OZON.IOc1edafe2016-12-12 16:25:30 +01002420.. js:function:: AppletTCP.set_var(applet, var, value)
2421
2422 Converts a Lua type in a HAProxy type and stores it in a variable <var>.
2423
2424 :param class_AppletTCP applet: An :ref:`applettcp_class`
2425 :param string var: The variable name according with the HAProxy variable syntax.
2426 :param type value: The value associated to the variable. The type can be string or
2427 integer.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01002428 :see: :js:func:`AppletTCP.unset_var`
2429 :see: :js:func:`AppletTCP.get_var`
Thierry FOURNIER / OZON.IOc1edafe2016-12-12 16:25:30 +01002430
2431.. js:function:: AppletTCP.unset_var(applet, var)
2432
2433 Unsets the variable <var>.
2434
2435 :param class_AppletTCP applet: An :ref:`applettcp_class`
2436 :param string var: The variable name according with the HAProxy variable syntax.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01002437 :see: :js:func:`AppletTCP.unset_var`
2438 :see: :js:func:`AppletTCP.set_var`
Thierry FOURNIER / OZON.IOc1edafe2016-12-12 16:25:30 +01002439
2440.. js:function:: AppletTCP.get_var(applet, var)
2441
2442 Returns data stored in the variable <var> converter in Lua type.
2443
2444 :param class_AppletTCP applet: An :ref:`applettcp_class`
2445 :param string var: The variable name according with the HAProxy variable syntax.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01002446 :see: :js:func:`AppletTCP.unset_var`
2447 :see: :js:func:`AppletTCP.set_var`
Thierry FOURNIER / OZON.IOc1edafe2016-12-12 16:25:30 +01002448
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002449External Lua libraries
2450======================
2451
2452A lot of useful lua libraries can be found here:
2453
2454* `https://lua-toolbox.com/ <https://lua-toolbox.com/>`_
2455
2456Redis acces:
2457
2458* `https://github.com/nrk/redis-lua <https://github.com/nrk/redis-lua>`_
2459
2460This is an example about the usage of the Redis library with HAProxy. Note that
2461each call of any function of this library can throw an error if the socket
2462connection fails.
2463
2464.. code-block:: lua
2465
2466 -- load the redis library
2467 local redis = require("redis");
2468
2469 function do_something(txn)
2470
2471 -- create and connect new tcp socket
2472 local tcp = core.tcp();
2473 tcp:settimeout(1);
2474 tcp:connect("127.0.0.1", 6379);
2475
2476 -- use the redis library with this new socket
2477 local client = redis.connect({socket=tcp});
2478 client:ping();
2479
2480 end
2481
2482OpenSSL:
2483
2484* `http://mkottman.github.io/luacrypto/index.html
2485 <http://mkottman.github.io/luacrypto/index.html>`_
2486
2487* `https://github.com/brunoos/luasec/wiki
2488 <https://github.com/brunoos/luasec/wiki>`_
Thierry FOURNIER2e4893c2015-03-18 13:37:27 +01002489