blob: f25f9dce18946898fc75c0e461b9ce1599d636c9 [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
945 function(event, event_data, sub)
946..
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)
951
952 .. Warning::
953 The callback function will only be scheduled on the very same thread that
954 performed the subscription.
955
956 Moreover, each thread treats events sequentially. It means that if you have,
957 let's say SERVER_UP followed by a SERVER_DOWN in a short timelapse, then
958 the cb function will first be called with SERVER_UP, and once it's done
959 handling the event, the cb function will be called again with SERVER_DOWN.
960
961 This is to ensure event consistency when it comes to logging / triggering logic
962 from lua.
963
964 Your lua cb function may yield if needed, but you're pleased to process the
965 event as fast as possible to prevent the event queue from growing up, depending
966 on the event flow that is expected for the given subscription.
967
968 To prevent abuses, if the event queue for the current subscription goes over
969 a certain amount of unconsumed events, the subscription will pause itself
970 automatically for as long as it takes for your handler to catch up. This would
971 lead to events being missed, so an error will be reported in the logs to warn
972 you about that.
973 This is not something you want to let happen too often, it may indicate that
974 you subscribed to an event that is occurring too frequently or/and that your
975 callback function is too slow to keep up the pace and you should review it.
976
977 If you want to do some parallel processing because your callback functions are
978 slow: you might want to create subtasks from lua using
979 :js:func:`core.register_task()` from within your callback function to perform
980 the heavy job in a dedicated task and allow remaining events to be processed
981 more quickly.
982
Thierry Fournierf61aa632016-02-19 20:56:00 +0100983.. _proxy_class:
984
985Proxy class
986============
987
988.. js:class:: Proxy
989
990 This class provides a way for manipulating proxy and retrieving information
991 like statistics.
992
Thierry FOURNIER817e7592017-07-24 14:35:04 +0200993.. js:attribute:: Proxy.name
994
995 Contain the name of the proxy.
996
Aurelien DARRAGONc4b24372023-03-02 12:00:06 +0100997 .. warning::
998 This attribute is now deprecated and will eventually be removed.
999 Please use :js:func:`Proxy.get_name()` function instead.
1000
Thierry Fournierb0467732022-10-07 12:07:24 +02001001.. js:function:: Proxy.get_name()
1002
1003 Returns the name of the proxy.
1004
Baptiste Assmann46c72552017-10-26 21:51:58 +02001005.. js:attribute:: Proxy.uuid
1006
1007 Contain the unique identifier of the proxy.
1008
Aurelien DARRAGONc4b24372023-03-02 12:00:06 +01001009 .. warning::
1010 This attribute is now deprecated and will eventually be removed.
1011 Please use :js:func:`Proxy.get_uuid()` function instead.
1012
Thierry Fournierb0467732022-10-07 12:07:24 +02001013.. js:function:: Proxy.get_uuid()
1014
1015 Returns the unique identifier of the proxy.
1016
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001017.. js:attribute:: Proxy.servers
1018
Patrick Hemmerc6a1d712018-05-01 21:30:41 -04001019 Contain a table with the attached servers. The table is indexed by server
1020 name, and each server entry is an object of type :ref:`server_class`.
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001021
Adis Nezirovic8878f8e2018-07-13 12:18:33 +02001022.. js:attribute:: Proxy.stktable
1023
1024 Contains a stick table object attached to the proxy.
1025
Thierry Fournierff480422016-02-25 08:36:46 +01001026.. js:attribute:: Proxy.listeners
1027
Patrick Hemmerc6a1d712018-05-01 21:30:41 -04001028 Contain a table with the attached listeners. The table is indexed by listener
1029 name, and each each listeners entry is an object of type
1030 :ref:`listener_class`.
Thierry Fournierff480422016-02-25 08:36:46 +01001031
Thierry Fournierf61aa632016-02-19 20:56:00 +01001032.. js:function:: Proxy.pause(px)
1033
1034 Pause the proxy. See the management socket documentation for more information.
1035
1036 :param class_proxy px: A :ref:`proxy_class` which indicates the manipulated
1037 proxy.
1038
1039.. js:function:: Proxy.resume(px)
1040
1041 Resume the proxy. See the management socket documentation for more
1042 information.
1043
1044 :param class_proxy px: A :ref:`proxy_class` which indicates the manipulated
1045 proxy.
1046
1047.. js:function:: Proxy.stop(px)
1048
1049 Stop the proxy. See the management socket documentation for more information.
1050
1051 :param class_proxy px: A :ref:`proxy_class` which indicates the manipulated
1052 proxy.
1053
1054.. js:function:: Proxy.shut_bcksess(px)
1055
1056 Kill the session attached to a backup server. See the management socket
1057 documentation for more information.
1058
1059 :param class_proxy px: A :ref:`proxy_class` which indicates the manipulated
1060 proxy.
1061
1062.. js:function:: Proxy.get_cap(px)
1063
1064 Returns a string describing the capabilities of the proxy.
1065
1066 :param class_proxy px: A :ref:`proxy_class` which indicates the manipulated
1067 proxy.
1068 :returns: a string "frontend", "backend", "proxy" or "ruleset".
1069
1070.. js:function:: Proxy.get_mode(px)
1071
1072 Returns a string describing the mode of the current proxy.
1073
1074 :param class_proxy px: A :ref:`proxy_class` which indicates the manipulated
1075 proxy.
1076 :returns: a string "tcp", "http", "health" or "unknown"
1077
1078.. js:function:: Proxy.get_stats(px)
1079
Bertrand Jacquin874a35c2018-09-10 21:26:07 +01001080 Returns a table containing the proxy statistics. The statistics returned are
Thierry Fournierf61aa632016-02-19 20:56:00 +01001081 not the same if the proxy is frontend or a backend.
1082
1083 :param class_proxy px: A :ref:`proxy_class` which indicates the manipulated
1084 proxy.
Patrick Hemmerc6a1d712018-05-01 21:30:41 -04001085 :returns: a key/value table containing stats
Thierry Fournierf61aa632016-02-19 20:56:00 +01001086
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001087.. _server_class:
1088
1089Server class
1090============
1091
Patrick Hemmerc6a1d712018-05-01 21:30:41 -04001092.. js:class:: Server
1093
1094 This class provides a way for manipulating servers and retrieving information.
1095
Patrick Hemmera62ae7e2018-04-29 14:23:48 -04001096.. js:attribute:: Server.name
1097
1098 Contain the name of the server.
1099
Aurelien DARRAGONc4b24372023-03-02 12:00:06 +01001100 .. warning::
1101 This attribute is now deprecated and will eventually be removed.
1102 Please use :js:func:`Server.get_name()` function instead.
1103
Thierry Fournierb0467732022-10-07 12:07:24 +02001104.. js:function:: Server.get_name(sv)
1105
1106 Returns the name of the server.
1107
Patrick Hemmera62ae7e2018-04-29 14:23:48 -04001108.. js:attribute:: Server.puid
1109
1110 Contain the proxy unique identifier of the server.
1111
Aurelien DARRAGONc4b24372023-03-02 12:00:06 +01001112 .. warning::
1113 This attribute is now deprecated and will eventually be removed.
1114 Please use :js:func:`Server.get_puid()` function instead.
1115
Thierry Fournierb0467732022-10-07 12:07:24 +02001116.. js:function:: Server.get_puid(sv)
1117
1118 Returns the proxy unique identifier of the server.
1119
Aurelien DARRAGON94ee6632023-03-10 15:11:27 +01001120.. js:function:: Server.get_rid(sv)
1121
1122 Returns the rid (revision ID) of the server.
1123 It is an unsigned integer that is set upon server creation. Value is derived
1124 from a global counter that starts at 0 and is incremented each time one or
1125 multiple server deletions are followed by a server addition (meaning that
1126 old name/id reuse could occur).
1127
1128 Combining server name/id with server rid yields a process-wide unique
1129 identifier.
1130
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001131.. js:function:: Server.is_draining(sv)
1132
Patrick Hemmerc6a1d712018-05-01 21:30:41 -04001133 Return true if the server is currently draining sticky connections.
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001134
1135 :param class_server sv: A :ref:`server_class` which indicates the manipulated
1136 server.
1137 :returns: a boolean
1138
Patrick Hemmer32d539f2018-04-29 14:25:46 -04001139.. js:function:: Server.set_maxconn(sv, weight)
1140
1141 Dynamically change the maximum connections of the server. See the management
1142 socket documentation for more information about the format of the string.
1143
1144 :param class_server sv: A :ref:`server_class` which indicates the manipulated
1145 server.
1146 :param string maxconn: A string describing the server maximum connections.
1147
1148.. js:function:: Server.get_maxconn(sv, weight)
1149
1150 This function returns an integer representing the server maximum connections.
1151
1152 :param class_server sv: A :ref:`server_class` which indicates the manipulated
1153 server.
1154 :returns: an integer.
1155
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001156.. js:function:: Server.set_weight(sv, weight)
1157
Patrick Hemmerc6a1d712018-05-01 21:30:41 -04001158 Dynamically change the weight of the server. See the management socket
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001159 documentation for more information about the format of the string.
1160
1161 :param class_server sv: A :ref:`server_class` which indicates the manipulated
1162 server.
1163 :param string weight: A string describing the server weight.
1164
1165.. js:function:: Server.get_weight(sv)
1166
Patrick Hemmerc6a1d712018-05-01 21:30:41 -04001167 This function returns an integer representing the server weight.
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001168
1169 :param class_server sv: A :ref:`server_class` which indicates the manipulated
1170 server.
1171 :returns: an integer.
1172
Joseph C. Sible49bbf522020-05-04 22:20:32 -04001173.. js:function:: Server.set_addr(sv, addr[, port])
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001174
Patrick Hemmerc6a1d712018-05-01 21:30:41 -04001175 Dynamically change the address of the server. See the management socket
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001176 documentation for more information about the format of the string.
1177
1178 :param class_server sv: A :ref:`server_class` which indicates the manipulated
1179 server.
Patrick Hemmerc6a1d712018-05-01 21:30:41 -04001180 :param string addr: A string describing the server address.
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001181
1182.. js:function:: Server.get_addr(sv)
1183
Patrick Hemmerc6a1d712018-05-01 21:30:41 -04001184 Returns a string describing the address of the server.
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001185
1186 :param class_server sv: A :ref:`server_class` which indicates the manipulated
1187 server.
1188 :returns: A string
1189
1190.. js:function:: Server.get_stats(sv)
1191
1192 Returns server statistics.
1193
1194 :param class_server sv: A :ref:`server_class` which indicates the manipulated
1195 server.
Patrick Hemmerc6a1d712018-05-01 21:30:41 -04001196 :returns: a key/value table containing stats
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001197
1198.. js:function:: Server.shut_sess(sv)
1199
1200 Shutdown all the sessions attached to the server. See the management socket
1201 documentation for more information about this function.
1202
1203 :param class_server sv: A :ref:`server_class` which indicates the manipulated
1204 server.
1205
1206.. js:function:: Server.set_drain(sv)
1207
1208 Drain sticky sessions. See the management socket documentation for more
1209 information about this function.
1210
1211 :param class_server sv: A :ref:`server_class` which indicates the manipulated
1212 server.
1213
1214.. js:function:: Server.set_maint(sv)
1215
1216 Set maintenance mode. See the management socket documentation for more
1217 information about this function.
1218
1219 :param class_server sv: A :ref:`server_class` which indicates the manipulated
1220 server.
1221
1222.. js:function:: Server.set_ready(sv)
1223
1224 Set normal mode. See the management socket documentation for more information
1225 about this function.
1226
1227 :param class_server sv: A :ref:`server_class` which indicates the manipulated
1228 server.
1229
1230.. js:function:: Server.check_enable(sv)
1231
1232 Enable health checks. See the management socket documentation for more
1233 information about this function.
1234
1235 :param class_server sv: A :ref:`server_class` which indicates the manipulated
1236 server.
1237
1238.. js:function:: Server.check_disable(sv)
1239
1240 Disable health checks. See the management socket documentation for more
1241 information about this function.
1242
1243 :param class_server sv: A :ref:`server_class` which indicates the manipulated
1244 server.
1245
1246.. js:function:: Server.check_force_up(sv)
1247
1248 Force health-check up. See the management socket documentation for more
1249 information about this function.
1250
1251 :param class_server sv: A :ref:`server_class` which indicates the manipulated
1252 server.
1253
1254.. js:function:: Server.check_force_nolb(sv)
1255
1256 Force health-check nolb mode. See the management socket documentation for more
1257 information about this function.
1258
1259 :param class_server sv: A :ref:`server_class` which indicates the manipulated
1260 server.
1261
1262.. js:function:: Server.check_force_down(sv)
1263
1264 Force health-check down. See the management socket documentation for more
1265 information about this function.
1266
1267 :param class_server sv: A :ref:`server_class` which indicates the manipulated
1268 server.
1269
1270.. js:function:: Server.agent_enable(sv)
1271
1272 Enable agent check. See the management socket documentation for more
1273 information about this function.
1274
1275 :param class_server sv: A :ref:`server_class` which indicates the manipulated
1276 server.
1277
1278.. js:function:: Server.agent_disable(sv)
1279
1280 Disable agent check. See the management socket documentation for more
1281 information about this function.
1282
1283 :param class_server sv: A :ref:`server_class` which indicates the manipulated
1284 server.
1285
1286.. js:function:: Server.agent_force_up(sv)
1287
1288 Force agent check up. See the management socket documentation for more
1289 information about this function.
1290
1291 :param class_server sv: A :ref:`server_class` which indicates the manipulated
1292 server.
1293
1294.. js:function:: Server.agent_force_down(sv)
1295
1296 Force agent check down. See the management socket documentation for more
1297 information about this function.
1298
1299 :param class_server sv: A :ref:`server_class` which indicates the manipulated
1300 server.
1301
Aurelien DARRAGON223770d2023-03-10 15:34:35 +01001302.. js:function:: Server.event_sub(sv, event_types, func)
1303
1304 Register a function that will be called on specific server events.
1305 It works exactly like :js:func:`core.event_sub()` except that the subscription
1306 will be performed within the server dedicated subscription list instead of the
1307 global one.
1308 (Your callback function will only be called for server events affecting sv)
1309
1310 See :js:func:`core.event_sub()` for function usage.
1311
1312 A key advantage to using :js:func:`Server.event_sub()` over
1313 :js:func:`core.event_sub()` for servers is that :js:func:`Server.event_sub()`
1314 allows you to be notified for servers events of a single server only.
1315 It removes the needs for extra filtering in your callback function if you only
1316 care about a single server, and also prevents useless wakeups.
1317
1318 For instance, if you want to be notified for UP/DOWN events on a given set of
1319 servers, it is recommended to peform multiple per-server subscriptions since
1320 it will be more efficient that doing a single global subscription that will
1321 filter the received events.
1322 Unless you really want to be notified for servers events of ALL servers of
1323 course, which could make sense given you setup but should be avoided if you
1324 have an important number of servers as it will add a significant load on your
1325 haproxy process in case of multiple servers state change in a short amount of
1326 time.
1327
1328 .. Note::
1329 You may also combine :js:func:`core.event_sub()` with
1330 :js:func:`Server.event_sub()`.
1331
1332 Also, don't forget that you can use :js:func:`core.register_task()` from
1333 your callback function if needed. (ie: parallel work)
1334
1335 Here is a working example combining :js:func:`core.event_sub()` with
1336 :js:func:`Server.event_sub()` and :js:func:`core.register_task()`
1337 (This only serves as a demo, this is not necessarily useful to do so)
1338
1339.. code-block:: lua
1340
1341 core.event_sub({"SERVER_ADD"}, function(event, data, sub)
1342 -- in the global event handler
1343 if data["reference"] ~= nil then
1344 print("Tracking new server: ", data["name"])
1345 data["reference"]:event_sub({"SERVER_UP", "SERVER_DOWN"}, function(event, data, sub)
1346 -- in the per-server event handler
1347 if data["reference"] ~= nil then
1348 core.register_task(function(server)
1349 -- subtask to perform some async work (e.g.: HTTP API calls, sending emails...)
1350 print("ASYNC: SERVER ", server:get_name(), " is ", event == "SERVER_UP" and "UP" or "DOWN")
1351 end, data["reference"])
1352 end
1353 end)
1354 end
1355 end)
1356
1357..
1358
1359 In this example, we will first track global server addition events.
1360 For each newly added server ("add server" on the cli), we will register a
1361 UP/DOWN server subscription.
1362 Then, the callback function will schedule the event handling in an async
1363 subtask which will receive the server reference as an argument.
1364
Thierry Fournierff480422016-02-25 08:36:46 +01001365.. _listener_class:
1366
1367Listener class
1368==============
1369
1370.. js:function:: Listener.get_stats(ls)
1371
1372 Returns server statistics.
1373
1374 :param class_listener ls: A :ref:`listener_class` which indicates the
1375 manipulated listener.
Patrick Hemmerc6a1d712018-05-01 21:30:41 -04001376 :returns: a key/value table containing stats
Thierry Fournierff480422016-02-25 08:36:46 +01001377
Aurelien DARRAGONc84899c2023-02-20 18:18:59 +01001378.. _event_sub_class:
1379
1380EventSub class
1381==============
1382
1383.. js:function:: EventSub.unsub()
1384
1385 End the subscription, the callback function will not be called again.
1386
1387.. _server_event_class:
1388
1389ServerEvent class
1390=================
1391
1392.. js:attribute:: ServerEvent.name
1393
1394 Contains the name of the server.
1395
1396.. js:attribute:: ServerEvent.puid
1397
1398 Contains the proxy-unique uid of the server
1399
1400.. js:attribute:: ServerEvent.rid
1401
1402 Contains the revision ID of the server
1403
1404.. js:attribute:: ServerEvent.proxy_name
1405
1406 Contains the name of the proxy to which the server belongs
1407
1408.. js:attribute:: ServerEvent.reference
1409
1410 Reference to the live server (A :ref:`server_class`).
1411
1412 .. Warning::
1413 Not available if the server was removed in the meantime.
1414 (Will never be set for SERVER_DEL event since the server does not exist anymore)
1415
Thierry Fournier1de16592016-01-27 09:49:07 +01001416.. _concat_class:
1417
1418Concat class
1419============
1420
1421.. js:class:: Concat
1422
1423 This class provides a fast way for string concatenation. The way using native
1424 Lua concatenation like the code below is slow for some reasons.
1425
1426.. code-block:: lua
1427
1428 str = "string1"
1429 str = str .. ", string2"
1430 str = str .. ", string3"
1431..
1432
1433 For each concatenation, Lua:
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02001434 - allocates memory for the result,
1435 - catenates the two string copying the strings in the new memory block,
1436 - frees the old memory block containing the string which is no longer used.
1437
Thierry Fournier1de16592016-01-27 09:49:07 +01001438 This process does many memory move, allocation and free. In addition, the
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02001439 memory is not really freed, it is just marked as unused and waits for the
Thierry Fournier1de16592016-01-27 09:49:07 +01001440 garbage collector.
1441
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02001442 The Concat class provides an alternative way to concatenate strings. It uses
Thierry Fournier1de16592016-01-27 09:49:07 +01001443 the internal Lua mechanism (it does not allocate memory), but it doesn't copy
1444 the data more than once.
1445
1446 On my computer, the following loops spends 0.2s for the Concat method and
1447 18.5s for the pure Lua implementation. So, the Concat class is about 1000x
1448 faster than the embedded solution.
1449
1450.. code-block:: lua
1451
1452 for j = 1, 100 do
1453 c = core.concat()
1454 for i = 1, 20000 do
1455 c:add("#####")
1456 end
1457 end
1458..
1459
1460.. code-block:: lua
1461
1462 for j = 1, 100 do
1463 c = ""
1464 for i = 1, 20000 do
1465 c = c .. "#####"
1466 end
1467 end
1468..
1469
1470.. js:function:: Concat.add(concat, string)
1471
1472 This function adds a string to the current concatenated string.
1473
1474 :param class_concat concat: A :ref:`concat_class` which contains the currently
Ilya Shipitsin2075ca82020-03-06 23:22:22 +05001475 built string.
Bertrand Jacquin874a35c2018-09-10 21:26:07 +01001476 :param string string: A new string to concatenate to the current built
Thierry Fournier1de16592016-01-27 09:49:07 +01001477 string.
1478
1479.. js:function:: Concat.dump(concat)
1480
Bertrand Jacquin874a35c2018-09-10 21:26:07 +01001481 This function returns the concatenated string.
Thierry Fournier1de16592016-01-27 09:49:07 +01001482
1483 :param class_concat concat: A :ref:`concat_class` which contains the currently
Ilya Shipitsin2075ca82020-03-06 23:22:22 +05001484 built string.
Thierry Fournier1de16592016-01-27 09:49:07 +01001485 :returns: the concatenated string
1486
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001487.. _fetches_class:
1488
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001489Fetches class
1490=============
1491
1492.. js:class:: Fetches
1493
1494 This class contains a lot of internal HAProxy sample fetches. See the
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02001495 HAProxy "configuration.txt" documentation for more information.
1496 (chapters 7.3.2 to 7.3.6)
Thierry FOURNIER2e4893c2015-03-18 13:37:27 +01001497
Christopher Faulet1e9b1b62021-08-11 10:14:30 +02001498 .. warning::
1499 some sample fetches are not available in some context. These limitations
1500 are specified in this documentation when they're useful.
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001501
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01001502 :see: :js:attr:`TXN.f`
1503 :see: :js:attr:`TXN.sf`
Thierry FOURNIER2e4893c2015-03-18 13:37:27 +01001504
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02001505 Fetches are useful to:
Thierry FOURNIER2e4893c2015-03-18 13:37:27 +01001506
1507 * get system time,
1508 * get environment variable,
1509 * get random numbers,
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02001510 * know backend status like the number of users in queue or the number of
Thierry FOURNIER2e4893c2015-03-18 13:37:27 +01001511 connections established,
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02001512 * get client information like ip source or destination,
Thierry FOURNIER2e4893c2015-03-18 13:37:27 +01001513 * deal with stick tables,
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02001514 * fetch established SSL information,
1515 * fetch HTTP information like headers or method.
Thierry FOURNIER2e4893c2015-03-18 13:37:27 +01001516
1517.. code-block:: lua
1518
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001519 function action(txn)
1520 -- Get source IP
1521 local clientip = txn.f:src()
1522 end
Thierry FOURNIER2e4893c2015-03-18 13:37:27 +01001523..
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001524
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001525.. _converters_class:
1526
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001527Converters class
1528================
1529
1530.. js:class:: Converters
1531
1532 This class contains a lot of internal HAProxy sample converters. See the
Thierry FOURNIER2e4893c2015-03-18 13:37:27 +01001533 HAProxy documentation "configuration.txt" for more information about her
1534 usage. Its the chapter 7.3.1.
1535
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01001536 :see: :js:attr:`TXN.c`
1537 :see: :js:attr:`TXN.sc`
Thierry FOURNIER2e4893c2015-03-18 13:37:27 +01001538
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02001539 Converters provides stateful transformation. They are useful to:
Thierry FOURNIER2e4893c2015-03-18 13:37:27 +01001540
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02001541 * convert input to base64,
1542 * apply hash on input string (djb2, crc32, sdbm, wt6),
Thierry FOURNIER2e4893c2015-03-18 13:37:27 +01001543 * format date,
1544 * json escape,
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02001545 * extract preferred language comparing two lists,
Thierry FOURNIER2e4893c2015-03-18 13:37:27 +01001546 * turn to lower or upper chars,
1547 * deal with stick tables.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001548
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001549.. _channel_class:
1550
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001551Channel class
1552=============
1553
1554.. js:class:: Channel
1555
Christopher Faulet5a2c6612021-08-15 20:35:25 +02001556 **context**: action, sample-fetch, convert, filter
1557
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001558 HAProxy uses two buffers for the processing of the requests. The first one is
1559 used with the request data (from the client to the server) and the second is
1560 used for the response data (from the server to the client).
1561
1562 Each buffer contains two types of data. The first type is the incoming data
1563 waiting for a processing. The second part is the outgoing data already
1564 processed. Usually, the incoming data is processed, after it is tagged as
1565 outgoing data, and finally it is sent. The following functions provides tools
1566 for manipulating these data in a buffer.
1567
1568 The following diagram shows where the channel class function are applied.
1569
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001570 .. image:: _static/channel.png
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001571
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001572 .. warning::
1573 It is not possible to read from the response in request action, and it is
Boyang Li60cfe8b2022-05-10 18:11:00 +00001574 not possible to read from the request channel in response action.
Christopher Faulet09530392021-06-14 11:43:18 +02001575
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001576 .. warning::
1577 It is forbidden to alter the Channels buffer from HTTP contexts. So only
1578 :js:func:`Channel.input`, :js:func:`Channel.output`,
1579 :js:func:`Channel.may_recv`, :js:func:`Channel.is_full` and
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02001580 :js:func:`Channel.is_resp` can be called from a HTTP context.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001581
Christopher Faulet5a2c6612021-08-15 20:35:25 +02001582 All the functions provided by this class are available in the
1583 **sample-fetches**, **actions** and **filters** contexts. For **filters**,
1584 incoming data (offset and length) are relative to the filter. Some functions
Boyang Li60cfe8b2022-05-10 18:11:00 +00001585 may yield, but only for **actions**. Yield is not possible for
Christopher Faulet5a2c6612021-08-15 20:35:25 +02001586 **sample-fetches**, **converters** and **filters**.
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001587
1588.. js:function:: Channel.append(channel, string)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001589
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001590 This function copies the string **string** at the end of incoming data of the
1591 channel buffer. The function returns the copied length on success or -1 if
1592 data cannot be copied.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001593
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001594 Same that :js:func:`Channel.insert(channel, string, channel:input())`.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001595
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001596 :param class_channel channel: The manipulated Channel.
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02001597 :param string string: The data to copy at the end of incoming data.
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001598 :returns: an integer containing the amount of bytes copied or -1.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001599
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001600.. js:function:: Channel.data(channel [, offset [, length]])
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001601
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001602 This function returns **length** bytes of incoming data from the channel
1603 buffer, starting at the offset **offset**. The data are not removed from the
1604 buffer.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001605
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001606 By default, if no length is provided, all incoming data found, starting at the
1607 given offset, are returned. If **length** is set to -1, the function tries to
Christopher Faulet5a2c6612021-08-15 20:35:25 +02001608 retrieve a maximum of data and, if called by an action, it yields if
1609 necessary. It also waits for more data if the requested length exceeds the
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02001610 available amount of incoming data. Not providing an offset is the same as
Ilya Shipitsinff0f2782021-08-22 22:18:07 +05001611 setting it to 0. A positive offset is relative to the beginning of incoming
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02001612 data of the channel buffer while negative offset is relative to the end.
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001613
1614 If there is no incoming data and the channel can't receive more data, a 'nil'
1615 value is returned.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001616
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001617 :param class_channel channel: The manipulated Channel.
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001618 :param integer offset: *optional* The offset in incoming data to start to get
1619 data. 0 by default. May be negative to be relative to
1620 the end of incoming data.
1621 :param integer length: *optional* The expected length of data to retrieve. All
1622 incoming data by default. May be set to -1 to get a
1623 maximum of data.
1624 :returns: a string containing the data found or nil.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001625
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001626.. js:function:: Channel.forward(channel, length)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001627
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001628 This function forwards **length** bytes of data from the channel buffer. If
Christopher Faulet5a2c6612021-08-15 20:35:25 +02001629 the requested length exceeds the available amount of incoming data, and if
1630 called by an action, the function yields, waiting for more data to forward. It
1631 returns the amount of data forwarded.
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001632
1633 :param class_channel channel: The manipulated Channel.
1634 :param integer int: The amount of data to forward.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001635
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001636.. js:function:: Channel.input(channel)
1637
Christopher Faulet5a2c6612021-08-15 20:35:25 +02001638 This function returns the length of incoming data in the channel buffer. When
1639 called by a filter, this value is relative to the filter.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001640
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001641 :param class_channel channel: The manipulated Channel.
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001642 :returns: an integer containing the amount of available bytes.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001643
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001644.. js:function:: Channel.insert(channel, string [, offset])
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001645
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001646 This function copies the string **string** at the offset **offset** in
1647 incoming data of the channel buffer. The function returns the copied length on
1648 success or -1 if data cannot be copied.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001649
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001650 By default, if no offset is provided, the string is copied in front of
Ilya Shipitsinff0f2782021-08-22 22:18:07 +05001651 incoming data. A positive offset is relative to the beginning of incoming data
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001652 of the channel buffer while negative offset is relative to their end.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001653
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001654 :param class_channel channel: The manipulated Channel.
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02001655 :param string string: The data to copy into incoming data.
1656 :param integer offset: *optional* The offset in incoming data where to copy
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001657 data. 0 by default. May be negative to be relative to
1658 the end of incoming data.
Pieter Baauw4d7f7662015-11-08 16:38:08 +01001659 :returns: an integer containing the amount of bytes copied or -1.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001660
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001661.. js:function:: Channel.is_full(channel)
1662
1663 This function returns true if the channel buffer is full.
1664
Christopher Faulet5a2c6612021-08-15 20:35:25 +02001665 :param class_channel channel: The manipulated Channel.
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001666 :returns: a boolean
1667
1668.. js:function:: Channel.is_resp(channel)
1669
1670 This function returns true if the channel is the response one.
1671
Christopher Faulet5a2c6612021-08-15 20:35:25 +02001672 :param class_channel channel: The manipulated Channel.
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001673 :returns: a boolean
1674
1675.. js:function:: Channel.line(channel [, offset [, length]])
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001676
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001677 This function parses **length** bytes of incoming data of the channel buffer,
1678 starting at offset **offset**, and returns the first line found, including the
Christopher Faulet5a2c6612021-08-15 20:35:25 +02001679 '\\n'. The data are not removed from the buffer. If no line is found, all data
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001680 are returned.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001681
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001682 By default, if no length is provided, all incoming data, starting at the given
1683 offset, are evaluated. If **length** is set to -1, the function tries to
Christopher Faulet5a2c6612021-08-15 20:35:25 +02001684 retrieve a maximum of data and, if called by an action, yields if
1685 necessary. It also waits for more data if the requested length exceeds the
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02001686 available amount of incoming data. Not providing an offset is the same as
Ilya Shipitsinff0f2782021-08-22 22:18:07 +05001687 setting it to 0. A positive offset is relative to the beginning of incoming
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02001688 data of the channel buffer while negative offset is relative to the end.
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001689
1690 If there is no incoming data and the channel can't receive more data, a 'nil'
1691 value is returned.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001692
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001693 :param class_channel channel: The manipulated Channel.
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02001694 :param integer offset: *optional* The offset in incoming data to start to
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001695 parse data. 0 by default. May be negative to be
1696 relative to the end of incoming data.
1697 :param integer length: *optional* The length of data to parse. All incoming
1698 data by default. May be set to -1 to get a maximum of
1699 data.
1700 :returns: a string containing the line found or nil.
1701
1702.. js:function:: Channel.may_recv(channel)
1703
1704 This function returns true if the channel may still receive data.
1705
Christopher Faulet5a2c6612021-08-15 20:35:25 +02001706 :param class_channel channel: The manipulated Channel.
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001707 :returns: a boolean
1708
1709.. js:function:: Channel.output(channel)
1710
Christopher Faulet5a2c6612021-08-15 20:35:25 +02001711 This function returns the length of outgoing data of the channel buffer. When
1712 called by a filter, this value is relative to the filter.
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001713
1714 :param class_channel channel: The manipulated Channel.
1715 :returns: an integer containing the amount of available bytes.
1716
1717.. js:function:: Channel.prepend(channel, string)
1718
1719 This function copies the string **string** in front of incoming data of the
1720 channel buffer. The function returns the copied length on success or -1 if
1721 data cannot be copied.
1722
1723 Same that :js:func:`Channel.insert(channel, string, 0)`.
1724
1725 :param class_channel channel: The manipulated Channel.
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02001726 :param string string: The data to copy in front of incoming data.
Pieter Baauw4d7f7662015-11-08 16:38:08 +01001727 :returns: an integer containing the amount of bytes copied or -1.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001728
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001729.. js:function:: Channel.remove(channel [, offset [, length]])
1730
1731 This function removes **length** bytes of incoming data of the channel buffer,
1732 starting at offset **offset**. This function returns number of bytes removed
1733 on success.
1734
1735 By default, if no length is provided, all incoming data, starting at the given
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02001736 offset, are removed. Not providing an offset is the same as setting it
Ilya Shipitsinff0f2782021-08-22 22:18:07 +05001737 to 0. A positive offset is relative to the beginning of incoming data of the
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02001738 channel buffer while negative offset is relative to the end.
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001739
1740 :param class_channel channel: The manipulated Channel.
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02001741 :param integer offset: *optional* The offset in incoming data where to start
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001742 to remove data. 0 by default. May be negative to
1743 be relative to the end of incoming data.
1744 :param integer length: *optional* The length of data to remove. All incoming
1745 data by default.
1746 :returns: an integer containing the amount of bytes removed.
1747
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001748.. js:function:: Channel.send(channel, string)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001749
Christopher Faulet5a2c6612021-08-15 20:35:25 +02001750 This function requires immediate send of the string **string**. It means the
Ilya Shipitsinff0f2782021-08-22 22:18:07 +05001751 string is copied at the beginning of incoming data of the channel buffer and
Christopher Faulet5a2c6612021-08-15 20:35:25 +02001752 immediately forwarded. Unless if the connection is close, and if called by an
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02001753 action, this function yields to copy and forward all the string.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001754
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001755 :param class_channel channel: The manipulated Channel.
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001756 :param string string: The data to send.
Pieter Baauw4d7f7662015-11-08 16:38:08 +01001757 :returns: an integer containing the amount of bytes copied or -1.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001758
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001759.. js:function:: Channel.set(channel, string [, offset [, length]])
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001760
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02001761 This function replaces **length** bytes of incoming data of the channel buffer,
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001762 starting at offset **offset**, by the string **string**. The function returns
1763 the copied length on success or -1 if data cannot be copied.
1764
1765 By default, if no length is provided, all incoming data, starting at the given
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02001766 offset, are replaced. Not providing an offset is the same as setting it
Ilya Shipitsinff0f2782021-08-22 22:18:07 +05001767 to 0. A positive offset is relative to the beginning of incoming data of the
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02001768 channel buffer while negative offset is relative to the end.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001769
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001770 :param class_channel channel: The manipulated Channel.
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02001771 :param string string: The data to copy into incoming data.
1772 :param integer offset: *optional* The offset in incoming data where to start
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001773 the data replacement. 0 by default. May be negative to
1774 be relative to the end of incoming data.
1775 :param integer length: *optional* The length of data to replace. All incoming
1776 data by default.
1777 :returns: an integer containing the amount of bytes copied or -1.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001778
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001779.. js:function:: Channel.dup(channel)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001780
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001781 **DEPRECATED**
1782
1783 This function returns all incoming data found in the channel buffer. The data
Boyang Li60cfe8b2022-05-10 18:11:00 +00001784 are not removed from the buffer and can be reprocessed later.
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001785
1786 If there is no incoming data and the channel can't receive more data, a 'nil'
1787 value is returned.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001788
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001789 :param class_channel channel: The manipulated Channel.
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001790 :returns: a string containing all data found or nil.
1791
1792 .. warning::
1793 This function is deprecated. :js:func:`Channel.data()` must be used
1794 instead.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001795
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001796.. js:function:: Channel.get(channel)
1797
1798 **DEPRECATED**
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001799
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001800 This function returns all incoming data found in the channel buffer and remove
1801 them from the buffer.
1802
1803 If there is no incoming data and the channel can't receive more data, a 'nil'
1804 value is returned.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001805
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001806 :param class_channel channel: The manipulated Channel.
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001807 :returns: a string containing all the data found or nil.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001808
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001809 .. warning::
1810 This function is deprecated. :js:func:`Channel.data()` must be used to
1811 retrieve data followed by a call to :js:func:`Channel:remove()` to remove
1812 data.
Thierry FOURNIER / OZON.IO65192f32016-11-07 15:28:40 +01001813
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001814 .. code-block:: lua
Thierry FOURNIER / OZON.IO65192f32016-11-07 15:28:40 +01001815
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001816 local data = chn:data()
1817 chn:remove(0, data:len())
1818
1819 ..
1820
1821.. js:function:: Channel.getline(channel)
1822
1823 **DEPRECATED**
1824
1825 This function returns the first line found in incoming data of the channel
Christopher Faulet5a2c6612021-08-15 20:35:25 +02001826 buffer, including the '\\n'. The returned data are removed from the buffer. If
1827 no line is found, and if called by an action, this function yields to wait for
1828 more data, except if the channel can't receive more data. In this case all
1829 data are returned.
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001830
1831 If there is no incoming data and the channel can't receive more data, a 'nil'
1832 value is returned.
1833
1834 :param class_channel channel: The manipulated Channel.
1835 :returns: a string containing the line found or nil.
1836
1837 .. warning::
Boyang Li60cfe8b2022-05-10 18:11:00 +00001838 This function is deprecated. :js:func:`Channel.line()` must be used to
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001839 retrieve a line followed by a call to :js:func:`Channel:remove()` to remove
1840 data.
1841
1842 .. code-block:: lua
1843
1844 local line = chn:line(0, -1)
1845 chn:remove(0, line:len())
1846
1847 ..
1848
1849.. js:function:: Channel.get_in_len(channel)
1850
Boyang Li60cfe8b2022-05-10 18:11:00 +00001851 **DEPRECATED**
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001852
Christopher Faulet5a2c6612021-08-15 20:35:25 +02001853 This function returns the length of the input part of the buffer. When called
1854 by a filter, this value is relative to the filter.
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001855
1856 :param class_channel channel: The manipulated Channel.
1857 :returns: an integer containing the amount of available bytes.
1858
1859 .. warning::
1860 This function is deprecated. :js:func:`Channel.input()` must be used
1861 instead.
1862
1863.. js:function:: Channel.get_out_len(channel)
1864
Boyang Li60cfe8b2022-05-10 18:11:00 +00001865 **DEPRECATED**
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001866
Christopher Faulet5a2c6612021-08-15 20:35:25 +02001867 This function returns the length of the output part of the buffer. When called
1868 by a filter, this value is relative to the filter.
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001869
1870 :param class_channel channel: The manipulated Channel.
1871 :returns: an integer containing the amount of available bytes.
1872
1873 .. warning::
1874 This function is deprecated. :js:func:`Channel.output()` must be used
1875 instead.
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001876
1877.. _http_class:
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001878
1879HTTP class
1880==========
1881
1882.. js:class:: HTTP
1883
1884 This class contain all the HTTP manipulation functions.
1885
Pieter Baauw386a1272015-08-16 15:26:24 +02001886.. js:function:: HTTP.req_get_headers(http)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001887
Patrick Hemmerc6a1d712018-05-01 21:30:41 -04001888 Returns a table containing all the request headers.
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001889
1890 :param class_http http: The related http object.
Patrick Hemmerc6a1d712018-05-01 21:30:41 -04001891 :returns: table of headers.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01001892 :see: :js:func:`HTTP.res_get_headers`
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001893
Patrick Hemmerc6a1d712018-05-01 21:30:41 -04001894 This is the form of the returned table:
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001895
1896.. code-block:: lua
1897
1898 HTTP:req_get_headers()['<header-name>'][<header-index>] = "<header-value>"
1899
1900 local hdr = HTTP:req_get_headers()
1901 hdr["host"][0] = "www.test.com"
1902 hdr["accept"][0] = "audio/basic q=1"
1903 hdr["accept"][1] = "audio/*, q=0.2"
1904 hdr["accept"][2] = "*/*, q=0.1"
1905..
1906
Pieter Baauw386a1272015-08-16 15:26:24 +02001907.. js:function:: HTTP.res_get_headers(http)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001908
Patrick Hemmerc6a1d712018-05-01 21:30:41 -04001909 Returns a table containing all the response headers.
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001910
1911 :param class_http http: The related http object.
Patrick Hemmerc6a1d712018-05-01 21:30:41 -04001912 :returns: table of headers.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01001913 :see: :js:func:`HTTP.req_get_headers`
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001914
Patrick Hemmerc6a1d712018-05-01 21:30:41 -04001915 This is the form of the returned table:
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001916
1917.. code-block:: lua
1918
1919 HTTP:res_get_headers()['<header-name>'][<header-index>] = "<header-value>"
1920
1921 local hdr = HTTP:req_get_headers()
1922 hdr["host"][0] = "www.test.com"
1923 hdr["accept"][0] = "audio/basic q=1"
1924 hdr["accept"][1] = "audio/*, q=0.2"
1925 hdr["accept"][2] = "*.*, q=0.1"
1926..
1927
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001928.. js:function:: HTTP.req_add_header(http, name, value)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001929
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02001930 Appends a HTTP header field in the request whose name is
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001931 specified in "name" and whose value is defined in "value".
1932
1933 :param class_http http: The related http object.
1934 :param string name: The header name.
1935 :param string value: The header value.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01001936 :see: :js:func:`HTTP.res_add_header`
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001937
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001938.. js:function:: HTTP.res_add_header(http, name, value)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001939
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02001940 Appends a HTTP header field in the response whose name is
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001941 specified in "name" and whose value is defined in "value".
1942
1943 :param class_http http: The related http object.
1944 :param string name: The header name.
1945 :param string value: The header value.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01001946 :see: :js:func:`HTTP.req_add_header`
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001947
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001948.. js:function:: HTTP.req_del_header(http, name)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001949
1950 Removes all HTTP header fields in the request whose name is
1951 specified in "name".
1952
1953 :param class_http http: The related http object.
1954 :param string name: The header name.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01001955 :see: :js:func:`HTTP.res_del_header`
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001956
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001957.. js:function:: HTTP.res_del_header(http, name)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001958
1959 Removes all HTTP header fields in the response whose name is
1960 specified in "name".
1961
1962 :param class_http http: The related http object.
1963 :param string name: The header name.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01001964 :see: :js:func:`HTTP.req_del_header`
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001965
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001966.. js:function:: HTTP.req_set_header(http, name, value)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001967
Bertrand Jacquin874a35c2018-09-10 21:26:07 +01001968 This variable replace all occurrence of all header "name", by only
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001969 one containing the "value".
1970
1971 :param class_http http: The related http object.
1972 :param string name: The header name.
1973 :param string value: The header value.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01001974 :see: :js:func:`HTTP.res_set_header`
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001975
Bertrand Jacquin874a35c2018-09-10 21:26:07 +01001976 This function does the same work as the following code:
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001977
1978.. code-block:: lua
1979
1980 function fcn(txn)
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001981 TXN.http:req_del_header("header")
1982 TXN.http:req_add_header("header", "value")
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001983 end
1984..
1985
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001986.. js:function:: HTTP.res_set_header(http, name, value)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001987
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02001988 This function replaces all occurrence of all header "name", by only
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001989 one containing the "value".
1990
1991 :param class_http http: The related http object.
1992 :param string name: The header name.
1993 :param string value: The header value.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01001994 :see: :js:func:`HTTP.req_rep_header()`
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001995
Pieter Baauw386a1272015-08-16 15:26:24 +02001996.. js:function:: HTTP.req_rep_header(http, name, regex, replace)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001997
1998 Matches the regular expression in all occurrences of header field "name"
1999 according to "regex", and replaces them with the "replace" argument. The
2000 replacement value can contain back references like \1, \2, ... This
2001 function works with the request.
2002
2003 :param class_http http: The related http object.
2004 :param string name: The header name.
2005 :param string regex: The match regular expression.
2006 :param string replace: The replacement value.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01002007 :see: :js:func:`HTTP.res_rep_header()`
Thierry FOURNIER08504f42015-03-16 14:17:08 +01002008
Pieter Baauw386a1272015-08-16 15:26:24 +02002009.. js:function:: HTTP.res_rep_header(http, name, regex, string)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01002010
2011 Matches the regular expression in all occurrences of header field "name"
2012 according to "regex", and replaces them with the "replace" argument. The
2013 replacement value can contain back references like \1, \2, ... This
2014 function works with the request.
2015
2016 :param class_http http: The related http object.
2017 :param string name: The header name.
2018 :param string regex: The match regular expression.
2019 :param string replace: The replacement value.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01002020 :see: :js:func:`HTTP.req_rep_header()`
Thierry FOURNIER08504f42015-03-16 14:17:08 +01002021
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002022.. js:function:: HTTP.req_set_method(http, method)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01002023
2024 Rewrites the request method with the parameter "method".
2025
2026 :param class_http http: The related http object.
2027 :param string method: The new method.
2028
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002029.. js:function:: HTTP.req_set_path(http, path)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01002030
2031 Rewrites the request path with the "path" parameter.
2032
2033 :param class_http http: The related http object.
2034 :param string path: The new path.
2035
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002036.. js:function:: HTTP.req_set_query(http, query)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01002037
2038 Rewrites the request's query string which appears after the first question
2039 mark ("?") with the parameter "query".
2040
2041 :param class_http http: The related http object.
2042 :param string query: The new query.
2043
Thierry FOURNIER0d79cf62015-08-26 14:20:58 +02002044.. js:function:: HTTP.req_set_uri(http, uri)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01002045
2046 Rewrites the request URI with the parameter "uri".
2047
2048 :param class_http http: The related http object.
2049 :param string uri: The new uri.
2050
Robin H. Johnson52f5db22017-01-01 13:10:52 -08002051.. js:function:: HTTP.res_set_status(http, status [, reason])
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +02002052
Robin H. Johnson52f5db22017-01-01 13:10:52 -08002053 Rewrites the response status code with the parameter "code".
2054
2055 If no custom reason is provided, it will be generated from the status.
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +02002056
2057 :param class_http http: The related http object.
2058 :param integer status: The new response status code.
Robin H. Johnson52f5db22017-01-01 13:10:52 -08002059 :param string reason: The new response reason (optional).
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +02002060
William Lallemand00a15022021-11-19 16:02:44 +01002061.. _httpclient_class:
2062
2063HTTPClient class
2064================
2065
2066.. js:class:: HTTPClient
2067
2068 The httpclient class allows issue of outbound HTTP requests through a simple
2069 API without the knowledge of HAProxy internals.
2070
2071.. js:function:: HTTPClient.get(httpclient, request)
2072.. js:function:: HTTPClient.head(httpclient, request)
2073.. js:function:: HTTPClient.put(httpclient, request)
2074.. js:function:: HTTPClient.post(httpclient, request)
2075.. js:function:: HTTPClient.delete(httpclient, request)
2076
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02002077 Send a HTTP request and wait for a response. GET, HEAD PUT, POST and DELETE methods can be used.
2078 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 +01002079
William Lallemanda9256192022-10-21 11:48:24 +02002080 The HTTPClient interface is not able to decompress responses, it is not
2081 recommended to send an Accept-Encoding in the request so the response is
2082 received uncompressed.
William Lallemand00a15022021-11-19 16:02:44 +01002083
2084 :param class httpclient: Is the manipulated HTTPClient.
2085 :param table request: Is a table containing the parameters of the request that will be send.
2086 :param string request.url: Is a mandatory parameter for the request that contains the URL.
2087 :param string request.body: Is an optional parameter for the request that contains the body to send.
2088 :param table request.headers: Is an optional parameter for the request that contains the headers to send.
William Lallemand18340302022-02-23 15:57:45 +01002089 :param string request.dst: Is an optional parameter for the destination in haproxy address format.
William Lallemandb4a4ef62022-02-23 14:18:16 +01002090 :param integer request.timeout: Optional timeout parameter, set a "timeout server" on the connections.
William Lallemand00a15022021-11-19 16:02:44 +01002091 :returns: Lua table containing the response
2092
2093
2094.. code-block:: lua
2095
2096 local httpclient = core.httpclient()
William Lallemand4f4f2b72022-02-17 20:00:23 +01002097 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 +01002098
2099..
2100
2101.. code-block:: lua
2102
2103 response = {
2104 status = 400,
2105 reason = "Bad request",
2106 headers = {
2107 ["content-type"] = { "text/html" },
2108 ["cache-control"] = { "no-cache", "no-store" },
2109 },
William Lallemand4f4f2b72022-02-17 20:00:23 +01002110 body = "<html><body><h1>invalid request<h1></body></html>",
William Lallemand00a15022021-11-19 16:02:44 +01002111 }
2112..
2113
2114
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002115.. _txn_class:
2116
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002117TXN class
2118=========
2119
2120.. js:class:: TXN
2121
2122 The txn class contain all the functions relative to the http or tcp
2123 transaction (Note than a tcp stream is the same than a tcp transaction, but
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02002124 a HTTP transaction is not the same than a tcp stream).
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002125
2126 The usage of this class permits to retrieve data from the requests, alter it
2127 and forward it.
2128
2129 All the functions provided by this class are available in the context
Christopher Faulet5a2c6612021-08-15 20:35:25 +02002130 **sample-fetches**, **actions** and **filters**.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002131
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002132.. js:attribute:: TXN.c
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002133
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002134 :returns: An :ref:`converters_class`.
2135
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002136 This attribute contains a Converters class object.
2137
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002138.. js:attribute:: TXN.sc
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002139
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002140 :returns: An :ref:`converters_class`.
2141
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002142 This attribute contains a Converters class object. The functions of
2143 this object returns always a string.
2144
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002145.. js:attribute:: TXN.f
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002146
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002147 :returns: An :ref:`fetches_class`.
2148
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002149 This attribute contains a Fetches class object.
2150
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002151.. js:attribute:: TXN.sf
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002152
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002153 :returns: An :ref:`fetches_class`.
2154
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002155 This attribute contains a Fetches class object. The functions of
2156 this object returns always a string.
2157
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002158.. js:attribute:: TXN.req
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002159
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002160 :returns: An :ref:`channel_class`.
2161
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002162 This attribute contains a channel class object for the request buffer.
2163
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002164.. js:attribute:: TXN.res
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002165
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002166 :returns: An :ref:`channel_class`.
2167
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002168 This attribute contains a channel class object for the response buffer.
2169
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002170.. js:attribute:: TXN.http
Thierry FOURNIER08504f42015-03-16 14:17:08 +01002171
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002172 :returns: An :ref:`http_class`.
2173
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02002174 This attribute contains a HTTP class object. It is available only if the
Thierry FOURNIER08504f42015-03-16 14:17:08 +01002175 proxy has the "mode http" enabled.
2176
Christopher Faulet5a2c6612021-08-15 20:35:25 +02002177.. js:attribute:: TXN.http_req
2178
2179 :returns: An :ref:`httpmessage_class`.
2180
2181 This attribute contains the request HTTPMessage class object. It is available
2182 only if the proxy has the "mode http" enabled and only in the **filters**
2183 context.
2184
2185.. js:attribute:: TXN.http_res
2186
2187 :returns: An :ref:`httpmessage_class`.
2188
2189 This attribute contains the response HTTPMessage class object. It is available
2190 only if the proxy has the "mode http" enabled and only in the **filters**
2191 context.
2192
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01002193.. js:function:: TXN.log(TXN, loglevel, msg)
2194
2195 This function sends a log. The log is sent, according with the HAProxy
2196 configuration file, on the default syslog server if it is configured and on
2197 the stderr if it is allowed.
2198
2199 :param class_txn txn: The class txn object containing the data.
Bertrand Jacquin874a35c2018-09-10 21:26:07 +01002200 :param integer loglevel: Is the log level associated with the message. It is a
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01002201 number between 0 and 7.
2202 :param string msg: The log content.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01002203 :see: :js:attr:`core.emerg`, :js:attr:`core.alert`, :js:attr:`core.crit`,
2204 :js:attr:`core.err`, :js:attr:`core.warning`, :js:attr:`core.notice`,
2205 :js:attr:`core.info`, :js:attr:`core.debug` (log level definitions)
2206 :see: :js:func:`TXN.deflog`
2207 :see: :js:func:`TXN.Debug`
2208 :see: :js:func:`TXN.Info`
2209 :see: :js:func:`TXN.Warning`
2210 :see: :js:func:`TXN.Alert`
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01002211
2212.. js:function:: TXN.deflog(TXN, msg)
2213
Bertrand Jacquin874a35c2018-09-10 21:26:07 +01002214 Sends a log line with the default loglevel for the proxy associated with the
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01002215 transaction.
2216
2217 :param class_txn txn: The class txn object containing the data.
2218 :param string msg: The log content.
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02002219 :see: :js:func:`TXN.log`
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01002220
2221.. js:function:: TXN.Debug(txn, msg)
2222
2223 :param class_txn txn: The class txn object containing the data.
2224 :param string msg: The log content.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01002225 :see: :js:func:`TXN.log`
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01002226
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02002227 Does the same job as:
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01002228
2229.. code-block:: lua
2230
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002231 function Debug(txn, msg)
2232 TXN.log(txn, core.debug, msg)
2233 end
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01002234..
2235
2236.. js:function:: TXN.Info(txn, msg)
2237
2238 :param class_txn txn: The class txn object containing the data.
2239 :param string msg: The log content.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01002240 :see: :js:func:`TXN.log`
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01002241
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02002242 Does the same job as:
2243
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01002244.. code-block:: lua
2245
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02002246 function Info(txn, msg)
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002247 TXN.log(txn, core.info, msg)
2248 end
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01002249..
2250
2251.. js:function:: TXN.Warning(txn, msg)
2252
2253 :param class_txn txn: The class txn object containing the data.
2254 :param string msg: The log content.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01002255 :see: :js:func:`TXN.log`
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01002256
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02002257 Does the same job as:
2258
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01002259.. code-block:: lua
2260
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02002261 function Warning(txn, msg)
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002262 TXN.log(txn, core.warning, msg)
2263 end
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01002264..
2265
2266.. js:function:: TXN.Alert(txn, msg)
2267
2268 :param class_txn txn: The class txn object containing the data.
2269 :param string msg: The log content.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01002270 :see: :js:func:`TXN.log`
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01002271
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02002272 Does the same job as:
2273
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01002274.. code-block:: lua
2275
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02002276 function Alert(txn, msg)
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002277 TXN.log(txn, core.alert, msg)
2278 end
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01002279..
2280
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002281.. js:function:: TXN.get_priv(txn)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002282
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002283 Return Lua data stored in the current transaction (with the `TXN.set_priv()`)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002284 function. If no data are stored, it returns a nil value.
2285
2286 :param class_txn txn: The class txn object containing the data.
Bertrand Jacquin874a35c2018-09-10 21:26:07 +01002287 :returns: the opaque data previously stored, or nil if nothing is
2288 available.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002289
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002290.. js:function:: TXN.set_priv(txn, data)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002291
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02002292 Store any data in the current HAProxy transaction. This action replaces the
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002293 old stored data.
2294
2295 :param class_txn txn: The class txn object containing the data.
2296 :param opaque data: The data which is stored in the transaction.
2297
Tim Duesterhus4e172c92020-05-19 13:49:42 +02002298.. js:function:: TXN.set_var(TXN, var, value[, ifexist])
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +02002299
David Carlier61fdf8b2015-10-02 11:59:38 +01002300 Converts a Lua type in a HAProxy type and store it in a variable <var>.
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +02002301
2302 :param class_txn txn: The class txn object containing the data.
2303 :param string var: The variable name according with the HAProxy variable syntax.
Thierry FOURNIER / OZON.IOb210bcc2016-12-12 16:24:16 +01002304 :param type value: The value associated to the variable. The type can be string or
2305 integer.
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02002306 :param boolean ifexist: If this parameter is set to true the variable
Tim Duesterhus4e172c92020-05-19 13:49:42 +02002307 will only be set if it was defined elsewhere (i.e. used
Willy Tarreau7978c5c2021-09-07 14:24:07 +02002308 within the configuration). For global variables (using the
2309 "proc" scope), they will only be updated and never created.
2310 It is highly recommended to always set this to true.
Christopher Faulet85d79c92016-11-09 16:54:56 +01002311
2312.. js:function:: TXN.unset_var(TXN, var)
2313
2314 Unset the variable <var>.
2315
2316 :param class_txn txn: The class txn object containing the data.
2317 :param string var: The variable name according with the HAProxy variable syntax.
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +02002318
2319.. js:function:: TXN.get_var(TXN, var)
2320
2321 Returns data stored in the variable <var> converter in Lua type.
2322
2323 :param class_txn txn: The class txn object containing the data.
2324 :param string var: The variable name according with the HAProxy variable syntax.
2325
Christopher Faulet700d9e82020-01-31 12:21:52 +01002326.. js:function:: TXN.reply([reply])
2327
2328 Return a new reply object
2329
2330 :param table reply: A table containing info to initialize the reply fields.
2331 :returns: A :ref:`reply_class` object.
2332
2333 The table used to initialized the reply object may contain following entries :
2334
2335 * status : The reply status code. the code 200 is used by default.
2336 * reason : The reply reason. The reason corresponding to the status code is
2337 used by default.
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02002338 * headers : A list of headers, indexed by header name. Empty by default. For
Christopher Faulet700d9e82020-01-31 12:21:52 +01002339 a given name, multiple values are possible, stored in an ordered list.
2340 * body : The reply body, empty by default.
2341
2342.. code-block:: lua
2343
2344 local reply = txn:reply{
2345 status = 400,
2346 reason = "Bad request",
2347 headers = {
2348 ["content-type"] = { "text/html" },
2349 ["cache-control"] = {"no-cache", "no-store" }
2350 },
2351 body = "<html><body><h1>invalid request<h1></body></html>"
2352 }
2353..
2354 :see: :js:class:`Reply`
2355
2356.. js:function:: TXN.done(txn[, reply])
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002357
Willy Tarreaubc183a62015-08-28 10:39:11 +02002358 This function terminates processing of the transaction and the associated
Christopher Faulet700d9e82020-01-31 12:21:52 +01002359 session and optionally reply to the client for HTTP sessions.
2360
2361 :param class_txn txn: The class txn object containing the data.
2362 :param class_reply reply: The class reply object to return to the client.
2363
2364 This functions can be used when a critical error is detected or to terminate
Willy Tarreaubc183a62015-08-28 10:39:11 +02002365 processing after some data have been returned to the client (eg: a redirect).
Christopher Faulet700d9e82020-01-31 12:21:52 +01002366 To do so, a reply may be provided. This object is optional and may contain a
2367 status code, a reason, a header list and a body. All these fields are
Christopher Faulet7855b192021-11-09 18:39:51 +01002368 optional. When not provided, the default values are used. By default, with an
2369 empty reply object, an empty HTTP 200 response is returned to the client. If
2370 no reply object is provided, the transaction is terminated without any
2371 reply. If a reply object is provided, it must not exceed the buffer size once
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02002372 converted into the internal HTTP representation. Because for now there is no
Christopher Faulet7855b192021-11-09 18:39:51 +01002373 easy way to be sure it fits, it is probably better to keep it reasonably
2374 small.
Christopher Faulet700d9e82020-01-31 12:21:52 +01002375
2376 The reply object may be fully created in lua or the class Reply may be used to
2377 create it.
2378
2379.. code-block:: lua
2380
2381 local reply = txn:reply()
2382 reply:set_status(400, "Bad request")
2383 reply:add_header("content-type", "text/html")
2384 reply:add_header("cache-control", "no-cache")
2385 reply:add_header("cache-control", "no-store")
2386 reply:set_body("<html><body><h1>invalid request<h1></body></html>")
2387 txn:done(reply)
2388..
2389
2390.. code-block:: lua
2391
2392 txn:done{
2393 status = 400,
2394 reason = "Bad request",
2395 headers = {
2396 ["content-type"] = { "text/html" },
2397 ["cache-control"] = { "no-cache", "no-store" },
2398 },
2399 body = "<html><body><h1>invalid request<h1></body></html>"
2400 }
2401..
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002402
Christopher Faulet1e9b1b62021-08-11 10:14:30 +02002403 .. warning::
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02002404 It does not make sense to call this function from sample-fetches. In this case
2405 the behavior is the same than core.done(): it finishes the Lua
Christopher Faulet1e9b1b62021-08-11 10:14:30 +02002406 execution. The transaction is really aborted only from an action registered
2407 function.
Thierry FOURNIERab00df62016-07-14 11:42:37 +02002408
Christopher Faulet700d9e82020-01-31 12:21:52 +01002409 :see: :js:func:`TXN.reply`, :js:class:`Reply`
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002410
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002411.. js:function:: TXN.set_loglevel(txn, loglevel)
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01002412
2413 Is used to change the log level of the current request. The "loglevel" must
2414 be an integer between 0 and 7.
2415
2416 :param class_txn txn: The class txn object containing the data.
2417 :param integer loglevel: The required log level. This variable can be one of
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01002418 :see: :js:attr:`core.emerg`, :js:attr:`core.alert`, :js:attr:`core.crit`,
2419 :js:attr:`core.err`, :js:attr:`core.warning`, :js:attr:`core.notice`,
2420 :js:attr:`core.info`, :js:attr:`core.debug` (log level definitions)
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01002421
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002422.. js:function:: TXN.set_tos(txn, tos)
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01002423
2424 Is used to set the TOS or DSCP field value of packets sent to the client to
2425 the value passed in "tos" on platforms which support this.
2426
2427 :param class_txn txn: The class txn object containing the data.
2428 :param integer tos: The new TOS os DSCP.
2429
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002430.. js:function:: TXN.set_mark(txn, mark)
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01002431
2432 Is used to set the Netfilter MARK on all packets sent to the client to the
2433 value passed in "mark" on platforms which support it.
2434
2435 :param class_txn txn: The class txn object containing the data.
2436 :param integer mark: The mark value.
2437
Patrick Hemmer268a7072018-05-11 12:52:31 -04002438.. js:function:: TXN.set_priority_class(txn, prio)
2439
2440 This function adjusts the priority class of the transaction. The value should
2441 be within the range -2047..2047. Values outside this range will be
2442 truncated.
2443
2444 See the HAProxy configuration.txt file keyword "http-request" action
2445 "set-priority-class" for details.
2446
2447.. js:function:: TXN.set_priority_offset(txn, prio)
2448
2449 This function adjusts the priority offset of the transaction. The value
2450 should be within the range -524287..524287. Values outside this range will be
2451 truncated.
2452
2453 See the HAProxy configuration.txt file keyword "http-request" action
2454 "set-priority-offset" for details.
2455
Christopher Faulet700d9e82020-01-31 12:21:52 +01002456.. _reply_class:
2457
2458Reply class
2459============
2460
2461.. js:class:: Reply
2462
2463 **context**: action
2464
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02002465 This class represents a HTTP response message. It provides some methods to
Christopher Faulet7855b192021-11-09 18:39:51 +01002466 enrich it. Once converted into the internal HTTP representation, the response
2467 message must not exceed the buffer size. Because for now there is no
2468 easy way to be sure it fits, it is probably better to keep it reasonably
2469 small.
2470
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02002471 See tune.bufsize in the configuration manual for details.
Christopher Faulet700d9e82020-01-31 12:21:52 +01002472
2473.. code-block:: lua
2474
2475 local reply = txn:reply({status = 400}) -- default HTTP 400 reason-phase used
2476 reply:add_header("content-type", "text/html")
2477 reply:add_header("cache-control", "no-cache")
2478 reply:add_header("cache-control", "no-store")
2479 reply:set_body("<html><body><h1>invalid request<h1></body></html>")
2480..
2481
2482 :see: :js:func:`TXN.reply`
2483
2484.. js:attribute:: Reply.status
2485
2486 The reply status code. By default, the status code is set to 200.
2487
2488 :returns: integer
2489
2490.. js:attribute:: Reply.reason
2491
2492 The reason string describing the status code.
2493
2494 :returns: string
2495
2496.. js:attribute:: Reply.headers
2497
2498 A table indexing all reply headers by name. To each name is associated an
2499 ordered list of values.
2500
2501 :returns: Lua table
2502
2503.. code-block:: lua
2504
2505 {
2506 ["content-type"] = { "text/html" },
2507 ["cache-control"] = {"no-cache", "no-store" },
2508 x_header_name = { "value1", "value2", ... }
2509 ...
2510 }
2511..
2512
2513.. js:attribute:: Reply.body
2514
2515 The reply payload.
2516
2517 :returns: string
2518
2519.. js:function:: Reply.set_status(REPLY, status[, reason])
2520
2521 Set the reply status code and optionally the reason-phrase. If the reason is
2522 not provided, the default reason corresponding to the status code is used.
2523
2524 :param class_reply reply: The related Reply object.
2525 :param integer status: The reply status code.
2526 :param string reason: The reply status reason (optional).
2527
2528.. js:function:: Reply.add_header(REPLY, name, value)
2529
2530 Add a header to the reply object. If the header does not already exist, a new
2531 entry is created with its name as index and a one-element list containing its
2532 value as value. Otherwise, the header value is appended to the ordered list of
2533 values associated to the header name.
2534
2535 :param class_reply reply: The related Reply object.
2536 :param string name: The header field name.
2537 :param string value: The header field value.
2538
2539.. js:function:: Reply.del_header(REPLY, name)
2540
2541 Remove all occurrences of a header name from the reply object.
2542
2543 :param class_reply reply: The related Reply object.
2544 :param string name: The header field name.
2545
2546.. js:function:: Reply.set_body(REPLY, body)
2547
2548 Set the reply payload.
2549
2550 :param class_reply reply: The related Reply object.
2551 :param string body: The reply payload.
2552
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002553.. _socket_class:
2554
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002555Socket class
2556============
2557
2558.. js:class:: Socket
2559
2560 This class must be compatible with the Lua Socket class. Only the 'client'
2561 functions are available. See the Lua Socket documentation:
2562
2563 `http://w3.impa.br/~diego/software/luasocket/tcp.html
2564 <http://w3.impa.br/~diego/software/luasocket/tcp.html>`_
2565
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002566.. js:function:: Socket.close(socket)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002567
2568 Closes a TCP object. The internal socket used by the object is closed and the
2569 local address to which the object was bound is made available to other
2570 applications. No further operations (except for further calls to the close
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002571 method) are allowed on a closed Socket.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002572
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002573 :param class_socket socket: Is the manipulated Socket.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002574
2575 Note: It is important to close all used sockets once they are not needed,
2576 since, in many systems, each socket uses a file descriptor, which are limited
2577 system resources. Garbage-collected objects are automatically closed before
2578 destruction, though.
2579
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002580.. js:function:: Socket.connect(socket, address[, port])
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002581
2582 Attempts to connect a socket object to a remote host.
2583
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002584
2585 In case of error, the method returns nil followed by a string describing the
2586 error. In case of success, the method returns 1.
2587
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002588 :param class_socket socket: Is the manipulated Socket.
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002589 :param string address: can be an IP address or a host name. See below for more
2590 information.
2591 :param integer port: must be an integer number in the range [1..64K].
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002592 :returns: 1 or nil.
2593
Bertrand Jacquin874a35c2018-09-10 21:26:07 +01002594 An address field extension permits to use the connect() function to connect to
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002595 other stream than TCP. The syntax containing a simpleipv4 or ipv6 address is
2596 the basically expected format. This format requires the port.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002597
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002598 Other format accepted are a socket path like "/socket/path", it permits to
Bertrand Jacquin874a35c2018-09-10 21:26:07 +01002599 connect to a socket. Abstract namespaces are supported with the prefix
Joseph Herlant02cedc42018-11-13 19:45:17 -08002600 "abns@", and finally a file descriptor can be passed with the prefix "fd@".
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002601 The prefix "ipv4@", "ipv6@" and "unix@" are also supported. The port can be
Bertrand Jacquin874a35c2018-09-10 21:26:07 +01002602 passed int the string. The syntax "127.0.0.1:1234" is valid. In this case, the
Tim Duesterhus6edab862018-01-06 19:04:45 +01002603 parameter *port* must not be set.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002604
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002605.. js:function:: Socket.connect_ssl(socket, address, port)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002606
2607 Same behavior than the function socket:connect, but uses SSL.
2608
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002609 :param class_socket socket: Is the manipulated Socket.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002610 :returns: 1 or nil.
2611
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002612.. js:function:: Socket.getpeername(socket)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002613
2614 Returns information about the remote side of a connected client object.
2615
2616 Returns a string with the IP address of the peer, followed by the port number
2617 that peer is using for the connection. In case of error, the method returns
2618 nil.
2619
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002620 :param class_socket socket: Is the manipulated Socket.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002621 :returns: a string containing the server information.
2622
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002623.. js:function:: Socket.getsockname(socket)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002624
2625 Returns the local address information associated to the object.
2626
2627 The method returns a string with local IP address and a number with the port.
2628 In case of error, the method returns nil.
2629
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002630 :param class_socket socket: Is the manipulated Socket.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002631 :returns: a string containing the client information.
2632
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002633.. js:function:: Socket.receive(socket, [pattern [, prefix]])
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002634
2635 Reads data from a client object, according to the specified read pattern.
2636 Patterns follow the Lua file I/O format, and the difference in performance
2637 between all patterns is negligible.
2638
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002639 :param class_socket socket: Is the manipulated Socket.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002640 :param string|integer pattern: Describe what is required (see below).
2641 :param string prefix: A string which will be prefix the returned data.
2642 :returns: a string containing the required data or nil.
2643
2644 Pattern can be any of the following:
2645
2646 * **`*a`**: reads from the socket until the connection is closed. No
2647 end-of-line translation is performed;
2648
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002649 * **`*l`**: reads a line of text from the Socket. The line is terminated by a
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002650 LF character (ASCII 10), optionally preceded by a CR character
2651 (ASCII 13). The CR and LF characters are not included in the
2652 returned line. In fact, all CR characters are ignored by the
2653 pattern. This is the default pattern.
2654
2655 * **number**: causes the method to read a specified number of bytes from the
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002656 Socket. Prefix is an optional string to be concatenated to the
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002657 beginning of any received data before return.
2658
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002659 * **empty**: If the pattern is left empty, the default option is `*l`.
2660
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002661 If successful, the method returns the received pattern. In case of error, the
2662 method returns nil followed by an error message which can be the string
2663 'closed' in case the connection was closed before the transmission was
2664 completed or the string 'timeout' in case there was a timeout during the
2665 operation. Also, after the error message, the function returns the partial
2666 result of the transmission.
2667
2668 Important note: This function was changed severely. It used to support
2669 multiple patterns (but I have never seen this feature used) and now it
2670 doesn't anymore. Partial results used to be returned in the same way as
2671 successful results. This last feature violated the idea that all functions
2672 should return nil on error. Thus it was changed too.
2673
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002674.. js:function:: Socket.send(socket, data [, start [, end ]])
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002675
2676 Sends data through client object.
2677
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002678 :param class_socket socket: Is the manipulated Socket.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002679 :param string data: The data that will be sent.
2680 :param integer start: The start position in the buffer of the data which will
2681 be sent.
2682 :param integer end: The end position in the buffer of the data which will
2683 be sent.
2684 :returns: see below.
2685
2686 Data is the string to be sent. The optional arguments i and j work exactly
2687 like the standard string.sub Lua function to allow the selection of a
2688 substring to be sent.
2689
2690 If successful, the method returns the index of the last byte within [start,
2691 end] that has been sent. Notice that, if start is 1 or absent, this is
2692 effectively the total number of bytes sent. In case of error, the method
2693 returns nil, followed by an error message, followed by the index of the last
2694 byte within [start, end] that has been sent. You might want to try again from
2695 the byte following that. The error message can be 'closed' in case the
2696 connection was closed before the transmission was completed or the string
2697 'timeout' in case there was a timeout during the operation.
2698
2699 Note: Output is not buffered. For small strings, it is always better to
2700 concatenate them in Lua (with the '..' operator) and send the result in one
2701 call instead of calling the method several times.
2702
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002703.. js:function:: Socket.setoption(socket, option [, value])
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002704
2705 Just implemented for compatibility, this cal does nothing.
2706
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002707.. js:function:: Socket.settimeout(socket, value [, mode])
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002708
2709 Changes the timeout values for the object. All I/O operations are blocking.
2710 That is, any call to the methods send, receive, and accept will block
2711 indefinitely, until the operation completes. The settimeout method defines a
2712 limit on the amount of time the I/O methods can block. When a timeout time
2713 has elapsed, the affected methods give up and fail with an error code.
2714
2715 The amount of time to wait is specified as the value parameter, in seconds.
2716
Mark Lakes56cc1252018-03-27 09:48:06 +02002717 The timeout modes are not implemented, the only settable timeout is the
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002718 inactivity time waiting for complete the internal buffer send or waiting for
2719 receive data.
2720
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002721 :param class_socket socket: Is the manipulated Socket.
Bertrand Jacquin874a35c2018-09-10 21:26:07 +01002722 :param float value: The timeout value. Use floating point to specify
Mark Lakes56cc1252018-03-27 09:48:06 +02002723 milliseconds.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002724
Thierry FOURNIER31904272017-10-25 12:59:51 +02002725.. _regex_class:
2726
2727Regex class
2728===========
2729
2730.. js:class:: Regex
2731
2732 This class allows the usage of HAProxy regexes because classic lua doesn't
2733 provides regexes. This class inherits the HAProxy compilation options, so the
2734 regexes can be libc regex, pcre regex or pcre JIT regex.
2735
2736 The expression matching number is limited to 20 per regex. The only available
2737 option is case sensitive.
2738
2739 Because regexes compilation is a heavy process, it is better to define all
2740 your regexes in the **body context** and use it during the runtime.
2741
2742.. code-block:: lua
2743
2744 -- Create the regex
2745 st, regex = Regex.new("needle (..) (...)", true);
2746
2747 -- Check compilation errors
2748 if st == false then
2749 print "error: " .. regex
2750 end
2751
2752 -- Match the regexes
2753 print(regex:exec("Looking for a needle in the haystack")) -- true
2754 print(regex:exec("Lokking for a cat in the haystack")) -- false
2755
2756 -- Extract words
2757 st, list = regex:match("Looking for a needle in the haystack")
2758 print(st) -- true
2759 print(list[1]) -- needle in the
2760 print(list[2]) -- in
2761 print(list[3]) -- the
2762
2763.. js:function:: Regex.new(regex, case_sensitive)
2764
2765 Create and compile a regex.
2766
2767 :param string regex: The regular expression according with the libc or pcre
2768 standard
2769 :param boolean case_sensitive: Match is case sensitive or not.
2770 :returns: boolean status and :ref:`regex_class` or string containing fail reason.
2771
2772.. js:function:: Regex.exec(regex, str)
2773
2774 Execute the regex.
2775
2776 :param class_regex regex: A :ref:`regex_class` object.
2777 :param string str: The input string will be compared with the compiled regex.
2778 :returns: a boolean status according with the match result.
2779
2780.. js:function:: Regex.match(regex, str)
2781
2782 Execute the regex and return matched expressions.
2783
2784 :param class_map map: 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, and
2787 a table containing all the string matched in order of declaration.
2788
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002789.. _map_class:
2790
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002791Map class
2792=========
2793
2794.. js:class:: Map
2795
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02002796 This class permits to do some lookups in HAProxy maps. The declared maps can
Bertrand Jacquin874a35c2018-09-10 21:26:07 +01002797 be modified during the runtime through the HAProxy management socket.
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002798
2799.. code-block:: lua
2800
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002801 default = "usa"
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002802
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002803 -- Create and load map
Thierry FOURNIER4dc71972017-01-28 08:33:08 +01002804 geo = Map.new("geo.map", Map._ip);
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002805
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002806 -- Create new fetch that returns the user country
2807 core.register_fetches("country", function(txn)
2808 local src;
2809 local loc;
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002810
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002811 src = txn.f:fhdr("x-forwarded-for");
2812 if (src == nil) then
2813 src = txn.f:src()
2814 if (src == nil) then
2815 return default;
2816 end
2817 end
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002818
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002819 -- Perform lookup
2820 loc = geo:lookup(src);
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002821
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002822 if (loc == nil) then
2823 return default;
2824 end
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002825
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002826 return loc;
2827 end);
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002828
Thierry FOURNIER4dc71972017-01-28 08:33:08 +01002829.. js:attribute:: Map._int
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002830
2831 See the HAProxy configuration.txt file, chapter "Using ACLs and fetching
Ilya Shipitsin2075ca82020-03-06 23:22:22 +05002832 samples" and subchapter "ACL basics" to understand this pattern matching
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002833 method.
2834
Thierry FOURNIER4dc71972017-01-28 08:33:08 +01002835 Note that :js:attr:`Map.int` is also available for compatibility.
2836
2837.. js:attribute:: Map._ip
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.ip` is also available for compatibility.
2844
2845.. js:attribute:: Map._str
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.str` is also available for compatibility.
2852
2853.. js:attribute:: Map._beg
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.beg` is also available for compatibility.
2860
2861.. js:attribute:: Map._sub
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.sub` is also available for compatibility.
2868
2869.. js:attribute:: Map._dir
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.dir` is also available for compatibility.
2876
2877.. js:attribute:: Map._dom
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.dom` is also available for compatibility.
2884
2885.. js:attribute:: Map._end
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.. js:attribute:: Map._reg
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002892
2893 See the HAProxy configuration.txt file, chapter "Using ACLs and fetching
Ilya Shipitsin2075ca82020-03-06 23:22:22 +05002894 samples" and subchapter "ACL basics" to understand this pattern matching
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002895 method.
2896
Thierry FOURNIER4dc71972017-01-28 08:33:08 +01002897 Note that :js:attr:`Map.reg` is also available for compatibility.
2898
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002899
2900.. js:function:: Map.new(file, method)
2901
2902 Creates and load a map.
2903
2904 :param string file: Is the file containing the map.
2905 :param integer method: Is the map pattern matching method. See the attributes
2906 of the Map class.
2907 :returns: a class Map object.
Thierry FOURNIER4dc71972017-01-28 08:33:08 +01002908 :see: The Map attributes: :js:attr:`Map._int`, :js:attr:`Map._ip`,
2909 :js:attr:`Map._str`, :js:attr:`Map._beg`, :js:attr:`Map._sub`,
2910 :js:attr:`Map._dir`, :js:attr:`Map._dom`, :js:attr:`Map._end` and
2911 :js:attr:`Map._reg`.
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002912
2913.. js:function:: Map.lookup(map, str)
2914
2915 Perform a lookup in a map.
2916
2917 :param class_map map: Is the class Map object.
2918 :param string str: Is the string used as key.
2919 :returns: a string containing the result or nil if no match.
2920
2921.. js:function:: Map.slookup(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 empty string if no match.
2928
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002929.. _applethttp_class:
2930
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002931AppletHTTP class
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002932================
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002933
2934.. js:class:: AppletHTTP
2935
2936 This class is used with applets that requires the 'http' mode. The http applet
2937 can be registered with the *core.register_service()* function. They are used
2938 for processing an http request like a server in back of HAProxy.
2939
2940 This is an hello world sample code:
2941
2942.. code-block:: lua
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002943
Pieter Baauw4d7f7662015-11-08 16:38:08 +01002944 core.register_service("hello-world", "http", function(applet)
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002945 local response = "Hello World !"
2946 applet:set_status(200)
Pieter Baauw2dcb9bc2015-10-01 22:47:12 +02002947 applet:add_header("content-length", string.len(response))
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002948 applet:add_header("content-type", "text/plain")
Pieter Baauw2dcb9bc2015-10-01 22:47:12 +02002949 applet:start_response()
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002950 applet:send(response)
2951 end)
2952
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002953.. js:attribute:: AppletHTTP.c
2954
2955 :returns: A :ref:`converters_class`
2956
2957 This attribute contains a Converters class object.
2958
2959.. js:attribute:: AppletHTTP.sc
2960
2961 :returns: A :ref:`converters_class`
2962
2963 This attribute contains a Converters class object. The
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02002964 functions of this object always return a string.
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002965
2966.. js:attribute:: AppletHTTP.f
2967
2968 :returns: A :ref:`fetches_class`
2969
2970 This attribute contains a Fetches class object. Note that the
2971 applet execution place cannot access to a valid HAProxy core HTTP
Ilya Shipitsin2075ca82020-03-06 23:22:22 +05002972 transaction, so some sample fetches related to the HTTP dependent
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002973 values (hdr, path, ...) are not available.
2974
2975.. js:attribute:: AppletHTTP.sf
2976
2977 :returns: A :ref:`fetches_class`
2978
2979 This attribute contains a Fetches class object. The functions of
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02002980 this object always return a string. Note that the applet
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002981 execution place cannot access to a valid HAProxy core HTTP
Ilya Shipitsin2075ca82020-03-06 23:22:22 +05002982 transaction, so some sample fetches related to the HTTP dependent
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002983 values (hdr, path, ...) are not available.
2984
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01002985.. js:attribute:: AppletHTTP.method
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002986
2987 :returns: string
2988
2989 The attribute method returns a string containing the HTTP
2990 method.
2991
2992.. js:attribute:: AppletHTTP.version
2993
2994 :returns: string
2995
2996 The attribute version, returns a string containing the HTTP
2997 request version.
2998
2999.. js:attribute:: AppletHTTP.path
3000
3001 :returns: string
3002
3003 The attribute path returns a string containing the HTTP
3004 request path.
3005
3006.. js:attribute:: AppletHTTP.qs
3007
3008 :returns: string
3009
3010 The attribute qs returns a string containing the HTTP
3011 request query string.
3012
3013.. js:attribute:: AppletHTTP.length
3014
3015 :returns: integer
3016
3017 The attribute length returns an integer containing the HTTP
3018 body length.
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02003019
Thierry FOURNIER841475e2015-12-11 17:10:09 +01003020.. js:attribute:: AppletHTTP.headers
3021
Patrick Hemmerc6a1d712018-05-01 21:30:41 -04003022 :returns: table
Thierry FOURNIERdc595002015-12-21 11:13:52 +01003023
Patrick Hemmerc6a1d712018-05-01 21:30:41 -04003024 The attribute headers returns a table containing the HTTP
Thierry FOURNIERdc595002015-12-21 11:13:52 +01003025 headers. The header names are always in lower case. As the header name can be
3026 encountered more than once in each request, the value is indexed with 0 as
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02003027 first index value. The table has this form:
Thierry FOURNIERdc595002015-12-21 11:13:52 +01003028
3029.. code-block:: lua
3030
3031 AppletHTTP.headers['<header-name>'][<header-index>] = "<header-value>"
3032
3033 AppletHTTP.headers["host"][0] = "www.test.com"
3034 AppletHTTP.headers["accept"][0] = "audio/basic q=1"
3035 AppletHTTP.headers["accept"][1] = "audio/*, q=0.2"
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01003036 AppletHTTP.headers["accept"][2] = "*/*, q=0.1"
Thierry FOURNIERdc595002015-12-21 11:13:52 +01003037..
3038
Robin H. Johnson52f5db22017-01-01 13:10:52 -08003039.. js:function:: AppletHTTP.set_status(applet, code [, reason])
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02003040
3041 This function sets the HTTP status code for the response. The allowed code are
3042 from 100 to 599.
3043
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01003044 :param class_AppletHTTP applet: An :ref:`applethttp_class`
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02003045 :param integer code: the status code returned to the client.
Robin H. Johnson52f5db22017-01-01 13:10:52 -08003046 :param string reason: the status reason returned to the client (optional).
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02003047
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01003048.. js:function:: AppletHTTP.add_header(applet, name, value)
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02003049
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02003050 This function adds a header in the response. Duplicated headers are not
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02003051 collapsed. The special header *content-length* is used to determinate the
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02003052 response length. If it does not exist, a *transfer-encoding: chunked* is set, and
Ilya Shipitsin2075ca82020-03-06 23:22:22 +05003053 all the write from the function *AppletHTTP:send()* become a chunk.
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02003054
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01003055 :param class_AppletHTTP applet: An :ref:`applethttp_class`
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02003056 :param string name: the header name
3057 :param string value: the header value
3058
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01003059.. js:function:: AppletHTTP.start_response(applet)
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02003060
3061 This function indicates to the HTTP engine that it can process and send the
3062 response headers. After this called we cannot add headers to the response; We
3063 cannot use the *AppletHTTP:send()* function if the
3064 *AppletHTTP:start_response()* is not called.
3065
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01003066 :param class_AppletHTTP applet: An :ref:`applethttp_class`
3067
3068.. js:function:: AppletHTTP.getline(applet)
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02003069
3070 This function returns a string containing one line from the http body. If the
3071 data returned doesn't contains a final '\\n' its assumed than its the last
3072 available data before the end of stream.
3073
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01003074 :param class_AppletHTTP applet: An :ref:`applethttp_class`
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02003075 :returns: a string. The string can be empty if we reach the end of the stream.
3076
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01003077.. js:function:: AppletHTTP.receive(applet, [size])
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02003078
3079 Reads data from the HTTP body, according to the specified read *size*. If the
3080 *size* is missing, the function tries to read all the content of the stream
3081 until the end. If the *size* is bigger than the http body, it returns the
Bertrand Jacquin874a35c2018-09-10 21:26:07 +01003082 amount of data available.
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02003083
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01003084 :param class_AppletHTTP applet: An :ref:`applethttp_class`
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02003085 :param integer size: the required read size.
Ilya Shipitsin11057a32020-06-21 21:18:27 +05003086 :returns: always return a string,the string can be empty is the connection is
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02003087 closed.
3088
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01003089.. js:function:: AppletHTTP.send(applet, msg)
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02003090
3091 Send the message *msg* on the http request body.
3092
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01003093 :param class_AppletHTTP applet: An :ref:`applethttp_class`
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02003094 :param string msg: the message to send.
3095
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01003096.. js:function:: AppletHTTP.get_priv(applet)
3097
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01003098 Return Lua data stored in the current transaction. If no data are stored,
3099 it returns a nil value.
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01003100
3101 :param class_AppletHTTP applet: An :ref:`applethttp_class`
Bertrand Jacquin874a35c2018-09-10 21:26:07 +01003102 :returns: the opaque data previously stored, or nil if nothing is
3103 available.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01003104 :see: :js:func:`AppletHTTP.set_priv`
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01003105
3106.. js:function:: AppletHTTP.set_priv(applet, data)
3107
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02003108 Store any data in the current HAProxy transaction. This action replaces the
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01003109 old stored data.
3110
3111 :param class_AppletHTTP applet: An :ref:`applethttp_class`
3112 :param opaque data: The data which is stored in the transaction.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01003113 :see: :js:func:`AppletHTTP.get_priv`
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01003114
Tim Duesterhus4e172c92020-05-19 13:49:42 +02003115.. js:function:: AppletHTTP.set_var(applet, var, value[, ifexist])
Thierry FOURNIER / OZON.IOc1edafe2016-12-12 16:25:30 +01003116
3117 Converts a Lua type in a HAProxy type and store it in a variable <var>.
3118
3119 :param class_AppletHTTP applet: An :ref:`applethttp_class`
3120 :param string var: The variable name according with the HAProxy variable syntax.
3121 :param type value: The value associated to the variable. The type ca be string or
3122 integer.
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02003123 :param boolean ifexist: If this parameter is set to true the variable
Tim Duesterhus4e172c92020-05-19 13:49:42 +02003124 will only be set if it was defined elsewhere (i.e. used
Willy Tarreau7978c5c2021-09-07 14:24:07 +02003125 within the configuration). For global variables (using the
3126 "proc" scope), they will only be updated and never created.
Aurelien DARRAGON21f7ebb2023-03-13 19:49:31 +01003127 It is highly recommended to always set this to true.
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02003128
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01003129 :see: :js:func:`AppletHTTP.unset_var`
3130 :see: :js:func:`AppletHTTP.get_var`
Thierry FOURNIER / OZON.IOc1edafe2016-12-12 16:25:30 +01003131
3132.. js:function:: AppletHTTP.unset_var(applet, var)
3133
3134 Unset the variable <var>.
3135
3136 :param class_AppletHTTP applet: An :ref:`applethttp_class`
3137 :param string var: The variable name according with the HAProxy variable syntax.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01003138 :see: :js:func:`AppletHTTP.set_var`
3139 :see: :js:func:`AppletHTTP.get_var`
Thierry FOURNIER / OZON.IOc1edafe2016-12-12 16:25:30 +01003140
3141.. js:function:: AppletHTTP.get_var(applet, var)
3142
3143 Returns data stored in the variable <var> converter in Lua type.
3144
3145 :param class_AppletHTTP applet: An :ref:`applethttp_class`
3146 :param string var: The variable name according with the HAProxy variable syntax.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01003147 :see: :js:func:`AppletHTTP.set_var`
3148 :see: :js:func:`AppletHTTP.unset_var`
Thierry FOURNIER / OZON.IOc1edafe2016-12-12 16:25:30 +01003149
Thierry FOURNIERdc595002015-12-21 11:13:52 +01003150.. _applettcp_class:
3151
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02003152AppletTCP class
3153===============
3154
3155.. js:class:: AppletTCP
3156
3157 This class is used with applets that requires the 'tcp' mode. The tcp applet
3158 can be registered with the *core.register_service()* function. They are used
3159 for processing a tcp stream like a server in back of HAProxy.
3160
Thierry FOURNIERdc595002015-12-21 11:13:52 +01003161.. js:attribute:: AppletTCP.c
3162
3163 :returns: A :ref:`converters_class`
3164
3165 This attribute contains a Converters class object.
3166
3167.. js:attribute:: AppletTCP.sc
3168
3169 :returns: A :ref:`converters_class`
3170
3171 This attribute contains a Converters class object. The
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02003172 functions of this object always return a string.
Thierry FOURNIERdc595002015-12-21 11:13:52 +01003173
3174.. js:attribute:: AppletTCP.f
3175
3176 :returns: A :ref:`fetches_class`
3177
3178 This attribute contains a Fetches class object.
3179
3180.. js:attribute:: AppletTCP.sf
3181
3182 :returns: A :ref:`fetches_class`
3183
3184 This attribute contains a Fetches class object.
3185
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01003186.. js:function:: AppletTCP.getline(applet)
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02003187
3188 This function returns a string containing one line from the stream. If the
3189 data returned doesn't contains a final '\\n' its assumed than its the last
3190 available data before the end of stream.
3191
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01003192 :param class_AppletTCP applet: An :ref:`applettcp_class`
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02003193 :returns: a string. The string can be empty if we reach the end of the stream.
3194
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01003195.. js:function:: AppletTCP.receive(applet, [size])
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02003196
3197 Reads data from the TCP stream, according to the specified read *size*. If the
3198 *size* is missing, the function tries to read all the content of the stream
3199 until the end.
3200
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01003201 :param class_AppletTCP applet: An :ref:`applettcp_class`
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02003202 :param integer size: the required read size.
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02003203 :returns: always return a string, the string can be empty if the connection is
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02003204 closed.
3205
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01003206.. js:function:: AppletTCP.send(appletmsg)
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02003207
3208 Send the message on the stream.
3209
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01003210 :param class_AppletTCP applet: An :ref:`applettcp_class`
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02003211 :param string msg: the message to send.
3212
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01003213.. js:function:: AppletTCP.get_priv(applet)
3214
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01003215 Return Lua data stored in the current transaction. If no data are stored,
3216 it returns a nil value.
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01003217
3218 :param class_AppletTCP applet: An :ref:`applettcp_class`
Bertrand Jacquin874a35c2018-09-10 21:26:07 +01003219 :returns: the opaque data previously stored, or nil if nothing is
3220 available.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01003221 :see: :js:func:`AppletTCP.set_priv`
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01003222
3223.. js:function:: AppletTCP.set_priv(applet, data)
3224
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02003225 Store any data in the current HAProxy transaction. This action replaces the
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01003226 old stored data.
3227
3228 :param class_AppletTCP applet: An :ref:`applettcp_class`
3229 :param opaque data: The data which is stored in the transaction.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01003230 :see: :js:func:`AppletTCP.get_priv`
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01003231
Tim Duesterhus4e172c92020-05-19 13:49:42 +02003232.. js:function:: AppletTCP.set_var(applet, var, value[, ifexist])
Thierry FOURNIER / OZON.IOc1edafe2016-12-12 16:25:30 +01003233
3234 Converts a Lua type in a HAProxy type and stores it in a variable <var>.
3235
3236 :param class_AppletTCP applet: An :ref:`applettcp_class`
3237 :param string var: The variable name according with the HAProxy variable syntax.
3238 :param type value: The value associated to the variable. The type can be string or
3239 integer.
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02003240 :param boolean ifexist: If this parameter is set to true the variable
Tim Duesterhus4e172c92020-05-19 13:49:42 +02003241 will only be set if it was defined elsewhere (i.e. used
Willy Tarreau7978c5c2021-09-07 14:24:07 +02003242 within the configuration). For global variables (using the
3243 "proc" scope), they will only be updated and never created.
Aurelien DARRAGON21f7ebb2023-03-13 19:49:31 +01003244 It is highly recommended to always set this to true.
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02003245
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01003246 :see: :js:func:`AppletTCP.unset_var`
3247 :see: :js:func:`AppletTCP.get_var`
Thierry FOURNIER / OZON.IOc1edafe2016-12-12 16:25:30 +01003248
3249.. js:function:: AppletTCP.unset_var(applet, var)
3250
3251 Unsets the variable <var>.
3252
3253 :param class_AppletTCP applet: An :ref:`applettcp_class`
3254 :param string var: The variable name according with the HAProxy variable syntax.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01003255 :see: :js:func:`AppletTCP.unset_var`
3256 :see: :js:func:`AppletTCP.set_var`
Thierry FOURNIER / OZON.IOc1edafe2016-12-12 16:25:30 +01003257
3258.. js:function:: AppletTCP.get_var(applet, var)
3259
3260 Returns data stored in the variable <var> converter in Lua type.
3261
3262 :param class_AppletTCP applet: An :ref:`applettcp_class`
3263 :param string var: The variable name according with the HAProxy variable syntax.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01003264 :see: :js:func:`AppletTCP.unset_var`
3265 :see: :js:func:`AppletTCP.set_var`
Thierry FOURNIER / OZON.IOc1edafe2016-12-12 16:25:30 +01003266
Adis Nezirovic8878f8e2018-07-13 12:18:33 +02003267StickTable class
3268================
3269
3270.. js:class:: StickTable
3271
3272 **context**: task, action, sample-fetch
3273
3274 This class can be used to access the HAProxy stick tables from Lua.
3275
3276.. js:function:: StickTable.info()
3277
3278 Returns stick table attributes as a Lua table. See HAProxy documentation for
Ilya Shipitsin2272d8a2020-12-21 01:22:40 +05003279 "stick-table" for canonical info, or check out example below.
Adis Nezirovic8878f8e2018-07-13 12:18:33 +02003280
3281 :returns: Lua table
3282
3283 Assume our table has IPv4 key and gpc0 and conn_rate "columns":
3284
3285.. code-block:: lua
3286
3287 {
3288 expire=<int>, # Value in ms
3289 size=<int>, # Maximum table size
3290 used=<int>, # Actual number of entries in table
3291 data={ # Data columns, with types as key, and periods as values
3292 (-1 if type is not rate counter)
3293 conn_rate=<int>,
3294 gpc0=-1
3295 },
3296 length=<int>, # max string length for string table keys, key length
3297 # otherwise
3298 nopurge=<boolean>, # purge oldest entries when table is full
3299 type="ip" # can be "ip", "ipv6", "integer", "string", "binary"
3300 }
3301
3302.. js:function:: StickTable.lookup(key)
3303
3304 Returns stick table entry for given <key>
3305
3306 :param string key: Stick table key (IP addresses and strings are supported)
3307 :returns: Lua table
3308
3309.. js:function:: StickTable.dump([filter])
3310
3311 Returns all entries in stick table. An optional filter can be used
3312 to extract entries with specific data values. Filter is a table with valid
3313 comparison operators as keys followed by data type name and value pairs.
3314 Check out the HAProxy docs for "show table" for more details. For the
3315 reference, the supported operators are:
Aurelien DARRAGON21f7ebb2023-03-13 19:49:31 +01003316
Adis Nezirovic8878f8e2018-07-13 12:18:33 +02003317 "eq", "ne", "le", "lt", "ge", "gt"
3318
3319 For large tables, execution of this function can take a long time (for
3320 HAProxy standards). That's also true when filter is used, so take care and
3321 measure the impact.
3322
3323 :param table filter: Stick table filter
3324 :returns: Stick table entries (table)
3325
3326 See below for example filter, which contains 4 entries (or comparisons).
3327 (Maximum number of filter entries is 4, defined in the source code)
3328
3329.. code-block:: lua
3330
3331 local filter = {
3332 {"gpc0", "gt", 30}, {"gpc1", "gt", 20}}, {"conn_rate", "le", 10}
3333 }
3334
Christopher Faulet0f3c8902020-01-31 18:57:12 +01003335.. _action_class:
3336
3337Action class
3338=============
3339
3340.. js:class:: Act
3341
3342 **context**: action
3343
3344 This class contains all return codes an action may return. It is the lua
3345 equivalent to HAProxy "ACT_RET_*" code.
3346
3347.. code-block:: lua
3348
3349 core.register_action("deny", { "http-req" }, function (txn)
3350 return act.DENY
3351 end)
3352..
3353.. js:attribute:: act.CONTINUE
3354
3355 This attribute is an integer (0). It instructs HAProxy to continue the current
3356 ruleset processing on the message. It is the default return code for a lua
3357 action.
3358
3359 :returns: integer
3360
3361.. js:attribute:: act.STOP
3362
3363 This attribute is an integer (1). It instructs HAProxy to stop the current
3364 ruleset processing on the message.
3365
3366.. js:attribute:: act.YIELD
3367
3368 This attribute is an integer (2). It instructs HAProxy to temporarily pause
3369 the message processing. It will be resumed later on the same rule. The
3370 corresponding lua script is re-executed for the start.
3371
3372.. js:attribute:: act.ERROR
3373
3374 This attribute is an integer (3). It triggers an internal errors The message
3375 processing is stopped and the transaction is terminated. For HTTP streams, an
3376 HTTP 500 error is returned to the client.
3377
3378 :returns: integer
3379
3380.. js:attribute:: act.DONE
3381
3382 This attribute is an integer (4). It instructs HAProxy to stop the message
3383 processing.
3384
3385 :returns: integer
3386
3387.. js:attribute:: act.DENY
3388
3389 This attribute is an integer (5). It denies the current message. The message
3390 processing is stopped and the transaction is terminated. For HTTP streams, an
3391 HTTP 403 error is returned to the client if the deny is returned during the
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02003392 request analysis. During the response analysis, a HTTP 502 error is returned
Christopher Faulet0f3c8902020-01-31 18:57:12 +01003393 and the server response is discarded.
3394
3395 :returns: integer
3396
3397.. js:attribute:: act.ABORT
3398
3399 This attribute is an integer (6). It aborts the current message. The message
3400 processing is stopped and the transaction is terminated. For HTTP streams,
Willy Tarreau714f3452021-05-09 06:47:26 +02003401 HAProxy assumes a response was already sent to the client. From the Lua
Christopher Faulet0f3c8902020-01-31 18:57:12 +01003402 actions point of view, when this code is used, the transaction is terminated
3403 with no reply.
3404
3405 :returns: integer
3406
3407.. js:attribute:: act.INVALID
3408
3409 This attribute is an integer (7). It triggers an internal errors. The message
3410 processing is stopped and the transaction is terminated. For HTTP streams, an
3411 HTTP 400 error is returned to the client if the error is returned during the
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02003412 request analysis. During the response analysis, a HTTP 502 error is returned
Christopher Faulet0f3c8902020-01-31 18:57:12 +01003413 and the server response is discarded.
3414
3415 :returns: integer
Adis Nezirovic8878f8e2018-07-13 12:18:33 +02003416
Christopher Faulet2c2c2e32020-01-31 19:07:52 +01003417.. js:function:: act:wake_time(milliseconds)
3418
3419 **context**: action
3420
3421 Set the script pause timeout to the specified time, defined in
3422 milliseconds.
3423
3424 :param integer milliseconds: the required milliseconds.
3425
3426 This function may be used when a lua action returns `act.YIELD`, to force its
3427 wake-up at most after the specified number of milliseconds.
3428
Christopher Faulet5a2c6612021-08-15 20:35:25 +02003429.. _filter_class:
3430
3431Filter class
3432=============
3433
3434.. js:class:: filter
3435
3436 **context**: filter
3437
3438 This class contains return codes some filter callback functions may return. It
3439 also contains configuration flags and some helper functions. To understand how
3440 the filter API works, see `doc/internal/filters.txt` documentation.
3441
3442.. js:attribute:: filter.CONTINUE
3443
3444 This attribute is an integer (1). It may be returned by some filter callback
3445 functions to instruct this filtering step is finished for this filter.
3446
3447.. js:attribute:: filter.WAIT
3448
3449 This attribute is an integer (0). It may be returned by some filter callback
3450 functions to instruct the filtering must be paused, waiting for more data or
3451 for an external event depending on this filter.
3452
3453.. js:attribute:: filter.ERROR
3454
3455 This attribute is an integer (-1). It may be returned by some filter callback
3456 functions to trigger an error.
3457
3458.. js:attribute:: filter.FLT_CFG_FL_HTX
3459
3460 This attribute is a flag corresponding to the filter flag FLT_CFG_FL_HTX. When
3461 it is set for a filter, it means the filter is able to filter HTTP streams.
3462
3463.. js:function:: filter.register_data_filter(chn)
3464
3465 **context**: filter
3466
3467 Enable the data filtering on the channel **chn** for the current filter. It
Ilya Shipitsinff0f2782021-08-22 22:18:07 +05003468 may be called at any time from any callback functions proceeding the data
Christopher Faulet5a2c6612021-08-15 20:35:25 +02003469 analysis.
3470
3471 :param class_Channel chn: A :ref:`channel_class`.
3472
3473.. js:function:: filter.unregister_data_filter(chn)
3474
3475 **context**: filter
3476
3477 Disable the data filtering on the channel **chn** for the current filter. It
3478 may be called at any time from any callback functions.
3479
3480 :param class_Channel chn: A :ref:`channel_class`.
3481
3482.. js:function:: filter.wake_time(milliseconds)
3483
3484 **context**: filter
3485
3486 Set the script pause timeout to the specified time, defined in
3487 milliseconds.
3488
3489 :param integer milliseconds: the required milliseconds.
3490
3491 This function may be used from any lua filter callback function to force its
3492 wake-up at most after the specified number of milliseconds. Especially, when
3493 `filter.CONTINUE` is returned.
3494
3495
3496A filters is declared using :js:func:`core.register_filter()` function. The
3497provided class will be used to instantiate filters. It may define following
3498attributes:
3499
3500* id: The filter identifier. It is a string that identifies the filter and is
3501 optional.
3502
3503* flags: The filter flags. Only :js:attr:`filter.FLT_CFG_FL_HTX` may be set for now.
3504
3505Such filter class must also define all required callback functions in the
3506following list. Note that :js:func:`Filter.new()` must be defined otherwise the
Ilya Shipitsinff0f2782021-08-22 22:18:07 +05003507filter is ignored. Others are optional.
Christopher Faulet5a2c6612021-08-15 20:35:25 +02003508
3509* .. js:function:: FILTER.new()
3510
3511 Called to instantiate a new filter. This function must be defined.
3512
3513 :returns: a Lua object that will be used as filter instance for the current
3514 stream.
3515
3516* .. js:function:: FILTER.start_analyze(flt, txn, chn)
3517
3518 Called when the analysis starts on the channel **chn**.
3519
3520* .. js:function:: FILTER.end_analyze(flt, txn, chn)
3521
3522 Called when the analysis ends on the channel **chn**.
3523
3524* .. js:function:: FILTER.http_headers(flt, txn, http_msg)
3525
3526 Called just before the HTTP payload analysis and after any processing on the
3527 HTTP message **http_msg**. This callback functions is only called for HTTP
3528 streams.
3529
3530* .. js:function:: FILTER.http_payload(flt, txn, http_msg)
3531
3532 Called during the HTTP payload analysis on the HTTP message **http_msg**. This
3533 callback functions is only called for HTTP streams.
3534
3535* .. js:function:: FILTER.http_end(flt, txn, http_msg)
3536
3537 Called after the HTTP payload analysis on the HTTP message **http_msg**. This
3538 callback functions is only called for HTTP streams.
3539
3540* .. js:function:: FILTER.tcp_payload(flt, txn, chn)
3541
3542 Called during the TCP payload analysis on the channel **chn**.
3543
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02003544Here is a full example:
Christopher Faulet5a2c6612021-08-15 20:35:25 +02003545
3546.. code-block:: lua
3547
3548 Trace = {}
3549 Trace.id = "Lua trace filter"
3550 Trace.flags = filter.FLT_CFG_FL_HTX;
3551 Trace.__index = Trace
3552
3553 function Trace:new()
3554 local trace = {}
3555 setmetatable(trace, Trace)
3556 trace.req_len = 0
3557 trace.res_len = 0
3558 return trace
3559 end
3560
3561 function Trace:start_analyze(txn, chn)
3562 if chn:is_resp() then
3563 print("Start response analysis")
3564 else
3565 print("Start request analysis")
3566 end
3567 filter.register_data_filter(self, chn)
3568 end
3569
3570 function Trace:end_analyze(txn, chn)
3571 if chn:is_resp() then
3572 print("End response analysis: "..self.res_len.." bytes filtered")
3573 else
3574 print("End request analysis: "..self.req_len.." bytes filtered")
3575 end
3576 end
3577
3578 function Trace:http_headers(txn, http_msg)
3579 stline = http_msg:get_stline()
3580 if http_msg.channel:is_resp() then
3581 print("response:")
3582 print(stline.version.." "..stline.code.." "..stline.reason)
3583 else
3584 print("request:")
3585 print(stline.method.." "..stline.uri.." "..stline.version)
3586 end
3587
3588 for n, hdrs in pairs(http_msg:get_headers()) do
3589 for i,v in pairs(hdrs) do
3590 print(n..": "..v)
3591 end
3592 end
3593 return filter.CONTINUE
3594 end
3595
3596 function Trace:http_payload(txn, http_msg)
3597 body = http_msg:body(-20000)
3598 if http_msg.channel:is_resp() then
3599 self.res_len = self.res_len + body:len()
3600 else
3601 self.req_len = self.req_len + body:len()
3602 end
3603 end
3604
3605 core.register_filter("trace", Trace, function(trace, args)
3606 return trace
3607 end)
3608
3609..
3610
3611.. _httpmessage_class:
3612
3613HTTPMessage class
3614===================
3615
3616.. js:class:: HTTPMessage
3617
3618 **context**: filter
3619
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02003620 This class contains all functions to manipulate a HTTP message. For now, this
Christopher Faulet5a2c6612021-08-15 20:35:25 +02003621 class is only available from a filter context.
3622
3623.. js:function:: HTTPMessage.add_header(http_msg, name, value)
3624
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02003625 Appends a HTTP header field in the HTTP message **http_msg** whose name is
Christopher Faulet5a2c6612021-08-15 20:35:25 +02003626 specified in **name** and whose value is defined in **value**.
3627
3628 :param class_httpmessage http_msg: The manipulated HTTP message.
3629 :param string name: The header name.
3630 :param string value: The header value.
3631
3632.. js:function:: HTTPMessage.append(http_msg, string)
3633
3634 This function copies the string **string** at the end of incoming data of the
3635 HTTP message **http_msg**. The function returns the copied length on success
3636 or -1 if data cannot be copied.
3637
3638 Same that :js:func:`HTTPMessage.insert(http_msg, string, http_msg:input())`.
3639
3640 :param class_httpmessage http_msg: The manipulated HTTP message.
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02003641 :param string string: The data to copy at the end of incoming data.
Christopher Faulet5a2c6612021-08-15 20:35:25 +02003642 :returns: an integer containing the amount of bytes copied or -1.
3643
3644.. js:function:: HTTPMessage.body(http_msgl[, offset[, length]])
3645
3646 This function returns **length** bytes of incoming data from the HTTP message
3647 **http_msg**, starting at the offset **offset**. The data are not removed from
3648 the buffer.
3649
3650 By default, if no length is provided, all incoming data found, starting at the
3651 given offset, are returned. If **length** is set to -1, the function tries to
3652 retrieve a maximum of data. Because it is called in the filter context, it
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02003653 never yield. Not providing an offset is the same as setting it to 0. A
Ilya Shipitsinff0f2782021-08-22 22:18:07 +05003654 positive offset is relative to the beginning of incoming data of the
Christopher Faulet5a2c6612021-08-15 20:35:25 +02003655 http_message buffer while negative offset is relative to their end.
3656
3657 If there is no incoming data and the HTTP message can't receive more data, a 'nil'
3658 value is returned.
3659
3660 :param class_httpmessage http_msg: The manipulated HTTP message.
3661 :param integer offset: *optional* The offset in incoming data to start to get
3662 data. 0 by default. May be negative to be relative to
3663 the end of incoming data.
3664 :param integer length: *optional* The expected length of data to retrieve. All
3665 incoming data by default. May be set to -1 to get a
3666 maximum of data.
3667 :returns: a string containing the data found or nil.
3668
3669.. js:function:: HTTPMessage.eom(http_msg)
3670
3671 This function returns true if the end of message is reached for the HTTP
3672 message **http_msg**.
3673
3674 :param class_httpmessage http_msg: The manipulated HTTP message.
3675 :returns: an integer containing the amount of available bytes.
3676
3677.. js:function:: HTTPMessage.del_header(http_msg, name)
3678
3679 Removes all HTTP header fields in the HTTP message **http_msg** whose name is
3680 specified in **name**.
3681
3682 :param class_httpmessage http_msg: The manipulated http message.
3683 :param string name: The header name.
3684
3685.. js:function:: HTTPMessage.get_headers(http_msg)
3686
3687 Returns a table containing all the headers of the HTTP message **http_msg**.
3688
3689 :param class_httpmessage http_msg: The manipulated http message.
3690 :returns: table of headers.
3691
3692 This is the form of the returned table:
3693
3694.. code-block:: lua
3695
3696 http_msg:get_headers()['<header-name>'][<header-index>] = "<header-value>"
3697
3698 local hdr = http_msg:get_headers()
3699 hdr["host"][0] = "www.test.com"
3700 hdr["accept"][0] = "audio/basic q=1"
3701 hdr["accept"][1] = "audio/*, q=0.2"
3702 hdr["accept"][2] = "*.*, q=0.1"
3703..
3704
3705.. js:function:: HTTPMessage.get_stline(http_msg)
3706
3707 Returns a table containing the start-line of the HTTP message **http_msg**.
3708
3709 :param class_httpmessage http_msg: The manipulated http message.
3710 :returns: the start-line.
3711
3712 This is the form of the returned table:
3713
3714.. code-block:: lua
3715
3716 -- for the request :
3717 {"method" = string, "uri" = string, "version" = string}
3718
3719 -- for the response:
3720 {"version" = string, "code" = string, "reason" = string}
3721..
3722
3723.. js:function:: HTTPMessage.forward(http_msg, length)
3724
3725 This function forwards **length** bytes of data from the HTTP message
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02003726 **http_msg**. Because it is called in the filter context, it never yields. Only
Christopher Faulet5a2c6612021-08-15 20:35:25 +02003727 available incoming data may be forwarded, event if the requested length
3728 exceeds the available amount of incoming data. It returns the amount of data
3729 forwarded.
3730
3731 :param class_httpmessage http_msg: The manipulated HTTP message.
3732 :param integer int: The amount of data to forward.
3733
3734.. js:function:: HTTPMessage.input(http_msg)
3735
3736 This function returns the length of incoming data in the HTTP message
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02003737 **http_msg** from the filter point of view.
Christopher Faulet5a2c6612021-08-15 20:35:25 +02003738
3739 :param class_httpmessage http_msg: The manipulated HTTP message.
3740 :returns: an integer containing the amount of available bytes.
3741
3742.. js:function:: HTTPMessage.insert(http_msg, string[, offset])
3743
3744 This function copies the string **string** at the offset **offset** in
3745 incoming data of the HTTP message **http_msg**. The function returns the
3746 copied length on success or -1 if data cannot be copied.
3747
3748 By default, if no offset is provided, the string is copied in front of
Ilya Shipitsinff0f2782021-08-22 22:18:07 +05003749 incoming data. A positive offset is relative to the beginning of incoming data
Christopher Faulet5a2c6612021-08-15 20:35:25 +02003750 of the HTTP message while negative offset is relative to their end.
3751
3752 :param class_httpmessage http_msg: The manipulated HTTP message.
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02003753 :param string string: The data to copy into incoming data.
3754 :param integer offset: *optional* The offset in incoming data where to copy
Christopher Faulet5a2c6612021-08-15 20:35:25 +02003755 data. 0 by default. May be negative to be relative to
3756 the end of incoming data.
3757 :returns: an integer containing the amount of bytes copied or -1.
3758
3759.. js:function:: HTTPMessage.is_full(http_msg)
3760
3761 This function returns true if the HTTP message **http_msg** is full.
3762
3763 :param class_httpmessage http_msg: The manipulated HTTP message.
3764 :returns: a boolean
3765
3766.. js:function:: HTTPMessage.is_resp(http_msg)
3767
3768 This function returns true if the HTTP message **http_msg** is the response
3769 one.
3770
3771 :param class_httpmessage http_msg: The manipulated HTTP message.
3772 :returns: a boolean
3773
3774.. js:function:: HTTPMessage.may_recv(http_msg)
3775
3776 This function returns true if the HTTP message **http_msg** may still receive
3777 data.
3778
3779 :param class_httpmessage http_msg: The manipulated HTTP message.
3780 :returns: a boolean
3781
3782.. js:function:: HTTPMessage.output(http_msg)
3783
3784 This function returns the length of outgoing data of the HTTP message
3785 **http_msg**.
3786
3787 :param class_httpmessage http_msg: The manipulated HTTP message.
3788 :returns: an integer containing the amount of available bytes.
3789
3790.. js:function:: HTTPMessage.prepend(http_msg, string)
3791
3792 This function copies the string **string** in front of incoming data of the
3793 HTTP message **http_msg**. The function returns the copied length on success
3794 or -1 if data cannot be copied.
3795
3796 Same that :js:func:`HTTPMessage.insert(http_msg, string, 0)`.
3797
3798 :param class_httpmessage http_msg: The manipulated HTTP message.
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02003799 :param string string: The data to copy in front of incoming data.
Christopher Faulet5a2c6612021-08-15 20:35:25 +02003800 :returns: an integer containing the amount of bytes copied or -1.
3801
3802.. js:function:: HTTPMessage.remove(http_msg[, offset[, length]])
3803
3804 This function removes **length** bytes of incoming data of the HTTP message
3805 **http_msg**, starting at offset **offset**. This function returns number of
3806 bytes removed on success.
3807
3808 By default, if no length is provided, all incoming data, starting at the given
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02003809 offset, are removed. Not providing an offset is the same that setting it
Ilya Shipitsinff0f2782021-08-22 22:18:07 +05003810 to 0. A positive offset is relative to the beginning of incoming data of the
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02003811 HTTP message while negative offset is relative to the end.
Christopher Faulet5a2c6612021-08-15 20:35:25 +02003812
3813 :param class_httpmessage http_msg: The manipulated HTTP message.
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02003814 :param integer offset: *optional* The offset in incoming data where to start
Christopher Faulet5a2c6612021-08-15 20:35:25 +02003815 to remove data. 0 by default. May be negative to
3816 be relative to the end of incoming data.
3817 :param integer length: *optional* The length of data to remove. All incoming
3818 data by default.
3819 :returns: an integer containing the amount of bytes removed.
3820
3821.. js:function:: HTTPMessage.rep_header(http_msg, name, regex, replace)
3822
3823 Matches the regular expression in all occurrences of header field **name**
3824 according to regex **regex**, and replaces them with the string **replace**.
3825 The replacement value can contain back references like \1, \2, ... This
3826 function acts on whole header lines, regardless of the number of values they
3827 may contain.
3828
3829 :param class_httpmessage http_msg: The manipulated HTTP message.
3830 :param string name: The header name.
3831 :param string regex: The match regular expression.
3832 :param string replace: The replacement value.
3833
3834.. js:function:: HTTPMessage.rep_value(http_msg, name, regex, replace)
3835
3836 Matches the regular expression on every comma-delimited value of header field
3837 **name** according to regex **regex**, and replaces them with the string
3838 **replace**. The replacement value can contain back references like \1, \2,
3839 ...
3840
3841 :param class_httpmessage http_msg: The manipulated HTTP message.
3842 :param string name: The header name.
3843 :param string regex: The match regular expression.
3844 :param string replace: The replacement value.
3845
3846.. js:function:: HTTPMessage.send(http_msg, string)
3847
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02003848 This function requires immediate send of the string **string**. It means the
Ilya Shipitsinff0f2782021-08-22 22:18:07 +05003849 string is copied at the beginning of incoming data of the HTTP message
Christopher Faulet5a2c6612021-08-15 20:35:25 +02003850 **http_msg** and immediately forwarded. Because it is called in the filter
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02003851 context, it never yields.
Christopher Faulet5a2c6612021-08-15 20:35:25 +02003852
3853 :param class_httpmessage http_msg: The manipulated HTTP message.
3854 :param string string: The data to send.
3855 :returns: an integer containing the amount of bytes copied or -1.
3856
3857.. js:function:: HTTPMessage.set(http_msg, string[, offset[, length]])
3858
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02003859 This function replaces **length** bytes of incoming data of the HTTP message
Christopher Faulet5a2c6612021-08-15 20:35:25 +02003860 **http_msg**, starting at offset **offset**, by the string **string**. The
3861 function returns the copied length on success or -1 if data cannot be copied.
3862
3863 By default, if no length is provided, all incoming data, starting at the given
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02003864 offset, are replaced. Not providing an offset is the same as setting it
Ilya Shipitsinff0f2782021-08-22 22:18:07 +05003865 to 0. A positive offset is relative to the beginning of incoming data of the
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02003866 HTTP message while negative offset is relative to the end.
Christopher Faulet5a2c6612021-08-15 20:35:25 +02003867
3868 :param class_httpmessage http_msg: The manipulated HTTP message.
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02003869 :param string string: The data to copy into incoming data.
3870 :param integer offset: *optional* The offset in incoming data where to start
Christopher Faulet5a2c6612021-08-15 20:35:25 +02003871 the data replacement. 0 by default. May be negative to
3872 be relative to the end of incoming data.
3873 :param integer length: *optional* The length of data to replace. All incoming
3874 data by default.
3875 :returns: an integer containing the amount of bytes copied or -1.
3876
3877.. js:function:: HTTPMessage.set_eom(http_msg)
3878
3879 This function set the end of message for the HTTP message **http_msg**.
3880
3881 :param class_httpmessage http_msg: The manipulated HTTP message.
3882
3883.. js:function:: HTTPMessage.set_header(http_msg, name, value)
3884
3885 This variable replace all occurrence of all header matching the name **name**,
3886 by only one containing the value **value**.
3887
3888 :param class_httpmessage http_msg: The manipulated HTTP message.
3889 :param string name: The header name.
3890 :param string value: The header value.
3891
3892 This function does the same work as the following code:
3893
3894.. code-block:: lua
3895
3896 http_msg:del_header("header")
3897 http_msg:add_header("header", "value")
3898..
3899
3900.. js:function:: HTTPMessage.set_method(http_msg, method)
3901
3902 Rewrites the request method with the string **method**. The HTTP message
3903 **http_msg** must be the request.
3904
3905 :param class_httpmessage http_msg: The manipulated HTTP message.
3906 :param string method: The new method.
3907
3908.. js:function:: HTTPMessage.set_path(http_msg, path)
3909
3910 Rewrites the request path with the string **path**. 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_query(http_msg, query)
3917
3918 Rewrites the request's query string which appears after the first question
3919 mark ("?") with the string **query**. The HTTP message **http_msg** must be
3920 the request.
3921
3922 :param class_httpmessage http_msg: The manipulated HTTP message.
3923 :param string query: The new query.
3924
3925.. js:function:: HTTPMessage.set_status(http_msg, status[, reason])
3926
Ilya Shipitsinff0f2782021-08-22 22:18:07 +05003927 Rewrites the response status code with the integer **code** and optional the
Christopher Faulet5a2c6612021-08-15 20:35:25 +02003928 reason **reason**. If no custom reason is provided, it will be generated from
3929 the status. The HTTP message **http_msg** must be the response.
3930
3931 :param class_httpmessage http_msg: The manipulated HTTP message.
3932 :param integer status: The new response status code.
3933 :param string reason: The new response reason (optional).
3934
3935.. js:function:: HTTPMessage.set_uri(http_msg, uri)
3936
3937 Rewrites the request URI with the string **uri**. The HTTP message
3938 **http_msg** must be the request.
3939
3940 :param class_httpmessage http_msg: The manipulated HTTP message.
3941 :param string uri: The new uri.
3942
3943.. js:function:: HTTPMessage.unset_eom(http_msg)
3944
3945 This function remove the end of message for the HTTP message **http_msg**.
3946
3947 :param class_httpmessage http_msg: The manipulated HTTP message.
3948
William Lallemand10cea5c2022-03-30 16:02:43 +02003949.. _CertCache_class:
3950
3951CertCache class
3952================
3953
3954.. js:class:: CertCache
3955
3956 This class allows to update an SSL certificate file in the memory of the
3957 current HAProxy process. It will do the same as "set ssl cert" + "commit ssl
3958 cert" over the HAProxy CLI.
3959
3960.. js:function:: CertCache.set(certificate)
3961
3962 This function updates a certificate in memory.
3963
3964 :param table certificate: A table containing the fields to update.
3965 :param string certificate.filename: The mandatory filename of the certificate
3966 to update, it must already exist in memory.
3967 :param string certificate.crt: A certificate in the PEM format. It can also
3968 contain a private key.
3969 :param string certificate.key: A private key in the PEM format.
3970 :param string certificate.ocsp: An OCSP response in base64. (cf management.txt)
3971 :param string certificate.issuer: The certificate of the OCSP issuer.
3972 :param string certificate.sctl: An SCTL file.
3973
3974.. code-block:: lua
3975
3976 CertCache.set{filename="certs/localhost9994.pem.rsa", crt=crt}
3977
3978
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01003979External Lua libraries
3980======================
3981
3982A lot of useful lua libraries can be found here:
3983
Aurelien DARRAGON846fc7d2022-10-14 08:48:57 +02003984* Lua toolbox has been superseded by `https://luarocks.org/ <https://luarocks.org/>`_
3985 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 +01003986
Ilya Shipitsin2075ca82020-03-06 23:22:22 +05003987Redis client library:
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01003988
3989* `https://github.com/nrk/redis-lua <https://github.com/nrk/redis-lua>`_
3990
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02003991This is an example about the usage of the Redis library within HAProxy. Note that
3992each call to any function of this library can throw an error if the socket
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01003993connection fails.
3994
3995.. code-block:: lua
3996
3997 -- load the redis library
3998 local redis = require("redis");
3999
4000 function do_something(txn)
4001
4002 -- create and connect new tcp socket
4003 local tcp = core.tcp();
4004 tcp:settimeout(1);
4005 tcp:connect("127.0.0.1", 6379);
4006
4007 -- use the redis library with this new socket
4008 local client = redis.connect({socket=tcp});
4009 client:ping();
4010
4011 end
4012
4013OpenSSL:
4014
4015* `http://mkottman.github.io/luacrypto/index.html
4016 <http://mkottman.github.io/luacrypto/index.html>`_
4017
4018* `https://github.com/brunoos/luasec/wiki
4019 <https://github.com/brunoos/luasec/wiki>`_