blob: 991dc6f2f616c2055e87ced15485ce9975e4f967 [file] [log] [blame]
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001.. toctree::
2 :maxdepth: 2
3
4
5How Lua runs in HAProxy
6=======================
7
8HAProxy Lua running contexts
9----------------------------
10
11The Lua code executed in HAProxy can be processed in 2 main modes. The first one
12is the **initialisation mode**, and the second is the **runtime mode**.
13
14* In the **initialisation mode**, we can perform DNS solves, but we cannot
15 perform socket I/O. In this initialisation mode, HAProxy still blocked during
16 the execution of the Lua program.
17
18* In the **runtime mode**, we cannot perform DNS solves, but we can use sockets.
19 The execution of the Lua code is multiplexed with the requests processing, so
20 the Lua code seems to be run in blocking, but it is not the case.
21
22The Lua code is loaded in one or more files. These files contains main code and
Christopher Faulet5a2c6612021-08-15 20:35:25 +020023functions. Lua have 7 execution context.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +010024
251. The Lua file **body context**. It is executed during the load of the Lua file
26 in the HAProxy `[global]` section with the directive `lua-load`. It is
27 executed in initialisation mode. This section is use for configuring Lua
28 bindings in HAProxy.
29
David Carlier61fdf8b2015-10-02 11:59:38 +0100302. The Lua **init context**. It is a Lua function executed just after the
Thierry FOURNIER17bd1522015-03-11 20:31:00 +010031 HAProxy configuration parsing. The execution is in initialisation mode. In
32 this context the HAProxy environment are already initialized. It is useful to
33 check configuration, or initializing socket connections or tasks. These
34 functions are declared in the body context with the Lua function
35 `core.register_init()`. The prototype of the function is a simple function
36 without return value and without parameters, like this: `function fcn()`.
37
David Carlier61fdf8b2015-10-02 11:59:38 +0100383. The Lua **task context**. It is a Lua function executed after the start
Thierry FOURNIER17bd1522015-03-11 20:31:00 +010039 of the HAProxy scheduler, and just after the declaration of the task with the
40 Lua function `core.register_task()`. This context can be concurrent with the
41 traffic processing. It is executed in runtime mode. The prototype of the
42 function is a simple function without return value and without parameters,
43 like this: `function fcn()`.
44
David Carlier61fdf8b2015-10-02 11:59:38 +0100454. The **action context**. It is a Lua function conditionally executed. These
Thierry FOURNIERa2d02252015-10-01 15:00:42 +020046 actions are registered by the Lua directives "`core.register_action()`". The
47 prototype of the Lua called function is a function with doesn't returns
48 anything and that take an object of class TXN as entry. `function fcn(txn)`.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +010049
505. The **sample-fetch context**. This function takes a TXN object as entry
51 argument and returns a string. These types of function cannot execute any
52 blocking function. They are useful to aggregate some of original HAProxy
53 sample-fetches and return the result. The prototype of the function is
54 `function string fcn(txn)`. These functions can be registered with the Lua
55 function `core.register_fetches()`. Each declared sample-fetch is prefixed by
56 the string "lua.".
57
Christopher Faulet1e9b1b62021-08-11 10:14:30 +020058 .. note::
59 It is possible that this function cannot found the required data in the
60 original HAProxy sample-fetches, in this case, it cannot return the
61 result. This case is not yet supported
Thierry FOURNIER17bd1522015-03-11 20:31:00 +010062
David Carlier61fdf8b2015-10-02 11:59:38 +0100636. The **converter context**. It is a Lua function that takes a string as input
Thierry FOURNIER17bd1522015-03-11 20:31:00 +010064 and returns another string as output. These types of function are stateless,
65 it cannot access to any context. They don't execute any blocking function.
66 The call prototype is `function string fcn(string)`. This function can be
67 registered with the Lua function `core.register_converters()`. Each declared
68 converter is prefixed by the string "lua.".
69
Christopher Faulet5a2c6612021-08-15 20:35:25 +0200707. The **filter context**: It is a Lua object based on a class defining filter
71 callback functions. Lua filters are registered using
72 `core.register_filter()`. Each declared filter is prefixed by the string
73 "lua.".
74
75 .. warning::
76 The Lua filter support is highly experimental. The API is still unstable
77 and may change without notice. No backward compatibility should be
78 expected for now. Use it with an extreme caution and report any issue or
79 comment about it. The feature was unveiled to improve it and to adapt it
80 to real usages.
81
82
Thierry FOURNIER17bd1522015-03-11 20:31:00 +010083HAProxy Lua Hello world
84-----------------------
85
86HAProxy configuration file (`hello_world.conf`):
87
88::
89
90 global
91 lua-load hello_world.lua
92
93 listen proxy
94 bind 127.0.0.1:10001
Thierry FOURNIERa2d02252015-10-01 15:00:42 +020095 tcp-request inspect-delay 1s
96 tcp-request content use-service lua.hello_world
Thierry FOURNIER17bd1522015-03-11 20:31:00 +010097
98HAProxy Lua file (`hello_world.lua`):
99
100.. code-block:: lua
101
Thierry FOURNIERa2d02252015-10-01 15:00:42 +0200102 core.register_service("hello_world", "tcp", function(applet)
103 applet:send("hello world\n")
104 end)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100105
106How to start HAProxy for testing this configuration:
107
108::
109
110 ./haproxy -f hello_world.conf
111
112On other terminal, you can test with telnet:
113
114::
115
116 #:~ telnet 127.0.0.1 10001
117 hello world
118
119Core class
120==========
121
122.. js:class:: core
123
124 The "core" class contains all the HAProxy core functions. These function are
125 useful for the controlling the execution flow, registering hooks, manipulating
126 global maps or ACL, ...
127
128 "core" class is basically provided with HAProxy. No `require` line is
129 required to uses these function.
130
David Carlier61fdf8b2015-10-02 11:59:38 +0100131 The "core" class is static, it is not possible to create a new object of this
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100132 type.
133
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100134.. js:attribute:: core.emerg
135
Thierry FOURNIERdc595002015-12-21 11:13:52 +0100136 :returns: integer
137
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100138 This attribute is an integer, it contains the value of the loglevel "emergency" (0).
139
140.. js:attribute:: core.alert
141
Thierry FOURNIERdc595002015-12-21 11:13:52 +0100142 :returns: integer
143
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100144 This attribute is an integer, it contains the value of the loglevel "alert" (1).
145
146.. js:attribute:: core.crit
147
Thierry FOURNIERdc595002015-12-21 11:13:52 +0100148 :returns: integer
149
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100150 This attribute is an integer, it contains the value of the loglevel "critical" (2).
151
152.. js:attribute:: core.err
153
Thierry FOURNIERdc595002015-12-21 11:13:52 +0100154 :returns: integer
155
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100156 This attribute is an integer, it contains the value of the loglevel "error" (3).
157
158.. js:attribute:: core.warning
159
Thierry FOURNIERdc595002015-12-21 11:13:52 +0100160 :returns: integer
161
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100162 This attribute is an integer, it contains the value of the loglevel "warning" (4).
163
164.. js:attribute:: core.notice
165
Thierry FOURNIERdc595002015-12-21 11:13:52 +0100166 :returns: integer
167
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100168 This attribute is an integer, it contains the value of the loglevel "notice" (5).
169
170.. js:attribute:: core.info
171
Thierry FOURNIERdc595002015-12-21 11:13:52 +0100172 :returns: integer
173
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100174 This attribute is an integer, it contains the value of the loglevel "info" (6).
175
176.. js:attribute:: core.debug
177
Thierry FOURNIERdc595002015-12-21 11:13:52 +0100178 :returns: integer
179
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100180 This attribute is an integer, it contains the value of the loglevel "debug" (7).
181
Thierry FOURNIER12a865d2016-12-14 19:40:37 +0100182.. js:attribute:: core.proxies
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 (frontend and backends). Each
187 proxy give an access to his list of listeners and servers. The table is
188 indexed by proxy name, and each entry is of type :ref:`proxy_class`.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +0100189
Christopher Faulet1e9b1b62021-08-11 10:14:30 +0200190 .. Warning::
191 if you are declared frontend and backend with the same name, only one of
192 these are listed.
Thierry FOURNIER9b82a582017-07-24 13:30:43 +0200193
194 :see: :js:attr:`core.backends`
195 :see: :js:attr:`core.frontends`
196
197.. js:attribute:: core.backends
198
199 **context**: task, action, sample-fetch, converter
200
Patrick Hemmerc6a1d712018-05-01 21:30:41 -0400201 This attribute is a table of declared proxies with backend capability. Each
202 proxy give an access to his list of listeners and servers. The table is
203 indexed by the backend name, and each entry is of type :ref:`proxy_class`.
Thierry FOURNIER9b82a582017-07-24 13:30:43 +0200204
205 :see: :js:attr:`core.proxies`
206 :see: :js:attr:`core.frontends`
207
208.. js:attribute:: core.frontends
209
210 **context**: task, action, sample-fetch, converter
211
Patrick Hemmerc6a1d712018-05-01 21:30:41 -0400212 This attribute is a table of declared proxies with frontend capability. Each
213 proxy give an access to his list of listeners and servers. The table is
214 indexed by the frontend name, and each entry is of type :ref:`proxy_class`.
Thierry FOURNIER9b82a582017-07-24 13:30:43 +0200215
216 :see: :js:attr:`core.proxies`
217 :see: :js:attr:`core.backends`
218
Thierry Fournierecb83c22020-11-28 15:49:44 +0100219.. js:attribute:: core.thread
220
221 **context**: task, action, sample-fetch, converter, applet
222
223 This variable contains the executing thread number starting at 1. 0 is a
224 special case for the common lua context. So, if thread is 0, Lua scope is
225 shared by all threads, otherwise the scope is dedicated to a single thread.
226 A program which needs to execute some parts exactly once regardless of the
227 number of threads can check that core.thread is 0 or 1.
228
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100229.. js:function:: core.log(loglevel, msg)
230
231 **context**: body, init, task, action, sample-fetch, converter
232
David Carlier61fdf8b2015-10-02 11:59:38 +0100233 This function sends a log. The log is sent, according with the HAProxy
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100234 configuration file, on the default syslog server if it is configured and on
235 the stderr if it is allowed.
236
Bertrand Jacquin874a35c2018-09-10 21:26:07 +0100237 :param integer loglevel: Is the log level associated with the message. It is a
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100238 number between 0 and 7.
239 :param string msg: The log content.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +0100240 :see: :js:attr:`core.emerg`, :js:attr:`core.alert`, :js:attr:`core.crit`,
241 :js:attr:`core.err`, :js:attr:`core.warning`, :js:attr:`core.notice`,
242 :js:attr:`core.info`, :js:attr:`core.debug` (log level definitions)
243 :see: :js:func:`core.Debug`
244 :see: :js:func:`core.Info`
245 :see: :js:func:`core.Warning`
246 :see: :js:func:`core.Alert`
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100247
248.. js:function:: core.Debug(msg)
249
250 **context**: body, init, task, action, sample-fetch, converter
251
252 :param string msg: The log content.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +0100253 :see: :js:func:`core.log`
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100254
255 Does the same job than:
256
257.. code-block:: lua
258
Thierry FOURNIERdc595002015-12-21 11:13:52 +0100259 function Debug(msg)
260 core.log(core.debug, msg)
261 end
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100262..
263
264.. js:function:: core.Info(msg)
265
266 **context**: body, init, task, action, sample-fetch, converter
267
268 :param string msg: The log content.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +0100269 :see: :js:func:`core.log`
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100270
271.. code-block:: lua
272
Thierry FOURNIERdc595002015-12-21 11:13:52 +0100273 function Info(msg)
274 core.log(core.info, msg)
275 end
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100276..
277
278.. js:function:: core.Warning(msg)
279
280 **context**: body, init, task, action, sample-fetch, converter
281
282 :param string msg: The log content.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +0100283 :see: :js:func:`core.log`
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100284
285.. code-block:: lua
286
Thierry FOURNIERdc595002015-12-21 11:13:52 +0100287 function Warning(msg)
288 core.log(core.warning, msg)
289 end
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100290..
291
292.. js:function:: core.Alert(msg)
293
294 **context**: body, init, task, action, sample-fetch, converter
295
296 :param string msg: The log content.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +0100297 :see: :js:func:`core.log`
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100298
299.. code-block:: lua
300
Thierry FOURNIERdc595002015-12-21 11:13:52 +0100301 function Alert(msg)
302 core.log(core.alert, msg)
303 end
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100304..
305
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100306.. js:function:: core.add_acl(filename, key)
307
308 **context**: init, task, action, sample-fetch, converter
309
310 Add the ACL *key* in the ACLs list referenced by the file *filename*.
311
312 :param string filename: the filename that reference the ACL entries.
313 :param string key: the key which will be added.
314
315.. js:function:: core.del_acl(filename, key)
316
317 **context**: init, task, action, sample-fetch, converter
318
319 Delete the ACL entry referenced by the key *key* in the list of ACLs
320 referenced by *filename*.
321
322 :param string filename: the filename that reference the ACL entries.
323 :param string key: the key which will be deleted.
324
325.. js:function:: core.del_map(filename, key)
326
327 **context**: init, task, action, sample-fetch, converter
328
329 Delete the map entry indexed with the specified key in the list of maps
330 referenced by his filename.
331
332 :param string filename: the filename that reference the map entries.
333 :param string key: the key which will be deleted.
334
Thierry Fourniereea77c02016-03-18 08:47:13 +0100335.. js:function:: core.get_info()
336
337 **context**: body, init, task, action, sample-fetch, converter
338
Ilya Shipitsin2075ca82020-03-06 23:22:22 +0500339 Returns HAProxy core information. We can found information like the uptime,
Thierry Fourniereea77c02016-03-18 08:47:13 +0100340 the pid, memory pool usage, tasks number, ...
341
Bertrand Jacquin874a35c2018-09-10 21:26:07 +0100342 These information are also returned by the management socket via the command
343 "show info". See the management socket documentation for more information
Thierry Fourniereea77c02016-03-18 08:47:13 +0100344 about the content of these variables.
345
346 :returns: an array of values.
347
Thierry Fournierb1f46562016-01-21 09:46:15 +0100348.. js:function:: core.now()
349
350 **context**: body, init, task, action
351
352 This function returns the current time. The time returned is fixed by the
Bertrand Jacquin874a35c2018-09-10 21:26:07 +0100353 HAProxy core and assures than the hour will be monotonic and that the system
Thierry Fournierb1f46562016-01-21 09:46:15 +0100354 call 'gettimeofday' will not be called too. The time is refreshed between each
355 Lua execution or resume, so two consecutive call to the function "now" will
356 probably returns the same result.
357
Patrick Hemmerc6a1d712018-05-01 21:30:41 -0400358 :returns: a table which contains two entries "sec" and "usec". "sec"
Thierry Fournierb1f46562016-01-21 09:46:15 +0100359 contains the current at the epoch format, and "usec" contains the
360 current microseconds.
361
Thierry FOURNIERa78f0372016-12-14 19:04:41 +0100362.. js:function:: core.http_date(date)
363
364 **context**: body, init, task, action
365
Bertrand Jacquin874a35c2018-09-10 21:26:07 +0100366 This function take a string representing http date, and returns an integer
Thierry FOURNIERa78f0372016-12-14 19:04:41 +0100367 containing the corresponding date with a epoch format. A valid http date
368 me respect the format IMF, RFC850 or ASCTIME.
369
370 :param string date: a date http-date formatted
371 :returns: integer containing epoch date
372 :see: :js:func:`core.imf_date`.
373 :see: :js:func:`core.rfc850_date`.
374 :see: :js:func:`core.asctime_date`.
375 :see: https://tools.ietf.org/html/rfc7231#section-7.1.1.1
376
377.. js:function:: core.imf_date(date)
378
379 **context**: body, init, task, action
380
Bertrand Jacquin874a35c2018-09-10 21:26:07 +0100381 This function take a string representing IMF date, and returns an integer
Thierry FOURNIERa78f0372016-12-14 19:04:41 +0100382 containing the corresponding date with a epoch format.
383
384 :param string date: a date IMF formatted
385 :returns: integer containing epoch date
386 :see: https://tools.ietf.org/html/rfc7231#section-7.1.1.1
387
388 The IMF format is like this:
389
390.. code-block:: text
391
392 Sun, 06 Nov 1994 08:49:37 GMT
393..
394
395.. js:function:: core.rfc850_date(date)
396
397 **context**: body, init, task, action
398
Bertrand Jacquin874a35c2018-09-10 21:26:07 +0100399 This function take a string representing RFC850 date, and returns an integer
Thierry FOURNIERa78f0372016-12-14 19:04:41 +0100400 containing the corresponding date with a epoch format.
401
402 :param string date: a date RFC859 formatted
403 :returns: integer containing epoch date
404 :see: https://tools.ietf.org/html/rfc7231#section-7.1.1.1
405
406 The RFC850 format is like this:
407
408.. code-block:: text
409
410 Sunday, 06-Nov-94 08:49:37 GMT
411..
412
413.. js:function:: core.asctime_date(date)
414
415 **context**: body, init, task, action
416
Bertrand Jacquin874a35c2018-09-10 21:26:07 +0100417 This function take a string representing ASCTIME date, and returns an integer
Thierry FOURNIERa78f0372016-12-14 19:04:41 +0100418 containing the corresponding date with a epoch format.
419
420 :param string date: a date ASCTIME formatted
421 :returns: integer containing epoch date
422 :see: https://tools.ietf.org/html/rfc7231#section-7.1.1.1
423
424 The ASCTIME format is like this:
425
426.. code-block:: text
427
428 Sun Nov 6 08:49:37 1994
429..
430
431.. js:function:: core.rfc850_date(date)
432
433 **context**: body, init, task, action
434
Bertrand Jacquin874a35c2018-09-10 21:26:07 +0100435 This function take a string representing http date, and returns an integer
Thierry FOURNIERa78f0372016-12-14 19:04:41 +0100436 containing the corresponding date with a epoch format.
437
438 :param string date: a date http-date formatted
439
440.. js:function:: core.asctime_date(date)
441
442 **context**: body, init, task, action
443
Bertrand Jacquin874a35c2018-09-10 21:26:07 +0100444 This function take a string representing http date, and returns an integer
Thierry FOURNIERa78f0372016-12-14 19:04:41 +0100445 containing the corresponding date with a epoch format.
446
447 :param string date: a date http-date formatted
448
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100449.. js:function:: core.msleep(milliseconds)
450
451 **context**: body, init, task, action
452
453 The `core.msleep()` stops the Lua execution between specified milliseconds.
454
455 :param integer milliseconds: the required milliseconds.
456
Thierry Fournierf61aa632016-02-19 20:56:00 +0100457.. js:attribute:: core.proxies
458
459 **context**: body, init, task, action, sample-fetch, converter
460
Bertrand Jacquin874a35c2018-09-10 21:26:07 +0100461 Proxies is a table containing the list of all proxies declared in the
Patrick Hemmerc6a1d712018-05-01 21:30:41 -0400462 configuration file. The table is indexed by the proxy name, and each entry
463 of the proxies table is an object of type :ref:`proxy_class`.
464
Christopher Faulet1e9b1b62021-08-11 10:14:30 +0200465 .. warning::
466 if you have declared a frontend and backend with the same name, only one of
467 these are listed.
Thierry Fournierf61aa632016-02-19 20:56:00 +0100468
Thierry FOURNIERc5d11c62018-02-12 14:46:54 +0100469.. js:function:: core.register_action(name, actions, func [, nb_args])
Thierry FOURNIER8255a752015-09-23 21:03:35 +0200470
471 **context**: body
472
David Carlier61fdf8b2015-10-02 11:59:38 +0100473 Register a Lua function executed as action. All the registered action can be
Thierry FOURNIER8255a752015-09-23 21:03:35 +0200474 used in HAProxy with the prefix "lua.". An action gets a TXN object class as
475 input.
476
477 :param string name: is the name of the converter.
Willy Tarreau61add3c2015-09-28 15:39:10 +0200478 :param table actions: is a table of string describing the HAProxy actions who
Thierry FOURNIER8255a752015-09-23 21:03:35 +0200479 want to register to. The expected actions are 'tcp-req',
480 'tcp-res', 'http-req' or 'http-res'.
Thierry FOURNIERc5d11c62018-02-12 14:46:54 +0100481 :param integer nb_args: is the expected number of argument for the action.
482 By default the value is 0.
Thierry FOURNIER8255a752015-09-23 21:03:35 +0200483 :param function func: is the Lua function called to work as converter.
484
485 The prototype of the Lua function used as argument is:
486
487.. code-block:: lua
488
Thierry FOURNIERc5d11c62018-02-12 14:46:54 +0100489 function(txn [, arg1 [, arg2]])
Thierry FOURNIER8255a752015-09-23 21:03:35 +0200490..
491
Thierry FOURNIERdc595002015-12-21 11:13:52 +0100492 * **txn** (:ref:`txn_class`): this is a TXN object used for manipulating the
Thierry FOURNIER8255a752015-09-23 21:03:35 +0200493 current request or TCP stream.
494
Bertrand Jacquin874a35c2018-09-10 21:26:07 +0100495 * **argX**: this is argument provided through the HAProxy configuration file.
Thierry FOURNIERc5d11c62018-02-12 14:46:54 +0100496
Bertrand Jacquin874a35c2018-09-10 21:26:07 +0100497 Here, an example of action registration. The action just send an 'Hello world'
Thierry FOURNIER8255a752015-09-23 21:03:35 +0200498 in the logs.
499
500.. code-block:: lua
501
502 core.register_action("hello-world", { "tcp-req", "http-req" }, function(txn)
503 txn:Info("Hello world")
504 end)
505..
506
Willy Tarreau714f3452021-05-09 06:47:26 +0200507 This example code is used in HAProxy configuration like this:
Thierry FOURNIER8255a752015-09-23 21:03:35 +0200508
509::
510
511 frontend tcp_frt
512 mode tcp
513 tcp-request content lua.hello-world
514
515 frontend http_frt
516 mode http
517 http-request lua.hello-world
Thierry FOURNIERc5d11c62018-02-12 14:46:54 +0100518..
519
Bertrand Jacquin874a35c2018-09-10 21:26:07 +0100520 A second example using arguments
Thierry FOURNIERc5d11c62018-02-12 14:46:54 +0100521
522.. code-block:: lua
523
524 function hello_world(txn, arg)
525 txn:Info("Hello world for " .. arg)
526 end
527 core.register_action("hello-world", { "tcp-req", "http-req" }, hello_world, 2)
528..
Thierry FOURNIER8255a752015-09-23 21:03:35 +0200529
Willy Tarreau714f3452021-05-09 06:47:26 +0200530 This example code is used in HAProxy configuration like this:
Thierry FOURNIERc5d11c62018-02-12 14:46:54 +0100531
532::
533
534 frontend tcp_frt
535 mode tcp
536 tcp-request content lua.hello-world everybody
537..
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100538.. js:function:: core.register_converters(name, func)
539
540 **context**: body
541
David Carlier61fdf8b2015-10-02 11:59:38 +0100542 Register a Lua function executed as converter. All the registered converters
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100543 can be used in HAProxy with the prefix "lua.". An converter get a string as
544 input and return a string as output. The registered function can take up to 9
545 values as 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 converter.
549
550 The prototype of the Lua function used as argument is:
551
552.. code-block:: lua
553
554 function(str, [p1 [, p2 [, ... [, p5]]]])
555..
556
557 * **str** (*string*): this is the input value automatically converted in
558 string.
559 * **p1** .. **p5** (*string*): this is a list of string arguments declared in
Bertrand Jacquin874a35c2018-09-10 21:26:07 +0100560 the HAProxy configuration file. The number of arguments doesn't exceed 5.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100561 The order and the nature of these is conventionally choose by the
Bertrand Jacquin874a35c2018-09-10 21:26:07 +0100562 developer.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100563
564.. js:function:: core.register_fetches(name, func)
565
566 **context**: body
567
David Carlier61fdf8b2015-10-02 11:59:38 +0100568 Register a Lua function executed as sample fetch. All the registered sample
Bertrand Jacquin874a35c2018-09-10 21:26:07 +0100569 fetch can be used in HAProxy with the prefix "lua.". A Lua sample fetch
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100570 return a string as output. The registered function can take up to 9 values as
571 parameter. All the value are strings.
572
573 :param string name: is the name of the converter.
574 :param function func: is the Lua function called to work as sample fetch.
575
576 The prototype of the Lua function used as argument is:
577
578.. code-block:: lua
579
580 string function(txn, [p1 [, p2 [, ... [, p5]]]])
581..
582
Thierry FOURNIERdc595002015-12-21 11:13:52 +0100583 * **txn** (:ref:`txn_class`): this is the txn object associated with the current
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100584 request.
585 * **p1** .. **p5** (*string*): this is a list of string arguments declared in
Bertrand Jacquin874a35c2018-09-10 21:26:07 +0100586 the HAProxy configuration file. The number of arguments doesn't exceed 5.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100587 The order and the nature of these is conventionally choose by the
Bertrand Jacquin874a35c2018-09-10 21:26:07 +0100588 developer.
589 * **Returns**: A string containing some data, or nil if the value cannot be
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100590 returned now.
591
592 lua example code:
593
594.. code-block:: lua
595
596 core.register_fetches("hello", function(txn)
597 return "hello"
598 end)
599..
600
601 HAProxy example configuration:
602
603::
604
605 frontend example
606 http-request redirect location /%[lua.hello]
607
Christopher Faulet5a2c6612021-08-15 20:35:25 +0200608.. js:function:: core.register_filter(name, Flt, func)
609
610 **context**: body
611
612 Register a Lua function used to declare a filter. All the registered filters
613 can by used in HAProxy with the prefix "lua.".
614
615 :param string name: is the name of the filter.
616 :param table Flt: is a Lua class containing the filter definition (id, flags,
617 callbacks).
618 :param function func: is the Lua function called to create the Lua filter.
619
620 The prototype of the Lua function used as argument is:
621
622.. code-block:: lua
623
624 function(flt, args)
625..
626
627 * **flt** : Is a filter object based on the class provided in
628 :js:func:`core.register_filter()` function.
629
630 * **args**: Is a table of strings containing all arguments provided through
631 the HAProxy configuration file, on the filter line.
632
633 It must return the filter to use or nil to ignore it. Here, an example of
634 filter registration.
635
636.. code-block:: lua
637
638 core.register_filter("my-filter", MyFilter, function(flt, args)
639 flt.args = args -- Save arguments
640 return flt
641 end)
642..
643
644 This example code is used in HAProxy configuration like this:
645
646::
647
648 frontend http
649 mode http
650 filter lua.my-filter arg1 arg2 arg3
651..
652
653 :see: :js:class:`Filter`
654
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +0200655.. js:function:: core.register_service(name, mode, func)
656
657 **context**: body
658
David Carlier61fdf8b2015-10-02 11:59:38 +0100659 Register a Lua function executed as a service. All the registered service can
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +0200660 be used in HAProxy with the prefix "lua.". A service gets an object class as
661 input according with the required mode.
662
663 :param string name: is the name of the converter.
Willy Tarreau61add3c2015-09-28 15:39:10 +0200664 :param string mode: is string describing the required mode. Only 'tcp' or
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +0200665 'http' are allowed.
666 :param function func: is the Lua function called to work as converter.
667
668 The prototype of the Lua function used as argument is:
669
670.. code-block:: lua
671
672 function(applet)
673..
674
Thierry FOURNIERdc595002015-12-21 11:13:52 +0100675 * **applet** *applet* will be a :ref:`applettcp_class` or a
676 :ref:`applethttp_class`. It depends the type of registered applet. An applet
677 registered with the 'http' value for the *mode* parameter will gets a
678 :ref:`applethttp_class`. If the *mode* value is 'tcp', the applet will gets
679 a :ref:`applettcp_class`.
680
Christopher Faulet1e9b1b62021-08-11 10:14:30 +0200681 .. warning::
682 Applets of type 'http' cannot be called from 'tcp-*' rulesets. Only the
683 'http-*' rulesets are authorized, this means that is not possible to call
684 an HTTP applet from a proxy in tcp mode. Applets of type 'tcp' can be
685 called from anywhere.
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +0200686
Bertrand Jacquin874a35c2018-09-10 21:26:07 +0100687 Here, an example of service registration. The service just send an 'Hello world'
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +0200688 as an http response.
689
690.. code-block:: lua
691
Pieter Baauw4d7f7662015-11-08 16:38:08 +0100692 core.register_service("hello-world", "http", function(applet)
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +0200693 local response = "Hello World !"
694 applet:set_status(200)
Pieter Baauw2dcb9bc2015-10-01 22:47:12 +0200695 applet:add_header("content-length", string.len(response))
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +0200696 applet:add_header("content-type", "text/plain")
Pieter Baauw2dcb9bc2015-10-01 22:47:12 +0200697 applet:start_response()
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +0200698 applet:send(response)
699 end)
700..
701
Willy Tarreau714f3452021-05-09 06:47:26 +0200702 This example code is used in HAProxy configuration like this:
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +0200703
704::
705
706 frontend example
707 http-request use-service lua.hello-world
708
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100709.. js:function:: core.register_init(func)
710
711 **context**: body
712
713 Register a function executed after the configuration parsing. This is useful
714 to check any parameters.
715
Pieter Baauw4d7f7662015-11-08 16:38:08 +0100716 :param function func: is the Lua function called to work as initializer.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100717
718 The prototype of the Lua function used as argument is:
719
720.. code-block:: lua
721
722 function()
723..
724
725 It takes no input, and no output is expected.
726
727.. js:function:: core.register_task(func)
728
729 **context**: body, init, task, action, sample-fetch, converter
730
731 Register and start independent task. The task is started when the HAProxy
732 main scheduler starts. For example this type of tasks can be executed to
Thierry FOURNIER2e4893c2015-03-18 13:37:27 +0100733 perform complex health checks.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100734
Pieter Baauw4d7f7662015-11-08 16:38:08 +0100735 :param function func: is the Lua function called to work as initializer.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100736
737 The prototype of the Lua function used as argument is:
738
739.. code-block:: lua
740
741 function()
742..
743
744 It takes no input, and no output is expected.
745
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +0100746.. js:function:: core.register_cli([path], usage, func)
747
748 **context**: body
749
750 Register and start independent task. The task is started when the HAProxy
751 main scheduler starts. For example this type of tasks can be executed to
752 perform complex health checks.
753
754 :param array path: is the sequence of word for which the cli execute the Lua
755 binding.
756 :param string usage: is the usage message displayed in the help.
757 :param function func: is the Lua function called to handle the CLI commands.
758
759 The prototype of the Lua function used as argument is:
760
761.. code-block:: lua
762
763 function(AppletTCP, [arg1, [arg2, [...]]])
764..
765
766 I/O are managed with the :ref:`applettcp_class` object. Args are given as
Bertrand Jacquin874a35c2018-09-10 21:26:07 +0100767 parameter. The args embed the registered path. If the path is declared like
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +0100768 this:
769
770.. code-block:: lua
771
772 core.register_cli({"show", "ssl", "stats"}, "Display SSL stats..", function(applet, arg1, arg2, arg3, arg4, arg5)
773 end)
774..
775
776 And we execute this in the prompt:
777
778.. code-block:: text
779
780 > prompt
781 > show ssl stats all
782..
783
Bertrand Jacquin874a35c2018-09-10 21:26:07 +0100784 Then, arg1, arg2 and arg3 will contains respectively "show", "ssl" and "stats".
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +0100785 arg4 will contain "all". arg5 contains nil.
786
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100787.. js:function:: core.set_nice(nice)
788
789 **context**: task, action, sample-fetch, converter
790
791 Change the nice of the current task or current session.
Thierry FOURNIER2e4893c2015-03-18 13:37:27 +0100792
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100793 :param integer nice: the nice value, it must be between -1024 and 1024.
794
795.. js:function:: core.set_map(filename, key, value)
796
797 **context**: init, task, action, sample-fetch, converter
798
Bertrand Jacquin874a35c2018-09-10 21:26:07 +0100799 Set the value *value* associated to the key *key* in the map referenced by
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100800 *filename*.
801
Thierry FOURNIERdc595002015-12-21 11:13:52 +0100802 :param string filename: the Map reference
803 :param string key: the key to set or replace
804 :param string value: the associated value
805
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100806.. js:function:: core.sleep(int seconds)
807
808 **context**: body, init, task, action
809
810 The `core.sleep()` functions stop the Lua execution between specified seconds.
811
812 :param integer seconds: the required seconds.
813
814.. js:function:: core.tcp()
815
816 **context**: init, task, action
817
818 This function returns a new object of a *socket* class.
819
Thierry FOURNIERdc595002015-12-21 11:13:52 +0100820 :returns: A :ref:`socket_class` object.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100821
Thierry Fournier1de16592016-01-27 09:49:07 +0100822.. js:function:: core.concat()
823
824 **context**: body, init, task, action, sample-fetch, converter
825
Bertrand Jacquin874a35c2018-09-10 21:26:07 +0100826 This function returns a new concat object.
Thierry Fournier1de16592016-01-27 09:49:07 +0100827
828 :returns: A :ref:`concat_class` object.
829
Thierry FOURNIER0a99b892015-08-26 00:14:17 +0200830.. js:function:: core.done(data)
831
832 **context**: body, init, task, action, sample-fetch, converter
833
834 :param any data: Return some data for the caller. It is useful with
835 sample-fetches and sample-converters.
836
837 Immediately stops the current Lua execution and returns to the caller which
838 may be a sample fetch, a converter or an action and returns the specified
Thierry Fournier4234dbd2020-11-28 13:18:23 +0100839 value (ignored for actions and init). It is used when the LUA process finishes
840 its work and wants to give back the control to HAProxy without executing the
Thierry FOURNIER0a99b892015-08-26 00:14:17 +0200841 remaining code. It can be seen as a multi-level "return".
842
Thierry FOURNIER486f5a02015-03-16 15:13:03 +0100843.. js:function:: core.yield()
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100844
845 **context**: task, action, sample-fetch, converter
846
847 Give back the hand at the HAProxy scheduler. It is used when the LUA
848 processing consumes a lot of processing time.
849
Thierry FOURNIER / OZON.IO62fec752016-11-10 20:38:11 +0100850.. js:function:: core.parse_addr(address)
851
852 **context**: body, init, task, action, sample-fetch, converter
853
854 :param network: is a string describing an ipv4 or ipv6 address and optionally
855 its network length, like this: "127.0.0.1/8" or "aaaa::1234/32".
856 :returns: a userdata containing network or nil if an error occurs.
857
Bertrand Jacquin874a35c2018-09-10 21:26:07 +0100858 Parse ipv4 or ipv6 addresses and its facultative associated network.
Thierry FOURNIER / OZON.IO62fec752016-11-10 20:38:11 +0100859
860.. js:function:: core.match_addr(addr1, addr2)
861
862 **context**: body, init, task, action, sample-fetch, converter
863
864 :param addr1: is an address created with "core.parse_addr".
865 :param addr2: is an address created with "core.parse_addr".
Bertrand Jacquin874a35c2018-09-10 21:26:07 +0100866 :returns: boolean, true if the network of the addresses match, else returns
Thierry FOURNIER / OZON.IO62fec752016-11-10 20:38:11 +0100867 false.
868
Ilya Shipitsin2075ca82020-03-06 23:22:22 +0500869 Match two networks. For example "127.0.0.1/32" matches "127.0.0.0/8". The order
Thierry FOURNIER / OZON.IO62fec752016-11-10 20:38:11 +0100870 of network is not important.
871
Thierry FOURNIER / OZON.IO8a1027a2016-11-24 20:48:38 +0100872.. js:function:: core.tokenize(str, separators [, noblank])
873
874 **context**: body, init, task, action, sample-fetch, converter
875
876 This function is useful for tokenizing an entry, or splitting some messages.
877 :param string str: The string which will be split.
878 :param string separators: A string containing a list of separators.
879 :param boolean noblank: Ignore empty entries.
880 :returns: an array of string.
881
882 For example:
883
884.. code-block:: lua
885
886 local array = core.tokenize("This function is useful, for tokenizing an entry.", "., ", true)
887 print_r(array)
888..
889
890 Returns this array:
891
892.. code-block:: text
893
894 (table) table: 0x21c01e0 [
895 1: (string) "This"
896 2: (string) "function"
897 3: (string) "is"
898 4: (string) "useful"
899 5: (string) "for"
900 6: (string) "tokenizing"
901 7: (string) "an"
902 8: (string) "entry"
903 ]
904..
905
Thierry Fournierf61aa632016-02-19 20:56:00 +0100906.. _proxy_class:
907
908Proxy class
909============
910
911.. js:class:: Proxy
912
913 This class provides a way for manipulating proxy and retrieving information
914 like statistics.
915
Thierry FOURNIER817e7592017-07-24 14:35:04 +0200916.. js:attribute:: Proxy.name
917
918 Contain the name of the proxy.
919
Baptiste Assmann46c72552017-10-26 21:51:58 +0200920.. js:attribute:: Proxy.uuid
921
922 Contain the unique identifier of the proxy.
923
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +0100924.. js:attribute:: Proxy.servers
925
Patrick Hemmerc6a1d712018-05-01 21:30:41 -0400926 Contain a table with the attached servers. The table is indexed by server
927 name, and each server entry is an object of type :ref:`server_class`.
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +0100928
Adis Nezirovic8878f8e2018-07-13 12:18:33 +0200929.. js:attribute:: Proxy.stktable
930
931 Contains a stick table object attached to the proxy.
932
Thierry Fournierff480422016-02-25 08:36:46 +0100933.. js:attribute:: Proxy.listeners
934
Patrick Hemmerc6a1d712018-05-01 21:30:41 -0400935 Contain a table with the attached listeners. The table is indexed by listener
936 name, and each each listeners entry is an object of type
937 :ref:`listener_class`.
Thierry Fournierff480422016-02-25 08:36:46 +0100938
Thierry Fournierf61aa632016-02-19 20:56:00 +0100939.. js:function:: Proxy.pause(px)
940
941 Pause the proxy. See the management socket documentation for more information.
942
943 :param class_proxy px: A :ref:`proxy_class` which indicates the manipulated
944 proxy.
945
946.. js:function:: Proxy.resume(px)
947
948 Resume the proxy. See the management socket documentation for more
949 information.
950
951 :param class_proxy px: A :ref:`proxy_class` which indicates the manipulated
952 proxy.
953
954.. js:function:: Proxy.stop(px)
955
956 Stop the proxy. See the management socket documentation for more information.
957
958 :param class_proxy px: A :ref:`proxy_class` which indicates the manipulated
959 proxy.
960
961.. js:function:: Proxy.shut_bcksess(px)
962
963 Kill the session attached to a backup server. See the management socket
964 documentation for more information.
965
966 :param class_proxy px: A :ref:`proxy_class` which indicates the manipulated
967 proxy.
968
969.. js:function:: Proxy.get_cap(px)
970
971 Returns a string describing the capabilities of the proxy.
972
973 :param class_proxy px: A :ref:`proxy_class` which indicates the manipulated
974 proxy.
975 :returns: a string "frontend", "backend", "proxy" or "ruleset".
976
977.. js:function:: Proxy.get_mode(px)
978
979 Returns a string describing the mode of the current proxy.
980
981 :param class_proxy px: A :ref:`proxy_class` which indicates the manipulated
982 proxy.
983 :returns: a string "tcp", "http", "health" or "unknown"
984
985.. js:function:: Proxy.get_stats(px)
986
Bertrand Jacquin874a35c2018-09-10 21:26:07 +0100987 Returns a table containing the proxy statistics. The statistics returned are
Thierry Fournierf61aa632016-02-19 20:56:00 +0100988 not the same if the proxy is frontend or a backend.
989
990 :param class_proxy px: A :ref:`proxy_class` which indicates the manipulated
991 proxy.
Patrick Hemmerc6a1d712018-05-01 21:30:41 -0400992 :returns: a key/value table containing stats
Thierry Fournierf61aa632016-02-19 20:56:00 +0100993
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +0100994.. _server_class:
995
996Server class
997============
998
Patrick Hemmerc6a1d712018-05-01 21:30:41 -0400999.. js:class:: Server
1000
1001 This class provides a way for manipulating servers and retrieving information.
1002
Patrick Hemmera62ae7e2018-04-29 14:23:48 -04001003.. js:attribute:: Server.name
1004
1005 Contain the name of the server.
1006
1007.. js:attribute:: Server.puid
1008
1009 Contain the proxy unique identifier of the server.
1010
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001011.. js:function:: Server.is_draining(sv)
1012
Patrick Hemmerc6a1d712018-05-01 21:30:41 -04001013 Return true if the server is currently draining sticky connections.
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001014
1015 :param class_server sv: A :ref:`server_class` which indicates the manipulated
1016 server.
1017 :returns: a boolean
1018
Patrick Hemmer32d539f2018-04-29 14:25:46 -04001019.. js:function:: Server.set_maxconn(sv, weight)
1020
1021 Dynamically change the maximum connections of the server. See the management
1022 socket documentation for more information about the format of the string.
1023
1024 :param class_server sv: A :ref:`server_class` which indicates the manipulated
1025 server.
1026 :param string maxconn: A string describing the server maximum connections.
1027
1028.. js:function:: Server.get_maxconn(sv, weight)
1029
1030 This function returns an integer representing the server maximum connections.
1031
1032 :param class_server sv: A :ref:`server_class` which indicates the manipulated
1033 server.
1034 :returns: an integer.
1035
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001036.. js:function:: Server.set_weight(sv, weight)
1037
Patrick Hemmerc6a1d712018-05-01 21:30:41 -04001038 Dynamically change the weight of the server. See the management socket
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001039 documentation for more information about the format of the string.
1040
1041 :param class_server sv: A :ref:`server_class` which indicates the manipulated
1042 server.
1043 :param string weight: A string describing the server weight.
1044
1045.. js:function:: Server.get_weight(sv)
1046
Patrick Hemmerc6a1d712018-05-01 21:30:41 -04001047 This function returns an integer representing the server weight.
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001048
1049 :param class_server sv: A :ref:`server_class` which indicates the manipulated
1050 server.
1051 :returns: an integer.
1052
Joseph C. Sible49bbf522020-05-04 22:20:32 -04001053.. js:function:: Server.set_addr(sv, addr[, port])
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001054
Patrick Hemmerc6a1d712018-05-01 21:30:41 -04001055 Dynamically change the address of the server. See the management socket
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001056 documentation for more information about the format of the string.
1057
1058 :param class_server sv: A :ref:`server_class` which indicates the manipulated
1059 server.
Patrick Hemmerc6a1d712018-05-01 21:30:41 -04001060 :param string addr: A string describing the server address.
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001061
1062.. js:function:: Server.get_addr(sv)
1063
Patrick Hemmerc6a1d712018-05-01 21:30:41 -04001064 Returns a string describing the address of the server.
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001065
1066 :param class_server sv: A :ref:`server_class` which indicates the manipulated
1067 server.
1068 :returns: A string
1069
1070.. js:function:: Server.get_stats(sv)
1071
1072 Returns server statistics.
1073
1074 :param class_server sv: A :ref:`server_class` which indicates the manipulated
1075 server.
Patrick Hemmerc6a1d712018-05-01 21:30:41 -04001076 :returns: a key/value table containing stats
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001077
1078.. js:function:: Server.shut_sess(sv)
1079
1080 Shutdown all the sessions attached to the server. See the management socket
1081 documentation for more information about this function.
1082
1083 :param class_server sv: A :ref:`server_class` which indicates the manipulated
1084 server.
1085
1086.. js:function:: Server.set_drain(sv)
1087
1088 Drain sticky sessions. See the management socket documentation for more
1089 information about this function.
1090
1091 :param class_server sv: A :ref:`server_class` which indicates the manipulated
1092 server.
1093
1094.. js:function:: Server.set_maint(sv)
1095
1096 Set maintenance mode. See the management socket documentation for more
1097 information about this function.
1098
1099 :param class_server sv: A :ref:`server_class` which indicates the manipulated
1100 server.
1101
1102.. js:function:: Server.set_ready(sv)
1103
1104 Set normal mode. See the management socket documentation for more information
1105 about this function.
1106
1107 :param class_server sv: A :ref:`server_class` which indicates the manipulated
1108 server.
1109
1110.. js:function:: Server.check_enable(sv)
1111
1112 Enable health checks. See the management socket documentation for more
1113 information about this function.
1114
1115 :param class_server sv: A :ref:`server_class` which indicates the manipulated
1116 server.
1117
1118.. js:function:: Server.check_disable(sv)
1119
1120 Disable health checks. See the management socket documentation for more
1121 information about this function.
1122
1123 :param class_server sv: A :ref:`server_class` which indicates the manipulated
1124 server.
1125
1126.. js:function:: Server.check_force_up(sv)
1127
1128 Force health-check up. See the management socket documentation for more
1129 information about this function.
1130
1131 :param class_server sv: A :ref:`server_class` which indicates the manipulated
1132 server.
1133
1134.. js:function:: Server.check_force_nolb(sv)
1135
1136 Force health-check nolb mode. See the management socket documentation for more
1137 information about this function.
1138
1139 :param class_server sv: A :ref:`server_class` which indicates the manipulated
1140 server.
1141
1142.. js:function:: Server.check_force_down(sv)
1143
1144 Force health-check down. See the management socket documentation for more
1145 information about this function.
1146
1147 :param class_server sv: A :ref:`server_class` which indicates the manipulated
1148 server.
1149
1150.. js:function:: Server.agent_enable(sv)
1151
1152 Enable agent check. See the management socket documentation for more
1153 information about this function.
1154
1155 :param class_server sv: A :ref:`server_class` which indicates the manipulated
1156 server.
1157
1158.. js:function:: Server.agent_disable(sv)
1159
1160 Disable agent check. See the management socket documentation for more
1161 information about this function.
1162
1163 :param class_server sv: A :ref:`server_class` which indicates the manipulated
1164 server.
1165
1166.. js:function:: Server.agent_force_up(sv)
1167
1168 Force agent check up. See the management socket documentation for more
1169 information about this function.
1170
1171 :param class_server sv: A :ref:`server_class` which indicates the manipulated
1172 server.
1173
1174.. js:function:: Server.agent_force_down(sv)
1175
1176 Force agent check down. See the management socket documentation for more
1177 information about this function.
1178
1179 :param class_server sv: A :ref:`server_class` which indicates the manipulated
1180 server.
1181
Thierry Fournierff480422016-02-25 08:36:46 +01001182.. _listener_class:
1183
1184Listener class
1185==============
1186
1187.. js:function:: Listener.get_stats(ls)
1188
1189 Returns server statistics.
1190
1191 :param class_listener ls: A :ref:`listener_class` which indicates the
1192 manipulated listener.
Patrick Hemmerc6a1d712018-05-01 21:30:41 -04001193 :returns: a key/value table containing stats
Thierry Fournierff480422016-02-25 08:36:46 +01001194
Thierry Fournier1de16592016-01-27 09:49:07 +01001195.. _concat_class:
1196
1197Concat class
1198============
1199
1200.. js:class:: Concat
1201
1202 This class provides a fast way for string concatenation. The way using native
1203 Lua concatenation like the code below is slow for some reasons.
1204
1205.. code-block:: lua
1206
1207 str = "string1"
1208 str = str .. ", string2"
1209 str = str .. ", string3"
1210..
1211
1212 For each concatenation, Lua:
1213 * allocate memory for the result,
Ilya Shipitsin2075ca82020-03-06 23:22:22 +05001214 * catenate the two string copying the strings in the new memory block,
Bertrand Jacquin874a35c2018-09-10 21:26:07 +01001215 * free the old memory block containing the string which is no longer used.
Thierry Fournier1de16592016-01-27 09:49:07 +01001216 This process does many memory move, allocation and free. In addition, the
Bertrand Jacquin874a35c2018-09-10 21:26:07 +01001217 memory is not really freed, it is just mark mark as unused and wait for the
Thierry Fournier1de16592016-01-27 09:49:07 +01001218 garbage collector.
1219
Bertrand Jacquin874a35c2018-09-10 21:26:07 +01001220 The Concat class provide an alternative way to concatenate strings. It uses
Thierry Fournier1de16592016-01-27 09:49:07 +01001221 the internal Lua mechanism (it does not allocate memory), but it doesn't copy
1222 the data more than once.
1223
1224 On my computer, the following loops spends 0.2s for the Concat method and
1225 18.5s for the pure Lua implementation. So, the Concat class is about 1000x
1226 faster than the embedded solution.
1227
1228.. code-block:: lua
1229
1230 for j = 1, 100 do
1231 c = core.concat()
1232 for i = 1, 20000 do
1233 c:add("#####")
1234 end
1235 end
1236..
1237
1238.. code-block:: lua
1239
1240 for j = 1, 100 do
1241 c = ""
1242 for i = 1, 20000 do
1243 c = c .. "#####"
1244 end
1245 end
1246..
1247
1248.. js:function:: Concat.add(concat, string)
1249
1250 This function adds a string to the current concatenated string.
1251
1252 :param class_concat concat: A :ref:`concat_class` which contains the currently
Ilya Shipitsin2075ca82020-03-06 23:22:22 +05001253 built string.
Bertrand Jacquin874a35c2018-09-10 21:26:07 +01001254 :param string string: A new string to concatenate to the current built
Thierry Fournier1de16592016-01-27 09:49:07 +01001255 string.
1256
1257.. js:function:: Concat.dump(concat)
1258
Bertrand Jacquin874a35c2018-09-10 21:26:07 +01001259 This function returns the concatenated string.
Thierry Fournier1de16592016-01-27 09:49:07 +01001260
1261 :param class_concat concat: A :ref:`concat_class` which contains the currently
Ilya Shipitsin2075ca82020-03-06 23:22:22 +05001262 built string.
Thierry Fournier1de16592016-01-27 09:49:07 +01001263 :returns: the concatenated string
1264
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001265.. _fetches_class:
1266
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001267Fetches class
1268=============
1269
1270.. js:class:: Fetches
1271
1272 This class contains a lot of internal HAProxy sample fetches. See the
Thierry FOURNIER2e4893c2015-03-18 13:37:27 +01001273 HAProxy "configuration.txt" documentation for more information about her
Bertrand Jacquin874a35c2018-09-10 21:26:07 +01001274 usage. They are the chapters 7.3.2 to 7.3.6.
Thierry FOURNIER2e4893c2015-03-18 13:37:27 +01001275
Christopher Faulet1e9b1b62021-08-11 10:14:30 +02001276 .. warning::
1277 some sample fetches are not available in some context. These limitations
1278 are specified in this documentation when they're useful.
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001279
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01001280 :see: :js:attr:`TXN.f`
1281 :see: :js:attr:`TXN.sf`
Thierry FOURNIER2e4893c2015-03-18 13:37:27 +01001282
1283 Fetches are useful for:
1284
1285 * get system time,
1286 * get environment variable,
1287 * get random numbers,
1288 * known backend status like the number of users in queue or the number of
1289 connections established,
1290 * client information like ip source or destination,
1291 * deal with stick tables,
Ilya Shipitsin2075ca82020-03-06 23:22:22 +05001292 * Established SSL information,
Thierry FOURNIER2e4893c2015-03-18 13:37:27 +01001293 * HTTP information like headers or method.
1294
1295.. code-block:: lua
1296
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001297 function action(txn)
1298 -- Get source IP
1299 local clientip = txn.f:src()
1300 end
Thierry FOURNIER2e4893c2015-03-18 13:37:27 +01001301..
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001302
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001303.. _converters_class:
1304
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001305Converters class
1306================
1307
1308.. js:class:: Converters
1309
1310 This class contains a lot of internal HAProxy sample converters. See the
Thierry FOURNIER2e4893c2015-03-18 13:37:27 +01001311 HAProxy documentation "configuration.txt" for more information about her
1312 usage. Its the chapter 7.3.1.
1313
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01001314 :see: :js:attr:`TXN.c`
1315 :see: :js:attr:`TXN.sc`
Thierry FOURNIER2e4893c2015-03-18 13:37:27 +01001316
1317 Converters provides statefull transformation. They are useful for:
1318
1319 * converting input to base64,
1320 * applying hash on input string (djb2, crc32, sdbm, wt6),
1321 * format date,
1322 * json escape,
Pieter Baauw4d7f7662015-11-08 16:38:08 +01001323 * extracting preferred language comparing two lists,
Thierry FOURNIER2e4893c2015-03-18 13:37:27 +01001324 * turn to lower or upper chars,
1325 * deal with stick tables.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001326
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001327.. _channel_class:
1328
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001329Channel class
1330=============
1331
1332.. js:class:: Channel
1333
Christopher Faulet5a2c6612021-08-15 20:35:25 +02001334 **context**: action, sample-fetch, convert, filter
1335
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001336 HAProxy uses two buffers for the processing of the requests. The first one is
1337 used with the request data (from the client to the server) and the second is
1338 used for the response data (from the server to the client).
1339
1340 Each buffer contains two types of data. The first type is the incoming data
1341 waiting for a processing. The second part is the outgoing data already
1342 processed. Usually, the incoming data is processed, after it is tagged as
1343 outgoing data, and finally it is sent. The following functions provides tools
1344 for manipulating these data in a buffer.
1345
1346 The following diagram shows where the channel class function are applied.
1347
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001348 .. image:: _static/channel.png
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001349
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001350 .. warning::
1351 It is not possible to read from the response in request action, and it is
1352 not possible to read for the request channel in response action.
Christopher Faulet09530392021-06-14 11:43:18 +02001353
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001354 .. warning::
1355 It is forbidden to alter the Channels buffer from HTTP contexts. So only
1356 :js:func:`Channel.input`, :js:func:`Channel.output`,
1357 :js:func:`Channel.may_recv`, :js:func:`Channel.is_full` and
1358 :js:func:`Channel.is_resp` can be called from an HTTP conetext.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001359
Christopher Faulet5a2c6612021-08-15 20:35:25 +02001360 All the functions provided by this class are available in the
1361 **sample-fetches**, **actions** and **filters** contexts. For **filters**,
1362 incoming data (offset and length) are relative to the filter. Some functions
1363 may yield, by only for **actions**. Yield is not possible for
1364 **sample-fetches**, **converters** and **filters**.
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001365
1366.. js:function:: Channel.append(channel, string)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001367
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001368 This function copies the string **string** at the end of incoming data of the
1369 channel buffer. The function returns the copied length on success or -1 if
1370 data cannot be copied.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001371
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001372 Same that :js:func:`Channel.insert(channel, string, channel:input())`.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001373
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001374 :param class_channel channel: The manipulated Channel.
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001375 :param string string: The data to copied into incoming data.
1376 :returns: an integer containing the amount of bytes copied or -1.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001377
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001378.. js:function:: Channel.data(channel [, offset [, length]])
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001379
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001380 This function returns **length** bytes of incoming data from the channel
1381 buffer, starting at the offset **offset**. The data are not removed from the
1382 buffer.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001383
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001384 By default, if no length is provided, all incoming data found, starting at the
1385 given offset, are returned. If **length** is set to -1, the function tries to
Christopher Faulet5a2c6612021-08-15 20:35:25 +02001386 retrieve a maximum of data and, if called by an action, it yields if
1387 necessary. It also waits for more data if the requested length exceeds the
1388 available amount of incoming data. Do not providing an offset is the same that
Ilya Shipitsinff0f2782021-08-22 22:18:07 +05001389 setting it to 0. A positive offset is relative to the beginning of incoming
Christopher Faulet5a2c6612021-08-15 20:35:25 +02001390 data of the channel buffer while negative offset is relative to their end.
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001391
1392 If there is no incoming data and the channel can't receive more data, a 'nil'
1393 value is returned.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001394
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001395 :param class_channel channel: The manipulated Channel.
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001396 :param integer offset: *optional* The offset in incoming data to start to get
1397 data. 0 by default. May be negative to be relative to
1398 the end of incoming data.
1399 :param integer length: *optional* The expected length of data to retrieve. All
1400 incoming data by default. May be set to -1 to get a
1401 maximum of data.
1402 :returns: a string containing the data found or nil.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001403
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001404.. js:function:: Channel.forward(channel, length)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001405
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001406 This function forwards **length** bytes of data from the channel buffer. If
Christopher Faulet5a2c6612021-08-15 20:35:25 +02001407 the requested length exceeds the available amount of incoming data, and if
1408 called by an action, the function yields, waiting for more data to forward. It
1409 returns the amount of data forwarded.
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001410
1411 :param class_channel channel: The manipulated Channel.
1412 :param integer int: The amount of data to forward.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001413
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001414.. js:function:: Channel.input(channel)
1415
Christopher Faulet5a2c6612021-08-15 20:35:25 +02001416 This function returns the length of incoming data in the channel buffer. When
1417 called by a filter, this value is relative to the filter.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001418
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001419 :param class_channel channel: The manipulated Channel.
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001420 :returns: an integer containing the amount of available bytes.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001421
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001422.. js:function:: Channel.insert(channel, string [, offset])
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001423
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001424 This function copies the string **string** at the offset **offset** in
1425 incoming data of the channel buffer. The function returns the copied length on
1426 success or -1 if data cannot be copied.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001427
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001428 By default, if no offset is provided, the string is copied in front of
Ilya Shipitsinff0f2782021-08-22 22:18:07 +05001429 incoming data. A positive offset is relative to the beginning of incoming data
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001430 of the channel buffer while negative offset is relative to their end.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001431
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001432 :param class_channel channel: The manipulated Channel.
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001433 :param string string: The data to copied into incoming data.
1434 :param integer offset: *optional* The offset in incomding data where to copied
1435 data. 0 by default. May be negative to be relative to
1436 the end of incoming data.
Pieter Baauw4d7f7662015-11-08 16:38:08 +01001437 :returns: an integer containing the amount of bytes copied or -1.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001438
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001439.. js:function:: Channel.is_full(channel)
1440
1441 This function returns true if the channel buffer is full.
1442
Christopher Faulet5a2c6612021-08-15 20:35:25 +02001443 :param class_channel channel: The manipulated Channel.
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001444 :returns: a boolean
1445
1446.. js:function:: Channel.is_resp(channel)
1447
1448 This function returns true if the channel is the response one.
1449
Christopher Faulet5a2c6612021-08-15 20:35:25 +02001450 :param class_channel channel: The manipulated Channel.
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001451 :returns: a boolean
1452
1453.. js:function:: Channel.line(channel [, offset [, length]])
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001454
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001455 This function parses **length** bytes of incoming data of the channel buffer,
1456 starting at offset **offset**, and returns the first line found, including the
Christopher Faulet5a2c6612021-08-15 20:35:25 +02001457 '\\n'. The data are not removed from the buffer. If no line is found, all data
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001458 are returned.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001459
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001460 By default, if no length is provided, all incoming data, starting at the given
1461 offset, are evaluated. If **length** is set to -1, the function tries to
Christopher Faulet5a2c6612021-08-15 20:35:25 +02001462 retrieve a maximum of data and, if called by an action, yields if
1463 necessary. It also waits for more data if the requested length exceeds the
1464 available amount of incoming data. Do not providing an offset is the same that
Ilya Shipitsinff0f2782021-08-22 22:18:07 +05001465 setting it to 0. A positive offset is relative to the beginning of incoming
Christopher Faulet5a2c6612021-08-15 20:35:25 +02001466 data of the channel buffer while negative offset is relative to their end.
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001467
1468 If there is no incoming data and the channel can't receive more data, a 'nil'
1469 value is returned.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001470
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001471 :param class_channel channel: The manipulated Channel.
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001472 :param integer offset: *optional* The offset in incomding data to start to
1473 parse data. 0 by default. May be negative to be
1474 relative to the end of incoming data.
1475 :param integer length: *optional* The length of data to parse. All incoming
1476 data by default. May be set to -1 to get a maximum of
1477 data.
1478 :returns: a string containing the line found or nil.
1479
1480.. js:function:: Channel.may_recv(channel)
1481
1482 This function returns true if the channel may still receive data.
1483
Christopher Faulet5a2c6612021-08-15 20:35:25 +02001484 :param class_channel channel: The manipulated Channel.
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001485 :returns: a boolean
1486
1487.. js:function:: Channel.output(channel)
1488
Christopher Faulet5a2c6612021-08-15 20:35:25 +02001489 This function returns the length of outgoing data of the channel buffer. When
1490 called by a filter, this value is relative to the filter.
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001491
1492 :param class_channel channel: The manipulated Channel.
1493 :returns: an integer containing the amount of available bytes.
1494
1495.. js:function:: Channel.prepend(channel, string)
1496
1497 This function copies the string **string** in front of incoming data of the
1498 channel buffer. The function returns the copied length on success or -1 if
1499 data cannot be copied.
1500
1501 Same that :js:func:`Channel.insert(channel, string, 0)`.
1502
1503 :param class_channel channel: The manipulated Channel.
1504 :param string string: The data to copied into incoming data.
Pieter Baauw4d7f7662015-11-08 16:38:08 +01001505 :returns: an integer containing the amount of bytes copied or -1.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001506
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001507.. js:function:: Channel.remove(channel [, offset [, length]])
1508
1509 This function removes **length** bytes of incoming data of the channel buffer,
1510 starting at offset **offset**. This function returns number of bytes removed
1511 on success.
1512
1513 By default, if no length is provided, all incoming data, starting at the given
1514 offset, are removed. Do not providing an offset is the same that setting it
Ilya Shipitsinff0f2782021-08-22 22:18:07 +05001515 to 0. A positive offset is relative to the beginning of incoming data of the
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001516 channel buffer while negative offset is relative to their end.
1517
1518 :param class_channel channel: The manipulated Channel.
1519 :param integer offset: *optional* The offset in incomding data where to start
1520 to remove data. 0 by default. May be negative to
1521 be relative to the end of incoming data.
1522 :param integer length: *optional* The length of data to remove. All incoming
1523 data by default.
1524 :returns: an integer containing the amount of bytes removed.
1525
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001526.. js:function:: Channel.send(channel, string)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001527
Christopher Faulet5a2c6612021-08-15 20:35:25 +02001528 This function requires immediate send of the string **string**. It means the
Ilya Shipitsinff0f2782021-08-22 22:18:07 +05001529 string is copied at the beginning of incoming data of the channel buffer and
Christopher Faulet5a2c6612021-08-15 20:35:25 +02001530 immediately forwarded. Unless if the connection is close, and if called by an
1531 action, this function yields to copied and forward all the string.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001532
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001533 :param class_channel channel: The manipulated Channel.
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001534 :param string string: The data to send.
Pieter Baauw4d7f7662015-11-08 16:38:08 +01001535 :returns: an integer containing the amount of bytes copied or -1.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001536
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001537.. js:function:: Channel.set(channel, string [, offset [, length]])
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001538
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001539 This function replace **length** bytes of incoming data of the channel buffer,
1540 starting at offset **offset**, by the string **string**. The function returns
1541 the copied length on success or -1 if data cannot be copied.
1542
1543 By default, if no length is provided, all incoming data, starting at the given
1544 offset, are replaced. Do not providing an offset is the same that setting it
Ilya Shipitsinff0f2782021-08-22 22:18:07 +05001545 to 0. A positive offset is relative to the beginning of incoming data of the
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001546 channel buffer while negative offset is relative to their end.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001547
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001548 :param class_channel channel: The manipulated Channel.
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001549 :param string string: The data to copied into incoming data.
1550 :param integer offset: *optional* The offset in incomding data where to start
1551 the data replacement. 0 by default. May be negative to
1552 be relative to the end of incoming data.
1553 :param integer length: *optional* The length of data to replace. All incoming
1554 data by default.
1555 :returns: an integer containing the amount of bytes copied or -1.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001556
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001557.. js:function:: Channel.dup(channel)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001558
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001559 **DEPRECATED**
1560
1561 This function returns all incoming data found in the channel buffer. The data
1562 are not remove from the buffer and can be reprocessed later.
1563
1564 If there is no incoming data and the channel can't receive more data, a 'nil'
1565 value is returned.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001566
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001567 :param class_channel channel: The manipulated Channel.
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001568 :returns: a string containing all data found or nil.
1569
1570 .. warning::
1571 This function is deprecated. :js:func:`Channel.data()` must be used
1572 instead.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001573
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001574.. js:function:: Channel.get(channel)
1575
1576 **DEPRECATED**
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001577
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001578 This function returns all incoming data found in the channel buffer and remove
1579 them from the buffer.
1580
1581 If there is no incoming data and the channel can't receive more data, a 'nil'
1582 value is returned.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001583
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001584 :param class_channel channel: The manipulated Channel.
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001585 :returns: a string containing all the data found or nil.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001586
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001587 .. warning::
1588 This function is deprecated. :js:func:`Channel.data()` must be used to
1589 retrieve data followed by a call to :js:func:`Channel:remove()` to remove
1590 data.
Thierry FOURNIER / OZON.IO65192f32016-11-07 15:28:40 +01001591
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001592 .. code-block:: lua
Thierry FOURNIER / OZON.IO65192f32016-11-07 15:28:40 +01001593
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001594 local data = chn:data()
1595 chn:remove(0, data:len())
1596
1597 ..
1598
1599.. js:function:: Channel.getline(channel)
1600
1601 **DEPRECATED**
1602
1603 This function returns the first line found in incoming data of the channel
Christopher Faulet5a2c6612021-08-15 20:35:25 +02001604 buffer, including the '\\n'. The returned data are removed from the buffer. If
1605 no line is found, and if called by an action, this function yields to wait for
1606 more data, except if the channel can't receive more data. In this case all
1607 data are returned.
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001608
1609 If there is no incoming data and the channel can't receive more data, a 'nil'
1610 value is returned.
1611
1612 :param class_channel channel: The manipulated Channel.
1613 :returns: a string containing the line found or nil.
1614
1615 .. warning::
1616 This function is depdrecated. :js:func:`Channel.line()` must be used to
1617 retrieve a line followed by a call to :js:func:`Channel:remove()` to remove
1618 data.
1619
1620 .. code-block:: lua
1621
1622 local line = chn:line(0, -1)
1623 chn:remove(0, line:len())
1624
1625 ..
1626
1627.. js:function:: Channel.get_in_len(channel)
1628
1629 **DEPDRECATED**
1630
Christopher Faulet5a2c6612021-08-15 20:35:25 +02001631 This function returns the length of the input part of the buffer. When called
1632 by a filter, this value is relative to the filter.
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001633
1634 :param class_channel channel: The manipulated Channel.
1635 :returns: an integer containing the amount of available bytes.
1636
1637 .. warning::
1638 This function is deprecated. :js:func:`Channel.input()` must be used
1639 instead.
1640
1641.. js:function:: Channel.get_out_len(channel)
1642
1643 **DEPDRECATED**
1644
Christopher Faulet5a2c6612021-08-15 20:35:25 +02001645 This function returns the length of the output part of the buffer. When called
1646 by a filter, this value is relative to the filter.
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001647
1648 :param class_channel channel: The manipulated Channel.
1649 :returns: an integer containing the amount of available bytes.
1650
1651 .. warning::
1652 This function is deprecated. :js:func:`Channel.output()` must be used
1653 instead.
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001654
1655.. _http_class:
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001656
1657HTTP class
1658==========
1659
1660.. js:class:: HTTP
1661
1662 This class contain all the HTTP manipulation functions.
1663
Pieter Baauw386a1272015-08-16 15:26:24 +02001664.. js:function:: HTTP.req_get_headers(http)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001665
Patrick Hemmerc6a1d712018-05-01 21:30:41 -04001666 Returns a table containing all the request headers.
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001667
1668 :param class_http http: The related http object.
Patrick Hemmerc6a1d712018-05-01 21:30:41 -04001669 :returns: table of headers.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01001670 :see: :js:func:`HTTP.res_get_headers`
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001671
Patrick Hemmerc6a1d712018-05-01 21:30:41 -04001672 This is the form of the returned table:
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001673
1674.. code-block:: lua
1675
1676 HTTP:req_get_headers()['<header-name>'][<header-index>] = "<header-value>"
1677
1678 local hdr = HTTP:req_get_headers()
1679 hdr["host"][0] = "www.test.com"
1680 hdr["accept"][0] = "audio/basic q=1"
1681 hdr["accept"][1] = "audio/*, q=0.2"
1682 hdr["accept"][2] = "*/*, q=0.1"
1683..
1684
Pieter Baauw386a1272015-08-16 15:26:24 +02001685.. js:function:: HTTP.res_get_headers(http)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001686
Patrick Hemmerc6a1d712018-05-01 21:30:41 -04001687 Returns a table containing all the response headers.
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001688
1689 :param class_http http: The related http object.
Patrick Hemmerc6a1d712018-05-01 21:30:41 -04001690 :returns: table of headers.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01001691 :see: :js:func:`HTTP.req_get_headers`
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001692
Patrick Hemmerc6a1d712018-05-01 21:30:41 -04001693 This is the form of the returned table:
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001694
1695.. code-block:: lua
1696
1697 HTTP:res_get_headers()['<header-name>'][<header-index>] = "<header-value>"
1698
1699 local hdr = HTTP:req_get_headers()
1700 hdr["host"][0] = "www.test.com"
1701 hdr["accept"][0] = "audio/basic q=1"
1702 hdr["accept"][1] = "audio/*, q=0.2"
1703 hdr["accept"][2] = "*.*, q=0.1"
1704..
1705
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001706.. js:function:: HTTP.req_add_header(http, name, value)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001707
1708 Appends an HTTP header field in the request whose name is
1709 specified in "name" and whose value is defined in "value".
1710
1711 :param class_http http: The related http object.
1712 :param string name: The header name.
1713 :param string value: The header value.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01001714 :see: :js:func:`HTTP.res_add_header`
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001715
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001716.. js:function:: HTTP.res_add_header(http, name, value)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001717
Bertrand Jacquin874a35c2018-09-10 21:26:07 +01001718 Appends an HTTP header field in the response whose name is
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001719 specified in "name" and whose value is defined in "value".
1720
1721 :param class_http http: The related http object.
1722 :param string name: The header name.
1723 :param string value: The header value.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01001724 :see: :js:func:`HTTP.req_add_header`
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001725
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001726.. js:function:: HTTP.req_del_header(http, name)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001727
1728 Removes all HTTP header fields in the request whose name is
1729 specified in "name".
1730
1731 :param class_http http: The related http object.
1732 :param string name: The header name.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01001733 :see: :js:func:`HTTP.res_del_header`
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001734
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001735.. js:function:: HTTP.res_del_header(http, name)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001736
1737 Removes all HTTP header fields in the response whose name is
1738 specified in "name".
1739
1740 :param class_http http: The related http object.
1741 :param string name: The header name.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01001742 :see: :js:func:`HTTP.req_del_header`
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001743
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001744.. js:function:: HTTP.req_set_header(http, name, value)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001745
Bertrand Jacquin874a35c2018-09-10 21:26:07 +01001746 This variable replace all occurrence of all header "name", by only
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001747 one containing the "value".
1748
1749 :param class_http http: The related http object.
1750 :param string name: The header name.
1751 :param string value: The header value.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01001752 :see: :js:func:`HTTP.res_set_header`
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001753
Bertrand Jacquin874a35c2018-09-10 21:26:07 +01001754 This function does the same work as the following code:
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001755
1756.. code-block:: lua
1757
1758 function fcn(txn)
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001759 TXN.http:req_del_header("header")
1760 TXN.http:req_add_header("header", "value")
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001761 end
1762..
1763
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001764.. js:function:: HTTP.res_set_header(http, name, value)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001765
Bertrand Jacquin874a35c2018-09-10 21:26:07 +01001766 This variable replace all occurrence of all header "name", by only
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001767 one containing the "value".
1768
1769 :param class_http http: The related http object.
1770 :param string name: The header name.
1771 :param string value: The header value.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01001772 :see: :js:func:`HTTP.req_rep_header()`
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001773
Pieter Baauw386a1272015-08-16 15:26:24 +02001774.. js:function:: HTTP.req_rep_header(http, name, regex, replace)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001775
1776 Matches the regular expression in all occurrences of header field "name"
1777 according to "regex", and replaces them with the "replace" argument. The
1778 replacement value can contain back references like \1, \2, ... This
1779 function works with the request.
1780
1781 :param class_http http: The related http object.
1782 :param string name: The header name.
1783 :param string regex: The match regular expression.
1784 :param string replace: The replacement value.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01001785 :see: :js:func:`HTTP.res_rep_header()`
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001786
Pieter Baauw386a1272015-08-16 15:26:24 +02001787.. js:function:: HTTP.res_rep_header(http, name, regex, string)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001788
1789 Matches the regular expression in all occurrences of header field "name"
1790 according to "regex", and replaces them with the "replace" argument. The
1791 replacement value can contain back references like \1, \2, ... This
1792 function works with the request.
1793
1794 :param class_http http: The related http object.
1795 :param string name: The header name.
1796 :param string regex: The match regular expression.
1797 :param string replace: The replacement value.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01001798 :see: :js:func:`HTTP.req_rep_header()`
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001799
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001800.. js:function:: HTTP.req_set_method(http, method)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001801
1802 Rewrites the request method with the parameter "method".
1803
1804 :param class_http http: The related http object.
1805 :param string method: The new method.
1806
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001807.. js:function:: HTTP.req_set_path(http, path)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001808
1809 Rewrites the request path with the "path" parameter.
1810
1811 :param class_http http: The related http object.
1812 :param string path: The new path.
1813
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001814.. js:function:: HTTP.req_set_query(http, query)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001815
1816 Rewrites the request's query string which appears after the first question
1817 mark ("?") with the parameter "query".
1818
1819 :param class_http http: The related http object.
1820 :param string query: The new query.
1821
Thierry FOURNIER0d79cf62015-08-26 14:20:58 +02001822.. js:function:: HTTP.req_set_uri(http, uri)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001823
1824 Rewrites the request URI with the parameter "uri".
1825
1826 :param class_http http: The related http object.
1827 :param string uri: The new uri.
1828
Robin H. Johnson52f5db22017-01-01 13:10:52 -08001829.. js:function:: HTTP.res_set_status(http, status [, reason])
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +02001830
Robin H. Johnson52f5db22017-01-01 13:10:52 -08001831 Rewrites the response status code with the parameter "code".
1832
1833 If no custom reason is provided, it will be generated from the status.
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +02001834
1835 :param class_http http: The related http object.
1836 :param integer status: The new response status code.
Robin H. Johnson52f5db22017-01-01 13:10:52 -08001837 :param string reason: The new response reason (optional).
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +02001838
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001839.. _txn_class:
1840
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001841TXN class
1842=========
1843
1844.. js:class:: TXN
1845
1846 The txn class contain all the functions relative to the http or tcp
1847 transaction (Note than a tcp stream is the same than a tcp transaction, but
1848 an HTTP transaction is not the same than a tcp stream).
1849
1850 The usage of this class permits to retrieve data from the requests, alter it
1851 and forward it.
1852
1853 All the functions provided by this class are available in the context
Christopher Faulet5a2c6612021-08-15 20:35:25 +02001854 **sample-fetches**, **actions** and **filters**.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001855
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001856.. js:attribute:: TXN.c
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001857
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001858 :returns: An :ref:`converters_class`.
1859
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001860 This attribute contains a Converters class object.
1861
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001862.. js:attribute:: TXN.sc
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001863
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001864 :returns: An :ref:`converters_class`.
1865
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001866 This attribute contains a Converters class object. The functions of
1867 this object returns always a string.
1868
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001869.. js:attribute:: TXN.f
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001870
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001871 :returns: An :ref:`fetches_class`.
1872
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001873 This attribute contains a Fetches class object.
1874
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001875.. js:attribute:: TXN.sf
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001876
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001877 :returns: An :ref:`fetches_class`.
1878
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001879 This attribute contains a Fetches class object. The functions of
1880 this object returns always a string.
1881
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001882.. js:attribute:: TXN.req
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001883
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001884 :returns: An :ref:`channel_class`.
1885
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001886 This attribute contains a channel class object for the request buffer.
1887
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001888.. js:attribute:: TXN.res
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001889
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001890 :returns: An :ref:`channel_class`.
1891
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001892 This attribute contains a channel class object for the response buffer.
1893
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001894.. js:attribute:: TXN.http
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001895
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001896 :returns: An :ref:`http_class`.
1897
Bertrand Jacquin874a35c2018-09-10 21:26:07 +01001898 This attribute contains an HTTP class object. It is available only if the
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001899 proxy has the "mode http" enabled.
1900
Christopher Faulet5a2c6612021-08-15 20:35:25 +02001901.. js:attribute:: TXN.http_req
1902
1903 :returns: An :ref:`httpmessage_class`.
1904
1905 This attribute contains the request HTTPMessage class object. It is available
1906 only if the proxy has the "mode http" enabled and only in the **filters**
1907 context.
1908
1909.. js:attribute:: TXN.http_res
1910
1911 :returns: An :ref:`httpmessage_class`.
1912
1913 This attribute contains the response HTTPMessage class object. It is available
1914 only if the proxy has the "mode http" enabled and only in the **filters**
1915 context.
1916
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01001917.. js:function:: TXN.log(TXN, loglevel, msg)
1918
1919 This function sends a log. The log is sent, according with the HAProxy
1920 configuration file, on the default syslog server if it is configured and on
1921 the stderr if it is allowed.
1922
1923 :param class_txn txn: The class txn object containing the data.
Bertrand Jacquin874a35c2018-09-10 21:26:07 +01001924 :param integer loglevel: Is the log level associated with the message. It is a
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01001925 number between 0 and 7.
1926 :param string msg: The log content.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01001927 :see: :js:attr:`core.emerg`, :js:attr:`core.alert`, :js:attr:`core.crit`,
1928 :js:attr:`core.err`, :js:attr:`core.warning`, :js:attr:`core.notice`,
1929 :js:attr:`core.info`, :js:attr:`core.debug` (log level definitions)
1930 :see: :js:func:`TXN.deflog`
1931 :see: :js:func:`TXN.Debug`
1932 :see: :js:func:`TXN.Info`
1933 :see: :js:func:`TXN.Warning`
1934 :see: :js:func:`TXN.Alert`
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01001935
1936.. js:function:: TXN.deflog(TXN, msg)
1937
Bertrand Jacquin874a35c2018-09-10 21:26:07 +01001938 Sends a log line with the default loglevel for the proxy associated with the
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01001939 transaction.
1940
1941 :param class_txn txn: The class txn object containing the data.
1942 :param string msg: The log content.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01001943 :see: :js:func:`TXN.log
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01001944
1945.. js:function:: TXN.Debug(txn, msg)
1946
1947 :param class_txn txn: The class txn object containing the data.
1948 :param string msg: The log content.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01001949 :see: :js:func:`TXN.log`
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01001950
1951 Does the same job than:
1952
1953.. code-block:: lua
1954
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001955 function Debug(txn, msg)
1956 TXN.log(txn, core.debug, msg)
1957 end
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01001958..
1959
1960.. js:function:: TXN.Info(txn, msg)
1961
1962 :param class_txn txn: The class txn object containing the data.
1963 :param string msg: The log content.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01001964 :see: :js:func:`TXN.log`
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01001965
1966.. code-block:: lua
1967
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001968 function Debug(txn, msg)
1969 TXN.log(txn, core.info, msg)
1970 end
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01001971..
1972
1973.. js:function:: TXN.Warning(txn, msg)
1974
1975 :param class_txn txn: The class txn object containing the data.
1976 :param string msg: The log content.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01001977 :see: :js:func:`TXN.log`
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01001978
1979.. code-block:: lua
1980
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001981 function Debug(txn, msg)
1982 TXN.log(txn, core.warning, msg)
1983 end
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01001984..
1985
1986.. js:function:: TXN.Alert(txn, msg)
1987
1988 :param class_txn txn: The class txn object containing the data.
1989 :param string msg: The log content.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01001990 :see: :js:func:`TXN.log`
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01001991
1992.. code-block:: lua
1993
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001994 function Debug(txn, msg)
1995 TXN.log(txn, core.alert, msg)
1996 end
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01001997..
1998
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001999.. js:function:: TXN.get_priv(txn)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002000
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002001 Return Lua data stored in the current transaction (with the `TXN.set_priv()`)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002002 function. If no data are stored, it returns a nil value.
2003
2004 :param class_txn txn: The class txn object containing the data.
Bertrand Jacquin874a35c2018-09-10 21:26:07 +01002005 :returns: the opaque data previously stored, or nil if nothing is
2006 available.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002007
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002008.. js:function:: TXN.set_priv(txn, data)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002009
2010 Store any data in the current HAProxy transaction. This action replace the
2011 old stored data.
2012
2013 :param class_txn txn: The class txn object containing the data.
2014 :param opaque data: The data which is stored in the transaction.
2015
Tim Duesterhus4e172c92020-05-19 13:49:42 +02002016.. js:function:: TXN.set_var(TXN, var, value[, ifexist])
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +02002017
David Carlier61fdf8b2015-10-02 11:59:38 +01002018 Converts a Lua type in a HAProxy type and store it in a variable <var>.
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +02002019
2020 :param class_txn txn: The class txn object containing the data.
2021 :param string var: The variable name according with the HAProxy variable syntax.
Thierry FOURNIER / OZON.IOb210bcc2016-12-12 16:24:16 +01002022 :param type value: The value associated to the variable. The type can be string or
2023 integer.
Tim Duesterhus4e172c92020-05-19 13:49:42 +02002024 :param boolean ifexist: If this parameter is set to a truthy value the variable
2025 will only be set if it was defined elsewhere (i.e. used
Willy Tarreau7978c5c2021-09-07 14:24:07 +02002026 within the configuration). For global variables (using the
2027 "proc" scope), they will only be updated and never created.
2028 It is highly recommended to always set this to true.
Christopher Faulet85d79c92016-11-09 16:54:56 +01002029
2030.. js:function:: TXN.unset_var(TXN, var)
2031
2032 Unset the variable <var>.
2033
2034 :param class_txn txn: The class txn object containing the data.
2035 :param string var: The variable name according with the HAProxy variable syntax.
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +02002036
2037.. js:function:: TXN.get_var(TXN, var)
2038
2039 Returns data stored in the variable <var> converter in Lua type.
2040
2041 :param class_txn txn: The class txn object containing the data.
2042 :param string var: The variable name according with the HAProxy variable syntax.
2043
Christopher Faulet700d9e82020-01-31 12:21:52 +01002044.. js:function:: TXN.reply([reply])
2045
2046 Return a new reply object
2047
2048 :param table reply: A table containing info to initialize the reply fields.
2049 :returns: A :ref:`reply_class` object.
2050
2051 The table used to initialized the reply object may contain following entries :
2052
2053 * status : The reply status code. the code 200 is used by default.
2054 * reason : The reply reason. The reason corresponding to the status code is
2055 used by default.
2056 * headers : An list of headers, indexed by header name. Empty by default. For
2057 a given name, multiple values are possible, stored in an ordered list.
2058 * body : The reply body, empty by default.
2059
2060.. code-block:: lua
2061
2062 local reply = txn:reply{
2063 status = 400,
2064 reason = "Bad request",
2065 headers = {
2066 ["content-type"] = { "text/html" },
2067 ["cache-control"] = {"no-cache", "no-store" }
2068 },
2069 body = "<html><body><h1>invalid request<h1></body></html>"
2070 }
2071..
2072 :see: :js:class:`Reply`
2073
2074.. js:function:: TXN.done(txn[, reply])
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002075
Willy Tarreaubc183a62015-08-28 10:39:11 +02002076 This function terminates processing of the transaction and the associated
Christopher Faulet700d9e82020-01-31 12:21:52 +01002077 session and optionally reply to the client for HTTP sessions.
2078
2079 :param class_txn txn: The class txn object containing the data.
2080 :param class_reply reply: The class reply object to return to the client.
2081
2082 This functions can be used when a critical error is detected or to terminate
Willy Tarreaubc183a62015-08-28 10:39:11 +02002083 processing after some data have been returned to the client (eg: a redirect).
Christopher Faulet700d9e82020-01-31 12:21:52 +01002084 To do so, a reply may be provided. This object is optional and may contain a
2085 status code, a reason, a header list and a body. All these fields are
Ilya Shipitsinff0f2782021-08-22 22:18:07 +05002086 optional. When not provided, the default values are used. By default, with
Christopher Faulet700d9e82020-01-31 12:21:52 +01002087 an empty reply object, an empty HTTP 200 response is returned to the
2088 client. If no reply object is provided, the transaction is terminated without
2089 any reply.
2090
2091 The reply object may be fully created in lua or the class Reply may be used to
2092 create it.
2093
2094.. code-block:: lua
2095
2096 local reply = txn:reply()
2097 reply:set_status(400, "Bad request")
2098 reply:add_header("content-type", "text/html")
2099 reply:add_header("cache-control", "no-cache")
2100 reply:add_header("cache-control", "no-store")
2101 reply:set_body("<html><body><h1>invalid request<h1></body></html>")
2102 txn:done(reply)
2103..
2104
2105.. code-block:: lua
2106
2107 txn:done{
2108 status = 400,
2109 reason = "Bad request",
2110 headers = {
2111 ["content-type"] = { "text/html" },
2112 ["cache-control"] = { "no-cache", "no-store" },
2113 },
2114 body = "<html><body><h1>invalid request<h1></body></html>"
2115 }
2116..
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002117
Christopher Faulet1e9b1b62021-08-11 10:14:30 +02002118 .. warning::
2119 It not make sense to call this function from sample-fetches. In this case
2120 the behaviour of this one is the same than core.done(): it quit the Lua
2121 execution. The transaction is really aborted only from an action registered
2122 function.
Thierry FOURNIERab00df62016-07-14 11:42:37 +02002123
Christopher Faulet700d9e82020-01-31 12:21:52 +01002124 :see: :js:func:`TXN.reply`, :js:class:`Reply`
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002125
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002126.. js:function:: TXN.set_loglevel(txn, loglevel)
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01002127
2128 Is used to change the log level of the current request. The "loglevel" must
2129 be an integer between 0 and 7.
2130
2131 :param class_txn txn: The class txn object containing the data.
2132 :param integer loglevel: The required log level. This variable can be one of
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01002133 :see: :js:attr:`core.emerg`, :js:attr:`core.alert`, :js:attr:`core.crit`,
2134 :js:attr:`core.err`, :js:attr:`core.warning`, :js:attr:`core.notice`,
2135 :js:attr:`core.info`, :js:attr:`core.debug` (log level definitions)
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01002136
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002137.. js:function:: TXN.set_tos(txn, tos)
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01002138
2139 Is used to set the TOS or DSCP field value of packets sent to the client to
2140 the value passed in "tos" on platforms which support this.
2141
2142 :param class_txn txn: The class txn object containing the data.
2143 :param integer tos: The new TOS os DSCP.
2144
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002145.. js:function:: TXN.set_mark(txn, mark)
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01002146
2147 Is used to set the Netfilter MARK on all packets sent to the client to the
2148 value passed in "mark" on platforms which support it.
2149
2150 :param class_txn txn: The class txn object containing the data.
2151 :param integer mark: The mark value.
2152
Patrick Hemmer268a7072018-05-11 12:52:31 -04002153.. js:function:: TXN.set_priority_class(txn, prio)
2154
2155 This function adjusts the priority class of the transaction. The value should
2156 be within the range -2047..2047. Values outside this range will be
2157 truncated.
2158
2159 See the HAProxy configuration.txt file keyword "http-request" action
2160 "set-priority-class" for details.
2161
2162.. js:function:: TXN.set_priority_offset(txn, prio)
2163
2164 This function adjusts the priority offset of the transaction. The value
2165 should be within the range -524287..524287. Values outside this range will be
2166 truncated.
2167
2168 See the HAProxy configuration.txt file keyword "http-request" action
2169 "set-priority-offset" for details.
2170
Christopher Faulet700d9e82020-01-31 12:21:52 +01002171.. _reply_class:
2172
2173Reply class
2174============
2175
2176.. js:class:: Reply
2177
2178 **context**: action
2179
2180 This class represents an HTTP response message. It provides some methods to
2181 enrich it.
2182
2183.. code-block:: lua
2184
2185 local reply = txn:reply({status = 400}) -- default HTTP 400 reason-phase used
2186 reply:add_header("content-type", "text/html")
2187 reply:add_header("cache-control", "no-cache")
2188 reply:add_header("cache-control", "no-store")
2189 reply:set_body("<html><body><h1>invalid request<h1></body></html>")
2190..
2191
2192 :see: :js:func:`TXN.reply`
2193
2194.. js:attribute:: Reply.status
2195
2196 The reply status code. By default, the status code is set to 200.
2197
2198 :returns: integer
2199
2200.. js:attribute:: Reply.reason
2201
2202 The reason string describing the status code.
2203
2204 :returns: string
2205
2206.. js:attribute:: Reply.headers
2207
2208 A table indexing all reply headers by name. To each name is associated an
2209 ordered list of values.
2210
2211 :returns: Lua table
2212
2213.. code-block:: lua
2214
2215 {
2216 ["content-type"] = { "text/html" },
2217 ["cache-control"] = {"no-cache", "no-store" },
2218 x_header_name = { "value1", "value2", ... }
2219 ...
2220 }
2221..
2222
2223.. js:attribute:: Reply.body
2224
2225 The reply payload.
2226
2227 :returns: string
2228
2229.. js:function:: Reply.set_status(REPLY, status[, reason])
2230
2231 Set the reply status code and optionally the reason-phrase. If the reason is
2232 not provided, the default reason corresponding to the status code is used.
2233
2234 :param class_reply reply: The related Reply object.
2235 :param integer status: The reply status code.
2236 :param string reason: The reply status reason (optional).
2237
2238.. js:function:: Reply.add_header(REPLY, name, value)
2239
2240 Add a header to the reply object. If the header does not already exist, a new
2241 entry is created with its name as index and a one-element list containing its
2242 value as value. Otherwise, the header value is appended to the ordered list of
2243 values associated to the header name.
2244
2245 :param class_reply reply: The related Reply object.
2246 :param string name: The header field name.
2247 :param string value: The header field value.
2248
2249.. js:function:: Reply.del_header(REPLY, name)
2250
2251 Remove all occurrences of a header name from the reply object.
2252
2253 :param class_reply reply: The related Reply object.
2254 :param string name: The header field name.
2255
2256.. js:function:: Reply.set_body(REPLY, body)
2257
2258 Set the reply payload.
2259
2260 :param class_reply reply: The related Reply object.
2261 :param string body: The reply payload.
2262
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002263.. _socket_class:
2264
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002265Socket class
2266============
2267
2268.. js:class:: Socket
2269
2270 This class must be compatible with the Lua Socket class. Only the 'client'
2271 functions are available. See the Lua Socket documentation:
2272
2273 `http://w3.impa.br/~diego/software/luasocket/tcp.html
2274 <http://w3.impa.br/~diego/software/luasocket/tcp.html>`_
2275
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002276.. js:function:: Socket.close(socket)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002277
2278 Closes a TCP object. The internal socket used by the object is closed and the
2279 local address to which the object was bound is made available to other
2280 applications. No further operations (except for further calls to the close
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002281 method) are allowed on a closed Socket.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002282
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002283 :param class_socket socket: Is the manipulated Socket.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002284
2285 Note: It is important to close all used sockets once they are not needed,
2286 since, in many systems, each socket uses a file descriptor, which are limited
2287 system resources. Garbage-collected objects are automatically closed before
2288 destruction, though.
2289
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002290.. js:function:: Socket.connect(socket, address[, port])
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002291
2292 Attempts to connect a socket object to a remote host.
2293
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002294
2295 In case of error, the method returns nil followed by a string describing the
2296 error. In case of success, the method returns 1.
2297
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002298 :param class_socket socket: Is the manipulated Socket.
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002299 :param string address: can be an IP address or a host name. See below for more
2300 information.
2301 :param integer port: must be an integer number in the range [1..64K].
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002302 :returns: 1 or nil.
2303
Bertrand Jacquin874a35c2018-09-10 21:26:07 +01002304 An address field extension permits to use the connect() function to connect to
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002305 other stream than TCP. The syntax containing a simpleipv4 or ipv6 address is
2306 the basically expected format. This format requires the port.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002307
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002308 Other format accepted are a socket path like "/socket/path", it permits to
Bertrand Jacquin874a35c2018-09-10 21:26:07 +01002309 connect to a socket. Abstract namespaces are supported with the prefix
Joseph Herlant02cedc42018-11-13 19:45:17 -08002310 "abns@", and finally a file descriptor can be passed with the prefix "fd@".
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002311 The prefix "ipv4@", "ipv6@" and "unix@" are also supported. The port can be
Bertrand Jacquin874a35c2018-09-10 21:26:07 +01002312 passed int the string. The syntax "127.0.0.1:1234" is valid. In this case, the
Tim Duesterhus6edab862018-01-06 19:04:45 +01002313 parameter *port* must not be set.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002314
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002315.. js:function:: Socket.connect_ssl(socket, address, port)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002316
2317 Same behavior than the function socket:connect, but uses SSL.
2318
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002319 :param class_socket socket: Is the manipulated Socket.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002320 :returns: 1 or nil.
2321
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002322.. js:function:: Socket.getpeername(socket)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002323
2324 Returns information about the remote side of a connected client object.
2325
2326 Returns a string with the IP address of the peer, followed by the port number
2327 that peer is using for the connection. In case of error, the method returns
2328 nil.
2329
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002330 :param class_socket socket: Is the manipulated Socket.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002331 :returns: a string containing the server information.
2332
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002333.. js:function:: Socket.getsockname(socket)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002334
2335 Returns the local address information associated to the object.
2336
2337 The method returns a string with local IP address and a number with the port.
2338 In case of error, the method returns nil.
2339
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002340 :param class_socket socket: Is the manipulated Socket.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002341 :returns: a string containing the client information.
2342
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002343.. js:function:: Socket.receive(socket, [pattern [, prefix]])
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002344
2345 Reads data from a client object, according to the specified read pattern.
2346 Patterns follow the Lua file I/O format, and the difference in performance
2347 between all patterns is negligible.
2348
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002349 :param class_socket socket: Is the manipulated Socket.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002350 :param string|integer pattern: Describe what is required (see below).
2351 :param string prefix: A string which will be prefix the returned data.
2352 :returns: a string containing the required data or nil.
2353
2354 Pattern can be any of the following:
2355
2356 * **`*a`**: reads from the socket until the connection is closed. No
2357 end-of-line translation is performed;
2358
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002359 * **`*l`**: reads a line of text from the Socket. The line is terminated by a
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002360 LF character (ASCII 10), optionally preceded by a CR character
2361 (ASCII 13). The CR and LF characters are not included in the
2362 returned line. In fact, all CR characters are ignored by the
2363 pattern. This is the default pattern.
2364
2365 * **number**: causes the method to read a specified number of bytes from the
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002366 Socket. Prefix is an optional string to be concatenated to the
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002367 beginning of any received data before return.
2368
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002369 * **empty**: If the pattern is left empty, the default option is `*l`.
2370
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002371 If successful, the method returns the received pattern. In case of error, the
2372 method returns nil followed by an error message which can be the string
2373 'closed' in case the connection was closed before the transmission was
2374 completed or the string 'timeout' in case there was a timeout during the
2375 operation. Also, after the error message, the function returns the partial
2376 result of the transmission.
2377
2378 Important note: This function was changed severely. It used to support
2379 multiple patterns (but I have never seen this feature used) and now it
2380 doesn't anymore. Partial results used to be returned in the same way as
2381 successful results. This last feature violated the idea that all functions
2382 should return nil on error. Thus it was changed too.
2383
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002384.. js:function:: Socket.send(socket, data [, start [, end ]])
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002385
2386 Sends data through client object.
2387
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002388 :param class_socket socket: Is the manipulated Socket.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002389 :param string data: The data that will be sent.
2390 :param integer start: The start position in the buffer of the data which will
2391 be sent.
2392 :param integer end: The end position in the buffer of the data which will
2393 be sent.
2394 :returns: see below.
2395
2396 Data is the string to be sent. The optional arguments i and j work exactly
2397 like the standard string.sub Lua function to allow the selection of a
2398 substring to be sent.
2399
2400 If successful, the method returns the index of the last byte within [start,
2401 end] that has been sent. Notice that, if start is 1 or absent, this is
2402 effectively the total number of bytes sent. In case of error, the method
2403 returns nil, followed by an error message, followed by the index of the last
2404 byte within [start, end] that has been sent. You might want to try again from
2405 the byte following that. The error message can be 'closed' in case the
2406 connection was closed before the transmission was completed or the string
2407 'timeout' in case there was a timeout during the operation.
2408
2409 Note: Output is not buffered. For small strings, it is always better to
2410 concatenate them in Lua (with the '..' operator) and send the result in one
2411 call instead of calling the method several times.
2412
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002413.. js:function:: Socket.setoption(socket, option [, value])
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002414
2415 Just implemented for compatibility, this cal does nothing.
2416
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002417.. js:function:: Socket.settimeout(socket, value [, mode])
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002418
2419 Changes the timeout values for the object. All I/O operations are blocking.
2420 That is, any call to the methods send, receive, and accept will block
2421 indefinitely, until the operation completes. The settimeout method defines a
2422 limit on the amount of time the I/O methods can block. When a timeout time
2423 has elapsed, the affected methods give up and fail with an error code.
2424
2425 The amount of time to wait is specified as the value parameter, in seconds.
2426
Mark Lakes56cc1252018-03-27 09:48:06 +02002427 The timeout modes are not implemented, the only settable timeout is the
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002428 inactivity time waiting for complete the internal buffer send or waiting for
2429 receive data.
2430
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002431 :param class_socket socket: Is the manipulated Socket.
Bertrand Jacquin874a35c2018-09-10 21:26:07 +01002432 :param float value: The timeout value. Use floating point to specify
Mark Lakes56cc1252018-03-27 09:48:06 +02002433 milliseconds.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002434
Thierry FOURNIER31904272017-10-25 12:59:51 +02002435.. _regex_class:
2436
2437Regex class
2438===========
2439
2440.. js:class:: Regex
2441
2442 This class allows the usage of HAProxy regexes because classic lua doesn't
2443 provides regexes. This class inherits the HAProxy compilation options, so the
2444 regexes can be libc regex, pcre regex or pcre JIT regex.
2445
2446 The expression matching number is limited to 20 per regex. The only available
2447 option is case sensitive.
2448
2449 Because regexes compilation is a heavy process, it is better to define all
2450 your regexes in the **body context** and use it during the runtime.
2451
2452.. code-block:: lua
2453
2454 -- Create the regex
2455 st, regex = Regex.new("needle (..) (...)", true);
2456
2457 -- Check compilation errors
2458 if st == false then
2459 print "error: " .. regex
2460 end
2461
2462 -- Match the regexes
2463 print(regex:exec("Looking for a needle in the haystack")) -- true
2464 print(regex:exec("Lokking for a cat in the haystack")) -- false
2465
2466 -- Extract words
2467 st, list = regex:match("Looking for a needle in the haystack")
2468 print(st) -- true
2469 print(list[1]) -- needle in the
2470 print(list[2]) -- in
2471 print(list[3]) -- the
2472
2473.. js:function:: Regex.new(regex, case_sensitive)
2474
2475 Create and compile a regex.
2476
2477 :param string regex: The regular expression according with the libc or pcre
2478 standard
2479 :param boolean case_sensitive: Match is case sensitive or not.
2480 :returns: boolean status and :ref:`regex_class` or string containing fail reason.
2481
2482.. js:function:: Regex.exec(regex, str)
2483
2484 Execute the regex.
2485
2486 :param class_regex regex: A :ref:`regex_class` object.
2487 :param string str: The input string will be compared with the compiled regex.
2488 :returns: a boolean status according with the match result.
2489
2490.. js:function:: Regex.match(regex, str)
2491
2492 Execute the regex and return matched expressions.
2493
2494 :param class_map map: A :ref:`regex_class` object.
2495 :param string str: The input string will be compared with the compiled regex.
2496 :returns: a boolean status according with the match result, and
2497 a table containing all the string matched in order of declaration.
2498
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002499.. _map_class:
2500
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002501Map class
2502=========
2503
2504.. js:class:: Map
2505
2506 This class permits to do some lookup in HAProxy maps. The declared maps can
Bertrand Jacquin874a35c2018-09-10 21:26:07 +01002507 be modified during the runtime through the HAProxy management socket.
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002508
2509.. code-block:: lua
2510
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002511 default = "usa"
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002512
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002513 -- Create and load map
Thierry FOURNIER4dc71972017-01-28 08:33:08 +01002514 geo = Map.new("geo.map", Map._ip);
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002515
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002516 -- Create new fetch that returns the user country
2517 core.register_fetches("country", function(txn)
2518 local src;
2519 local loc;
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002520
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002521 src = txn.f:fhdr("x-forwarded-for");
2522 if (src == nil) then
2523 src = txn.f:src()
2524 if (src == nil) then
2525 return default;
2526 end
2527 end
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002528
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002529 -- Perform lookup
2530 loc = geo:lookup(src);
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002531
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002532 if (loc == nil) then
2533 return default;
2534 end
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002535
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002536 return loc;
2537 end);
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002538
Thierry FOURNIER4dc71972017-01-28 08:33:08 +01002539.. js:attribute:: Map._int
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002540
2541 See the HAProxy configuration.txt file, chapter "Using ACLs and fetching
Ilya Shipitsin2075ca82020-03-06 23:22:22 +05002542 samples" and subchapter "ACL basics" to understand this pattern matching
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002543 method.
2544
Thierry FOURNIER4dc71972017-01-28 08:33:08 +01002545 Note that :js:attr:`Map.int` is also available for compatibility.
2546
2547.. js:attribute:: Map._ip
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002548
2549 See the HAProxy configuration.txt file, chapter "Using ACLs and fetching
Ilya Shipitsin2075ca82020-03-06 23:22:22 +05002550 samples" and subchapter "ACL basics" to understand this pattern matching
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002551 method.
2552
Thierry FOURNIER4dc71972017-01-28 08:33:08 +01002553 Note that :js:attr:`Map.ip` is also available for compatibility.
2554
2555.. js:attribute:: Map._str
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002556
2557 See the HAProxy configuration.txt file, chapter "Using ACLs and fetching
Ilya Shipitsin2075ca82020-03-06 23:22:22 +05002558 samples" and subchapter "ACL basics" to understand this pattern matching
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002559 method.
2560
Thierry FOURNIER4dc71972017-01-28 08:33:08 +01002561 Note that :js:attr:`Map.str` is also available for compatibility.
2562
2563.. js:attribute:: Map._beg
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002564
2565 See the HAProxy configuration.txt file, chapter "Using ACLs and fetching
Ilya Shipitsin2075ca82020-03-06 23:22:22 +05002566 samples" and subchapter "ACL basics" to understand this pattern matching
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002567 method.
2568
Thierry FOURNIER4dc71972017-01-28 08:33:08 +01002569 Note that :js:attr:`Map.beg` is also available for compatibility.
2570
2571.. js:attribute:: Map._sub
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002572
2573 See the HAProxy configuration.txt file, chapter "Using ACLs and fetching
Ilya Shipitsin2075ca82020-03-06 23:22:22 +05002574 samples" and subchapter "ACL basics" to understand this pattern matching
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002575 method.
2576
Thierry FOURNIER4dc71972017-01-28 08:33:08 +01002577 Note that :js:attr:`Map.sub` is also available for compatibility.
2578
2579.. js:attribute:: Map._dir
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002580
2581 See the HAProxy configuration.txt file, chapter "Using ACLs and fetching
Ilya Shipitsin2075ca82020-03-06 23:22:22 +05002582 samples" and subchapter "ACL basics" to understand this pattern matching
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002583 method.
2584
Thierry FOURNIER4dc71972017-01-28 08:33:08 +01002585 Note that :js:attr:`Map.dir` is also available for compatibility.
2586
2587.. js:attribute:: Map._dom
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002588
2589 See the HAProxy configuration.txt file, chapter "Using ACLs and fetching
Ilya Shipitsin2075ca82020-03-06 23:22:22 +05002590 samples" and subchapter "ACL basics" to understand this pattern matching
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002591 method.
2592
Thierry FOURNIER4dc71972017-01-28 08:33:08 +01002593 Note that :js:attr:`Map.dom` is also available for compatibility.
2594
2595.. js:attribute:: Map._end
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002596
2597 See the HAProxy configuration.txt file, chapter "Using ACLs and fetching
Ilya Shipitsin2075ca82020-03-06 23:22:22 +05002598 samples" and subchapter "ACL basics" to understand this pattern matching
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002599 method.
2600
Thierry FOURNIER4dc71972017-01-28 08:33:08 +01002601.. js:attribute:: Map._reg
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002602
2603 See the HAProxy configuration.txt file, chapter "Using ACLs and fetching
Ilya Shipitsin2075ca82020-03-06 23:22:22 +05002604 samples" and subchapter "ACL basics" to understand this pattern matching
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002605 method.
2606
Thierry FOURNIER4dc71972017-01-28 08:33:08 +01002607 Note that :js:attr:`Map.reg` is also available for compatibility.
2608
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002609
2610.. js:function:: Map.new(file, method)
2611
2612 Creates and load a map.
2613
2614 :param string file: Is the file containing the map.
2615 :param integer method: Is the map pattern matching method. See the attributes
2616 of the Map class.
2617 :returns: a class Map object.
Thierry FOURNIER4dc71972017-01-28 08:33:08 +01002618 :see: The Map attributes: :js:attr:`Map._int`, :js:attr:`Map._ip`,
2619 :js:attr:`Map._str`, :js:attr:`Map._beg`, :js:attr:`Map._sub`,
2620 :js:attr:`Map._dir`, :js:attr:`Map._dom`, :js:attr:`Map._end` and
2621 :js:attr:`Map._reg`.
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002622
2623.. js:function:: Map.lookup(map, str)
2624
2625 Perform a lookup in a map.
2626
2627 :param class_map map: Is the class Map object.
2628 :param string str: Is the string used as key.
2629 :returns: a string containing the result or nil if no match.
2630
2631.. js:function:: Map.slookup(map, str)
2632
2633 Perform a lookup in a map.
2634
2635 :param class_map map: Is the class Map object.
2636 :param string str: Is the string used as key.
2637 :returns: a string containing the result or empty string if no match.
2638
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002639.. _applethttp_class:
2640
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002641AppletHTTP class
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002642================
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002643
2644.. js:class:: AppletHTTP
2645
2646 This class is used with applets that requires the 'http' mode. The http applet
2647 can be registered with the *core.register_service()* function. They are used
2648 for processing an http request like a server in back of HAProxy.
2649
2650 This is an hello world sample code:
2651
2652.. code-block:: lua
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002653
Pieter Baauw4d7f7662015-11-08 16:38:08 +01002654 core.register_service("hello-world", "http", function(applet)
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002655 local response = "Hello World !"
2656 applet:set_status(200)
Pieter Baauw2dcb9bc2015-10-01 22:47:12 +02002657 applet:add_header("content-length", string.len(response))
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002658 applet:add_header("content-type", "text/plain")
Pieter Baauw2dcb9bc2015-10-01 22:47:12 +02002659 applet:start_response()
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002660 applet:send(response)
2661 end)
2662
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002663.. js:attribute:: AppletHTTP.c
2664
2665 :returns: A :ref:`converters_class`
2666
2667 This attribute contains a Converters class object.
2668
2669.. js:attribute:: AppletHTTP.sc
2670
2671 :returns: A :ref:`converters_class`
2672
2673 This attribute contains a Converters class object. The
2674 functions of this object returns always a string.
2675
2676.. js:attribute:: AppletHTTP.f
2677
2678 :returns: A :ref:`fetches_class`
2679
2680 This attribute contains a Fetches class object. Note that the
2681 applet execution place cannot access to a valid HAProxy core HTTP
Ilya Shipitsin2075ca82020-03-06 23:22:22 +05002682 transaction, so some sample fetches related to the HTTP dependent
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002683 values (hdr, path, ...) are not available.
2684
2685.. js:attribute:: AppletHTTP.sf
2686
2687 :returns: A :ref:`fetches_class`
2688
2689 This attribute contains a Fetches class object. The functions of
2690 this object returns always a string. Note that the applet
2691 execution place cannot access to a valid HAProxy core HTTP
Ilya Shipitsin2075ca82020-03-06 23:22:22 +05002692 transaction, so some sample fetches related to the HTTP dependent
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002693 values (hdr, path, ...) are not available.
2694
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01002695.. js:attribute:: AppletHTTP.method
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002696
2697 :returns: string
2698
2699 The attribute method returns a string containing the HTTP
2700 method.
2701
2702.. js:attribute:: AppletHTTP.version
2703
2704 :returns: string
2705
2706 The attribute version, returns a string containing the HTTP
2707 request version.
2708
2709.. js:attribute:: AppletHTTP.path
2710
2711 :returns: string
2712
2713 The attribute path returns a string containing the HTTP
2714 request path.
2715
2716.. js:attribute:: AppletHTTP.qs
2717
2718 :returns: string
2719
2720 The attribute qs returns a string containing the HTTP
2721 request query string.
2722
2723.. js:attribute:: AppletHTTP.length
2724
2725 :returns: integer
2726
2727 The attribute length returns an integer containing the HTTP
2728 body length.
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002729
Thierry FOURNIER841475e2015-12-11 17:10:09 +01002730.. js:attribute:: AppletHTTP.headers
2731
Patrick Hemmerc6a1d712018-05-01 21:30:41 -04002732 :returns: table
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002733
Patrick Hemmerc6a1d712018-05-01 21:30:41 -04002734 The attribute headers returns a table containing the HTTP
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002735 headers. The header names are always in lower case. As the header name can be
2736 encountered more than once in each request, the value is indexed with 0 as
Patrick Hemmerc6a1d712018-05-01 21:30:41 -04002737 first index value. The table have this form:
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002738
2739.. code-block:: lua
2740
2741 AppletHTTP.headers['<header-name>'][<header-index>] = "<header-value>"
2742
2743 AppletHTTP.headers["host"][0] = "www.test.com"
2744 AppletHTTP.headers["accept"][0] = "audio/basic q=1"
2745 AppletHTTP.headers["accept"][1] = "audio/*, q=0.2"
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01002746 AppletHTTP.headers["accept"][2] = "*/*, q=0.1"
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002747..
2748
Robin H. Johnson52f5db22017-01-01 13:10:52 -08002749.. js:function:: AppletHTTP.set_status(applet, code [, reason])
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002750
2751 This function sets the HTTP status code for the response. The allowed code are
2752 from 100 to 599.
2753
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01002754 :param class_AppletHTTP applet: An :ref:`applethttp_class`
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002755 :param integer code: the status code returned to the client.
Robin H. Johnson52f5db22017-01-01 13:10:52 -08002756 :param string reason: the status reason returned to the client (optional).
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002757
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01002758.. js:function:: AppletHTTP.add_header(applet, name, value)
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002759
2760 This function add an header in the response. Duplicated headers are not
2761 collapsed. The special header *content-length* is used to determinate the
2762 response length. If it not exists, a *transfer-encoding: chunked* is set, and
Ilya Shipitsin2075ca82020-03-06 23:22:22 +05002763 all the write from the function *AppletHTTP:send()* become a chunk.
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002764
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01002765 :param class_AppletHTTP applet: An :ref:`applethttp_class`
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002766 :param string name: the header name
2767 :param string value: the header value
2768
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01002769.. js:function:: AppletHTTP.start_response(applet)
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002770
2771 This function indicates to the HTTP engine that it can process and send the
2772 response headers. After this called we cannot add headers to the response; We
2773 cannot use the *AppletHTTP:send()* function if the
2774 *AppletHTTP:start_response()* is not called.
2775
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01002776 :param class_AppletHTTP applet: An :ref:`applethttp_class`
2777
2778.. js:function:: AppletHTTP.getline(applet)
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002779
2780 This function returns a string containing one line from the http body. If the
2781 data returned doesn't contains a final '\\n' its assumed than its the last
2782 available data before the end of stream.
2783
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01002784 :param class_AppletHTTP applet: An :ref:`applethttp_class`
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002785 :returns: a string. The string can be empty if we reach the end of the stream.
2786
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01002787.. js:function:: AppletHTTP.receive(applet, [size])
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002788
2789 Reads data from the HTTP body, according to the specified read *size*. If the
2790 *size* is missing, the function tries to read all the content of the stream
2791 until the end. If the *size* is bigger than the http body, it returns the
Bertrand Jacquin874a35c2018-09-10 21:26:07 +01002792 amount of data available.
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002793
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01002794 :param class_AppletHTTP applet: An :ref:`applethttp_class`
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002795 :param integer size: the required read size.
Ilya Shipitsin11057a32020-06-21 21:18:27 +05002796 :returns: always return a string,the string can be empty is the connection is
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002797 closed.
2798
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01002799.. js:function:: AppletHTTP.send(applet, msg)
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002800
2801 Send the message *msg* on the http request body.
2802
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01002803 :param class_AppletHTTP applet: An :ref:`applethttp_class`
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002804 :param string msg: the message to send.
2805
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01002806.. js:function:: AppletHTTP.get_priv(applet)
2807
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01002808 Return Lua data stored in the current transaction. If no data are stored,
2809 it returns a nil value.
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01002810
2811 :param class_AppletHTTP applet: An :ref:`applethttp_class`
Bertrand Jacquin874a35c2018-09-10 21:26:07 +01002812 :returns: the opaque data previously stored, or nil if nothing is
2813 available.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01002814 :see: :js:func:`AppletHTTP.set_priv`
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01002815
2816.. js:function:: AppletHTTP.set_priv(applet, data)
2817
2818 Store any data in the current HAProxy transaction. This action replace the
2819 old stored data.
2820
2821 :param class_AppletHTTP applet: An :ref:`applethttp_class`
2822 :param opaque data: The data which is stored in the transaction.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01002823 :see: :js:func:`AppletHTTP.get_priv`
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01002824
Tim Duesterhus4e172c92020-05-19 13:49:42 +02002825.. js:function:: AppletHTTP.set_var(applet, var, value[, ifexist])
Thierry FOURNIER / OZON.IOc1edafe2016-12-12 16:25:30 +01002826
2827 Converts a Lua type in a HAProxy type and store it in a variable <var>.
2828
2829 :param class_AppletHTTP applet: An :ref:`applethttp_class`
2830 :param string var: The variable name according with the HAProxy variable syntax.
2831 :param type value: The value associated to the variable. The type ca be string or
2832 integer.
Tim Duesterhus4e172c92020-05-19 13:49:42 +02002833 :param boolean ifexist: If this parameter is set to a truthy value the variable
2834 will only be set if it was defined elsewhere (i.e. used
Willy Tarreau7978c5c2021-09-07 14:24:07 +02002835 within the configuration). For global variables (using the
2836 "proc" scope), they will only be updated and never created.
2837 It is highly recommended to
Tim Duesterhus4e172c92020-05-19 13:49:42 +02002838 always set this to true.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01002839 :see: :js:func:`AppletHTTP.unset_var`
2840 :see: :js:func:`AppletHTTP.get_var`
Thierry FOURNIER / OZON.IOc1edafe2016-12-12 16:25:30 +01002841
2842.. js:function:: AppletHTTP.unset_var(applet, var)
2843
2844 Unset the variable <var>.
2845
2846 :param class_AppletHTTP applet: An :ref:`applethttp_class`
2847 :param string var: The variable name according with the HAProxy variable syntax.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01002848 :see: :js:func:`AppletHTTP.set_var`
2849 :see: :js:func:`AppletHTTP.get_var`
Thierry FOURNIER / OZON.IOc1edafe2016-12-12 16:25:30 +01002850
2851.. js:function:: AppletHTTP.get_var(applet, var)
2852
2853 Returns data stored in the variable <var> converter in Lua type.
2854
2855 :param class_AppletHTTP applet: An :ref:`applethttp_class`
2856 :param string var: The variable name according with the HAProxy variable syntax.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01002857 :see: :js:func:`AppletHTTP.set_var`
2858 :see: :js:func:`AppletHTTP.unset_var`
Thierry FOURNIER / OZON.IOc1edafe2016-12-12 16:25:30 +01002859
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002860.. _applettcp_class:
2861
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002862AppletTCP class
2863===============
2864
2865.. js:class:: AppletTCP
2866
2867 This class is used with applets that requires the 'tcp' mode. The tcp applet
2868 can be registered with the *core.register_service()* function. They are used
2869 for processing a tcp stream like a server in back of HAProxy.
2870
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002871.. js:attribute:: AppletTCP.c
2872
2873 :returns: A :ref:`converters_class`
2874
2875 This attribute contains a Converters class object.
2876
2877.. js:attribute:: AppletTCP.sc
2878
2879 :returns: A :ref:`converters_class`
2880
2881 This attribute contains a Converters class object. The
2882 functions of this object returns always a string.
2883
2884.. js:attribute:: AppletTCP.f
2885
2886 :returns: A :ref:`fetches_class`
2887
2888 This attribute contains a Fetches class object.
2889
2890.. js:attribute:: AppletTCP.sf
2891
2892 :returns: A :ref:`fetches_class`
2893
2894 This attribute contains a Fetches class object.
2895
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01002896.. js:function:: AppletTCP.getline(applet)
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002897
2898 This function returns a string containing one line from the stream. If the
2899 data returned doesn't contains a final '\\n' its assumed than its the last
2900 available data before the end of stream.
2901
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01002902 :param class_AppletTCP applet: An :ref:`applettcp_class`
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002903 :returns: a string. The string can be empty if we reach the end of the stream.
2904
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01002905.. js:function:: AppletTCP.receive(applet, [size])
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002906
2907 Reads data from the TCP stream, according to the specified read *size*. If the
2908 *size* is missing, the function tries to read all the content of the stream
2909 until the end.
2910
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01002911 :param class_AppletTCP applet: An :ref:`applettcp_class`
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002912 :param integer size: the required read size.
Ilya Shipitsin11057a32020-06-21 21:18:27 +05002913 :returns: always return a string,the string can be empty is the connection is
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002914 closed.
2915
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01002916.. js:function:: AppletTCP.send(appletmsg)
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002917
2918 Send the message on the stream.
2919
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01002920 :param class_AppletTCP applet: An :ref:`applettcp_class`
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002921 :param string msg: the message to send.
2922
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01002923.. js:function:: AppletTCP.get_priv(applet)
2924
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01002925 Return Lua data stored in the current transaction. If no data are stored,
2926 it returns a nil value.
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01002927
2928 :param class_AppletTCP applet: An :ref:`applettcp_class`
Bertrand Jacquin874a35c2018-09-10 21:26:07 +01002929 :returns: the opaque data previously stored, or nil if nothing is
2930 available.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01002931 :see: :js:func:`AppletTCP.set_priv`
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01002932
2933.. js:function:: AppletTCP.set_priv(applet, data)
2934
2935 Store any data in the current HAProxy transaction. This action replace the
2936 old stored data.
2937
2938 :param class_AppletTCP applet: An :ref:`applettcp_class`
2939 :param opaque data: The data which is stored in the transaction.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01002940 :see: :js:func:`AppletTCP.get_priv`
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01002941
Tim Duesterhus4e172c92020-05-19 13:49:42 +02002942.. js:function:: AppletTCP.set_var(applet, var, value[, ifexist])
Thierry FOURNIER / OZON.IOc1edafe2016-12-12 16:25:30 +01002943
2944 Converts a Lua type in a HAProxy type and stores it in a variable <var>.
2945
2946 :param class_AppletTCP applet: An :ref:`applettcp_class`
2947 :param string var: The variable name according with the HAProxy variable syntax.
2948 :param type value: The value associated to the variable. The type can be string or
2949 integer.
Tim Duesterhus4e172c92020-05-19 13:49:42 +02002950 :param boolean ifexist: If this parameter is set to a truthy value the variable
2951 will only be set if it was defined elsewhere (i.e. used
Willy Tarreau7978c5c2021-09-07 14:24:07 +02002952 within the configuration). For global variables (using the
2953 "proc" scope), they will only be updated and never created.
2954 It is highly recommended to
Tim Duesterhus4e172c92020-05-19 13:49:42 +02002955 always set this to true.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01002956 :see: :js:func:`AppletTCP.unset_var`
2957 :see: :js:func:`AppletTCP.get_var`
Thierry FOURNIER / OZON.IOc1edafe2016-12-12 16:25:30 +01002958
2959.. js:function:: AppletTCP.unset_var(applet, var)
2960
2961 Unsets the variable <var>.
2962
2963 :param class_AppletTCP applet: An :ref:`applettcp_class`
2964 :param string var: The variable name according with the HAProxy variable syntax.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01002965 :see: :js:func:`AppletTCP.unset_var`
2966 :see: :js:func:`AppletTCP.set_var`
Thierry FOURNIER / OZON.IOc1edafe2016-12-12 16:25:30 +01002967
2968.. js:function:: AppletTCP.get_var(applet, var)
2969
2970 Returns data stored in the variable <var> converter in Lua type.
2971
2972 :param class_AppletTCP applet: An :ref:`applettcp_class`
2973 :param string var: The variable name according with the HAProxy variable syntax.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01002974 :see: :js:func:`AppletTCP.unset_var`
2975 :see: :js:func:`AppletTCP.set_var`
Thierry FOURNIER / OZON.IOc1edafe2016-12-12 16:25:30 +01002976
Adis Nezirovic8878f8e2018-07-13 12:18:33 +02002977StickTable class
2978================
2979
2980.. js:class:: StickTable
2981
2982 **context**: task, action, sample-fetch
2983
2984 This class can be used to access the HAProxy stick tables from Lua.
2985
2986.. js:function:: StickTable.info()
2987
2988 Returns stick table attributes as a Lua table. See HAProxy documentation for
Ilya Shipitsin2272d8a2020-12-21 01:22:40 +05002989 "stick-table" for canonical info, or check out example below.
Adis Nezirovic8878f8e2018-07-13 12:18:33 +02002990
2991 :returns: Lua table
2992
2993 Assume our table has IPv4 key and gpc0 and conn_rate "columns":
2994
2995.. code-block:: lua
2996
2997 {
2998 expire=<int>, # Value in ms
2999 size=<int>, # Maximum table size
3000 used=<int>, # Actual number of entries in table
3001 data={ # Data columns, with types as key, and periods as values
3002 (-1 if type is not rate counter)
3003 conn_rate=<int>,
3004 gpc0=-1
3005 },
3006 length=<int>, # max string length for string table keys, key length
3007 # otherwise
3008 nopurge=<boolean>, # purge oldest entries when table is full
3009 type="ip" # can be "ip", "ipv6", "integer", "string", "binary"
3010 }
3011
3012.. js:function:: StickTable.lookup(key)
3013
3014 Returns stick table entry for given <key>
3015
3016 :param string key: Stick table key (IP addresses and strings are supported)
3017 :returns: Lua table
3018
3019.. js:function:: StickTable.dump([filter])
3020
3021 Returns all entries in stick table. An optional filter can be used
3022 to extract entries with specific data values. Filter is a table with valid
3023 comparison operators as keys followed by data type name and value pairs.
3024 Check out the HAProxy docs for "show table" for more details. For the
3025 reference, the supported operators are:
3026 "eq", "ne", "le", "lt", "ge", "gt"
3027
3028 For large tables, execution of this function can take a long time (for
3029 HAProxy standards). That's also true when filter is used, so take care and
3030 measure the impact.
3031
3032 :param table filter: Stick table filter
3033 :returns: Stick table entries (table)
3034
3035 See below for example filter, which contains 4 entries (or comparisons).
3036 (Maximum number of filter entries is 4, defined in the source code)
3037
3038.. code-block:: lua
3039
3040 local filter = {
3041 {"gpc0", "gt", 30}, {"gpc1", "gt", 20}}, {"conn_rate", "le", 10}
3042 }
3043
Christopher Faulet0f3c8902020-01-31 18:57:12 +01003044.. _action_class:
3045
3046Action class
3047=============
3048
3049.. js:class:: Act
3050
3051 **context**: action
3052
3053 This class contains all return codes an action may return. It is the lua
3054 equivalent to HAProxy "ACT_RET_*" code.
3055
3056.. code-block:: lua
3057
3058 core.register_action("deny", { "http-req" }, function (txn)
3059 return act.DENY
3060 end)
3061..
3062.. js:attribute:: act.CONTINUE
3063
3064 This attribute is an integer (0). It instructs HAProxy to continue the current
3065 ruleset processing on the message. It is the default return code for a lua
3066 action.
3067
3068 :returns: integer
3069
3070.. js:attribute:: act.STOP
3071
3072 This attribute is an integer (1). It instructs HAProxy to stop the current
3073 ruleset processing on the message.
3074
3075.. js:attribute:: act.YIELD
3076
3077 This attribute is an integer (2). It instructs HAProxy to temporarily pause
3078 the message processing. It will be resumed later on the same rule. The
3079 corresponding lua script is re-executed for the start.
3080
3081.. js:attribute:: act.ERROR
3082
3083 This attribute is an integer (3). It triggers an internal errors The message
3084 processing is stopped and the transaction is terminated. For HTTP streams, an
3085 HTTP 500 error is returned to the client.
3086
3087 :returns: integer
3088
3089.. js:attribute:: act.DONE
3090
3091 This attribute is an integer (4). It instructs HAProxy to stop the message
3092 processing.
3093
3094 :returns: integer
3095
3096.. js:attribute:: act.DENY
3097
3098 This attribute is an integer (5). It denies the current message. The message
3099 processing is stopped and the transaction is terminated. For HTTP streams, an
3100 HTTP 403 error is returned to the client if the deny is returned during the
3101 request analysis. During the response analysis, an HTTP 502 error is returned
3102 and the server response is discarded.
3103
3104 :returns: integer
3105
3106.. js:attribute:: act.ABORT
3107
3108 This attribute is an integer (6). It aborts the current message. The message
3109 processing is stopped and the transaction is terminated. For HTTP streams,
Willy Tarreau714f3452021-05-09 06:47:26 +02003110 HAProxy assumes a response was already sent to the client. From the Lua
Christopher Faulet0f3c8902020-01-31 18:57:12 +01003111 actions point of view, when this code is used, the transaction is terminated
3112 with no reply.
3113
3114 :returns: integer
3115
3116.. js:attribute:: act.INVALID
3117
3118 This attribute is an integer (7). It triggers an internal errors. The message
3119 processing is stopped and the transaction is terminated. For HTTP streams, an
3120 HTTP 400 error is returned to the client if the error is returned during the
3121 request analysis. During the response analysis, an HTTP 502 error is returned
3122 and the server response is discarded.
3123
3124 :returns: integer
Adis Nezirovic8878f8e2018-07-13 12:18:33 +02003125
Christopher Faulet2c2c2e32020-01-31 19:07:52 +01003126.. js:function:: act:wake_time(milliseconds)
3127
3128 **context**: action
3129
3130 Set the script pause timeout to the specified time, defined in
3131 milliseconds.
3132
3133 :param integer milliseconds: the required milliseconds.
3134
3135 This function may be used when a lua action returns `act.YIELD`, to force its
3136 wake-up at most after the specified number of milliseconds.
3137
Christopher Faulet5a2c6612021-08-15 20:35:25 +02003138.. _filter_class:
3139
3140Filter class
3141=============
3142
3143.. js:class:: filter
3144
3145 **context**: filter
3146
3147 This class contains return codes some filter callback functions may return. It
3148 also contains configuration flags and some helper functions. To understand how
3149 the filter API works, see `doc/internal/filters.txt` documentation.
3150
3151.. js:attribute:: filter.CONTINUE
3152
3153 This attribute is an integer (1). It may be returned by some filter callback
3154 functions to instruct this filtering step is finished for this filter.
3155
3156.. js:attribute:: filter.WAIT
3157
3158 This attribute is an integer (0). It may be returned by some filter callback
3159 functions to instruct the filtering must be paused, waiting for more data or
3160 for an external event depending on this filter.
3161
3162.. js:attribute:: filter.ERROR
3163
3164 This attribute is an integer (-1). It may be returned by some filter callback
3165 functions to trigger an error.
3166
3167.. js:attribute:: filter.FLT_CFG_FL_HTX
3168
3169 This attribute is a flag corresponding to the filter flag FLT_CFG_FL_HTX. When
3170 it is set for a filter, it means the filter is able to filter HTTP streams.
3171
3172.. js:function:: filter.register_data_filter(chn)
3173
3174 **context**: filter
3175
3176 Enable the data filtering on the channel **chn** for the current filter. It
Ilya Shipitsinff0f2782021-08-22 22:18:07 +05003177 may be called at any time from any callback functions proceeding the data
Christopher Faulet5a2c6612021-08-15 20:35:25 +02003178 analysis.
3179
3180 :param class_Channel chn: A :ref:`channel_class`.
3181
3182.. js:function:: filter.unregister_data_filter(chn)
3183
3184 **context**: filter
3185
3186 Disable the data filtering on the channel **chn** for the current filter. It
3187 may be called at any time from any callback functions.
3188
3189 :param class_Channel chn: A :ref:`channel_class`.
3190
3191.. js:function:: filter.wake_time(milliseconds)
3192
3193 **context**: filter
3194
3195 Set the script pause timeout to the specified time, defined in
3196 milliseconds.
3197
3198 :param integer milliseconds: the required milliseconds.
3199
3200 This function may be used from any lua filter callback function to force its
3201 wake-up at most after the specified number of milliseconds. Especially, when
3202 `filter.CONTINUE` is returned.
3203
3204
3205A filters is declared using :js:func:`core.register_filter()` function. The
3206provided class will be used to instantiate filters. It may define following
3207attributes:
3208
3209* id: The filter identifier. It is a string that identifies the filter and is
3210 optional.
3211
3212* flags: The filter flags. Only :js:attr:`filter.FLT_CFG_FL_HTX` may be set for now.
3213
3214Such filter class must also define all required callback functions in the
3215following list. Note that :js:func:`Filter.new()` must be defined otherwise the
Ilya Shipitsinff0f2782021-08-22 22:18:07 +05003216filter is ignored. Others are optional.
Christopher Faulet5a2c6612021-08-15 20:35:25 +02003217
3218* .. js:function:: FILTER.new()
3219
3220 Called to instantiate a new filter. This function must be defined.
3221
3222 :returns: a Lua object that will be used as filter instance for the current
3223 stream.
3224
3225* .. js:function:: FILTER.start_analyze(flt, txn, chn)
3226
3227 Called when the analysis starts on the channel **chn**.
3228
3229* .. js:function:: FILTER.end_analyze(flt, txn, chn)
3230
3231 Called when the analysis ends on the channel **chn**.
3232
3233* .. js:function:: FILTER.http_headers(flt, txn, http_msg)
3234
3235 Called just before the HTTP payload analysis and after any processing on the
3236 HTTP message **http_msg**. This callback functions is only called for HTTP
3237 streams.
3238
3239* .. js:function:: FILTER.http_payload(flt, txn, http_msg)
3240
3241 Called during the HTTP payload analysis on the HTTP message **http_msg**. This
3242 callback functions is only called for HTTP streams.
3243
3244* .. js:function:: FILTER.http_end(flt, txn, http_msg)
3245
3246 Called after the HTTP payload analysis on the HTTP message **http_msg**. This
3247 callback functions is only called for HTTP streams.
3248
3249* .. js:function:: FILTER.tcp_payload(flt, txn, chn)
3250
3251 Called during the TCP payload analysis on the channel **chn**.
3252
3253Here is an full example:
3254
3255.. code-block:: lua
3256
3257 Trace = {}
3258 Trace.id = "Lua trace filter"
3259 Trace.flags = filter.FLT_CFG_FL_HTX;
3260 Trace.__index = Trace
3261
3262 function Trace:new()
3263 local trace = {}
3264 setmetatable(trace, Trace)
3265 trace.req_len = 0
3266 trace.res_len = 0
3267 return trace
3268 end
3269
3270 function Trace:start_analyze(txn, chn)
3271 if chn:is_resp() then
3272 print("Start response analysis")
3273 else
3274 print("Start request analysis")
3275 end
3276 filter.register_data_filter(self, chn)
3277 end
3278
3279 function Trace:end_analyze(txn, chn)
3280 if chn:is_resp() then
3281 print("End response analysis: "..self.res_len.." bytes filtered")
3282 else
3283 print("End request analysis: "..self.req_len.." bytes filtered")
3284 end
3285 end
3286
3287 function Trace:http_headers(txn, http_msg)
3288 stline = http_msg:get_stline()
3289 if http_msg.channel:is_resp() then
3290 print("response:")
3291 print(stline.version.." "..stline.code.." "..stline.reason)
3292 else
3293 print("request:")
3294 print(stline.method.." "..stline.uri.." "..stline.version)
3295 end
3296
3297 for n, hdrs in pairs(http_msg:get_headers()) do
3298 for i,v in pairs(hdrs) do
3299 print(n..": "..v)
3300 end
3301 end
3302 return filter.CONTINUE
3303 end
3304
3305 function Trace:http_payload(txn, http_msg)
3306 body = http_msg:body(-20000)
3307 if http_msg.channel:is_resp() then
3308 self.res_len = self.res_len + body:len()
3309 else
3310 self.req_len = self.req_len + body:len()
3311 end
3312 end
3313
3314 core.register_filter("trace", Trace, function(trace, args)
3315 return trace
3316 end)
3317
3318..
3319
3320.. _httpmessage_class:
3321
3322HTTPMessage class
3323===================
3324
3325.. js:class:: HTTPMessage
3326
3327 **context**: filter
3328
3329 This class contains all functions to manipulate an HTTP message. For now, this
3330 class is only available from a filter context.
3331
3332.. js:function:: HTTPMessage.add_header(http_msg, name, value)
3333
3334 Appends an HTTP header field in the HTTP message **http_msg** whose name is
3335 specified in **name** and whose value is defined in **value**.
3336
3337 :param class_httpmessage http_msg: The manipulated HTTP message.
3338 :param string name: The header name.
3339 :param string value: The header value.
3340
3341.. js:function:: HTTPMessage.append(http_msg, string)
3342
3343 This function copies the string **string** at the end of incoming data of the
3344 HTTP message **http_msg**. The function returns the copied length on success
3345 or -1 if data cannot be copied.
3346
3347 Same that :js:func:`HTTPMessage.insert(http_msg, string, http_msg:input())`.
3348
3349 :param class_httpmessage http_msg: The manipulated HTTP message.
3350 :param string string: The data to copied into incoming data.
3351 :returns: an integer containing the amount of bytes copied or -1.
3352
3353.. js:function:: HTTPMessage.body(http_msgl[, offset[, length]])
3354
3355 This function returns **length** bytes of incoming data from the HTTP message
3356 **http_msg**, starting at the offset **offset**. The data are not removed from
3357 the buffer.
3358
3359 By default, if no length is provided, all incoming data found, starting at the
3360 given offset, are returned. If **length** is set to -1, the function tries to
3361 retrieve a maximum of data. Because it is called in the filter context, it
3362 never yield. Do not providing an offset is the same that setting it to 0. A
Ilya Shipitsinff0f2782021-08-22 22:18:07 +05003363 positive offset is relative to the beginning of incoming data of the
Christopher Faulet5a2c6612021-08-15 20:35:25 +02003364 http_message buffer while negative offset is relative to their end.
3365
3366 If there is no incoming data and the HTTP message can't receive more data, a 'nil'
3367 value is returned.
3368
3369 :param class_httpmessage http_msg: The manipulated HTTP message.
3370 :param integer offset: *optional* The offset in incoming data to start to get
3371 data. 0 by default. May be negative to be relative to
3372 the end of incoming data.
3373 :param integer length: *optional* The expected length of data to retrieve. All
3374 incoming data by default. May be set to -1 to get a
3375 maximum of data.
3376 :returns: a string containing the data found or nil.
3377
3378.. js:function:: HTTPMessage.eom(http_msg)
3379
3380 This function returns true if the end of message is reached for the HTTP
3381 message **http_msg**.
3382
3383 :param class_httpmessage http_msg: The manipulated HTTP message.
3384 :returns: an integer containing the amount of available bytes.
3385
3386.. js:function:: HTTPMessage.del_header(http_msg, name)
3387
3388 Removes all HTTP header fields in the HTTP message **http_msg** whose name is
3389 specified in **name**.
3390
3391 :param class_httpmessage http_msg: The manipulated http message.
3392 :param string name: The header name.
3393
3394.. js:function:: HTTPMessage.get_headers(http_msg)
3395
3396 Returns a table containing all the headers of the HTTP message **http_msg**.
3397
3398 :param class_httpmessage http_msg: The manipulated http message.
3399 :returns: table of headers.
3400
3401 This is the form of the returned table:
3402
3403.. code-block:: lua
3404
3405 http_msg:get_headers()['<header-name>'][<header-index>] = "<header-value>"
3406
3407 local hdr = http_msg:get_headers()
3408 hdr["host"][0] = "www.test.com"
3409 hdr["accept"][0] = "audio/basic q=1"
3410 hdr["accept"][1] = "audio/*, q=0.2"
3411 hdr["accept"][2] = "*.*, q=0.1"
3412..
3413
3414.. js:function:: HTTPMessage.get_stline(http_msg)
3415
3416 Returns a table containing the start-line of the HTTP message **http_msg**.
3417
3418 :param class_httpmessage http_msg: The manipulated http message.
3419 :returns: the start-line.
3420
3421 This is the form of the returned table:
3422
3423.. code-block:: lua
3424
3425 -- for the request :
3426 {"method" = string, "uri" = string, "version" = string}
3427
3428 -- for the response:
3429 {"version" = string, "code" = string, "reason" = string}
3430..
3431
3432.. js:function:: HTTPMessage.forward(http_msg, length)
3433
3434 This function forwards **length** bytes of data from the HTTP message
3435 **http_msg**. Because it is called in the filter context, it never yield. Only
3436 available incoming data may be forwarded, event if the requested length
3437 exceeds the available amount of incoming data. It returns the amount of data
3438 forwarded.
3439
3440 :param class_httpmessage http_msg: The manipulated HTTP message.
3441 :param integer int: The amount of data to forward.
3442
3443.. js:function:: HTTPMessage.input(http_msg)
3444
3445 This function returns the length of incoming data in the HTTP message
3446 **http_msg** from the calling filter point of view.
3447
3448 :param class_httpmessage http_msg: The manipulated HTTP message.
3449 :returns: an integer containing the amount of available bytes.
3450
3451.. js:function:: HTTPMessage.insert(http_msg, string[, offset])
3452
3453 This function copies the string **string** at the offset **offset** in
3454 incoming data of the HTTP message **http_msg**. The function returns the
3455 copied length on success or -1 if data cannot be copied.
3456
3457 By default, if no offset is provided, the string is copied in front of
Ilya Shipitsinff0f2782021-08-22 22:18:07 +05003458 incoming data. A positive offset is relative to the beginning of incoming data
Christopher Faulet5a2c6612021-08-15 20:35:25 +02003459 of the HTTP message while negative offset is relative to their end.
3460
3461 :param class_httpmessage http_msg: The manipulated HTTP message.
3462 :param string string: The data to copied into incoming data.
3463 :param integer offset: *optional* The offset in incomding data where to copied
3464 data. 0 by default. May be negative to be relative to
3465 the end of incoming data.
3466 :returns: an integer containing the amount of bytes copied or -1.
3467
3468.. js:function:: HTTPMessage.is_full(http_msg)
3469
3470 This function returns true if the HTTP message **http_msg** is full.
3471
3472 :param class_httpmessage http_msg: The manipulated HTTP message.
3473 :returns: a boolean
3474
3475.. js:function:: HTTPMessage.is_resp(http_msg)
3476
3477 This function returns true if the HTTP message **http_msg** is the response
3478 one.
3479
3480 :param class_httpmessage http_msg: The manipulated HTTP message.
3481 :returns: a boolean
3482
3483.. js:function:: HTTPMessage.may_recv(http_msg)
3484
3485 This function returns true if the HTTP message **http_msg** may still receive
3486 data.
3487
3488 :param class_httpmessage http_msg: The manipulated HTTP message.
3489 :returns: a boolean
3490
3491.. js:function:: HTTPMessage.output(http_msg)
3492
3493 This function returns the length of outgoing data of the HTTP message
3494 **http_msg**.
3495
3496 :param class_httpmessage http_msg: The manipulated HTTP message.
3497 :returns: an integer containing the amount of available bytes.
3498
3499.. js:function:: HTTPMessage.prepend(http_msg, string)
3500
3501 This function copies the string **string** in front of incoming data of the
3502 HTTP message **http_msg**. The function returns the copied length on success
3503 or -1 if data cannot be copied.
3504
3505 Same that :js:func:`HTTPMessage.insert(http_msg, string, 0)`.
3506
3507 :param class_httpmessage http_msg: The manipulated HTTP message.
3508 :param string string: The data to copied into incoming data.
3509 :returns: an integer containing the amount of bytes copied or -1.
3510
3511.. js:function:: HTTPMessage.remove(http_msg[, offset[, length]])
3512
3513 This function removes **length** bytes of incoming data of the HTTP message
3514 **http_msg**, starting at offset **offset**. This function returns number of
3515 bytes removed on success.
3516
3517 By default, if no length is provided, all incoming data, starting at the given
3518 offset, are removed. Do not providing an offset is the same that setting it
Ilya Shipitsinff0f2782021-08-22 22:18:07 +05003519 to 0. A positive offset is relative to the beginning of incoming data of the
Christopher Faulet5a2c6612021-08-15 20:35:25 +02003520 HTTP message while negative offset is relative to their end.
3521
3522 :param class_httpmessage http_msg: The manipulated HTTP message.
3523 :param integer offset: *optional* The offset in incomding data where to start
3524 to remove data. 0 by default. May be negative to
3525 be relative to the end of incoming data.
3526 :param integer length: *optional* The length of data to remove. All incoming
3527 data by default.
3528 :returns: an integer containing the amount of bytes removed.
3529
3530.. js:function:: HTTPMessage.rep_header(http_msg, name, regex, replace)
3531
3532 Matches the regular expression in all occurrences of header field **name**
3533 according to regex **regex**, and replaces them with the string **replace**.
3534 The replacement value can contain back references like \1, \2, ... This
3535 function acts on whole header lines, regardless of the number of values they
3536 may contain.
3537
3538 :param class_httpmessage http_msg: The manipulated HTTP message.
3539 :param string name: The header name.
3540 :param string regex: The match regular expression.
3541 :param string replace: The replacement value.
3542
3543.. js:function:: HTTPMessage.rep_value(http_msg, name, regex, replace)
3544
3545 Matches the regular expression on every comma-delimited value of header field
3546 **name** according to regex **regex**, and replaces them with the string
3547 **replace**. The replacement value can contain back references like \1, \2,
3548 ...
3549
3550 :param class_httpmessage http_msg: The manipulated HTTP message.
3551 :param string name: The header name.
3552 :param string regex: The match regular expression.
3553 :param string replace: The replacement value.
3554
3555.. js:function:: HTTPMessage.send(http_msg, string)
3556
3557 This function required immediate send of the string **string**. It means the
Ilya Shipitsinff0f2782021-08-22 22:18:07 +05003558 string is copied at the beginning of incoming data of the HTTP message
Christopher Faulet5a2c6612021-08-15 20:35:25 +02003559 **http_msg** and immediately forwarded. Because it is called in the filter
3560 context, it never yield.
3561
3562 :param class_httpmessage http_msg: The manipulated HTTP message.
3563 :param string string: The data to send.
3564 :returns: an integer containing the amount of bytes copied or -1.
3565
3566.. js:function:: HTTPMessage.set(http_msg, string[, offset[, length]])
3567
3568 This function replace **length** bytes of incoming data of the HTTP message
3569 **http_msg**, starting at offset **offset**, by the string **string**. The
3570 function returns the copied length on success or -1 if data cannot be copied.
3571
3572 By default, if no length is provided, all incoming data, starting at the given
3573 offset, are replaced. Do not providing an offset is the same that setting it
Ilya Shipitsinff0f2782021-08-22 22:18:07 +05003574 to 0. A positive offset is relative to the beginning of incoming data of the
Christopher Faulet5a2c6612021-08-15 20:35:25 +02003575 HTTP message while negative offset is relative to their end.
3576
3577 :param class_httpmessage http_msg: The manipulated HTTP message.
3578 :param string string: The data to copied into incoming data.
3579 :param integer offset: *optional* The offset in incomding data where to start
3580 the data replacement. 0 by default. May be negative to
3581 be relative to the end of incoming data.
3582 :param integer length: *optional* The length of data to replace. All incoming
3583 data by default.
3584 :returns: an integer containing the amount of bytes copied or -1.
3585
3586.. js:function:: HTTPMessage.set_eom(http_msg)
3587
3588 This function set the end of message for the HTTP message **http_msg**.
3589
3590 :param class_httpmessage http_msg: The manipulated HTTP message.
3591
3592.. js:function:: HTTPMessage.set_header(http_msg, name, value)
3593
3594 This variable replace all occurrence of all header matching the name **name**,
3595 by only one containing the value **value**.
3596
3597 :param class_httpmessage http_msg: The manipulated HTTP message.
3598 :param string name: The header name.
3599 :param string value: The header value.
3600
3601 This function does the same work as the following code:
3602
3603.. code-block:: lua
3604
3605 http_msg:del_header("header")
3606 http_msg:add_header("header", "value")
3607..
3608
3609.. js:function:: HTTPMessage.set_method(http_msg, method)
3610
3611 Rewrites the request method with the string **method**. The HTTP message
3612 **http_msg** must be the request.
3613
3614 :param class_httpmessage http_msg: The manipulated HTTP message.
3615 :param string method: The new method.
3616
3617.. js:function:: HTTPMessage.set_path(http_msg, path)
3618
3619 Rewrites the request path with the string **path**. The HTTP message
3620 **http_msg** must be the request.
3621
3622 :param class_httpmessage http_msg: The manipulated HTTP message.
3623 :param string method: The new method.
3624
3625.. js:function:: HTTPMessage.set_query(http_msg, query)
3626
3627 Rewrites the request's query string which appears after the first question
3628 mark ("?") with the string **query**. The HTTP message **http_msg** must be
3629 the request.
3630
3631 :param class_httpmessage http_msg: The manipulated HTTP message.
3632 :param string query: The new query.
3633
3634.. js:function:: HTTPMessage.set_status(http_msg, status[, reason])
3635
Ilya Shipitsinff0f2782021-08-22 22:18:07 +05003636 Rewrites the response status code with the integer **code** and optional the
Christopher Faulet5a2c6612021-08-15 20:35:25 +02003637 reason **reason**. If no custom reason is provided, it will be generated from
3638 the status. The HTTP message **http_msg** must be the response.
3639
3640 :param class_httpmessage http_msg: The manipulated HTTP message.
3641 :param integer status: The new response status code.
3642 :param string reason: The new response reason (optional).
3643
3644.. js:function:: HTTPMessage.set_uri(http_msg, uri)
3645
3646 Rewrites the request URI with the string **uri**. The HTTP message
3647 **http_msg** must be the request.
3648
3649 :param class_httpmessage http_msg: The manipulated HTTP message.
3650 :param string uri: The new uri.
3651
3652.. js:function:: HTTPMessage.unset_eom(http_msg)
3653
3654 This function remove the end of message for the HTTP message **http_msg**.
3655
3656 :param class_httpmessage http_msg: The manipulated HTTP message.
3657
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01003658External Lua libraries
3659======================
3660
3661A lot of useful lua libraries can be found here:
3662
3663* `https://lua-toolbox.com/ <https://lua-toolbox.com/>`_
3664
Ilya Shipitsin2075ca82020-03-06 23:22:22 +05003665Redis client library:
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01003666
3667* `https://github.com/nrk/redis-lua <https://github.com/nrk/redis-lua>`_
3668
3669This is an example about the usage of the Redis library with HAProxy. Note that
3670each call of any function of this library can throw an error if the socket
3671connection fails.
3672
3673.. code-block:: lua
3674
3675 -- load the redis library
3676 local redis = require("redis");
3677
3678 function do_something(txn)
3679
3680 -- create and connect new tcp socket
3681 local tcp = core.tcp();
3682 tcp:settimeout(1);
3683 tcp:connect("127.0.0.1", 6379);
3684
3685 -- use the redis library with this new socket
3686 local client = redis.connect({socket=tcp});
3687 client:ping();
3688
3689 end
3690
3691OpenSSL:
3692
3693* `http://mkottman.github.io/luacrypto/index.html
3694 <http://mkottman.github.io/luacrypto/index.html>`_
3695
3696* `https://github.com/brunoos/luasec/wiki
3697 <https://github.com/brunoos/luasec/wiki>`_