blob: 24c52cf56699489beaa36b430f89bd1b9b19dbf8 [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.
927
928 List of available event types :
929
930 **SERVER** Family:
931
932 * **SERVER_ADD**: when a server is added
933 * **SERVER_DEL**: when a server is removed
934 * **SERVER_DOWN**: when a server state goes from UP to DOWN
935 * **SERVER_UP**: when a server state goes from DOWN to UP
936
937 .. Note::
938 You may also use **SERVER** in **event_types** to subscribe to all server events types at once.
939
940 The prototype of the Lua function used as argument is:
941
942.. code-block:: lua
943
944 function(event, event_data, sub)
945..
946
947 * **event** (*string*): the event type (one of the **event_types** you specified when subscribing)
948 * **event_data**: specific to each event family (For **SERVER** family, a :ref:`server_event_class` object)
949 * **sub**: class to manage the subscription from within the event (a :ref:`event_sub_class` object)
950
951 .. Warning::
952 The callback function will only be scheduled on the very same thread that
953 performed the subscription.
954
955 Moreover, each thread treats events sequentially. It means that if you have,
956 let's say SERVER_UP followed by a SERVER_DOWN in a short timelapse, then
957 the cb function will first be called with SERVER_UP, and once it's done
958 handling the event, the cb function will be called again with SERVER_DOWN.
959
960 This is to ensure event consistency when it comes to logging / triggering logic
961 from lua.
962
963 Your lua cb function may yield if needed, but you're pleased to process the
964 event as fast as possible to prevent the event queue from growing up, depending
965 on the event flow that is expected for the given subscription.
966
967 To prevent abuses, if the event queue for the current subscription goes over
968 a certain amount of unconsumed events, the subscription will pause itself
969 automatically for as long as it takes for your handler to catch up. This would
970 lead to events being missed, so an error will be reported in the logs to warn
971 you about that.
972 This is not something you want to let happen too often, it may indicate that
973 you subscribed to an event that is occurring too frequently or/and that your
974 callback function is too slow to keep up the pace and you should review it.
975
976 If you want to do some parallel processing because your callback functions are
977 slow: you might want to create subtasks from lua using
978 :js:func:`core.register_task()` from within your callback function to perform
979 the heavy job in a dedicated task and allow remaining events to be processed
980 more quickly.
981
Thierry Fournierf61aa632016-02-19 20:56:00 +0100982.. _proxy_class:
983
984Proxy class
985============
986
987.. js:class:: Proxy
988
989 This class provides a way for manipulating proxy and retrieving information
990 like statistics.
991
Thierry FOURNIER817e7592017-07-24 14:35:04 +0200992.. js:attribute:: Proxy.name
993
994 Contain the name of the proxy.
995
Aurelien DARRAGONc4b24372023-03-02 12:00:06 +0100996 .. warning::
997 This attribute is now deprecated and will eventually be removed.
998 Please use :js:func:`Proxy.get_name()` function instead.
999
Thierry Fournierb0467732022-10-07 12:07:24 +02001000.. js:function:: Proxy.get_name()
1001
1002 Returns the name of the proxy.
1003
Baptiste Assmann46c72552017-10-26 21:51:58 +02001004.. js:attribute:: Proxy.uuid
1005
1006 Contain the unique identifier of the proxy.
1007
Aurelien DARRAGONc4b24372023-03-02 12:00:06 +01001008 .. warning::
1009 This attribute is now deprecated and will eventually be removed.
1010 Please use :js:func:`Proxy.get_uuid()` function instead.
1011
Thierry Fournierb0467732022-10-07 12:07:24 +02001012.. js:function:: Proxy.get_uuid()
1013
1014 Returns the unique identifier of the proxy.
1015
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001016.. js:attribute:: Proxy.servers
1017
Patrick Hemmerc6a1d712018-05-01 21:30:41 -04001018 Contain a table with the attached servers. The table is indexed by server
1019 name, and each server entry is an object of type :ref:`server_class`.
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001020
Adis Nezirovic8878f8e2018-07-13 12:18:33 +02001021.. js:attribute:: Proxy.stktable
1022
1023 Contains a stick table object attached to the proxy.
1024
Thierry Fournierff480422016-02-25 08:36:46 +01001025.. js:attribute:: Proxy.listeners
1026
Patrick Hemmerc6a1d712018-05-01 21:30:41 -04001027 Contain a table with the attached listeners. The table is indexed by listener
1028 name, and each each listeners entry is an object of type
1029 :ref:`listener_class`.
Thierry Fournierff480422016-02-25 08:36:46 +01001030
Thierry Fournierf61aa632016-02-19 20:56:00 +01001031.. js:function:: Proxy.pause(px)
1032
1033 Pause the proxy. See the management socket documentation for more information.
1034
1035 :param class_proxy px: A :ref:`proxy_class` which indicates the manipulated
1036 proxy.
1037
1038.. js:function:: Proxy.resume(px)
1039
1040 Resume the proxy. See the management socket documentation for more
1041 information.
1042
1043 :param class_proxy px: A :ref:`proxy_class` which indicates the manipulated
1044 proxy.
1045
1046.. js:function:: Proxy.stop(px)
1047
1048 Stop the proxy. See the management socket documentation for more information.
1049
1050 :param class_proxy px: A :ref:`proxy_class` which indicates the manipulated
1051 proxy.
1052
1053.. js:function:: Proxy.shut_bcksess(px)
1054
1055 Kill the session attached to a backup server. See the management socket
1056 documentation for more information.
1057
1058 :param class_proxy px: A :ref:`proxy_class` which indicates the manipulated
1059 proxy.
1060
1061.. js:function:: Proxy.get_cap(px)
1062
1063 Returns a string describing the capabilities of the proxy.
1064
1065 :param class_proxy px: A :ref:`proxy_class` which indicates the manipulated
1066 proxy.
1067 :returns: a string "frontend", "backend", "proxy" or "ruleset".
1068
1069.. js:function:: Proxy.get_mode(px)
1070
1071 Returns a string describing the mode of the current proxy.
1072
1073 :param class_proxy px: A :ref:`proxy_class` which indicates the manipulated
1074 proxy.
1075 :returns: a string "tcp", "http", "health" or "unknown"
1076
1077.. js:function:: Proxy.get_stats(px)
1078
Bertrand Jacquin874a35c2018-09-10 21:26:07 +01001079 Returns a table containing the proxy statistics. The statistics returned are
Thierry Fournierf61aa632016-02-19 20:56:00 +01001080 not the same if the proxy is frontend or a backend.
1081
1082 :param class_proxy px: A :ref:`proxy_class` which indicates the manipulated
1083 proxy.
Patrick Hemmerc6a1d712018-05-01 21:30:41 -04001084 :returns: a key/value table containing stats
Thierry Fournierf61aa632016-02-19 20:56:00 +01001085
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001086.. _server_class:
1087
1088Server class
1089============
1090
Patrick Hemmerc6a1d712018-05-01 21:30:41 -04001091.. js:class:: Server
1092
1093 This class provides a way for manipulating servers and retrieving information.
1094
Patrick Hemmera62ae7e2018-04-29 14:23:48 -04001095.. js:attribute:: Server.name
1096
1097 Contain the name of the server.
1098
Aurelien DARRAGONc4b24372023-03-02 12:00:06 +01001099 .. warning::
1100 This attribute is now deprecated and will eventually be removed.
1101 Please use :js:func:`Server.get_name()` function instead.
1102
Thierry Fournierb0467732022-10-07 12:07:24 +02001103.. js:function:: Server.get_name(sv)
1104
1105 Returns the name of the server.
1106
Patrick Hemmera62ae7e2018-04-29 14:23:48 -04001107.. js:attribute:: Server.puid
1108
1109 Contain the proxy unique identifier of the server.
1110
Aurelien DARRAGONc4b24372023-03-02 12:00:06 +01001111 .. warning::
1112 This attribute is now deprecated and will eventually be removed.
1113 Please use :js:func:`Server.get_puid()` function instead.
1114
Thierry Fournierb0467732022-10-07 12:07:24 +02001115.. js:function:: Server.get_puid(sv)
1116
1117 Returns the proxy unique identifier of the server.
1118
Aurelien DARRAGON94ee6632023-03-10 15:11:27 +01001119.. js:function:: Server.get_rid(sv)
1120
1121 Returns the rid (revision ID) of the server.
1122 It is an unsigned integer that is set upon server creation. Value is derived
1123 from a global counter that starts at 0 and is incremented each time one or
1124 multiple server deletions are followed by a server addition (meaning that
1125 old name/id reuse could occur).
1126
1127 Combining server name/id with server rid yields a process-wide unique
1128 identifier.
1129
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001130.. js:function:: Server.is_draining(sv)
1131
Patrick Hemmerc6a1d712018-05-01 21:30:41 -04001132 Return true if the server is currently draining sticky connections.
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001133
1134 :param class_server sv: A :ref:`server_class` which indicates the manipulated
1135 server.
1136 :returns: a boolean
1137
Patrick Hemmer32d539f2018-04-29 14:25:46 -04001138.. js:function:: Server.set_maxconn(sv, weight)
1139
1140 Dynamically change the maximum connections of the server. See the management
1141 socket documentation for more information about the format of the string.
1142
1143 :param class_server sv: A :ref:`server_class` which indicates the manipulated
1144 server.
1145 :param string maxconn: A string describing the server maximum connections.
1146
1147.. js:function:: Server.get_maxconn(sv, weight)
1148
1149 This function returns an integer representing the server maximum connections.
1150
1151 :param class_server sv: A :ref:`server_class` which indicates the manipulated
1152 server.
1153 :returns: an integer.
1154
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001155.. js:function:: Server.set_weight(sv, weight)
1156
Patrick Hemmerc6a1d712018-05-01 21:30:41 -04001157 Dynamically change the weight of the server. See the management socket
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001158 documentation for more information about the format of the string.
1159
1160 :param class_server sv: A :ref:`server_class` which indicates the manipulated
1161 server.
1162 :param string weight: A string describing the server weight.
1163
1164.. js:function:: Server.get_weight(sv)
1165
Patrick Hemmerc6a1d712018-05-01 21:30:41 -04001166 This function returns an integer representing the server weight.
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001167
1168 :param class_server sv: A :ref:`server_class` which indicates the manipulated
1169 server.
1170 :returns: an integer.
1171
Joseph C. Sible49bbf522020-05-04 22:20:32 -04001172.. js:function:: Server.set_addr(sv, addr[, port])
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001173
Patrick Hemmerc6a1d712018-05-01 21:30:41 -04001174 Dynamically change the address of the server. See the management socket
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001175 documentation for more information about the format of the string.
1176
1177 :param class_server sv: A :ref:`server_class` which indicates the manipulated
1178 server.
Patrick Hemmerc6a1d712018-05-01 21:30:41 -04001179 :param string addr: A string describing the server address.
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001180
1181.. js:function:: Server.get_addr(sv)
1182
Patrick Hemmerc6a1d712018-05-01 21:30:41 -04001183 Returns a string describing the address of the server.
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001184
1185 :param class_server sv: A :ref:`server_class` which indicates the manipulated
1186 server.
1187 :returns: A string
1188
1189.. js:function:: Server.get_stats(sv)
1190
1191 Returns server statistics.
1192
1193 :param class_server sv: A :ref:`server_class` which indicates the manipulated
1194 server.
Patrick Hemmerc6a1d712018-05-01 21:30:41 -04001195 :returns: a key/value table containing stats
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001196
1197.. js:function:: Server.shut_sess(sv)
1198
1199 Shutdown all the sessions attached to the server. See the management socket
1200 documentation for more information about this function.
1201
1202 :param class_server sv: A :ref:`server_class` which indicates the manipulated
1203 server.
1204
1205.. js:function:: Server.set_drain(sv)
1206
1207 Drain sticky sessions. See the management socket documentation for more
1208 information about this function.
1209
1210 :param class_server sv: A :ref:`server_class` which indicates the manipulated
1211 server.
1212
1213.. js:function:: Server.set_maint(sv)
1214
1215 Set maintenance mode. See the management socket documentation for more
1216 information about this function.
1217
1218 :param class_server sv: A :ref:`server_class` which indicates the manipulated
1219 server.
1220
1221.. js:function:: Server.set_ready(sv)
1222
1223 Set normal mode. See the management socket documentation for more information
1224 about this function.
1225
1226 :param class_server sv: A :ref:`server_class` which indicates the manipulated
1227 server.
1228
1229.. js:function:: Server.check_enable(sv)
1230
1231 Enable health checks. See the management socket documentation for more
1232 information about this function.
1233
1234 :param class_server sv: A :ref:`server_class` which indicates the manipulated
1235 server.
1236
1237.. js:function:: Server.check_disable(sv)
1238
1239 Disable health checks. See the management socket documentation for more
1240 information about this function.
1241
1242 :param class_server sv: A :ref:`server_class` which indicates the manipulated
1243 server.
1244
1245.. js:function:: Server.check_force_up(sv)
1246
1247 Force health-check up. See the management socket documentation for more
1248 information about this function.
1249
1250 :param class_server sv: A :ref:`server_class` which indicates the manipulated
1251 server.
1252
1253.. js:function:: Server.check_force_nolb(sv)
1254
1255 Force health-check nolb mode. See the management socket documentation for more
1256 information about this function.
1257
1258 :param class_server sv: A :ref:`server_class` which indicates the manipulated
1259 server.
1260
1261.. js:function:: Server.check_force_down(sv)
1262
1263 Force health-check down. See the management socket documentation for more
1264 information about this function.
1265
1266 :param class_server sv: A :ref:`server_class` which indicates the manipulated
1267 server.
1268
1269.. js:function:: Server.agent_enable(sv)
1270
1271 Enable agent check. See the management socket documentation for more
1272 information about this function.
1273
1274 :param class_server sv: A :ref:`server_class` which indicates the manipulated
1275 server.
1276
1277.. js:function:: Server.agent_disable(sv)
1278
1279 Disable agent check. See the management socket documentation for more
1280 information about this function.
1281
1282 :param class_server sv: A :ref:`server_class` which indicates the manipulated
1283 server.
1284
1285.. js:function:: Server.agent_force_up(sv)
1286
1287 Force agent check up. See the management socket documentation for more
1288 information about this function.
1289
1290 :param class_server sv: A :ref:`server_class` which indicates the manipulated
1291 server.
1292
1293.. js:function:: Server.agent_force_down(sv)
1294
1295 Force agent check down. See the management socket documentation for more
1296 information about this function.
1297
1298 :param class_server sv: A :ref:`server_class` which indicates the manipulated
1299 server.
1300
Thierry Fournierff480422016-02-25 08:36:46 +01001301.. _listener_class:
1302
1303Listener class
1304==============
1305
1306.. js:function:: Listener.get_stats(ls)
1307
1308 Returns server statistics.
1309
1310 :param class_listener ls: A :ref:`listener_class` which indicates the
1311 manipulated listener.
Patrick Hemmerc6a1d712018-05-01 21:30:41 -04001312 :returns: a key/value table containing stats
Thierry Fournierff480422016-02-25 08:36:46 +01001313
Aurelien DARRAGONc84899c2023-02-20 18:18:59 +01001314.. _event_sub_class:
1315
1316EventSub class
1317==============
1318
1319.. js:function:: EventSub.unsub()
1320
1321 End the subscription, the callback function will not be called again.
1322
1323.. _server_event_class:
1324
1325ServerEvent class
1326=================
1327
1328.. js:attribute:: ServerEvent.name
1329
1330 Contains the name of the server.
1331
1332.. js:attribute:: ServerEvent.puid
1333
1334 Contains the proxy-unique uid of the server
1335
1336.. js:attribute:: ServerEvent.rid
1337
1338 Contains the revision ID of the server
1339
1340.. js:attribute:: ServerEvent.proxy_name
1341
1342 Contains the name of the proxy to which the server belongs
1343
1344.. js:attribute:: ServerEvent.reference
1345
1346 Reference to the live server (A :ref:`server_class`).
1347
1348 .. Warning::
1349 Not available if the server was removed in the meantime.
1350 (Will never be set for SERVER_DEL event since the server does not exist anymore)
1351
Thierry Fournier1de16592016-01-27 09:49:07 +01001352.. _concat_class:
1353
1354Concat class
1355============
1356
1357.. js:class:: Concat
1358
1359 This class provides a fast way for string concatenation. The way using native
1360 Lua concatenation like the code below is slow for some reasons.
1361
1362.. code-block:: lua
1363
1364 str = "string1"
1365 str = str .. ", string2"
1366 str = str .. ", string3"
1367..
1368
1369 For each concatenation, Lua:
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02001370 - allocates memory for the result,
1371 - catenates the two string copying the strings in the new memory block,
1372 - frees the old memory block containing the string which is no longer used.
1373
Thierry Fournier1de16592016-01-27 09:49:07 +01001374 This process does many memory move, allocation and free. In addition, the
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02001375 memory is not really freed, it is just marked as unused and waits for the
Thierry Fournier1de16592016-01-27 09:49:07 +01001376 garbage collector.
1377
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02001378 The Concat class provides an alternative way to concatenate strings. It uses
Thierry Fournier1de16592016-01-27 09:49:07 +01001379 the internal Lua mechanism (it does not allocate memory), but it doesn't copy
1380 the data more than once.
1381
1382 On my computer, the following loops spends 0.2s for the Concat method and
1383 18.5s for the pure Lua implementation. So, the Concat class is about 1000x
1384 faster than the embedded solution.
1385
1386.. code-block:: lua
1387
1388 for j = 1, 100 do
1389 c = core.concat()
1390 for i = 1, 20000 do
1391 c:add("#####")
1392 end
1393 end
1394..
1395
1396.. code-block:: lua
1397
1398 for j = 1, 100 do
1399 c = ""
1400 for i = 1, 20000 do
1401 c = c .. "#####"
1402 end
1403 end
1404..
1405
1406.. js:function:: Concat.add(concat, string)
1407
1408 This function adds a string to the current concatenated string.
1409
1410 :param class_concat concat: A :ref:`concat_class` which contains the currently
Ilya Shipitsin2075ca82020-03-06 23:22:22 +05001411 built string.
Bertrand Jacquin874a35c2018-09-10 21:26:07 +01001412 :param string string: A new string to concatenate to the current built
Thierry Fournier1de16592016-01-27 09:49:07 +01001413 string.
1414
1415.. js:function:: Concat.dump(concat)
1416
Bertrand Jacquin874a35c2018-09-10 21:26:07 +01001417 This function returns the concatenated string.
Thierry Fournier1de16592016-01-27 09:49:07 +01001418
1419 :param class_concat concat: A :ref:`concat_class` which contains the currently
Ilya Shipitsin2075ca82020-03-06 23:22:22 +05001420 built string.
Thierry Fournier1de16592016-01-27 09:49:07 +01001421 :returns: the concatenated string
1422
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001423.. _fetches_class:
1424
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001425Fetches class
1426=============
1427
1428.. js:class:: Fetches
1429
1430 This class contains a lot of internal HAProxy sample fetches. See the
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02001431 HAProxy "configuration.txt" documentation for more information.
1432 (chapters 7.3.2 to 7.3.6)
Thierry FOURNIER2e4893c2015-03-18 13:37:27 +01001433
Christopher Faulet1e9b1b62021-08-11 10:14:30 +02001434 .. warning::
1435 some sample fetches are not available in some context. These limitations
1436 are specified in this documentation when they're useful.
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001437
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01001438 :see: :js:attr:`TXN.f`
1439 :see: :js:attr:`TXN.sf`
Thierry FOURNIER2e4893c2015-03-18 13:37:27 +01001440
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02001441 Fetches are useful to:
Thierry FOURNIER2e4893c2015-03-18 13:37:27 +01001442
1443 * get system time,
1444 * get environment variable,
1445 * get random numbers,
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02001446 * know backend status like the number of users in queue or the number of
Thierry FOURNIER2e4893c2015-03-18 13:37:27 +01001447 connections established,
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02001448 * get client information like ip source or destination,
Thierry FOURNIER2e4893c2015-03-18 13:37:27 +01001449 * deal with stick tables,
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02001450 * fetch established SSL information,
1451 * fetch HTTP information like headers or method.
Thierry FOURNIER2e4893c2015-03-18 13:37:27 +01001452
1453.. code-block:: lua
1454
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001455 function action(txn)
1456 -- Get source IP
1457 local clientip = txn.f:src()
1458 end
Thierry FOURNIER2e4893c2015-03-18 13:37:27 +01001459..
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001460
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001461.. _converters_class:
1462
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001463Converters class
1464================
1465
1466.. js:class:: Converters
1467
1468 This class contains a lot of internal HAProxy sample converters. See the
Thierry FOURNIER2e4893c2015-03-18 13:37:27 +01001469 HAProxy documentation "configuration.txt" for more information about her
1470 usage. Its the chapter 7.3.1.
1471
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01001472 :see: :js:attr:`TXN.c`
1473 :see: :js:attr:`TXN.sc`
Thierry FOURNIER2e4893c2015-03-18 13:37:27 +01001474
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02001475 Converters provides stateful transformation. They are useful to:
Thierry FOURNIER2e4893c2015-03-18 13:37:27 +01001476
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02001477 * convert input to base64,
1478 * apply hash on input string (djb2, crc32, sdbm, wt6),
Thierry FOURNIER2e4893c2015-03-18 13:37:27 +01001479 * format date,
1480 * json escape,
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02001481 * extract preferred language comparing two lists,
Thierry FOURNIER2e4893c2015-03-18 13:37:27 +01001482 * turn to lower or upper chars,
1483 * deal with stick tables.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001484
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001485.. _channel_class:
1486
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001487Channel class
1488=============
1489
1490.. js:class:: Channel
1491
Christopher Faulet5a2c6612021-08-15 20:35:25 +02001492 **context**: action, sample-fetch, convert, filter
1493
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001494 HAProxy uses two buffers for the processing of the requests. The first one is
1495 used with the request data (from the client to the server) and the second is
1496 used for the response data (from the server to the client).
1497
1498 Each buffer contains two types of data. The first type is the incoming data
1499 waiting for a processing. The second part is the outgoing data already
1500 processed. Usually, the incoming data is processed, after it is tagged as
1501 outgoing data, and finally it is sent. The following functions provides tools
1502 for manipulating these data in a buffer.
1503
1504 The following diagram shows where the channel class function are applied.
1505
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001506 .. image:: _static/channel.png
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001507
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001508 .. warning::
1509 It is not possible to read from the response in request action, and it is
Boyang Li60cfe8b2022-05-10 18:11:00 +00001510 not possible to read from the request channel in response action.
Christopher Faulet09530392021-06-14 11:43:18 +02001511
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001512 .. warning::
1513 It is forbidden to alter the Channels buffer from HTTP contexts. So only
1514 :js:func:`Channel.input`, :js:func:`Channel.output`,
1515 :js:func:`Channel.may_recv`, :js:func:`Channel.is_full` and
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02001516 :js:func:`Channel.is_resp` can be called from a HTTP context.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001517
Christopher Faulet5a2c6612021-08-15 20:35:25 +02001518 All the functions provided by this class are available in the
1519 **sample-fetches**, **actions** and **filters** contexts. For **filters**,
1520 incoming data (offset and length) are relative to the filter. Some functions
Boyang Li60cfe8b2022-05-10 18:11:00 +00001521 may yield, but only for **actions**. Yield is not possible for
Christopher Faulet5a2c6612021-08-15 20:35:25 +02001522 **sample-fetches**, **converters** and **filters**.
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001523
1524.. js:function:: Channel.append(channel, string)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001525
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001526 This function copies the string **string** at the end of incoming data of the
1527 channel buffer. The function returns the copied length on success or -1 if
1528 data cannot be copied.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001529
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001530 Same that :js:func:`Channel.insert(channel, string, channel:input())`.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001531
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001532 :param class_channel channel: The manipulated Channel.
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02001533 :param string string: The data to copy at the end of incoming data.
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001534 :returns: an integer containing the amount of bytes copied or -1.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001535
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001536.. js:function:: Channel.data(channel [, offset [, length]])
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001537
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001538 This function returns **length** bytes of incoming data from the channel
1539 buffer, starting at the offset **offset**. The data are not removed from the
1540 buffer.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001541
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001542 By default, if no length is provided, all incoming data found, starting at the
1543 given offset, are returned. If **length** is set to -1, the function tries to
Christopher Faulet5a2c6612021-08-15 20:35:25 +02001544 retrieve a maximum of data and, if called by an action, it yields if
1545 necessary. It also waits for more data if the requested length exceeds the
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02001546 available amount of incoming data. Not providing an offset is the same as
Ilya Shipitsinff0f2782021-08-22 22:18:07 +05001547 setting it to 0. A positive offset is relative to the beginning of incoming
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02001548 data of the channel buffer while negative offset is relative to the end.
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001549
1550 If there is no incoming data and the channel can't receive more data, a 'nil'
1551 value is returned.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001552
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001553 :param class_channel channel: The manipulated Channel.
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001554 :param integer offset: *optional* The offset in incoming data to start to get
1555 data. 0 by default. May be negative to be relative to
1556 the end of incoming data.
1557 :param integer length: *optional* The expected length of data to retrieve. All
1558 incoming data by default. May be set to -1 to get a
1559 maximum of data.
1560 :returns: a string containing the data found or nil.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001561
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001562.. js:function:: Channel.forward(channel, length)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001563
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001564 This function forwards **length** bytes of data from the channel buffer. If
Christopher Faulet5a2c6612021-08-15 20:35:25 +02001565 the requested length exceeds the available amount of incoming data, and if
1566 called by an action, the function yields, waiting for more data to forward. It
1567 returns the amount of data forwarded.
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001568
1569 :param class_channel channel: The manipulated Channel.
1570 :param integer int: The amount of data to forward.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001571
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001572.. js:function:: Channel.input(channel)
1573
Christopher Faulet5a2c6612021-08-15 20:35:25 +02001574 This function returns the length of incoming data in the channel buffer. When
1575 called by a filter, this value is relative to the filter.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001576
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001577 :param class_channel channel: The manipulated Channel.
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001578 :returns: an integer containing the amount of available bytes.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001579
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001580.. js:function:: Channel.insert(channel, string [, offset])
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001581
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001582 This function copies the string **string** at the offset **offset** in
1583 incoming data of the channel buffer. The function returns the copied length on
1584 success or -1 if data cannot be copied.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001585
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001586 By default, if no offset is provided, the string is copied in front of
Ilya Shipitsinff0f2782021-08-22 22:18:07 +05001587 incoming data. A positive offset is relative to the beginning of incoming data
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001588 of the channel buffer while negative offset is relative to their end.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001589
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001590 :param class_channel channel: The manipulated Channel.
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02001591 :param string string: The data to copy into incoming data.
1592 :param integer offset: *optional* The offset in incoming data where to copy
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001593 data. 0 by default. May be negative to be relative to
1594 the end of incoming data.
Pieter Baauw4d7f7662015-11-08 16:38:08 +01001595 :returns: an integer containing the amount of bytes copied or -1.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001596
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001597.. js:function:: Channel.is_full(channel)
1598
1599 This function returns true if the channel buffer is full.
1600
Christopher Faulet5a2c6612021-08-15 20:35:25 +02001601 :param class_channel channel: The manipulated Channel.
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001602 :returns: a boolean
1603
1604.. js:function:: Channel.is_resp(channel)
1605
1606 This function returns true if the channel is the response one.
1607
Christopher Faulet5a2c6612021-08-15 20:35:25 +02001608 :param class_channel channel: The manipulated Channel.
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001609 :returns: a boolean
1610
1611.. js:function:: Channel.line(channel [, offset [, length]])
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001612
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001613 This function parses **length** bytes of incoming data of the channel buffer,
1614 starting at offset **offset**, and returns the first line found, including the
Christopher Faulet5a2c6612021-08-15 20:35:25 +02001615 '\\n'. The data are not removed from the buffer. If no line is found, all data
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001616 are returned.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001617
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001618 By default, if no length is provided, all incoming data, starting at the given
1619 offset, are evaluated. If **length** is set to -1, the function tries to
Christopher Faulet5a2c6612021-08-15 20:35:25 +02001620 retrieve a maximum of data and, if called by an action, yields if
1621 necessary. It also waits for more data if the requested length exceeds the
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02001622 available amount of incoming data. Not providing an offset is the same as
Ilya Shipitsinff0f2782021-08-22 22:18:07 +05001623 setting it to 0. A positive offset is relative to the beginning of incoming
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02001624 data of the channel buffer while negative offset is relative to the end.
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001625
1626 If there is no incoming data and the channel can't receive more data, a 'nil'
1627 value is returned.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001628
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001629 :param class_channel channel: The manipulated Channel.
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02001630 :param integer offset: *optional* The offset in incoming data to start to
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001631 parse data. 0 by default. May be negative to be
1632 relative to the end of incoming data.
1633 :param integer length: *optional* The length of data to parse. All incoming
1634 data by default. May be set to -1 to get a maximum of
1635 data.
1636 :returns: a string containing the line found or nil.
1637
1638.. js:function:: Channel.may_recv(channel)
1639
1640 This function returns true if the channel may still receive data.
1641
Christopher Faulet5a2c6612021-08-15 20:35:25 +02001642 :param class_channel channel: The manipulated Channel.
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001643 :returns: a boolean
1644
1645.. js:function:: Channel.output(channel)
1646
Christopher Faulet5a2c6612021-08-15 20:35:25 +02001647 This function returns the length of outgoing data of the channel buffer. When
1648 called by a filter, this value is relative to the filter.
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001649
1650 :param class_channel channel: The manipulated Channel.
1651 :returns: an integer containing the amount of available bytes.
1652
1653.. js:function:: Channel.prepend(channel, string)
1654
1655 This function copies the string **string** in front of incoming data of the
1656 channel buffer. The function returns the copied length on success or -1 if
1657 data cannot be copied.
1658
1659 Same that :js:func:`Channel.insert(channel, string, 0)`.
1660
1661 :param class_channel channel: The manipulated Channel.
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02001662 :param string string: The data to copy in front of incoming data.
Pieter Baauw4d7f7662015-11-08 16:38:08 +01001663 :returns: an integer containing the amount of bytes copied or -1.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001664
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001665.. js:function:: Channel.remove(channel [, offset [, length]])
1666
1667 This function removes **length** bytes of incoming data of the channel buffer,
1668 starting at offset **offset**. This function returns number of bytes removed
1669 on success.
1670
1671 By default, if no length is provided, all incoming data, starting at the given
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02001672 offset, are removed. Not providing an offset is the same as setting it
Ilya Shipitsinff0f2782021-08-22 22:18:07 +05001673 to 0. A positive offset is relative to the beginning of incoming data of the
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02001674 channel buffer while negative offset is relative to the end.
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001675
1676 :param class_channel channel: The manipulated Channel.
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02001677 :param integer offset: *optional* The offset in incoming data where to start
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001678 to remove data. 0 by default. May be negative to
1679 be relative to the end of incoming data.
1680 :param integer length: *optional* The length of data to remove. All incoming
1681 data by default.
1682 :returns: an integer containing the amount of bytes removed.
1683
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001684.. js:function:: Channel.send(channel, string)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001685
Christopher Faulet5a2c6612021-08-15 20:35:25 +02001686 This function requires immediate send of the string **string**. It means the
Ilya Shipitsinff0f2782021-08-22 22:18:07 +05001687 string is copied at the beginning of incoming data of the channel buffer and
Christopher Faulet5a2c6612021-08-15 20:35:25 +02001688 immediately forwarded. Unless if the connection is close, and if called by an
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02001689 action, this function yields to copy and forward all the string.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001690
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001691 :param class_channel channel: The manipulated Channel.
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001692 :param string string: The data to send.
Pieter Baauw4d7f7662015-11-08 16:38:08 +01001693 :returns: an integer containing the amount of bytes copied or -1.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001694
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001695.. js:function:: Channel.set(channel, string [, offset [, length]])
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001696
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02001697 This function replaces **length** bytes of incoming data of the channel buffer,
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001698 starting at offset **offset**, by the string **string**. The function returns
1699 the copied length on success or -1 if data cannot be copied.
1700
1701 By default, if no length is provided, all incoming data, starting at the given
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02001702 offset, are replaced. Not providing an offset is the same as setting it
Ilya Shipitsinff0f2782021-08-22 22:18:07 +05001703 to 0. A positive offset is relative to the beginning of incoming data of the
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02001704 channel buffer while negative offset is relative to the end.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001705
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001706 :param class_channel channel: The manipulated Channel.
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02001707 :param string string: The data to copy into incoming data.
1708 :param integer offset: *optional* The offset in incoming data where to start
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001709 the data replacement. 0 by default. May be negative to
1710 be relative to the end of incoming data.
1711 :param integer length: *optional* The length of data to replace. All incoming
1712 data by default.
1713 :returns: an integer containing the amount of bytes copied or -1.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001714
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001715.. js:function:: Channel.dup(channel)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001716
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001717 **DEPRECATED**
1718
1719 This function returns all incoming data found in the channel buffer. The data
Boyang Li60cfe8b2022-05-10 18:11:00 +00001720 are not removed from the buffer and can be reprocessed later.
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001721
1722 If there is no incoming data and the channel can't receive more data, a 'nil'
1723 value is returned.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001724
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001725 :param class_channel channel: The manipulated Channel.
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001726 :returns: a string containing all data found or nil.
1727
1728 .. warning::
1729 This function is deprecated. :js:func:`Channel.data()` must be used
1730 instead.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001731
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001732.. js:function:: Channel.get(channel)
1733
1734 **DEPRECATED**
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001735
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001736 This function returns all incoming data found in the channel buffer and remove
1737 them from the buffer.
1738
1739 If there is no incoming data and the channel can't receive more data, a 'nil'
1740 value is returned.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001741
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001742 :param class_channel channel: The manipulated Channel.
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001743 :returns: a string containing all the data found or nil.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001744
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001745 .. warning::
1746 This function is deprecated. :js:func:`Channel.data()` must be used to
1747 retrieve data followed by a call to :js:func:`Channel:remove()` to remove
1748 data.
Thierry FOURNIER / OZON.IO65192f32016-11-07 15:28:40 +01001749
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001750 .. code-block:: lua
Thierry FOURNIER / OZON.IO65192f32016-11-07 15:28:40 +01001751
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001752 local data = chn:data()
1753 chn:remove(0, data:len())
1754
1755 ..
1756
1757.. js:function:: Channel.getline(channel)
1758
1759 **DEPRECATED**
1760
1761 This function returns the first line found in incoming data of the channel
Christopher Faulet5a2c6612021-08-15 20:35:25 +02001762 buffer, including the '\\n'. The returned data are removed from the buffer. If
1763 no line is found, and if called by an action, this function yields to wait for
1764 more data, except if the channel can't receive more data. In this case all
1765 data are returned.
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001766
1767 If there is no incoming data and the channel can't receive more data, a 'nil'
1768 value is returned.
1769
1770 :param class_channel channel: The manipulated Channel.
1771 :returns: a string containing the line found or nil.
1772
1773 .. warning::
Boyang Li60cfe8b2022-05-10 18:11:00 +00001774 This function is deprecated. :js:func:`Channel.line()` must be used to
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001775 retrieve a line followed by a call to :js:func:`Channel:remove()` to remove
1776 data.
1777
1778 .. code-block:: lua
1779
1780 local line = chn:line(0, -1)
1781 chn:remove(0, line:len())
1782
1783 ..
1784
1785.. js:function:: Channel.get_in_len(channel)
1786
Boyang Li60cfe8b2022-05-10 18:11:00 +00001787 **DEPRECATED**
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001788
Christopher Faulet5a2c6612021-08-15 20:35:25 +02001789 This function returns the length of the input part of the buffer. When called
1790 by a filter, this value is relative to the filter.
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001791
1792 :param class_channel channel: The manipulated Channel.
1793 :returns: an integer containing the amount of available bytes.
1794
1795 .. warning::
1796 This function is deprecated. :js:func:`Channel.input()` must be used
1797 instead.
1798
1799.. js:function:: Channel.get_out_len(channel)
1800
Boyang Li60cfe8b2022-05-10 18:11:00 +00001801 **DEPRECATED**
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001802
Christopher Faulet5a2c6612021-08-15 20:35:25 +02001803 This function returns the length of the output part of the buffer. When called
1804 by a filter, this value is relative to the filter.
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001805
1806 :param class_channel channel: The manipulated Channel.
1807 :returns: an integer containing the amount of available bytes.
1808
1809 .. warning::
1810 This function is deprecated. :js:func:`Channel.output()` must be used
1811 instead.
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001812
1813.. _http_class:
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001814
1815HTTP class
1816==========
1817
1818.. js:class:: HTTP
1819
1820 This class contain all the HTTP manipulation functions.
1821
Pieter Baauw386a1272015-08-16 15:26:24 +02001822.. js:function:: HTTP.req_get_headers(http)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001823
Patrick Hemmerc6a1d712018-05-01 21:30:41 -04001824 Returns a table containing all the request headers.
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001825
1826 :param class_http http: The related http object.
Patrick Hemmerc6a1d712018-05-01 21:30:41 -04001827 :returns: table of headers.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01001828 :see: :js:func:`HTTP.res_get_headers`
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001829
Patrick Hemmerc6a1d712018-05-01 21:30:41 -04001830 This is the form of the returned table:
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001831
1832.. code-block:: lua
1833
1834 HTTP:req_get_headers()['<header-name>'][<header-index>] = "<header-value>"
1835
1836 local hdr = HTTP:req_get_headers()
1837 hdr["host"][0] = "www.test.com"
1838 hdr["accept"][0] = "audio/basic q=1"
1839 hdr["accept"][1] = "audio/*, q=0.2"
1840 hdr["accept"][2] = "*/*, q=0.1"
1841..
1842
Pieter Baauw386a1272015-08-16 15:26:24 +02001843.. js:function:: HTTP.res_get_headers(http)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001844
Patrick Hemmerc6a1d712018-05-01 21:30:41 -04001845 Returns a table containing all the response headers.
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001846
1847 :param class_http http: The related http object.
Patrick Hemmerc6a1d712018-05-01 21:30:41 -04001848 :returns: table of headers.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01001849 :see: :js:func:`HTTP.req_get_headers`
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001850
Patrick Hemmerc6a1d712018-05-01 21:30:41 -04001851 This is the form of the returned table:
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001852
1853.. code-block:: lua
1854
1855 HTTP:res_get_headers()['<header-name>'][<header-index>] = "<header-value>"
1856
1857 local hdr = HTTP:req_get_headers()
1858 hdr["host"][0] = "www.test.com"
1859 hdr["accept"][0] = "audio/basic q=1"
1860 hdr["accept"][1] = "audio/*, q=0.2"
1861 hdr["accept"][2] = "*.*, q=0.1"
1862..
1863
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001864.. js:function:: HTTP.req_add_header(http, name, value)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001865
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02001866 Appends a HTTP header field in the request whose name is
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001867 specified in "name" and whose value is defined in "value".
1868
1869 :param class_http http: The related http object.
1870 :param string name: The header name.
1871 :param string value: The header value.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01001872 :see: :js:func:`HTTP.res_add_header`
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001873
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001874.. js:function:: HTTP.res_add_header(http, name, value)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001875
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02001876 Appends a HTTP header field in the response whose name is
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001877 specified in "name" and whose value is defined in "value".
1878
1879 :param class_http http: The related http object.
1880 :param string name: The header name.
1881 :param string value: The header value.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01001882 :see: :js:func:`HTTP.req_add_header`
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001883
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001884.. js:function:: HTTP.req_del_header(http, name)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001885
1886 Removes all HTTP header fields in the request whose name is
1887 specified in "name".
1888
1889 :param class_http http: The related http object.
1890 :param string name: The header name.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01001891 :see: :js:func:`HTTP.res_del_header`
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001892
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001893.. js:function:: HTTP.res_del_header(http, name)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001894
1895 Removes all HTTP header fields in the response whose name is
1896 specified in "name".
1897
1898 :param class_http http: The related http object.
1899 :param string name: The header name.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01001900 :see: :js:func:`HTTP.req_del_header`
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001901
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001902.. js:function:: HTTP.req_set_header(http, name, value)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001903
Bertrand Jacquin874a35c2018-09-10 21:26:07 +01001904 This variable replace all occurrence of all header "name", by only
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001905 one containing the "value".
1906
1907 :param class_http http: The related http object.
1908 :param string name: The header name.
1909 :param string value: The header value.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01001910 :see: :js:func:`HTTP.res_set_header`
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001911
Bertrand Jacquin874a35c2018-09-10 21:26:07 +01001912 This function does the same work as the following code:
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001913
1914.. code-block:: lua
1915
1916 function fcn(txn)
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001917 TXN.http:req_del_header("header")
1918 TXN.http:req_add_header("header", "value")
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001919 end
1920..
1921
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001922.. js:function:: HTTP.res_set_header(http, name, value)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001923
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02001924 This function replaces all occurrence of all header "name", by only
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001925 one containing the "value".
1926
1927 :param class_http http: The related http object.
1928 :param string name: The header name.
1929 :param string value: The header value.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01001930 :see: :js:func:`HTTP.req_rep_header()`
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001931
Pieter Baauw386a1272015-08-16 15:26:24 +02001932.. js:function:: HTTP.req_rep_header(http, name, regex, replace)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001933
1934 Matches the regular expression in all occurrences of header field "name"
1935 according to "regex", and replaces them with the "replace" argument. The
1936 replacement value can contain back references like \1, \2, ... This
1937 function works with the request.
1938
1939 :param class_http http: The related http object.
1940 :param string name: The header name.
1941 :param string regex: The match regular expression.
1942 :param string replace: The replacement value.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01001943 :see: :js:func:`HTTP.res_rep_header()`
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001944
Pieter Baauw386a1272015-08-16 15:26:24 +02001945.. js:function:: HTTP.res_rep_header(http, name, regex, string)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001946
1947 Matches the regular expression in all occurrences of header field "name"
1948 according to "regex", and replaces them with the "replace" argument. The
1949 replacement value can contain back references like \1, \2, ... This
1950 function works with the request.
1951
1952 :param class_http http: The related http object.
1953 :param string name: The header name.
1954 :param string regex: The match regular expression.
1955 :param string replace: The replacement value.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01001956 :see: :js:func:`HTTP.req_rep_header()`
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001957
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001958.. js:function:: HTTP.req_set_method(http, method)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001959
1960 Rewrites the request method with the parameter "method".
1961
1962 :param class_http http: The related http object.
1963 :param string method: The new method.
1964
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001965.. js:function:: HTTP.req_set_path(http, path)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001966
1967 Rewrites the request path with the "path" parameter.
1968
1969 :param class_http http: The related http object.
1970 :param string path: The new path.
1971
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001972.. js:function:: HTTP.req_set_query(http, query)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001973
1974 Rewrites the request's query string which appears after the first question
1975 mark ("?") with the parameter "query".
1976
1977 :param class_http http: The related http object.
1978 :param string query: The new query.
1979
Thierry FOURNIER0d79cf62015-08-26 14:20:58 +02001980.. js:function:: HTTP.req_set_uri(http, uri)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001981
1982 Rewrites the request URI with the parameter "uri".
1983
1984 :param class_http http: The related http object.
1985 :param string uri: The new uri.
1986
Robin H. Johnson52f5db22017-01-01 13:10:52 -08001987.. js:function:: HTTP.res_set_status(http, status [, reason])
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +02001988
Robin H. Johnson52f5db22017-01-01 13:10:52 -08001989 Rewrites the response status code with the parameter "code".
1990
1991 If no custom reason is provided, it will be generated from the status.
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +02001992
1993 :param class_http http: The related http object.
1994 :param integer status: The new response status code.
Robin H. Johnson52f5db22017-01-01 13:10:52 -08001995 :param string reason: The new response reason (optional).
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +02001996
William Lallemand00a15022021-11-19 16:02:44 +01001997.. _httpclient_class:
1998
1999HTTPClient class
2000================
2001
2002.. js:class:: HTTPClient
2003
2004 The httpclient class allows issue of outbound HTTP requests through a simple
2005 API without the knowledge of HAProxy internals.
2006
2007.. js:function:: HTTPClient.get(httpclient, request)
2008.. js:function:: HTTPClient.head(httpclient, request)
2009.. js:function:: HTTPClient.put(httpclient, request)
2010.. js:function:: HTTPClient.post(httpclient, request)
2011.. js:function:: HTTPClient.delete(httpclient, request)
2012
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02002013 Send a HTTP request and wait for a response. GET, HEAD PUT, POST and DELETE methods can be used.
2014 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 +01002015
William Lallemanda9256192022-10-21 11:48:24 +02002016 The HTTPClient interface is not able to decompress responses, it is not
2017 recommended to send an Accept-Encoding in the request so the response is
2018 received uncompressed.
William Lallemand00a15022021-11-19 16:02:44 +01002019
2020 :param class httpclient: Is the manipulated HTTPClient.
2021 :param table request: Is a table containing the parameters of the request that will be send.
2022 :param string request.url: Is a mandatory parameter for the request that contains the URL.
2023 :param string request.body: Is an optional parameter for the request that contains the body to send.
2024 :param table request.headers: Is an optional parameter for the request that contains the headers to send.
William Lallemand18340302022-02-23 15:57:45 +01002025 :param string request.dst: Is an optional parameter for the destination in haproxy address format.
William Lallemandb4a4ef62022-02-23 14:18:16 +01002026 :param integer request.timeout: Optional timeout parameter, set a "timeout server" on the connections.
William Lallemand00a15022021-11-19 16:02:44 +01002027 :returns: Lua table containing the response
2028
2029
2030.. code-block:: lua
2031
2032 local httpclient = core.httpclient()
William Lallemand4f4f2b72022-02-17 20:00:23 +01002033 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 +01002034
2035..
2036
2037.. code-block:: lua
2038
2039 response = {
2040 status = 400,
2041 reason = "Bad request",
2042 headers = {
2043 ["content-type"] = { "text/html" },
2044 ["cache-control"] = { "no-cache", "no-store" },
2045 },
William Lallemand4f4f2b72022-02-17 20:00:23 +01002046 body = "<html><body><h1>invalid request<h1></body></html>",
William Lallemand00a15022021-11-19 16:02:44 +01002047 }
2048..
2049
2050
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002051.. _txn_class:
2052
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002053TXN class
2054=========
2055
2056.. js:class:: TXN
2057
2058 The txn class contain all the functions relative to the http or tcp
2059 transaction (Note than a tcp stream is the same than a tcp transaction, but
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02002060 a HTTP transaction is not the same than a tcp stream).
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002061
2062 The usage of this class permits to retrieve data from the requests, alter it
2063 and forward it.
2064
2065 All the functions provided by this class are available in the context
Christopher Faulet5a2c6612021-08-15 20:35:25 +02002066 **sample-fetches**, **actions** and **filters**.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002067
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002068.. js:attribute:: TXN.c
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002069
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002070 :returns: An :ref:`converters_class`.
2071
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002072 This attribute contains a Converters class object.
2073
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002074.. js:attribute:: TXN.sc
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002075
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002076 :returns: An :ref:`converters_class`.
2077
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002078 This attribute contains a Converters class object. The functions of
2079 this object returns always a string.
2080
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002081.. js:attribute:: TXN.f
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002082
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002083 :returns: An :ref:`fetches_class`.
2084
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002085 This attribute contains a Fetches class object.
2086
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002087.. js:attribute:: TXN.sf
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002088
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002089 :returns: An :ref:`fetches_class`.
2090
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002091 This attribute contains a Fetches class object. The functions of
2092 this object returns always a string.
2093
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002094.. js:attribute:: TXN.req
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002095
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002096 :returns: An :ref:`channel_class`.
2097
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002098 This attribute contains a channel class object for the request buffer.
2099
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002100.. js:attribute:: TXN.res
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002101
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002102 :returns: An :ref:`channel_class`.
2103
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002104 This attribute contains a channel class object for the response buffer.
2105
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002106.. js:attribute:: TXN.http
Thierry FOURNIER08504f42015-03-16 14:17:08 +01002107
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002108 :returns: An :ref:`http_class`.
2109
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02002110 This attribute contains a HTTP class object. It is available only if the
Thierry FOURNIER08504f42015-03-16 14:17:08 +01002111 proxy has the "mode http" enabled.
2112
Christopher Faulet5a2c6612021-08-15 20:35:25 +02002113.. js:attribute:: TXN.http_req
2114
2115 :returns: An :ref:`httpmessage_class`.
2116
2117 This attribute contains the request HTTPMessage class object. It is available
2118 only if the proxy has the "mode http" enabled and only in the **filters**
2119 context.
2120
2121.. js:attribute:: TXN.http_res
2122
2123 :returns: An :ref:`httpmessage_class`.
2124
2125 This attribute contains the response HTTPMessage class object. It is available
2126 only if the proxy has the "mode http" enabled and only in the **filters**
2127 context.
2128
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01002129.. js:function:: TXN.log(TXN, loglevel, msg)
2130
2131 This function sends a log. The log is sent, according with the HAProxy
2132 configuration file, on the default syslog server if it is configured and on
2133 the stderr if it is allowed.
2134
2135 :param class_txn txn: The class txn object containing the data.
Bertrand Jacquin874a35c2018-09-10 21:26:07 +01002136 :param integer loglevel: Is the log level associated with the message. It is a
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01002137 number between 0 and 7.
2138 :param string msg: The log content.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01002139 :see: :js:attr:`core.emerg`, :js:attr:`core.alert`, :js:attr:`core.crit`,
2140 :js:attr:`core.err`, :js:attr:`core.warning`, :js:attr:`core.notice`,
2141 :js:attr:`core.info`, :js:attr:`core.debug` (log level definitions)
2142 :see: :js:func:`TXN.deflog`
2143 :see: :js:func:`TXN.Debug`
2144 :see: :js:func:`TXN.Info`
2145 :see: :js:func:`TXN.Warning`
2146 :see: :js:func:`TXN.Alert`
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01002147
2148.. js:function:: TXN.deflog(TXN, msg)
2149
Bertrand Jacquin874a35c2018-09-10 21:26:07 +01002150 Sends a log line with the default loglevel for the proxy associated with the
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01002151 transaction.
2152
2153 :param class_txn txn: The class txn object containing the data.
2154 :param string msg: The log content.
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02002155 :see: :js:func:`TXN.log`
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01002156
2157.. js:function:: TXN.Debug(txn, msg)
2158
2159 :param class_txn txn: The class txn object containing the data.
2160 :param string msg: The log content.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01002161 :see: :js:func:`TXN.log`
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01002162
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02002163 Does the same job as:
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01002164
2165.. code-block:: lua
2166
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002167 function Debug(txn, msg)
2168 TXN.log(txn, core.debug, msg)
2169 end
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01002170..
2171
2172.. js:function:: TXN.Info(txn, msg)
2173
2174 :param class_txn txn: The class txn object containing the data.
2175 :param string msg: The log content.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01002176 :see: :js:func:`TXN.log`
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01002177
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02002178 Does the same job as:
2179
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01002180.. code-block:: lua
2181
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02002182 function Info(txn, msg)
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002183 TXN.log(txn, core.info, msg)
2184 end
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01002185..
2186
2187.. js:function:: TXN.Warning(txn, msg)
2188
2189 :param class_txn txn: The class txn object containing the data.
2190 :param string msg: The log content.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01002191 :see: :js:func:`TXN.log`
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01002192
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02002193 Does the same job as:
2194
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01002195.. code-block:: lua
2196
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02002197 function Warning(txn, msg)
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002198 TXN.log(txn, core.warning, msg)
2199 end
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01002200..
2201
2202.. js:function:: TXN.Alert(txn, msg)
2203
2204 :param class_txn txn: The class txn object containing the data.
2205 :param string msg: The log content.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01002206 :see: :js:func:`TXN.log`
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01002207
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02002208 Does the same job as:
2209
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01002210.. code-block:: lua
2211
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02002212 function Alert(txn, msg)
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002213 TXN.log(txn, core.alert, msg)
2214 end
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01002215..
2216
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002217.. js:function:: TXN.get_priv(txn)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002218
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002219 Return Lua data stored in the current transaction (with the `TXN.set_priv()`)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002220 function. If no data are stored, it returns a nil value.
2221
2222 :param class_txn txn: The class txn object containing the data.
Bertrand Jacquin874a35c2018-09-10 21:26:07 +01002223 :returns: the opaque data previously stored, or nil if nothing is
2224 available.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002225
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002226.. js:function:: TXN.set_priv(txn, data)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002227
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02002228 Store any data in the current HAProxy transaction. This action replaces the
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002229 old stored data.
2230
2231 :param class_txn txn: The class txn object containing the data.
2232 :param opaque data: The data which is stored in the transaction.
2233
Tim Duesterhus4e172c92020-05-19 13:49:42 +02002234.. js:function:: TXN.set_var(TXN, var, value[, ifexist])
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +02002235
David Carlier61fdf8b2015-10-02 11:59:38 +01002236 Converts a Lua type in a HAProxy type and store it in a variable <var>.
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +02002237
2238 :param class_txn txn: The class txn object containing the data.
2239 :param string var: The variable name according with the HAProxy variable syntax.
Thierry FOURNIER / OZON.IOb210bcc2016-12-12 16:24:16 +01002240 :param type value: The value associated to the variable. The type can be string or
2241 integer.
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02002242 :param boolean ifexist: If this parameter is set to true the variable
Tim Duesterhus4e172c92020-05-19 13:49:42 +02002243 will only be set if it was defined elsewhere (i.e. used
Willy Tarreau7978c5c2021-09-07 14:24:07 +02002244 within the configuration). For global variables (using the
2245 "proc" scope), they will only be updated and never created.
2246 It is highly recommended to always set this to true.
Christopher Faulet85d79c92016-11-09 16:54:56 +01002247
2248.. js:function:: TXN.unset_var(TXN, var)
2249
2250 Unset the variable <var>.
2251
2252 :param class_txn txn: The class txn object containing the data.
2253 :param string var: The variable name according with the HAProxy variable syntax.
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +02002254
2255.. js:function:: TXN.get_var(TXN, var)
2256
2257 Returns data stored in the variable <var> converter in Lua type.
2258
2259 :param class_txn txn: The class txn object containing the data.
2260 :param string var: The variable name according with the HAProxy variable syntax.
2261
Christopher Faulet700d9e82020-01-31 12:21:52 +01002262.. js:function:: TXN.reply([reply])
2263
2264 Return a new reply object
2265
2266 :param table reply: A table containing info to initialize the reply fields.
2267 :returns: A :ref:`reply_class` object.
2268
2269 The table used to initialized the reply object may contain following entries :
2270
2271 * status : The reply status code. the code 200 is used by default.
2272 * reason : The reply reason. The reason corresponding to the status code is
2273 used by default.
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02002274 * headers : A list of headers, indexed by header name. Empty by default. For
Christopher Faulet700d9e82020-01-31 12:21:52 +01002275 a given name, multiple values are possible, stored in an ordered list.
2276 * body : The reply body, empty by default.
2277
2278.. code-block:: lua
2279
2280 local reply = txn:reply{
2281 status = 400,
2282 reason = "Bad request",
2283 headers = {
2284 ["content-type"] = { "text/html" },
2285 ["cache-control"] = {"no-cache", "no-store" }
2286 },
2287 body = "<html><body><h1>invalid request<h1></body></html>"
2288 }
2289..
2290 :see: :js:class:`Reply`
2291
2292.. js:function:: TXN.done(txn[, reply])
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002293
Willy Tarreaubc183a62015-08-28 10:39:11 +02002294 This function terminates processing of the transaction and the associated
Christopher Faulet700d9e82020-01-31 12:21:52 +01002295 session and optionally reply to the client for HTTP sessions.
2296
2297 :param class_txn txn: The class txn object containing the data.
2298 :param class_reply reply: The class reply object to return to the client.
2299
2300 This functions can be used when a critical error is detected or to terminate
Willy Tarreaubc183a62015-08-28 10:39:11 +02002301 processing after some data have been returned to the client (eg: a redirect).
Christopher Faulet700d9e82020-01-31 12:21:52 +01002302 To do so, a reply may be provided. This object is optional and may contain a
2303 status code, a reason, a header list and a body. All these fields are
Christopher Faulet7855b192021-11-09 18:39:51 +01002304 optional. When not provided, the default values are used. By default, with an
2305 empty reply object, an empty HTTP 200 response is returned to the client. If
2306 no reply object is provided, the transaction is terminated without any
2307 reply. If a reply object is provided, it must not exceed the buffer size once
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02002308 converted into the internal HTTP representation. Because for now there is no
Christopher Faulet7855b192021-11-09 18:39:51 +01002309 easy way to be sure it fits, it is probably better to keep it reasonably
2310 small.
Christopher Faulet700d9e82020-01-31 12:21:52 +01002311
2312 The reply object may be fully created in lua or the class Reply may be used to
2313 create it.
2314
2315.. code-block:: lua
2316
2317 local reply = txn:reply()
2318 reply:set_status(400, "Bad request")
2319 reply:add_header("content-type", "text/html")
2320 reply:add_header("cache-control", "no-cache")
2321 reply:add_header("cache-control", "no-store")
2322 reply:set_body("<html><body><h1>invalid request<h1></body></html>")
2323 txn:done(reply)
2324..
2325
2326.. code-block:: lua
2327
2328 txn:done{
2329 status = 400,
2330 reason = "Bad request",
2331 headers = {
2332 ["content-type"] = { "text/html" },
2333 ["cache-control"] = { "no-cache", "no-store" },
2334 },
2335 body = "<html><body><h1>invalid request<h1></body></html>"
2336 }
2337..
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002338
Christopher Faulet1e9b1b62021-08-11 10:14:30 +02002339 .. warning::
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02002340 It does not make sense to call this function from sample-fetches. In this case
2341 the behavior is the same than core.done(): it finishes the Lua
Christopher Faulet1e9b1b62021-08-11 10:14:30 +02002342 execution. The transaction is really aborted only from an action registered
2343 function.
Thierry FOURNIERab00df62016-07-14 11:42:37 +02002344
Christopher Faulet700d9e82020-01-31 12:21:52 +01002345 :see: :js:func:`TXN.reply`, :js:class:`Reply`
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002346
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002347.. js:function:: TXN.set_loglevel(txn, loglevel)
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01002348
2349 Is used to change the log level of the current request. The "loglevel" must
2350 be an integer between 0 and 7.
2351
2352 :param class_txn txn: The class txn object containing the data.
2353 :param integer loglevel: The required log level. This variable can be one of
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01002354 :see: :js:attr:`core.emerg`, :js:attr:`core.alert`, :js:attr:`core.crit`,
2355 :js:attr:`core.err`, :js:attr:`core.warning`, :js:attr:`core.notice`,
2356 :js:attr:`core.info`, :js:attr:`core.debug` (log level definitions)
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01002357
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002358.. js:function:: TXN.set_tos(txn, tos)
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01002359
2360 Is used to set the TOS or DSCP field value of packets sent to the client to
2361 the value passed in "tos" on platforms which support this.
2362
2363 :param class_txn txn: The class txn object containing the data.
2364 :param integer tos: The new TOS os DSCP.
2365
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002366.. js:function:: TXN.set_mark(txn, mark)
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01002367
2368 Is used to set the Netfilter MARK on all packets sent to the client to the
2369 value passed in "mark" on platforms which support it.
2370
2371 :param class_txn txn: The class txn object containing the data.
2372 :param integer mark: The mark value.
2373
Patrick Hemmer268a7072018-05-11 12:52:31 -04002374.. js:function:: TXN.set_priority_class(txn, prio)
2375
2376 This function adjusts the priority class of the transaction. The value should
2377 be within the range -2047..2047. Values outside this range will be
2378 truncated.
2379
2380 See the HAProxy configuration.txt file keyword "http-request" action
2381 "set-priority-class" for details.
2382
2383.. js:function:: TXN.set_priority_offset(txn, prio)
2384
2385 This function adjusts the priority offset of the transaction. The value
2386 should be within the range -524287..524287. Values outside this range will be
2387 truncated.
2388
2389 See the HAProxy configuration.txt file keyword "http-request" action
2390 "set-priority-offset" for details.
2391
Christopher Faulet700d9e82020-01-31 12:21:52 +01002392.. _reply_class:
2393
2394Reply class
2395============
2396
2397.. js:class:: Reply
2398
2399 **context**: action
2400
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02002401 This class represents a HTTP response message. It provides some methods to
Christopher Faulet7855b192021-11-09 18:39:51 +01002402 enrich it. Once converted into the internal HTTP representation, the response
2403 message must not exceed the buffer size. Because for now there is no
2404 easy way to be sure it fits, it is probably better to keep it reasonably
2405 small.
2406
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02002407 See tune.bufsize in the configuration manual for details.
Christopher Faulet700d9e82020-01-31 12:21:52 +01002408
2409.. code-block:: lua
2410
2411 local reply = txn:reply({status = 400}) -- default HTTP 400 reason-phase used
2412 reply:add_header("content-type", "text/html")
2413 reply:add_header("cache-control", "no-cache")
2414 reply:add_header("cache-control", "no-store")
2415 reply:set_body("<html><body><h1>invalid request<h1></body></html>")
2416..
2417
2418 :see: :js:func:`TXN.reply`
2419
2420.. js:attribute:: Reply.status
2421
2422 The reply status code. By default, the status code is set to 200.
2423
2424 :returns: integer
2425
2426.. js:attribute:: Reply.reason
2427
2428 The reason string describing the status code.
2429
2430 :returns: string
2431
2432.. js:attribute:: Reply.headers
2433
2434 A table indexing all reply headers by name. To each name is associated an
2435 ordered list of values.
2436
2437 :returns: Lua table
2438
2439.. code-block:: lua
2440
2441 {
2442 ["content-type"] = { "text/html" },
2443 ["cache-control"] = {"no-cache", "no-store" },
2444 x_header_name = { "value1", "value2", ... }
2445 ...
2446 }
2447..
2448
2449.. js:attribute:: Reply.body
2450
2451 The reply payload.
2452
2453 :returns: string
2454
2455.. js:function:: Reply.set_status(REPLY, status[, reason])
2456
2457 Set the reply status code and optionally the reason-phrase. If the reason is
2458 not provided, the default reason corresponding to the status code is used.
2459
2460 :param class_reply reply: The related Reply object.
2461 :param integer status: The reply status code.
2462 :param string reason: The reply status reason (optional).
2463
2464.. js:function:: Reply.add_header(REPLY, name, value)
2465
2466 Add a header to the reply object. If the header does not already exist, a new
2467 entry is created with its name as index and a one-element list containing its
2468 value as value. Otherwise, the header value is appended to the ordered list of
2469 values associated to the header name.
2470
2471 :param class_reply reply: The related Reply object.
2472 :param string name: The header field name.
2473 :param string value: The header field value.
2474
2475.. js:function:: Reply.del_header(REPLY, name)
2476
2477 Remove all occurrences of a header name from the reply object.
2478
2479 :param class_reply reply: The related Reply object.
2480 :param string name: The header field name.
2481
2482.. js:function:: Reply.set_body(REPLY, body)
2483
2484 Set the reply payload.
2485
2486 :param class_reply reply: The related Reply object.
2487 :param string body: The reply payload.
2488
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002489.. _socket_class:
2490
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002491Socket class
2492============
2493
2494.. js:class:: Socket
2495
2496 This class must be compatible with the Lua Socket class. Only the 'client'
2497 functions are available. See the Lua Socket documentation:
2498
2499 `http://w3.impa.br/~diego/software/luasocket/tcp.html
2500 <http://w3.impa.br/~diego/software/luasocket/tcp.html>`_
2501
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002502.. js:function:: Socket.close(socket)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002503
2504 Closes a TCP object. The internal socket used by the object is closed and the
2505 local address to which the object was bound is made available to other
2506 applications. No further operations (except for further calls to the close
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002507 method) are allowed on a closed Socket.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002508
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002509 :param class_socket socket: Is the manipulated Socket.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002510
2511 Note: It is important to close all used sockets once they are not needed,
2512 since, in many systems, each socket uses a file descriptor, which are limited
2513 system resources. Garbage-collected objects are automatically closed before
2514 destruction, though.
2515
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002516.. js:function:: Socket.connect(socket, address[, port])
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002517
2518 Attempts to connect a socket object to a remote host.
2519
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002520
2521 In case of error, the method returns nil followed by a string describing the
2522 error. In case of success, the method returns 1.
2523
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002524 :param class_socket socket: Is the manipulated Socket.
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002525 :param string address: can be an IP address or a host name. See below for more
2526 information.
2527 :param integer port: must be an integer number in the range [1..64K].
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002528 :returns: 1 or nil.
2529
Bertrand Jacquin874a35c2018-09-10 21:26:07 +01002530 An address field extension permits to use the connect() function to connect to
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002531 other stream than TCP. The syntax containing a simpleipv4 or ipv6 address is
2532 the basically expected format. This format requires the port.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002533
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002534 Other format accepted are a socket path like "/socket/path", it permits to
Bertrand Jacquin874a35c2018-09-10 21:26:07 +01002535 connect to a socket. Abstract namespaces are supported with the prefix
Joseph Herlant02cedc42018-11-13 19:45:17 -08002536 "abns@", and finally a file descriptor can be passed with the prefix "fd@".
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002537 The prefix "ipv4@", "ipv6@" and "unix@" are also supported. The port can be
Bertrand Jacquin874a35c2018-09-10 21:26:07 +01002538 passed int the string. The syntax "127.0.0.1:1234" is valid. In this case, the
Tim Duesterhus6edab862018-01-06 19:04:45 +01002539 parameter *port* must not be set.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002540
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002541.. js:function:: Socket.connect_ssl(socket, address, port)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002542
2543 Same behavior than the function socket:connect, but uses SSL.
2544
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002545 :param class_socket socket: Is the manipulated Socket.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002546 :returns: 1 or nil.
2547
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002548.. js:function:: Socket.getpeername(socket)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002549
2550 Returns information about the remote side of a connected client object.
2551
2552 Returns a string with the IP address of the peer, followed by the port number
2553 that peer is using for the connection. In case of error, the method returns
2554 nil.
2555
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002556 :param class_socket socket: Is the manipulated Socket.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002557 :returns: a string containing the server information.
2558
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002559.. js:function:: Socket.getsockname(socket)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002560
2561 Returns the local address information associated to the object.
2562
2563 The method returns a string with local IP address and a number with the port.
2564 In case of error, the method returns nil.
2565
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002566 :param class_socket socket: Is the manipulated Socket.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002567 :returns: a string containing the client information.
2568
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002569.. js:function:: Socket.receive(socket, [pattern [, prefix]])
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002570
2571 Reads data from a client object, according to the specified read pattern.
2572 Patterns follow the Lua file I/O format, and the difference in performance
2573 between all patterns is negligible.
2574
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002575 :param class_socket socket: Is the manipulated Socket.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002576 :param string|integer pattern: Describe what is required (see below).
2577 :param string prefix: A string which will be prefix the returned data.
2578 :returns: a string containing the required data or nil.
2579
2580 Pattern can be any of the following:
2581
2582 * **`*a`**: reads from the socket until the connection is closed. No
2583 end-of-line translation is performed;
2584
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002585 * **`*l`**: reads a line of text from the Socket. The line is terminated by a
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002586 LF character (ASCII 10), optionally preceded by a CR character
2587 (ASCII 13). The CR and LF characters are not included in the
2588 returned line. In fact, all CR characters are ignored by the
2589 pattern. This is the default pattern.
2590
2591 * **number**: causes the method to read a specified number of bytes from the
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002592 Socket. Prefix is an optional string to be concatenated to the
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002593 beginning of any received data before return.
2594
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002595 * **empty**: If the pattern is left empty, the default option is `*l`.
2596
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002597 If successful, the method returns the received pattern. In case of error, the
2598 method returns nil followed by an error message which can be the string
2599 'closed' in case the connection was closed before the transmission was
2600 completed or the string 'timeout' in case there was a timeout during the
2601 operation. Also, after the error message, the function returns the partial
2602 result of the transmission.
2603
2604 Important note: This function was changed severely. It used to support
2605 multiple patterns (but I have never seen this feature used) and now it
2606 doesn't anymore. Partial results used to be returned in the same way as
2607 successful results. This last feature violated the idea that all functions
2608 should return nil on error. Thus it was changed too.
2609
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002610.. js:function:: Socket.send(socket, data [, start [, end ]])
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002611
2612 Sends data through client object.
2613
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002614 :param class_socket socket: Is the manipulated Socket.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002615 :param string data: The data that will be sent.
2616 :param integer start: The start position in the buffer of the data which will
2617 be sent.
2618 :param integer end: The end position in the buffer of the data which will
2619 be sent.
2620 :returns: see below.
2621
2622 Data is the string to be sent. The optional arguments i and j work exactly
2623 like the standard string.sub Lua function to allow the selection of a
2624 substring to be sent.
2625
2626 If successful, the method returns the index of the last byte within [start,
2627 end] that has been sent. Notice that, if start is 1 or absent, this is
2628 effectively the total number of bytes sent. In case of error, the method
2629 returns nil, followed by an error message, followed by the index of the last
2630 byte within [start, end] that has been sent. You might want to try again from
2631 the byte following that. The error message can be 'closed' in case the
2632 connection was closed before the transmission was completed or the string
2633 'timeout' in case there was a timeout during the operation.
2634
2635 Note: Output is not buffered. For small strings, it is always better to
2636 concatenate them in Lua (with the '..' operator) and send the result in one
2637 call instead of calling the method several times.
2638
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002639.. js:function:: Socket.setoption(socket, option [, value])
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002640
2641 Just implemented for compatibility, this cal does nothing.
2642
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002643.. js:function:: Socket.settimeout(socket, value [, mode])
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002644
2645 Changes the timeout values for the object. All I/O operations are blocking.
2646 That is, any call to the methods send, receive, and accept will block
2647 indefinitely, until the operation completes. The settimeout method defines a
2648 limit on the amount of time the I/O methods can block. When a timeout time
2649 has elapsed, the affected methods give up and fail with an error code.
2650
2651 The amount of time to wait is specified as the value parameter, in seconds.
2652
Mark Lakes56cc1252018-03-27 09:48:06 +02002653 The timeout modes are not implemented, the only settable timeout is the
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002654 inactivity time waiting for complete the internal buffer send or waiting for
2655 receive data.
2656
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002657 :param class_socket socket: Is the manipulated Socket.
Bertrand Jacquin874a35c2018-09-10 21:26:07 +01002658 :param float value: The timeout value. Use floating point to specify
Mark Lakes56cc1252018-03-27 09:48:06 +02002659 milliseconds.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002660
Thierry FOURNIER31904272017-10-25 12:59:51 +02002661.. _regex_class:
2662
2663Regex class
2664===========
2665
2666.. js:class:: Regex
2667
2668 This class allows the usage of HAProxy regexes because classic lua doesn't
2669 provides regexes. This class inherits the HAProxy compilation options, so the
2670 regexes can be libc regex, pcre regex or pcre JIT regex.
2671
2672 The expression matching number is limited to 20 per regex. The only available
2673 option is case sensitive.
2674
2675 Because regexes compilation is a heavy process, it is better to define all
2676 your regexes in the **body context** and use it during the runtime.
2677
2678.. code-block:: lua
2679
2680 -- Create the regex
2681 st, regex = Regex.new("needle (..) (...)", true);
2682
2683 -- Check compilation errors
2684 if st == false then
2685 print "error: " .. regex
2686 end
2687
2688 -- Match the regexes
2689 print(regex:exec("Looking for a needle in the haystack")) -- true
2690 print(regex:exec("Lokking for a cat in the haystack")) -- false
2691
2692 -- Extract words
2693 st, list = regex:match("Looking for a needle in the haystack")
2694 print(st) -- true
2695 print(list[1]) -- needle in the
2696 print(list[2]) -- in
2697 print(list[3]) -- the
2698
2699.. js:function:: Regex.new(regex, case_sensitive)
2700
2701 Create and compile a regex.
2702
2703 :param string regex: The regular expression according with the libc or pcre
2704 standard
2705 :param boolean case_sensitive: Match is case sensitive or not.
2706 :returns: boolean status and :ref:`regex_class` or string containing fail reason.
2707
2708.. js:function:: Regex.exec(regex, str)
2709
2710 Execute the regex.
2711
2712 :param class_regex regex: A :ref:`regex_class` object.
2713 :param string str: The input string will be compared with the compiled regex.
2714 :returns: a boolean status according with the match result.
2715
2716.. js:function:: Regex.match(regex, str)
2717
2718 Execute the regex and return matched expressions.
2719
2720 :param class_map map: A :ref:`regex_class` object.
2721 :param string str: The input string will be compared with the compiled regex.
2722 :returns: a boolean status according with the match result, and
2723 a table containing all the string matched in order of declaration.
2724
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002725.. _map_class:
2726
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002727Map class
2728=========
2729
2730.. js:class:: Map
2731
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02002732 This class permits to do some lookups in HAProxy maps. The declared maps can
Bertrand Jacquin874a35c2018-09-10 21:26:07 +01002733 be modified during the runtime through the HAProxy management socket.
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002734
2735.. code-block:: lua
2736
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002737 default = "usa"
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002738
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002739 -- Create and load map
Thierry FOURNIER4dc71972017-01-28 08:33:08 +01002740 geo = Map.new("geo.map", Map._ip);
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002741
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002742 -- Create new fetch that returns the user country
2743 core.register_fetches("country", function(txn)
2744 local src;
2745 local loc;
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002746
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002747 src = txn.f:fhdr("x-forwarded-for");
2748 if (src == nil) then
2749 src = txn.f:src()
2750 if (src == nil) then
2751 return default;
2752 end
2753 end
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002754
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002755 -- Perform lookup
2756 loc = geo:lookup(src);
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002757
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002758 if (loc == nil) then
2759 return default;
2760 end
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002761
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002762 return loc;
2763 end);
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002764
Thierry FOURNIER4dc71972017-01-28 08:33:08 +01002765.. js:attribute:: Map._int
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002766
2767 See the HAProxy configuration.txt file, chapter "Using ACLs and fetching
Ilya Shipitsin2075ca82020-03-06 23:22:22 +05002768 samples" and subchapter "ACL basics" to understand this pattern matching
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002769 method.
2770
Thierry FOURNIER4dc71972017-01-28 08:33:08 +01002771 Note that :js:attr:`Map.int` is also available for compatibility.
2772
2773.. js:attribute:: Map._ip
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002774
2775 See the HAProxy configuration.txt file, chapter "Using ACLs and fetching
Ilya Shipitsin2075ca82020-03-06 23:22:22 +05002776 samples" and subchapter "ACL basics" to understand this pattern matching
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002777 method.
2778
Thierry FOURNIER4dc71972017-01-28 08:33:08 +01002779 Note that :js:attr:`Map.ip` is also available for compatibility.
2780
2781.. js:attribute:: Map._str
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002782
2783 See the HAProxy configuration.txt file, chapter "Using ACLs and fetching
Ilya Shipitsin2075ca82020-03-06 23:22:22 +05002784 samples" and subchapter "ACL basics" to understand this pattern matching
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002785 method.
2786
Thierry FOURNIER4dc71972017-01-28 08:33:08 +01002787 Note that :js:attr:`Map.str` is also available for compatibility.
2788
2789.. js:attribute:: Map._beg
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002790
2791 See the HAProxy configuration.txt file, chapter "Using ACLs and fetching
Ilya Shipitsin2075ca82020-03-06 23:22:22 +05002792 samples" and subchapter "ACL basics" to understand this pattern matching
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002793 method.
2794
Thierry FOURNIER4dc71972017-01-28 08:33:08 +01002795 Note that :js:attr:`Map.beg` is also available for compatibility.
2796
2797.. js:attribute:: Map._sub
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002798
2799 See the HAProxy configuration.txt file, chapter "Using ACLs and fetching
Ilya Shipitsin2075ca82020-03-06 23:22:22 +05002800 samples" and subchapter "ACL basics" to understand this pattern matching
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002801 method.
2802
Thierry FOURNIER4dc71972017-01-28 08:33:08 +01002803 Note that :js:attr:`Map.sub` is also available for compatibility.
2804
2805.. js:attribute:: Map._dir
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002806
2807 See the HAProxy configuration.txt file, chapter "Using ACLs and fetching
Ilya Shipitsin2075ca82020-03-06 23:22:22 +05002808 samples" and subchapter "ACL basics" to understand this pattern matching
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002809 method.
2810
Thierry FOURNIER4dc71972017-01-28 08:33:08 +01002811 Note that :js:attr:`Map.dir` is also available for compatibility.
2812
2813.. js:attribute:: Map._dom
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002814
2815 See the HAProxy configuration.txt file, chapter "Using ACLs and fetching
Ilya Shipitsin2075ca82020-03-06 23:22:22 +05002816 samples" and subchapter "ACL basics" to understand this pattern matching
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002817 method.
2818
Thierry FOURNIER4dc71972017-01-28 08:33:08 +01002819 Note that :js:attr:`Map.dom` is also available for compatibility.
2820
2821.. js:attribute:: Map._end
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002822
2823 See the HAProxy configuration.txt file, chapter "Using ACLs and fetching
Ilya Shipitsin2075ca82020-03-06 23:22:22 +05002824 samples" and subchapter "ACL basics" to understand this pattern matching
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002825 method.
2826
Thierry FOURNIER4dc71972017-01-28 08:33:08 +01002827.. js:attribute:: Map._reg
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002828
2829 See the HAProxy configuration.txt file, chapter "Using ACLs and fetching
Ilya Shipitsin2075ca82020-03-06 23:22:22 +05002830 samples" and subchapter "ACL basics" to understand this pattern matching
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002831 method.
2832
Thierry FOURNIER4dc71972017-01-28 08:33:08 +01002833 Note that :js:attr:`Map.reg` is also available for compatibility.
2834
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002835
2836.. js:function:: Map.new(file, method)
2837
2838 Creates and load a map.
2839
2840 :param string file: Is the file containing the map.
2841 :param integer method: Is the map pattern matching method. See the attributes
2842 of the Map class.
2843 :returns: a class Map object.
Thierry FOURNIER4dc71972017-01-28 08:33:08 +01002844 :see: The Map attributes: :js:attr:`Map._int`, :js:attr:`Map._ip`,
2845 :js:attr:`Map._str`, :js:attr:`Map._beg`, :js:attr:`Map._sub`,
2846 :js:attr:`Map._dir`, :js:attr:`Map._dom`, :js:attr:`Map._end` and
2847 :js:attr:`Map._reg`.
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002848
2849.. js:function:: Map.lookup(map, str)
2850
2851 Perform a lookup in a map.
2852
2853 :param class_map map: Is the class Map object.
2854 :param string str: Is the string used as key.
2855 :returns: a string containing the result or nil if no match.
2856
2857.. js:function:: Map.slookup(map, str)
2858
2859 Perform a lookup in a map.
2860
2861 :param class_map map: Is the class Map object.
2862 :param string str: Is the string used as key.
2863 :returns: a string containing the result or empty string if no match.
2864
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002865.. _applethttp_class:
2866
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002867AppletHTTP class
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002868================
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002869
2870.. js:class:: AppletHTTP
2871
2872 This class is used with applets that requires the 'http' mode. The http applet
2873 can be registered with the *core.register_service()* function. They are used
2874 for processing an http request like a server in back of HAProxy.
2875
2876 This is an hello world sample code:
2877
2878.. code-block:: lua
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002879
Pieter Baauw4d7f7662015-11-08 16:38:08 +01002880 core.register_service("hello-world", "http", function(applet)
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002881 local response = "Hello World !"
2882 applet:set_status(200)
Pieter Baauw2dcb9bc2015-10-01 22:47:12 +02002883 applet:add_header("content-length", string.len(response))
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002884 applet:add_header("content-type", "text/plain")
Pieter Baauw2dcb9bc2015-10-01 22:47:12 +02002885 applet:start_response()
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002886 applet:send(response)
2887 end)
2888
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002889.. js:attribute:: AppletHTTP.c
2890
2891 :returns: A :ref:`converters_class`
2892
2893 This attribute contains a Converters class object.
2894
2895.. js:attribute:: AppletHTTP.sc
2896
2897 :returns: A :ref:`converters_class`
2898
2899 This attribute contains a Converters class object. The
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02002900 functions of this object always return a string.
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002901
2902.. js:attribute:: AppletHTTP.f
2903
2904 :returns: A :ref:`fetches_class`
2905
2906 This attribute contains a Fetches class object. Note that the
2907 applet execution place cannot access to a valid HAProxy core HTTP
Ilya Shipitsin2075ca82020-03-06 23:22:22 +05002908 transaction, so some sample fetches related to the HTTP dependent
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002909 values (hdr, path, ...) are not available.
2910
2911.. js:attribute:: AppletHTTP.sf
2912
2913 :returns: A :ref:`fetches_class`
2914
2915 This attribute contains a Fetches class object. The functions of
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02002916 this object always return a string. Note that the applet
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002917 execution place cannot access to a valid HAProxy core HTTP
Ilya Shipitsin2075ca82020-03-06 23:22:22 +05002918 transaction, so some sample fetches related to the HTTP dependent
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002919 values (hdr, path, ...) are not available.
2920
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01002921.. js:attribute:: AppletHTTP.method
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002922
2923 :returns: string
2924
2925 The attribute method returns a string containing the HTTP
2926 method.
2927
2928.. js:attribute:: AppletHTTP.version
2929
2930 :returns: string
2931
2932 The attribute version, returns a string containing the HTTP
2933 request version.
2934
2935.. js:attribute:: AppletHTTP.path
2936
2937 :returns: string
2938
2939 The attribute path returns a string containing the HTTP
2940 request path.
2941
2942.. js:attribute:: AppletHTTP.qs
2943
2944 :returns: string
2945
2946 The attribute qs returns a string containing the HTTP
2947 request query string.
2948
2949.. js:attribute:: AppletHTTP.length
2950
2951 :returns: integer
2952
2953 The attribute length returns an integer containing the HTTP
2954 body length.
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002955
Thierry FOURNIER841475e2015-12-11 17:10:09 +01002956.. js:attribute:: AppletHTTP.headers
2957
Patrick Hemmerc6a1d712018-05-01 21:30:41 -04002958 :returns: table
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002959
Patrick Hemmerc6a1d712018-05-01 21:30:41 -04002960 The attribute headers returns a table containing the HTTP
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002961 headers. The header names are always in lower case. As the header name can be
2962 encountered more than once in each request, the value is indexed with 0 as
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02002963 first index value. The table has this form:
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002964
2965.. code-block:: lua
2966
2967 AppletHTTP.headers['<header-name>'][<header-index>] = "<header-value>"
2968
2969 AppletHTTP.headers["host"][0] = "www.test.com"
2970 AppletHTTP.headers["accept"][0] = "audio/basic q=1"
2971 AppletHTTP.headers["accept"][1] = "audio/*, q=0.2"
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01002972 AppletHTTP.headers["accept"][2] = "*/*, q=0.1"
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002973..
2974
Robin H. Johnson52f5db22017-01-01 13:10:52 -08002975.. js:function:: AppletHTTP.set_status(applet, code [, reason])
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002976
2977 This function sets the HTTP status code for the response. The allowed code are
2978 from 100 to 599.
2979
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01002980 :param class_AppletHTTP applet: An :ref:`applethttp_class`
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002981 :param integer code: the status code returned to the client.
Robin H. Johnson52f5db22017-01-01 13:10:52 -08002982 :param string reason: the status reason returned to the client (optional).
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002983
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01002984.. js:function:: AppletHTTP.add_header(applet, name, value)
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002985
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02002986 This function adds a header in the response. Duplicated headers are not
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002987 collapsed. The special header *content-length* is used to determinate the
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02002988 response length. If it does not exist, a *transfer-encoding: chunked* is set, and
Ilya Shipitsin2075ca82020-03-06 23:22:22 +05002989 all the write from the function *AppletHTTP:send()* become a chunk.
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002990
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01002991 :param class_AppletHTTP applet: An :ref:`applethttp_class`
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002992 :param string name: the header name
2993 :param string value: the header value
2994
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01002995.. js:function:: AppletHTTP.start_response(applet)
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002996
2997 This function indicates to the HTTP engine that it can process and send the
2998 response headers. After this called we cannot add headers to the response; We
2999 cannot use the *AppletHTTP:send()* function if the
3000 *AppletHTTP:start_response()* is not called.
3001
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01003002 :param class_AppletHTTP applet: An :ref:`applethttp_class`
3003
3004.. js:function:: AppletHTTP.getline(applet)
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02003005
3006 This function returns a string containing one line from the http body. If the
3007 data returned doesn't contains a final '\\n' its assumed than its the last
3008 available data before the end of stream.
3009
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01003010 :param class_AppletHTTP applet: An :ref:`applethttp_class`
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02003011 :returns: a string. The string can be empty if we reach the end of the stream.
3012
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01003013.. js:function:: AppletHTTP.receive(applet, [size])
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02003014
3015 Reads data from the HTTP body, according to the specified read *size*. If the
3016 *size* is missing, the function tries to read all the content of the stream
3017 until the end. If the *size* is bigger than the http body, it returns the
Bertrand Jacquin874a35c2018-09-10 21:26:07 +01003018 amount of data available.
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02003019
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01003020 :param class_AppletHTTP applet: An :ref:`applethttp_class`
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02003021 :param integer size: the required read size.
Ilya Shipitsin11057a32020-06-21 21:18:27 +05003022 :returns: always return a string,the string can be empty is the connection is
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02003023 closed.
3024
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01003025.. js:function:: AppletHTTP.send(applet, msg)
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02003026
3027 Send the message *msg* on the http request body.
3028
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01003029 :param class_AppletHTTP applet: An :ref:`applethttp_class`
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02003030 :param string msg: the message to send.
3031
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01003032.. js:function:: AppletHTTP.get_priv(applet)
3033
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01003034 Return Lua data stored in the current transaction. If no data are stored,
3035 it returns a nil value.
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01003036
3037 :param class_AppletHTTP applet: An :ref:`applethttp_class`
Bertrand Jacquin874a35c2018-09-10 21:26:07 +01003038 :returns: the opaque data previously stored, or nil if nothing is
3039 available.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01003040 :see: :js:func:`AppletHTTP.set_priv`
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01003041
3042.. js:function:: AppletHTTP.set_priv(applet, data)
3043
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02003044 Store any data in the current HAProxy transaction. This action replaces the
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01003045 old stored data.
3046
3047 :param class_AppletHTTP applet: An :ref:`applethttp_class`
3048 :param opaque data: The data which is stored in the transaction.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01003049 :see: :js:func:`AppletHTTP.get_priv`
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01003050
Tim Duesterhus4e172c92020-05-19 13:49:42 +02003051.. js:function:: AppletHTTP.set_var(applet, var, value[, ifexist])
Thierry FOURNIER / OZON.IOc1edafe2016-12-12 16:25:30 +01003052
3053 Converts a Lua type in a HAProxy type and store it in a variable <var>.
3054
3055 :param class_AppletHTTP applet: An :ref:`applethttp_class`
3056 :param string var: The variable name according with the HAProxy variable syntax.
3057 :param type value: The value associated to the variable. The type ca be string or
3058 integer.
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02003059 :param boolean ifexist: If this parameter is set to true the variable
Tim Duesterhus4e172c92020-05-19 13:49:42 +02003060 will only be set if it was defined elsewhere (i.e. used
Willy Tarreau7978c5c2021-09-07 14:24:07 +02003061 within the configuration). For global variables (using the
3062 "proc" scope), they will only be updated and never created.
Aurelien DARRAGON21f7ebb2023-03-13 19:49:31 +01003063 It is highly recommended to always set this to true.
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02003064
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01003065 :see: :js:func:`AppletHTTP.unset_var`
3066 :see: :js:func:`AppletHTTP.get_var`
Thierry FOURNIER / OZON.IOc1edafe2016-12-12 16:25:30 +01003067
3068.. js:function:: AppletHTTP.unset_var(applet, var)
3069
3070 Unset the variable <var>.
3071
3072 :param class_AppletHTTP applet: An :ref:`applethttp_class`
3073 :param string var: The variable name according with the HAProxy variable syntax.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01003074 :see: :js:func:`AppletHTTP.set_var`
3075 :see: :js:func:`AppletHTTP.get_var`
Thierry FOURNIER / OZON.IOc1edafe2016-12-12 16:25:30 +01003076
3077.. js:function:: AppletHTTP.get_var(applet, var)
3078
3079 Returns data stored in the variable <var> converter in Lua type.
3080
3081 :param class_AppletHTTP applet: An :ref:`applethttp_class`
3082 :param string var: The variable name according with the HAProxy variable syntax.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01003083 :see: :js:func:`AppletHTTP.set_var`
3084 :see: :js:func:`AppletHTTP.unset_var`
Thierry FOURNIER / OZON.IOc1edafe2016-12-12 16:25:30 +01003085
Thierry FOURNIERdc595002015-12-21 11:13:52 +01003086.. _applettcp_class:
3087
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02003088AppletTCP class
3089===============
3090
3091.. js:class:: AppletTCP
3092
3093 This class is used with applets that requires the 'tcp' mode. The tcp applet
3094 can be registered with the *core.register_service()* function. They are used
3095 for processing a tcp stream like a server in back of HAProxy.
3096
Thierry FOURNIERdc595002015-12-21 11:13:52 +01003097.. js:attribute:: AppletTCP.c
3098
3099 :returns: A :ref:`converters_class`
3100
3101 This attribute contains a Converters class object.
3102
3103.. js:attribute:: AppletTCP.sc
3104
3105 :returns: A :ref:`converters_class`
3106
3107 This attribute contains a Converters class object. The
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02003108 functions of this object always return a string.
Thierry FOURNIERdc595002015-12-21 11:13:52 +01003109
3110.. js:attribute:: AppletTCP.f
3111
3112 :returns: A :ref:`fetches_class`
3113
3114 This attribute contains a Fetches class object.
3115
3116.. js:attribute:: AppletTCP.sf
3117
3118 :returns: A :ref:`fetches_class`
3119
3120 This attribute contains a Fetches class object.
3121
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01003122.. js:function:: AppletTCP.getline(applet)
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02003123
3124 This function returns a string containing one line from the stream. If the
3125 data returned doesn't contains a final '\\n' its assumed than its the last
3126 available data before the end of stream.
3127
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01003128 :param class_AppletTCP applet: An :ref:`applettcp_class`
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02003129 :returns: a string. The string can be empty if we reach the end of the stream.
3130
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01003131.. js:function:: AppletTCP.receive(applet, [size])
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02003132
3133 Reads data from the TCP stream, according to the specified read *size*. If the
3134 *size* is missing, the function tries to read all the content of the stream
3135 until the end.
3136
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01003137 :param class_AppletTCP applet: An :ref:`applettcp_class`
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02003138 :param integer size: the required read size.
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02003139 :returns: always return a string, the string can be empty if the connection is
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02003140 closed.
3141
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01003142.. js:function:: AppletTCP.send(appletmsg)
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02003143
3144 Send the message on the stream.
3145
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01003146 :param class_AppletTCP applet: An :ref:`applettcp_class`
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02003147 :param string msg: the message to send.
3148
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01003149.. js:function:: AppletTCP.get_priv(applet)
3150
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01003151 Return Lua data stored in the current transaction. If no data are stored,
3152 it returns a nil value.
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01003153
3154 :param class_AppletTCP applet: An :ref:`applettcp_class`
Bertrand Jacquin874a35c2018-09-10 21:26:07 +01003155 :returns: the opaque data previously stored, or nil if nothing is
3156 available.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01003157 :see: :js:func:`AppletTCP.set_priv`
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01003158
3159.. js:function:: AppletTCP.set_priv(applet, data)
3160
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02003161 Store any data in the current HAProxy transaction. This action replaces the
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01003162 old stored data.
3163
3164 :param class_AppletTCP applet: An :ref:`applettcp_class`
3165 :param opaque data: The data which is stored in the transaction.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01003166 :see: :js:func:`AppletTCP.get_priv`
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01003167
Tim Duesterhus4e172c92020-05-19 13:49:42 +02003168.. js:function:: AppletTCP.set_var(applet, var, value[, ifexist])
Thierry FOURNIER / OZON.IOc1edafe2016-12-12 16:25:30 +01003169
3170 Converts a Lua type in a HAProxy type and stores it in a variable <var>.
3171
3172 :param class_AppletTCP applet: An :ref:`applettcp_class`
3173 :param string var: The variable name according with the HAProxy variable syntax.
3174 :param type value: The value associated to the variable. The type can be string or
3175 integer.
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02003176 :param boolean ifexist: If this parameter is set to true the variable
Tim Duesterhus4e172c92020-05-19 13:49:42 +02003177 will only be set if it was defined elsewhere (i.e. used
Willy Tarreau7978c5c2021-09-07 14:24:07 +02003178 within the configuration). For global variables (using the
3179 "proc" scope), they will only be updated and never created.
Aurelien DARRAGON21f7ebb2023-03-13 19:49:31 +01003180 It is highly recommended to always set this to true.
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02003181
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01003182 :see: :js:func:`AppletTCP.unset_var`
3183 :see: :js:func:`AppletTCP.get_var`
Thierry FOURNIER / OZON.IOc1edafe2016-12-12 16:25:30 +01003184
3185.. js:function:: AppletTCP.unset_var(applet, var)
3186
3187 Unsets the variable <var>.
3188
3189 :param class_AppletTCP applet: An :ref:`applettcp_class`
3190 :param string var: The variable name according with the HAProxy variable syntax.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01003191 :see: :js:func:`AppletTCP.unset_var`
3192 :see: :js:func:`AppletTCP.set_var`
Thierry FOURNIER / OZON.IOc1edafe2016-12-12 16:25:30 +01003193
3194.. js:function:: AppletTCP.get_var(applet, var)
3195
3196 Returns data stored in the variable <var> converter in Lua type.
3197
3198 :param class_AppletTCP applet: An :ref:`applettcp_class`
3199 :param string var: The variable name according with the HAProxy variable syntax.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01003200 :see: :js:func:`AppletTCP.unset_var`
3201 :see: :js:func:`AppletTCP.set_var`
Thierry FOURNIER / OZON.IOc1edafe2016-12-12 16:25:30 +01003202
Adis Nezirovic8878f8e2018-07-13 12:18:33 +02003203StickTable class
3204================
3205
3206.. js:class:: StickTable
3207
3208 **context**: task, action, sample-fetch
3209
3210 This class can be used to access the HAProxy stick tables from Lua.
3211
3212.. js:function:: StickTable.info()
3213
3214 Returns stick table attributes as a Lua table. See HAProxy documentation for
Ilya Shipitsin2272d8a2020-12-21 01:22:40 +05003215 "stick-table" for canonical info, or check out example below.
Adis Nezirovic8878f8e2018-07-13 12:18:33 +02003216
3217 :returns: Lua table
3218
3219 Assume our table has IPv4 key and gpc0 and conn_rate "columns":
3220
3221.. code-block:: lua
3222
3223 {
3224 expire=<int>, # Value in ms
3225 size=<int>, # Maximum table size
3226 used=<int>, # Actual number of entries in table
3227 data={ # Data columns, with types as key, and periods as values
3228 (-1 if type is not rate counter)
3229 conn_rate=<int>,
3230 gpc0=-1
3231 },
3232 length=<int>, # max string length for string table keys, key length
3233 # otherwise
3234 nopurge=<boolean>, # purge oldest entries when table is full
3235 type="ip" # can be "ip", "ipv6", "integer", "string", "binary"
3236 }
3237
3238.. js:function:: StickTable.lookup(key)
3239
3240 Returns stick table entry for given <key>
3241
3242 :param string key: Stick table key (IP addresses and strings are supported)
3243 :returns: Lua table
3244
3245.. js:function:: StickTable.dump([filter])
3246
3247 Returns all entries in stick table. An optional filter can be used
3248 to extract entries with specific data values. Filter is a table with valid
3249 comparison operators as keys followed by data type name and value pairs.
3250 Check out the HAProxy docs for "show table" for more details. For the
3251 reference, the supported operators are:
Aurelien DARRAGON21f7ebb2023-03-13 19:49:31 +01003252
Adis Nezirovic8878f8e2018-07-13 12:18:33 +02003253 "eq", "ne", "le", "lt", "ge", "gt"
3254
3255 For large tables, execution of this function can take a long time (for
3256 HAProxy standards). That's also true when filter is used, so take care and
3257 measure the impact.
3258
3259 :param table filter: Stick table filter
3260 :returns: Stick table entries (table)
3261
3262 See below for example filter, which contains 4 entries (or comparisons).
3263 (Maximum number of filter entries is 4, defined in the source code)
3264
3265.. code-block:: lua
3266
3267 local filter = {
3268 {"gpc0", "gt", 30}, {"gpc1", "gt", 20}}, {"conn_rate", "le", 10}
3269 }
3270
Christopher Faulet0f3c8902020-01-31 18:57:12 +01003271.. _action_class:
3272
3273Action class
3274=============
3275
3276.. js:class:: Act
3277
3278 **context**: action
3279
3280 This class contains all return codes an action may return. It is the lua
3281 equivalent to HAProxy "ACT_RET_*" code.
3282
3283.. code-block:: lua
3284
3285 core.register_action("deny", { "http-req" }, function (txn)
3286 return act.DENY
3287 end)
3288..
3289.. js:attribute:: act.CONTINUE
3290
3291 This attribute is an integer (0). It instructs HAProxy to continue the current
3292 ruleset processing on the message. It is the default return code for a lua
3293 action.
3294
3295 :returns: integer
3296
3297.. js:attribute:: act.STOP
3298
3299 This attribute is an integer (1). It instructs HAProxy to stop the current
3300 ruleset processing on the message.
3301
3302.. js:attribute:: act.YIELD
3303
3304 This attribute is an integer (2). It instructs HAProxy to temporarily pause
3305 the message processing. It will be resumed later on the same rule. The
3306 corresponding lua script is re-executed for the start.
3307
3308.. js:attribute:: act.ERROR
3309
3310 This attribute is an integer (3). It triggers an internal errors The message
3311 processing is stopped and the transaction is terminated. For HTTP streams, an
3312 HTTP 500 error is returned to the client.
3313
3314 :returns: integer
3315
3316.. js:attribute:: act.DONE
3317
3318 This attribute is an integer (4). It instructs HAProxy to stop the message
3319 processing.
3320
3321 :returns: integer
3322
3323.. js:attribute:: act.DENY
3324
3325 This attribute is an integer (5). It denies the current message. The message
3326 processing is stopped and the transaction is terminated. For HTTP streams, an
3327 HTTP 403 error is returned to the client if the deny is returned during the
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02003328 request analysis. During the response analysis, a HTTP 502 error is returned
Christopher Faulet0f3c8902020-01-31 18:57:12 +01003329 and the server response is discarded.
3330
3331 :returns: integer
3332
3333.. js:attribute:: act.ABORT
3334
3335 This attribute is an integer (6). It aborts the current message. The message
3336 processing is stopped and the transaction is terminated. For HTTP streams,
Willy Tarreau714f3452021-05-09 06:47:26 +02003337 HAProxy assumes a response was already sent to the client. From the Lua
Christopher Faulet0f3c8902020-01-31 18:57:12 +01003338 actions point of view, when this code is used, the transaction is terminated
3339 with no reply.
3340
3341 :returns: integer
3342
3343.. js:attribute:: act.INVALID
3344
3345 This attribute is an integer (7). It triggers an internal errors. The message
3346 processing is stopped and the transaction is terminated. For HTTP streams, an
3347 HTTP 400 error is returned to the client if the error is returned during the
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02003348 request analysis. During the response analysis, a HTTP 502 error is returned
Christopher Faulet0f3c8902020-01-31 18:57:12 +01003349 and the server response is discarded.
3350
3351 :returns: integer
Adis Nezirovic8878f8e2018-07-13 12:18:33 +02003352
Christopher Faulet2c2c2e32020-01-31 19:07:52 +01003353.. js:function:: act:wake_time(milliseconds)
3354
3355 **context**: action
3356
3357 Set the script pause timeout to the specified time, defined in
3358 milliseconds.
3359
3360 :param integer milliseconds: the required milliseconds.
3361
3362 This function may be used when a lua action returns `act.YIELD`, to force its
3363 wake-up at most after the specified number of milliseconds.
3364
Christopher Faulet5a2c6612021-08-15 20:35:25 +02003365.. _filter_class:
3366
3367Filter class
3368=============
3369
3370.. js:class:: filter
3371
3372 **context**: filter
3373
3374 This class contains return codes some filter callback functions may return. It
3375 also contains configuration flags and some helper functions. To understand how
3376 the filter API works, see `doc/internal/filters.txt` documentation.
3377
3378.. js:attribute:: filter.CONTINUE
3379
3380 This attribute is an integer (1). It may be returned by some filter callback
3381 functions to instruct this filtering step is finished for this filter.
3382
3383.. js:attribute:: filter.WAIT
3384
3385 This attribute is an integer (0). It may be returned by some filter callback
3386 functions to instruct the filtering must be paused, waiting for more data or
3387 for an external event depending on this filter.
3388
3389.. js:attribute:: filter.ERROR
3390
3391 This attribute is an integer (-1). It may be returned by some filter callback
3392 functions to trigger an error.
3393
3394.. js:attribute:: filter.FLT_CFG_FL_HTX
3395
3396 This attribute is a flag corresponding to the filter flag FLT_CFG_FL_HTX. When
3397 it is set for a filter, it means the filter is able to filter HTTP streams.
3398
3399.. js:function:: filter.register_data_filter(chn)
3400
3401 **context**: filter
3402
3403 Enable the data filtering on the channel **chn** for the current filter. It
Ilya Shipitsinff0f2782021-08-22 22:18:07 +05003404 may be called at any time from any callback functions proceeding the data
Christopher Faulet5a2c6612021-08-15 20:35:25 +02003405 analysis.
3406
3407 :param class_Channel chn: A :ref:`channel_class`.
3408
3409.. js:function:: filter.unregister_data_filter(chn)
3410
3411 **context**: filter
3412
3413 Disable the data filtering on the channel **chn** for the current filter. It
3414 may be called at any time from any callback functions.
3415
3416 :param class_Channel chn: A :ref:`channel_class`.
3417
3418.. js:function:: filter.wake_time(milliseconds)
3419
3420 **context**: filter
3421
3422 Set the script pause timeout to the specified time, defined in
3423 milliseconds.
3424
3425 :param integer milliseconds: the required milliseconds.
3426
3427 This function may be used from any lua filter callback function to force its
3428 wake-up at most after the specified number of milliseconds. Especially, when
3429 `filter.CONTINUE` is returned.
3430
3431
3432A filters is declared using :js:func:`core.register_filter()` function. The
3433provided class will be used to instantiate filters. It may define following
3434attributes:
3435
3436* id: The filter identifier. It is a string that identifies the filter and is
3437 optional.
3438
3439* flags: The filter flags. Only :js:attr:`filter.FLT_CFG_FL_HTX` may be set for now.
3440
3441Such filter class must also define all required callback functions in the
3442following list. Note that :js:func:`Filter.new()` must be defined otherwise the
Ilya Shipitsinff0f2782021-08-22 22:18:07 +05003443filter is ignored. Others are optional.
Christopher Faulet5a2c6612021-08-15 20:35:25 +02003444
3445* .. js:function:: FILTER.new()
3446
3447 Called to instantiate a new filter. This function must be defined.
3448
3449 :returns: a Lua object that will be used as filter instance for the current
3450 stream.
3451
3452* .. js:function:: FILTER.start_analyze(flt, txn, chn)
3453
3454 Called when the analysis starts on the channel **chn**.
3455
3456* .. js:function:: FILTER.end_analyze(flt, txn, chn)
3457
3458 Called when the analysis ends on the channel **chn**.
3459
3460* .. js:function:: FILTER.http_headers(flt, txn, http_msg)
3461
3462 Called just before the HTTP payload analysis and after any processing on the
3463 HTTP message **http_msg**. This callback functions is only called for HTTP
3464 streams.
3465
3466* .. js:function:: FILTER.http_payload(flt, txn, http_msg)
3467
3468 Called during the HTTP payload analysis on the HTTP message **http_msg**. This
3469 callback functions is only called for HTTP streams.
3470
3471* .. js:function:: FILTER.http_end(flt, txn, http_msg)
3472
3473 Called after the HTTP payload analysis on the HTTP message **http_msg**. This
3474 callback functions is only called for HTTP streams.
3475
3476* .. js:function:: FILTER.tcp_payload(flt, txn, chn)
3477
3478 Called during the TCP payload analysis on the channel **chn**.
3479
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02003480Here is a full example:
Christopher Faulet5a2c6612021-08-15 20:35:25 +02003481
3482.. code-block:: lua
3483
3484 Trace = {}
3485 Trace.id = "Lua trace filter"
3486 Trace.flags = filter.FLT_CFG_FL_HTX;
3487 Trace.__index = Trace
3488
3489 function Trace:new()
3490 local trace = {}
3491 setmetatable(trace, Trace)
3492 trace.req_len = 0
3493 trace.res_len = 0
3494 return trace
3495 end
3496
3497 function Trace:start_analyze(txn, chn)
3498 if chn:is_resp() then
3499 print("Start response analysis")
3500 else
3501 print("Start request analysis")
3502 end
3503 filter.register_data_filter(self, chn)
3504 end
3505
3506 function Trace:end_analyze(txn, chn)
3507 if chn:is_resp() then
3508 print("End response analysis: "..self.res_len.." bytes filtered")
3509 else
3510 print("End request analysis: "..self.req_len.." bytes filtered")
3511 end
3512 end
3513
3514 function Trace:http_headers(txn, http_msg)
3515 stline = http_msg:get_stline()
3516 if http_msg.channel:is_resp() then
3517 print("response:")
3518 print(stline.version.." "..stline.code.." "..stline.reason)
3519 else
3520 print("request:")
3521 print(stline.method.." "..stline.uri.." "..stline.version)
3522 end
3523
3524 for n, hdrs in pairs(http_msg:get_headers()) do
3525 for i,v in pairs(hdrs) do
3526 print(n..": "..v)
3527 end
3528 end
3529 return filter.CONTINUE
3530 end
3531
3532 function Trace:http_payload(txn, http_msg)
3533 body = http_msg:body(-20000)
3534 if http_msg.channel:is_resp() then
3535 self.res_len = self.res_len + body:len()
3536 else
3537 self.req_len = self.req_len + body:len()
3538 end
3539 end
3540
3541 core.register_filter("trace", Trace, function(trace, args)
3542 return trace
3543 end)
3544
3545..
3546
3547.. _httpmessage_class:
3548
3549HTTPMessage class
3550===================
3551
3552.. js:class:: HTTPMessage
3553
3554 **context**: filter
3555
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02003556 This class contains all functions to manipulate a HTTP message. For now, this
Christopher Faulet5a2c6612021-08-15 20:35:25 +02003557 class is only available from a filter context.
3558
3559.. js:function:: HTTPMessage.add_header(http_msg, name, value)
3560
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02003561 Appends a HTTP header field in the HTTP message **http_msg** whose name is
Christopher Faulet5a2c6612021-08-15 20:35:25 +02003562 specified in **name** and whose value is defined in **value**.
3563
3564 :param class_httpmessage http_msg: The manipulated HTTP message.
3565 :param string name: The header name.
3566 :param string value: The header value.
3567
3568.. js:function:: HTTPMessage.append(http_msg, string)
3569
3570 This function copies the string **string** at the end of incoming data of the
3571 HTTP message **http_msg**. The function returns the copied length on success
3572 or -1 if data cannot be copied.
3573
3574 Same that :js:func:`HTTPMessage.insert(http_msg, string, http_msg:input())`.
3575
3576 :param class_httpmessage http_msg: The manipulated HTTP message.
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02003577 :param string string: The data to copy at the end of incoming data.
Christopher Faulet5a2c6612021-08-15 20:35:25 +02003578 :returns: an integer containing the amount of bytes copied or -1.
3579
3580.. js:function:: HTTPMessage.body(http_msgl[, offset[, length]])
3581
3582 This function returns **length** bytes of incoming data from the HTTP message
3583 **http_msg**, starting at the offset **offset**. The data are not removed from
3584 the buffer.
3585
3586 By default, if no length is provided, all incoming data found, starting at the
3587 given offset, are returned. If **length** is set to -1, the function tries to
3588 retrieve a maximum of data. Because it is called in the filter context, it
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02003589 never yield. Not providing an offset is the same as setting it to 0. A
Ilya Shipitsinff0f2782021-08-22 22:18:07 +05003590 positive offset is relative to the beginning of incoming data of the
Christopher Faulet5a2c6612021-08-15 20:35:25 +02003591 http_message buffer while negative offset is relative to their end.
3592
3593 If there is no incoming data and the HTTP message can't receive more data, a 'nil'
3594 value is returned.
3595
3596 :param class_httpmessage http_msg: The manipulated HTTP message.
3597 :param integer offset: *optional* The offset in incoming data to start to get
3598 data. 0 by default. May be negative to be relative to
3599 the end of incoming data.
3600 :param integer length: *optional* The expected length of data to retrieve. All
3601 incoming data by default. May be set to -1 to get a
3602 maximum of data.
3603 :returns: a string containing the data found or nil.
3604
3605.. js:function:: HTTPMessage.eom(http_msg)
3606
3607 This function returns true if the end of message is reached for the HTTP
3608 message **http_msg**.
3609
3610 :param class_httpmessage http_msg: The manipulated HTTP message.
3611 :returns: an integer containing the amount of available bytes.
3612
3613.. js:function:: HTTPMessage.del_header(http_msg, name)
3614
3615 Removes all HTTP header fields in the HTTP message **http_msg** whose name is
3616 specified in **name**.
3617
3618 :param class_httpmessage http_msg: The manipulated http message.
3619 :param string name: The header name.
3620
3621.. js:function:: HTTPMessage.get_headers(http_msg)
3622
3623 Returns a table containing all the headers of the HTTP message **http_msg**.
3624
3625 :param class_httpmessage http_msg: The manipulated http message.
3626 :returns: table of headers.
3627
3628 This is the form of the returned table:
3629
3630.. code-block:: lua
3631
3632 http_msg:get_headers()['<header-name>'][<header-index>] = "<header-value>"
3633
3634 local hdr = http_msg:get_headers()
3635 hdr["host"][0] = "www.test.com"
3636 hdr["accept"][0] = "audio/basic q=1"
3637 hdr["accept"][1] = "audio/*, q=0.2"
3638 hdr["accept"][2] = "*.*, q=0.1"
3639..
3640
3641.. js:function:: HTTPMessage.get_stline(http_msg)
3642
3643 Returns a table containing the start-line of the HTTP message **http_msg**.
3644
3645 :param class_httpmessage http_msg: The manipulated http message.
3646 :returns: the start-line.
3647
3648 This is the form of the returned table:
3649
3650.. code-block:: lua
3651
3652 -- for the request :
3653 {"method" = string, "uri" = string, "version" = string}
3654
3655 -- for the response:
3656 {"version" = string, "code" = string, "reason" = string}
3657..
3658
3659.. js:function:: HTTPMessage.forward(http_msg, length)
3660
3661 This function forwards **length** bytes of data from the HTTP message
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02003662 **http_msg**. Because it is called in the filter context, it never yields. Only
Christopher Faulet5a2c6612021-08-15 20:35:25 +02003663 available incoming data may be forwarded, event if the requested length
3664 exceeds the available amount of incoming data. It returns the amount of data
3665 forwarded.
3666
3667 :param class_httpmessage http_msg: The manipulated HTTP message.
3668 :param integer int: The amount of data to forward.
3669
3670.. js:function:: HTTPMessage.input(http_msg)
3671
3672 This function returns the length of incoming data in the HTTP message
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02003673 **http_msg** from the filter point of view.
Christopher Faulet5a2c6612021-08-15 20:35:25 +02003674
3675 :param class_httpmessage http_msg: The manipulated HTTP message.
3676 :returns: an integer containing the amount of available bytes.
3677
3678.. js:function:: HTTPMessage.insert(http_msg, string[, offset])
3679
3680 This function copies the string **string** at the offset **offset** in
3681 incoming data of the HTTP message **http_msg**. The function returns the
3682 copied length on success or -1 if data cannot be copied.
3683
3684 By default, if no offset is provided, the string is copied in front of
Ilya Shipitsinff0f2782021-08-22 22:18:07 +05003685 incoming data. A positive offset is relative to the beginning of incoming data
Christopher Faulet5a2c6612021-08-15 20:35:25 +02003686 of the HTTP message while negative offset is relative to their end.
3687
3688 :param class_httpmessage http_msg: The manipulated HTTP message.
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02003689 :param string string: The data to copy into incoming data.
3690 :param integer offset: *optional* The offset in incoming data where to copy
Christopher Faulet5a2c6612021-08-15 20:35:25 +02003691 data. 0 by default. May be negative to be relative to
3692 the end of incoming data.
3693 :returns: an integer containing the amount of bytes copied or -1.
3694
3695.. js:function:: HTTPMessage.is_full(http_msg)
3696
3697 This function returns true if the HTTP message **http_msg** is full.
3698
3699 :param class_httpmessage http_msg: The manipulated HTTP message.
3700 :returns: a boolean
3701
3702.. js:function:: HTTPMessage.is_resp(http_msg)
3703
3704 This function returns true if the HTTP message **http_msg** is the response
3705 one.
3706
3707 :param class_httpmessage http_msg: The manipulated HTTP message.
3708 :returns: a boolean
3709
3710.. js:function:: HTTPMessage.may_recv(http_msg)
3711
3712 This function returns true if the HTTP message **http_msg** may still receive
3713 data.
3714
3715 :param class_httpmessage http_msg: The manipulated HTTP message.
3716 :returns: a boolean
3717
3718.. js:function:: HTTPMessage.output(http_msg)
3719
3720 This function returns the length of outgoing data of the HTTP message
3721 **http_msg**.
3722
3723 :param class_httpmessage http_msg: The manipulated HTTP message.
3724 :returns: an integer containing the amount of available bytes.
3725
3726.. js:function:: HTTPMessage.prepend(http_msg, string)
3727
3728 This function copies the string **string** in front of incoming data of the
3729 HTTP message **http_msg**. The function returns the copied length on success
3730 or -1 if data cannot be copied.
3731
3732 Same that :js:func:`HTTPMessage.insert(http_msg, string, 0)`.
3733
3734 :param class_httpmessage http_msg: The manipulated HTTP message.
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02003735 :param string string: The data to copy in front of incoming data.
Christopher Faulet5a2c6612021-08-15 20:35:25 +02003736 :returns: an integer containing the amount of bytes copied or -1.
3737
3738.. js:function:: HTTPMessage.remove(http_msg[, offset[, length]])
3739
3740 This function removes **length** bytes of incoming data of the HTTP message
3741 **http_msg**, starting at offset **offset**. This function returns number of
3742 bytes removed on success.
3743
3744 By default, if no length is provided, all incoming data, starting at the given
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02003745 offset, are removed. Not providing an offset is the same that setting it
Ilya Shipitsinff0f2782021-08-22 22:18:07 +05003746 to 0. A positive offset is relative to the beginning of incoming data of the
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02003747 HTTP message while negative offset is relative to the end.
Christopher Faulet5a2c6612021-08-15 20:35:25 +02003748
3749 :param class_httpmessage http_msg: The manipulated HTTP message.
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02003750 :param integer offset: *optional* The offset in incoming data where to start
Christopher Faulet5a2c6612021-08-15 20:35:25 +02003751 to remove data. 0 by default. May be negative to
3752 be relative to the end of incoming data.
3753 :param integer length: *optional* The length of data to remove. All incoming
3754 data by default.
3755 :returns: an integer containing the amount of bytes removed.
3756
3757.. js:function:: HTTPMessage.rep_header(http_msg, name, regex, replace)
3758
3759 Matches the regular expression in all occurrences of header field **name**
3760 according to regex **regex**, and replaces them with the string **replace**.
3761 The replacement value can contain back references like \1, \2, ... This
3762 function acts on whole header lines, regardless of the number of values they
3763 may contain.
3764
3765 :param class_httpmessage http_msg: The manipulated HTTP message.
3766 :param string name: The header name.
3767 :param string regex: The match regular expression.
3768 :param string replace: The replacement value.
3769
3770.. js:function:: HTTPMessage.rep_value(http_msg, name, regex, replace)
3771
3772 Matches the regular expression on every comma-delimited value of header field
3773 **name** according to regex **regex**, and replaces them with the string
3774 **replace**. The replacement value can contain back references like \1, \2,
3775 ...
3776
3777 :param class_httpmessage http_msg: The manipulated HTTP message.
3778 :param string name: The header name.
3779 :param string regex: The match regular expression.
3780 :param string replace: The replacement value.
3781
3782.. js:function:: HTTPMessage.send(http_msg, string)
3783
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02003784 This function requires immediate send of the string **string**. It means the
Ilya Shipitsinff0f2782021-08-22 22:18:07 +05003785 string is copied at the beginning of incoming data of the HTTP message
Christopher Faulet5a2c6612021-08-15 20:35:25 +02003786 **http_msg** and immediately forwarded. Because it is called in the filter
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02003787 context, it never yields.
Christopher Faulet5a2c6612021-08-15 20:35:25 +02003788
3789 :param class_httpmessage http_msg: The manipulated HTTP message.
3790 :param string string: The data to send.
3791 :returns: an integer containing the amount of bytes copied or -1.
3792
3793.. js:function:: HTTPMessage.set(http_msg, string[, offset[, length]])
3794
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02003795 This function replaces **length** bytes of incoming data of the HTTP message
Christopher Faulet5a2c6612021-08-15 20:35:25 +02003796 **http_msg**, starting at offset **offset**, by the string **string**. The
3797 function returns the copied length on success or -1 if data cannot be copied.
3798
3799 By default, if no length is provided, all incoming data, starting at the given
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02003800 offset, are replaced. Not providing an offset is the same as setting it
Ilya Shipitsinff0f2782021-08-22 22:18:07 +05003801 to 0. A positive offset is relative to the beginning of incoming data of the
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02003802 HTTP message while negative offset is relative to the end.
Christopher Faulet5a2c6612021-08-15 20:35:25 +02003803
3804 :param class_httpmessage http_msg: The manipulated HTTP message.
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02003805 :param string string: The data to copy into incoming data.
3806 :param integer offset: *optional* The offset in incoming data where to start
Christopher Faulet5a2c6612021-08-15 20:35:25 +02003807 the data replacement. 0 by default. May be negative to
3808 be relative to the end of incoming data.
3809 :param integer length: *optional* The length of data to replace. All incoming
3810 data by default.
3811 :returns: an integer containing the amount of bytes copied or -1.
3812
3813.. js:function:: HTTPMessage.set_eom(http_msg)
3814
3815 This function set the end of message for the HTTP message **http_msg**.
3816
3817 :param class_httpmessage http_msg: The manipulated HTTP message.
3818
3819.. js:function:: HTTPMessage.set_header(http_msg, name, value)
3820
3821 This variable replace all occurrence of all header matching the name **name**,
3822 by only one containing the value **value**.
3823
3824 :param class_httpmessage http_msg: The manipulated HTTP message.
3825 :param string name: The header name.
3826 :param string value: The header value.
3827
3828 This function does the same work as the following code:
3829
3830.. code-block:: lua
3831
3832 http_msg:del_header("header")
3833 http_msg:add_header("header", "value")
3834..
3835
3836.. js:function:: HTTPMessage.set_method(http_msg, method)
3837
3838 Rewrites the request method with the string **method**. The HTTP message
3839 **http_msg** must be the request.
3840
3841 :param class_httpmessage http_msg: The manipulated HTTP message.
3842 :param string method: The new method.
3843
3844.. js:function:: HTTPMessage.set_path(http_msg, path)
3845
3846 Rewrites the request path with the string **path**. The HTTP message
3847 **http_msg** must be the request.
3848
3849 :param class_httpmessage http_msg: The manipulated HTTP message.
3850 :param string method: The new method.
3851
3852.. js:function:: HTTPMessage.set_query(http_msg, query)
3853
3854 Rewrites the request's query string which appears after the first question
3855 mark ("?") with the string **query**. The HTTP message **http_msg** must be
3856 the request.
3857
3858 :param class_httpmessage http_msg: The manipulated HTTP message.
3859 :param string query: The new query.
3860
3861.. js:function:: HTTPMessage.set_status(http_msg, status[, reason])
3862
Ilya Shipitsinff0f2782021-08-22 22:18:07 +05003863 Rewrites the response status code with the integer **code** and optional the
Christopher Faulet5a2c6612021-08-15 20:35:25 +02003864 reason **reason**. If no custom reason is provided, it will be generated from
3865 the status. The HTTP message **http_msg** must be the response.
3866
3867 :param class_httpmessage http_msg: The manipulated HTTP message.
3868 :param integer status: The new response status code.
3869 :param string reason: The new response reason (optional).
3870
3871.. js:function:: HTTPMessage.set_uri(http_msg, uri)
3872
3873 Rewrites the request URI with the string **uri**. The HTTP message
3874 **http_msg** must be the request.
3875
3876 :param class_httpmessage http_msg: The manipulated HTTP message.
3877 :param string uri: The new uri.
3878
3879.. js:function:: HTTPMessage.unset_eom(http_msg)
3880
3881 This function remove the end of message for the HTTP message **http_msg**.
3882
3883 :param class_httpmessage http_msg: The manipulated HTTP message.
3884
William Lallemand10cea5c2022-03-30 16:02:43 +02003885.. _CertCache_class:
3886
3887CertCache class
3888================
3889
3890.. js:class:: CertCache
3891
3892 This class allows to update an SSL certificate file in the memory of the
3893 current HAProxy process. It will do the same as "set ssl cert" + "commit ssl
3894 cert" over the HAProxy CLI.
3895
3896.. js:function:: CertCache.set(certificate)
3897
3898 This function updates a certificate in memory.
3899
3900 :param table certificate: A table containing the fields to update.
3901 :param string certificate.filename: The mandatory filename of the certificate
3902 to update, it must already exist in memory.
3903 :param string certificate.crt: A certificate in the PEM format. It can also
3904 contain a private key.
3905 :param string certificate.key: A private key in the PEM format.
3906 :param string certificate.ocsp: An OCSP response in base64. (cf management.txt)
3907 :param string certificate.issuer: The certificate of the OCSP issuer.
3908 :param string certificate.sctl: An SCTL file.
3909
3910.. code-block:: lua
3911
3912 CertCache.set{filename="certs/localhost9994.pem.rsa", crt=crt}
3913
3914
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01003915External Lua libraries
3916======================
3917
3918A lot of useful lua libraries can be found here:
3919
Aurelien DARRAGON846fc7d2022-10-14 08:48:57 +02003920* Lua toolbox has been superseded by `https://luarocks.org/ <https://luarocks.org/>`_
3921 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 +01003922
Ilya Shipitsin2075ca82020-03-06 23:22:22 +05003923Redis client library:
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01003924
3925* `https://github.com/nrk/redis-lua <https://github.com/nrk/redis-lua>`_
3926
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02003927This is an example about the usage of the Redis library within HAProxy. Note that
3928each call to any function of this library can throw an error if the socket
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01003929connection fails.
3930
3931.. code-block:: lua
3932
3933 -- load the redis library
3934 local redis = require("redis");
3935
3936 function do_something(txn)
3937
3938 -- create and connect new tcp socket
3939 local tcp = core.tcp();
3940 tcp:settimeout(1);
3941 tcp:connect("127.0.0.1", 6379);
3942
3943 -- use the redis library with this new socket
3944 local client = redis.connect({socket=tcp});
3945 client:ping();
3946
3947 end
3948
3949OpenSSL:
3950
3951* `http://mkottman.github.io/luacrypto/index.html
3952 <http://mkottman.github.io/luacrypto/index.html>`_
3953
3954* `https://github.com/brunoos/luasec/wiki
3955 <https://github.com/brunoos/luasec/wiki>`_