blob: ee30b73a297ca2b4a017f74d1646e33cd7cf45f5 [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
Aurelien DARRAGONc84899c2023-02-20 18:18:59 +010023functions. Lua has 8 execution contexts.
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
Aurelien DARRAGONc84899c2023-02-20 18:18:59 +0100758. The **event context**: Inside a function that handles events subscribed
76 through `core.event_sub()` or `Server.event_sub()`.
77
Christopher Faulet5a2c6612021-08-15 20:35:25 +020078
Thierry FOURNIER17bd1522015-03-11 20:31:00 +010079HAProxy Lua Hello world
80-----------------------
81
82HAProxy configuration file (`hello_world.conf`):
83
84::
85
86 global
87 lua-load hello_world.lua
88
89 listen proxy
90 bind 127.0.0.1:10001
Thierry FOURNIERa2d02252015-10-01 15:00:42 +020091 tcp-request inspect-delay 1s
92 tcp-request content use-service lua.hello_world
Thierry FOURNIER17bd1522015-03-11 20:31:00 +010093
94HAProxy Lua file (`hello_world.lua`):
95
96.. code-block:: lua
97
Thierry FOURNIERa2d02252015-10-01 15:00:42 +020098 core.register_service("hello_world", "tcp", function(applet)
99 applet:send("hello world\n")
100 end)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100101
102How to start HAProxy for testing this configuration:
103
104::
105
106 ./haproxy -f hello_world.conf
107
108On other terminal, you can test with telnet:
109
110::
111
112 #:~ telnet 127.0.0.1 10001
113 hello world
114
Thierry Fournierae6b5682022-09-19 09:04:16 +0200115Usage of load parameters
116------------------------
117
Ilya Shipitsin4a689da2022-10-29 09:34:32 +0500118HAProxy lua-load(-per-thread) directives allow a list of parameters after
Thierry Fournierae6b5682022-09-19 09:04:16 +0200119the lua file name. These parameters are accessible through an array of args
120using this code `local args = table.pack(...)` in the body of loaded file.
121
122Below, a new version of the hello world using load parameters
123
124HAProxy configuration file (`hello_world.conf`):
125
126::
127
128 global
129 lua-load hello_world.lua "this is not an hello world"
130
131 listen proxy
132 bind 127.0.0.1:10001
133 tcp-request inspect-delay 1s
134 tcp-request content use-service lua.hello_world
135
136HAProxy Lua file (`hello_world.lua`):
137
138.. code-block:: lua
139
140 local args = table.pack(...)
141
142 core.register_service("hello_world", "tcp", function(applet)
143 applet:send(args[1] .. "\n")
144 end)
145
146
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100147Core class
148==========
149
150.. js:class:: core
151
152 The "core" class contains all the HAProxy core functions. These function are
Aurelien DARRAGON53901f42022-10-13 19:49:42 +0200153 useful for the controlling of the execution flow, registering hooks, manipulating
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100154 global maps or ACL, ...
155
156 "core" class is basically provided with HAProxy. No `require` line is
157 required to uses these function.
158
David Carlier61fdf8b2015-10-02 11:59:38 +0100159 The "core" class is static, it is not possible to create a new object of this
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100160 type.
161
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100162.. js:attribute:: core.emerg
163
Thierry FOURNIERdc595002015-12-21 11:13:52 +0100164 :returns: integer
165
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100166 This attribute is an integer, it contains the value of the loglevel "emergency" (0).
167
168.. js:attribute:: core.alert
169
Thierry FOURNIERdc595002015-12-21 11:13:52 +0100170 :returns: integer
171
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100172 This attribute is an integer, it contains the value of the loglevel "alert" (1).
173
174.. js:attribute:: core.crit
175
Thierry FOURNIERdc595002015-12-21 11:13:52 +0100176 :returns: integer
177
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100178 This attribute is an integer, it contains the value of the loglevel "critical" (2).
179
180.. js:attribute:: core.err
181
Thierry FOURNIERdc595002015-12-21 11:13:52 +0100182 :returns: integer
183
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100184 This attribute is an integer, it contains the value of the loglevel "error" (3).
185
186.. js:attribute:: core.warning
187
Thierry FOURNIERdc595002015-12-21 11:13:52 +0100188 :returns: integer
189
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100190 This attribute is an integer, it contains the value of the loglevel "warning" (4).
191
192.. js:attribute:: core.notice
193
Thierry FOURNIERdc595002015-12-21 11:13:52 +0100194 :returns: integer
195
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100196 This attribute is an integer, it contains the value of the loglevel "notice" (5).
197
198.. js:attribute:: core.info
199
Thierry FOURNIERdc595002015-12-21 11:13:52 +0100200 :returns: integer
201
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100202 This attribute is an integer, it contains the value of the loglevel "info" (6).
203
204.. js:attribute:: core.debug
205
Thierry FOURNIERdc595002015-12-21 11:13:52 +0100206 :returns: integer
207
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100208 This attribute is an integer, it contains the value of the loglevel "debug" (7).
209
Thierry FOURNIER12a865d2016-12-14 19:40:37 +0100210.. js:attribute:: core.proxies
211
212 **context**: task, action, sample-fetch, converter
213
Patrick Hemmerc6a1d712018-05-01 21:30:41 -0400214 This attribute is a table of declared proxies (frontend and backends). Each
215 proxy give an access to his list of listeners and servers. The table is
216 indexed by proxy name, and each entry is of type :ref:`proxy_class`.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +0100217
Christopher Faulet1e9b1b62021-08-11 10:14:30 +0200218 .. Warning::
Aurelien DARRAGON53901f42022-10-13 19:49:42 +0200219 if you declared a frontend and backend with the same name, only one of
220 them will be listed.
Thierry FOURNIER9b82a582017-07-24 13:30:43 +0200221
222 :see: :js:attr:`core.backends`
223 :see: :js:attr:`core.frontends`
224
225.. js:attribute:: core.backends
226
227 **context**: task, action, sample-fetch, converter
228
Patrick Hemmerc6a1d712018-05-01 21:30:41 -0400229 This attribute is a table of declared proxies with backend capability. Each
230 proxy give an access to his list of listeners and servers. The table is
231 indexed by the backend name, and each entry is of type :ref:`proxy_class`.
Thierry FOURNIER9b82a582017-07-24 13:30:43 +0200232
233 :see: :js:attr:`core.proxies`
234 :see: :js:attr:`core.frontends`
235
236.. js:attribute:: core.frontends
237
238 **context**: task, action, sample-fetch, converter
239
Patrick Hemmerc6a1d712018-05-01 21:30:41 -0400240 This attribute is a table of declared proxies with frontend capability. Each
241 proxy give an access to his list of listeners and servers. The table is
242 indexed by the frontend name, and each entry is of type :ref:`proxy_class`.
Thierry FOURNIER9b82a582017-07-24 13:30:43 +0200243
244 :see: :js:attr:`core.proxies`
245 :see: :js:attr:`core.backends`
246
Thierry Fournierecb83c22020-11-28 15:49:44 +0100247.. js:attribute:: core.thread
248
249 **context**: task, action, sample-fetch, converter, applet
250
251 This variable contains the executing thread number starting at 1. 0 is a
252 special case for the common lua context. So, if thread is 0, Lua scope is
253 shared by all threads, otherwise the scope is dedicated to a single thread.
254 A program which needs to execute some parts exactly once regardless of the
255 number of threads can check that core.thread is 0 or 1.
256
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100257.. js:function:: core.log(loglevel, msg)
258
259 **context**: body, init, task, action, sample-fetch, converter
260
David Carlier61fdf8b2015-10-02 11:59:38 +0100261 This function sends a log. The log is sent, according with the HAProxy
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100262 configuration file, on the default syslog server if it is configured and on
263 the stderr if it is allowed.
264
Bertrand Jacquin874a35c2018-09-10 21:26:07 +0100265 :param integer loglevel: Is the log level associated with the message. It is a
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100266 number between 0 and 7.
267 :param string msg: The log content.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +0100268 :see: :js:attr:`core.emerg`, :js:attr:`core.alert`, :js:attr:`core.crit`,
269 :js:attr:`core.err`, :js:attr:`core.warning`, :js:attr:`core.notice`,
270 :js:attr:`core.info`, :js:attr:`core.debug` (log level definitions)
271 :see: :js:func:`core.Debug`
272 :see: :js:func:`core.Info`
273 :see: :js:func:`core.Warning`
274 :see: :js:func:`core.Alert`
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100275
276.. js:function:: core.Debug(msg)
277
278 **context**: body, init, task, action, sample-fetch, converter
279
280 :param string msg: The log content.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +0100281 :see: :js:func:`core.log`
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100282
283 Does the same job than:
284
285.. code-block:: lua
286
Thierry FOURNIERdc595002015-12-21 11:13:52 +0100287 function Debug(msg)
288 core.log(core.debug, msg)
289 end
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100290..
291
292.. js:function:: core.Info(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 Info(msg)
302 core.log(core.info, msg)
303 end
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100304..
305
306.. js:function:: core.Warning(msg)
307
308 **context**: body, init, task, action, sample-fetch, converter
309
310 :param string msg: The log content.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +0100311 :see: :js:func:`core.log`
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100312
313.. code-block:: lua
314
Thierry FOURNIERdc595002015-12-21 11:13:52 +0100315 function Warning(msg)
316 core.log(core.warning, msg)
317 end
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100318..
319
320.. js:function:: core.Alert(msg)
321
322 **context**: body, init, task, action, sample-fetch, converter
323
324 :param string msg: The log content.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +0100325 :see: :js:func:`core.log`
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100326
327.. code-block:: lua
328
Thierry FOURNIERdc595002015-12-21 11:13:52 +0100329 function Alert(msg)
330 core.log(core.alert, msg)
331 end
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100332..
333
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100334.. js:function:: core.add_acl(filename, key)
335
336 **context**: init, task, action, sample-fetch, converter
337
338 Add the ACL *key* in the ACLs list referenced by the file *filename*.
339
340 :param string filename: the filename that reference the ACL entries.
341 :param string key: the key which will be added.
342
343.. js:function:: core.del_acl(filename, key)
344
345 **context**: init, task, action, sample-fetch, converter
346
347 Delete the ACL entry referenced by the key *key* in the list of ACLs
348 referenced by *filename*.
349
350 :param string filename: the filename that reference the ACL entries.
351 :param string key: the key which will be deleted.
352
353.. js:function:: core.del_map(filename, key)
354
355 **context**: init, task, action, sample-fetch, converter
356
357 Delete the map entry indexed with the specified key in the list of maps
358 referenced by his filename.
359
360 :param string filename: the filename that reference the map entries.
361 :param string key: the key which will be deleted.
362
Thierry Fourniereea77c02016-03-18 08:47:13 +0100363.. js:function:: core.get_info()
364
365 **context**: body, init, task, action, sample-fetch, converter
366
Aurelien DARRAGON53901f42022-10-13 19:49:42 +0200367 Returns HAProxy core information. We can find information like the uptime,
Thierry Fourniereea77c02016-03-18 08:47:13 +0100368 the pid, memory pool usage, tasks number, ...
369
Ilya Shipitsin5fa29b82022-12-07 09:46:19 +0500370 This information is also returned by the management socket via the command
Bertrand Jacquin874a35c2018-09-10 21:26:07 +0100371 "show info". See the management socket documentation for more information
Thierry Fourniereea77c02016-03-18 08:47:13 +0100372 about the content of these variables.
373
374 :returns: an array of values.
375
Thierry Fournierb1f46562016-01-21 09:46:15 +0100376.. js:function:: core.now()
377
378 **context**: body, init, task, action
379
380 This function returns the current time. The time returned is fixed by the
Bertrand Jacquin874a35c2018-09-10 21:26:07 +0100381 HAProxy core and assures than the hour will be monotonic and that the system
Thierry Fournierb1f46562016-01-21 09:46:15 +0100382 call 'gettimeofday' will not be called too. The time is refreshed between each
383 Lua execution or resume, so two consecutive call to the function "now" will
384 probably returns the same result.
385
Patrick Hemmerc6a1d712018-05-01 21:30:41 -0400386 :returns: a table which contains two entries "sec" and "usec". "sec"
Thierry Fournierb1f46562016-01-21 09:46:15 +0100387 contains the current at the epoch format, and "usec" contains the
388 current microseconds.
389
Thierry FOURNIERa78f0372016-12-14 19:04:41 +0100390.. js:function:: core.http_date(date)
391
392 **context**: body, init, task, action
393
Bertrand Jacquin874a35c2018-09-10 21:26:07 +0100394 This function take a string representing http date, and returns an integer
Thierry FOURNIERa78f0372016-12-14 19:04:41 +0100395 containing the corresponding date with a epoch format. A valid http date
396 me respect the format IMF, RFC850 or ASCTIME.
397
398 :param string date: a date http-date formatted
399 :returns: integer containing epoch date
400 :see: :js:func:`core.imf_date`.
401 :see: :js:func:`core.rfc850_date`.
402 :see: :js:func:`core.asctime_date`.
403 :see: https://tools.ietf.org/html/rfc7231#section-7.1.1.1
404
405.. js:function:: core.imf_date(date)
406
407 **context**: body, init, task, action
408
Bertrand Jacquin874a35c2018-09-10 21:26:07 +0100409 This function take a string representing IMF date, and returns an integer
Thierry FOURNIERa78f0372016-12-14 19:04:41 +0100410 containing the corresponding date with a epoch format.
411
412 :param string date: a date IMF formatted
413 :returns: integer containing epoch date
414 :see: https://tools.ietf.org/html/rfc7231#section-7.1.1.1
415
416 The IMF format is like this:
417
418.. code-block:: text
419
420 Sun, 06 Nov 1994 08:49:37 GMT
421..
422
423.. js:function:: core.rfc850_date(date)
424
425 **context**: body, init, task, action
426
Bertrand Jacquin874a35c2018-09-10 21:26:07 +0100427 This function take a string representing RFC850 date, and returns an integer
Thierry FOURNIERa78f0372016-12-14 19:04:41 +0100428 containing the corresponding date with a epoch format.
429
430 :param string date: a date RFC859 formatted
431 :returns: integer containing epoch date
432 :see: https://tools.ietf.org/html/rfc7231#section-7.1.1.1
433
434 The RFC850 format is like this:
435
436.. code-block:: text
437
438 Sunday, 06-Nov-94 08:49:37 GMT
439..
440
441.. js:function:: core.asctime_date(date)
442
443 **context**: body, init, task, action
444
Bertrand Jacquin874a35c2018-09-10 21:26:07 +0100445 This function take a string representing ASCTIME date, and returns an integer
Thierry FOURNIERa78f0372016-12-14 19:04:41 +0100446 containing the corresponding date with a epoch format.
447
448 :param string date: a date ASCTIME formatted
449 :returns: integer containing epoch date
450 :see: https://tools.ietf.org/html/rfc7231#section-7.1.1.1
451
452 The ASCTIME format is like this:
453
454.. code-block:: text
455
456 Sun Nov 6 08:49:37 1994
457..
458
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100459.. js:function:: core.msleep(milliseconds)
460
461 **context**: body, init, task, action
462
463 The `core.msleep()` stops the Lua execution between specified milliseconds.
464
465 :param integer milliseconds: the required milliseconds.
466
Thierry FOURNIERc5d11c62018-02-12 14:46:54 +0100467.. js:function:: core.register_action(name, actions, func [, nb_args])
Thierry FOURNIER8255a752015-09-23 21:03:35 +0200468
469 **context**: body
470
David Carlier61fdf8b2015-10-02 11:59:38 +0100471 Register a Lua function executed as action. All the registered action can be
Thierry FOURNIER8255a752015-09-23 21:03:35 +0200472 used in HAProxy with the prefix "lua.". An action gets a TXN object class as
473 input.
474
475 :param string name: is the name of the converter.
Willy Tarreau61add3c2015-09-28 15:39:10 +0200476 :param table actions: is a table of string describing the HAProxy actions who
Thierry FOURNIER8255a752015-09-23 21:03:35 +0200477 want to register to. The expected actions are 'tcp-req',
478 'tcp-res', 'http-req' or 'http-res'.
Aurelien DARRAGON53901f42022-10-13 19:49:42 +0200479 :param function func: is the Lua function called to work as converter.
Thierry FOURNIERc5d11c62018-02-12 14:46:54 +0100480 :param integer nb_args: is the expected number of argument for the action.
481 By default the value is 0.
Thierry FOURNIER8255a752015-09-23 21:03:35 +0200482
483 The prototype of the Lua function used as argument is:
484
485.. code-block:: lua
486
Thierry FOURNIERc5d11c62018-02-12 14:46:54 +0100487 function(txn [, arg1 [, arg2]])
Thierry FOURNIER8255a752015-09-23 21:03:35 +0200488..
489
Thierry FOURNIERdc595002015-12-21 11:13:52 +0100490 * **txn** (:ref:`txn_class`): this is a TXN object used for manipulating the
Thierry FOURNIER8255a752015-09-23 21:03:35 +0200491 current request or TCP stream.
492
Bertrand Jacquin874a35c2018-09-10 21:26:07 +0100493 * **argX**: this is argument provided through the HAProxy configuration file.
Thierry FOURNIERc5d11c62018-02-12 14:46:54 +0100494
Bertrand Jacquin874a35c2018-09-10 21:26:07 +0100495 Here, an example of action registration. The action just send an 'Hello world'
Thierry FOURNIER8255a752015-09-23 21:03:35 +0200496 in the logs.
497
498.. code-block:: lua
499
500 core.register_action("hello-world", { "tcp-req", "http-req" }, function(txn)
501 txn:Info("Hello world")
502 end)
503..
504
Willy Tarreau714f3452021-05-09 06:47:26 +0200505 This example code is used in HAProxy configuration like this:
Thierry FOURNIER8255a752015-09-23 21:03:35 +0200506
507::
508
509 frontend tcp_frt
510 mode tcp
511 tcp-request content lua.hello-world
512
513 frontend http_frt
514 mode http
515 http-request lua.hello-world
Aurelien DARRAGONd5c80d72023-03-13 19:36:13 +0100516
Thierry FOURNIERc5d11c62018-02-12 14:46:54 +0100517..
518
Bertrand Jacquin874a35c2018-09-10 21:26:07 +0100519 A second example using arguments
Thierry FOURNIERc5d11c62018-02-12 14:46:54 +0100520
521.. code-block:: lua
522
523 function hello_world(txn, arg)
524 txn:Info("Hello world for " .. arg)
525 end
526 core.register_action("hello-world", { "tcp-req", "http-req" }, hello_world, 2)
Aurelien DARRAGONd5c80d72023-03-13 19:36:13 +0100527
Thierry FOURNIERc5d11c62018-02-12 14:46:54 +0100528..
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
Aurelien DARRAGONd5c80d72023-03-13 19:36:13 +0100537
Thierry FOURNIERc5d11c62018-02-12 14:46:54 +0100538..
Aurelien DARRAGON53901f42022-10-13 19:49:42 +0200539
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100540.. js:function:: core.register_converters(name, func)
541
542 **context**: body
543
David Carlier61fdf8b2015-10-02 11:59:38 +0100544 Register a Lua function executed as converter. All the registered converters
Aurelien DARRAGON53901f42022-10-13 19:49:42 +0200545 can be used in HAProxy with the prefix "lua.". A converter gets a string as
546 input and returns a string as output. The registered function can take up to 9
547 values as parameter. All the values are strings.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100548
549 :param string name: is the name of the converter.
550 :param function func: is the Lua function called to work as converter.
551
552 The prototype of the Lua function used as argument is:
553
554.. code-block:: lua
555
556 function(str, [p1 [, p2 [, ... [, p5]]]])
557..
558
559 * **str** (*string*): this is the input value automatically converted in
560 string.
561 * **p1** .. **p5** (*string*): this is a list of string arguments declared in
Bertrand Jacquin874a35c2018-09-10 21:26:07 +0100562 the HAProxy configuration file. The number of arguments doesn't exceed 5.
Aurelien DARRAGON53901f42022-10-13 19:49:42 +0200563 The order and the nature of these is conventionally chosen by the
Bertrand Jacquin874a35c2018-09-10 21:26:07 +0100564 developer.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100565
566.. js:function:: core.register_fetches(name, func)
567
568 **context**: body
569
David Carlier61fdf8b2015-10-02 11:59:38 +0100570 Register a Lua function executed as sample fetch. All the registered sample
Bertrand Jacquin874a35c2018-09-10 21:26:07 +0100571 fetch can be used in HAProxy with the prefix "lua.". A Lua sample fetch
Aurelien DARRAGON53901f42022-10-13 19:49:42 +0200572 returns a string as output. The registered function can take up to 9 values as
573 parameter. All the values are strings.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100574
Aurelien DARRAGON53901f42022-10-13 19:49:42 +0200575 :param string name: is the name of the sample fetch.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100576 :param function func: is the Lua function called to work as sample fetch.
577
578 The prototype of the Lua function used as argument is:
579
580.. code-block:: lua
581
582 string function(txn, [p1 [, p2 [, ... [, p5]]]])
583..
584
Thierry FOURNIERdc595002015-12-21 11:13:52 +0100585 * **txn** (:ref:`txn_class`): this is the txn object associated with the current
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100586 request.
587 * **p1** .. **p5** (*string*): this is a list of string arguments declared in
Bertrand Jacquin874a35c2018-09-10 21:26:07 +0100588 the HAProxy configuration file. The number of arguments doesn't exceed 5.
Aurelien DARRAGON53901f42022-10-13 19:49:42 +0200589 The order and the nature of these is conventionally chosen by the
Bertrand Jacquin874a35c2018-09-10 21:26:07 +0100590 developer.
591 * **Returns**: A string containing some data, or nil if the value cannot be
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100592 returned now.
593
594 lua example code:
595
596.. code-block:: lua
597
598 core.register_fetches("hello", function(txn)
599 return "hello"
600 end)
601..
602
603 HAProxy example configuration:
604
605::
606
607 frontend example
608 http-request redirect location /%[lua.hello]
609
Christopher Faulet5a2c6612021-08-15 20:35:25 +0200610.. js:function:: core.register_filter(name, Flt, func)
611
612 **context**: body
613
614 Register a Lua function used to declare a filter. All the registered filters
615 can by used in HAProxy with the prefix "lua.".
616
617 :param string name: is the name of the filter.
618 :param table Flt: is a Lua class containing the filter definition (id, flags,
619 callbacks).
620 :param function func: is the Lua function called to create the Lua filter.
621
622 The prototype of the Lua function used as argument is:
623
624.. code-block:: lua
625
626 function(flt, args)
627..
628
629 * **flt** : Is a filter object based on the class provided in
630 :js:func:`core.register_filter()` function.
631
632 * **args**: Is a table of strings containing all arguments provided through
633 the HAProxy configuration file, on the filter line.
634
635 It must return the filter to use or nil to ignore it. Here, an example of
636 filter registration.
637
638.. code-block:: lua
639
640 core.register_filter("my-filter", MyFilter, function(flt, args)
641 flt.args = args -- Save arguments
642 return flt
643 end)
644..
645
646 This example code is used in HAProxy configuration like this:
647
648::
649
650 frontend http
651 mode http
652 filter lua.my-filter arg1 arg2 arg3
Aurelien DARRAGONd5c80d72023-03-13 19:36:13 +0100653
Christopher Faulet5a2c6612021-08-15 20:35:25 +0200654..
655
656 :see: :js:class:`Filter`
657
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +0200658.. js:function:: core.register_service(name, mode, func)
659
660 **context**: body
661
Aurelien DARRAGON53901f42022-10-13 19:49:42 +0200662 Register a Lua function executed as a service. All the registered services can
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +0200663 be used in HAProxy with the prefix "lua.". A service gets an object class as
664 input according with the required mode.
665
Aurelien DARRAGON53901f42022-10-13 19:49:42 +0200666 :param string name: is the name of the service.
Willy Tarreau61add3c2015-09-28 15:39:10 +0200667 :param string mode: is string describing the required mode. Only 'tcp' or
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +0200668 'http' are allowed.
Aurelien DARRAGON53901f42022-10-13 19:49:42 +0200669 :param function func: is the Lua function called to work as service.
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +0200670
671 The prototype of the Lua function used as argument is:
672
673.. code-block:: lua
674
675 function(applet)
676..
677
Thierry FOURNIERdc595002015-12-21 11:13:52 +0100678 * **applet** *applet* will be a :ref:`applettcp_class` or a
679 :ref:`applethttp_class`. It depends the type of registered applet. An applet
680 registered with the 'http' value for the *mode* parameter will gets a
681 :ref:`applethttp_class`. If the *mode* value is 'tcp', the applet will gets
682 a :ref:`applettcp_class`.
683
Christopher Faulet1e9b1b62021-08-11 10:14:30 +0200684 .. warning::
685 Applets of type 'http' cannot be called from 'tcp-*' rulesets. Only the
686 'http-*' rulesets are authorized, this means that is not possible to call
Aurelien DARRAGON53901f42022-10-13 19:49:42 +0200687 a HTTP applet from a proxy in tcp mode. Applets of type 'tcp' can be
Christopher Faulet1e9b1b62021-08-11 10:14:30 +0200688 called from anywhere.
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +0200689
Bertrand Jacquin874a35c2018-09-10 21:26:07 +0100690 Here, an example of service registration. The service just send an 'Hello world'
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +0200691 as an http response.
692
693.. code-block:: lua
694
Pieter Baauw4d7f7662015-11-08 16:38:08 +0100695 core.register_service("hello-world", "http", function(applet)
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +0200696 local response = "Hello World !"
697 applet:set_status(200)
Pieter Baauw2dcb9bc2015-10-01 22:47:12 +0200698 applet:add_header("content-length", string.len(response))
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +0200699 applet:add_header("content-type", "text/plain")
Pieter Baauw2dcb9bc2015-10-01 22:47:12 +0200700 applet:start_response()
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +0200701 applet:send(response)
702 end)
703..
704
Willy Tarreau714f3452021-05-09 06:47:26 +0200705 This example code is used in HAProxy configuration like this:
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +0200706
707::
708
709 frontend example
710 http-request use-service lua.hello-world
711
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100712.. js:function:: core.register_init(func)
713
714 **context**: body
715
716 Register a function executed after the configuration parsing. This is useful
717 to check any parameters.
718
Pieter Baauw4d7f7662015-11-08 16:38:08 +0100719 :param function func: is the Lua function called to work as initializer.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100720
721 The prototype of the Lua function used as argument is:
722
723.. code-block:: lua
724
725 function()
726..
727
728 It takes no input, and no output is expected.
729
Aurelien DARRAGONb8038992023-03-09 16:48:30 +0100730.. js:function:: core.register_task(func[, arg1[, arg2[, ...[, arg4]]]])
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100731
Aurelien DARRAGONc84899c2023-02-20 18:18:59 +0100732 **context**: body, init, task, action, sample-fetch, converter, event
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100733
734 Register and start independent task. The task is started when the HAProxy
735 main scheduler starts. For example this type of tasks can be executed to
Thierry FOURNIER2e4893c2015-03-18 13:37:27 +0100736 perform complex health checks.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100737
Aurelien DARRAGONb8038992023-03-09 16:48:30 +0100738 :param function func: is the Lua function called to work as an async task.
739
740 Up to 4 optional arguments (all types supported) may be passed to the function.
741 (They will be passed as-is to the task function)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100742
743 The prototype of the Lua function used as argument is:
744
745.. code-block:: lua
746
Aurelien DARRAGONb8038992023-03-09 16:48:30 +0100747 function([arg1[, arg2[, ...[, arg4]]]])
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100748..
749
Aurelien DARRAGONb8038992023-03-09 16:48:30 +0100750 It takes up to 4 optional arguments (provided when registering), and no output is expected.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100751
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +0100752.. js:function:: core.register_cli([path], usage, func)
753
754 **context**: body
755
Aurelien DARRAGON53901f42022-10-13 19:49:42 +0200756 Register a custom cli that will be available from haproxy stats socket.
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +0100757
758 :param array path: is the sequence of word for which the cli execute the Lua
759 binding.
760 :param string usage: is the usage message displayed in the help.
761 :param function func: is the Lua function called to handle the CLI commands.
762
763 The prototype of the Lua function used as argument is:
764
765.. code-block:: lua
766
767 function(AppletTCP, [arg1, [arg2, [...]]])
768..
769
770 I/O are managed with the :ref:`applettcp_class` object. Args are given as
Bertrand Jacquin874a35c2018-09-10 21:26:07 +0100771 parameter. The args embed the registered path. If the path is declared like
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +0100772 this:
773
774.. code-block:: lua
775
776 core.register_cli({"show", "ssl", "stats"}, "Display SSL stats..", function(applet, arg1, arg2, arg3, arg4, arg5)
777 end)
778..
779
780 And we execute this in the prompt:
781
782.. code-block:: text
783
784 > prompt
785 > show ssl stats all
786..
787
Bertrand Jacquin874a35c2018-09-10 21:26:07 +0100788 Then, arg1, arg2 and arg3 will contains respectively "show", "ssl" and "stats".
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +0100789 arg4 will contain "all". arg5 contains nil.
790
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100791.. js:function:: core.set_nice(nice)
792
793 **context**: task, action, sample-fetch, converter
794
795 Change the nice of the current task or current session.
Thierry FOURNIER2e4893c2015-03-18 13:37:27 +0100796
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100797 :param integer nice: the nice value, it must be between -1024 and 1024.
798
799.. js:function:: core.set_map(filename, key, value)
800
801 **context**: init, task, action, sample-fetch, converter
802
Bertrand Jacquin874a35c2018-09-10 21:26:07 +0100803 Set the value *value* associated to the key *key* in the map referenced by
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100804 *filename*.
805
Thierry FOURNIERdc595002015-12-21 11:13:52 +0100806 :param string filename: the Map reference
807 :param string key: the key to set or replace
808 :param string value: the associated value
809
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100810.. js:function:: core.sleep(int seconds)
811
812 **context**: body, init, task, action
813
814 The `core.sleep()` functions stop the Lua execution between specified seconds.
815
816 :param integer seconds: the required seconds.
817
818.. js:function:: core.tcp()
819
820 **context**: init, task, action
821
822 This function returns a new object of a *socket* class.
823
Thierry FOURNIERdc595002015-12-21 11:13:52 +0100824 :returns: A :ref:`socket_class` object.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100825
William Lallemand00a15022021-11-19 16:02:44 +0100826.. js:function:: core.httpclient()
827
828 **context**: init, task, action
829
830 This function returns a new object of a *httpclient* class.
831
832 :returns: A :ref:`httpclient_class` object.
833
Thierry Fournier1de16592016-01-27 09:49:07 +0100834.. js:function:: core.concat()
835
836 **context**: body, init, task, action, sample-fetch, converter
837
Bertrand Jacquin874a35c2018-09-10 21:26:07 +0100838 This function returns a new concat object.
Thierry Fournier1de16592016-01-27 09:49:07 +0100839
840 :returns: A :ref:`concat_class` object.
841
Thierry FOURNIER0a99b892015-08-26 00:14:17 +0200842.. js:function:: core.done(data)
843
844 **context**: body, init, task, action, sample-fetch, converter
845
846 :param any data: Return some data for the caller. It is useful with
847 sample-fetches and sample-converters.
848
849 Immediately stops the current Lua execution and returns to the caller which
850 may be a sample fetch, a converter or an action and returns the specified
Thierry Fournier4234dbd2020-11-28 13:18:23 +0100851 value (ignored for actions and init). It is used when the LUA process finishes
852 its work and wants to give back the control to HAProxy without executing the
Thierry FOURNIER0a99b892015-08-26 00:14:17 +0200853 remaining code. It can be seen as a multi-level "return".
854
Thierry FOURNIER486f5a02015-03-16 15:13:03 +0100855.. js:function:: core.yield()
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100856
857 **context**: task, action, sample-fetch, converter
858
859 Give back the hand at the HAProxy scheduler. It is used when the LUA
860 processing consumes a lot of processing time.
861
Thierry FOURNIER / OZON.IO62fec752016-11-10 20:38:11 +0100862.. js:function:: core.parse_addr(address)
863
864 **context**: body, init, task, action, sample-fetch, converter
865
866 :param network: is a string describing an ipv4 or ipv6 address and optionally
867 its network length, like this: "127.0.0.1/8" or "aaaa::1234/32".
868 :returns: a userdata containing network or nil if an error occurs.
869
Bertrand Jacquin874a35c2018-09-10 21:26:07 +0100870 Parse ipv4 or ipv6 addresses and its facultative associated network.
Thierry FOURNIER / OZON.IO62fec752016-11-10 20:38:11 +0100871
872.. js:function:: core.match_addr(addr1, addr2)
873
874 **context**: body, init, task, action, sample-fetch, converter
875
876 :param addr1: is an address created with "core.parse_addr".
877 :param addr2: is an address created with "core.parse_addr".
Bertrand Jacquin874a35c2018-09-10 21:26:07 +0100878 :returns: boolean, true if the network of the addresses match, else returns
Thierry FOURNIER / OZON.IO62fec752016-11-10 20:38:11 +0100879 false.
880
Ilya Shipitsin2075ca82020-03-06 23:22:22 +0500881 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 +0100882 of network is not important.
883
Thierry FOURNIER / OZON.IO8a1027a2016-11-24 20:48:38 +0100884.. js:function:: core.tokenize(str, separators [, noblank])
885
886 **context**: body, init, task, action, sample-fetch, converter
887
888 This function is useful for tokenizing an entry, or splitting some messages.
889 :param string str: The string which will be split.
890 :param string separators: A string containing a list of separators.
891 :param boolean noblank: Ignore empty entries.
892 :returns: an array of string.
893
894 For example:
895
896.. code-block:: lua
897
898 local array = core.tokenize("This function is useful, for tokenizing an entry.", "., ", true)
899 print_r(array)
900..
901
902 Returns this array:
903
904.. code-block:: text
905
906 (table) table: 0x21c01e0 [
907 1: (string) "This"
908 2: (string) "function"
909 3: (string) "is"
910 4: (string) "useful"
911 5: (string) "for"
912 6: (string) "tokenizing"
913 7: (string) "an"
914 8: (string) "entry"
915 ]
916..
917
Aurelien DARRAGONc84899c2023-02-20 18:18:59 +0100918.. js:function:: core.event_sub(event_types, func)
919
920 **context**: body, init, task, action, sample-fetch, converter
921
922 Register a function that will be called on specific system events.
923
924 :param array event_types: array of string containing the event types you want to subscribe to
925 :param function func: is the Lua function called when one of the subscribed events occur.
926 :returns: A :ref:`event_sub_class` object.
Aurelien DARRAGON223770d2023-03-10 15:34:35 +0100927 :see: :js:func:`Server.event_sub()`.
Aurelien DARRAGONc84899c2023-02-20 18:18:59 +0100928
929 List of available event types :
930
931 **SERVER** Family:
932
933 * **SERVER_ADD**: when a server is added
934 * **SERVER_DEL**: when a server is removed
935 * **SERVER_DOWN**: when a server state goes from UP to DOWN
936 * **SERVER_UP**: when a server state goes from DOWN to UP
937
938 .. Note::
939 You may also use **SERVER** in **event_types** to subscribe to all server events types at once.
940
941 The prototype of the Lua function used as argument is:
942
943.. code-block:: lua
944
Aurelien DARRAGON096b3832023-04-20 11:32:46 +0200945 function(event, event_data, sub, when)
Aurelien DARRAGONc84899c2023-02-20 18:18:59 +0100946..
947
948 * **event** (*string*): the event type (one of the **event_types** you specified when subscribing)
949 * **event_data**: specific to each event family (For **SERVER** family, a :ref:`server_event_class` object)
950 * **sub**: class to manage the subscription from within the event (a :ref:`event_sub_class` object)
Aurelien DARRAGON096b3832023-04-20 11:32:46 +0200951 * **when**: timestamp corresponding to the date when the event was generated.
952 It is an integer representing the number of seconds elapsed since Epoch.
953 It may be provided as optional argument to `os.date()` lua function to
954 convert it to a string according to a given format string.
Aurelien DARRAGONc84899c2023-02-20 18:18:59 +0100955
956 .. Warning::
957 The callback function will only be scheduled on the very same thread that
958 performed the subscription.
959
960 Moreover, each thread treats events sequentially. It means that if you have,
961 let's say SERVER_UP followed by a SERVER_DOWN in a short timelapse, then
962 the cb function will first be called with SERVER_UP, and once it's done
963 handling the event, the cb function will be called again with SERVER_DOWN.
964
965 This is to ensure event consistency when it comes to logging / triggering logic
966 from lua.
967
968 Your lua cb function may yield if needed, but you're pleased to process the
969 event as fast as possible to prevent the event queue from growing up, depending
970 on the event flow that is expected for the given subscription.
971
972 To prevent abuses, if the event queue for the current subscription goes over
973 a certain amount of unconsumed events, the subscription will pause itself
974 automatically for as long as it takes for your handler to catch up. This would
975 lead to events being missed, so an error will be reported in the logs to warn
976 you about that.
977 This is not something you want to let happen too often, it may indicate that
978 you subscribed to an event that is occurring too frequently or/and that your
979 callback function is too slow to keep up the pace and you should review it.
980
981 If you want to do some parallel processing because your callback functions are
982 slow: you might want to create subtasks from lua using
983 :js:func:`core.register_task()` from within your callback function to perform
984 the heavy job in a dedicated task and allow remaining events to be processed
985 more quickly.
986
Thierry Fournierf61aa632016-02-19 20:56:00 +0100987.. _proxy_class:
988
989Proxy class
990============
991
992.. js:class:: Proxy
993
994 This class provides a way for manipulating proxy and retrieving information
995 like statistics.
996
Thierry FOURNIER817e7592017-07-24 14:35:04 +0200997.. js:attribute:: Proxy.name
998
999 Contain the name of the proxy.
1000
Aurelien DARRAGONc4b24372023-03-02 12:00:06 +01001001 .. warning::
1002 This attribute is now deprecated and will eventually be removed.
1003 Please use :js:func:`Proxy.get_name()` function instead.
1004
Thierry Fournierb0467732022-10-07 12:07:24 +02001005.. js:function:: Proxy.get_name()
1006
1007 Returns the name of the proxy.
1008
Baptiste Assmann46c72552017-10-26 21:51:58 +02001009.. js:attribute:: Proxy.uuid
1010
1011 Contain the unique identifier of the proxy.
1012
Aurelien DARRAGONc4b24372023-03-02 12:00:06 +01001013 .. warning::
1014 This attribute is now deprecated and will eventually be removed.
1015 Please use :js:func:`Proxy.get_uuid()` function instead.
1016
Thierry Fournierb0467732022-10-07 12:07:24 +02001017.. js:function:: Proxy.get_uuid()
1018
1019 Returns the unique identifier of the proxy.
1020
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001021.. js:attribute:: Proxy.servers
1022
Patrick Hemmerc6a1d712018-05-01 21:30:41 -04001023 Contain a table with the attached servers. The table is indexed by server
1024 name, and each server entry is an object of type :ref:`server_class`.
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001025
Adis Nezirovic8878f8e2018-07-13 12:18:33 +02001026.. js:attribute:: Proxy.stktable
1027
1028 Contains a stick table object attached to the proxy.
1029
Thierry Fournierff480422016-02-25 08:36:46 +01001030.. js:attribute:: Proxy.listeners
1031
Patrick Hemmerc6a1d712018-05-01 21:30:41 -04001032 Contain a table with the attached listeners. The table is indexed by listener
1033 name, and each each listeners entry is an object of type
1034 :ref:`listener_class`.
Thierry Fournierff480422016-02-25 08:36:46 +01001035
Thierry Fournierf61aa632016-02-19 20:56:00 +01001036.. js:function:: Proxy.pause(px)
1037
1038 Pause the proxy. See the management socket documentation for more information.
1039
1040 :param class_proxy px: A :ref:`proxy_class` which indicates the manipulated
1041 proxy.
1042
1043.. js:function:: Proxy.resume(px)
1044
1045 Resume the proxy. See the management socket documentation for more
1046 information.
1047
1048 :param class_proxy px: A :ref:`proxy_class` which indicates the manipulated
1049 proxy.
1050
1051.. js:function:: Proxy.stop(px)
1052
1053 Stop the proxy. See the management socket documentation for more information.
1054
1055 :param class_proxy px: A :ref:`proxy_class` which indicates the manipulated
1056 proxy.
1057
1058.. js:function:: Proxy.shut_bcksess(px)
1059
1060 Kill the session attached to a backup server. See the management socket
1061 documentation for more information.
1062
1063 :param class_proxy px: A :ref:`proxy_class` which indicates the manipulated
1064 proxy.
1065
1066.. js:function:: Proxy.get_cap(px)
1067
1068 Returns a string describing the capabilities of the proxy.
1069
1070 :param class_proxy px: A :ref:`proxy_class` which indicates the manipulated
1071 proxy.
1072 :returns: a string "frontend", "backend", "proxy" or "ruleset".
1073
1074.. js:function:: Proxy.get_mode(px)
1075
1076 Returns a string describing the mode of the current proxy.
1077
1078 :param class_proxy px: A :ref:`proxy_class` which indicates the manipulated
1079 proxy.
1080 :returns: a string "tcp", "http", "health" or "unknown"
1081
1082.. js:function:: Proxy.get_stats(px)
1083
Bertrand Jacquin874a35c2018-09-10 21:26:07 +01001084 Returns a table containing the proxy statistics. The statistics returned are
Thierry Fournierf61aa632016-02-19 20:56:00 +01001085 not the same if the proxy is frontend or a backend.
1086
1087 :param class_proxy px: A :ref:`proxy_class` which indicates the manipulated
1088 proxy.
Patrick Hemmerc6a1d712018-05-01 21:30:41 -04001089 :returns: a key/value table containing stats
Thierry Fournierf61aa632016-02-19 20:56:00 +01001090
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001091.. _server_class:
1092
1093Server class
1094============
1095
Patrick Hemmerc6a1d712018-05-01 21:30:41 -04001096.. js:class:: Server
1097
1098 This class provides a way for manipulating servers and retrieving information.
1099
Patrick Hemmera62ae7e2018-04-29 14:23:48 -04001100.. js:attribute:: Server.name
1101
1102 Contain the name of the server.
1103
Aurelien DARRAGONc4b24372023-03-02 12:00:06 +01001104 .. warning::
1105 This attribute is now deprecated and will eventually be removed.
1106 Please use :js:func:`Server.get_name()` function instead.
1107
Thierry Fournierb0467732022-10-07 12:07:24 +02001108.. js:function:: Server.get_name(sv)
1109
1110 Returns the name of the server.
1111
Patrick Hemmera62ae7e2018-04-29 14:23:48 -04001112.. js:attribute:: Server.puid
1113
1114 Contain the proxy unique identifier of the server.
1115
Aurelien DARRAGONc4b24372023-03-02 12:00:06 +01001116 .. warning::
1117 This attribute is now deprecated and will eventually be removed.
1118 Please use :js:func:`Server.get_puid()` function instead.
1119
Thierry Fournierb0467732022-10-07 12:07:24 +02001120.. js:function:: Server.get_puid(sv)
1121
1122 Returns the proxy unique identifier of the server.
1123
Aurelien DARRAGON94ee6632023-03-10 15:11:27 +01001124.. js:function:: Server.get_rid(sv)
1125
1126 Returns the rid (revision ID) of the server.
1127 It is an unsigned integer that is set upon server creation. Value is derived
1128 from a global counter that starts at 0 and is incremented each time one or
1129 multiple server deletions are followed by a server addition (meaning that
1130 old name/id reuse could occur).
1131
1132 Combining server name/id with server rid yields a process-wide unique
1133 identifier.
1134
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001135.. js:function:: Server.is_draining(sv)
1136
Patrick Hemmerc6a1d712018-05-01 21:30:41 -04001137 Return true if the server is currently draining sticky connections.
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001138
1139 :param class_server sv: A :ref:`server_class` which indicates the manipulated
1140 server.
1141 :returns: a boolean
1142
Patrick Hemmer32d539f2018-04-29 14:25:46 -04001143.. js:function:: Server.set_maxconn(sv, weight)
1144
1145 Dynamically change the maximum connections of the server. See the management
1146 socket documentation for more information about the format of the string.
1147
1148 :param class_server sv: A :ref:`server_class` which indicates the manipulated
1149 server.
1150 :param string maxconn: A string describing the server maximum connections.
1151
1152.. js:function:: Server.get_maxconn(sv, weight)
1153
1154 This function returns an integer representing the server maximum connections.
1155
1156 :param class_server sv: A :ref:`server_class` which indicates the manipulated
1157 server.
1158 :returns: an integer.
1159
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001160.. js:function:: Server.set_weight(sv, weight)
1161
Patrick Hemmerc6a1d712018-05-01 21:30:41 -04001162 Dynamically change the weight of the server. See the management socket
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001163 documentation for more information about the format of the string.
1164
1165 :param class_server sv: A :ref:`server_class` which indicates the manipulated
1166 server.
1167 :param string weight: A string describing the server weight.
1168
1169.. js:function:: Server.get_weight(sv)
1170
Patrick Hemmerc6a1d712018-05-01 21:30:41 -04001171 This function returns an integer representing the server weight.
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001172
1173 :param class_server sv: A :ref:`server_class` which indicates the manipulated
1174 server.
1175 :returns: an integer.
1176
Joseph C. Sible49bbf522020-05-04 22:20:32 -04001177.. js:function:: Server.set_addr(sv, addr[, port])
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001178
Patrick Hemmerc6a1d712018-05-01 21:30:41 -04001179 Dynamically change the address of the server. See the management socket
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001180 documentation for more information about the format of the string.
1181
1182 :param class_server sv: A :ref:`server_class` which indicates the manipulated
1183 server.
Patrick Hemmerc6a1d712018-05-01 21:30:41 -04001184 :param string addr: A string describing the server address.
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001185
1186.. js:function:: Server.get_addr(sv)
1187
Patrick Hemmerc6a1d712018-05-01 21:30:41 -04001188 Returns a string describing the address of the server.
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001189
1190 :param class_server sv: A :ref:`server_class` which indicates the manipulated
1191 server.
1192 :returns: A string
1193
1194.. js:function:: Server.get_stats(sv)
1195
1196 Returns server statistics.
1197
1198 :param class_server sv: A :ref:`server_class` which indicates the manipulated
1199 server.
Patrick Hemmerc6a1d712018-05-01 21:30:41 -04001200 :returns: a key/value table containing stats
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001201
1202.. js:function:: Server.shut_sess(sv)
1203
1204 Shutdown all the sessions attached to the server. See the management socket
1205 documentation for more information about this function.
1206
1207 :param class_server sv: A :ref:`server_class` which indicates the manipulated
1208 server.
1209
1210.. js:function:: Server.set_drain(sv)
1211
1212 Drain sticky sessions. See the management socket documentation for more
1213 information about this function.
1214
1215 :param class_server sv: A :ref:`server_class` which indicates the manipulated
1216 server.
1217
1218.. js:function:: Server.set_maint(sv)
1219
1220 Set maintenance mode. See the management socket documentation for more
1221 information about this function.
1222
1223 :param class_server sv: A :ref:`server_class` which indicates the manipulated
1224 server.
1225
1226.. js:function:: Server.set_ready(sv)
1227
1228 Set normal mode. See the management socket documentation for more information
1229 about this function.
1230
1231 :param class_server sv: A :ref:`server_class` which indicates the manipulated
1232 server.
1233
1234.. js:function:: Server.check_enable(sv)
1235
1236 Enable health checks. See the management socket documentation for more
1237 information about this function.
1238
1239 :param class_server sv: A :ref:`server_class` which indicates the manipulated
1240 server.
1241
1242.. js:function:: Server.check_disable(sv)
1243
1244 Disable health checks. See the management socket documentation for more
1245 information about this function.
1246
1247 :param class_server sv: A :ref:`server_class` which indicates the manipulated
1248 server.
1249
1250.. js:function:: Server.check_force_up(sv)
1251
1252 Force health-check up. See the management socket documentation for more
1253 information about this function.
1254
1255 :param class_server sv: A :ref:`server_class` which indicates the manipulated
1256 server.
1257
1258.. js:function:: Server.check_force_nolb(sv)
1259
1260 Force health-check nolb mode. See the management socket documentation for more
1261 information about this function.
1262
1263 :param class_server sv: A :ref:`server_class` which indicates the manipulated
1264 server.
1265
1266.. js:function:: Server.check_force_down(sv)
1267
1268 Force health-check down. See the management socket documentation for more
1269 information about this function.
1270
1271 :param class_server sv: A :ref:`server_class` which indicates the manipulated
1272 server.
1273
1274.. js:function:: Server.agent_enable(sv)
1275
1276 Enable agent check. See the management socket documentation for more
1277 information about this function.
1278
1279 :param class_server sv: A :ref:`server_class` which indicates the manipulated
1280 server.
1281
1282.. js:function:: Server.agent_disable(sv)
1283
1284 Disable agent check. See the management socket documentation for more
1285 information about this function.
1286
1287 :param class_server sv: A :ref:`server_class` which indicates the manipulated
1288 server.
1289
1290.. js:function:: Server.agent_force_up(sv)
1291
1292 Force agent check up. See the management socket documentation for more
1293 information about this function.
1294
1295 :param class_server sv: A :ref:`server_class` which indicates the manipulated
1296 server.
1297
1298.. js:function:: Server.agent_force_down(sv)
1299
1300 Force agent check down. See the management socket documentation for more
1301 information about this function.
1302
1303 :param class_server sv: A :ref:`server_class` which indicates the manipulated
1304 server.
1305
Aurelien DARRAGON223770d2023-03-10 15:34:35 +01001306.. js:function:: Server.event_sub(sv, event_types, func)
1307
1308 Register a function that will be called on specific server events.
1309 It works exactly like :js:func:`core.event_sub()` except that the subscription
1310 will be performed within the server dedicated subscription list instead of the
1311 global one.
1312 (Your callback function will only be called for server events affecting sv)
1313
1314 See :js:func:`core.event_sub()` for function usage.
1315
1316 A key advantage to using :js:func:`Server.event_sub()` over
1317 :js:func:`core.event_sub()` for servers is that :js:func:`Server.event_sub()`
1318 allows you to be notified for servers events of a single server only.
1319 It removes the needs for extra filtering in your callback function if you only
1320 care about a single server, and also prevents useless wakeups.
1321
1322 For instance, if you want to be notified for UP/DOWN events on a given set of
1323 servers, it is recommended to peform multiple per-server subscriptions since
1324 it will be more efficient that doing a single global subscription that will
1325 filter the received events.
1326 Unless you really want to be notified for servers events of ALL servers of
1327 course, which could make sense given you setup but should be avoided if you
1328 have an important number of servers as it will add a significant load on your
1329 haproxy process in case of multiple servers state change in a short amount of
1330 time.
1331
1332 .. Note::
1333 You may also combine :js:func:`core.event_sub()` with
1334 :js:func:`Server.event_sub()`.
1335
1336 Also, don't forget that you can use :js:func:`core.register_task()` from
1337 your callback function if needed. (ie: parallel work)
1338
1339 Here is a working example combining :js:func:`core.event_sub()` with
1340 :js:func:`Server.event_sub()` and :js:func:`core.register_task()`
1341 (This only serves as a demo, this is not necessarily useful to do so)
1342
1343.. code-block:: lua
1344
1345 core.event_sub({"SERVER_ADD"}, function(event, data, sub)
1346 -- in the global event handler
1347 if data["reference"] ~= nil then
1348 print("Tracking new server: ", data["name"])
1349 data["reference"]:event_sub({"SERVER_UP", "SERVER_DOWN"}, function(event, data, sub)
1350 -- in the per-server event handler
1351 if data["reference"] ~= nil then
1352 core.register_task(function(server)
1353 -- subtask to perform some async work (e.g.: HTTP API calls, sending emails...)
1354 print("ASYNC: SERVER ", server:get_name(), " is ", event == "SERVER_UP" and "UP" or "DOWN")
1355 end, data["reference"])
1356 end
1357 end)
1358 end
1359 end)
1360
1361..
1362
1363 In this example, we will first track global server addition events.
1364 For each newly added server ("add server" on the cli), we will register a
1365 UP/DOWN server subscription.
1366 Then, the callback function will schedule the event handling in an async
1367 subtask which will receive the server reference as an argument.
1368
Thierry Fournierff480422016-02-25 08:36:46 +01001369.. _listener_class:
1370
1371Listener class
1372==============
1373
1374.. js:function:: Listener.get_stats(ls)
1375
1376 Returns server statistics.
1377
1378 :param class_listener ls: A :ref:`listener_class` which indicates the
1379 manipulated listener.
Patrick Hemmerc6a1d712018-05-01 21:30:41 -04001380 :returns: a key/value table containing stats
Thierry Fournierff480422016-02-25 08:36:46 +01001381
Aurelien DARRAGONc84899c2023-02-20 18:18:59 +01001382.. _event_sub_class:
1383
1384EventSub class
1385==============
1386
1387.. js:function:: EventSub.unsub()
1388
1389 End the subscription, the callback function will not be called again.
1390
1391.. _server_event_class:
1392
1393ServerEvent class
1394=================
1395
1396.. js:attribute:: ServerEvent.name
1397
1398 Contains the name of the server.
1399
1400.. js:attribute:: ServerEvent.puid
1401
1402 Contains the proxy-unique uid of the server
1403
1404.. js:attribute:: ServerEvent.rid
1405
1406 Contains the revision ID of the server
1407
1408.. js:attribute:: ServerEvent.proxy_name
1409
1410 Contains the name of the proxy to which the server belongs
1411
Aurelien DARRAGON55f84c72023-03-22 17:49:04 +01001412.. js:attribute:: ServerEvent.proxy_uuid
1413
1414 Contains the uuid of the proxy to which the server belongs
1415
Aurelien DARRAGONc84899c2023-02-20 18:18:59 +01001416.. js:attribute:: ServerEvent.reference
1417
1418 Reference to the live server (A :ref:`server_class`).
1419
1420 .. Warning::
1421 Not available if the server was removed in the meantime.
1422 (Will never be set for SERVER_DEL event since the server does not exist anymore)
1423
Thierry Fournier1de16592016-01-27 09:49:07 +01001424.. _concat_class:
1425
1426Concat class
1427============
1428
1429.. js:class:: Concat
1430
1431 This class provides a fast way for string concatenation. The way using native
1432 Lua concatenation like the code below is slow for some reasons.
1433
1434.. code-block:: lua
1435
1436 str = "string1"
1437 str = str .. ", string2"
1438 str = str .. ", string3"
1439..
1440
1441 For each concatenation, Lua:
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02001442 - allocates memory for the result,
1443 - catenates the two string copying the strings in the new memory block,
1444 - frees the old memory block containing the string which is no longer used.
1445
Thierry Fournier1de16592016-01-27 09:49:07 +01001446 This process does many memory move, allocation and free. In addition, the
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02001447 memory is not really freed, it is just marked as unused and waits for the
Thierry Fournier1de16592016-01-27 09:49:07 +01001448 garbage collector.
1449
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02001450 The Concat class provides an alternative way to concatenate strings. It uses
Thierry Fournier1de16592016-01-27 09:49:07 +01001451 the internal Lua mechanism (it does not allocate memory), but it doesn't copy
1452 the data more than once.
1453
1454 On my computer, the following loops spends 0.2s for the Concat method and
1455 18.5s for the pure Lua implementation. So, the Concat class is about 1000x
1456 faster than the embedded solution.
1457
1458.. code-block:: lua
1459
1460 for j = 1, 100 do
1461 c = core.concat()
1462 for i = 1, 20000 do
1463 c:add("#####")
1464 end
1465 end
1466..
1467
1468.. code-block:: lua
1469
1470 for j = 1, 100 do
1471 c = ""
1472 for i = 1, 20000 do
1473 c = c .. "#####"
1474 end
1475 end
1476..
1477
1478.. js:function:: Concat.add(concat, string)
1479
1480 This function adds a string to the current concatenated string.
1481
1482 :param class_concat concat: A :ref:`concat_class` which contains the currently
Ilya Shipitsin2075ca82020-03-06 23:22:22 +05001483 built string.
Bertrand Jacquin874a35c2018-09-10 21:26:07 +01001484 :param string string: A new string to concatenate to the current built
Thierry Fournier1de16592016-01-27 09:49:07 +01001485 string.
1486
1487.. js:function:: Concat.dump(concat)
1488
Bertrand Jacquin874a35c2018-09-10 21:26:07 +01001489 This function returns the concatenated string.
Thierry Fournier1de16592016-01-27 09:49:07 +01001490
1491 :param class_concat concat: A :ref:`concat_class` which contains the currently
Ilya Shipitsin2075ca82020-03-06 23:22:22 +05001492 built string.
Thierry Fournier1de16592016-01-27 09:49:07 +01001493 :returns: the concatenated string
1494
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001495.. _fetches_class:
1496
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001497Fetches class
1498=============
1499
1500.. js:class:: Fetches
1501
1502 This class contains a lot of internal HAProxy sample fetches. See the
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02001503 HAProxy "configuration.txt" documentation for more information.
1504 (chapters 7.3.2 to 7.3.6)
Thierry FOURNIER2e4893c2015-03-18 13:37:27 +01001505
Christopher Faulet1e9b1b62021-08-11 10:14:30 +02001506 .. warning::
1507 some sample fetches are not available in some context. These limitations
1508 are specified in this documentation when they're useful.
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001509
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01001510 :see: :js:attr:`TXN.f`
1511 :see: :js:attr:`TXN.sf`
Thierry FOURNIER2e4893c2015-03-18 13:37:27 +01001512
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02001513 Fetches are useful to:
Thierry FOURNIER2e4893c2015-03-18 13:37:27 +01001514
1515 * get system time,
1516 * get environment variable,
1517 * get random numbers,
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02001518 * know backend status like the number of users in queue or the number of
Thierry FOURNIER2e4893c2015-03-18 13:37:27 +01001519 connections established,
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02001520 * get client information like ip source or destination,
Thierry FOURNIER2e4893c2015-03-18 13:37:27 +01001521 * deal with stick tables,
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02001522 * fetch established SSL information,
1523 * fetch HTTP information like headers or method.
Thierry FOURNIER2e4893c2015-03-18 13:37:27 +01001524
1525.. code-block:: lua
1526
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001527 function action(txn)
1528 -- Get source IP
1529 local clientip = txn.f:src()
1530 end
Thierry FOURNIER2e4893c2015-03-18 13:37:27 +01001531..
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001532
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001533.. _converters_class:
1534
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001535Converters class
1536================
1537
1538.. js:class:: Converters
1539
1540 This class contains a lot of internal HAProxy sample converters. See the
Thierry FOURNIER2e4893c2015-03-18 13:37:27 +01001541 HAProxy documentation "configuration.txt" for more information about her
1542 usage. Its the chapter 7.3.1.
1543
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01001544 :see: :js:attr:`TXN.c`
1545 :see: :js:attr:`TXN.sc`
Thierry FOURNIER2e4893c2015-03-18 13:37:27 +01001546
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02001547 Converters provides stateful transformation. They are useful to:
Thierry FOURNIER2e4893c2015-03-18 13:37:27 +01001548
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02001549 * convert input to base64,
1550 * apply hash on input string (djb2, crc32, sdbm, wt6),
Thierry FOURNIER2e4893c2015-03-18 13:37:27 +01001551 * format date,
1552 * json escape,
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02001553 * extract preferred language comparing two lists,
Thierry FOURNIER2e4893c2015-03-18 13:37:27 +01001554 * turn to lower or upper chars,
1555 * deal with stick tables.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001556
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001557.. _channel_class:
1558
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001559Channel class
1560=============
1561
1562.. js:class:: Channel
1563
Christopher Faulet5a2c6612021-08-15 20:35:25 +02001564 **context**: action, sample-fetch, convert, filter
1565
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001566 HAProxy uses two buffers for the processing of the requests. The first one is
1567 used with the request data (from the client to the server) and the second is
1568 used for the response data (from the server to the client).
1569
1570 Each buffer contains two types of data. The first type is the incoming data
1571 waiting for a processing. The second part is the outgoing data already
1572 processed. Usually, the incoming data is processed, after it is tagged as
1573 outgoing data, and finally it is sent. The following functions provides tools
1574 for manipulating these data in a buffer.
1575
1576 The following diagram shows where the channel class function are applied.
1577
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001578 .. image:: _static/channel.png
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001579
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001580 .. warning::
1581 It is not possible to read from the response in request action, and it is
Boyang Li60cfe8b2022-05-10 18:11:00 +00001582 not possible to read from the request channel in response action.
Christopher Faulet09530392021-06-14 11:43:18 +02001583
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001584 .. warning::
1585 It is forbidden to alter the Channels buffer from HTTP contexts. So only
1586 :js:func:`Channel.input`, :js:func:`Channel.output`,
1587 :js:func:`Channel.may_recv`, :js:func:`Channel.is_full` and
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02001588 :js:func:`Channel.is_resp` can be called from a HTTP context.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001589
Christopher Faulet5a2c6612021-08-15 20:35:25 +02001590 All the functions provided by this class are available in the
1591 **sample-fetches**, **actions** and **filters** contexts. For **filters**,
1592 incoming data (offset and length) are relative to the filter. Some functions
Boyang Li60cfe8b2022-05-10 18:11:00 +00001593 may yield, but only for **actions**. Yield is not possible for
Christopher Faulet5a2c6612021-08-15 20:35:25 +02001594 **sample-fetches**, **converters** and **filters**.
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001595
1596.. js:function:: Channel.append(channel, string)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001597
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001598 This function copies the string **string** at the end of incoming data of the
1599 channel buffer. The function returns the copied length on success or -1 if
1600 data cannot be copied.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001601
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001602 Same that :js:func:`Channel.insert(channel, string, channel:input())`.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001603
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001604 :param class_channel channel: The manipulated Channel.
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02001605 :param string string: The data to copy at the end of incoming data.
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001606 :returns: an integer containing the amount of bytes copied or -1.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001607
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001608.. js:function:: Channel.data(channel [, offset [, length]])
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001609
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001610 This function returns **length** bytes of incoming data from the channel
1611 buffer, starting at the offset **offset**. The data are not removed from the
1612 buffer.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001613
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001614 By default, if no length is provided, all incoming data found, starting at the
1615 given offset, are returned. If **length** is set to -1, the function tries to
Christopher Faulet5a2c6612021-08-15 20:35:25 +02001616 retrieve a maximum of data and, if called by an action, it yields if
1617 necessary. It also waits for more data if the requested length exceeds the
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02001618 available amount of incoming data. Not providing an offset is the same as
Ilya Shipitsinff0f2782021-08-22 22:18:07 +05001619 setting it to 0. A positive offset is relative to the beginning of incoming
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02001620 data of the channel buffer while negative offset is relative to the end.
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001621
1622 If there is no incoming data and the channel can't receive more data, a 'nil'
1623 value is returned.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001624
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001625 :param class_channel channel: The manipulated Channel.
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001626 :param integer offset: *optional* The offset in incoming data to start to get
1627 data. 0 by default. May be negative to be relative to
1628 the end of incoming data.
1629 :param integer length: *optional* The expected length of data to retrieve. All
1630 incoming data by default. May be set to -1 to get a
1631 maximum of data.
1632 :returns: a string containing the data found or nil.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001633
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001634.. js:function:: Channel.forward(channel, length)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001635
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001636 This function forwards **length** bytes of data from the channel buffer. If
Christopher Faulet5a2c6612021-08-15 20:35:25 +02001637 the requested length exceeds the available amount of incoming data, and if
1638 called by an action, the function yields, waiting for more data to forward. It
1639 returns the amount of data forwarded.
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001640
1641 :param class_channel channel: The manipulated Channel.
1642 :param integer int: The amount of data to forward.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001643
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001644.. js:function:: Channel.input(channel)
1645
Christopher Faulet5a2c6612021-08-15 20:35:25 +02001646 This function returns the length of incoming data in the channel buffer. When
1647 called by a filter, this value is relative to the filter.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001648
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001649 :param class_channel channel: The manipulated Channel.
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001650 :returns: an integer containing the amount of available bytes.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001651
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001652.. js:function:: Channel.insert(channel, string [, offset])
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001653
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001654 This function copies the string **string** at the offset **offset** in
1655 incoming data of the channel buffer. The function returns the copied length on
1656 success or -1 if data cannot be copied.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001657
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001658 By default, if no offset is provided, the string is copied in front of
Ilya Shipitsinff0f2782021-08-22 22:18:07 +05001659 incoming data. A positive offset is relative to the beginning of incoming data
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001660 of the channel buffer while negative offset is relative to their end.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001661
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001662 :param class_channel channel: The manipulated Channel.
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02001663 :param string string: The data to copy into incoming data.
1664 :param integer offset: *optional* The offset in incoming data where to copy
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001665 data. 0 by default. May be negative to be relative to
1666 the end of incoming data.
Pieter Baauw4d7f7662015-11-08 16:38:08 +01001667 :returns: an integer containing the amount of bytes copied or -1.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001668
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001669.. js:function:: Channel.is_full(channel)
1670
1671 This function returns true if the channel buffer is full.
1672
Christopher Faulet5a2c6612021-08-15 20:35:25 +02001673 :param class_channel channel: The manipulated Channel.
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001674 :returns: a boolean
1675
1676.. js:function:: Channel.is_resp(channel)
1677
1678 This function returns true if the channel is the response one.
1679
Christopher Faulet5a2c6612021-08-15 20:35:25 +02001680 :param class_channel channel: The manipulated Channel.
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001681 :returns: a boolean
1682
1683.. js:function:: Channel.line(channel [, offset [, length]])
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001684
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001685 This function parses **length** bytes of incoming data of the channel buffer,
1686 starting at offset **offset**, and returns the first line found, including the
Christopher Faulet5a2c6612021-08-15 20:35:25 +02001687 '\\n'. The data are not removed from the buffer. If no line is found, all data
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001688 are returned.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001689
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001690 By default, if no length is provided, all incoming data, starting at the given
1691 offset, are evaluated. If **length** is set to -1, the function tries to
Christopher Faulet5a2c6612021-08-15 20:35:25 +02001692 retrieve a maximum of data and, if called by an action, yields if
1693 necessary. It also waits for more data if the requested length exceeds the
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02001694 available amount of incoming data. Not providing an offset is the same as
Ilya Shipitsinff0f2782021-08-22 22:18:07 +05001695 setting it to 0. A positive offset is relative to the beginning of incoming
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02001696 data of the channel buffer while negative offset is relative to the end.
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001697
1698 If there is no incoming data and the channel can't receive more data, a 'nil'
1699 value is returned.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001700
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001701 :param class_channel channel: The manipulated Channel.
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02001702 :param integer offset: *optional* The offset in incoming data to start to
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001703 parse data. 0 by default. May be negative to be
1704 relative to the end of incoming data.
1705 :param integer length: *optional* The length of data to parse. All incoming
1706 data by default. May be set to -1 to get a maximum of
1707 data.
1708 :returns: a string containing the line found or nil.
1709
1710.. js:function:: Channel.may_recv(channel)
1711
1712 This function returns true if the channel may still receive data.
1713
Christopher Faulet5a2c6612021-08-15 20:35:25 +02001714 :param class_channel channel: The manipulated Channel.
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001715 :returns: a boolean
1716
1717.. js:function:: Channel.output(channel)
1718
Christopher Faulet5a2c6612021-08-15 20:35:25 +02001719 This function returns the length of outgoing data of the channel buffer. When
1720 called by a filter, this value is relative to the filter.
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001721
1722 :param class_channel channel: The manipulated Channel.
1723 :returns: an integer containing the amount of available bytes.
1724
1725.. js:function:: Channel.prepend(channel, string)
1726
1727 This function copies the string **string** in front of incoming data of the
1728 channel buffer. The function returns the copied length on success or -1 if
1729 data cannot be copied.
1730
1731 Same that :js:func:`Channel.insert(channel, string, 0)`.
1732
1733 :param class_channel channel: The manipulated Channel.
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02001734 :param string string: The data to copy in front of incoming data.
Pieter Baauw4d7f7662015-11-08 16:38:08 +01001735 :returns: an integer containing the amount of bytes copied or -1.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001736
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001737.. js:function:: Channel.remove(channel [, offset [, length]])
1738
1739 This function removes **length** bytes of incoming data of the channel buffer,
1740 starting at offset **offset**. This function returns number of bytes removed
1741 on success.
1742
1743 By default, if no length is provided, all incoming data, starting at the given
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02001744 offset, are removed. Not providing an offset is the same as setting it
Ilya Shipitsinff0f2782021-08-22 22:18:07 +05001745 to 0. A positive offset is relative to the beginning of incoming data of the
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02001746 channel buffer while negative offset is relative to the end.
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001747
1748 :param class_channel channel: The manipulated Channel.
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02001749 :param integer offset: *optional* The offset in incoming data where to start
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001750 to remove data. 0 by default. May be negative to
1751 be relative to the end of incoming data.
1752 :param integer length: *optional* The length of data to remove. All incoming
1753 data by default.
1754 :returns: an integer containing the amount of bytes removed.
1755
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001756.. js:function:: Channel.send(channel, string)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001757
Christopher Faulet5a2c6612021-08-15 20:35:25 +02001758 This function requires immediate send of the string **string**. It means the
Ilya Shipitsinff0f2782021-08-22 22:18:07 +05001759 string is copied at the beginning of incoming data of the channel buffer and
Christopher Faulet5a2c6612021-08-15 20:35:25 +02001760 immediately forwarded. Unless if the connection is close, and if called by an
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02001761 action, this function yields to copy and forward all the string.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001762
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001763 :param class_channel channel: The manipulated Channel.
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001764 :param string string: The data to send.
Pieter Baauw4d7f7662015-11-08 16:38:08 +01001765 :returns: an integer containing the amount of bytes copied or -1.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001766
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001767.. js:function:: Channel.set(channel, string [, offset [, length]])
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001768
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02001769 This function replaces **length** bytes of incoming data of the channel buffer,
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001770 starting at offset **offset**, by the string **string**. The function returns
1771 the copied length on success or -1 if data cannot be copied.
1772
1773 By default, if no length is provided, all incoming data, starting at the given
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02001774 offset, are replaced. Not providing an offset is the same as setting it
Ilya Shipitsinff0f2782021-08-22 22:18:07 +05001775 to 0. A positive offset is relative to the beginning of incoming data of the
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02001776 channel buffer while negative offset is relative to the end.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001777
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001778 :param class_channel channel: The manipulated Channel.
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02001779 :param string string: The data to copy into incoming data.
1780 :param integer offset: *optional* The offset in incoming data where to start
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001781 the data replacement. 0 by default. May be negative to
1782 be relative to the end of incoming data.
1783 :param integer length: *optional* The length of data to replace. All incoming
1784 data by default.
1785 :returns: an integer containing the amount of bytes copied or -1.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001786
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001787.. js:function:: Channel.dup(channel)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001788
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001789 **DEPRECATED**
1790
1791 This function returns all incoming data found in the channel buffer. The data
Boyang Li60cfe8b2022-05-10 18:11:00 +00001792 are not removed from the buffer and can be reprocessed later.
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001793
1794 If there is no incoming data and the channel can't receive more data, a 'nil'
1795 value is returned.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001796
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001797 :param class_channel channel: The manipulated Channel.
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001798 :returns: a string containing all data found or nil.
1799
1800 .. warning::
1801 This function is deprecated. :js:func:`Channel.data()` must be used
1802 instead.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001803
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001804.. js:function:: Channel.get(channel)
1805
1806 **DEPRECATED**
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001807
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001808 This function returns all incoming data found in the channel buffer and remove
1809 them from the buffer.
1810
1811 If there is no incoming data and the channel can't receive more data, a 'nil'
1812 value is returned.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001813
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001814 :param class_channel channel: The manipulated Channel.
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001815 :returns: a string containing all the data found or nil.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001816
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001817 .. warning::
1818 This function is deprecated. :js:func:`Channel.data()` must be used to
1819 retrieve data followed by a call to :js:func:`Channel:remove()` to remove
1820 data.
Thierry FOURNIER / OZON.IO65192f32016-11-07 15:28:40 +01001821
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001822 .. code-block:: lua
Thierry FOURNIER / OZON.IO65192f32016-11-07 15:28:40 +01001823
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001824 local data = chn:data()
1825 chn:remove(0, data:len())
1826
1827 ..
1828
1829.. js:function:: Channel.getline(channel)
1830
1831 **DEPRECATED**
1832
1833 This function returns the first line found in incoming data of the channel
Christopher Faulet5a2c6612021-08-15 20:35:25 +02001834 buffer, including the '\\n'. The returned data are removed from the buffer. If
1835 no line is found, and if called by an action, this function yields to wait for
1836 more data, except if the channel can't receive more data. In this case all
1837 data are returned.
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001838
1839 If there is no incoming data and the channel can't receive more data, a 'nil'
1840 value is returned.
1841
1842 :param class_channel channel: The manipulated Channel.
1843 :returns: a string containing the line found or nil.
1844
1845 .. warning::
Boyang Li60cfe8b2022-05-10 18:11:00 +00001846 This function is deprecated. :js:func:`Channel.line()` must be used to
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001847 retrieve a line followed by a call to :js:func:`Channel:remove()` to remove
1848 data.
1849
1850 .. code-block:: lua
1851
1852 local line = chn:line(0, -1)
1853 chn:remove(0, line:len())
1854
1855 ..
1856
1857.. js:function:: Channel.get_in_len(channel)
1858
Boyang Li60cfe8b2022-05-10 18:11:00 +00001859 **DEPRECATED**
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001860
Christopher Faulet5a2c6612021-08-15 20:35:25 +02001861 This function returns the length of the input part of the buffer. When called
1862 by a filter, this value is relative to the filter.
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001863
1864 :param class_channel channel: The manipulated Channel.
1865 :returns: an integer containing the amount of available bytes.
1866
1867 .. warning::
1868 This function is deprecated. :js:func:`Channel.input()` must be used
1869 instead.
1870
1871.. js:function:: Channel.get_out_len(channel)
1872
Boyang Li60cfe8b2022-05-10 18:11:00 +00001873 **DEPRECATED**
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001874
Christopher Faulet5a2c6612021-08-15 20:35:25 +02001875 This function returns the length of the output part of the buffer. When called
1876 by a filter, this value is relative to the filter.
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001877
1878 :param class_channel channel: The manipulated Channel.
1879 :returns: an integer containing the amount of available bytes.
1880
1881 .. warning::
1882 This function is deprecated. :js:func:`Channel.output()` must be used
1883 instead.
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001884
1885.. _http_class:
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001886
1887HTTP class
1888==========
1889
1890.. js:class:: HTTP
1891
1892 This class contain all the HTTP manipulation functions.
1893
Pieter Baauw386a1272015-08-16 15:26:24 +02001894.. js:function:: HTTP.req_get_headers(http)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001895
Patrick Hemmerc6a1d712018-05-01 21:30:41 -04001896 Returns a table containing all the request headers.
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001897
1898 :param class_http http: The related http object.
Patrick Hemmerc6a1d712018-05-01 21:30:41 -04001899 :returns: table of headers.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01001900 :see: :js:func:`HTTP.res_get_headers`
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001901
Patrick Hemmerc6a1d712018-05-01 21:30:41 -04001902 This is the form of the returned table:
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001903
1904.. code-block:: lua
1905
1906 HTTP:req_get_headers()['<header-name>'][<header-index>] = "<header-value>"
1907
1908 local hdr = HTTP:req_get_headers()
1909 hdr["host"][0] = "www.test.com"
1910 hdr["accept"][0] = "audio/basic q=1"
1911 hdr["accept"][1] = "audio/*, q=0.2"
1912 hdr["accept"][2] = "*/*, q=0.1"
1913..
1914
Pieter Baauw386a1272015-08-16 15:26:24 +02001915.. js:function:: HTTP.res_get_headers(http)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001916
Patrick Hemmerc6a1d712018-05-01 21:30:41 -04001917 Returns a table containing all the response headers.
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001918
1919 :param class_http http: The related http object.
Patrick Hemmerc6a1d712018-05-01 21:30:41 -04001920 :returns: table of headers.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01001921 :see: :js:func:`HTTP.req_get_headers`
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001922
Patrick Hemmerc6a1d712018-05-01 21:30:41 -04001923 This is the form of the returned table:
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001924
1925.. code-block:: lua
1926
1927 HTTP:res_get_headers()['<header-name>'][<header-index>] = "<header-value>"
1928
1929 local hdr = HTTP:req_get_headers()
1930 hdr["host"][0] = "www.test.com"
1931 hdr["accept"][0] = "audio/basic q=1"
1932 hdr["accept"][1] = "audio/*, q=0.2"
1933 hdr["accept"][2] = "*.*, q=0.1"
1934..
1935
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001936.. js:function:: HTTP.req_add_header(http, name, value)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001937
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02001938 Appends a HTTP header field in the request whose name is
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001939 specified in "name" and whose value is defined in "value".
1940
1941 :param class_http http: The related http object.
1942 :param string name: The header name.
1943 :param string value: The header value.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01001944 :see: :js:func:`HTTP.res_add_header`
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001945
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001946.. js:function:: HTTP.res_add_header(http, name, value)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001947
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02001948 Appends a HTTP header field in the response whose name is
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001949 specified in "name" and whose value is defined in "value".
1950
1951 :param class_http http: The related http object.
1952 :param string name: The header name.
1953 :param string value: The header value.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01001954 :see: :js:func:`HTTP.req_add_header`
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001955
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001956.. js:function:: HTTP.req_del_header(http, name)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001957
1958 Removes all HTTP header fields in the request whose name is
1959 specified in "name".
1960
1961 :param class_http http: The related http object.
1962 :param string name: The header name.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01001963 :see: :js:func:`HTTP.res_del_header`
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001964
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001965.. js:function:: HTTP.res_del_header(http, name)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001966
1967 Removes all HTTP header fields in the response whose name is
1968 specified in "name".
1969
1970 :param class_http http: The related http object.
1971 :param string name: The header name.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01001972 :see: :js:func:`HTTP.req_del_header`
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001973
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001974.. js:function:: HTTP.req_set_header(http, name, value)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001975
Bertrand Jacquin874a35c2018-09-10 21:26:07 +01001976 This variable replace all occurrence of all header "name", by only
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001977 one containing the "value".
1978
1979 :param class_http http: The related http object.
1980 :param string name: The header name.
1981 :param string value: The header value.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01001982 :see: :js:func:`HTTP.res_set_header`
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001983
Bertrand Jacquin874a35c2018-09-10 21:26:07 +01001984 This function does the same work as the following code:
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001985
1986.. code-block:: lua
1987
1988 function fcn(txn)
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001989 TXN.http:req_del_header("header")
1990 TXN.http:req_add_header("header", "value")
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001991 end
1992..
1993
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001994.. js:function:: HTTP.res_set_header(http, name, value)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001995
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02001996 This function replaces all occurrence of all header "name", by only
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001997 one containing the "value".
1998
1999 :param class_http http: The related http object.
2000 :param string name: The header name.
2001 :param string value: The header value.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01002002 :see: :js:func:`HTTP.req_rep_header()`
Thierry FOURNIER08504f42015-03-16 14:17:08 +01002003
Pieter Baauw386a1272015-08-16 15:26:24 +02002004.. js:function:: HTTP.req_rep_header(http, name, regex, replace)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01002005
2006 Matches the regular expression in all occurrences of header field "name"
2007 according to "regex", and replaces them with the "replace" argument. The
2008 replacement value can contain back references like \1, \2, ... This
2009 function works with the request.
2010
2011 :param class_http http: The related http object.
2012 :param string name: The header name.
2013 :param string regex: The match regular expression.
2014 :param string replace: The replacement value.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01002015 :see: :js:func:`HTTP.res_rep_header()`
Thierry FOURNIER08504f42015-03-16 14:17:08 +01002016
Pieter Baauw386a1272015-08-16 15:26:24 +02002017.. js:function:: HTTP.res_rep_header(http, name, regex, string)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01002018
2019 Matches the regular expression in all occurrences of header field "name"
2020 according to "regex", and replaces them with the "replace" argument. The
2021 replacement value can contain back references like \1, \2, ... This
2022 function works with the request.
2023
2024 :param class_http http: The related http object.
2025 :param string name: The header name.
2026 :param string regex: The match regular expression.
2027 :param string replace: The replacement value.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01002028 :see: :js:func:`HTTP.req_rep_header()`
Thierry FOURNIER08504f42015-03-16 14:17:08 +01002029
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002030.. js:function:: HTTP.req_set_method(http, method)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01002031
2032 Rewrites the request method with the parameter "method".
2033
2034 :param class_http http: The related http object.
2035 :param string method: The new method.
2036
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002037.. js:function:: HTTP.req_set_path(http, path)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01002038
2039 Rewrites the request path with the "path" parameter.
2040
2041 :param class_http http: The related http object.
2042 :param string path: The new path.
2043
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002044.. js:function:: HTTP.req_set_query(http, query)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01002045
2046 Rewrites the request's query string which appears after the first question
2047 mark ("?") with the parameter "query".
2048
2049 :param class_http http: The related http object.
2050 :param string query: The new query.
2051
Thierry FOURNIER0d79cf62015-08-26 14:20:58 +02002052.. js:function:: HTTP.req_set_uri(http, uri)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01002053
2054 Rewrites the request URI with the parameter "uri".
2055
2056 :param class_http http: The related http object.
2057 :param string uri: The new uri.
2058
Robin H. Johnson52f5db22017-01-01 13:10:52 -08002059.. js:function:: HTTP.res_set_status(http, status [, reason])
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +02002060
Robin H. Johnson52f5db22017-01-01 13:10:52 -08002061 Rewrites the response status code with the parameter "code".
2062
2063 If no custom reason is provided, it will be generated from the status.
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +02002064
2065 :param class_http http: The related http object.
2066 :param integer status: The new response status code.
Robin H. Johnson52f5db22017-01-01 13:10:52 -08002067 :param string reason: The new response reason (optional).
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +02002068
William Lallemand00a15022021-11-19 16:02:44 +01002069.. _httpclient_class:
2070
2071HTTPClient class
2072================
2073
2074.. js:class:: HTTPClient
2075
2076 The httpclient class allows issue of outbound HTTP requests through a simple
2077 API without the knowledge of HAProxy internals.
2078
2079.. js:function:: HTTPClient.get(httpclient, request)
2080.. js:function:: HTTPClient.head(httpclient, request)
2081.. js:function:: HTTPClient.put(httpclient, request)
2082.. js:function:: HTTPClient.post(httpclient, request)
2083.. js:function:: HTTPClient.delete(httpclient, request)
2084
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02002085 Send a HTTP request and wait for a response. GET, HEAD PUT, POST and DELETE methods can be used.
2086 The HTTPClient will send asynchronously the data and is able to send and receive more than HAProxy bufsize.
William Lallemand00a15022021-11-19 16:02:44 +01002087
William Lallemanda9256192022-10-21 11:48:24 +02002088 The HTTPClient interface is not able to decompress responses, it is not
2089 recommended to send an Accept-Encoding in the request so the response is
2090 received uncompressed.
William Lallemand00a15022021-11-19 16:02:44 +01002091
2092 :param class httpclient: Is the manipulated HTTPClient.
2093 :param table request: Is a table containing the parameters of the request that will be send.
2094 :param string request.url: Is a mandatory parameter for the request that contains the URL.
2095 :param string request.body: Is an optional parameter for the request that contains the body to send.
2096 :param table request.headers: Is an optional parameter for the request that contains the headers to send.
William Lallemand18340302022-02-23 15:57:45 +01002097 :param string request.dst: Is an optional parameter for the destination in haproxy address format.
William Lallemandb4a4ef62022-02-23 14:18:16 +01002098 :param integer request.timeout: Optional timeout parameter, set a "timeout server" on the connections.
William Lallemand00a15022021-11-19 16:02:44 +01002099 :returns: Lua table containing the response
2100
2101
2102.. code-block:: lua
2103
2104 local httpclient = core.httpclient()
William Lallemand4f4f2b72022-02-17 20:00:23 +01002105 local response = httpclient:post{url="http://127.0.0.1", body=body, dst="unix@/var/run/http.sock"}
William Lallemand00a15022021-11-19 16:02:44 +01002106
2107..
2108
2109.. code-block:: lua
2110
2111 response = {
2112 status = 400,
2113 reason = "Bad request",
2114 headers = {
2115 ["content-type"] = { "text/html" },
2116 ["cache-control"] = { "no-cache", "no-store" },
2117 },
William Lallemand4f4f2b72022-02-17 20:00:23 +01002118 body = "<html><body><h1>invalid request<h1></body></html>",
William Lallemand00a15022021-11-19 16:02:44 +01002119 }
2120..
2121
2122
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002123.. _txn_class:
2124
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002125TXN class
2126=========
2127
2128.. js:class:: TXN
2129
2130 The txn class contain all the functions relative to the http or tcp
2131 transaction (Note than a tcp stream is the same than a tcp transaction, but
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02002132 a HTTP transaction is not the same than a tcp stream).
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002133
2134 The usage of this class permits to retrieve data from the requests, alter it
2135 and forward it.
2136
2137 All the functions provided by this class are available in the context
Christopher Faulet5a2c6612021-08-15 20:35:25 +02002138 **sample-fetches**, **actions** and **filters**.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002139
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002140.. js:attribute:: TXN.c
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002141
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002142 :returns: An :ref:`converters_class`.
2143
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002144 This attribute contains a Converters class object.
2145
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002146.. js:attribute:: TXN.sc
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002147
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002148 :returns: An :ref:`converters_class`.
2149
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002150 This attribute contains a Converters class object. The functions of
2151 this object returns always a string.
2152
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002153.. js:attribute:: TXN.f
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002154
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002155 :returns: An :ref:`fetches_class`.
2156
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002157 This attribute contains a Fetches class object.
2158
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002159.. js:attribute:: TXN.sf
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002160
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002161 :returns: An :ref:`fetches_class`.
2162
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002163 This attribute contains a Fetches class object. The functions of
2164 this object returns always a string.
2165
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002166.. js:attribute:: TXN.req
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002167
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002168 :returns: An :ref:`channel_class`.
2169
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002170 This attribute contains a channel class object for the request buffer.
2171
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002172.. js:attribute:: TXN.res
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002173
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002174 :returns: An :ref:`channel_class`.
2175
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002176 This attribute contains a channel class object for the response buffer.
2177
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002178.. js:attribute:: TXN.http
Thierry FOURNIER08504f42015-03-16 14:17:08 +01002179
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002180 :returns: An :ref:`http_class`.
2181
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02002182 This attribute contains a HTTP class object. It is available only if the
Thierry FOURNIER08504f42015-03-16 14:17:08 +01002183 proxy has the "mode http" enabled.
2184
Christopher Faulet5a2c6612021-08-15 20:35:25 +02002185.. js:attribute:: TXN.http_req
2186
2187 :returns: An :ref:`httpmessage_class`.
2188
2189 This attribute contains the request HTTPMessage class object. It is available
2190 only if the proxy has the "mode http" enabled and only in the **filters**
2191 context.
2192
2193.. js:attribute:: TXN.http_res
2194
2195 :returns: An :ref:`httpmessage_class`.
2196
2197 This attribute contains the response HTTPMessage class object. It is available
2198 only if the proxy has the "mode http" enabled and only in the **filters**
2199 context.
2200
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01002201.. js:function:: TXN.log(TXN, loglevel, msg)
2202
2203 This function sends a log. The log is sent, according with the HAProxy
2204 configuration file, on the default syslog server if it is configured and on
2205 the stderr if it is allowed.
2206
2207 :param class_txn txn: The class txn object containing the data.
Bertrand Jacquin874a35c2018-09-10 21:26:07 +01002208 :param integer loglevel: Is the log level associated with the message. It is a
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01002209 number between 0 and 7.
2210 :param string msg: The log content.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01002211 :see: :js:attr:`core.emerg`, :js:attr:`core.alert`, :js:attr:`core.crit`,
2212 :js:attr:`core.err`, :js:attr:`core.warning`, :js:attr:`core.notice`,
2213 :js:attr:`core.info`, :js:attr:`core.debug` (log level definitions)
2214 :see: :js:func:`TXN.deflog`
2215 :see: :js:func:`TXN.Debug`
2216 :see: :js:func:`TXN.Info`
2217 :see: :js:func:`TXN.Warning`
2218 :see: :js:func:`TXN.Alert`
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01002219
2220.. js:function:: TXN.deflog(TXN, msg)
2221
Bertrand Jacquin874a35c2018-09-10 21:26:07 +01002222 Sends a log line with the default loglevel for the proxy associated with the
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01002223 transaction.
2224
2225 :param class_txn txn: The class txn object containing the data.
2226 :param string msg: The log content.
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02002227 :see: :js:func:`TXN.log`
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01002228
2229.. js:function:: TXN.Debug(txn, msg)
2230
2231 :param class_txn txn: The class txn object containing the data.
2232 :param string msg: The log content.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01002233 :see: :js:func:`TXN.log`
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01002234
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02002235 Does the same job as:
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01002236
2237.. code-block:: lua
2238
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002239 function Debug(txn, msg)
2240 TXN.log(txn, core.debug, msg)
2241 end
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01002242..
2243
2244.. js:function:: TXN.Info(txn, msg)
2245
2246 :param class_txn txn: The class txn object containing the data.
2247 :param string msg: The log content.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01002248 :see: :js:func:`TXN.log`
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01002249
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02002250 Does the same job as:
2251
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01002252.. code-block:: lua
2253
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02002254 function Info(txn, msg)
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002255 TXN.log(txn, core.info, msg)
2256 end
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01002257..
2258
2259.. js:function:: TXN.Warning(txn, msg)
2260
2261 :param class_txn txn: The class txn object containing the data.
2262 :param string msg: The log content.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01002263 :see: :js:func:`TXN.log`
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01002264
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02002265 Does the same job as:
2266
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01002267.. code-block:: lua
2268
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02002269 function Warning(txn, msg)
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002270 TXN.log(txn, core.warning, msg)
2271 end
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01002272..
2273
2274.. js:function:: TXN.Alert(txn, msg)
2275
2276 :param class_txn txn: The class txn object containing the data.
2277 :param string msg: The log content.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01002278 :see: :js:func:`TXN.log`
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01002279
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02002280 Does the same job as:
2281
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01002282.. code-block:: lua
2283
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02002284 function Alert(txn, msg)
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002285 TXN.log(txn, core.alert, msg)
2286 end
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01002287..
2288
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002289.. js:function:: TXN.get_priv(txn)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002290
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002291 Return Lua data stored in the current transaction (with the `TXN.set_priv()`)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002292 function. If no data are stored, it returns a nil value.
2293
2294 :param class_txn txn: The class txn object containing the data.
Bertrand Jacquin874a35c2018-09-10 21:26:07 +01002295 :returns: the opaque data previously stored, or nil if nothing is
2296 available.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002297
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002298.. js:function:: TXN.set_priv(txn, data)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002299
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02002300 Store any data in the current HAProxy transaction. This action replaces the
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002301 old stored data.
2302
2303 :param class_txn txn: The class txn object containing the data.
2304 :param opaque data: The data which is stored in the transaction.
2305
Tim Duesterhus4e172c92020-05-19 13:49:42 +02002306.. js:function:: TXN.set_var(TXN, var, value[, ifexist])
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +02002307
David Carlier61fdf8b2015-10-02 11:59:38 +01002308 Converts a Lua type in a HAProxy type and store it in a variable <var>.
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +02002309
2310 :param class_txn txn: The class txn object containing the data.
2311 :param string var: The variable name according with the HAProxy variable syntax.
Thierry FOURNIER / OZON.IOb210bcc2016-12-12 16:24:16 +01002312 :param type value: The value associated to the variable. The type can be string or
2313 integer.
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02002314 :param boolean ifexist: If this parameter is set to true the variable
Tim Duesterhus4e172c92020-05-19 13:49:42 +02002315 will only be set if it was defined elsewhere (i.e. used
Willy Tarreau7978c5c2021-09-07 14:24:07 +02002316 within the configuration). For global variables (using the
2317 "proc" scope), they will only be updated and never created.
2318 It is highly recommended to always set this to true.
Christopher Faulet85d79c92016-11-09 16:54:56 +01002319
2320.. js:function:: TXN.unset_var(TXN, var)
2321
2322 Unset the variable <var>.
2323
2324 :param class_txn txn: The class txn object containing the data.
2325 :param string var: The variable name according with the HAProxy variable syntax.
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +02002326
2327.. js:function:: TXN.get_var(TXN, var)
2328
2329 Returns data stored in the variable <var> converter in Lua type.
2330
2331 :param class_txn txn: The class txn object containing the data.
2332 :param string var: The variable name according with the HAProxy variable syntax.
2333
Christopher Faulet700d9e82020-01-31 12:21:52 +01002334.. js:function:: TXN.reply([reply])
2335
2336 Return a new reply object
2337
2338 :param table reply: A table containing info to initialize the reply fields.
2339 :returns: A :ref:`reply_class` object.
2340
2341 The table used to initialized the reply object may contain following entries :
2342
2343 * status : The reply status code. the code 200 is used by default.
2344 * reason : The reply reason. The reason corresponding to the status code is
2345 used by default.
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02002346 * headers : A list of headers, indexed by header name. Empty by default. For
Christopher Faulet700d9e82020-01-31 12:21:52 +01002347 a given name, multiple values are possible, stored in an ordered list.
2348 * body : The reply body, empty by default.
2349
2350.. code-block:: lua
2351
2352 local reply = txn:reply{
2353 status = 400,
2354 reason = "Bad request",
2355 headers = {
2356 ["content-type"] = { "text/html" },
2357 ["cache-control"] = {"no-cache", "no-store" }
2358 },
2359 body = "<html><body><h1>invalid request<h1></body></html>"
2360 }
2361..
2362 :see: :js:class:`Reply`
2363
2364.. js:function:: TXN.done(txn[, reply])
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002365
Willy Tarreaubc183a62015-08-28 10:39:11 +02002366 This function terminates processing of the transaction and the associated
Christopher Faulet700d9e82020-01-31 12:21:52 +01002367 session and optionally reply to the client for HTTP sessions.
2368
2369 :param class_txn txn: The class txn object containing the data.
2370 :param class_reply reply: The class reply object to return to the client.
2371
2372 This functions can be used when a critical error is detected or to terminate
Willy Tarreaubc183a62015-08-28 10:39:11 +02002373 processing after some data have been returned to the client (eg: a redirect).
Christopher Faulet700d9e82020-01-31 12:21:52 +01002374 To do so, a reply may be provided. This object is optional and may contain a
2375 status code, a reason, a header list and a body. All these fields are
Christopher Faulet7855b192021-11-09 18:39:51 +01002376 optional. When not provided, the default values are used. By default, with an
2377 empty reply object, an empty HTTP 200 response is returned to the client. If
2378 no reply object is provided, the transaction is terminated without any
2379 reply. If a reply object is provided, it must not exceed the buffer size once
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02002380 converted into the internal HTTP representation. Because for now there is no
Christopher Faulet7855b192021-11-09 18:39:51 +01002381 easy way to be sure it fits, it is probably better to keep it reasonably
2382 small.
Christopher Faulet700d9e82020-01-31 12:21:52 +01002383
2384 The reply object may be fully created in lua or the class Reply may be used to
2385 create it.
2386
2387.. code-block:: lua
2388
2389 local reply = txn:reply()
2390 reply:set_status(400, "Bad request")
2391 reply:add_header("content-type", "text/html")
2392 reply:add_header("cache-control", "no-cache")
2393 reply:add_header("cache-control", "no-store")
2394 reply:set_body("<html><body><h1>invalid request<h1></body></html>")
2395 txn:done(reply)
2396..
2397
2398.. code-block:: lua
2399
2400 txn:done{
2401 status = 400,
2402 reason = "Bad request",
2403 headers = {
2404 ["content-type"] = { "text/html" },
2405 ["cache-control"] = { "no-cache", "no-store" },
2406 },
2407 body = "<html><body><h1>invalid request<h1></body></html>"
2408 }
2409..
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002410
Christopher Faulet1e9b1b62021-08-11 10:14:30 +02002411 .. warning::
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02002412 It does not make sense to call this function from sample-fetches. In this case
2413 the behavior is the same than core.done(): it finishes the Lua
Christopher Faulet1e9b1b62021-08-11 10:14:30 +02002414 execution. The transaction is really aborted only from an action registered
2415 function.
Thierry FOURNIERab00df62016-07-14 11:42:37 +02002416
Christopher Faulet700d9e82020-01-31 12:21:52 +01002417 :see: :js:func:`TXN.reply`, :js:class:`Reply`
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002418
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002419.. js:function:: TXN.set_loglevel(txn, loglevel)
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01002420
2421 Is used to change the log level of the current request. The "loglevel" must
2422 be an integer between 0 and 7.
2423
2424 :param class_txn txn: The class txn object containing the data.
2425 :param integer loglevel: The required log level. This variable can be one of
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01002426 :see: :js:attr:`core.emerg`, :js:attr:`core.alert`, :js:attr:`core.crit`,
2427 :js:attr:`core.err`, :js:attr:`core.warning`, :js:attr:`core.notice`,
2428 :js:attr:`core.info`, :js:attr:`core.debug` (log level definitions)
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01002429
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002430.. js:function:: TXN.set_tos(txn, tos)
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01002431
2432 Is used to set the TOS or DSCP field value of packets sent to the client to
2433 the value passed in "tos" on platforms which support this.
2434
2435 :param class_txn txn: The class txn object containing the data.
2436 :param integer tos: The new TOS os DSCP.
2437
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002438.. js:function:: TXN.set_mark(txn, mark)
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01002439
2440 Is used to set the Netfilter MARK on all packets sent to the client to the
2441 value passed in "mark" on platforms which support it.
2442
2443 :param class_txn txn: The class txn object containing the data.
2444 :param integer mark: The mark value.
2445
Patrick Hemmer268a7072018-05-11 12:52:31 -04002446.. js:function:: TXN.set_priority_class(txn, prio)
2447
2448 This function adjusts the priority class of the transaction. The value should
2449 be within the range -2047..2047. Values outside this range will be
2450 truncated.
2451
2452 See the HAProxy configuration.txt file keyword "http-request" action
2453 "set-priority-class" for details.
2454
2455.. js:function:: TXN.set_priority_offset(txn, prio)
2456
2457 This function adjusts the priority offset of the transaction. The value
2458 should be within the range -524287..524287. Values outside this range will be
2459 truncated.
2460
2461 See the HAProxy configuration.txt file keyword "http-request" action
2462 "set-priority-offset" for details.
2463
Christopher Faulet700d9e82020-01-31 12:21:52 +01002464.. _reply_class:
2465
2466Reply class
2467============
2468
2469.. js:class:: Reply
2470
2471 **context**: action
2472
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02002473 This class represents a HTTP response message. It provides some methods to
Christopher Faulet7855b192021-11-09 18:39:51 +01002474 enrich it. Once converted into the internal HTTP representation, the response
2475 message must not exceed the buffer size. Because for now there is no
2476 easy way to be sure it fits, it is probably better to keep it reasonably
2477 small.
2478
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02002479 See tune.bufsize in the configuration manual for details.
Christopher Faulet700d9e82020-01-31 12:21:52 +01002480
2481.. code-block:: lua
2482
2483 local reply = txn:reply({status = 400}) -- default HTTP 400 reason-phase used
2484 reply:add_header("content-type", "text/html")
2485 reply:add_header("cache-control", "no-cache")
2486 reply:add_header("cache-control", "no-store")
2487 reply:set_body("<html><body><h1>invalid request<h1></body></html>")
2488..
2489
2490 :see: :js:func:`TXN.reply`
2491
2492.. js:attribute:: Reply.status
2493
2494 The reply status code. By default, the status code is set to 200.
2495
2496 :returns: integer
2497
2498.. js:attribute:: Reply.reason
2499
2500 The reason string describing the status code.
2501
2502 :returns: string
2503
2504.. js:attribute:: Reply.headers
2505
2506 A table indexing all reply headers by name. To each name is associated an
2507 ordered list of values.
2508
2509 :returns: Lua table
2510
2511.. code-block:: lua
2512
2513 {
2514 ["content-type"] = { "text/html" },
2515 ["cache-control"] = {"no-cache", "no-store" },
2516 x_header_name = { "value1", "value2", ... }
2517 ...
2518 }
2519..
2520
2521.. js:attribute:: Reply.body
2522
2523 The reply payload.
2524
2525 :returns: string
2526
2527.. js:function:: Reply.set_status(REPLY, status[, reason])
2528
2529 Set the reply status code and optionally the reason-phrase. If the reason is
2530 not provided, the default reason corresponding to the status code is used.
2531
2532 :param class_reply reply: The related Reply object.
2533 :param integer status: The reply status code.
2534 :param string reason: The reply status reason (optional).
2535
2536.. js:function:: Reply.add_header(REPLY, name, value)
2537
2538 Add a header to the reply object. If the header does not already exist, a new
2539 entry is created with its name as index and a one-element list containing its
2540 value as value. Otherwise, the header value is appended to the ordered list of
2541 values associated to the header name.
2542
2543 :param class_reply reply: The related Reply object.
2544 :param string name: The header field name.
2545 :param string value: The header field value.
2546
2547.. js:function:: Reply.del_header(REPLY, name)
2548
2549 Remove all occurrences of a header name from the reply object.
2550
2551 :param class_reply reply: The related Reply object.
2552 :param string name: The header field name.
2553
2554.. js:function:: Reply.set_body(REPLY, body)
2555
2556 Set the reply payload.
2557
2558 :param class_reply reply: The related Reply object.
2559 :param string body: The reply payload.
2560
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002561.. _socket_class:
2562
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002563Socket class
2564============
2565
2566.. js:class:: Socket
2567
2568 This class must be compatible with the Lua Socket class. Only the 'client'
2569 functions are available. See the Lua Socket documentation:
2570
2571 `http://w3.impa.br/~diego/software/luasocket/tcp.html
2572 <http://w3.impa.br/~diego/software/luasocket/tcp.html>`_
2573
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002574.. js:function:: Socket.close(socket)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002575
2576 Closes a TCP object. The internal socket used by the object is closed and the
2577 local address to which the object was bound is made available to other
2578 applications. No further operations (except for further calls to the close
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002579 method) are allowed on a closed Socket.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002580
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002581 :param class_socket socket: Is the manipulated Socket.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002582
2583 Note: It is important to close all used sockets once they are not needed,
2584 since, in many systems, each socket uses a file descriptor, which are limited
2585 system resources. Garbage-collected objects are automatically closed before
2586 destruction, though.
2587
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002588.. js:function:: Socket.connect(socket, address[, port])
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002589
2590 Attempts to connect a socket object to a remote host.
2591
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002592
2593 In case of error, the method returns nil followed by a string describing the
2594 error. In case of success, the method returns 1.
2595
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002596 :param class_socket socket: Is the manipulated Socket.
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002597 :param string address: can be an IP address or a host name. See below for more
2598 information.
2599 :param integer port: must be an integer number in the range [1..64K].
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002600 :returns: 1 or nil.
2601
Bertrand Jacquin874a35c2018-09-10 21:26:07 +01002602 An address field extension permits to use the connect() function to connect to
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002603 other stream than TCP. The syntax containing a simpleipv4 or ipv6 address is
2604 the basically expected format. This format requires the port.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002605
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002606 Other format accepted are a socket path like "/socket/path", it permits to
Bertrand Jacquin874a35c2018-09-10 21:26:07 +01002607 connect to a socket. Abstract namespaces are supported with the prefix
Joseph Herlant02cedc42018-11-13 19:45:17 -08002608 "abns@", and finally a file descriptor can be passed with the prefix "fd@".
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002609 The prefix "ipv4@", "ipv6@" and "unix@" are also supported. The port can be
Bertrand Jacquin874a35c2018-09-10 21:26:07 +01002610 passed int the string. The syntax "127.0.0.1:1234" is valid. In this case, the
Tim Duesterhus6edab862018-01-06 19:04:45 +01002611 parameter *port* must not be set.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002612
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002613.. js:function:: Socket.connect_ssl(socket, address, port)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002614
2615 Same behavior than the function socket:connect, but uses SSL.
2616
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002617 :param class_socket socket: Is the manipulated Socket.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002618 :returns: 1 or nil.
2619
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002620.. js:function:: Socket.getpeername(socket)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002621
2622 Returns information about the remote side of a connected client object.
2623
2624 Returns a string with the IP address of the peer, followed by the port number
2625 that peer is using for the connection. In case of error, the method returns
2626 nil.
2627
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002628 :param class_socket socket: Is the manipulated Socket.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002629 :returns: a string containing the server information.
2630
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002631.. js:function:: Socket.getsockname(socket)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002632
2633 Returns the local address information associated to the object.
2634
2635 The method returns a string with local IP address and a number with the port.
2636 In case of error, the method returns nil.
2637
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002638 :param class_socket socket: Is the manipulated Socket.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002639 :returns: a string containing the client information.
2640
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002641.. js:function:: Socket.receive(socket, [pattern [, prefix]])
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002642
2643 Reads data from a client object, according to the specified read pattern.
2644 Patterns follow the Lua file I/O format, and the difference in performance
2645 between all patterns is negligible.
2646
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002647 :param class_socket socket: Is the manipulated Socket.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002648 :param string|integer pattern: Describe what is required (see below).
2649 :param string prefix: A string which will be prefix the returned data.
2650 :returns: a string containing the required data or nil.
2651
2652 Pattern can be any of the following:
2653
2654 * **`*a`**: reads from the socket until the connection is closed. No
2655 end-of-line translation is performed;
2656
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002657 * **`*l`**: reads a line of text from the Socket. The line is terminated by a
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002658 LF character (ASCII 10), optionally preceded by a CR character
2659 (ASCII 13). The CR and LF characters are not included in the
2660 returned line. In fact, all CR characters are ignored by the
2661 pattern. This is the default pattern.
2662
2663 * **number**: causes the method to read a specified number of bytes from the
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002664 Socket. Prefix is an optional string to be concatenated to the
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002665 beginning of any received data before return.
2666
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002667 * **empty**: If the pattern is left empty, the default option is `*l`.
2668
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002669 If successful, the method returns the received pattern. In case of error, the
2670 method returns nil followed by an error message which can be the string
2671 'closed' in case the connection was closed before the transmission was
2672 completed or the string 'timeout' in case there was a timeout during the
2673 operation. Also, after the error message, the function returns the partial
2674 result of the transmission.
2675
2676 Important note: This function was changed severely. It used to support
2677 multiple patterns (but I have never seen this feature used) and now it
2678 doesn't anymore. Partial results used to be returned in the same way as
2679 successful results. This last feature violated the idea that all functions
2680 should return nil on error. Thus it was changed too.
2681
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002682.. js:function:: Socket.send(socket, data [, start [, end ]])
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002683
2684 Sends data through client object.
2685
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002686 :param class_socket socket: Is the manipulated Socket.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002687 :param string data: The data that will be sent.
2688 :param integer start: The start position in the buffer of the data which will
2689 be sent.
2690 :param integer end: The end position in the buffer of the data which will
2691 be sent.
2692 :returns: see below.
2693
2694 Data is the string to be sent. The optional arguments i and j work exactly
2695 like the standard string.sub Lua function to allow the selection of a
2696 substring to be sent.
2697
2698 If successful, the method returns the index of the last byte within [start,
2699 end] that has been sent. Notice that, if start is 1 or absent, this is
2700 effectively the total number of bytes sent. In case of error, the method
2701 returns nil, followed by an error message, followed by the index of the last
2702 byte within [start, end] that has been sent. You might want to try again from
2703 the byte following that. The error message can be 'closed' in case the
2704 connection was closed before the transmission was completed or the string
2705 'timeout' in case there was a timeout during the operation.
2706
2707 Note: Output is not buffered. For small strings, it is always better to
2708 concatenate them in Lua (with the '..' operator) and send the result in one
2709 call instead of calling the method several times.
2710
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002711.. js:function:: Socket.setoption(socket, option [, value])
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002712
2713 Just implemented for compatibility, this cal does nothing.
2714
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002715.. js:function:: Socket.settimeout(socket, value [, mode])
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002716
2717 Changes the timeout values for the object. All I/O operations are blocking.
2718 That is, any call to the methods send, receive, and accept will block
2719 indefinitely, until the operation completes. The settimeout method defines a
2720 limit on the amount of time the I/O methods can block. When a timeout time
2721 has elapsed, the affected methods give up and fail with an error code.
2722
2723 The amount of time to wait is specified as the value parameter, in seconds.
2724
Mark Lakes56cc1252018-03-27 09:48:06 +02002725 The timeout modes are not implemented, the only settable timeout is the
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002726 inactivity time waiting for complete the internal buffer send or waiting for
2727 receive data.
2728
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002729 :param class_socket socket: Is the manipulated Socket.
Bertrand Jacquin874a35c2018-09-10 21:26:07 +01002730 :param float value: The timeout value. Use floating point to specify
Mark Lakes56cc1252018-03-27 09:48:06 +02002731 milliseconds.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002732
Thierry FOURNIER31904272017-10-25 12:59:51 +02002733.. _regex_class:
2734
2735Regex class
2736===========
2737
2738.. js:class:: Regex
2739
2740 This class allows the usage of HAProxy regexes because classic lua doesn't
2741 provides regexes. This class inherits the HAProxy compilation options, so the
2742 regexes can be libc regex, pcre regex or pcre JIT regex.
2743
2744 The expression matching number is limited to 20 per regex. The only available
2745 option is case sensitive.
2746
2747 Because regexes compilation is a heavy process, it is better to define all
2748 your regexes in the **body context** and use it during the runtime.
2749
2750.. code-block:: lua
2751
2752 -- Create the regex
2753 st, regex = Regex.new("needle (..) (...)", true);
2754
2755 -- Check compilation errors
2756 if st == false then
2757 print "error: " .. regex
2758 end
2759
2760 -- Match the regexes
2761 print(regex:exec("Looking for a needle in the haystack")) -- true
2762 print(regex:exec("Lokking for a cat in the haystack")) -- false
2763
2764 -- Extract words
2765 st, list = regex:match("Looking for a needle in the haystack")
2766 print(st) -- true
2767 print(list[1]) -- needle in the
2768 print(list[2]) -- in
2769 print(list[3]) -- the
2770
2771.. js:function:: Regex.new(regex, case_sensitive)
2772
2773 Create and compile a regex.
2774
2775 :param string regex: The regular expression according with the libc or pcre
2776 standard
2777 :param boolean case_sensitive: Match is case sensitive or not.
2778 :returns: boolean status and :ref:`regex_class` or string containing fail reason.
2779
2780.. js:function:: Regex.exec(regex, str)
2781
2782 Execute the regex.
2783
2784 :param class_regex regex: A :ref:`regex_class` object.
2785 :param string str: The input string will be compared with the compiled regex.
2786 :returns: a boolean status according with the match result.
2787
2788.. js:function:: Regex.match(regex, str)
2789
2790 Execute the regex and return matched expressions.
2791
2792 :param class_map map: A :ref:`regex_class` object.
2793 :param string str: The input string will be compared with the compiled regex.
2794 :returns: a boolean status according with the match result, and
2795 a table containing all the string matched in order of declaration.
2796
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002797.. _map_class:
2798
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002799Map class
2800=========
2801
2802.. js:class:: Map
2803
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02002804 This class permits to do some lookups in HAProxy maps. The declared maps can
Bertrand Jacquin874a35c2018-09-10 21:26:07 +01002805 be modified during the runtime through the HAProxy management socket.
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002806
2807.. code-block:: lua
2808
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002809 default = "usa"
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002810
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002811 -- Create and load map
Thierry FOURNIER4dc71972017-01-28 08:33:08 +01002812 geo = Map.new("geo.map", Map._ip);
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002813
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002814 -- Create new fetch that returns the user country
2815 core.register_fetches("country", function(txn)
2816 local src;
2817 local loc;
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002818
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002819 src = txn.f:fhdr("x-forwarded-for");
2820 if (src == nil) then
2821 src = txn.f:src()
2822 if (src == nil) then
2823 return default;
2824 end
2825 end
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002826
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002827 -- Perform lookup
2828 loc = geo:lookup(src);
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002829
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002830 if (loc == nil) then
2831 return default;
2832 end
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002833
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002834 return loc;
2835 end);
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002836
Thierry FOURNIER4dc71972017-01-28 08:33:08 +01002837.. js:attribute:: Map._int
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002838
2839 See the HAProxy configuration.txt file, chapter "Using ACLs and fetching
Ilya Shipitsin2075ca82020-03-06 23:22:22 +05002840 samples" and subchapter "ACL basics" to understand this pattern matching
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002841 method.
2842
Thierry FOURNIER4dc71972017-01-28 08:33:08 +01002843 Note that :js:attr:`Map.int` is also available for compatibility.
2844
2845.. js:attribute:: Map._ip
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002846
2847 See the HAProxy configuration.txt file, chapter "Using ACLs and fetching
Ilya Shipitsin2075ca82020-03-06 23:22:22 +05002848 samples" and subchapter "ACL basics" to understand this pattern matching
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002849 method.
2850
Thierry FOURNIER4dc71972017-01-28 08:33:08 +01002851 Note that :js:attr:`Map.ip` is also available for compatibility.
2852
2853.. js:attribute:: Map._str
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002854
2855 See the HAProxy configuration.txt file, chapter "Using ACLs and fetching
Ilya Shipitsin2075ca82020-03-06 23:22:22 +05002856 samples" and subchapter "ACL basics" to understand this pattern matching
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002857 method.
2858
Thierry FOURNIER4dc71972017-01-28 08:33:08 +01002859 Note that :js:attr:`Map.str` is also available for compatibility.
2860
2861.. js:attribute:: Map._beg
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002862
2863 See the HAProxy configuration.txt file, chapter "Using ACLs and fetching
Ilya Shipitsin2075ca82020-03-06 23:22:22 +05002864 samples" and subchapter "ACL basics" to understand this pattern matching
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002865 method.
2866
Thierry FOURNIER4dc71972017-01-28 08:33:08 +01002867 Note that :js:attr:`Map.beg` is also available for compatibility.
2868
2869.. js:attribute:: Map._sub
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002870
2871 See the HAProxy configuration.txt file, chapter "Using ACLs and fetching
Ilya Shipitsin2075ca82020-03-06 23:22:22 +05002872 samples" and subchapter "ACL basics" to understand this pattern matching
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002873 method.
2874
Thierry FOURNIER4dc71972017-01-28 08:33:08 +01002875 Note that :js:attr:`Map.sub` is also available for compatibility.
2876
2877.. js:attribute:: Map._dir
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002878
2879 See the HAProxy configuration.txt file, chapter "Using ACLs and fetching
Ilya Shipitsin2075ca82020-03-06 23:22:22 +05002880 samples" and subchapter "ACL basics" to understand this pattern matching
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002881 method.
2882
Thierry FOURNIER4dc71972017-01-28 08:33:08 +01002883 Note that :js:attr:`Map.dir` is also available for compatibility.
2884
2885.. js:attribute:: Map._dom
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002886
2887 See the HAProxy configuration.txt file, chapter "Using ACLs and fetching
Ilya Shipitsin2075ca82020-03-06 23:22:22 +05002888 samples" and subchapter "ACL basics" to understand this pattern matching
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002889 method.
2890
Thierry FOURNIER4dc71972017-01-28 08:33:08 +01002891 Note that :js:attr:`Map.dom` is also available for compatibility.
2892
2893.. js:attribute:: Map._end
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002894
2895 See the HAProxy configuration.txt file, chapter "Using ACLs and fetching
Ilya Shipitsin2075ca82020-03-06 23:22:22 +05002896 samples" and subchapter "ACL basics" to understand this pattern matching
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002897 method.
2898
Thierry FOURNIER4dc71972017-01-28 08:33:08 +01002899.. js:attribute:: Map._reg
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002900
2901 See the HAProxy configuration.txt file, chapter "Using ACLs and fetching
Ilya Shipitsin2075ca82020-03-06 23:22:22 +05002902 samples" and subchapter "ACL basics" to understand this pattern matching
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002903 method.
2904
Thierry FOURNIER4dc71972017-01-28 08:33:08 +01002905 Note that :js:attr:`Map.reg` is also available for compatibility.
2906
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002907
2908.. js:function:: Map.new(file, method)
2909
2910 Creates and load a map.
2911
2912 :param string file: Is the file containing the map.
2913 :param integer method: Is the map pattern matching method. See the attributes
2914 of the Map class.
2915 :returns: a class Map object.
Thierry FOURNIER4dc71972017-01-28 08:33:08 +01002916 :see: The Map attributes: :js:attr:`Map._int`, :js:attr:`Map._ip`,
2917 :js:attr:`Map._str`, :js:attr:`Map._beg`, :js:attr:`Map._sub`,
2918 :js:attr:`Map._dir`, :js:attr:`Map._dom`, :js:attr:`Map._end` and
2919 :js:attr:`Map._reg`.
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002920
2921.. js:function:: Map.lookup(map, str)
2922
2923 Perform a lookup in a map.
2924
2925 :param class_map map: Is the class Map object.
2926 :param string str: Is the string used as key.
2927 :returns: a string containing the result or nil if no match.
2928
2929.. js:function:: Map.slookup(map, str)
2930
2931 Perform a lookup in a map.
2932
2933 :param class_map map: Is the class Map object.
2934 :param string str: Is the string used as key.
2935 :returns: a string containing the result or empty string if no match.
2936
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002937.. _applethttp_class:
2938
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002939AppletHTTP class
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002940================
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002941
2942.. js:class:: AppletHTTP
2943
2944 This class is used with applets that requires the 'http' mode. The http applet
2945 can be registered with the *core.register_service()* function. They are used
2946 for processing an http request like a server in back of HAProxy.
2947
2948 This is an hello world sample code:
2949
2950.. code-block:: lua
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002951
Pieter Baauw4d7f7662015-11-08 16:38:08 +01002952 core.register_service("hello-world", "http", function(applet)
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002953 local response = "Hello World !"
2954 applet:set_status(200)
Pieter Baauw2dcb9bc2015-10-01 22:47:12 +02002955 applet:add_header("content-length", string.len(response))
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002956 applet:add_header("content-type", "text/plain")
Pieter Baauw2dcb9bc2015-10-01 22:47:12 +02002957 applet:start_response()
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002958 applet:send(response)
2959 end)
2960
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002961.. js:attribute:: AppletHTTP.c
2962
2963 :returns: A :ref:`converters_class`
2964
2965 This attribute contains a Converters class object.
2966
2967.. js:attribute:: AppletHTTP.sc
2968
2969 :returns: A :ref:`converters_class`
2970
2971 This attribute contains a Converters class object. The
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02002972 functions of this object always return a string.
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002973
2974.. js:attribute:: AppletHTTP.f
2975
2976 :returns: A :ref:`fetches_class`
2977
2978 This attribute contains a Fetches class object. Note that the
2979 applet execution place cannot access to a valid HAProxy core HTTP
Ilya Shipitsin2075ca82020-03-06 23:22:22 +05002980 transaction, so some sample fetches related to the HTTP dependent
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002981 values (hdr, path, ...) are not available.
2982
2983.. js:attribute:: AppletHTTP.sf
2984
2985 :returns: A :ref:`fetches_class`
2986
2987 This attribute contains a Fetches class object. The functions of
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02002988 this object always return a string. Note that the applet
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002989 execution place cannot access to a valid HAProxy core HTTP
Ilya Shipitsin2075ca82020-03-06 23:22:22 +05002990 transaction, so some sample fetches related to the HTTP dependent
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002991 values (hdr, path, ...) are not available.
2992
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01002993.. js:attribute:: AppletHTTP.method
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002994
2995 :returns: string
2996
2997 The attribute method returns a string containing the HTTP
2998 method.
2999
3000.. js:attribute:: AppletHTTP.version
3001
3002 :returns: string
3003
3004 The attribute version, returns a string containing the HTTP
3005 request version.
3006
3007.. js:attribute:: AppletHTTP.path
3008
3009 :returns: string
3010
3011 The attribute path returns a string containing the HTTP
3012 request path.
3013
3014.. js:attribute:: AppletHTTP.qs
3015
3016 :returns: string
3017
3018 The attribute qs returns a string containing the HTTP
3019 request query string.
3020
3021.. js:attribute:: AppletHTTP.length
3022
3023 :returns: integer
3024
3025 The attribute length returns an integer containing the HTTP
3026 body length.
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02003027
Thierry FOURNIER841475e2015-12-11 17:10:09 +01003028.. js:attribute:: AppletHTTP.headers
3029
Patrick Hemmerc6a1d712018-05-01 21:30:41 -04003030 :returns: table
Thierry FOURNIERdc595002015-12-21 11:13:52 +01003031
Patrick Hemmerc6a1d712018-05-01 21:30:41 -04003032 The attribute headers returns a table containing the HTTP
Thierry FOURNIERdc595002015-12-21 11:13:52 +01003033 headers. The header names are always in lower case. As the header name can be
3034 encountered more than once in each request, the value is indexed with 0 as
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02003035 first index value. The table has this form:
Thierry FOURNIERdc595002015-12-21 11:13:52 +01003036
3037.. code-block:: lua
3038
3039 AppletHTTP.headers['<header-name>'][<header-index>] = "<header-value>"
3040
3041 AppletHTTP.headers["host"][0] = "www.test.com"
3042 AppletHTTP.headers["accept"][0] = "audio/basic q=1"
3043 AppletHTTP.headers["accept"][1] = "audio/*, q=0.2"
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01003044 AppletHTTP.headers["accept"][2] = "*/*, q=0.1"
Thierry FOURNIERdc595002015-12-21 11:13:52 +01003045..
3046
Robin H. Johnson52f5db22017-01-01 13:10:52 -08003047.. js:function:: AppletHTTP.set_status(applet, code [, reason])
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02003048
3049 This function sets the HTTP status code for the response. The allowed code are
3050 from 100 to 599.
3051
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01003052 :param class_AppletHTTP applet: An :ref:`applethttp_class`
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02003053 :param integer code: the status code returned to the client.
Robin H. Johnson52f5db22017-01-01 13:10:52 -08003054 :param string reason: the status reason returned to the client (optional).
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02003055
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01003056.. js:function:: AppletHTTP.add_header(applet, name, value)
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02003057
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02003058 This function adds a header in the response. Duplicated headers are not
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02003059 collapsed. The special header *content-length* is used to determinate the
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02003060 response length. If it does not exist, a *transfer-encoding: chunked* is set, and
Ilya Shipitsin2075ca82020-03-06 23:22:22 +05003061 all the write from the function *AppletHTTP:send()* become a chunk.
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02003062
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01003063 :param class_AppletHTTP applet: An :ref:`applethttp_class`
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02003064 :param string name: the header name
3065 :param string value: the header value
3066
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01003067.. js:function:: AppletHTTP.start_response(applet)
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02003068
3069 This function indicates to the HTTP engine that it can process and send the
3070 response headers. After this called we cannot add headers to the response; We
3071 cannot use the *AppletHTTP:send()* function if the
3072 *AppletHTTP:start_response()* is not called.
3073
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01003074 :param class_AppletHTTP applet: An :ref:`applethttp_class`
3075
3076.. js:function:: AppletHTTP.getline(applet)
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02003077
3078 This function returns a string containing one line from the http body. If the
3079 data returned doesn't contains a final '\\n' its assumed than its the last
3080 available data before the end of stream.
3081
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01003082 :param class_AppletHTTP applet: An :ref:`applethttp_class`
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02003083 :returns: a string. The string can be empty if we reach the end of the stream.
3084
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01003085.. js:function:: AppletHTTP.receive(applet, [size])
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02003086
3087 Reads data from the HTTP body, according to the specified read *size*. If the
3088 *size* is missing, the function tries to read all the content of the stream
3089 until the end. If the *size* is bigger than the http body, it returns the
Bertrand Jacquin874a35c2018-09-10 21:26:07 +01003090 amount of data available.
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02003091
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01003092 :param class_AppletHTTP applet: An :ref:`applethttp_class`
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02003093 :param integer size: the required read size.
Ilya Shipitsin11057a32020-06-21 21:18:27 +05003094 :returns: always return a string,the string can be empty is the connection is
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02003095 closed.
3096
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01003097.. js:function:: AppletHTTP.send(applet, msg)
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02003098
3099 Send the message *msg* on the http request body.
3100
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01003101 :param class_AppletHTTP applet: An :ref:`applethttp_class`
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02003102 :param string msg: the message to send.
3103
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01003104.. js:function:: AppletHTTP.get_priv(applet)
3105
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01003106 Return Lua data stored in the current transaction. If no data are stored,
3107 it returns a nil value.
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01003108
3109 :param class_AppletHTTP applet: An :ref:`applethttp_class`
Bertrand Jacquin874a35c2018-09-10 21:26:07 +01003110 :returns: the opaque data previously stored, or nil if nothing is
3111 available.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01003112 :see: :js:func:`AppletHTTP.set_priv`
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01003113
3114.. js:function:: AppletHTTP.set_priv(applet, data)
3115
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02003116 Store any data in the current HAProxy transaction. This action replaces the
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01003117 old stored data.
3118
3119 :param class_AppletHTTP applet: An :ref:`applethttp_class`
3120 :param opaque data: The data which is stored in the transaction.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01003121 :see: :js:func:`AppletHTTP.get_priv`
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01003122
Tim Duesterhus4e172c92020-05-19 13:49:42 +02003123.. js:function:: AppletHTTP.set_var(applet, var, value[, ifexist])
Thierry FOURNIER / OZON.IOc1edafe2016-12-12 16:25:30 +01003124
3125 Converts a Lua type in a HAProxy type and store it in a variable <var>.
3126
3127 :param class_AppletHTTP applet: An :ref:`applethttp_class`
3128 :param string var: The variable name according with the HAProxy variable syntax.
3129 :param type value: The value associated to the variable. The type ca be string or
3130 integer.
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02003131 :param boolean ifexist: If this parameter is set to true the variable
Tim Duesterhus4e172c92020-05-19 13:49:42 +02003132 will only be set if it was defined elsewhere (i.e. used
Willy Tarreau7978c5c2021-09-07 14:24:07 +02003133 within the configuration). For global variables (using the
3134 "proc" scope), they will only be updated and never created.
Aurelien DARRAGON21f7ebb2023-03-13 19:49:31 +01003135 It is highly recommended to always set this to true.
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02003136
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01003137 :see: :js:func:`AppletHTTP.unset_var`
3138 :see: :js:func:`AppletHTTP.get_var`
Thierry FOURNIER / OZON.IOc1edafe2016-12-12 16:25:30 +01003139
3140.. js:function:: AppletHTTP.unset_var(applet, var)
3141
3142 Unset the variable <var>.
3143
3144 :param class_AppletHTTP applet: An :ref:`applethttp_class`
3145 :param string var: The variable name according with the HAProxy variable syntax.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01003146 :see: :js:func:`AppletHTTP.set_var`
3147 :see: :js:func:`AppletHTTP.get_var`
Thierry FOURNIER / OZON.IOc1edafe2016-12-12 16:25:30 +01003148
3149.. js:function:: AppletHTTP.get_var(applet, var)
3150
3151 Returns data stored in the variable <var> converter in Lua type.
3152
3153 :param class_AppletHTTP applet: An :ref:`applethttp_class`
3154 :param string var: The variable name according with the HAProxy variable syntax.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01003155 :see: :js:func:`AppletHTTP.set_var`
3156 :see: :js:func:`AppletHTTP.unset_var`
Thierry FOURNIER / OZON.IOc1edafe2016-12-12 16:25:30 +01003157
Thierry FOURNIERdc595002015-12-21 11:13:52 +01003158.. _applettcp_class:
3159
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02003160AppletTCP class
3161===============
3162
3163.. js:class:: AppletTCP
3164
3165 This class is used with applets that requires the 'tcp' mode. The tcp applet
3166 can be registered with the *core.register_service()* function. They are used
3167 for processing a tcp stream like a server in back of HAProxy.
3168
Thierry FOURNIERdc595002015-12-21 11:13:52 +01003169.. js:attribute:: AppletTCP.c
3170
3171 :returns: A :ref:`converters_class`
3172
3173 This attribute contains a Converters class object.
3174
3175.. js:attribute:: AppletTCP.sc
3176
3177 :returns: A :ref:`converters_class`
3178
3179 This attribute contains a Converters class object. The
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02003180 functions of this object always return a string.
Thierry FOURNIERdc595002015-12-21 11:13:52 +01003181
3182.. js:attribute:: AppletTCP.f
3183
3184 :returns: A :ref:`fetches_class`
3185
3186 This attribute contains a Fetches class object.
3187
3188.. js:attribute:: AppletTCP.sf
3189
3190 :returns: A :ref:`fetches_class`
3191
3192 This attribute contains a Fetches class object.
3193
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01003194.. js:function:: AppletTCP.getline(applet)
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02003195
3196 This function returns a string containing one line from the stream. If the
3197 data returned doesn't contains a final '\\n' its assumed than its the last
3198 available data before the end of stream.
3199
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01003200 :param class_AppletTCP applet: An :ref:`applettcp_class`
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02003201 :returns: a string. The string can be empty if we reach the end of the stream.
3202
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01003203.. js:function:: AppletTCP.receive(applet, [size])
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02003204
3205 Reads data from the TCP stream, according to the specified read *size*. If the
3206 *size* is missing, the function tries to read all the content of the stream
3207 until the end.
3208
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01003209 :param class_AppletTCP applet: An :ref:`applettcp_class`
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02003210 :param integer size: the required read size.
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02003211 :returns: always return a string, the string can be empty if the connection is
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02003212 closed.
3213
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01003214.. js:function:: AppletTCP.send(appletmsg)
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02003215
3216 Send the message on the stream.
3217
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01003218 :param class_AppletTCP applet: An :ref:`applettcp_class`
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02003219 :param string msg: the message to send.
3220
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01003221.. js:function:: AppletTCP.get_priv(applet)
3222
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01003223 Return Lua data stored in the current transaction. If no data are stored,
3224 it returns a nil value.
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01003225
3226 :param class_AppletTCP applet: An :ref:`applettcp_class`
Bertrand Jacquin874a35c2018-09-10 21:26:07 +01003227 :returns: the opaque data previously stored, or nil if nothing is
3228 available.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01003229 :see: :js:func:`AppletTCP.set_priv`
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01003230
3231.. js:function:: AppletTCP.set_priv(applet, data)
3232
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02003233 Store any data in the current HAProxy transaction. This action replaces the
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01003234 old stored data.
3235
3236 :param class_AppletTCP applet: An :ref:`applettcp_class`
3237 :param opaque data: The data which is stored in the transaction.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01003238 :see: :js:func:`AppletTCP.get_priv`
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01003239
Tim Duesterhus4e172c92020-05-19 13:49:42 +02003240.. js:function:: AppletTCP.set_var(applet, var, value[, ifexist])
Thierry FOURNIER / OZON.IOc1edafe2016-12-12 16:25:30 +01003241
3242 Converts a Lua type in a HAProxy type and stores it in a variable <var>.
3243
3244 :param class_AppletTCP applet: An :ref:`applettcp_class`
3245 :param string var: The variable name according with the HAProxy variable syntax.
3246 :param type value: The value associated to the variable. The type can be string or
3247 integer.
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02003248 :param boolean ifexist: If this parameter is set to true the variable
Tim Duesterhus4e172c92020-05-19 13:49:42 +02003249 will only be set if it was defined elsewhere (i.e. used
Willy Tarreau7978c5c2021-09-07 14:24:07 +02003250 within the configuration). For global variables (using the
3251 "proc" scope), they will only be updated and never created.
Aurelien DARRAGON21f7ebb2023-03-13 19:49:31 +01003252 It is highly recommended to always set this to true.
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02003253
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01003254 :see: :js:func:`AppletTCP.unset_var`
3255 :see: :js:func:`AppletTCP.get_var`
Thierry FOURNIER / OZON.IOc1edafe2016-12-12 16:25:30 +01003256
3257.. js:function:: AppletTCP.unset_var(applet, var)
3258
3259 Unsets the variable <var>.
3260
3261 :param class_AppletTCP applet: An :ref:`applettcp_class`
3262 :param string var: The variable name according with the HAProxy variable syntax.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01003263 :see: :js:func:`AppletTCP.unset_var`
3264 :see: :js:func:`AppletTCP.set_var`
Thierry FOURNIER / OZON.IOc1edafe2016-12-12 16:25:30 +01003265
3266.. js:function:: AppletTCP.get_var(applet, var)
3267
3268 Returns data stored in the variable <var> converter in Lua type.
3269
3270 :param class_AppletTCP applet: An :ref:`applettcp_class`
3271 :param string var: The variable name according with the HAProxy variable syntax.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01003272 :see: :js:func:`AppletTCP.unset_var`
3273 :see: :js:func:`AppletTCP.set_var`
Thierry FOURNIER / OZON.IOc1edafe2016-12-12 16:25:30 +01003274
Adis Nezirovic8878f8e2018-07-13 12:18:33 +02003275StickTable class
3276================
3277
3278.. js:class:: StickTable
3279
3280 **context**: task, action, sample-fetch
3281
3282 This class can be used to access the HAProxy stick tables from Lua.
3283
3284.. js:function:: StickTable.info()
3285
3286 Returns stick table attributes as a Lua table. See HAProxy documentation for
Ilya Shipitsin2272d8a2020-12-21 01:22:40 +05003287 "stick-table" for canonical info, or check out example below.
Adis Nezirovic8878f8e2018-07-13 12:18:33 +02003288
3289 :returns: Lua table
3290
3291 Assume our table has IPv4 key and gpc0 and conn_rate "columns":
3292
3293.. code-block:: lua
3294
3295 {
3296 expire=<int>, # Value in ms
3297 size=<int>, # Maximum table size
3298 used=<int>, # Actual number of entries in table
3299 data={ # Data columns, with types as key, and periods as values
3300 (-1 if type is not rate counter)
3301 conn_rate=<int>,
3302 gpc0=-1
3303 },
3304 length=<int>, # max string length for string table keys, key length
3305 # otherwise
3306 nopurge=<boolean>, # purge oldest entries when table is full
3307 type="ip" # can be "ip", "ipv6", "integer", "string", "binary"
3308 }
3309
3310.. js:function:: StickTable.lookup(key)
3311
3312 Returns stick table entry for given <key>
3313
3314 :param string key: Stick table key (IP addresses and strings are supported)
3315 :returns: Lua table
3316
3317.. js:function:: StickTable.dump([filter])
3318
3319 Returns all entries in stick table. An optional filter can be used
3320 to extract entries with specific data values. Filter is a table with valid
3321 comparison operators as keys followed by data type name and value pairs.
3322 Check out the HAProxy docs for "show table" for more details. For the
3323 reference, the supported operators are:
Aurelien DARRAGON21f7ebb2023-03-13 19:49:31 +01003324
Adis Nezirovic8878f8e2018-07-13 12:18:33 +02003325 "eq", "ne", "le", "lt", "ge", "gt"
3326
3327 For large tables, execution of this function can take a long time (for
3328 HAProxy standards). That's also true when filter is used, so take care and
3329 measure the impact.
3330
3331 :param table filter: Stick table filter
3332 :returns: Stick table entries (table)
3333
3334 See below for example filter, which contains 4 entries (or comparisons).
3335 (Maximum number of filter entries is 4, defined in the source code)
3336
3337.. code-block:: lua
3338
3339 local filter = {
3340 {"gpc0", "gt", 30}, {"gpc1", "gt", 20}}, {"conn_rate", "le", 10}
3341 }
3342
Christopher Faulet0f3c8902020-01-31 18:57:12 +01003343.. _action_class:
3344
3345Action class
3346=============
3347
3348.. js:class:: Act
3349
3350 **context**: action
3351
3352 This class contains all return codes an action may return. It is the lua
3353 equivalent to HAProxy "ACT_RET_*" code.
3354
3355.. code-block:: lua
3356
3357 core.register_action("deny", { "http-req" }, function (txn)
3358 return act.DENY
3359 end)
3360..
3361.. js:attribute:: act.CONTINUE
3362
3363 This attribute is an integer (0). It instructs HAProxy to continue the current
3364 ruleset processing on the message. It is the default return code for a lua
3365 action.
3366
3367 :returns: integer
3368
3369.. js:attribute:: act.STOP
3370
3371 This attribute is an integer (1). It instructs HAProxy to stop the current
3372 ruleset processing on the message.
3373
3374.. js:attribute:: act.YIELD
3375
3376 This attribute is an integer (2). It instructs HAProxy to temporarily pause
3377 the message processing. It will be resumed later on the same rule. The
3378 corresponding lua script is re-executed for the start.
3379
3380.. js:attribute:: act.ERROR
3381
3382 This attribute is an integer (3). It triggers an internal errors The message
3383 processing is stopped and the transaction is terminated. For HTTP streams, an
3384 HTTP 500 error is returned to the client.
3385
3386 :returns: integer
3387
3388.. js:attribute:: act.DONE
3389
3390 This attribute is an integer (4). It instructs HAProxy to stop the message
3391 processing.
3392
3393 :returns: integer
3394
3395.. js:attribute:: act.DENY
3396
3397 This attribute is an integer (5). It denies the current message. The message
3398 processing is stopped and the transaction is terminated. For HTTP streams, an
3399 HTTP 403 error is returned to the client if the deny is returned during the
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02003400 request analysis. During the response analysis, a HTTP 502 error is returned
Christopher Faulet0f3c8902020-01-31 18:57:12 +01003401 and the server response is discarded.
3402
3403 :returns: integer
3404
3405.. js:attribute:: act.ABORT
3406
3407 This attribute is an integer (6). It aborts the current message. The message
3408 processing is stopped and the transaction is terminated. For HTTP streams,
Willy Tarreau714f3452021-05-09 06:47:26 +02003409 HAProxy assumes a response was already sent to the client. From the Lua
Christopher Faulet0f3c8902020-01-31 18:57:12 +01003410 actions point of view, when this code is used, the transaction is terminated
3411 with no reply.
3412
3413 :returns: integer
3414
3415.. js:attribute:: act.INVALID
3416
3417 This attribute is an integer (7). It triggers an internal errors. The message
3418 processing is stopped and the transaction is terminated. For HTTP streams, an
3419 HTTP 400 error is returned to the client if the error is returned during the
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02003420 request analysis. During the response analysis, a HTTP 502 error is returned
Christopher Faulet0f3c8902020-01-31 18:57:12 +01003421 and the server response is discarded.
3422
3423 :returns: integer
Adis Nezirovic8878f8e2018-07-13 12:18:33 +02003424
Christopher Faulet2c2c2e32020-01-31 19:07:52 +01003425.. js:function:: act:wake_time(milliseconds)
3426
3427 **context**: action
3428
3429 Set the script pause timeout to the specified time, defined in
3430 milliseconds.
3431
3432 :param integer milliseconds: the required milliseconds.
3433
3434 This function may be used when a lua action returns `act.YIELD`, to force its
3435 wake-up at most after the specified number of milliseconds.
3436
Christopher Faulet5a2c6612021-08-15 20:35:25 +02003437.. _filter_class:
3438
3439Filter class
3440=============
3441
3442.. js:class:: filter
3443
3444 **context**: filter
3445
3446 This class contains return codes some filter callback functions may return. It
3447 also contains configuration flags and some helper functions. To understand how
3448 the filter API works, see `doc/internal/filters.txt` documentation.
3449
3450.. js:attribute:: filter.CONTINUE
3451
3452 This attribute is an integer (1). It may be returned by some filter callback
3453 functions to instruct this filtering step is finished for this filter.
3454
3455.. js:attribute:: filter.WAIT
3456
3457 This attribute is an integer (0). It may be returned by some filter callback
3458 functions to instruct the filtering must be paused, waiting for more data or
3459 for an external event depending on this filter.
3460
3461.. js:attribute:: filter.ERROR
3462
3463 This attribute is an integer (-1). It may be returned by some filter callback
3464 functions to trigger an error.
3465
3466.. js:attribute:: filter.FLT_CFG_FL_HTX
3467
3468 This attribute is a flag corresponding to the filter flag FLT_CFG_FL_HTX. When
3469 it is set for a filter, it means the filter is able to filter HTTP streams.
3470
3471.. js:function:: filter.register_data_filter(chn)
3472
3473 **context**: filter
3474
3475 Enable the data filtering on the channel **chn** for the current filter. It
Ilya Shipitsinff0f2782021-08-22 22:18:07 +05003476 may be called at any time from any callback functions proceeding the data
Christopher Faulet5a2c6612021-08-15 20:35:25 +02003477 analysis.
3478
3479 :param class_Channel chn: A :ref:`channel_class`.
3480
3481.. js:function:: filter.unregister_data_filter(chn)
3482
3483 **context**: filter
3484
3485 Disable the data filtering on the channel **chn** for the current filter. It
3486 may be called at any time from any callback functions.
3487
3488 :param class_Channel chn: A :ref:`channel_class`.
3489
3490.. js:function:: filter.wake_time(milliseconds)
3491
3492 **context**: filter
3493
3494 Set the script pause timeout to the specified time, defined in
3495 milliseconds.
3496
3497 :param integer milliseconds: the required milliseconds.
3498
3499 This function may be used from any lua filter callback function to force its
3500 wake-up at most after the specified number of milliseconds. Especially, when
3501 `filter.CONTINUE` is returned.
3502
3503
3504A filters is declared using :js:func:`core.register_filter()` function. The
3505provided class will be used to instantiate filters. It may define following
3506attributes:
3507
3508* id: The filter identifier. It is a string that identifies the filter and is
3509 optional.
3510
3511* flags: The filter flags. Only :js:attr:`filter.FLT_CFG_FL_HTX` may be set for now.
3512
3513Such filter class must also define all required callback functions in the
3514following list. Note that :js:func:`Filter.new()` must be defined otherwise the
Ilya Shipitsinff0f2782021-08-22 22:18:07 +05003515filter is ignored. Others are optional.
Christopher Faulet5a2c6612021-08-15 20:35:25 +02003516
3517* .. js:function:: FILTER.new()
3518
3519 Called to instantiate a new filter. This function must be defined.
3520
3521 :returns: a Lua object that will be used as filter instance for the current
3522 stream.
3523
3524* .. js:function:: FILTER.start_analyze(flt, txn, chn)
3525
3526 Called when the analysis starts on the channel **chn**.
3527
3528* .. js:function:: FILTER.end_analyze(flt, txn, chn)
3529
3530 Called when the analysis ends on the channel **chn**.
3531
3532* .. js:function:: FILTER.http_headers(flt, txn, http_msg)
3533
3534 Called just before the HTTP payload analysis and after any processing on the
3535 HTTP message **http_msg**. This callback functions is only called for HTTP
3536 streams.
3537
3538* .. js:function:: FILTER.http_payload(flt, txn, http_msg)
3539
3540 Called during the HTTP payload analysis on the HTTP message **http_msg**. This
3541 callback functions is only called for HTTP streams.
3542
3543* .. js:function:: FILTER.http_end(flt, txn, http_msg)
3544
3545 Called after the HTTP payload analysis on the HTTP message **http_msg**. This
3546 callback functions is only called for HTTP streams.
3547
3548* .. js:function:: FILTER.tcp_payload(flt, txn, chn)
3549
3550 Called during the TCP payload analysis on the channel **chn**.
3551
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02003552Here is a full example:
Christopher Faulet5a2c6612021-08-15 20:35:25 +02003553
3554.. code-block:: lua
3555
3556 Trace = {}
3557 Trace.id = "Lua trace filter"
3558 Trace.flags = filter.FLT_CFG_FL_HTX;
3559 Trace.__index = Trace
3560
3561 function Trace:new()
3562 local trace = {}
3563 setmetatable(trace, Trace)
3564 trace.req_len = 0
3565 trace.res_len = 0
3566 return trace
3567 end
3568
3569 function Trace:start_analyze(txn, chn)
3570 if chn:is_resp() then
3571 print("Start response analysis")
3572 else
3573 print("Start request analysis")
3574 end
3575 filter.register_data_filter(self, chn)
3576 end
3577
3578 function Trace:end_analyze(txn, chn)
3579 if chn:is_resp() then
3580 print("End response analysis: "..self.res_len.." bytes filtered")
3581 else
3582 print("End request analysis: "..self.req_len.." bytes filtered")
3583 end
3584 end
3585
3586 function Trace:http_headers(txn, http_msg)
3587 stline = http_msg:get_stline()
3588 if http_msg.channel:is_resp() then
3589 print("response:")
3590 print(stline.version.." "..stline.code.." "..stline.reason)
3591 else
3592 print("request:")
3593 print(stline.method.." "..stline.uri.." "..stline.version)
3594 end
3595
3596 for n, hdrs in pairs(http_msg:get_headers()) do
3597 for i,v in pairs(hdrs) do
3598 print(n..": "..v)
3599 end
3600 end
3601 return filter.CONTINUE
3602 end
3603
3604 function Trace:http_payload(txn, http_msg)
3605 body = http_msg:body(-20000)
3606 if http_msg.channel:is_resp() then
3607 self.res_len = self.res_len + body:len()
3608 else
3609 self.req_len = self.req_len + body:len()
3610 end
3611 end
3612
3613 core.register_filter("trace", Trace, function(trace, args)
3614 return trace
3615 end)
3616
3617..
3618
3619.. _httpmessage_class:
3620
3621HTTPMessage class
3622===================
3623
3624.. js:class:: HTTPMessage
3625
3626 **context**: filter
3627
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02003628 This class contains all functions to manipulate a HTTP message. For now, this
Christopher Faulet5a2c6612021-08-15 20:35:25 +02003629 class is only available from a filter context.
3630
3631.. js:function:: HTTPMessage.add_header(http_msg, name, value)
3632
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02003633 Appends a HTTP header field in the HTTP message **http_msg** whose name is
Christopher Faulet5a2c6612021-08-15 20:35:25 +02003634 specified in **name** and whose value is defined in **value**.
3635
3636 :param class_httpmessage http_msg: The manipulated HTTP message.
3637 :param string name: The header name.
3638 :param string value: The header value.
3639
3640.. js:function:: HTTPMessage.append(http_msg, string)
3641
3642 This function copies the string **string** at the end of incoming data of the
3643 HTTP message **http_msg**. The function returns the copied length on success
3644 or -1 if data cannot be copied.
3645
3646 Same that :js:func:`HTTPMessage.insert(http_msg, string, http_msg:input())`.
3647
3648 :param class_httpmessage http_msg: The manipulated HTTP message.
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02003649 :param string string: The data to copy at the end of incoming data.
Christopher Faulet5a2c6612021-08-15 20:35:25 +02003650 :returns: an integer containing the amount of bytes copied or -1.
3651
3652.. js:function:: HTTPMessage.body(http_msgl[, offset[, length]])
3653
3654 This function returns **length** bytes of incoming data from the HTTP message
3655 **http_msg**, starting at the offset **offset**. The data are not removed from
3656 the buffer.
3657
3658 By default, if no length is provided, all incoming data found, starting at the
3659 given offset, are returned. If **length** is set to -1, the function tries to
3660 retrieve a maximum of data. Because it is called in the filter context, it
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02003661 never yield. Not providing an offset is the same as setting it to 0. A
Ilya Shipitsinff0f2782021-08-22 22:18:07 +05003662 positive offset is relative to the beginning of incoming data of the
Christopher Faulet5a2c6612021-08-15 20:35:25 +02003663 http_message buffer while negative offset is relative to their end.
3664
3665 If there is no incoming data and the HTTP message can't receive more data, a 'nil'
3666 value is returned.
3667
3668 :param class_httpmessage http_msg: The manipulated HTTP message.
3669 :param integer offset: *optional* The offset in incoming data to start to get
3670 data. 0 by default. May be negative to be relative to
3671 the end of incoming data.
3672 :param integer length: *optional* The expected length of data to retrieve. All
3673 incoming data by default. May be set to -1 to get a
3674 maximum of data.
3675 :returns: a string containing the data found or nil.
3676
3677.. js:function:: HTTPMessage.eom(http_msg)
3678
3679 This function returns true if the end of message is reached for the HTTP
3680 message **http_msg**.
3681
3682 :param class_httpmessage http_msg: The manipulated HTTP message.
3683 :returns: an integer containing the amount of available bytes.
3684
3685.. js:function:: HTTPMessage.del_header(http_msg, name)
3686
3687 Removes all HTTP header fields in the HTTP message **http_msg** whose name is
3688 specified in **name**.
3689
3690 :param class_httpmessage http_msg: The manipulated http message.
3691 :param string name: The header name.
3692
3693.. js:function:: HTTPMessage.get_headers(http_msg)
3694
3695 Returns a table containing all the headers of the HTTP message **http_msg**.
3696
3697 :param class_httpmessage http_msg: The manipulated http message.
3698 :returns: table of headers.
3699
3700 This is the form of the returned table:
3701
3702.. code-block:: lua
3703
3704 http_msg:get_headers()['<header-name>'][<header-index>] = "<header-value>"
3705
3706 local hdr = http_msg:get_headers()
3707 hdr["host"][0] = "www.test.com"
3708 hdr["accept"][0] = "audio/basic q=1"
3709 hdr["accept"][1] = "audio/*, q=0.2"
3710 hdr["accept"][2] = "*.*, q=0.1"
3711..
3712
3713.. js:function:: HTTPMessage.get_stline(http_msg)
3714
3715 Returns a table containing the start-line of the HTTP message **http_msg**.
3716
3717 :param class_httpmessage http_msg: The manipulated http message.
3718 :returns: the start-line.
3719
3720 This is the form of the returned table:
3721
3722.. code-block:: lua
3723
3724 -- for the request :
3725 {"method" = string, "uri" = string, "version" = string}
3726
3727 -- for the response:
3728 {"version" = string, "code" = string, "reason" = string}
3729..
3730
3731.. js:function:: HTTPMessage.forward(http_msg, length)
3732
3733 This function forwards **length** bytes of data from the HTTP message
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02003734 **http_msg**. Because it is called in the filter context, it never yields. Only
Christopher Faulet5a2c6612021-08-15 20:35:25 +02003735 available incoming data may be forwarded, event if the requested length
3736 exceeds the available amount of incoming data. It returns the amount of data
3737 forwarded.
3738
3739 :param class_httpmessage http_msg: The manipulated HTTP message.
3740 :param integer int: The amount of data to forward.
3741
3742.. js:function:: HTTPMessage.input(http_msg)
3743
3744 This function returns the length of incoming data in the HTTP message
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02003745 **http_msg** from the filter point of view.
Christopher Faulet5a2c6612021-08-15 20:35:25 +02003746
3747 :param class_httpmessage http_msg: The manipulated HTTP message.
3748 :returns: an integer containing the amount of available bytes.
3749
3750.. js:function:: HTTPMessage.insert(http_msg, string[, offset])
3751
3752 This function copies the string **string** at the offset **offset** in
3753 incoming data of the HTTP message **http_msg**. The function returns the
3754 copied length on success or -1 if data cannot be copied.
3755
3756 By default, if no offset is provided, the string is copied in front of
Ilya Shipitsinff0f2782021-08-22 22:18:07 +05003757 incoming data. A positive offset is relative to the beginning of incoming data
Christopher Faulet5a2c6612021-08-15 20:35:25 +02003758 of the HTTP message while negative offset is relative to their end.
3759
3760 :param class_httpmessage http_msg: The manipulated HTTP message.
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02003761 :param string string: The data to copy into incoming data.
3762 :param integer offset: *optional* The offset in incoming data where to copy
Christopher Faulet5a2c6612021-08-15 20:35:25 +02003763 data. 0 by default. May be negative to be relative to
3764 the end of incoming data.
3765 :returns: an integer containing the amount of bytes copied or -1.
3766
3767.. js:function:: HTTPMessage.is_full(http_msg)
3768
3769 This function returns true if the HTTP message **http_msg** is full.
3770
3771 :param class_httpmessage http_msg: The manipulated HTTP message.
3772 :returns: a boolean
3773
3774.. js:function:: HTTPMessage.is_resp(http_msg)
3775
3776 This function returns true if the HTTP message **http_msg** is the response
3777 one.
3778
3779 :param class_httpmessage http_msg: The manipulated HTTP message.
3780 :returns: a boolean
3781
3782.. js:function:: HTTPMessage.may_recv(http_msg)
3783
3784 This function returns true if the HTTP message **http_msg** may still receive
3785 data.
3786
3787 :param class_httpmessage http_msg: The manipulated HTTP message.
3788 :returns: a boolean
3789
3790.. js:function:: HTTPMessage.output(http_msg)
3791
3792 This function returns the length of outgoing data of the HTTP message
3793 **http_msg**.
3794
3795 :param class_httpmessage http_msg: The manipulated HTTP message.
3796 :returns: an integer containing the amount of available bytes.
3797
3798.. js:function:: HTTPMessage.prepend(http_msg, string)
3799
3800 This function copies the string **string** in front of incoming data of the
3801 HTTP message **http_msg**. The function returns the copied length on success
3802 or -1 if data cannot be copied.
3803
3804 Same that :js:func:`HTTPMessage.insert(http_msg, string, 0)`.
3805
3806 :param class_httpmessage http_msg: The manipulated HTTP message.
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02003807 :param string string: The data to copy in front of incoming data.
Christopher Faulet5a2c6612021-08-15 20:35:25 +02003808 :returns: an integer containing the amount of bytes copied or -1.
3809
3810.. js:function:: HTTPMessage.remove(http_msg[, offset[, length]])
3811
3812 This function removes **length** bytes of incoming data of the HTTP message
3813 **http_msg**, starting at offset **offset**. This function returns number of
3814 bytes removed on success.
3815
3816 By default, if no length is provided, all incoming data, starting at the given
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02003817 offset, are removed. Not providing an offset is the same that setting it
Ilya Shipitsinff0f2782021-08-22 22:18:07 +05003818 to 0. A positive offset is relative to the beginning of incoming data of the
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02003819 HTTP message while negative offset is relative to the end.
Christopher Faulet5a2c6612021-08-15 20:35:25 +02003820
3821 :param class_httpmessage http_msg: The manipulated HTTP message.
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02003822 :param integer offset: *optional* The offset in incoming data where to start
Christopher Faulet5a2c6612021-08-15 20:35:25 +02003823 to remove data. 0 by default. May be negative to
3824 be relative to the end of incoming data.
3825 :param integer length: *optional* The length of data to remove. All incoming
3826 data by default.
3827 :returns: an integer containing the amount of bytes removed.
3828
3829.. js:function:: HTTPMessage.rep_header(http_msg, name, regex, replace)
3830
3831 Matches the regular expression in all occurrences of header field **name**
3832 according to regex **regex**, and replaces them with the string **replace**.
3833 The replacement value can contain back references like \1, \2, ... This
3834 function acts on whole header lines, regardless of the number of values they
3835 may contain.
3836
3837 :param class_httpmessage http_msg: The manipulated HTTP message.
3838 :param string name: The header name.
3839 :param string regex: The match regular expression.
3840 :param string replace: The replacement value.
3841
3842.. js:function:: HTTPMessage.rep_value(http_msg, name, regex, replace)
3843
3844 Matches the regular expression on every comma-delimited value of header field
3845 **name** according to regex **regex**, and replaces them with the string
3846 **replace**. The replacement value can contain back references like \1, \2,
3847 ...
3848
3849 :param class_httpmessage http_msg: The manipulated HTTP message.
3850 :param string name: The header name.
3851 :param string regex: The match regular expression.
3852 :param string replace: The replacement value.
3853
3854.. js:function:: HTTPMessage.send(http_msg, string)
3855
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02003856 This function requires immediate send of the string **string**. It means the
Ilya Shipitsinff0f2782021-08-22 22:18:07 +05003857 string is copied at the beginning of incoming data of the HTTP message
Christopher Faulet5a2c6612021-08-15 20:35:25 +02003858 **http_msg** and immediately forwarded. Because it is called in the filter
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02003859 context, it never yields.
Christopher Faulet5a2c6612021-08-15 20:35:25 +02003860
3861 :param class_httpmessage http_msg: The manipulated HTTP message.
3862 :param string string: The data to send.
3863 :returns: an integer containing the amount of bytes copied or -1.
3864
3865.. js:function:: HTTPMessage.set(http_msg, string[, offset[, length]])
3866
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02003867 This function replaces **length** bytes of incoming data of the HTTP message
Christopher Faulet5a2c6612021-08-15 20:35:25 +02003868 **http_msg**, starting at offset **offset**, by the string **string**. The
3869 function returns the copied length on success or -1 if data cannot be copied.
3870
3871 By default, if no length is provided, all incoming data, starting at the given
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02003872 offset, are replaced. Not providing an offset is the same as setting it
Ilya Shipitsinff0f2782021-08-22 22:18:07 +05003873 to 0. A positive offset is relative to the beginning of incoming data of the
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02003874 HTTP message while negative offset is relative to the end.
Christopher Faulet5a2c6612021-08-15 20:35:25 +02003875
3876 :param class_httpmessage http_msg: The manipulated HTTP message.
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02003877 :param string string: The data to copy into incoming data.
3878 :param integer offset: *optional* The offset in incoming data where to start
Christopher Faulet5a2c6612021-08-15 20:35:25 +02003879 the data replacement. 0 by default. May be negative to
3880 be relative to the end of incoming data.
3881 :param integer length: *optional* The length of data to replace. All incoming
3882 data by default.
3883 :returns: an integer containing the amount of bytes copied or -1.
3884
3885.. js:function:: HTTPMessage.set_eom(http_msg)
3886
3887 This function set the end of message for the HTTP message **http_msg**.
3888
3889 :param class_httpmessage http_msg: The manipulated HTTP message.
3890
3891.. js:function:: HTTPMessage.set_header(http_msg, name, value)
3892
3893 This variable replace all occurrence of all header matching the name **name**,
3894 by only one containing the value **value**.
3895
3896 :param class_httpmessage http_msg: The manipulated HTTP message.
3897 :param string name: The header name.
3898 :param string value: The header value.
3899
3900 This function does the same work as the following code:
3901
3902.. code-block:: lua
3903
3904 http_msg:del_header("header")
3905 http_msg:add_header("header", "value")
3906..
3907
3908.. js:function:: HTTPMessage.set_method(http_msg, method)
3909
3910 Rewrites the request method with the string **method**. The HTTP message
3911 **http_msg** must be the request.
3912
3913 :param class_httpmessage http_msg: The manipulated HTTP message.
3914 :param string method: The new method.
3915
3916.. js:function:: HTTPMessage.set_path(http_msg, path)
3917
3918 Rewrites the request path with the string **path**. The HTTP message
3919 **http_msg** must be the request.
3920
3921 :param class_httpmessage http_msg: The manipulated HTTP message.
3922 :param string method: The new method.
3923
3924.. js:function:: HTTPMessage.set_query(http_msg, query)
3925
3926 Rewrites the request's query string which appears after the first question
3927 mark ("?") with the string **query**. The HTTP message **http_msg** must be
3928 the request.
3929
3930 :param class_httpmessage http_msg: The manipulated HTTP message.
3931 :param string query: The new query.
3932
3933.. js:function:: HTTPMessage.set_status(http_msg, status[, reason])
3934
Ilya Shipitsinff0f2782021-08-22 22:18:07 +05003935 Rewrites the response status code with the integer **code** and optional the
Christopher Faulet5a2c6612021-08-15 20:35:25 +02003936 reason **reason**. If no custom reason is provided, it will be generated from
3937 the status. The HTTP message **http_msg** must be the response.
3938
3939 :param class_httpmessage http_msg: The manipulated HTTP message.
3940 :param integer status: The new response status code.
3941 :param string reason: The new response reason (optional).
3942
3943.. js:function:: HTTPMessage.set_uri(http_msg, uri)
3944
3945 Rewrites the request URI with the string **uri**. The HTTP message
3946 **http_msg** must be the request.
3947
3948 :param class_httpmessage http_msg: The manipulated HTTP message.
3949 :param string uri: The new uri.
3950
3951.. js:function:: HTTPMessage.unset_eom(http_msg)
3952
3953 This function remove the end of message for the HTTP message **http_msg**.
3954
3955 :param class_httpmessage http_msg: The manipulated HTTP message.
3956
William Lallemand10cea5c2022-03-30 16:02:43 +02003957.. _CertCache_class:
3958
3959CertCache class
3960================
3961
3962.. js:class:: CertCache
3963
3964 This class allows to update an SSL certificate file in the memory of the
3965 current HAProxy process. It will do the same as "set ssl cert" + "commit ssl
3966 cert" over the HAProxy CLI.
3967
3968.. js:function:: CertCache.set(certificate)
3969
3970 This function updates a certificate in memory.
3971
3972 :param table certificate: A table containing the fields to update.
3973 :param string certificate.filename: The mandatory filename of the certificate
3974 to update, it must already exist in memory.
3975 :param string certificate.crt: A certificate in the PEM format. It can also
3976 contain a private key.
3977 :param string certificate.key: A private key in the PEM format.
3978 :param string certificate.ocsp: An OCSP response in base64. (cf management.txt)
3979 :param string certificate.issuer: The certificate of the OCSP issuer.
3980 :param string certificate.sctl: An SCTL file.
3981
3982.. code-block:: lua
3983
3984 CertCache.set{filename="certs/localhost9994.pem.rsa", crt=crt}
3985
3986
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01003987External Lua libraries
3988======================
3989
3990A lot of useful lua libraries can be found here:
3991
Aurelien DARRAGON846fc7d2022-10-14 08:48:57 +02003992* Lua toolbox has been superseded by `https://luarocks.org/ <https://luarocks.org/>`_
3993 The old lua toolbox source code is still available here `https://github.com/catwell/lua-toolbox <https://github.com/catwell/lua-toolbox>`_ (DEPRECATED)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01003994
Ilya Shipitsin2075ca82020-03-06 23:22:22 +05003995Redis client library:
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01003996
3997* `https://github.com/nrk/redis-lua <https://github.com/nrk/redis-lua>`_
3998
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02003999This is an example about the usage of the Redis library within HAProxy. Note that
4000each call to any function of this library can throw an error if the socket
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01004001connection fails.
4002
4003.. code-block:: lua
4004
4005 -- load the redis library
4006 local redis = require("redis");
4007
4008 function do_something(txn)
4009
4010 -- create and connect new tcp socket
4011 local tcp = core.tcp();
4012 tcp:settimeout(1);
4013 tcp:connect("127.0.0.1", 6379);
4014
4015 -- use the redis library with this new socket
4016 local client = redis.connect({socket=tcp});
4017 client:ping();
4018
4019 end
4020
4021OpenSSL:
4022
4023* `http://mkottman.github.io/luacrypto/index.html
4024 <http://mkottman.github.io/luacrypto/index.html>`_
4025
4026* `https://github.com/brunoos/luasec/wiki
4027 <https://github.com/brunoos/luasec/wiki>`_