blob: 70dbf63ba4e15c0b13ce4fc2104ef2fe92f90e16 [file] [log] [blame]
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001.. toctree::
2 :maxdepth: 2
3
4
5How Lua runs in HAProxy
6=======================
7
8HAProxy Lua running contexts
9----------------------------
10
11The Lua code executed in HAProxy can be processed in 2 main modes. The first one
12is the **initialisation mode**, and the second is the **runtime mode**.
13
14* In the **initialisation mode**, we can perform DNS solves, but we cannot
15 perform socket I/O. In this initialisation mode, HAProxy still blocked during
16 the execution of the Lua program.
17
18* In the **runtime mode**, we cannot perform DNS solves, but we can use sockets.
19 The execution of the Lua code is multiplexed with the requests processing, so
20 the Lua code seems to be run in blocking, but it is not the case.
21
22The Lua code is loaded in one or more files. These files contains main code and
Christopher Faulet5a2c6612021-08-15 20:35:25 +020023functions. Lua have 7 execution context.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +010024
251. The Lua file **body context**. It is executed during the load of the Lua file
26 in the HAProxy `[global]` section with the directive `lua-load`. It is
27 executed in initialisation mode. This section is use for configuring Lua
28 bindings in HAProxy.
29
David Carlier61fdf8b2015-10-02 11:59:38 +0100302. The Lua **init context**. It is a Lua function executed just after the
Thierry FOURNIER17bd1522015-03-11 20:31:00 +010031 HAProxy configuration parsing. The execution is in initialisation mode. In
32 this context the HAProxy environment are already initialized. It is useful to
33 check configuration, or initializing socket connections or tasks. These
34 functions are declared in the body context with the Lua function
35 `core.register_init()`. The prototype of the function is a simple function
36 without return value and without parameters, like this: `function fcn()`.
37
David Carlier61fdf8b2015-10-02 11:59:38 +0100383. The Lua **task context**. It is a Lua function executed after the start
Thierry FOURNIER17bd1522015-03-11 20:31:00 +010039 of the HAProxy scheduler, and just after the declaration of the task with the
40 Lua function `core.register_task()`. This context can be concurrent with the
41 traffic processing. It is executed in runtime mode. The prototype of the
42 function is a simple function without return value and without parameters,
43 like this: `function fcn()`.
44
David Carlier61fdf8b2015-10-02 11:59:38 +0100454. The **action context**. It is a Lua function conditionally executed. These
Thierry FOURNIERa2d02252015-10-01 15:00:42 +020046 actions are registered by the Lua directives "`core.register_action()`". The
47 prototype of the Lua called function is a function with doesn't returns
48 anything and that take an object of class TXN as entry. `function fcn(txn)`.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +010049
505. The **sample-fetch context**. This function takes a TXN object as entry
51 argument and returns a string. These types of function cannot execute any
52 blocking function. They are useful to aggregate some of original HAProxy
53 sample-fetches and return the result. The prototype of the function is
54 `function string fcn(txn)`. These functions can be registered with the Lua
55 function `core.register_fetches()`. Each declared sample-fetch is prefixed by
56 the string "lua.".
57
Christopher Faulet1e9b1b62021-08-11 10:14:30 +020058 .. note::
59 It is possible that this function cannot found the required data in the
60 original HAProxy sample-fetches, in this case, it cannot return the
61 result. This case is not yet supported
Thierry FOURNIER17bd1522015-03-11 20:31:00 +010062
David Carlier61fdf8b2015-10-02 11:59:38 +0100636. The **converter context**. It is a Lua function that takes a string as input
Thierry FOURNIER17bd1522015-03-11 20:31:00 +010064 and returns another string as output. These types of function are stateless,
65 it cannot access to any context. They don't execute any blocking function.
66 The call prototype is `function string fcn(string)`. This function can be
67 registered with the Lua function `core.register_converters()`. Each declared
68 converter is prefixed by the string "lua.".
69
Christopher Faulet5a2c6612021-08-15 20:35:25 +0200707. The **filter context**: It is a Lua object based on a class defining filter
71 callback functions. Lua filters are registered using
72 `core.register_filter()`. Each declared filter is prefixed by the string
73 "lua.".
74
75 .. warning::
76 The Lua filter support is highly experimental. The API is still unstable
77 and may change without notice. No backward compatibility should be
78 expected for now. Use it with an extreme caution and report any issue or
79 comment about it. The feature was unveiled to improve it and to adapt it
80 to real usages.
81
82
Thierry FOURNIER17bd1522015-03-11 20:31:00 +010083HAProxy Lua Hello world
84-----------------------
85
86HAProxy configuration file (`hello_world.conf`):
87
88::
89
90 global
91 lua-load hello_world.lua
92
93 listen proxy
94 bind 127.0.0.1:10001
Thierry FOURNIERa2d02252015-10-01 15:00:42 +020095 tcp-request inspect-delay 1s
96 tcp-request content use-service lua.hello_world
Thierry FOURNIER17bd1522015-03-11 20:31:00 +010097
98HAProxy Lua file (`hello_world.lua`):
99
100.. code-block:: lua
101
Thierry FOURNIERa2d02252015-10-01 15:00:42 +0200102 core.register_service("hello_world", "tcp", function(applet)
103 applet:send("hello world\n")
104 end)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100105
106How to start HAProxy for testing this configuration:
107
108::
109
110 ./haproxy -f hello_world.conf
111
112On other terminal, you can test with telnet:
113
114::
115
116 #:~ telnet 127.0.0.1 10001
117 hello world
118
Thierry Fournierae6b5682022-09-19 09:04:16 +0200119Usage of load parameters
120------------------------
121
Ilya Shipitsin4a689da2022-10-29 09:34:32 +0500122HAProxy lua-load(-per-thread) directives allow a list of parameters after
Thierry Fournierae6b5682022-09-19 09:04:16 +0200123the lua file name. These parameters are accessible through an array of args
124using this code `local args = table.pack(...)` in the body of loaded file.
125
126Below, a new version of the hello world using load parameters
127
128HAProxy configuration file (`hello_world.conf`):
129
130::
131
132 global
133 lua-load hello_world.lua "this is not an hello world"
134
135 listen proxy
136 bind 127.0.0.1:10001
137 tcp-request inspect-delay 1s
138 tcp-request content use-service lua.hello_world
139
140HAProxy Lua file (`hello_world.lua`):
141
142.. code-block:: lua
143
144 local args = table.pack(...)
145
146 core.register_service("hello_world", "tcp", function(applet)
147 applet:send(args[1] .. "\n")
148 end)
149
150
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100151Core class
152==========
153
154.. js:class:: core
155
156 The "core" class contains all the HAProxy core functions. These function are
Aurelien DARRAGON53901f42022-10-13 19:49:42 +0200157 useful for the controlling of the execution flow, registering hooks, manipulating
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100158 global maps or ACL, ...
159
160 "core" class is basically provided with HAProxy. No `require` line is
161 required to uses these function.
162
David Carlier61fdf8b2015-10-02 11:59:38 +0100163 The "core" class is static, it is not possible to create a new object of this
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100164 type.
165
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100166.. js:attribute:: core.emerg
167
Thierry FOURNIERdc595002015-12-21 11:13:52 +0100168 :returns: integer
169
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100170 This attribute is an integer, it contains the value of the loglevel "emergency" (0).
171
172.. js:attribute:: core.alert
173
Thierry FOURNIERdc595002015-12-21 11:13:52 +0100174 :returns: integer
175
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100176 This attribute is an integer, it contains the value of the loglevel "alert" (1).
177
178.. js:attribute:: core.crit
179
Thierry FOURNIERdc595002015-12-21 11:13:52 +0100180 :returns: integer
181
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100182 This attribute is an integer, it contains the value of the loglevel "critical" (2).
183
184.. js:attribute:: core.err
185
Thierry FOURNIERdc595002015-12-21 11:13:52 +0100186 :returns: integer
187
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100188 This attribute is an integer, it contains the value of the loglevel "error" (3).
189
190.. js:attribute:: core.warning
191
Thierry FOURNIERdc595002015-12-21 11:13:52 +0100192 :returns: integer
193
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100194 This attribute is an integer, it contains the value of the loglevel "warning" (4).
195
196.. js:attribute:: core.notice
197
Thierry FOURNIERdc595002015-12-21 11:13:52 +0100198 :returns: integer
199
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100200 This attribute is an integer, it contains the value of the loglevel "notice" (5).
201
202.. js:attribute:: core.info
203
Thierry FOURNIERdc595002015-12-21 11:13:52 +0100204 :returns: integer
205
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100206 This attribute is an integer, it contains the value of the loglevel "info" (6).
207
208.. js:attribute:: core.debug
209
Thierry FOURNIERdc595002015-12-21 11:13:52 +0100210 :returns: integer
211
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100212 This attribute is an integer, it contains the value of the loglevel "debug" (7).
213
Thierry FOURNIER12a865d2016-12-14 19:40:37 +0100214.. js:attribute:: core.proxies
215
216 **context**: task, action, sample-fetch, converter
217
Patrick Hemmerc6a1d712018-05-01 21:30:41 -0400218 This attribute is a table of declared proxies (frontend and backends). Each
219 proxy give an access to his list of listeners and servers. The table is
220 indexed by proxy name, and each entry is of type :ref:`proxy_class`.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +0100221
Christopher Faulet1e9b1b62021-08-11 10:14:30 +0200222 .. Warning::
Aurelien DARRAGON53901f42022-10-13 19:49:42 +0200223 if you declared a frontend and backend with the same name, only one of
224 them will be listed.
Thierry FOURNIER9b82a582017-07-24 13:30:43 +0200225
226 :see: :js:attr:`core.backends`
227 :see: :js:attr:`core.frontends`
228
229.. js:attribute:: core.backends
230
231 **context**: task, action, sample-fetch, converter
232
Patrick Hemmerc6a1d712018-05-01 21:30:41 -0400233 This attribute is a table of declared proxies with backend capability. Each
234 proxy give an access to his list of listeners and servers. The table is
235 indexed by the backend name, and each entry is of type :ref:`proxy_class`.
Thierry FOURNIER9b82a582017-07-24 13:30:43 +0200236
237 :see: :js:attr:`core.proxies`
238 :see: :js:attr:`core.frontends`
239
240.. js:attribute:: core.frontends
241
242 **context**: task, action, sample-fetch, converter
243
Patrick Hemmerc6a1d712018-05-01 21:30:41 -0400244 This attribute is a table of declared proxies with frontend capability. Each
245 proxy give an access to his list of listeners and servers. The table is
246 indexed by the frontend name, and each entry is of type :ref:`proxy_class`.
Thierry FOURNIER9b82a582017-07-24 13:30:43 +0200247
248 :see: :js:attr:`core.proxies`
249 :see: :js:attr:`core.backends`
250
Thierry Fournierecb83c22020-11-28 15:49:44 +0100251.. js:attribute:: core.thread
252
253 **context**: task, action, sample-fetch, converter, applet
254
255 This variable contains the executing thread number starting at 1. 0 is a
256 special case for the common lua context. So, if thread is 0, Lua scope is
257 shared by all threads, otherwise the scope is dedicated to a single thread.
258 A program which needs to execute some parts exactly once regardless of the
259 number of threads can check that core.thread is 0 or 1.
260
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100261.. js:function:: core.log(loglevel, msg)
262
263 **context**: body, init, task, action, sample-fetch, converter
264
David Carlier61fdf8b2015-10-02 11:59:38 +0100265 This function sends a log. The log is sent, according with the HAProxy
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100266 configuration file, on the default syslog server if it is configured and on
267 the stderr if it is allowed.
268
Bertrand Jacquin874a35c2018-09-10 21:26:07 +0100269 :param integer loglevel: Is the log level associated with the message. It is a
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100270 number between 0 and 7.
271 :param string msg: The log content.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +0100272 :see: :js:attr:`core.emerg`, :js:attr:`core.alert`, :js:attr:`core.crit`,
273 :js:attr:`core.err`, :js:attr:`core.warning`, :js:attr:`core.notice`,
274 :js:attr:`core.info`, :js:attr:`core.debug` (log level definitions)
275 :see: :js:func:`core.Debug`
276 :see: :js:func:`core.Info`
277 :see: :js:func:`core.Warning`
278 :see: :js:func:`core.Alert`
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100279
280.. js:function:: core.Debug(msg)
281
282 **context**: body, init, task, action, sample-fetch, converter
283
284 :param string msg: The log content.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +0100285 :see: :js:func:`core.log`
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100286
287 Does the same job than:
288
289.. code-block:: lua
290
Thierry FOURNIERdc595002015-12-21 11:13:52 +0100291 function Debug(msg)
292 core.log(core.debug, msg)
293 end
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100294..
295
296.. js:function:: core.Info(msg)
297
298 **context**: body, init, task, action, sample-fetch, converter
299
300 :param string msg: The log content.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +0100301 :see: :js:func:`core.log`
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100302
303.. code-block:: lua
304
Thierry FOURNIERdc595002015-12-21 11:13:52 +0100305 function Info(msg)
306 core.log(core.info, msg)
307 end
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100308..
309
310.. js:function:: core.Warning(msg)
311
312 **context**: body, init, task, action, sample-fetch, converter
313
314 :param string msg: The log content.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +0100315 :see: :js:func:`core.log`
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100316
317.. code-block:: lua
318
Thierry FOURNIERdc595002015-12-21 11:13:52 +0100319 function Warning(msg)
320 core.log(core.warning, msg)
321 end
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100322..
323
324.. js:function:: core.Alert(msg)
325
326 **context**: body, init, task, action, sample-fetch, converter
327
328 :param string msg: The log content.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +0100329 :see: :js:func:`core.log`
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100330
331.. code-block:: lua
332
Thierry FOURNIERdc595002015-12-21 11:13:52 +0100333 function Alert(msg)
334 core.log(core.alert, msg)
335 end
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100336..
337
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100338.. js:function:: core.add_acl(filename, key)
339
340 **context**: init, task, action, sample-fetch, converter
341
342 Add the ACL *key* in the ACLs list referenced by the file *filename*.
343
344 :param string filename: the filename that reference the ACL entries.
345 :param string key: the key which will be added.
346
347.. js:function:: core.del_acl(filename, key)
348
349 **context**: init, task, action, sample-fetch, converter
350
351 Delete the ACL entry referenced by the key *key* in the list of ACLs
352 referenced by *filename*.
353
354 :param string filename: the filename that reference the ACL entries.
355 :param string key: the key which will be deleted.
356
357.. js:function:: core.del_map(filename, key)
358
359 **context**: init, task, action, sample-fetch, converter
360
361 Delete the map entry indexed with the specified key in the list of maps
362 referenced by his filename.
363
364 :param string filename: the filename that reference the map entries.
365 :param string key: the key which will be deleted.
366
Thierry Fourniereea77c02016-03-18 08:47:13 +0100367.. js:function:: core.get_info()
368
369 **context**: body, init, task, action, sample-fetch, converter
370
Aurelien DARRAGON53901f42022-10-13 19:49:42 +0200371 Returns HAProxy core information. We can find information like the uptime,
Thierry Fourniereea77c02016-03-18 08:47:13 +0100372 the pid, memory pool usage, tasks number, ...
373
Aurelien DARRAGON53901f42022-10-13 19:49:42 +0200374 These informations are also returned by the management socket via the command
Bertrand Jacquin874a35c2018-09-10 21:26:07 +0100375 "show info". See the management socket documentation for more information
Thierry Fourniereea77c02016-03-18 08:47:13 +0100376 about the content of these variables.
377
378 :returns: an array of values.
379
Thierry Fournierb1f46562016-01-21 09:46:15 +0100380.. js:function:: core.now()
381
382 **context**: body, init, task, action
383
384 This function returns the current time. The time returned is fixed by the
Bertrand Jacquin874a35c2018-09-10 21:26:07 +0100385 HAProxy core and assures than the hour will be monotonic and that the system
Thierry Fournierb1f46562016-01-21 09:46:15 +0100386 call 'gettimeofday' will not be called too. The time is refreshed between each
387 Lua execution or resume, so two consecutive call to the function "now" will
388 probably returns the same result.
389
Patrick Hemmerc6a1d712018-05-01 21:30:41 -0400390 :returns: a table which contains two entries "sec" and "usec". "sec"
Thierry Fournierb1f46562016-01-21 09:46:15 +0100391 contains the current at the epoch format, and "usec" contains the
392 current microseconds.
393
Thierry FOURNIERa78f0372016-12-14 19:04:41 +0100394.. js:function:: core.http_date(date)
395
396 **context**: body, init, task, action
397
Bertrand Jacquin874a35c2018-09-10 21:26:07 +0100398 This function take a string representing http date, and returns an integer
Thierry FOURNIERa78f0372016-12-14 19:04:41 +0100399 containing the corresponding date with a epoch format. A valid http date
400 me respect the format IMF, RFC850 or ASCTIME.
401
402 :param string date: a date http-date formatted
403 :returns: integer containing epoch date
404 :see: :js:func:`core.imf_date`.
405 :see: :js:func:`core.rfc850_date`.
406 :see: :js:func:`core.asctime_date`.
407 :see: https://tools.ietf.org/html/rfc7231#section-7.1.1.1
408
409.. js:function:: core.imf_date(date)
410
411 **context**: body, init, task, action
412
Bertrand Jacquin874a35c2018-09-10 21:26:07 +0100413 This function take a string representing IMF date, and returns an integer
Thierry FOURNIERa78f0372016-12-14 19:04:41 +0100414 containing the corresponding date with a epoch format.
415
416 :param string date: a date IMF formatted
417 :returns: integer containing epoch date
418 :see: https://tools.ietf.org/html/rfc7231#section-7.1.1.1
419
420 The IMF format is like this:
421
422.. code-block:: text
423
424 Sun, 06 Nov 1994 08:49:37 GMT
425..
426
427.. js:function:: core.rfc850_date(date)
428
429 **context**: body, init, task, action
430
Bertrand Jacquin874a35c2018-09-10 21:26:07 +0100431 This function take a string representing RFC850 date, and returns an integer
Thierry FOURNIERa78f0372016-12-14 19:04:41 +0100432 containing the corresponding date with a epoch format.
433
434 :param string date: a date RFC859 formatted
435 :returns: integer containing epoch date
436 :see: https://tools.ietf.org/html/rfc7231#section-7.1.1.1
437
438 The RFC850 format is like this:
439
440.. code-block:: text
441
442 Sunday, 06-Nov-94 08:49:37 GMT
443..
444
445.. js:function:: core.asctime_date(date)
446
447 **context**: body, init, task, action
448
Bertrand Jacquin874a35c2018-09-10 21:26:07 +0100449 This function take a string representing ASCTIME date, and returns an integer
Thierry FOURNIERa78f0372016-12-14 19:04:41 +0100450 containing the corresponding date with a epoch format.
451
452 :param string date: a date ASCTIME formatted
453 :returns: integer containing epoch date
454 :see: https://tools.ietf.org/html/rfc7231#section-7.1.1.1
455
456 The ASCTIME format is like this:
457
458.. code-block:: text
459
460 Sun Nov 6 08:49:37 1994
461..
462
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100463.. js:function:: core.msleep(milliseconds)
464
465 **context**: body, init, task, action
466
467 The `core.msleep()` stops the Lua execution between specified milliseconds.
468
469 :param integer milliseconds: the required milliseconds.
470
Thierry FOURNIERc5d11c62018-02-12 14:46:54 +0100471.. js:function:: core.register_action(name, actions, func [, nb_args])
Thierry FOURNIER8255a752015-09-23 21:03:35 +0200472
473 **context**: body
474
David Carlier61fdf8b2015-10-02 11:59:38 +0100475 Register a Lua function executed as action. All the registered action can be
Thierry FOURNIER8255a752015-09-23 21:03:35 +0200476 used in HAProxy with the prefix "lua.". An action gets a TXN object class as
477 input.
478
479 :param string name: is the name of the converter.
Willy Tarreau61add3c2015-09-28 15:39:10 +0200480 :param table actions: is a table of string describing the HAProxy actions who
Thierry FOURNIER8255a752015-09-23 21:03:35 +0200481 want to register to. The expected actions are 'tcp-req',
482 'tcp-res', 'http-req' or 'http-res'.
Aurelien DARRAGON53901f42022-10-13 19:49:42 +0200483 :param function func: is the Lua function called to work as converter.
Thierry FOURNIERc5d11c62018-02-12 14:46:54 +0100484 :param integer nb_args: is the expected number of argument for the action.
485 By default the value is 0.
Thierry FOURNIER8255a752015-09-23 21:03:35 +0200486
487 The prototype of the Lua function used as argument is:
488
489.. code-block:: lua
490
Thierry FOURNIERc5d11c62018-02-12 14:46:54 +0100491 function(txn [, arg1 [, arg2]])
Thierry FOURNIER8255a752015-09-23 21:03:35 +0200492..
493
Thierry FOURNIERdc595002015-12-21 11:13:52 +0100494 * **txn** (:ref:`txn_class`): this is a TXN object used for manipulating the
Thierry FOURNIER8255a752015-09-23 21:03:35 +0200495 current request or TCP stream.
496
Bertrand Jacquin874a35c2018-09-10 21:26:07 +0100497 * **argX**: this is argument provided through the HAProxy configuration file.
Thierry FOURNIERc5d11c62018-02-12 14:46:54 +0100498
Bertrand Jacquin874a35c2018-09-10 21:26:07 +0100499 Here, an example of action registration. The action just send an 'Hello world'
Thierry FOURNIER8255a752015-09-23 21:03:35 +0200500 in the logs.
501
502.. code-block:: lua
503
504 core.register_action("hello-world", { "tcp-req", "http-req" }, function(txn)
505 txn:Info("Hello world")
506 end)
507..
508
Willy Tarreau714f3452021-05-09 06:47:26 +0200509 This example code is used in HAProxy configuration like this:
Thierry FOURNIER8255a752015-09-23 21:03:35 +0200510
511::
512
513 frontend tcp_frt
514 mode tcp
515 tcp-request content lua.hello-world
516
517 frontend http_frt
518 mode http
519 http-request lua.hello-world
Thierry FOURNIERc5d11c62018-02-12 14:46:54 +0100520..
521
Bertrand Jacquin874a35c2018-09-10 21:26:07 +0100522 A second example using arguments
Thierry FOURNIERc5d11c62018-02-12 14:46:54 +0100523
524.. code-block:: lua
525
526 function hello_world(txn, arg)
527 txn:Info("Hello world for " .. arg)
528 end
529 core.register_action("hello-world", { "tcp-req", "http-req" }, hello_world, 2)
530..
Thierry FOURNIER8255a752015-09-23 21:03:35 +0200531
Willy Tarreau714f3452021-05-09 06:47:26 +0200532 This example code is used in HAProxy configuration like this:
Thierry FOURNIERc5d11c62018-02-12 14:46:54 +0100533
534::
535
536 frontend tcp_frt
537 mode tcp
538 tcp-request content lua.hello-world everybody
539..
Aurelien DARRAGON53901f42022-10-13 19:49:42 +0200540
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100541.. js:function:: core.register_converters(name, func)
542
543 **context**: body
544
David Carlier61fdf8b2015-10-02 11:59:38 +0100545 Register a Lua function executed as converter. All the registered converters
Aurelien DARRAGON53901f42022-10-13 19:49:42 +0200546 can be used in HAProxy with the prefix "lua.". A converter gets a string as
547 input and returns a string as output. The registered function can take up to 9
548 values as parameter. All the values are strings.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100549
550 :param string name: is the name of the converter.
551 :param function func: is the Lua function called to work as converter.
552
553 The prototype of the Lua function used as argument is:
554
555.. code-block:: lua
556
557 function(str, [p1 [, p2 [, ... [, p5]]]])
558..
559
560 * **str** (*string*): this is the input value automatically converted in
561 string.
562 * **p1** .. **p5** (*string*): this is a list of string arguments declared in
Bertrand Jacquin874a35c2018-09-10 21:26:07 +0100563 the HAProxy configuration file. The number of arguments doesn't exceed 5.
Aurelien DARRAGON53901f42022-10-13 19:49:42 +0200564 The order and the nature of these is conventionally chosen by the
Bertrand Jacquin874a35c2018-09-10 21:26:07 +0100565 developer.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100566
567.. js:function:: core.register_fetches(name, func)
568
569 **context**: body
570
David Carlier61fdf8b2015-10-02 11:59:38 +0100571 Register a Lua function executed as sample fetch. All the registered sample
Bertrand Jacquin874a35c2018-09-10 21:26:07 +0100572 fetch can be used in HAProxy with the prefix "lua.". A Lua sample fetch
Aurelien DARRAGON53901f42022-10-13 19:49:42 +0200573 returns a string as output. The registered function can take up to 9 values as
574 parameter. All the values are strings.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100575
Aurelien DARRAGON53901f42022-10-13 19:49:42 +0200576 :param string name: is the name of the sample fetch.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100577 :param function func: is the Lua function called to work as sample fetch.
578
579 The prototype of the Lua function used as argument is:
580
581.. code-block:: lua
582
583 string function(txn, [p1 [, p2 [, ... [, p5]]]])
584..
585
Thierry FOURNIERdc595002015-12-21 11:13:52 +0100586 * **txn** (:ref:`txn_class`): this is the txn object associated with the current
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100587 request.
588 * **p1** .. **p5** (*string*): this is a list of string arguments declared in
Bertrand Jacquin874a35c2018-09-10 21:26:07 +0100589 the HAProxy configuration file. The number of arguments doesn't exceed 5.
Aurelien DARRAGON53901f42022-10-13 19:49:42 +0200590 The order and the nature of these is conventionally chosen by the
Bertrand Jacquin874a35c2018-09-10 21:26:07 +0100591 developer.
592 * **Returns**: A string containing some data, or nil if the value cannot be
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100593 returned now.
594
595 lua example code:
596
597.. code-block:: lua
598
599 core.register_fetches("hello", function(txn)
600 return "hello"
601 end)
602..
603
604 HAProxy example configuration:
605
606::
607
608 frontend example
609 http-request redirect location /%[lua.hello]
610
Christopher Faulet5a2c6612021-08-15 20:35:25 +0200611.. js:function:: core.register_filter(name, Flt, func)
612
613 **context**: body
614
615 Register a Lua function used to declare a filter. All the registered filters
616 can by used in HAProxy with the prefix "lua.".
617
618 :param string name: is the name of the filter.
619 :param table Flt: is a Lua class containing the filter definition (id, flags,
620 callbacks).
621 :param function func: is the Lua function called to create the Lua filter.
622
623 The prototype of the Lua function used as argument is:
624
625.. code-block:: lua
626
627 function(flt, args)
628..
629
630 * **flt** : Is a filter object based on the class provided in
631 :js:func:`core.register_filter()` function.
632
633 * **args**: Is a table of strings containing all arguments provided through
634 the HAProxy configuration file, on the filter line.
635
636 It must return the filter to use or nil to ignore it. Here, an example of
637 filter registration.
638
639.. code-block:: lua
640
641 core.register_filter("my-filter", MyFilter, function(flt, args)
642 flt.args = args -- Save arguments
643 return flt
644 end)
645..
646
647 This example code is used in HAProxy configuration like this:
648
649::
650
651 frontend http
652 mode http
653 filter lua.my-filter arg1 arg2 arg3
654..
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
730.. js:function:: core.register_task(func)
731
732 **context**: body, init, task, action, sample-fetch, converter
733
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
Pieter Baauw4d7f7662015-11-08 16:38:08 +0100738 :param function func: is the Lua function called to work as initializer.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100739
740 The prototype of the Lua function used as argument is:
741
742.. code-block:: lua
743
744 function()
745..
746
747 It takes no input, and no output is expected.
748
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +0100749.. js:function:: core.register_cli([path], usage, func)
750
751 **context**: body
752
Aurelien DARRAGON53901f42022-10-13 19:49:42 +0200753 Register a custom cli that will be available from haproxy stats socket.
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +0100754
755 :param array path: is the sequence of word for which the cli execute the Lua
756 binding.
757 :param string usage: is the usage message displayed in the help.
758 :param function func: is the Lua function called to handle the CLI commands.
759
760 The prototype of the Lua function used as argument is:
761
762.. code-block:: lua
763
764 function(AppletTCP, [arg1, [arg2, [...]]])
765..
766
767 I/O are managed with the :ref:`applettcp_class` object. Args are given as
Bertrand Jacquin874a35c2018-09-10 21:26:07 +0100768 parameter. The args embed the registered path. If the path is declared like
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +0100769 this:
770
771.. code-block:: lua
772
773 core.register_cli({"show", "ssl", "stats"}, "Display SSL stats..", function(applet, arg1, arg2, arg3, arg4, arg5)
774 end)
775..
776
777 And we execute this in the prompt:
778
779.. code-block:: text
780
781 > prompt
782 > show ssl stats all
783..
784
Bertrand Jacquin874a35c2018-09-10 21:26:07 +0100785 Then, arg1, arg2 and arg3 will contains respectively "show", "ssl" and "stats".
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +0100786 arg4 will contain "all". arg5 contains nil.
787
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100788.. js:function:: core.set_nice(nice)
789
790 **context**: task, action, sample-fetch, converter
791
792 Change the nice of the current task or current session.
Thierry FOURNIER2e4893c2015-03-18 13:37:27 +0100793
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100794 :param integer nice: the nice value, it must be between -1024 and 1024.
795
796.. js:function:: core.set_map(filename, key, value)
797
798 **context**: init, task, action, sample-fetch, converter
799
Bertrand Jacquin874a35c2018-09-10 21:26:07 +0100800 Set the value *value* associated to the key *key* in the map referenced by
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100801 *filename*.
802
Thierry FOURNIERdc595002015-12-21 11:13:52 +0100803 :param string filename: the Map reference
804 :param string key: the key to set or replace
805 :param string value: the associated value
806
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100807.. js:function:: core.sleep(int seconds)
808
809 **context**: body, init, task, action
810
811 The `core.sleep()` functions stop the Lua execution between specified seconds.
812
813 :param integer seconds: the required seconds.
814
815.. js:function:: core.tcp()
816
817 **context**: init, task, action
818
819 This function returns a new object of a *socket* class.
820
Thierry FOURNIERdc595002015-12-21 11:13:52 +0100821 :returns: A :ref:`socket_class` object.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100822
William Lallemand00a15022021-11-19 16:02:44 +0100823.. js:function:: core.httpclient()
824
825 **context**: init, task, action
826
827 This function returns a new object of a *httpclient* class.
828
829 :returns: A :ref:`httpclient_class` object.
830
Thierry Fournier1de16592016-01-27 09:49:07 +0100831.. js:function:: core.concat()
832
833 **context**: body, init, task, action, sample-fetch, converter
834
Bertrand Jacquin874a35c2018-09-10 21:26:07 +0100835 This function returns a new concat object.
Thierry Fournier1de16592016-01-27 09:49:07 +0100836
837 :returns: A :ref:`concat_class` object.
838
Thierry FOURNIER0a99b892015-08-26 00:14:17 +0200839.. js:function:: core.done(data)
840
841 **context**: body, init, task, action, sample-fetch, converter
842
843 :param any data: Return some data for the caller. It is useful with
844 sample-fetches and sample-converters.
845
846 Immediately stops the current Lua execution and returns to the caller which
847 may be a sample fetch, a converter or an action and returns the specified
Thierry Fournier4234dbd2020-11-28 13:18:23 +0100848 value (ignored for actions and init). It is used when the LUA process finishes
849 its work and wants to give back the control to HAProxy without executing the
Thierry FOURNIER0a99b892015-08-26 00:14:17 +0200850 remaining code. It can be seen as a multi-level "return".
851
Thierry FOURNIER486f5a02015-03-16 15:13:03 +0100852.. js:function:: core.yield()
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100853
854 **context**: task, action, sample-fetch, converter
855
856 Give back the hand at the HAProxy scheduler. It is used when the LUA
857 processing consumes a lot of processing time.
858
Thierry FOURNIER / OZON.IO62fec752016-11-10 20:38:11 +0100859.. js:function:: core.parse_addr(address)
860
861 **context**: body, init, task, action, sample-fetch, converter
862
863 :param network: is a string describing an ipv4 or ipv6 address and optionally
864 its network length, like this: "127.0.0.1/8" or "aaaa::1234/32".
865 :returns: a userdata containing network or nil if an error occurs.
866
Bertrand Jacquin874a35c2018-09-10 21:26:07 +0100867 Parse ipv4 or ipv6 addresses and its facultative associated network.
Thierry FOURNIER / OZON.IO62fec752016-11-10 20:38:11 +0100868
869.. js:function:: core.match_addr(addr1, addr2)
870
871 **context**: body, init, task, action, sample-fetch, converter
872
873 :param addr1: is an address created with "core.parse_addr".
874 :param addr2: is an address created with "core.parse_addr".
Bertrand Jacquin874a35c2018-09-10 21:26:07 +0100875 :returns: boolean, true if the network of the addresses match, else returns
Thierry FOURNIER / OZON.IO62fec752016-11-10 20:38:11 +0100876 false.
877
Ilya Shipitsin2075ca82020-03-06 23:22:22 +0500878 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 +0100879 of network is not important.
880
Thierry FOURNIER / OZON.IO8a1027a2016-11-24 20:48:38 +0100881.. js:function:: core.tokenize(str, separators [, noblank])
882
883 **context**: body, init, task, action, sample-fetch, converter
884
885 This function is useful for tokenizing an entry, or splitting some messages.
886 :param string str: The string which will be split.
887 :param string separators: A string containing a list of separators.
888 :param boolean noblank: Ignore empty entries.
889 :returns: an array of string.
890
891 For example:
892
893.. code-block:: lua
894
895 local array = core.tokenize("This function is useful, for tokenizing an entry.", "., ", true)
896 print_r(array)
897..
898
899 Returns this array:
900
901.. code-block:: text
902
903 (table) table: 0x21c01e0 [
904 1: (string) "This"
905 2: (string) "function"
906 3: (string) "is"
907 4: (string) "useful"
908 5: (string) "for"
909 6: (string) "tokenizing"
910 7: (string) "an"
911 8: (string) "entry"
912 ]
913..
914
Thierry Fournierf61aa632016-02-19 20:56:00 +0100915.. _proxy_class:
916
917Proxy class
918============
919
920.. js:class:: Proxy
921
922 This class provides a way for manipulating proxy and retrieving information
923 like statistics.
924
Thierry FOURNIER817e7592017-07-24 14:35:04 +0200925.. js:attribute:: Proxy.name
926
927 Contain the name of the proxy.
928
Baptiste Assmann46c72552017-10-26 21:51:58 +0200929.. js:attribute:: Proxy.uuid
930
931 Contain the unique identifier of the proxy.
932
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +0100933.. js:attribute:: Proxy.servers
934
Patrick Hemmerc6a1d712018-05-01 21:30:41 -0400935 Contain a table with the attached servers. The table is indexed by server
936 name, and each server entry is an object of type :ref:`server_class`.
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +0100937
Adis Nezirovic8878f8e2018-07-13 12:18:33 +0200938.. js:attribute:: Proxy.stktable
939
940 Contains a stick table object attached to the proxy.
941
Thierry Fournierff480422016-02-25 08:36:46 +0100942.. js:attribute:: Proxy.listeners
943
Patrick Hemmerc6a1d712018-05-01 21:30:41 -0400944 Contain a table with the attached listeners. The table is indexed by listener
945 name, and each each listeners entry is an object of type
946 :ref:`listener_class`.
Thierry Fournierff480422016-02-25 08:36:46 +0100947
Thierry Fournierf61aa632016-02-19 20:56:00 +0100948.. js:function:: Proxy.pause(px)
949
950 Pause the proxy. See the management socket documentation for more information.
951
952 :param class_proxy px: A :ref:`proxy_class` which indicates the manipulated
953 proxy.
954
955.. js:function:: Proxy.resume(px)
956
957 Resume the proxy. See the management socket documentation for more
958 information.
959
960 :param class_proxy px: A :ref:`proxy_class` which indicates the manipulated
961 proxy.
962
963.. js:function:: Proxy.stop(px)
964
965 Stop the proxy. See the management socket documentation for more information.
966
967 :param class_proxy px: A :ref:`proxy_class` which indicates the manipulated
968 proxy.
969
970.. js:function:: Proxy.shut_bcksess(px)
971
972 Kill the session attached to a backup server. See the management socket
973 documentation for more information.
974
975 :param class_proxy px: A :ref:`proxy_class` which indicates the manipulated
976 proxy.
977
978.. js:function:: Proxy.get_cap(px)
979
980 Returns a string describing the capabilities of the proxy.
981
982 :param class_proxy px: A :ref:`proxy_class` which indicates the manipulated
983 proxy.
984 :returns: a string "frontend", "backend", "proxy" or "ruleset".
985
986.. js:function:: Proxy.get_mode(px)
987
988 Returns a string describing the mode of the current proxy.
989
990 :param class_proxy px: A :ref:`proxy_class` which indicates the manipulated
991 proxy.
992 :returns: a string "tcp", "http", "health" or "unknown"
993
994.. js:function:: Proxy.get_stats(px)
995
Bertrand Jacquin874a35c2018-09-10 21:26:07 +0100996 Returns a table containing the proxy statistics. The statistics returned are
Thierry Fournierf61aa632016-02-19 20:56:00 +0100997 not the same if the proxy is frontend or a backend.
998
999 :param class_proxy px: A :ref:`proxy_class` which indicates the manipulated
1000 proxy.
Patrick Hemmerc6a1d712018-05-01 21:30:41 -04001001 :returns: a key/value table containing stats
Thierry Fournierf61aa632016-02-19 20:56:00 +01001002
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001003.. _server_class:
1004
1005Server class
1006============
1007
Patrick Hemmerc6a1d712018-05-01 21:30:41 -04001008.. js:class:: Server
1009
1010 This class provides a way for manipulating servers and retrieving information.
1011
Patrick Hemmera62ae7e2018-04-29 14:23:48 -04001012.. js:attribute:: Server.name
1013
1014 Contain the name of the server.
1015
1016.. js:attribute:: Server.puid
1017
1018 Contain the proxy unique identifier of the server.
1019
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001020.. js:function:: Server.is_draining(sv)
1021
Patrick Hemmerc6a1d712018-05-01 21:30:41 -04001022 Return true if the server is currently draining sticky connections.
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001023
1024 :param class_server sv: A :ref:`server_class` which indicates the manipulated
1025 server.
1026 :returns: a boolean
1027
Patrick Hemmer32d539f2018-04-29 14:25:46 -04001028.. js:function:: Server.set_maxconn(sv, weight)
1029
1030 Dynamically change the maximum connections of the server. See the management
1031 socket documentation for more information about the format of the string.
1032
1033 :param class_server sv: A :ref:`server_class` which indicates the manipulated
1034 server.
1035 :param string maxconn: A string describing the server maximum connections.
1036
1037.. js:function:: Server.get_maxconn(sv, weight)
1038
1039 This function returns an integer representing the server maximum connections.
1040
1041 :param class_server sv: A :ref:`server_class` which indicates the manipulated
1042 server.
1043 :returns: an integer.
1044
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001045.. js:function:: Server.set_weight(sv, weight)
1046
Patrick Hemmerc6a1d712018-05-01 21:30:41 -04001047 Dynamically change the weight of the server. See the management socket
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001048 documentation for more information about the format of the string.
1049
1050 :param class_server sv: A :ref:`server_class` which indicates the manipulated
1051 server.
1052 :param string weight: A string describing the server weight.
1053
1054.. js:function:: Server.get_weight(sv)
1055
Patrick Hemmerc6a1d712018-05-01 21:30:41 -04001056 This function returns an integer representing the server weight.
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001057
1058 :param class_server sv: A :ref:`server_class` which indicates the manipulated
1059 server.
1060 :returns: an integer.
1061
Joseph C. Sible49bbf522020-05-04 22:20:32 -04001062.. js:function:: Server.set_addr(sv, addr[, port])
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001063
Patrick Hemmerc6a1d712018-05-01 21:30:41 -04001064 Dynamically change the address of the server. See the management socket
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001065 documentation for more information about the format of the string.
1066
1067 :param class_server sv: A :ref:`server_class` which indicates the manipulated
1068 server.
Patrick Hemmerc6a1d712018-05-01 21:30:41 -04001069 :param string addr: A string describing the server address.
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001070
1071.. js:function:: Server.get_addr(sv)
1072
Patrick Hemmerc6a1d712018-05-01 21:30:41 -04001073 Returns a string describing the address of the server.
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001074
1075 :param class_server sv: A :ref:`server_class` which indicates the manipulated
1076 server.
1077 :returns: A string
1078
1079.. js:function:: Server.get_stats(sv)
1080
1081 Returns server statistics.
1082
1083 :param class_server sv: A :ref:`server_class` which indicates the manipulated
1084 server.
Patrick Hemmerc6a1d712018-05-01 21:30:41 -04001085 :returns: a key/value table containing stats
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001086
1087.. js:function:: Server.shut_sess(sv)
1088
1089 Shutdown all the sessions attached to the server. See the management socket
1090 documentation for more information about this function.
1091
1092 :param class_server sv: A :ref:`server_class` which indicates the manipulated
1093 server.
1094
1095.. js:function:: Server.set_drain(sv)
1096
1097 Drain sticky sessions. See the management socket documentation for more
1098 information about this function.
1099
1100 :param class_server sv: A :ref:`server_class` which indicates the manipulated
1101 server.
1102
1103.. js:function:: Server.set_maint(sv)
1104
1105 Set maintenance mode. See the management socket documentation for more
1106 information about this function.
1107
1108 :param class_server sv: A :ref:`server_class` which indicates the manipulated
1109 server.
1110
1111.. js:function:: Server.set_ready(sv)
1112
1113 Set normal mode. See the management socket documentation for more information
1114 about this function.
1115
1116 :param class_server sv: A :ref:`server_class` which indicates the manipulated
1117 server.
1118
1119.. js:function:: Server.check_enable(sv)
1120
1121 Enable health checks. See the management socket documentation for more
1122 information about this function.
1123
1124 :param class_server sv: A :ref:`server_class` which indicates the manipulated
1125 server.
1126
1127.. js:function:: Server.check_disable(sv)
1128
1129 Disable health checks. See the management socket documentation for more
1130 information about this function.
1131
1132 :param class_server sv: A :ref:`server_class` which indicates the manipulated
1133 server.
1134
1135.. js:function:: Server.check_force_up(sv)
1136
1137 Force health-check up. See the management socket documentation for more
1138 information about this function.
1139
1140 :param class_server sv: A :ref:`server_class` which indicates the manipulated
1141 server.
1142
1143.. js:function:: Server.check_force_nolb(sv)
1144
1145 Force health-check nolb mode. See the management socket documentation for more
1146 information about this function.
1147
1148 :param class_server sv: A :ref:`server_class` which indicates the manipulated
1149 server.
1150
1151.. js:function:: Server.check_force_down(sv)
1152
1153 Force health-check down. See the management socket documentation for more
1154 information about this function.
1155
1156 :param class_server sv: A :ref:`server_class` which indicates the manipulated
1157 server.
1158
1159.. js:function:: Server.agent_enable(sv)
1160
1161 Enable agent check. See the management socket documentation for more
1162 information about this function.
1163
1164 :param class_server sv: A :ref:`server_class` which indicates the manipulated
1165 server.
1166
1167.. js:function:: Server.agent_disable(sv)
1168
1169 Disable agent check. See the management socket documentation for more
1170 information about this function.
1171
1172 :param class_server sv: A :ref:`server_class` which indicates the manipulated
1173 server.
1174
1175.. js:function:: Server.agent_force_up(sv)
1176
1177 Force agent check up. See the management socket documentation for more
1178 information about this function.
1179
1180 :param class_server sv: A :ref:`server_class` which indicates the manipulated
1181 server.
1182
1183.. js:function:: Server.agent_force_down(sv)
1184
1185 Force agent check down. See the management socket documentation for more
1186 information about this function.
1187
1188 :param class_server sv: A :ref:`server_class` which indicates the manipulated
1189 server.
1190
Thierry Fournierff480422016-02-25 08:36:46 +01001191.. _listener_class:
1192
1193Listener class
1194==============
1195
1196.. js:function:: Listener.get_stats(ls)
1197
1198 Returns server statistics.
1199
1200 :param class_listener ls: A :ref:`listener_class` which indicates the
1201 manipulated listener.
Patrick Hemmerc6a1d712018-05-01 21:30:41 -04001202 :returns: a key/value table containing stats
Thierry Fournierff480422016-02-25 08:36:46 +01001203
Thierry Fournier1de16592016-01-27 09:49:07 +01001204.. _concat_class:
1205
1206Concat class
1207============
1208
1209.. js:class:: Concat
1210
1211 This class provides a fast way for string concatenation. The way using native
1212 Lua concatenation like the code below is slow for some reasons.
1213
1214.. code-block:: lua
1215
1216 str = "string1"
1217 str = str .. ", string2"
1218 str = str .. ", string3"
1219..
1220
1221 For each concatenation, Lua:
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02001222 - allocates memory for the result,
1223 - catenates the two string copying the strings in the new memory block,
1224 - frees the old memory block containing the string which is no longer used.
1225
Thierry Fournier1de16592016-01-27 09:49:07 +01001226 This process does many memory move, allocation and free. In addition, the
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02001227 memory is not really freed, it is just marked as unused and waits for the
Thierry Fournier1de16592016-01-27 09:49:07 +01001228 garbage collector.
1229
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02001230 The Concat class provides an alternative way to concatenate strings. It uses
Thierry Fournier1de16592016-01-27 09:49:07 +01001231 the internal Lua mechanism (it does not allocate memory), but it doesn't copy
1232 the data more than once.
1233
1234 On my computer, the following loops spends 0.2s for the Concat method and
1235 18.5s for the pure Lua implementation. So, the Concat class is about 1000x
1236 faster than the embedded solution.
1237
1238.. code-block:: lua
1239
1240 for j = 1, 100 do
1241 c = core.concat()
1242 for i = 1, 20000 do
1243 c:add("#####")
1244 end
1245 end
1246..
1247
1248.. code-block:: lua
1249
1250 for j = 1, 100 do
1251 c = ""
1252 for i = 1, 20000 do
1253 c = c .. "#####"
1254 end
1255 end
1256..
1257
1258.. js:function:: Concat.add(concat, string)
1259
1260 This function adds a string to the current concatenated string.
1261
1262 :param class_concat concat: A :ref:`concat_class` which contains the currently
Ilya Shipitsin2075ca82020-03-06 23:22:22 +05001263 built string.
Bertrand Jacquin874a35c2018-09-10 21:26:07 +01001264 :param string string: A new string to concatenate to the current built
Thierry Fournier1de16592016-01-27 09:49:07 +01001265 string.
1266
1267.. js:function:: Concat.dump(concat)
1268
Bertrand Jacquin874a35c2018-09-10 21:26:07 +01001269 This function returns the concatenated string.
Thierry Fournier1de16592016-01-27 09:49:07 +01001270
1271 :param class_concat concat: A :ref:`concat_class` which contains the currently
Ilya Shipitsin2075ca82020-03-06 23:22:22 +05001272 built string.
Thierry Fournier1de16592016-01-27 09:49:07 +01001273 :returns: the concatenated string
1274
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001275.. _fetches_class:
1276
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001277Fetches class
1278=============
1279
1280.. js:class:: Fetches
1281
1282 This class contains a lot of internal HAProxy sample fetches. See the
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02001283 HAProxy "configuration.txt" documentation for more information.
1284 (chapters 7.3.2 to 7.3.6)
Thierry FOURNIER2e4893c2015-03-18 13:37:27 +01001285
Christopher Faulet1e9b1b62021-08-11 10:14:30 +02001286 .. warning::
1287 some sample fetches are not available in some context. These limitations
1288 are specified in this documentation when they're useful.
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001289
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01001290 :see: :js:attr:`TXN.f`
1291 :see: :js:attr:`TXN.sf`
Thierry FOURNIER2e4893c2015-03-18 13:37:27 +01001292
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02001293 Fetches are useful to:
Thierry FOURNIER2e4893c2015-03-18 13:37:27 +01001294
1295 * get system time,
1296 * get environment variable,
1297 * get random numbers,
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02001298 * know backend status like the number of users in queue or the number of
Thierry FOURNIER2e4893c2015-03-18 13:37:27 +01001299 connections established,
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02001300 * get client information like ip source or destination,
Thierry FOURNIER2e4893c2015-03-18 13:37:27 +01001301 * deal with stick tables,
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02001302 * fetch established SSL information,
1303 * fetch HTTP information like headers or method.
Thierry FOURNIER2e4893c2015-03-18 13:37:27 +01001304
1305.. code-block:: lua
1306
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001307 function action(txn)
1308 -- Get source IP
1309 local clientip = txn.f:src()
1310 end
Thierry FOURNIER2e4893c2015-03-18 13:37:27 +01001311..
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001312
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001313.. _converters_class:
1314
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001315Converters class
1316================
1317
1318.. js:class:: Converters
1319
1320 This class contains a lot of internal HAProxy sample converters. See the
Thierry FOURNIER2e4893c2015-03-18 13:37:27 +01001321 HAProxy documentation "configuration.txt" for more information about her
1322 usage. Its the chapter 7.3.1.
1323
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01001324 :see: :js:attr:`TXN.c`
1325 :see: :js:attr:`TXN.sc`
Thierry FOURNIER2e4893c2015-03-18 13:37:27 +01001326
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02001327 Converters provides stateful transformation. They are useful to:
Thierry FOURNIER2e4893c2015-03-18 13:37:27 +01001328
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02001329 * convert input to base64,
1330 * apply hash on input string (djb2, crc32, sdbm, wt6),
Thierry FOURNIER2e4893c2015-03-18 13:37:27 +01001331 * format date,
1332 * json escape,
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02001333 * extract preferred language comparing two lists,
Thierry FOURNIER2e4893c2015-03-18 13:37:27 +01001334 * turn to lower or upper chars,
1335 * deal with stick tables.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001336
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001337.. _channel_class:
1338
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001339Channel class
1340=============
1341
1342.. js:class:: Channel
1343
Christopher Faulet5a2c6612021-08-15 20:35:25 +02001344 **context**: action, sample-fetch, convert, filter
1345
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001346 HAProxy uses two buffers for the processing of the requests. The first one is
1347 used with the request data (from the client to the server) and the second is
1348 used for the response data (from the server to the client).
1349
1350 Each buffer contains two types of data. The first type is the incoming data
1351 waiting for a processing. The second part is the outgoing data already
1352 processed. Usually, the incoming data is processed, after it is tagged as
1353 outgoing data, and finally it is sent. The following functions provides tools
1354 for manipulating these data in a buffer.
1355
1356 The following diagram shows where the channel class function are applied.
1357
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001358 .. image:: _static/channel.png
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001359
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001360 .. warning::
1361 It is not possible to read from the response in request action, and it is
Boyang Li60cfe8b2022-05-10 18:11:00 +00001362 not possible to read from the request channel in response action.
Christopher Faulet09530392021-06-14 11:43:18 +02001363
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001364 .. warning::
1365 It is forbidden to alter the Channels buffer from HTTP contexts. So only
1366 :js:func:`Channel.input`, :js:func:`Channel.output`,
1367 :js:func:`Channel.may_recv`, :js:func:`Channel.is_full` and
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02001368 :js:func:`Channel.is_resp` can be called from a HTTP context.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001369
Christopher Faulet5a2c6612021-08-15 20:35:25 +02001370 All the functions provided by this class are available in the
1371 **sample-fetches**, **actions** and **filters** contexts. For **filters**,
1372 incoming data (offset and length) are relative to the filter. Some functions
Boyang Li60cfe8b2022-05-10 18:11:00 +00001373 may yield, but only for **actions**. Yield is not possible for
Christopher Faulet5a2c6612021-08-15 20:35:25 +02001374 **sample-fetches**, **converters** and **filters**.
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001375
1376.. js:function:: Channel.append(channel, string)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001377
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001378 This function copies the string **string** at the end of incoming data of the
1379 channel buffer. The function returns the copied length on success or -1 if
1380 data cannot be copied.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001381
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001382 Same that :js:func:`Channel.insert(channel, string, channel:input())`.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001383
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001384 :param class_channel channel: The manipulated Channel.
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02001385 :param string string: The data to copy at the end of incoming data.
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001386 :returns: an integer containing the amount of bytes copied or -1.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001387
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001388.. js:function:: Channel.data(channel [, offset [, length]])
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001389
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001390 This function returns **length** bytes of incoming data from the channel
1391 buffer, starting at the offset **offset**. The data are not removed from the
1392 buffer.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001393
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001394 By default, if no length is provided, all incoming data found, starting at the
1395 given offset, are returned. If **length** is set to -1, the function tries to
Christopher Faulet5a2c6612021-08-15 20:35:25 +02001396 retrieve a maximum of data and, if called by an action, it yields if
1397 necessary. It also waits for more data if the requested length exceeds the
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02001398 available amount of incoming data. Not providing an offset is the same as
Ilya Shipitsinff0f2782021-08-22 22:18:07 +05001399 setting it to 0. A positive offset is relative to the beginning of incoming
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02001400 data of the channel buffer while negative offset is relative to the end.
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001401
1402 If there is no incoming data and the channel can't receive more data, a 'nil'
1403 value is returned.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001404
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001405 :param class_channel channel: The manipulated Channel.
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001406 :param integer offset: *optional* The offset in incoming data to start to get
1407 data. 0 by default. May be negative to be relative to
1408 the end of incoming data.
1409 :param integer length: *optional* The expected length of data to retrieve. All
1410 incoming data by default. May be set to -1 to get a
1411 maximum of data.
1412 :returns: a string containing the data found or nil.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001413
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001414.. js:function:: Channel.forward(channel, length)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001415
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001416 This function forwards **length** bytes of data from the channel buffer. If
Christopher Faulet5a2c6612021-08-15 20:35:25 +02001417 the requested length exceeds the available amount of incoming data, and if
1418 called by an action, the function yields, waiting for more data to forward. It
1419 returns the amount of data forwarded.
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001420
1421 :param class_channel channel: The manipulated Channel.
1422 :param integer int: The amount of data to forward.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001423
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001424.. js:function:: Channel.input(channel)
1425
Christopher Faulet5a2c6612021-08-15 20:35:25 +02001426 This function returns the length of incoming data in the channel buffer. When
1427 called by a filter, this value is relative to the filter.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001428
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001429 :param class_channel channel: The manipulated Channel.
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001430 :returns: an integer containing the amount of available bytes.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001431
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001432.. js:function:: Channel.insert(channel, string [, offset])
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001433
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001434 This function copies the string **string** at the offset **offset** in
1435 incoming data of the channel buffer. The function returns the copied length on
1436 success or -1 if data cannot be copied.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001437
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001438 By default, if no offset is provided, the string is copied in front of
Ilya Shipitsinff0f2782021-08-22 22:18:07 +05001439 incoming data. A positive offset is relative to the beginning of incoming data
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001440 of the channel buffer while negative offset is relative to their end.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001441
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001442 :param class_channel channel: The manipulated Channel.
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02001443 :param string string: The data to copy into incoming data.
1444 :param integer offset: *optional* The offset in incoming data where to copy
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001445 data. 0 by default. May be negative to be relative to
1446 the end of incoming data.
Pieter Baauw4d7f7662015-11-08 16:38:08 +01001447 :returns: an integer containing the amount of bytes copied or -1.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001448
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001449.. js:function:: Channel.is_full(channel)
1450
1451 This function returns true if the channel buffer is full.
1452
Christopher Faulet5a2c6612021-08-15 20:35:25 +02001453 :param class_channel channel: The manipulated Channel.
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001454 :returns: a boolean
1455
1456.. js:function:: Channel.is_resp(channel)
1457
1458 This function returns true if the channel is the response one.
1459
Christopher Faulet5a2c6612021-08-15 20:35:25 +02001460 :param class_channel channel: The manipulated Channel.
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001461 :returns: a boolean
1462
1463.. js:function:: Channel.line(channel [, offset [, length]])
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001464
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001465 This function parses **length** bytes of incoming data of the channel buffer,
1466 starting at offset **offset**, and returns the first line found, including the
Christopher Faulet5a2c6612021-08-15 20:35:25 +02001467 '\\n'. The data are not removed from the buffer. If no line is found, all data
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001468 are returned.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001469
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001470 By default, if no length is provided, all incoming data, starting at the given
1471 offset, are evaluated. If **length** is set to -1, the function tries to
Christopher Faulet5a2c6612021-08-15 20:35:25 +02001472 retrieve a maximum of data and, if called by an action, yields if
1473 necessary. It also waits for more data if the requested length exceeds the
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02001474 available amount of incoming data. Not providing an offset is the same as
Ilya Shipitsinff0f2782021-08-22 22:18:07 +05001475 setting it to 0. A positive offset is relative to the beginning of incoming
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02001476 data of the channel buffer while negative offset is relative to the end.
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001477
1478 If there is no incoming data and the channel can't receive more data, a 'nil'
1479 value is returned.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001480
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001481 :param class_channel channel: The manipulated Channel.
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02001482 :param integer offset: *optional* The offset in incoming data to start to
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001483 parse data. 0 by default. May be negative to be
1484 relative to the end of incoming data.
1485 :param integer length: *optional* The length of data to parse. All incoming
1486 data by default. May be set to -1 to get a maximum of
1487 data.
1488 :returns: a string containing the line found or nil.
1489
1490.. js:function:: Channel.may_recv(channel)
1491
1492 This function returns true if the channel may still receive data.
1493
Christopher Faulet5a2c6612021-08-15 20:35:25 +02001494 :param class_channel channel: The manipulated Channel.
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001495 :returns: a boolean
1496
1497.. js:function:: Channel.output(channel)
1498
Christopher Faulet5a2c6612021-08-15 20:35:25 +02001499 This function returns the length of outgoing data of the channel buffer. When
1500 called by a filter, this value is relative to the filter.
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001501
1502 :param class_channel channel: The manipulated Channel.
1503 :returns: an integer containing the amount of available bytes.
1504
1505.. js:function:: Channel.prepend(channel, string)
1506
1507 This function copies the string **string** in front of incoming data of the
1508 channel buffer. The function returns the copied length on success or -1 if
1509 data cannot be copied.
1510
1511 Same that :js:func:`Channel.insert(channel, string, 0)`.
1512
1513 :param class_channel channel: The manipulated Channel.
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02001514 :param string string: The data to copy in front of incoming data.
Pieter Baauw4d7f7662015-11-08 16:38:08 +01001515 :returns: an integer containing the amount of bytes copied or -1.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001516
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001517.. js:function:: Channel.remove(channel [, offset [, length]])
1518
1519 This function removes **length** bytes of incoming data of the channel buffer,
1520 starting at offset **offset**. This function returns number of bytes removed
1521 on success.
1522
1523 By default, if no length is provided, all incoming data, starting at the given
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02001524 offset, are removed. Not providing an offset is the same as setting it
Ilya Shipitsinff0f2782021-08-22 22:18:07 +05001525 to 0. A positive offset is relative to the beginning of incoming data of the
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02001526 channel buffer while negative offset is relative to the end.
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001527
1528 :param class_channel channel: The manipulated Channel.
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02001529 :param integer offset: *optional* The offset in incoming data where to start
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001530 to remove data. 0 by default. May be negative to
1531 be relative to the end of incoming data.
1532 :param integer length: *optional* The length of data to remove. All incoming
1533 data by default.
1534 :returns: an integer containing the amount of bytes removed.
1535
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001536.. js:function:: Channel.send(channel, string)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001537
Christopher Faulet5a2c6612021-08-15 20:35:25 +02001538 This function requires immediate send of the string **string**. It means the
Ilya Shipitsinff0f2782021-08-22 22:18:07 +05001539 string is copied at the beginning of incoming data of the channel buffer and
Christopher Faulet5a2c6612021-08-15 20:35:25 +02001540 immediately forwarded. Unless if the connection is close, and if called by an
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02001541 action, this function yields to copy and forward all the string.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001542
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001543 :param class_channel channel: The manipulated Channel.
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001544 :param string string: The data to send.
Pieter Baauw4d7f7662015-11-08 16:38:08 +01001545 :returns: an integer containing the amount of bytes copied or -1.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001546
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001547.. js:function:: Channel.set(channel, string [, offset [, length]])
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001548
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02001549 This function replaces **length** bytes of incoming data of the channel buffer,
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001550 starting at offset **offset**, by the string **string**. The function returns
1551 the copied length on success or -1 if data cannot be copied.
1552
1553 By default, if no length is provided, all incoming data, starting at the given
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02001554 offset, are replaced. Not providing an offset is the same as setting it
Ilya Shipitsinff0f2782021-08-22 22:18:07 +05001555 to 0. A positive offset is relative to the beginning of incoming data of the
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02001556 channel buffer while negative offset is relative to the end.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001557
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001558 :param class_channel channel: The manipulated Channel.
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02001559 :param string string: The data to copy into incoming data.
1560 :param integer offset: *optional* The offset in incoming data where to start
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001561 the data replacement. 0 by default. May be negative to
1562 be relative to the end of incoming data.
1563 :param integer length: *optional* The length of data to replace. All incoming
1564 data by default.
1565 :returns: an integer containing the amount of bytes copied or -1.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001566
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001567.. js:function:: Channel.dup(channel)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001568
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001569 **DEPRECATED**
1570
1571 This function returns all incoming data found in the channel buffer. The data
Boyang Li60cfe8b2022-05-10 18:11:00 +00001572 are not removed from the buffer and can be reprocessed later.
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001573
1574 If there is no incoming data and the channel can't receive more data, a 'nil'
1575 value is returned.
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: a string containing all data found or nil.
1579
1580 .. warning::
1581 This function is deprecated. :js:func:`Channel.data()` must be used
1582 instead.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001583
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001584.. js:function:: Channel.get(channel)
1585
1586 **DEPRECATED**
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001587
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001588 This function returns all incoming data found in the channel buffer and remove
1589 them from the buffer.
1590
1591 If there is no incoming data and the channel can't receive more data, a 'nil'
1592 value is returned.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001593
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001594 :param class_channel channel: The manipulated Channel.
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001595 :returns: a string containing all the data found or nil.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001596
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001597 .. warning::
1598 This function is deprecated. :js:func:`Channel.data()` must be used to
1599 retrieve data followed by a call to :js:func:`Channel:remove()` to remove
1600 data.
Thierry FOURNIER / OZON.IO65192f32016-11-07 15:28:40 +01001601
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001602 .. code-block:: lua
Thierry FOURNIER / OZON.IO65192f32016-11-07 15:28:40 +01001603
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001604 local data = chn:data()
1605 chn:remove(0, data:len())
1606
1607 ..
1608
1609.. js:function:: Channel.getline(channel)
1610
1611 **DEPRECATED**
1612
1613 This function returns the first line found in incoming data of the channel
Christopher Faulet5a2c6612021-08-15 20:35:25 +02001614 buffer, including the '\\n'. The returned data are removed from the buffer. If
1615 no line is found, and if called by an action, this function yields to wait for
1616 more data, except if the channel can't receive more data. In this case all
1617 data are returned.
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001618
1619 If there is no incoming data and the channel can't receive more data, a 'nil'
1620 value is returned.
1621
1622 :param class_channel channel: The manipulated Channel.
1623 :returns: a string containing the line found or nil.
1624
1625 .. warning::
Boyang Li60cfe8b2022-05-10 18:11:00 +00001626 This function is deprecated. :js:func:`Channel.line()` must be used to
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001627 retrieve a line followed by a call to :js:func:`Channel:remove()` to remove
1628 data.
1629
1630 .. code-block:: lua
1631
1632 local line = chn:line(0, -1)
1633 chn:remove(0, line:len())
1634
1635 ..
1636
1637.. js:function:: Channel.get_in_len(channel)
1638
Boyang Li60cfe8b2022-05-10 18:11:00 +00001639 **DEPRECATED**
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001640
Christopher Faulet5a2c6612021-08-15 20:35:25 +02001641 This function returns the length of the input part of the buffer. When called
1642 by a filter, this value is relative to the filter.
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001643
1644 :param class_channel channel: The manipulated Channel.
1645 :returns: an integer containing the amount of available bytes.
1646
1647 .. warning::
1648 This function is deprecated. :js:func:`Channel.input()` must be used
1649 instead.
1650
1651.. js:function:: Channel.get_out_len(channel)
1652
Boyang Li60cfe8b2022-05-10 18:11:00 +00001653 **DEPRECATED**
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001654
Christopher Faulet5a2c6612021-08-15 20:35:25 +02001655 This function returns the length of the output part of the buffer. When called
1656 by a filter, this value is relative to the filter.
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001657
1658 :param class_channel channel: The manipulated Channel.
1659 :returns: an integer containing the amount of available bytes.
1660
1661 .. warning::
1662 This function is deprecated. :js:func:`Channel.output()` must be used
1663 instead.
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001664
1665.. _http_class:
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001666
1667HTTP class
1668==========
1669
1670.. js:class:: HTTP
1671
1672 This class contain all the HTTP manipulation functions.
1673
Pieter Baauw386a1272015-08-16 15:26:24 +02001674.. js:function:: HTTP.req_get_headers(http)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001675
Patrick Hemmerc6a1d712018-05-01 21:30:41 -04001676 Returns a table containing all the request headers.
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001677
1678 :param class_http http: The related http object.
Patrick Hemmerc6a1d712018-05-01 21:30:41 -04001679 :returns: table of headers.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01001680 :see: :js:func:`HTTP.res_get_headers`
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001681
Patrick Hemmerc6a1d712018-05-01 21:30:41 -04001682 This is the form of the returned table:
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001683
1684.. code-block:: lua
1685
1686 HTTP:req_get_headers()['<header-name>'][<header-index>] = "<header-value>"
1687
1688 local hdr = HTTP:req_get_headers()
1689 hdr["host"][0] = "www.test.com"
1690 hdr["accept"][0] = "audio/basic q=1"
1691 hdr["accept"][1] = "audio/*, q=0.2"
1692 hdr["accept"][2] = "*/*, q=0.1"
1693..
1694
Pieter Baauw386a1272015-08-16 15:26:24 +02001695.. js:function:: HTTP.res_get_headers(http)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001696
Patrick Hemmerc6a1d712018-05-01 21:30:41 -04001697 Returns a table containing all the response headers.
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001698
1699 :param class_http http: The related http object.
Patrick Hemmerc6a1d712018-05-01 21:30:41 -04001700 :returns: table of headers.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01001701 :see: :js:func:`HTTP.req_get_headers`
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001702
Patrick Hemmerc6a1d712018-05-01 21:30:41 -04001703 This is the form of the returned table:
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001704
1705.. code-block:: lua
1706
1707 HTTP:res_get_headers()['<header-name>'][<header-index>] = "<header-value>"
1708
1709 local hdr = HTTP:req_get_headers()
1710 hdr["host"][0] = "www.test.com"
1711 hdr["accept"][0] = "audio/basic q=1"
1712 hdr["accept"][1] = "audio/*, q=0.2"
1713 hdr["accept"][2] = "*.*, q=0.1"
1714..
1715
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001716.. js:function:: HTTP.req_add_header(http, name, value)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001717
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02001718 Appends a HTTP header field in the request whose name is
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001719 specified in "name" and whose value is defined in "value".
1720
1721 :param class_http http: The related http object.
1722 :param string name: The header name.
1723 :param string value: The header value.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01001724 :see: :js:func:`HTTP.res_add_header`
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001725
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001726.. js:function:: HTTP.res_add_header(http, name, value)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001727
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02001728 Appends a HTTP header field in the response whose name is
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001729 specified in "name" and whose value is defined in "value".
1730
1731 :param class_http http: The related http object.
1732 :param string name: The header name.
1733 :param string value: The header value.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01001734 :see: :js:func:`HTTP.req_add_header`
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001735
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001736.. js:function:: HTTP.req_del_header(http, name)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001737
1738 Removes all HTTP header fields in the request whose name is
1739 specified in "name".
1740
1741 :param class_http http: The related http object.
1742 :param string name: The header name.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01001743 :see: :js:func:`HTTP.res_del_header`
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001744
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001745.. js:function:: HTTP.res_del_header(http, name)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001746
1747 Removes all HTTP header fields in the response whose name is
1748 specified in "name".
1749
1750 :param class_http http: The related http object.
1751 :param string name: The header name.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01001752 :see: :js:func:`HTTP.req_del_header`
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001753
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001754.. js:function:: HTTP.req_set_header(http, name, value)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001755
Bertrand Jacquin874a35c2018-09-10 21:26:07 +01001756 This variable replace all occurrence of all header "name", by only
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001757 one containing the "value".
1758
1759 :param class_http http: The related http object.
1760 :param string name: The header name.
1761 :param string value: The header value.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01001762 :see: :js:func:`HTTP.res_set_header`
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001763
Bertrand Jacquin874a35c2018-09-10 21:26:07 +01001764 This function does the same work as the following code:
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001765
1766.. code-block:: lua
1767
1768 function fcn(txn)
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001769 TXN.http:req_del_header("header")
1770 TXN.http:req_add_header("header", "value")
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001771 end
1772..
1773
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001774.. js:function:: HTTP.res_set_header(http, name, value)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001775
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02001776 This function replaces all occurrence of all header "name", by only
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001777 one containing the "value".
1778
1779 :param class_http http: The related http object.
1780 :param string name: The header name.
1781 :param string value: The header value.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01001782 :see: :js:func:`HTTP.req_rep_header()`
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001783
Pieter Baauw386a1272015-08-16 15:26:24 +02001784.. js:function:: HTTP.req_rep_header(http, name, regex, replace)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001785
1786 Matches the regular expression in all occurrences of header field "name"
1787 according to "regex", and replaces them with the "replace" argument. The
1788 replacement value can contain back references like \1, \2, ... This
1789 function works with the request.
1790
1791 :param class_http http: The related http object.
1792 :param string name: The header name.
1793 :param string regex: The match regular expression.
1794 :param string replace: The replacement value.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01001795 :see: :js:func:`HTTP.res_rep_header()`
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001796
Pieter Baauw386a1272015-08-16 15:26:24 +02001797.. js:function:: HTTP.res_rep_header(http, name, regex, string)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001798
1799 Matches the regular expression in all occurrences of header field "name"
1800 according to "regex", and replaces them with the "replace" argument. The
1801 replacement value can contain back references like \1, \2, ... This
1802 function works with the request.
1803
1804 :param class_http http: The related http object.
1805 :param string name: The header name.
1806 :param string regex: The match regular expression.
1807 :param string replace: The replacement value.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01001808 :see: :js:func:`HTTP.req_rep_header()`
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001809
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001810.. js:function:: HTTP.req_set_method(http, method)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001811
1812 Rewrites the request method with the parameter "method".
1813
1814 :param class_http http: The related http object.
1815 :param string method: The new method.
1816
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001817.. js:function:: HTTP.req_set_path(http, path)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001818
1819 Rewrites the request path with the "path" parameter.
1820
1821 :param class_http http: The related http object.
1822 :param string path: The new path.
1823
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001824.. js:function:: HTTP.req_set_query(http, query)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001825
1826 Rewrites the request's query string which appears after the first question
1827 mark ("?") with the parameter "query".
1828
1829 :param class_http http: The related http object.
1830 :param string query: The new query.
1831
Thierry FOURNIER0d79cf62015-08-26 14:20:58 +02001832.. js:function:: HTTP.req_set_uri(http, uri)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001833
1834 Rewrites the request URI with the parameter "uri".
1835
1836 :param class_http http: The related http object.
1837 :param string uri: The new uri.
1838
Robin H. Johnson52f5db22017-01-01 13:10:52 -08001839.. js:function:: HTTP.res_set_status(http, status [, reason])
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +02001840
Robin H. Johnson52f5db22017-01-01 13:10:52 -08001841 Rewrites the response status code with the parameter "code".
1842
1843 If no custom reason is provided, it will be generated from the status.
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +02001844
1845 :param class_http http: The related http object.
1846 :param integer status: The new response status code.
Robin H. Johnson52f5db22017-01-01 13:10:52 -08001847 :param string reason: The new response reason (optional).
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +02001848
William Lallemand00a15022021-11-19 16:02:44 +01001849.. _httpclient_class:
1850
1851HTTPClient class
1852================
1853
1854.. js:class:: HTTPClient
1855
1856 The httpclient class allows issue of outbound HTTP requests through a simple
1857 API without the knowledge of HAProxy internals.
1858
1859.. js:function:: HTTPClient.get(httpclient, request)
1860.. js:function:: HTTPClient.head(httpclient, request)
1861.. js:function:: HTTPClient.put(httpclient, request)
1862.. js:function:: HTTPClient.post(httpclient, request)
1863.. js:function:: HTTPClient.delete(httpclient, request)
1864
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02001865 Send a HTTP request and wait for a response. GET, HEAD PUT, POST and DELETE methods can be used.
1866 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 +01001867
William Lallemanda9256192022-10-21 11:48:24 +02001868 The HTTPClient interface is not able to decompress responses, it is not
1869 recommended to send an Accept-Encoding in the request so the response is
1870 received uncompressed.
William Lallemand00a15022021-11-19 16:02:44 +01001871
1872 :param class httpclient: Is the manipulated HTTPClient.
1873 :param table request: Is a table containing the parameters of the request that will be send.
1874 :param string request.url: Is a mandatory parameter for the request that contains the URL.
1875 :param string request.body: Is an optional parameter for the request that contains the body to send.
1876 :param table request.headers: Is an optional parameter for the request that contains the headers to send.
William Lallemand18340302022-02-23 15:57:45 +01001877 :param string request.dst: Is an optional parameter for the destination in haproxy address format.
William Lallemandb4a4ef62022-02-23 14:18:16 +01001878 :param integer request.timeout: Optional timeout parameter, set a "timeout server" on the connections.
William Lallemand00a15022021-11-19 16:02:44 +01001879 :returns: Lua table containing the response
1880
1881
1882.. code-block:: lua
1883
1884 local httpclient = core.httpclient()
William Lallemand4f4f2b72022-02-17 20:00:23 +01001885 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 +01001886
1887..
1888
1889.. code-block:: lua
1890
1891 response = {
1892 status = 400,
1893 reason = "Bad request",
1894 headers = {
1895 ["content-type"] = { "text/html" },
1896 ["cache-control"] = { "no-cache", "no-store" },
1897 },
William Lallemand4f4f2b72022-02-17 20:00:23 +01001898 body = "<html><body><h1>invalid request<h1></body></html>",
William Lallemand00a15022021-11-19 16:02:44 +01001899 }
1900..
1901
1902
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001903.. _txn_class:
1904
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001905TXN class
1906=========
1907
1908.. js:class:: TXN
1909
1910 The txn class contain all the functions relative to the http or tcp
1911 transaction (Note than a tcp stream is the same than a tcp transaction, but
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02001912 a HTTP transaction is not the same than a tcp stream).
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001913
1914 The usage of this class permits to retrieve data from the requests, alter it
1915 and forward it.
1916
1917 All the functions provided by this class are available in the context
Christopher Faulet5a2c6612021-08-15 20:35:25 +02001918 **sample-fetches**, **actions** and **filters**.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001919
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001920.. js:attribute:: TXN.c
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001921
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001922 :returns: An :ref:`converters_class`.
1923
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001924 This attribute contains a Converters class object.
1925
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001926.. js:attribute:: TXN.sc
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001927
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001928 :returns: An :ref:`converters_class`.
1929
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001930 This attribute contains a Converters class object. The functions of
1931 this object returns always a string.
1932
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001933.. js:attribute:: TXN.f
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001934
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001935 :returns: An :ref:`fetches_class`.
1936
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001937 This attribute contains a Fetches class object.
1938
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001939.. js:attribute:: TXN.sf
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001940
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001941 :returns: An :ref:`fetches_class`.
1942
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001943 This attribute contains a Fetches class object. The functions of
1944 this object returns always a string.
1945
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001946.. js:attribute:: TXN.req
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001947
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001948 :returns: An :ref:`channel_class`.
1949
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001950 This attribute contains a channel class object for the request buffer.
1951
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001952.. js:attribute:: TXN.res
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001953
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001954 :returns: An :ref:`channel_class`.
1955
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001956 This attribute contains a channel class object for the response buffer.
1957
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001958.. js:attribute:: TXN.http
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001959
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001960 :returns: An :ref:`http_class`.
1961
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02001962 This attribute contains a HTTP class object. It is available only if the
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001963 proxy has the "mode http" enabled.
1964
Christopher Faulet5a2c6612021-08-15 20:35:25 +02001965.. js:attribute:: TXN.http_req
1966
1967 :returns: An :ref:`httpmessage_class`.
1968
1969 This attribute contains the request HTTPMessage class object. It is available
1970 only if the proxy has the "mode http" enabled and only in the **filters**
1971 context.
1972
1973.. js:attribute:: TXN.http_res
1974
1975 :returns: An :ref:`httpmessage_class`.
1976
1977 This attribute contains the response HTTPMessage class object. It is available
1978 only if the proxy has the "mode http" enabled and only in the **filters**
1979 context.
1980
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01001981.. js:function:: TXN.log(TXN, loglevel, msg)
1982
1983 This function sends a log. The log is sent, according with the HAProxy
1984 configuration file, on the default syslog server if it is configured and on
1985 the stderr if it is allowed.
1986
1987 :param class_txn txn: The class txn object containing the data.
Bertrand Jacquin874a35c2018-09-10 21:26:07 +01001988 :param integer loglevel: Is the log level associated with the message. It is a
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01001989 number between 0 and 7.
1990 :param string msg: The log content.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01001991 :see: :js:attr:`core.emerg`, :js:attr:`core.alert`, :js:attr:`core.crit`,
1992 :js:attr:`core.err`, :js:attr:`core.warning`, :js:attr:`core.notice`,
1993 :js:attr:`core.info`, :js:attr:`core.debug` (log level definitions)
1994 :see: :js:func:`TXN.deflog`
1995 :see: :js:func:`TXN.Debug`
1996 :see: :js:func:`TXN.Info`
1997 :see: :js:func:`TXN.Warning`
1998 :see: :js:func:`TXN.Alert`
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01001999
2000.. js:function:: TXN.deflog(TXN, msg)
2001
Bertrand Jacquin874a35c2018-09-10 21:26:07 +01002002 Sends a log line with the default loglevel for the proxy associated with the
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01002003 transaction.
2004
2005 :param class_txn txn: The class txn object containing the data.
2006 :param string msg: The log content.
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02002007 :see: :js:func:`TXN.log`
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01002008
2009.. js:function:: TXN.Debug(txn, msg)
2010
2011 :param class_txn txn: The class txn object containing the data.
2012 :param string msg: The log content.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01002013 :see: :js:func:`TXN.log`
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01002014
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02002015 Does the same job as:
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01002016
2017.. code-block:: lua
2018
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002019 function Debug(txn, msg)
2020 TXN.log(txn, core.debug, msg)
2021 end
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01002022..
2023
2024.. js:function:: TXN.Info(txn, msg)
2025
2026 :param class_txn txn: The class txn object containing the data.
2027 :param string msg: The log content.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01002028 :see: :js:func:`TXN.log`
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01002029
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02002030 Does the same job as:
2031
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01002032.. code-block:: lua
2033
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02002034 function Info(txn, msg)
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002035 TXN.log(txn, core.info, msg)
2036 end
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01002037..
2038
2039.. js:function:: TXN.Warning(txn, msg)
2040
2041 :param class_txn txn: The class txn object containing the data.
2042 :param string msg: The log content.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01002043 :see: :js:func:`TXN.log`
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01002044
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02002045 Does the same job as:
2046
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01002047.. code-block:: lua
2048
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02002049 function Warning(txn, msg)
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002050 TXN.log(txn, core.warning, msg)
2051 end
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01002052..
2053
2054.. js:function:: TXN.Alert(txn, msg)
2055
2056 :param class_txn txn: The class txn object containing the data.
2057 :param string msg: The log content.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01002058 :see: :js:func:`TXN.log`
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01002059
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02002060 Does the same job as:
2061
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01002062.. code-block:: lua
2063
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02002064 function Alert(txn, msg)
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002065 TXN.log(txn, core.alert, msg)
2066 end
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01002067..
2068
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002069.. js:function:: TXN.get_priv(txn)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002070
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002071 Return Lua data stored in the current transaction (with the `TXN.set_priv()`)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002072 function. If no data are stored, it returns a nil value.
2073
2074 :param class_txn txn: The class txn object containing the data.
Bertrand Jacquin874a35c2018-09-10 21:26:07 +01002075 :returns: the opaque data previously stored, or nil if nothing is
2076 available.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002077
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002078.. js:function:: TXN.set_priv(txn, data)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002079
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02002080 Store any data in the current HAProxy transaction. This action replaces the
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002081 old stored data.
2082
2083 :param class_txn txn: The class txn object containing the data.
2084 :param opaque data: The data which is stored in the transaction.
2085
Tim Duesterhus4e172c92020-05-19 13:49:42 +02002086.. js:function:: TXN.set_var(TXN, var, value[, ifexist])
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +02002087
David Carlier61fdf8b2015-10-02 11:59:38 +01002088 Converts a Lua type in a HAProxy type and store it in a variable <var>.
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +02002089
2090 :param class_txn txn: The class txn object containing the data.
2091 :param string var: The variable name according with the HAProxy variable syntax.
Thierry FOURNIER / OZON.IOb210bcc2016-12-12 16:24:16 +01002092 :param type value: The value associated to the variable. The type can be string or
2093 integer.
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02002094 :param boolean ifexist: If this parameter is set to true the variable
Tim Duesterhus4e172c92020-05-19 13:49:42 +02002095 will only be set if it was defined elsewhere (i.e. used
Willy Tarreau7978c5c2021-09-07 14:24:07 +02002096 within the configuration). For global variables (using the
2097 "proc" scope), they will only be updated and never created.
2098 It is highly recommended to always set this to true.
Christopher Faulet85d79c92016-11-09 16:54:56 +01002099
2100.. js:function:: TXN.unset_var(TXN, var)
2101
2102 Unset the variable <var>.
2103
2104 :param class_txn txn: The class txn object containing the data.
2105 :param string var: The variable name according with the HAProxy variable syntax.
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +02002106
2107.. js:function:: TXN.get_var(TXN, var)
2108
2109 Returns data stored in the variable <var> converter in Lua type.
2110
2111 :param class_txn txn: The class txn object containing the data.
2112 :param string var: The variable name according with the HAProxy variable syntax.
2113
Christopher Faulet700d9e82020-01-31 12:21:52 +01002114.. js:function:: TXN.reply([reply])
2115
2116 Return a new reply object
2117
2118 :param table reply: A table containing info to initialize the reply fields.
2119 :returns: A :ref:`reply_class` object.
2120
2121 The table used to initialized the reply object may contain following entries :
2122
2123 * status : The reply status code. the code 200 is used by default.
2124 * reason : The reply reason. The reason corresponding to the status code is
2125 used by default.
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02002126 * headers : A list of headers, indexed by header name. Empty by default. For
Christopher Faulet700d9e82020-01-31 12:21:52 +01002127 a given name, multiple values are possible, stored in an ordered list.
2128 * body : The reply body, empty by default.
2129
2130.. code-block:: lua
2131
2132 local reply = txn:reply{
2133 status = 400,
2134 reason = "Bad request",
2135 headers = {
2136 ["content-type"] = { "text/html" },
2137 ["cache-control"] = {"no-cache", "no-store" }
2138 },
2139 body = "<html><body><h1>invalid request<h1></body></html>"
2140 }
2141..
2142 :see: :js:class:`Reply`
2143
2144.. js:function:: TXN.done(txn[, reply])
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002145
Willy Tarreaubc183a62015-08-28 10:39:11 +02002146 This function terminates processing of the transaction and the associated
Christopher Faulet700d9e82020-01-31 12:21:52 +01002147 session and optionally reply to the client for HTTP sessions.
2148
2149 :param class_txn txn: The class txn object containing the data.
2150 :param class_reply reply: The class reply object to return to the client.
2151
2152 This functions can be used when a critical error is detected or to terminate
Willy Tarreaubc183a62015-08-28 10:39:11 +02002153 processing after some data have been returned to the client (eg: a redirect).
Christopher Faulet700d9e82020-01-31 12:21:52 +01002154 To do so, a reply may be provided. This object is optional and may contain a
2155 status code, a reason, a header list and a body. All these fields are
Christopher Faulet7855b192021-11-09 18:39:51 +01002156 optional. When not provided, the default values are used. By default, with an
2157 empty reply object, an empty HTTP 200 response is returned to the client. If
2158 no reply object is provided, the transaction is terminated without any
2159 reply. If a reply object is provided, it must not exceed the buffer size once
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02002160 converted into the internal HTTP representation. Because for now there is no
Christopher Faulet7855b192021-11-09 18:39:51 +01002161 easy way to be sure it fits, it is probably better to keep it reasonably
2162 small.
Christopher Faulet700d9e82020-01-31 12:21:52 +01002163
2164 The reply object may be fully created in lua or the class Reply may be used to
2165 create it.
2166
2167.. code-block:: lua
2168
2169 local reply = txn:reply()
2170 reply:set_status(400, "Bad request")
2171 reply:add_header("content-type", "text/html")
2172 reply:add_header("cache-control", "no-cache")
2173 reply:add_header("cache-control", "no-store")
2174 reply:set_body("<html><body><h1>invalid request<h1></body></html>")
2175 txn:done(reply)
2176..
2177
2178.. code-block:: lua
2179
2180 txn:done{
2181 status = 400,
2182 reason = "Bad request",
2183 headers = {
2184 ["content-type"] = { "text/html" },
2185 ["cache-control"] = { "no-cache", "no-store" },
2186 },
2187 body = "<html><body><h1>invalid request<h1></body></html>"
2188 }
2189..
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002190
Christopher Faulet1e9b1b62021-08-11 10:14:30 +02002191 .. warning::
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02002192 It does not make sense to call this function from sample-fetches. In this case
2193 the behavior is the same than core.done(): it finishes the Lua
Christopher Faulet1e9b1b62021-08-11 10:14:30 +02002194 execution. The transaction is really aborted only from an action registered
2195 function.
Thierry FOURNIERab00df62016-07-14 11:42:37 +02002196
Christopher Faulet700d9e82020-01-31 12:21:52 +01002197 :see: :js:func:`TXN.reply`, :js:class:`Reply`
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002198
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002199.. js:function:: TXN.set_loglevel(txn, loglevel)
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01002200
2201 Is used to change the log level of the current request. The "loglevel" must
2202 be an integer between 0 and 7.
2203
2204 :param class_txn txn: The class txn object containing the data.
2205 :param integer loglevel: The required log level. This variable can be one of
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01002206 :see: :js:attr:`core.emerg`, :js:attr:`core.alert`, :js:attr:`core.crit`,
2207 :js:attr:`core.err`, :js:attr:`core.warning`, :js:attr:`core.notice`,
2208 :js:attr:`core.info`, :js:attr:`core.debug` (log level definitions)
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01002209
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002210.. js:function:: TXN.set_tos(txn, tos)
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01002211
2212 Is used to set the TOS or DSCP field value of packets sent to the client to
2213 the value passed in "tos" on platforms which support this.
2214
2215 :param class_txn txn: The class txn object containing the data.
2216 :param integer tos: The new TOS os DSCP.
2217
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002218.. js:function:: TXN.set_mark(txn, mark)
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01002219
2220 Is used to set the Netfilter MARK on all packets sent to the client to the
2221 value passed in "mark" on platforms which support it.
2222
2223 :param class_txn txn: The class txn object containing the data.
2224 :param integer mark: The mark value.
2225
Patrick Hemmer268a7072018-05-11 12:52:31 -04002226.. js:function:: TXN.set_priority_class(txn, prio)
2227
2228 This function adjusts the priority class of the transaction. The value should
2229 be within the range -2047..2047. Values outside this range will be
2230 truncated.
2231
2232 See the HAProxy configuration.txt file keyword "http-request" action
2233 "set-priority-class" for details.
2234
2235.. js:function:: TXN.set_priority_offset(txn, prio)
2236
2237 This function adjusts the priority offset of the transaction. The value
2238 should be within the range -524287..524287. Values outside this range will be
2239 truncated.
2240
2241 See the HAProxy configuration.txt file keyword "http-request" action
2242 "set-priority-offset" for details.
2243
Christopher Faulet700d9e82020-01-31 12:21:52 +01002244.. _reply_class:
2245
2246Reply class
2247============
2248
2249.. js:class:: Reply
2250
2251 **context**: action
2252
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02002253 This class represents a HTTP response message. It provides some methods to
Christopher Faulet7855b192021-11-09 18:39:51 +01002254 enrich it. Once converted into the internal HTTP representation, the response
2255 message must not exceed the buffer size. Because for now there is no
2256 easy way to be sure it fits, it is probably better to keep it reasonably
2257 small.
2258
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02002259 See tune.bufsize in the configuration manual for details.
Christopher Faulet700d9e82020-01-31 12:21:52 +01002260
2261.. code-block:: lua
2262
2263 local reply = txn:reply({status = 400}) -- default HTTP 400 reason-phase used
2264 reply:add_header("content-type", "text/html")
2265 reply:add_header("cache-control", "no-cache")
2266 reply:add_header("cache-control", "no-store")
2267 reply:set_body("<html><body><h1>invalid request<h1></body></html>")
2268..
2269
2270 :see: :js:func:`TXN.reply`
2271
2272.. js:attribute:: Reply.status
2273
2274 The reply status code. By default, the status code is set to 200.
2275
2276 :returns: integer
2277
2278.. js:attribute:: Reply.reason
2279
2280 The reason string describing the status code.
2281
2282 :returns: string
2283
2284.. js:attribute:: Reply.headers
2285
2286 A table indexing all reply headers by name. To each name is associated an
2287 ordered list of values.
2288
2289 :returns: Lua table
2290
2291.. code-block:: lua
2292
2293 {
2294 ["content-type"] = { "text/html" },
2295 ["cache-control"] = {"no-cache", "no-store" },
2296 x_header_name = { "value1", "value2", ... }
2297 ...
2298 }
2299..
2300
2301.. js:attribute:: Reply.body
2302
2303 The reply payload.
2304
2305 :returns: string
2306
2307.. js:function:: Reply.set_status(REPLY, status[, reason])
2308
2309 Set the reply status code and optionally the reason-phrase. If the reason is
2310 not provided, the default reason corresponding to the status code is used.
2311
2312 :param class_reply reply: The related Reply object.
2313 :param integer status: The reply status code.
2314 :param string reason: The reply status reason (optional).
2315
2316.. js:function:: Reply.add_header(REPLY, name, value)
2317
2318 Add a header to the reply object. If the header does not already exist, a new
2319 entry is created with its name as index and a one-element list containing its
2320 value as value. Otherwise, the header value is appended to the ordered list of
2321 values associated to the header name.
2322
2323 :param class_reply reply: The related Reply object.
2324 :param string name: The header field name.
2325 :param string value: The header field value.
2326
2327.. js:function:: Reply.del_header(REPLY, name)
2328
2329 Remove all occurrences of a header name from the reply object.
2330
2331 :param class_reply reply: The related Reply object.
2332 :param string name: The header field name.
2333
2334.. js:function:: Reply.set_body(REPLY, body)
2335
2336 Set the reply payload.
2337
2338 :param class_reply reply: The related Reply object.
2339 :param string body: The reply payload.
2340
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002341.. _socket_class:
2342
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002343Socket class
2344============
2345
2346.. js:class:: Socket
2347
2348 This class must be compatible with the Lua Socket class. Only the 'client'
2349 functions are available. See the Lua Socket documentation:
2350
2351 `http://w3.impa.br/~diego/software/luasocket/tcp.html
2352 <http://w3.impa.br/~diego/software/luasocket/tcp.html>`_
2353
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002354.. js:function:: Socket.close(socket)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002355
2356 Closes a TCP object. The internal socket used by the object is closed and the
2357 local address to which the object was bound is made available to other
2358 applications. No further operations (except for further calls to the close
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002359 method) are allowed on a closed Socket.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002360
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002361 :param class_socket socket: Is the manipulated Socket.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002362
2363 Note: It is important to close all used sockets once they are not needed,
2364 since, in many systems, each socket uses a file descriptor, which are limited
2365 system resources. Garbage-collected objects are automatically closed before
2366 destruction, though.
2367
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002368.. js:function:: Socket.connect(socket, address[, port])
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002369
2370 Attempts to connect a socket object to a remote host.
2371
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002372
2373 In case of error, the method returns nil followed by a string describing the
2374 error. In case of success, the method returns 1.
2375
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002376 :param class_socket socket: Is the manipulated Socket.
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002377 :param string address: can be an IP address or a host name. See below for more
2378 information.
2379 :param integer port: must be an integer number in the range [1..64K].
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002380 :returns: 1 or nil.
2381
Bertrand Jacquin874a35c2018-09-10 21:26:07 +01002382 An address field extension permits to use the connect() function to connect to
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002383 other stream than TCP. The syntax containing a simpleipv4 or ipv6 address is
2384 the basically expected format. This format requires the port.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002385
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002386 Other format accepted are a socket path like "/socket/path", it permits to
Bertrand Jacquin874a35c2018-09-10 21:26:07 +01002387 connect to a socket. Abstract namespaces are supported with the prefix
Joseph Herlant02cedc42018-11-13 19:45:17 -08002388 "abns@", and finally a file descriptor can be passed with the prefix "fd@".
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002389 The prefix "ipv4@", "ipv6@" and "unix@" are also supported. The port can be
Bertrand Jacquin874a35c2018-09-10 21:26:07 +01002390 passed int the string. The syntax "127.0.0.1:1234" is valid. In this case, the
Tim Duesterhus6edab862018-01-06 19:04:45 +01002391 parameter *port* must not be set.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002392
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002393.. js:function:: Socket.connect_ssl(socket, address, port)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002394
2395 Same behavior than the function socket:connect, but uses SSL.
2396
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002397 :param class_socket socket: Is the manipulated Socket.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002398 :returns: 1 or nil.
2399
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002400.. js:function:: Socket.getpeername(socket)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002401
2402 Returns information about the remote side of a connected client object.
2403
2404 Returns a string with the IP address of the peer, followed by the port number
2405 that peer is using for the connection. In case of error, the method returns
2406 nil.
2407
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002408 :param class_socket socket: Is the manipulated Socket.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002409 :returns: a string containing the server information.
2410
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002411.. js:function:: Socket.getsockname(socket)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002412
2413 Returns the local address information associated to the object.
2414
2415 The method returns a string with local IP address and a number with the port.
2416 In case of error, the method returns nil.
2417
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002418 :param class_socket socket: Is the manipulated Socket.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002419 :returns: a string containing the client information.
2420
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002421.. js:function:: Socket.receive(socket, [pattern [, prefix]])
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002422
2423 Reads data from a client object, according to the specified read pattern.
2424 Patterns follow the Lua file I/O format, and the difference in performance
2425 between all patterns is negligible.
2426
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002427 :param class_socket socket: Is the manipulated Socket.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002428 :param string|integer pattern: Describe what is required (see below).
2429 :param string prefix: A string which will be prefix the returned data.
2430 :returns: a string containing the required data or nil.
2431
2432 Pattern can be any of the following:
2433
2434 * **`*a`**: reads from the socket until the connection is closed. No
2435 end-of-line translation is performed;
2436
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002437 * **`*l`**: reads a line of text from the Socket. The line is terminated by a
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002438 LF character (ASCII 10), optionally preceded by a CR character
2439 (ASCII 13). The CR and LF characters are not included in the
2440 returned line. In fact, all CR characters are ignored by the
2441 pattern. This is the default pattern.
2442
2443 * **number**: causes the method to read a specified number of bytes from the
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002444 Socket. Prefix is an optional string to be concatenated to the
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002445 beginning of any received data before return.
2446
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002447 * **empty**: If the pattern is left empty, the default option is `*l`.
2448
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002449 If successful, the method returns the received pattern. In case of error, the
2450 method returns nil followed by an error message which can be the string
2451 'closed' in case the connection was closed before the transmission was
2452 completed or the string 'timeout' in case there was a timeout during the
2453 operation. Also, after the error message, the function returns the partial
2454 result of the transmission.
2455
2456 Important note: This function was changed severely. It used to support
2457 multiple patterns (but I have never seen this feature used) and now it
2458 doesn't anymore. Partial results used to be returned in the same way as
2459 successful results. This last feature violated the idea that all functions
2460 should return nil on error. Thus it was changed too.
2461
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002462.. js:function:: Socket.send(socket, data [, start [, end ]])
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002463
2464 Sends data through client object.
2465
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002466 :param class_socket socket: Is the manipulated Socket.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002467 :param string data: The data that will be sent.
2468 :param integer start: The start position in the buffer of the data which will
2469 be sent.
2470 :param integer end: The end position in the buffer of the data which will
2471 be sent.
2472 :returns: see below.
2473
2474 Data is the string to be sent. The optional arguments i and j work exactly
2475 like the standard string.sub Lua function to allow the selection of a
2476 substring to be sent.
2477
2478 If successful, the method returns the index of the last byte within [start,
2479 end] that has been sent. Notice that, if start is 1 or absent, this is
2480 effectively the total number of bytes sent. In case of error, the method
2481 returns nil, followed by an error message, followed by the index of the last
2482 byte within [start, end] that has been sent. You might want to try again from
2483 the byte following that. The error message can be 'closed' in case the
2484 connection was closed before the transmission was completed or the string
2485 'timeout' in case there was a timeout during the operation.
2486
2487 Note: Output is not buffered. For small strings, it is always better to
2488 concatenate them in Lua (with the '..' operator) and send the result in one
2489 call instead of calling the method several times.
2490
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002491.. js:function:: Socket.setoption(socket, option [, value])
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002492
2493 Just implemented for compatibility, this cal does nothing.
2494
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002495.. js:function:: Socket.settimeout(socket, value [, mode])
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002496
2497 Changes the timeout values for the object. All I/O operations are blocking.
2498 That is, any call to the methods send, receive, and accept will block
2499 indefinitely, until the operation completes. The settimeout method defines a
2500 limit on the amount of time the I/O methods can block. When a timeout time
2501 has elapsed, the affected methods give up and fail with an error code.
2502
2503 The amount of time to wait is specified as the value parameter, in seconds.
2504
Mark Lakes56cc1252018-03-27 09:48:06 +02002505 The timeout modes are not implemented, the only settable timeout is the
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002506 inactivity time waiting for complete the internal buffer send or waiting for
2507 receive data.
2508
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002509 :param class_socket socket: Is the manipulated Socket.
Bertrand Jacquin874a35c2018-09-10 21:26:07 +01002510 :param float value: The timeout value. Use floating point to specify
Mark Lakes56cc1252018-03-27 09:48:06 +02002511 milliseconds.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002512
Thierry FOURNIER31904272017-10-25 12:59:51 +02002513.. _regex_class:
2514
2515Regex class
2516===========
2517
2518.. js:class:: Regex
2519
2520 This class allows the usage of HAProxy regexes because classic lua doesn't
2521 provides regexes. This class inherits the HAProxy compilation options, so the
2522 regexes can be libc regex, pcre regex or pcre JIT regex.
2523
2524 The expression matching number is limited to 20 per regex. The only available
2525 option is case sensitive.
2526
2527 Because regexes compilation is a heavy process, it is better to define all
2528 your regexes in the **body context** and use it during the runtime.
2529
2530.. code-block:: lua
2531
2532 -- Create the regex
2533 st, regex = Regex.new("needle (..) (...)", true);
2534
2535 -- Check compilation errors
2536 if st == false then
2537 print "error: " .. regex
2538 end
2539
2540 -- Match the regexes
2541 print(regex:exec("Looking for a needle in the haystack")) -- true
2542 print(regex:exec("Lokking for a cat in the haystack")) -- false
2543
2544 -- Extract words
2545 st, list = regex:match("Looking for a needle in the haystack")
2546 print(st) -- true
2547 print(list[1]) -- needle in the
2548 print(list[2]) -- in
2549 print(list[3]) -- the
2550
2551.. js:function:: Regex.new(regex, case_sensitive)
2552
2553 Create and compile a regex.
2554
2555 :param string regex: The regular expression according with the libc or pcre
2556 standard
2557 :param boolean case_sensitive: Match is case sensitive or not.
2558 :returns: boolean status and :ref:`regex_class` or string containing fail reason.
2559
2560.. js:function:: Regex.exec(regex, str)
2561
2562 Execute the regex.
2563
2564 :param class_regex regex: A :ref:`regex_class` object.
2565 :param string str: The input string will be compared with the compiled regex.
2566 :returns: a boolean status according with the match result.
2567
2568.. js:function:: Regex.match(regex, str)
2569
2570 Execute the regex and return matched expressions.
2571
2572 :param class_map map: A :ref:`regex_class` object.
2573 :param string str: The input string will be compared with the compiled regex.
2574 :returns: a boolean status according with the match result, and
2575 a table containing all the string matched in order of declaration.
2576
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002577.. _map_class:
2578
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002579Map class
2580=========
2581
2582.. js:class:: Map
2583
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02002584 This class permits to do some lookups in HAProxy maps. The declared maps can
Bertrand Jacquin874a35c2018-09-10 21:26:07 +01002585 be modified during the runtime through the HAProxy management socket.
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002586
2587.. code-block:: lua
2588
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002589 default = "usa"
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002590
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002591 -- Create and load map
Thierry FOURNIER4dc71972017-01-28 08:33:08 +01002592 geo = Map.new("geo.map", Map._ip);
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002593
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002594 -- Create new fetch that returns the user country
2595 core.register_fetches("country", function(txn)
2596 local src;
2597 local loc;
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002598
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002599 src = txn.f:fhdr("x-forwarded-for");
2600 if (src == nil) then
2601 src = txn.f:src()
2602 if (src == nil) then
2603 return default;
2604 end
2605 end
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002606
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002607 -- Perform lookup
2608 loc = geo:lookup(src);
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002609
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002610 if (loc == nil) then
2611 return default;
2612 end
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002613
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002614 return loc;
2615 end);
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002616
Thierry FOURNIER4dc71972017-01-28 08:33:08 +01002617.. js:attribute:: Map._int
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002618
2619 See the HAProxy configuration.txt file, chapter "Using ACLs and fetching
Ilya Shipitsin2075ca82020-03-06 23:22:22 +05002620 samples" and subchapter "ACL basics" to understand this pattern matching
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002621 method.
2622
Thierry FOURNIER4dc71972017-01-28 08:33:08 +01002623 Note that :js:attr:`Map.int` is also available for compatibility.
2624
2625.. js:attribute:: Map._ip
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002626
2627 See the HAProxy configuration.txt file, chapter "Using ACLs and fetching
Ilya Shipitsin2075ca82020-03-06 23:22:22 +05002628 samples" and subchapter "ACL basics" to understand this pattern matching
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002629 method.
2630
Thierry FOURNIER4dc71972017-01-28 08:33:08 +01002631 Note that :js:attr:`Map.ip` is also available for compatibility.
2632
2633.. js:attribute:: Map._str
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002634
2635 See the HAProxy configuration.txt file, chapter "Using ACLs and fetching
Ilya Shipitsin2075ca82020-03-06 23:22:22 +05002636 samples" and subchapter "ACL basics" to understand this pattern matching
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002637 method.
2638
Thierry FOURNIER4dc71972017-01-28 08:33:08 +01002639 Note that :js:attr:`Map.str` is also available for compatibility.
2640
2641.. js:attribute:: Map._beg
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002642
2643 See the HAProxy configuration.txt file, chapter "Using ACLs and fetching
Ilya Shipitsin2075ca82020-03-06 23:22:22 +05002644 samples" and subchapter "ACL basics" to understand this pattern matching
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002645 method.
2646
Thierry FOURNIER4dc71972017-01-28 08:33:08 +01002647 Note that :js:attr:`Map.beg` is also available for compatibility.
2648
2649.. js:attribute:: Map._sub
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002650
2651 See the HAProxy configuration.txt file, chapter "Using ACLs and fetching
Ilya Shipitsin2075ca82020-03-06 23:22:22 +05002652 samples" and subchapter "ACL basics" to understand this pattern matching
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002653 method.
2654
Thierry FOURNIER4dc71972017-01-28 08:33:08 +01002655 Note that :js:attr:`Map.sub` is also available for compatibility.
2656
2657.. js:attribute:: Map._dir
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002658
2659 See the HAProxy configuration.txt file, chapter "Using ACLs and fetching
Ilya Shipitsin2075ca82020-03-06 23:22:22 +05002660 samples" and subchapter "ACL basics" to understand this pattern matching
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002661 method.
2662
Thierry FOURNIER4dc71972017-01-28 08:33:08 +01002663 Note that :js:attr:`Map.dir` is also available for compatibility.
2664
2665.. js:attribute:: Map._dom
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002666
2667 See the HAProxy configuration.txt file, chapter "Using ACLs and fetching
Ilya Shipitsin2075ca82020-03-06 23:22:22 +05002668 samples" and subchapter "ACL basics" to understand this pattern matching
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002669 method.
2670
Thierry FOURNIER4dc71972017-01-28 08:33:08 +01002671 Note that :js:attr:`Map.dom` is also available for compatibility.
2672
2673.. js:attribute:: Map._end
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002674
2675 See the HAProxy configuration.txt file, chapter "Using ACLs and fetching
Ilya Shipitsin2075ca82020-03-06 23:22:22 +05002676 samples" and subchapter "ACL basics" to understand this pattern matching
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002677 method.
2678
Thierry FOURNIER4dc71972017-01-28 08:33:08 +01002679.. js:attribute:: Map._reg
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002680
2681 See the HAProxy configuration.txt file, chapter "Using ACLs and fetching
Ilya Shipitsin2075ca82020-03-06 23:22:22 +05002682 samples" and subchapter "ACL basics" to understand this pattern matching
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002683 method.
2684
Thierry FOURNIER4dc71972017-01-28 08:33:08 +01002685 Note that :js:attr:`Map.reg` is also available for compatibility.
2686
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002687
2688.. js:function:: Map.new(file, method)
2689
2690 Creates and load a map.
2691
2692 :param string file: Is the file containing the map.
2693 :param integer method: Is the map pattern matching method. See the attributes
2694 of the Map class.
2695 :returns: a class Map object.
Thierry FOURNIER4dc71972017-01-28 08:33:08 +01002696 :see: The Map attributes: :js:attr:`Map._int`, :js:attr:`Map._ip`,
2697 :js:attr:`Map._str`, :js:attr:`Map._beg`, :js:attr:`Map._sub`,
2698 :js:attr:`Map._dir`, :js:attr:`Map._dom`, :js:attr:`Map._end` and
2699 :js:attr:`Map._reg`.
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002700
2701.. js:function:: Map.lookup(map, str)
2702
2703 Perform a lookup in a map.
2704
2705 :param class_map map: Is the class Map object.
2706 :param string str: Is the string used as key.
2707 :returns: a string containing the result or nil if no match.
2708
2709.. js:function:: Map.slookup(map, str)
2710
2711 Perform a lookup in a map.
2712
2713 :param class_map map: Is the class Map object.
2714 :param string str: Is the string used as key.
2715 :returns: a string containing the result or empty string if no match.
2716
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002717.. _applethttp_class:
2718
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002719AppletHTTP class
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002720================
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002721
2722.. js:class:: AppletHTTP
2723
2724 This class is used with applets that requires the 'http' mode. The http applet
2725 can be registered with the *core.register_service()* function. They are used
2726 for processing an http request like a server in back of HAProxy.
2727
2728 This is an hello world sample code:
2729
2730.. code-block:: lua
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002731
Pieter Baauw4d7f7662015-11-08 16:38:08 +01002732 core.register_service("hello-world", "http", function(applet)
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002733 local response = "Hello World !"
2734 applet:set_status(200)
Pieter Baauw2dcb9bc2015-10-01 22:47:12 +02002735 applet:add_header("content-length", string.len(response))
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002736 applet:add_header("content-type", "text/plain")
Pieter Baauw2dcb9bc2015-10-01 22:47:12 +02002737 applet:start_response()
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002738 applet:send(response)
2739 end)
2740
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002741.. js:attribute:: AppletHTTP.c
2742
2743 :returns: A :ref:`converters_class`
2744
2745 This attribute contains a Converters class object.
2746
2747.. js:attribute:: AppletHTTP.sc
2748
2749 :returns: A :ref:`converters_class`
2750
2751 This attribute contains a Converters class object. The
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02002752 functions of this object always return a string.
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002753
2754.. js:attribute:: AppletHTTP.f
2755
2756 :returns: A :ref:`fetches_class`
2757
2758 This attribute contains a Fetches class object. Note that the
2759 applet execution place cannot access to a valid HAProxy core HTTP
Ilya Shipitsin2075ca82020-03-06 23:22:22 +05002760 transaction, so some sample fetches related to the HTTP dependent
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002761 values (hdr, path, ...) are not available.
2762
2763.. js:attribute:: AppletHTTP.sf
2764
2765 :returns: A :ref:`fetches_class`
2766
2767 This attribute contains a Fetches class object. The functions of
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02002768 this object always return a string. Note that the applet
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002769 execution place cannot access to a valid HAProxy core HTTP
Ilya Shipitsin2075ca82020-03-06 23:22:22 +05002770 transaction, so some sample fetches related to the HTTP dependent
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002771 values (hdr, path, ...) are not available.
2772
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01002773.. js:attribute:: AppletHTTP.method
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002774
2775 :returns: string
2776
2777 The attribute method returns a string containing the HTTP
2778 method.
2779
2780.. js:attribute:: AppletHTTP.version
2781
2782 :returns: string
2783
2784 The attribute version, returns a string containing the HTTP
2785 request version.
2786
2787.. js:attribute:: AppletHTTP.path
2788
2789 :returns: string
2790
2791 The attribute path returns a string containing the HTTP
2792 request path.
2793
2794.. js:attribute:: AppletHTTP.qs
2795
2796 :returns: string
2797
2798 The attribute qs returns a string containing the HTTP
2799 request query string.
2800
2801.. js:attribute:: AppletHTTP.length
2802
2803 :returns: integer
2804
2805 The attribute length returns an integer containing the HTTP
2806 body length.
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002807
Thierry FOURNIER841475e2015-12-11 17:10:09 +01002808.. js:attribute:: AppletHTTP.headers
2809
Patrick Hemmerc6a1d712018-05-01 21:30:41 -04002810 :returns: table
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002811
Patrick Hemmerc6a1d712018-05-01 21:30:41 -04002812 The attribute headers returns a table containing the HTTP
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002813 headers. The header names are always in lower case. As the header name can be
2814 encountered more than once in each request, the value is indexed with 0 as
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02002815 first index value. The table has this form:
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002816
2817.. code-block:: lua
2818
2819 AppletHTTP.headers['<header-name>'][<header-index>] = "<header-value>"
2820
2821 AppletHTTP.headers["host"][0] = "www.test.com"
2822 AppletHTTP.headers["accept"][0] = "audio/basic q=1"
2823 AppletHTTP.headers["accept"][1] = "audio/*, q=0.2"
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01002824 AppletHTTP.headers["accept"][2] = "*/*, q=0.1"
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002825..
2826
Robin H. Johnson52f5db22017-01-01 13:10:52 -08002827.. js:function:: AppletHTTP.set_status(applet, code [, reason])
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002828
2829 This function sets the HTTP status code for the response. The allowed code are
2830 from 100 to 599.
2831
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01002832 :param class_AppletHTTP applet: An :ref:`applethttp_class`
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002833 :param integer code: the status code returned to the client.
Robin H. Johnson52f5db22017-01-01 13:10:52 -08002834 :param string reason: the status reason returned to the client (optional).
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002835
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01002836.. js:function:: AppletHTTP.add_header(applet, name, value)
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002837
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02002838 This function adds a header in the response. Duplicated headers are not
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002839 collapsed. The special header *content-length* is used to determinate the
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02002840 response length. If it does not exist, a *transfer-encoding: chunked* is set, and
Ilya Shipitsin2075ca82020-03-06 23:22:22 +05002841 all the write from the function *AppletHTTP:send()* become a chunk.
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002842
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01002843 :param class_AppletHTTP applet: An :ref:`applethttp_class`
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002844 :param string name: the header name
2845 :param string value: the header value
2846
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01002847.. js:function:: AppletHTTP.start_response(applet)
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002848
2849 This function indicates to the HTTP engine that it can process and send the
2850 response headers. After this called we cannot add headers to the response; We
2851 cannot use the *AppletHTTP:send()* function if the
2852 *AppletHTTP:start_response()* is not called.
2853
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01002854 :param class_AppletHTTP applet: An :ref:`applethttp_class`
2855
2856.. js:function:: AppletHTTP.getline(applet)
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002857
2858 This function returns a string containing one line from the http body. If the
2859 data returned doesn't contains a final '\\n' its assumed than its the last
2860 available data before the end of stream.
2861
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01002862 :param class_AppletHTTP applet: An :ref:`applethttp_class`
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002863 :returns: a string. The string can be empty if we reach the end of the stream.
2864
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01002865.. js:function:: AppletHTTP.receive(applet, [size])
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002866
2867 Reads data from the HTTP body, according to the specified read *size*. If the
2868 *size* is missing, the function tries to read all the content of the stream
2869 until the end. If the *size* is bigger than the http body, it returns the
Bertrand Jacquin874a35c2018-09-10 21:26:07 +01002870 amount of data available.
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002871
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01002872 :param class_AppletHTTP applet: An :ref:`applethttp_class`
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002873 :param integer size: the required read size.
Ilya Shipitsin11057a32020-06-21 21:18:27 +05002874 :returns: always return a string,the string can be empty is the connection is
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002875 closed.
2876
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01002877.. js:function:: AppletHTTP.send(applet, msg)
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002878
2879 Send the message *msg* on the http request body.
2880
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01002881 :param class_AppletHTTP applet: An :ref:`applethttp_class`
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002882 :param string msg: the message to send.
2883
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01002884.. js:function:: AppletHTTP.get_priv(applet)
2885
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01002886 Return Lua data stored in the current transaction. If no data are stored,
2887 it returns a nil value.
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01002888
2889 :param class_AppletHTTP applet: An :ref:`applethttp_class`
Bertrand Jacquin874a35c2018-09-10 21:26:07 +01002890 :returns: the opaque data previously stored, or nil if nothing is
2891 available.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01002892 :see: :js:func:`AppletHTTP.set_priv`
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01002893
2894.. js:function:: AppletHTTP.set_priv(applet, data)
2895
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02002896 Store any data in the current HAProxy transaction. This action replaces the
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01002897 old stored data.
2898
2899 :param class_AppletHTTP applet: An :ref:`applethttp_class`
2900 :param opaque data: The data which is stored in the transaction.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01002901 :see: :js:func:`AppletHTTP.get_priv`
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01002902
Tim Duesterhus4e172c92020-05-19 13:49:42 +02002903.. js:function:: AppletHTTP.set_var(applet, var, value[, ifexist])
Thierry FOURNIER / OZON.IOc1edafe2016-12-12 16:25:30 +01002904
2905 Converts a Lua type in a HAProxy type and store it in a variable <var>.
2906
2907 :param class_AppletHTTP applet: An :ref:`applethttp_class`
2908 :param string var: The variable name according with the HAProxy variable syntax.
2909 :param type value: The value associated to the variable. The type ca be string or
2910 integer.
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02002911 :param boolean ifexist: If this parameter is set to true the variable
Tim Duesterhus4e172c92020-05-19 13:49:42 +02002912 will only be set if it was defined elsewhere (i.e. used
Willy Tarreau7978c5c2021-09-07 14:24:07 +02002913 within the configuration). For global variables (using the
2914 "proc" scope), they will only be updated and never created.
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02002915 It is highly recommended to always set this to true.
2916
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01002917 :see: :js:func:`AppletHTTP.unset_var`
2918 :see: :js:func:`AppletHTTP.get_var`
Thierry FOURNIER / OZON.IOc1edafe2016-12-12 16:25:30 +01002919
2920.. js:function:: AppletHTTP.unset_var(applet, var)
2921
2922 Unset the variable <var>.
2923
2924 :param class_AppletHTTP applet: An :ref:`applethttp_class`
2925 :param string var: The variable name according with the HAProxy variable syntax.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01002926 :see: :js:func:`AppletHTTP.set_var`
2927 :see: :js:func:`AppletHTTP.get_var`
Thierry FOURNIER / OZON.IOc1edafe2016-12-12 16:25:30 +01002928
2929.. js:function:: AppletHTTP.get_var(applet, var)
2930
2931 Returns data stored in the variable <var> converter in Lua type.
2932
2933 :param class_AppletHTTP applet: An :ref:`applethttp_class`
2934 :param string var: The variable name according with the HAProxy variable syntax.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01002935 :see: :js:func:`AppletHTTP.set_var`
2936 :see: :js:func:`AppletHTTP.unset_var`
Thierry FOURNIER / OZON.IOc1edafe2016-12-12 16:25:30 +01002937
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002938.. _applettcp_class:
2939
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002940AppletTCP class
2941===============
2942
2943.. js:class:: AppletTCP
2944
2945 This class is used with applets that requires the 'tcp' mode. The tcp applet
2946 can be registered with the *core.register_service()* function. They are used
2947 for processing a tcp stream like a server in back of HAProxy.
2948
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002949.. js:attribute:: AppletTCP.c
2950
2951 :returns: A :ref:`converters_class`
2952
2953 This attribute contains a Converters class object.
2954
2955.. js:attribute:: AppletTCP.sc
2956
2957 :returns: A :ref:`converters_class`
2958
2959 This attribute contains a Converters class object. The
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02002960 functions of this object always return a string.
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002961
2962.. js:attribute:: AppletTCP.f
2963
2964 :returns: A :ref:`fetches_class`
2965
2966 This attribute contains a Fetches class object.
2967
2968.. js:attribute:: AppletTCP.sf
2969
2970 :returns: A :ref:`fetches_class`
2971
2972 This attribute contains a Fetches class object.
2973
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01002974.. js:function:: AppletTCP.getline(applet)
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002975
2976 This function returns a string containing one line from the stream. If the
2977 data returned doesn't contains a final '\\n' its assumed than its the last
2978 available data before the end of stream.
2979
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01002980 :param class_AppletTCP applet: An :ref:`applettcp_class`
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002981 :returns: a string. The string can be empty if we reach the end of the stream.
2982
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01002983.. js:function:: AppletTCP.receive(applet, [size])
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002984
2985 Reads data from the TCP stream, according to the specified read *size*. If the
2986 *size* is missing, the function tries to read all the content of the stream
2987 until the end.
2988
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01002989 :param class_AppletTCP applet: An :ref:`applettcp_class`
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002990 :param integer size: the required read size.
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02002991 :returns: always return a string, the string can be empty if the connection is
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002992 closed.
2993
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01002994.. js:function:: AppletTCP.send(appletmsg)
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002995
2996 Send the message on the stream.
2997
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01002998 :param class_AppletTCP applet: An :ref:`applettcp_class`
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002999 :param string msg: the message to send.
3000
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01003001.. js:function:: AppletTCP.get_priv(applet)
3002
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01003003 Return Lua data stored in the current transaction. If no data are stored,
3004 it returns a nil value.
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01003005
3006 :param class_AppletTCP applet: An :ref:`applettcp_class`
Bertrand Jacquin874a35c2018-09-10 21:26:07 +01003007 :returns: the opaque data previously stored, or nil if nothing is
3008 available.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01003009 :see: :js:func:`AppletTCP.set_priv`
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01003010
3011.. js:function:: AppletTCP.set_priv(applet, data)
3012
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02003013 Store any data in the current HAProxy transaction. This action replaces the
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01003014 old stored data.
3015
3016 :param class_AppletTCP applet: An :ref:`applettcp_class`
3017 :param opaque data: The data which is stored in the transaction.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01003018 :see: :js:func:`AppletTCP.get_priv`
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01003019
Tim Duesterhus4e172c92020-05-19 13:49:42 +02003020.. js:function:: AppletTCP.set_var(applet, var, value[, ifexist])
Thierry FOURNIER / OZON.IOc1edafe2016-12-12 16:25:30 +01003021
3022 Converts a Lua type in a HAProxy type and stores it in a variable <var>.
3023
3024 :param class_AppletTCP applet: An :ref:`applettcp_class`
3025 :param string var: The variable name according with the HAProxy variable syntax.
3026 :param type value: The value associated to the variable. The type can be string or
3027 integer.
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02003028 :param boolean ifexist: If this parameter is set to true the variable
Tim Duesterhus4e172c92020-05-19 13:49:42 +02003029 will only be set if it was defined elsewhere (i.e. used
Willy Tarreau7978c5c2021-09-07 14:24:07 +02003030 within the configuration). For global variables (using the
3031 "proc" scope), they will only be updated and never created.
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02003032 It is highly recommended to always set this to true.
3033
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01003034 :see: :js:func:`AppletTCP.unset_var`
3035 :see: :js:func:`AppletTCP.get_var`
Thierry FOURNIER / OZON.IOc1edafe2016-12-12 16:25:30 +01003036
3037.. js:function:: AppletTCP.unset_var(applet, var)
3038
3039 Unsets the variable <var>.
3040
3041 :param class_AppletTCP applet: An :ref:`applettcp_class`
3042 :param string var: The variable name according with the HAProxy variable syntax.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01003043 :see: :js:func:`AppletTCP.unset_var`
3044 :see: :js:func:`AppletTCP.set_var`
Thierry FOURNIER / OZON.IOc1edafe2016-12-12 16:25:30 +01003045
3046.. js:function:: AppletTCP.get_var(applet, var)
3047
3048 Returns data stored in the variable <var> converter in Lua type.
3049
3050 :param class_AppletTCP applet: An :ref:`applettcp_class`
3051 :param string var: The variable name according with the HAProxy variable syntax.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01003052 :see: :js:func:`AppletTCP.unset_var`
3053 :see: :js:func:`AppletTCP.set_var`
Thierry FOURNIER / OZON.IOc1edafe2016-12-12 16:25:30 +01003054
Adis Nezirovic8878f8e2018-07-13 12:18:33 +02003055StickTable class
3056================
3057
3058.. js:class:: StickTable
3059
3060 **context**: task, action, sample-fetch
3061
3062 This class can be used to access the HAProxy stick tables from Lua.
3063
3064.. js:function:: StickTable.info()
3065
3066 Returns stick table attributes as a Lua table. See HAProxy documentation for
Ilya Shipitsin2272d8a2020-12-21 01:22:40 +05003067 "stick-table" for canonical info, or check out example below.
Adis Nezirovic8878f8e2018-07-13 12:18:33 +02003068
3069 :returns: Lua table
3070
3071 Assume our table has IPv4 key and gpc0 and conn_rate "columns":
3072
3073.. code-block:: lua
3074
3075 {
3076 expire=<int>, # Value in ms
3077 size=<int>, # Maximum table size
3078 used=<int>, # Actual number of entries in table
3079 data={ # Data columns, with types as key, and periods as values
3080 (-1 if type is not rate counter)
3081 conn_rate=<int>,
3082 gpc0=-1
3083 },
3084 length=<int>, # max string length for string table keys, key length
3085 # otherwise
3086 nopurge=<boolean>, # purge oldest entries when table is full
3087 type="ip" # can be "ip", "ipv6", "integer", "string", "binary"
3088 }
3089
3090.. js:function:: StickTable.lookup(key)
3091
3092 Returns stick table entry for given <key>
3093
3094 :param string key: Stick table key (IP addresses and strings are supported)
3095 :returns: Lua table
3096
3097.. js:function:: StickTable.dump([filter])
3098
3099 Returns all entries in stick table. An optional filter can be used
3100 to extract entries with specific data values. Filter is a table with valid
3101 comparison operators as keys followed by data type name and value pairs.
3102 Check out the HAProxy docs for "show table" for more details. For the
3103 reference, the supported operators are:
3104 "eq", "ne", "le", "lt", "ge", "gt"
3105
3106 For large tables, execution of this function can take a long time (for
3107 HAProxy standards). That's also true when filter is used, so take care and
3108 measure the impact.
3109
3110 :param table filter: Stick table filter
3111 :returns: Stick table entries (table)
3112
3113 See below for example filter, which contains 4 entries (or comparisons).
3114 (Maximum number of filter entries is 4, defined in the source code)
3115
3116.. code-block:: lua
3117
3118 local filter = {
3119 {"gpc0", "gt", 30}, {"gpc1", "gt", 20}}, {"conn_rate", "le", 10}
3120 }
3121
Christopher Faulet0f3c8902020-01-31 18:57:12 +01003122.. _action_class:
3123
3124Action class
3125=============
3126
3127.. js:class:: Act
3128
3129 **context**: action
3130
3131 This class contains all return codes an action may return. It is the lua
3132 equivalent to HAProxy "ACT_RET_*" code.
3133
3134.. code-block:: lua
3135
3136 core.register_action("deny", { "http-req" }, function (txn)
3137 return act.DENY
3138 end)
3139..
3140.. js:attribute:: act.CONTINUE
3141
3142 This attribute is an integer (0). It instructs HAProxy to continue the current
3143 ruleset processing on the message. It is the default return code for a lua
3144 action.
3145
3146 :returns: integer
3147
3148.. js:attribute:: act.STOP
3149
3150 This attribute is an integer (1). It instructs HAProxy to stop the current
3151 ruleset processing on the message.
3152
3153.. js:attribute:: act.YIELD
3154
3155 This attribute is an integer (2). It instructs HAProxy to temporarily pause
3156 the message processing. It will be resumed later on the same rule. The
3157 corresponding lua script is re-executed for the start.
3158
3159.. js:attribute:: act.ERROR
3160
3161 This attribute is an integer (3). It triggers an internal errors The message
3162 processing is stopped and the transaction is terminated. For HTTP streams, an
3163 HTTP 500 error is returned to the client.
3164
3165 :returns: integer
3166
3167.. js:attribute:: act.DONE
3168
3169 This attribute is an integer (4). It instructs HAProxy to stop the message
3170 processing.
3171
3172 :returns: integer
3173
3174.. js:attribute:: act.DENY
3175
3176 This attribute is an integer (5). It denies the current message. The message
3177 processing is stopped and the transaction is terminated. For HTTP streams, an
3178 HTTP 403 error is returned to the client if the deny is returned during the
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02003179 request analysis. During the response analysis, a HTTP 502 error is returned
Christopher Faulet0f3c8902020-01-31 18:57:12 +01003180 and the server response is discarded.
3181
3182 :returns: integer
3183
3184.. js:attribute:: act.ABORT
3185
3186 This attribute is an integer (6). It aborts the current message. The message
3187 processing is stopped and the transaction is terminated. For HTTP streams,
Willy Tarreau714f3452021-05-09 06:47:26 +02003188 HAProxy assumes a response was already sent to the client. From the Lua
Christopher Faulet0f3c8902020-01-31 18:57:12 +01003189 actions point of view, when this code is used, the transaction is terminated
3190 with no reply.
3191
3192 :returns: integer
3193
3194.. js:attribute:: act.INVALID
3195
3196 This attribute is an integer (7). It triggers an internal errors. The message
3197 processing is stopped and the transaction is terminated. For HTTP streams, an
3198 HTTP 400 error is returned to the client if the error is returned during the
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02003199 request analysis. During the response analysis, a HTTP 502 error is returned
Christopher Faulet0f3c8902020-01-31 18:57:12 +01003200 and the server response is discarded.
3201
3202 :returns: integer
Adis Nezirovic8878f8e2018-07-13 12:18:33 +02003203
Christopher Faulet2c2c2e32020-01-31 19:07:52 +01003204.. js:function:: act:wake_time(milliseconds)
3205
3206 **context**: action
3207
3208 Set the script pause timeout to the specified time, defined in
3209 milliseconds.
3210
3211 :param integer milliseconds: the required milliseconds.
3212
3213 This function may be used when a lua action returns `act.YIELD`, to force its
3214 wake-up at most after the specified number of milliseconds.
3215
Christopher Faulet5a2c6612021-08-15 20:35:25 +02003216.. _filter_class:
3217
3218Filter class
3219=============
3220
3221.. js:class:: filter
3222
3223 **context**: filter
3224
3225 This class contains return codes some filter callback functions may return. It
3226 also contains configuration flags and some helper functions. To understand how
3227 the filter API works, see `doc/internal/filters.txt` documentation.
3228
3229.. js:attribute:: filter.CONTINUE
3230
3231 This attribute is an integer (1). It may be returned by some filter callback
3232 functions to instruct this filtering step is finished for this filter.
3233
3234.. js:attribute:: filter.WAIT
3235
3236 This attribute is an integer (0). It may be returned by some filter callback
3237 functions to instruct the filtering must be paused, waiting for more data or
3238 for an external event depending on this filter.
3239
3240.. js:attribute:: filter.ERROR
3241
3242 This attribute is an integer (-1). It may be returned by some filter callback
3243 functions to trigger an error.
3244
3245.. js:attribute:: filter.FLT_CFG_FL_HTX
3246
3247 This attribute is a flag corresponding to the filter flag FLT_CFG_FL_HTX. When
3248 it is set for a filter, it means the filter is able to filter HTTP streams.
3249
3250.. js:function:: filter.register_data_filter(chn)
3251
3252 **context**: filter
3253
3254 Enable the data filtering on the channel **chn** for the current filter. It
Ilya Shipitsinff0f2782021-08-22 22:18:07 +05003255 may be called at any time from any callback functions proceeding the data
Christopher Faulet5a2c6612021-08-15 20:35:25 +02003256 analysis.
3257
3258 :param class_Channel chn: A :ref:`channel_class`.
3259
3260.. js:function:: filter.unregister_data_filter(chn)
3261
3262 **context**: filter
3263
3264 Disable the data filtering on the channel **chn** for the current filter. It
3265 may be called at any time from any callback functions.
3266
3267 :param class_Channel chn: A :ref:`channel_class`.
3268
3269.. js:function:: filter.wake_time(milliseconds)
3270
3271 **context**: filter
3272
3273 Set the script pause timeout to the specified time, defined in
3274 milliseconds.
3275
3276 :param integer milliseconds: the required milliseconds.
3277
3278 This function may be used from any lua filter callback function to force its
3279 wake-up at most after the specified number of milliseconds. Especially, when
3280 `filter.CONTINUE` is returned.
3281
3282
3283A filters is declared using :js:func:`core.register_filter()` function. The
3284provided class will be used to instantiate filters. It may define following
3285attributes:
3286
3287* id: The filter identifier. It is a string that identifies the filter and is
3288 optional.
3289
3290* flags: The filter flags. Only :js:attr:`filter.FLT_CFG_FL_HTX` may be set for now.
3291
3292Such filter class must also define all required callback functions in the
3293following list. Note that :js:func:`Filter.new()` must be defined otherwise the
Ilya Shipitsinff0f2782021-08-22 22:18:07 +05003294filter is ignored. Others are optional.
Christopher Faulet5a2c6612021-08-15 20:35:25 +02003295
3296* .. js:function:: FILTER.new()
3297
3298 Called to instantiate a new filter. This function must be defined.
3299
3300 :returns: a Lua object that will be used as filter instance for the current
3301 stream.
3302
3303* .. js:function:: FILTER.start_analyze(flt, txn, chn)
3304
3305 Called when the analysis starts on the channel **chn**.
3306
3307* .. js:function:: FILTER.end_analyze(flt, txn, chn)
3308
3309 Called when the analysis ends on the channel **chn**.
3310
3311* .. js:function:: FILTER.http_headers(flt, txn, http_msg)
3312
3313 Called just before the HTTP payload analysis and after any processing on the
3314 HTTP message **http_msg**. This callback functions is only called for HTTP
3315 streams.
3316
3317* .. js:function:: FILTER.http_payload(flt, txn, http_msg)
3318
3319 Called during the HTTP payload analysis on the HTTP message **http_msg**. This
3320 callback functions is only called for HTTP streams.
3321
3322* .. js:function:: FILTER.http_end(flt, txn, http_msg)
3323
3324 Called after the HTTP payload analysis on the HTTP message **http_msg**. This
3325 callback functions is only called for HTTP streams.
3326
3327* .. js:function:: FILTER.tcp_payload(flt, txn, chn)
3328
3329 Called during the TCP payload analysis on the channel **chn**.
3330
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02003331Here is a full example:
Christopher Faulet5a2c6612021-08-15 20:35:25 +02003332
3333.. code-block:: lua
3334
3335 Trace = {}
3336 Trace.id = "Lua trace filter"
3337 Trace.flags = filter.FLT_CFG_FL_HTX;
3338 Trace.__index = Trace
3339
3340 function Trace:new()
3341 local trace = {}
3342 setmetatable(trace, Trace)
3343 trace.req_len = 0
3344 trace.res_len = 0
3345 return trace
3346 end
3347
3348 function Trace:start_analyze(txn, chn)
3349 if chn:is_resp() then
3350 print("Start response analysis")
3351 else
3352 print("Start request analysis")
3353 end
3354 filter.register_data_filter(self, chn)
3355 end
3356
3357 function Trace:end_analyze(txn, chn)
3358 if chn:is_resp() then
3359 print("End response analysis: "..self.res_len.." bytes filtered")
3360 else
3361 print("End request analysis: "..self.req_len.." bytes filtered")
3362 end
3363 end
3364
3365 function Trace:http_headers(txn, http_msg)
3366 stline = http_msg:get_stline()
3367 if http_msg.channel:is_resp() then
3368 print("response:")
3369 print(stline.version.." "..stline.code.." "..stline.reason)
3370 else
3371 print("request:")
3372 print(stline.method.." "..stline.uri.." "..stline.version)
3373 end
3374
3375 for n, hdrs in pairs(http_msg:get_headers()) do
3376 for i,v in pairs(hdrs) do
3377 print(n..": "..v)
3378 end
3379 end
3380 return filter.CONTINUE
3381 end
3382
3383 function Trace:http_payload(txn, http_msg)
3384 body = http_msg:body(-20000)
3385 if http_msg.channel:is_resp() then
3386 self.res_len = self.res_len + body:len()
3387 else
3388 self.req_len = self.req_len + body:len()
3389 end
3390 end
3391
3392 core.register_filter("trace", Trace, function(trace, args)
3393 return trace
3394 end)
3395
3396..
3397
3398.. _httpmessage_class:
3399
3400HTTPMessage class
3401===================
3402
3403.. js:class:: HTTPMessage
3404
3405 **context**: filter
3406
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02003407 This class contains all functions to manipulate a HTTP message. For now, this
Christopher Faulet5a2c6612021-08-15 20:35:25 +02003408 class is only available from a filter context.
3409
3410.. js:function:: HTTPMessage.add_header(http_msg, name, value)
3411
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02003412 Appends a HTTP header field in the HTTP message **http_msg** whose name is
Christopher Faulet5a2c6612021-08-15 20:35:25 +02003413 specified in **name** and whose value is defined in **value**.
3414
3415 :param class_httpmessage http_msg: The manipulated HTTP message.
3416 :param string name: The header name.
3417 :param string value: The header value.
3418
3419.. js:function:: HTTPMessage.append(http_msg, string)
3420
3421 This function copies the string **string** at the end of incoming data of the
3422 HTTP message **http_msg**. The function returns the copied length on success
3423 or -1 if data cannot be copied.
3424
3425 Same that :js:func:`HTTPMessage.insert(http_msg, string, http_msg:input())`.
3426
3427 :param class_httpmessage http_msg: The manipulated HTTP message.
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02003428 :param string string: The data to copy at the end of incoming data.
Christopher Faulet5a2c6612021-08-15 20:35:25 +02003429 :returns: an integer containing the amount of bytes copied or -1.
3430
3431.. js:function:: HTTPMessage.body(http_msgl[, offset[, length]])
3432
3433 This function returns **length** bytes of incoming data from the HTTP message
3434 **http_msg**, starting at the offset **offset**. The data are not removed from
3435 the buffer.
3436
3437 By default, if no length is provided, all incoming data found, starting at the
3438 given offset, are returned. If **length** is set to -1, the function tries to
3439 retrieve a maximum of data. Because it is called in the filter context, it
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02003440 never yield. Not providing an offset is the same as setting it to 0. A
Ilya Shipitsinff0f2782021-08-22 22:18:07 +05003441 positive offset is relative to the beginning of incoming data of the
Christopher Faulet5a2c6612021-08-15 20:35:25 +02003442 http_message buffer while negative offset is relative to their end.
3443
3444 If there is no incoming data and the HTTP message can't receive more data, a 'nil'
3445 value is returned.
3446
3447 :param class_httpmessage http_msg: The manipulated HTTP message.
3448 :param integer offset: *optional* The offset in incoming data to start to get
3449 data. 0 by default. May be negative to be relative to
3450 the end of incoming data.
3451 :param integer length: *optional* The expected length of data to retrieve. All
3452 incoming data by default. May be set to -1 to get a
3453 maximum of data.
3454 :returns: a string containing the data found or nil.
3455
3456.. js:function:: HTTPMessage.eom(http_msg)
3457
3458 This function returns true if the end of message is reached for the HTTP
3459 message **http_msg**.
3460
3461 :param class_httpmessage http_msg: The manipulated HTTP message.
3462 :returns: an integer containing the amount of available bytes.
3463
3464.. js:function:: HTTPMessage.del_header(http_msg, name)
3465
3466 Removes all HTTP header fields in the HTTP message **http_msg** whose name is
3467 specified in **name**.
3468
3469 :param class_httpmessage http_msg: The manipulated http message.
3470 :param string name: The header name.
3471
3472.. js:function:: HTTPMessage.get_headers(http_msg)
3473
3474 Returns a table containing all the headers of the HTTP message **http_msg**.
3475
3476 :param class_httpmessage http_msg: The manipulated http message.
3477 :returns: table of headers.
3478
3479 This is the form of the returned table:
3480
3481.. code-block:: lua
3482
3483 http_msg:get_headers()['<header-name>'][<header-index>] = "<header-value>"
3484
3485 local hdr = http_msg:get_headers()
3486 hdr["host"][0] = "www.test.com"
3487 hdr["accept"][0] = "audio/basic q=1"
3488 hdr["accept"][1] = "audio/*, q=0.2"
3489 hdr["accept"][2] = "*.*, q=0.1"
3490..
3491
3492.. js:function:: HTTPMessage.get_stline(http_msg)
3493
3494 Returns a table containing the start-line of the HTTP message **http_msg**.
3495
3496 :param class_httpmessage http_msg: The manipulated http message.
3497 :returns: the start-line.
3498
3499 This is the form of the returned table:
3500
3501.. code-block:: lua
3502
3503 -- for the request :
3504 {"method" = string, "uri" = string, "version" = string}
3505
3506 -- for the response:
3507 {"version" = string, "code" = string, "reason" = string}
3508..
3509
3510.. js:function:: HTTPMessage.forward(http_msg, length)
3511
3512 This function forwards **length** bytes of data from the HTTP message
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02003513 **http_msg**. Because it is called in the filter context, it never yields. Only
Christopher Faulet5a2c6612021-08-15 20:35:25 +02003514 available incoming data may be forwarded, event if the requested length
3515 exceeds the available amount of incoming data. It returns the amount of data
3516 forwarded.
3517
3518 :param class_httpmessage http_msg: The manipulated HTTP message.
3519 :param integer int: The amount of data to forward.
3520
3521.. js:function:: HTTPMessage.input(http_msg)
3522
3523 This function returns the length of incoming data in the HTTP message
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02003524 **http_msg** from the filter point of view.
Christopher Faulet5a2c6612021-08-15 20:35:25 +02003525
3526 :param class_httpmessage http_msg: The manipulated HTTP message.
3527 :returns: an integer containing the amount of available bytes.
3528
3529.. js:function:: HTTPMessage.insert(http_msg, string[, offset])
3530
3531 This function copies the string **string** at the offset **offset** in
3532 incoming data of the HTTP message **http_msg**. The function returns the
3533 copied length on success or -1 if data cannot be copied.
3534
3535 By default, if no offset is provided, the string is copied in front of
Ilya Shipitsinff0f2782021-08-22 22:18:07 +05003536 incoming data. A positive offset is relative to the beginning of incoming data
Christopher Faulet5a2c6612021-08-15 20:35:25 +02003537 of the HTTP message while negative offset is relative to their end.
3538
3539 :param class_httpmessage http_msg: The manipulated HTTP message.
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02003540 :param string string: The data to copy into incoming data.
3541 :param integer offset: *optional* The offset in incoming data where to copy
Christopher Faulet5a2c6612021-08-15 20:35:25 +02003542 data. 0 by default. May be negative to be relative to
3543 the end of incoming data.
3544 :returns: an integer containing the amount of bytes copied or -1.
3545
3546.. js:function:: HTTPMessage.is_full(http_msg)
3547
3548 This function returns true if the HTTP message **http_msg** is full.
3549
3550 :param class_httpmessage http_msg: The manipulated HTTP message.
3551 :returns: a boolean
3552
3553.. js:function:: HTTPMessage.is_resp(http_msg)
3554
3555 This function returns true if the HTTP message **http_msg** is the response
3556 one.
3557
3558 :param class_httpmessage http_msg: The manipulated HTTP message.
3559 :returns: a boolean
3560
3561.. js:function:: HTTPMessage.may_recv(http_msg)
3562
3563 This function returns true if the HTTP message **http_msg** may still receive
3564 data.
3565
3566 :param class_httpmessage http_msg: The manipulated HTTP message.
3567 :returns: a boolean
3568
3569.. js:function:: HTTPMessage.output(http_msg)
3570
3571 This function returns the length of outgoing data of the HTTP message
3572 **http_msg**.
3573
3574 :param class_httpmessage http_msg: The manipulated HTTP message.
3575 :returns: an integer containing the amount of available bytes.
3576
3577.. js:function:: HTTPMessage.prepend(http_msg, string)
3578
3579 This function copies the string **string** in front of incoming data of the
3580 HTTP message **http_msg**. The function returns the copied length on success
3581 or -1 if data cannot be copied.
3582
3583 Same that :js:func:`HTTPMessage.insert(http_msg, string, 0)`.
3584
3585 :param class_httpmessage http_msg: The manipulated HTTP message.
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02003586 :param string string: The data to copy in front of incoming data.
Christopher Faulet5a2c6612021-08-15 20:35:25 +02003587 :returns: an integer containing the amount of bytes copied or -1.
3588
3589.. js:function:: HTTPMessage.remove(http_msg[, offset[, length]])
3590
3591 This function removes **length** bytes of incoming data of the HTTP message
3592 **http_msg**, starting at offset **offset**. This function returns number of
3593 bytes removed on success.
3594
3595 By default, if no length is provided, all incoming data, starting at the given
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02003596 offset, are removed. Not providing an offset is the same that setting it
Ilya Shipitsinff0f2782021-08-22 22:18:07 +05003597 to 0. A positive offset is relative to the beginning of incoming data of the
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02003598 HTTP message while negative offset is relative to the end.
Christopher Faulet5a2c6612021-08-15 20:35:25 +02003599
3600 :param class_httpmessage http_msg: The manipulated HTTP message.
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02003601 :param integer offset: *optional* The offset in incoming data where to start
Christopher Faulet5a2c6612021-08-15 20:35:25 +02003602 to remove data. 0 by default. May be negative to
3603 be relative to the end of incoming data.
3604 :param integer length: *optional* The length of data to remove. All incoming
3605 data by default.
3606 :returns: an integer containing the amount of bytes removed.
3607
3608.. js:function:: HTTPMessage.rep_header(http_msg, name, regex, replace)
3609
3610 Matches the regular expression in all occurrences of header field **name**
3611 according to regex **regex**, and replaces them with the string **replace**.
3612 The replacement value can contain back references like \1, \2, ... This
3613 function acts on whole header lines, regardless of the number of values they
3614 may contain.
3615
3616 :param class_httpmessage http_msg: The manipulated HTTP message.
3617 :param string name: The header name.
3618 :param string regex: The match regular expression.
3619 :param string replace: The replacement value.
3620
3621.. js:function:: HTTPMessage.rep_value(http_msg, name, regex, replace)
3622
3623 Matches the regular expression on every comma-delimited value of header field
3624 **name** according to regex **regex**, and replaces them with the string
3625 **replace**. The replacement value can contain back references like \1, \2,
3626 ...
3627
3628 :param class_httpmessage http_msg: The manipulated HTTP message.
3629 :param string name: The header name.
3630 :param string regex: The match regular expression.
3631 :param string replace: The replacement value.
3632
3633.. js:function:: HTTPMessage.send(http_msg, string)
3634
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02003635 This function requires immediate send of the string **string**. It means the
Ilya Shipitsinff0f2782021-08-22 22:18:07 +05003636 string is copied at the beginning of incoming data of the HTTP message
Christopher Faulet5a2c6612021-08-15 20:35:25 +02003637 **http_msg** and immediately forwarded. Because it is called in the filter
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02003638 context, it never yields.
Christopher Faulet5a2c6612021-08-15 20:35:25 +02003639
3640 :param class_httpmessage http_msg: The manipulated HTTP message.
3641 :param string string: The data to send.
3642 :returns: an integer containing the amount of bytes copied or -1.
3643
3644.. js:function:: HTTPMessage.set(http_msg, string[, offset[, length]])
3645
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02003646 This function replaces **length** bytes of incoming data of the HTTP message
Christopher Faulet5a2c6612021-08-15 20:35:25 +02003647 **http_msg**, starting at offset **offset**, by the string **string**. The
3648 function returns the copied length on success or -1 if data cannot be copied.
3649
3650 By default, if no length is provided, all incoming data, starting at the given
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02003651 offset, are replaced. Not providing an offset is the same as setting it
Ilya Shipitsinff0f2782021-08-22 22:18:07 +05003652 to 0. A positive offset is relative to the beginning of incoming data of the
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02003653 HTTP message while negative offset is relative to the end.
Christopher Faulet5a2c6612021-08-15 20:35:25 +02003654
3655 :param class_httpmessage http_msg: The manipulated HTTP message.
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02003656 :param string string: The data to copy into incoming data.
3657 :param integer offset: *optional* The offset in incoming data where to start
Christopher Faulet5a2c6612021-08-15 20:35:25 +02003658 the data replacement. 0 by default. May be negative to
3659 be relative to the end of incoming data.
3660 :param integer length: *optional* The length of data to replace. All incoming
3661 data by default.
3662 :returns: an integer containing the amount of bytes copied or -1.
3663
3664.. js:function:: HTTPMessage.set_eom(http_msg)
3665
3666 This function set the end of message for the HTTP message **http_msg**.
3667
3668 :param class_httpmessage http_msg: The manipulated HTTP message.
3669
3670.. js:function:: HTTPMessage.set_header(http_msg, name, value)
3671
3672 This variable replace all occurrence of all header matching the name **name**,
3673 by only one containing the value **value**.
3674
3675 :param class_httpmessage http_msg: The manipulated HTTP message.
3676 :param string name: The header name.
3677 :param string value: The header value.
3678
3679 This function does the same work as the following code:
3680
3681.. code-block:: lua
3682
3683 http_msg:del_header("header")
3684 http_msg:add_header("header", "value")
3685..
3686
3687.. js:function:: HTTPMessage.set_method(http_msg, method)
3688
3689 Rewrites the request method with the string **method**. The HTTP message
3690 **http_msg** must be the request.
3691
3692 :param class_httpmessage http_msg: The manipulated HTTP message.
3693 :param string method: The new method.
3694
3695.. js:function:: HTTPMessage.set_path(http_msg, path)
3696
3697 Rewrites the request path with the string **path**. The HTTP message
3698 **http_msg** must be the request.
3699
3700 :param class_httpmessage http_msg: The manipulated HTTP message.
3701 :param string method: The new method.
3702
3703.. js:function:: HTTPMessage.set_query(http_msg, query)
3704
3705 Rewrites the request's query string which appears after the first question
3706 mark ("?") with the string **query**. The HTTP message **http_msg** must be
3707 the request.
3708
3709 :param class_httpmessage http_msg: The manipulated HTTP message.
3710 :param string query: The new query.
3711
3712.. js:function:: HTTPMessage.set_status(http_msg, status[, reason])
3713
Ilya Shipitsinff0f2782021-08-22 22:18:07 +05003714 Rewrites the response status code with the integer **code** and optional the
Christopher Faulet5a2c6612021-08-15 20:35:25 +02003715 reason **reason**. If no custom reason is provided, it will be generated from
3716 the status. The HTTP message **http_msg** must be the response.
3717
3718 :param class_httpmessage http_msg: The manipulated HTTP message.
3719 :param integer status: The new response status code.
3720 :param string reason: The new response reason (optional).
3721
3722.. js:function:: HTTPMessage.set_uri(http_msg, uri)
3723
3724 Rewrites the request URI with the string **uri**. The HTTP message
3725 **http_msg** must be the request.
3726
3727 :param class_httpmessage http_msg: The manipulated HTTP message.
3728 :param string uri: The new uri.
3729
3730.. js:function:: HTTPMessage.unset_eom(http_msg)
3731
3732 This function remove the end of message for the HTTP message **http_msg**.
3733
3734 :param class_httpmessage http_msg: The manipulated HTTP message.
3735
William Lallemand10cea5c2022-03-30 16:02:43 +02003736.. _CertCache_class:
3737
3738CertCache class
3739================
3740
3741.. js:class:: CertCache
3742
3743 This class allows to update an SSL certificate file in the memory of the
3744 current HAProxy process. It will do the same as "set ssl cert" + "commit ssl
3745 cert" over the HAProxy CLI.
3746
3747.. js:function:: CertCache.set(certificate)
3748
3749 This function updates a certificate in memory.
3750
3751 :param table certificate: A table containing the fields to update.
3752 :param string certificate.filename: The mandatory filename of the certificate
3753 to update, it must already exist in memory.
3754 :param string certificate.crt: A certificate in the PEM format. It can also
3755 contain a private key.
3756 :param string certificate.key: A private key in the PEM format.
3757 :param string certificate.ocsp: An OCSP response in base64. (cf management.txt)
3758 :param string certificate.issuer: The certificate of the OCSP issuer.
3759 :param string certificate.sctl: An SCTL file.
3760
3761.. code-block:: lua
3762
3763 CertCache.set{filename="certs/localhost9994.pem.rsa", crt=crt}
3764
3765
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01003766External Lua libraries
3767======================
3768
3769A lot of useful lua libraries can be found here:
3770
Aurelien DARRAGON846fc7d2022-10-14 08:48:57 +02003771* Lua toolbox has been superseded by `https://luarocks.org/ <https://luarocks.org/>`_
3772 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 +01003773
Ilya Shipitsin2075ca82020-03-06 23:22:22 +05003774Redis client library:
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01003775
3776* `https://github.com/nrk/redis-lua <https://github.com/nrk/redis-lua>`_
3777
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02003778This is an example about the usage of the Redis library within HAProxy. Note that
3779each call to any function of this library can throw an error if the socket
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01003780connection fails.
3781
3782.. code-block:: lua
3783
3784 -- load the redis library
3785 local redis = require("redis");
3786
3787 function do_something(txn)
3788
3789 -- create and connect new tcp socket
3790 local tcp = core.tcp();
3791 tcp:settimeout(1);
3792 tcp:connect("127.0.0.1", 6379);
3793
3794 -- use the redis library with this new socket
3795 local client = redis.connect({socket=tcp});
3796 client:ping();
3797
3798 end
3799
3800OpenSSL:
3801
3802* `http://mkottman.github.io/luacrypto/index.html
3803 <http://mkottman.github.io/luacrypto/index.html>`_
3804
3805* `https://github.com/brunoos/luasec/wiki
3806 <https://github.com/brunoos/luasec/wiki>`_