blob: 3aa1c69560231893dc9a89c8a9107708662a1785 [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
122HAProxy lua-load(-per-thread) directives allow a list of paramaters after
123the 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 Fournierf61aa632016-02-19 20:56:00 +0100471.. js:attribute:: core.proxies
472
473 **context**: body, init, task, action, sample-fetch, converter
474
Bertrand Jacquin874a35c2018-09-10 21:26:07 +0100475 Proxies is a table containing the list of all proxies declared in the
Patrick Hemmerc6a1d712018-05-01 21:30:41 -0400476 configuration file. The table is indexed by the proxy name, and each entry
477 of the proxies table is an object of type :ref:`proxy_class`.
478
Christopher Faulet1e9b1b62021-08-11 10:14:30 +0200479 .. warning::
480 if you have declared a frontend and backend with the same name, only one of
481 these are listed.
Thierry Fournierf61aa632016-02-19 20:56:00 +0100482
Thierry FOURNIERc5d11c62018-02-12 14:46:54 +0100483.. js:function:: core.register_action(name, actions, func [, nb_args])
Thierry FOURNIER8255a752015-09-23 21:03:35 +0200484
485 **context**: body
486
David Carlier61fdf8b2015-10-02 11:59:38 +0100487 Register a Lua function executed as action. All the registered action can be
Thierry FOURNIER8255a752015-09-23 21:03:35 +0200488 used in HAProxy with the prefix "lua.". An action gets a TXN object class as
489 input.
490
491 :param string name: is the name of the converter.
Willy Tarreau61add3c2015-09-28 15:39:10 +0200492 :param table actions: is a table of string describing the HAProxy actions who
Thierry FOURNIER8255a752015-09-23 21:03:35 +0200493 want to register to. The expected actions are 'tcp-req',
494 'tcp-res', 'http-req' or 'http-res'.
Aurelien DARRAGON53901f42022-10-13 19:49:42 +0200495 :param function func: is the Lua function called to work as converter.
Thierry FOURNIERc5d11c62018-02-12 14:46:54 +0100496 :param integer nb_args: is the expected number of argument for the action.
497 By default the value is 0.
Thierry FOURNIER8255a752015-09-23 21:03:35 +0200498
499 The prototype of the Lua function used as argument is:
500
501.. code-block:: lua
502
Thierry FOURNIERc5d11c62018-02-12 14:46:54 +0100503 function(txn [, arg1 [, arg2]])
Thierry FOURNIER8255a752015-09-23 21:03:35 +0200504..
505
Thierry FOURNIERdc595002015-12-21 11:13:52 +0100506 * **txn** (:ref:`txn_class`): this is a TXN object used for manipulating the
Thierry FOURNIER8255a752015-09-23 21:03:35 +0200507 current request or TCP stream.
508
Bertrand Jacquin874a35c2018-09-10 21:26:07 +0100509 * **argX**: this is argument provided through the HAProxy configuration file.
Thierry FOURNIERc5d11c62018-02-12 14:46:54 +0100510
Bertrand Jacquin874a35c2018-09-10 21:26:07 +0100511 Here, an example of action registration. The action just send an 'Hello world'
Thierry FOURNIER8255a752015-09-23 21:03:35 +0200512 in the logs.
513
514.. code-block:: lua
515
516 core.register_action("hello-world", { "tcp-req", "http-req" }, function(txn)
517 txn:Info("Hello world")
518 end)
519..
520
Willy Tarreau714f3452021-05-09 06:47:26 +0200521 This example code is used in HAProxy configuration like this:
Thierry FOURNIER8255a752015-09-23 21:03:35 +0200522
523::
524
525 frontend tcp_frt
526 mode tcp
527 tcp-request content lua.hello-world
528
529 frontend http_frt
530 mode http
531 http-request lua.hello-world
Thierry FOURNIERc5d11c62018-02-12 14:46:54 +0100532..
533
Bertrand Jacquin874a35c2018-09-10 21:26:07 +0100534 A second example using arguments
Thierry FOURNIERc5d11c62018-02-12 14:46:54 +0100535
536.. code-block:: lua
537
538 function hello_world(txn, arg)
539 txn:Info("Hello world for " .. arg)
540 end
541 core.register_action("hello-world", { "tcp-req", "http-req" }, hello_world, 2)
542..
Thierry FOURNIER8255a752015-09-23 21:03:35 +0200543
Willy Tarreau714f3452021-05-09 06:47:26 +0200544 This example code is used in HAProxy configuration like this:
Thierry FOURNIERc5d11c62018-02-12 14:46:54 +0100545
546::
547
548 frontend tcp_frt
549 mode tcp
550 tcp-request content lua.hello-world everybody
551..
Aurelien DARRAGON53901f42022-10-13 19:49:42 +0200552
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100553.. js:function:: core.register_converters(name, func)
554
555 **context**: body
556
David Carlier61fdf8b2015-10-02 11:59:38 +0100557 Register a Lua function executed as converter. All the registered converters
Aurelien DARRAGON53901f42022-10-13 19:49:42 +0200558 can be used in HAProxy with the prefix "lua.". A converter gets a string as
559 input and returns a string as output. The registered function can take up to 9
560 values as parameter. All the values are strings.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100561
562 :param string name: is the name of the converter.
563 :param function func: is the Lua function called to work as converter.
564
565 The prototype of the Lua function used as argument is:
566
567.. code-block:: lua
568
569 function(str, [p1 [, p2 [, ... [, p5]]]])
570..
571
572 * **str** (*string*): this is the input value automatically converted in
573 string.
574 * **p1** .. **p5** (*string*): this is a list of string arguments declared in
Bertrand Jacquin874a35c2018-09-10 21:26:07 +0100575 the HAProxy configuration file. The number of arguments doesn't exceed 5.
Aurelien DARRAGON53901f42022-10-13 19:49:42 +0200576 The order and the nature of these is conventionally chosen by the
Bertrand Jacquin874a35c2018-09-10 21:26:07 +0100577 developer.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100578
579.. js:function:: core.register_fetches(name, func)
580
581 **context**: body
582
David Carlier61fdf8b2015-10-02 11:59:38 +0100583 Register a Lua function executed as sample fetch. All the registered sample
Bertrand Jacquin874a35c2018-09-10 21:26:07 +0100584 fetch can be used in HAProxy with the prefix "lua.". A Lua sample fetch
Aurelien DARRAGON53901f42022-10-13 19:49:42 +0200585 returns a string as output. The registered function can take up to 9 values as
586 parameter. All the values are strings.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100587
Aurelien DARRAGON53901f42022-10-13 19:49:42 +0200588 :param string name: is the name of the sample fetch.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100589 :param function func: is the Lua function called to work as sample fetch.
590
591 The prototype of the Lua function used as argument is:
592
593.. code-block:: lua
594
595 string function(txn, [p1 [, p2 [, ... [, p5]]]])
596..
597
Thierry FOURNIERdc595002015-12-21 11:13:52 +0100598 * **txn** (:ref:`txn_class`): this is the txn object associated with the current
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100599 request.
600 * **p1** .. **p5** (*string*): this is a list of string arguments declared in
Bertrand Jacquin874a35c2018-09-10 21:26:07 +0100601 the HAProxy configuration file. The number of arguments doesn't exceed 5.
Aurelien DARRAGON53901f42022-10-13 19:49:42 +0200602 The order and the nature of these is conventionally chosen by the
Bertrand Jacquin874a35c2018-09-10 21:26:07 +0100603 developer.
604 * **Returns**: A string containing some data, or nil if the value cannot be
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100605 returned now.
606
607 lua example code:
608
609.. code-block:: lua
610
611 core.register_fetches("hello", function(txn)
612 return "hello"
613 end)
614..
615
616 HAProxy example configuration:
617
618::
619
620 frontend example
621 http-request redirect location /%[lua.hello]
622
Christopher Faulet5a2c6612021-08-15 20:35:25 +0200623.. js:function:: core.register_filter(name, Flt, func)
624
625 **context**: body
626
627 Register a Lua function used to declare a filter. All the registered filters
628 can by used in HAProxy with the prefix "lua.".
629
630 :param string name: is the name of the filter.
631 :param table Flt: is a Lua class containing the filter definition (id, flags,
632 callbacks).
633 :param function func: is the Lua function called to create the Lua filter.
634
635 The prototype of the Lua function used as argument is:
636
637.. code-block:: lua
638
639 function(flt, args)
640..
641
642 * **flt** : Is a filter object based on the class provided in
643 :js:func:`core.register_filter()` function.
644
645 * **args**: Is a table of strings containing all arguments provided through
646 the HAProxy configuration file, on the filter line.
647
648 It must return the filter to use or nil to ignore it. Here, an example of
649 filter registration.
650
651.. code-block:: lua
652
653 core.register_filter("my-filter", MyFilter, function(flt, args)
654 flt.args = args -- Save arguments
655 return flt
656 end)
657..
658
659 This example code is used in HAProxy configuration like this:
660
661::
662
663 frontend http
664 mode http
665 filter lua.my-filter arg1 arg2 arg3
666..
667
668 :see: :js:class:`Filter`
669
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +0200670.. js:function:: core.register_service(name, mode, func)
671
672 **context**: body
673
Aurelien DARRAGON53901f42022-10-13 19:49:42 +0200674 Register a Lua function executed as a service. All the registered services can
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +0200675 be used in HAProxy with the prefix "lua.". A service gets an object class as
676 input according with the required mode.
677
Aurelien DARRAGON53901f42022-10-13 19:49:42 +0200678 :param string name: is the name of the service.
Willy Tarreau61add3c2015-09-28 15:39:10 +0200679 :param string mode: is string describing the required mode. Only 'tcp' or
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +0200680 'http' are allowed.
Aurelien DARRAGON53901f42022-10-13 19:49:42 +0200681 :param function func: is the Lua function called to work as service.
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +0200682
683 The prototype of the Lua function used as argument is:
684
685.. code-block:: lua
686
687 function(applet)
688..
689
Thierry FOURNIERdc595002015-12-21 11:13:52 +0100690 * **applet** *applet* will be a :ref:`applettcp_class` or a
691 :ref:`applethttp_class`. It depends the type of registered applet. An applet
692 registered with the 'http' value for the *mode* parameter will gets a
693 :ref:`applethttp_class`. If the *mode* value is 'tcp', the applet will gets
694 a :ref:`applettcp_class`.
695
Christopher Faulet1e9b1b62021-08-11 10:14:30 +0200696 .. warning::
697 Applets of type 'http' cannot be called from 'tcp-*' rulesets. Only the
698 'http-*' rulesets are authorized, this means that is not possible to call
Aurelien DARRAGON53901f42022-10-13 19:49:42 +0200699 a HTTP applet from a proxy in tcp mode. Applets of type 'tcp' can be
Christopher Faulet1e9b1b62021-08-11 10:14:30 +0200700 called from anywhere.
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +0200701
Bertrand Jacquin874a35c2018-09-10 21:26:07 +0100702 Here, an example of service registration. The service just send an 'Hello world'
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +0200703 as an http response.
704
705.. code-block:: lua
706
Pieter Baauw4d7f7662015-11-08 16:38:08 +0100707 core.register_service("hello-world", "http", function(applet)
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +0200708 local response = "Hello World !"
709 applet:set_status(200)
Pieter Baauw2dcb9bc2015-10-01 22:47:12 +0200710 applet:add_header("content-length", string.len(response))
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +0200711 applet:add_header("content-type", "text/plain")
Pieter Baauw2dcb9bc2015-10-01 22:47:12 +0200712 applet:start_response()
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +0200713 applet:send(response)
714 end)
715..
716
Willy Tarreau714f3452021-05-09 06:47:26 +0200717 This example code is used in HAProxy configuration like this:
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +0200718
719::
720
721 frontend example
722 http-request use-service lua.hello-world
723
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100724.. js:function:: core.register_init(func)
725
726 **context**: body
727
728 Register a function executed after the configuration parsing. This is useful
729 to check any parameters.
730
Pieter Baauw4d7f7662015-11-08 16:38:08 +0100731 :param function func: is the Lua function called to work as initializer.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100732
733 The prototype of the Lua function used as argument is:
734
735.. code-block:: lua
736
737 function()
738..
739
740 It takes no input, and no output is expected.
741
742.. js:function:: core.register_task(func)
743
744 **context**: body, init, task, action, sample-fetch, converter
745
746 Register and start independent task. The task is started when the HAProxy
747 main scheduler starts. For example this type of tasks can be executed to
Thierry FOURNIER2e4893c2015-03-18 13:37:27 +0100748 perform complex health checks.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100749
Pieter Baauw4d7f7662015-11-08 16:38:08 +0100750 :param function func: is the Lua function called to work as initializer.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100751
752 The prototype of the Lua function used as argument is:
753
754.. code-block:: lua
755
756 function()
757..
758
759 It takes no input, and no output is expected.
760
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +0100761.. js:function:: core.register_cli([path], usage, func)
762
763 **context**: body
764
Aurelien DARRAGON53901f42022-10-13 19:49:42 +0200765 Register a custom cli that will be available from haproxy stats socket.
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +0100766
767 :param array path: is the sequence of word for which the cli execute the Lua
768 binding.
769 :param string usage: is the usage message displayed in the help.
770 :param function func: is the Lua function called to handle the CLI commands.
771
772 The prototype of the Lua function used as argument is:
773
774.. code-block:: lua
775
776 function(AppletTCP, [arg1, [arg2, [...]]])
777..
778
779 I/O are managed with the :ref:`applettcp_class` object. Args are given as
Bertrand Jacquin874a35c2018-09-10 21:26:07 +0100780 parameter. The args embed the registered path. If the path is declared like
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +0100781 this:
782
783.. code-block:: lua
784
785 core.register_cli({"show", "ssl", "stats"}, "Display SSL stats..", function(applet, arg1, arg2, arg3, arg4, arg5)
786 end)
787..
788
789 And we execute this in the prompt:
790
791.. code-block:: text
792
793 > prompt
794 > show ssl stats all
795..
796
Bertrand Jacquin874a35c2018-09-10 21:26:07 +0100797 Then, arg1, arg2 and arg3 will contains respectively "show", "ssl" and "stats".
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +0100798 arg4 will contain "all". arg5 contains nil.
799
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100800.. js:function:: core.set_nice(nice)
801
802 **context**: task, action, sample-fetch, converter
803
804 Change the nice of the current task or current session.
Thierry FOURNIER2e4893c2015-03-18 13:37:27 +0100805
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100806 :param integer nice: the nice value, it must be between -1024 and 1024.
807
808.. js:function:: core.set_map(filename, key, value)
809
810 **context**: init, task, action, sample-fetch, converter
811
Bertrand Jacquin874a35c2018-09-10 21:26:07 +0100812 Set the value *value* associated to the key *key* in the map referenced by
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100813 *filename*.
814
Thierry FOURNIERdc595002015-12-21 11:13:52 +0100815 :param string filename: the Map reference
816 :param string key: the key to set or replace
817 :param string value: the associated value
818
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100819.. js:function:: core.sleep(int seconds)
820
821 **context**: body, init, task, action
822
823 The `core.sleep()` functions stop the Lua execution between specified seconds.
824
825 :param integer seconds: the required seconds.
826
827.. js:function:: core.tcp()
828
829 **context**: init, task, action
830
831 This function returns a new object of a *socket* class.
832
Thierry FOURNIERdc595002015-12-21 11:13:52 +0100833 :returns: A :ref:`socket_class` object.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100834
William Lallemand00a15022021-11-19 16:02:44 +0100835.. js:function:: core.httpclient()
836
837 **context**: init, task, action
838
839 This function returns a new object of a *httpclient* class.
840
841 :returns: A :ref:`httpclient_class` object.
842
Thierry Fournier1de16592016-01-27 09:49:07 +0100843.. js:function:: core.concat()
844
845 **context**: body, init, task, action, sample-fetch, converter
846
Bertrand Jacquin874a35c2018-09-10 21:26:07 +0100847 This function returns a new concat object.
Thierry Fournier1de16592016-01-27 09:49:07 +0100848
849 :returns: A :ref:`concat_class` object.
850
Thierry FOURNIER0a99b892015-08-26 00:14:17 +0200851.. js:function:: core.done(data)
852
853 **context**: body, init, task, action, sample-fetch, converter
854
855 :param any data: Return some data for the caller. It is useful with
856 sample-fetches and sample-converters.
857
858 Immediately stops the current Lua execution and returns to the caller which
859 may be a sample fetch, a converter or an action and returns the specified
Thierry Fournier4234dbd2020-11-28 13:18:23 +0100860 value (ignored for actions and init). It is used when the LUA process finishes
861 its work and wants to give back the control to HAProxy without executing the
Thierry FOURNIER0a99b892015-08-26 00:14:17 +0200862 remaining code. It can be seen as a multi-level "return".
863
Thierry FOURNIER486f5a02015-03-16 15:13:03 +0100864.. js:function:: core.yield()
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100865
866 **context**: task, action, sample-fetch, converter
867
868 Give back the hand at the HAProxy scheduler. It is used when the LUA
869 processing consumes a lot of processing time.
870
Thierry FOURNIER / OZON.IO62fec752016-11-10 20:38:11 +0100871.. js:function:: core.parse_addr(address)
872
873 **context**: body, init, task, action, sample-fetch, converter
874
875 :param network: is a string describing an ipv4 or ipv6 address and optionally
876 its network length, like this: "127.0.0.1/8" or "aaaa::1234/32".
877 :returns: a userdata containing network or nil if an error occurs.
878
Bertrand Jacquin874a35c2018-09-10 21:26:07 +0100879 Parse ipv4 or ipv6 addresses and its facultative associated network.
Thierry FOURNIER / OZON.IO62fec752016-11-10 20:38:11 +0100880
881.. js:function:: core.match_addr(addr1, addr2)
882
883 **context**: body, init, task, action, sample-fetch, converter
884
885 :param addr1: is an address created with "core.parse_addr".
886 :param addr2: is an address created with "core.parse_addr".
Bertrand Jacquin874a35c2018-09-10 21:26:07 +0100887 :returns: boolean, true if the network of the addresses match, else returns
Thierry FOURNIER / OZON.IO62fec752016-11-10 20:38:11 +0100888 false.
889
Ilya Shipitsin2075ca82020-03-06 23:22:22 +0500890 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 +0100891 of network is not important.
892
Thierry FOURNIER / OZON.IO8a1027a2016-11-24 20:48:38 +0100893.. js:function:: core.tokenize(str, separators [, noblank])
894
895 **context**: body, init, task, action, sample-fetch, converter
896
897 This function is useful for tokenizing an entry, or splitting some messages.
898 :param string str: The string which will be split.
899 :param string separators: A string containing a list of separators.
900 :param boolean noblank: Ignore empty entries.
901 :returns: an array of string.
902
903 For example:
904
905.. code-block:: lua
906
907 local array = core.tokenize("This function is useful, for tokenizing an entry.", "., ", true)
908 print_r(array)
909..
910
911 Returns this array:
912
913.. code-block:: text
914
915 (table) table: 0x21c01e0 [
916 1: (string) "This"
917 2: (string) "function"
918 3: (string) "is"
919 4: (string) "useful"
920 5: (string) "for"
921 6: (string) "tokenizing"
922 7: (string) "an"
923 8: (string) "entry"
924 ]
925..
926
Thierry Fournierf61aa632016-02-19 20:56:00 +0100927.. _proxy_class:
928
929Proxy class
930============
931
932.. js:class:: Proxy
933
934 This class provides a way for manipulating proxy and retrieving information
935 like statistics.
936
Thierry FOURNIER817e7592017-07-24 14:35:04 +0200937.. js:attribute:: Proxy.name
938
939 Contain the name of the proxy.
940
Baptiste Assmann46c72552017-10-26 21:51:58 +0200941.. js:attribute:: Proxy.uuid
942
943 Contain the unique identifier of the proxy.
944
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +0100945.. js:attribute:: Proxy.servers
946
Patrick Hemmerc6a1d712018-05-01 21:30:41 -0400947 Contain a table with the attached servers. The table is indexed by server
948 name, and each server entry is an object of type :ref:`server_class`.
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +0100949
Adis Nezirovic8878f8e2018-07-13 12:18:33 +0200950.. js:attribute:: Proxy.stktable
951
952 Contains a stick table object attached to the proxy.
953
Thierry Fournierff480422016-02-25 08:36:46 +0100954.. js:attribute:: Proxy.listeners
955
Patrick Hemmerc6a1d712018-05-01 21:30:41 -0400956 Contain a table with the attached listeners. The table is indexed by listener
957 name, and each each listeners entry is an object of type
958 :ref:`listener_class`.
Thierry Fournierff480422016-02-25 08:36:46 +0100959
Thierry Fournierf61aa632016-02-19 20:56:00 +0100960.. js:function:: Proxy.pause(px)
961
962 Pause the proxy. See the management socket documentation for more information.
963
964 :param class_proxy px: A :ref:`proxy_class` which indicates the manipulated
965 proxy.
966
967.. js:function:: Proxy.resume(px)
968
969 Resume the proxy. See the management socket documentation for more
970 information.
971
972 :param class_proxy px: A :ref:`proxy_class` which indicates the manipulated
973 proxy.
974
975.. js:function:: Proxy.stop(px)
976
977 Stop the proxy. See the management socket documentation for more information.
978
979 :param class_proxy px: A :ref:`proxy_class` which indicates the manipulated
980 proxy.
981
982.. js:function:: Proxy.shut_bcksess(px)
983
984 Kill the session attached to a backup server. See the management socket
985 documentation for more information.
986
987 :param class_proxy px: A :ref:`proxy_class` which indicates the manipulated
988 proxy.
989
990.. js:function:: Proxy.get_cap(px)
991
992 Returns a string describing the capabilities of the proxy.
993
994 :param class_proxy px: A :ref:`proxy_class` which indicates the manipulated
995 proxy.
996 :returns: a string "frontend", "backend", "proxy" or "ruleset".
997
998.. js:function:: Proxy.get_mode(px)
999
1000 Returns a string describing the mode of the current proxy.
1001
1002 :param class_proxy px: A :ref:`proxy_class` which indicates the manipulated
1003 proxy.
1004 :returns: a string "tcp", "http", "health" or "unknown"
1005
1006.. js:function:: Proxy.get_stats(px)
1007
Bertrand Jacquin874a35c2018-09-10 21:26:07 +01001008 Returns a table containing the proxy statistics. The statistics returned are
Thierry Fournierf61aa632016-02-19 20:56:00 +01001009 not the same if the proxy is frontend or a backend.
1010
1011 :param class_proxy px: A :ref:`proxy_class` which indicates the manipulated
1012 proxy.
Patrick Hemmerc6a1d712018-05-01 21:30:41 -04001013 :returns: a key/value table containing stats
Thierry Fournierf61aa632016-02-19 20:56:00 +01001014
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001015.. _server_class:
1016
1017Server class
1018============
1019
Patrick Hemmerc6a1d712018-05-01 21:30:41 -04001020.. js:class:: Server
1021
1022 This class provides a way for manipulating servers and retrieving information.
1023
Patrick Hemmera62ae7e2018-04-29 14:23:48 -04001024.. js:attribute:: Server.name
1025
1026 Contain the name of the server.
1027
1028.. js:attribute:: Server.puid
1029
1030 Contain the proxy unique identifier of the server.
1031
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001032.. js:function:: Server.is_draining(sv)
1033
Patrick Hemmerc6a1d712018-05-01 21:30:41 -04001034 Return true if the server is currently draining sticky connections.
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001035
1036 :param class_server sv: A :ref:`server_class` which indicates the manipulated
1037 server.
1038 :returns: a boolean
1039
Patrick Hemmer32d539f2018-04-29 14:25:46 -04001040.. js:function:: Server.set_maxconn(sv, weight)
1041
1042 Dynamically change the maximum connections of the server. See the management
1043 socket documentation for more information about the format of the string.
1044
1045 :param class_server sv: A :ref:`server_class` which indicates the manipulated
1046 server.
1047 :param string maxconn: A string describing the server maximum connections.
1048
1049.. js:function:: Server.get_maxconn(sv, weight)
1050
1051 This function returns an integer representing the server maximum connections.
1052
1053 :param class_server sv: A :ref:`server_class` which indicates the manipulated
1054 server.
1055 :returns: an integer.
1056
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001057.. js:function:: Server.set_weight(sv, weight)
1058
Patrick Hemmerc6a1d712018-05-01 21:30:41 -04001059 Dynamically change the weight of the server. See the management socket
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001060 documentation for more information about the format of the string.
1061
1062 :param class_server sv: A :ref:`server_class` which indicates the manipulated
1063 server.
1064 :param string weight: A string describing the server weight.
1065
1066.. js:function:: Server.get_weight(sv)
1067
Patrick Hemmerc6a1d712018-05-01 21:30:41 -04001068 This function returns an integer representing the server weight.
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001069
1070 :param class_server sv: A :ref:`server_class` which indicates the manipulated
1071 server.
1072 :returns: an integer.
1073
Joseph C. Sible49bbf522020-05-04 22:20:32 -04001074.. js:function:: Server.set_addr(sv, addr[, port])
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001075
Patrick Hemmerc6a1d712018-05-01 21:30:41 -04001076 Dynamically change the address of the server. See the management socket
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001077 documentation for more information about the format of the string.
1078
1079 :param class_server sv: A :ref:`server_class` which indicates the manipulated
1080 server.
Patrick Hemmerc6a1d712018-05-01 21:30:41 -04001081 :param string addr: A string describing the server address.
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001082
1083.. js:function:: Server.get_addr(sv)
1084
Patrick Hemmerc6a1d712018-05-01 21:30:41 -04001085 Returns a string describing the address of the server.
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001086
1087 :param class_server sv: A :ref:`server_class` which indicates the manipulated
1088 server.
1089 :returns: A string
1090
1091.. js:function:: Server.get_stats(sv)
1092
1093 Returns server statistics.
1094
1095 :param class_server sv: A :ref:`server_class` which indicates the manipulated
1096 server.
Patrick Hemmerc6a1d712018-05-01 21:30:41 -04001097 :returns: a key/value table containing stats
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001098
1099.. js:function:: Server.shut_sess(sv)
1100
1101 Shutdown all the sessions attached to the server. See the management socket
1102 documentation for more information about this function.
1103
1104 :param class_server sv: A :ref:`server_class` which indicates the manipulated
1105 server.
1106
1107.. js:function:: Server.set_drain(sv)
1108
1109 Drain sticky sessions. See the management socket documentation for more
1110 information about this function.
1111
1112 :param class_server sv: A :ref:`server_class` which indicates the manipulated
1113 server.
1114
1115.. js:function:: Server.set_maint(sv)
1116
1117 Set maintenance mode. See the management socket documentation for more
1118 information about this function.
1119
1120 :param class_server sv: A :ref:`server_class` which indicates the manipulated
1121 server.
1122
1123.. js:function:: Server.set_ready(sv)
1124
1125 Set normal mode. See the management socket documentation for more information
1126 about this function.
1127
1128 :param class_server sv: A :ref:`server_class` which indicates the manipulated
1129 server.
1130
1131.. js:function:: Server.check_enable(sv)
1132
1133 Enable health checks. See the management socket documentation for more
1134 information about this function.
1135
1136 :param class_server sv: A :ref:`server_class` which indicates the manipulated
1137 server.
1138
1139.. js:function:: Server.check_disable(sv)
1140
1141 Disable health checks. See the management socket documentation for more
1142 information about this function.
1143
1144 :param class_server sv: A :ref:`server_class` which indicates the manipulated
1145 server.
1146
1147.. js:function:: Server.check_force_up(sv)
1148
1149 Force health-check up. See the management socket documentation for more
1150 information about this function.
1151
1152 :param class_server sv: A :ref:`server_class` which indicates the manipulated
1153 server.
1154
1155.. js:function:: Server.check_force_nolb(sv)
1156
1157 Force health-check nolb mode. See the management socket documentation for more
1158 information about this function.
1159
1160 :param class_server sv: A :ref:`server_class` which indicates the manipulated
1161 server.
1162
1163.. js:function:: Server.check_force_down(sv)
1164
1165 Force health-check down. See the management socket documentation for more
1166 information about this function.
1167
1168 :param class_server sv: A :ref:`server_class` which indicates the manipulated
1169 server.
1170
1171.. js:function:: Server.agent_enable(sv)
1172
1173 Enable agent check. See the management socket documentation for more
1174 information about this function.
1175
1176 :param class_server sv: A :ref:`server_class` which indicates the manipulated
1177 server.
1178
1179.. js:function:: Server.agent_disable(sv)
1180
1181 Disable agent check. See the management socket documentation for more
1182 information about this function.
1183
1184 :param class_server sv: A :ref:`server_class` which indicates the manipulated
1185 server.
1186
1187.. js:function:: Server.agent_force_up(sv)
1188
1189 Force agent check up. See the management socket documentation for more
1190 information about this function.
1191
1192 :param class_server sv: A :ref:`server_class` which indicates the manipulated
1193 server.
1194
1195.. js:function:: Server.agent_force_down(sv)
1196
1197 Force agent check down. See the management socket documentation for more
1198 information about this function.
1199
1200 :param class_server sv: A :ref:`server_class` which indicates the manipulated
1201 server.
1202
Thierry Fournierff480422016-02-25 08:36:46 +01001203.. _listener_class:
1204
1205Listener class
1206==============
1207
1208.. js:function:: Listener.get_stats(ls)
1209
1210 Returns server statistics.
1211
1212 :param class_listener ls: A :ref:`listener_class` which indicates the
1213 manipulated listener.
Patrick Hemmerc6a1d712018-05-01 21:30:41 -04001214 :returns: a key/value table containing stats
Thierry Fournierff480422016-02-25 08:36:46 +01001215
Thierry Fournier1de16592016-01-27 09:49:07 +01001216.. _concat_class:
1217
1218Concat class
1219============
1220
1221.. js:class:: Concat
1222
1223 This class provides a fast way for string concatenation. The way using native
1224 Lua concatenation like the code below is slow for some reasons.
1225
1226.. code-block:: lua
1227
1228 str = "string1"
1229 str = str .. ", string2"
1230 str = str .. ", string3"
1231..
1232
1233 For each concatenation, Lua:
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02001234 - allocates memory for the result,
1235 - catenates the two string copying the strings in the new memory block,
1236 - frees the old memory block containing the string which is no longer used.
1237
Thierry Fournier1de16592016-01-27 09:49:07 +01001238 This process does many memory move, allocation and free. In addition, the
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02001239 memory is not really freed, it is just marked as unused and waits for the
Thierry Fournier1de16592016-01-27 09:49:07 +01001240 garbage collector.
1241
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02001242 The Concat class provides an alternative way to concatenate strings. It uses
Thierry Fournier1de16592016-01-27 09:49:07 +01001243 the internal Lua mechanism (it does not allocate memory), but it doesn't copy
1244 the data more than once.
1245
1246 On my computer, the following loops spends 0.2s for the Concat method and
1247 18.5s for the pure Lua implementation. So, the Concat class is about 1000x
1248 faster than the embedded solution.
1249
1250.. code-block:: lua
1251
1252 for j = 1, 100 do
1253 c = core.concat()
1254 for i = 1, 20000 do
1255 c:add("#####")
1256 end
1257 end
1258..
1259
1260.. code-block:: lua
1261
1262 for j = 1, 100 do
1263 c = ""
1264 for i = 1, 20000 do
1265 c = c .. "#####"
1266 end
1267 end
1268..
1269
1270.. js:function:: Concat.add(concat, string)
1271
1272 This function adds a string to the current concatenated string.
1273
1274 :param class_concat concat: A :ref:`concat_class` which contains the currently
Ilya Shipitsin2075ca82020-03-06 23:22:22 +05001275 built string.
Bertrand Jacquin874a35c2018-09-10 21:26:07 +01001276 :param string string: A new string to concatenate to the current built
Thierry Fournier1de16592016-01-27 09:49:07 +01001277 string.
1278
1279.. js:function:: Concat.dump(concat)
1280
Bertrand Jacquin874a35c2018-09-10 21:26:07 +01001281 This function returns the concatenated string.
Thierry Fournier1de16592016-01-27 09:49:07 +01001282
1283 :param class_concat concat: A :ref:`concat_class` which contains the currently
Ilya Shipitsin2075ca82020-03-06 23:22:22 +05001284 built string.
Thierry Fournier1de16592016-01-27 09:49:07 +01001285 :returns: the concatenated string
1286
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001287.. _fetches_class:
1288
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001289Fetches class
1290=============
1291
1292.. js:class:: Fetches
1293
1294 This class contains a lot of internal HAProxy sample fetches. See the
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02001295 HAProxy "configuration.txt" documentation for more information.
1296 (chapters 7.3.2 to 7.3.6)
Thierry FOURNIER2e4893c2015-03-18 13:37:27 +01001297
Christopher Faulet1e9b1b62021-08-11 10:14:30 +02001298 .. warning::
1299 some sample fetches are not available in some context. These limitations
1300 are specified in this documentation when they're useful.
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001301
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01001302 :see: :js:attr:`TXN.f`
1303 :see: :js:attr:`TXN.sf`
Thierry FOURNIER2e4893c2015-03-18 13:37:27 +01001304
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02001305 Fetches are useful to:
Thierry FOURNIER2e4893c2015-03-18 13:37:27 +01001306
1307 * get system time,
1308 * get environment variable,
1309 * get random numbers,
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02001310 * know backend status like the number of users in queue or the number of
Thierry FOURNIER2e4893c2015-03-18 13:37:27 +01001311 connections established,
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02001312 * get client information like ip source or destination,
Thierry FOURNIER2e4893c2015-03-18 13:37:27 +01001313 * deal with stick tables,
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02001314 * fetch established SSL information,
1315 * fetch HTTP information like headers or method.
Thierry FOURNIER2e4893c2015-03-18 13:37:27 +01001316
1317.. code-block:: lua
1318
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001319 function action(txn)
1320 -- Get source IP
1321 local clientip = txn.f:src()
1322 end
Thierry FOURNIER2e4893c2015-03-18 13:37:27 +01001323..
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001324
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001325.. _converters_class:
1326
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001327Converters class
1328================
1329
1330.. js:class:: Converters
1331
1332 This class contains a lot of internal HAProxy sample converters. See the
Thierry FOURNIER2e4893c2015-03-18 13:37:27 +01001333 HAProxy documentation "configuration.txt" for more information about her
1334 usage. Its the chapter 7.3.1.
1335
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01001336 :see: :js:attr:`TXN.c`
1337 :see: :js:attr:`TXN.sc`
Thierry FOURNIER2e4893c2015-03-18 13:37:27 +01001338
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02001339 Converters provides stateful transformation. They are useful to:
Thierry FOURNIER2e4893c2015-03-18 13:37:27 +01001340
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02001341 * convert input to base64,
1342 * apply hash on input string (djb2, crc32, sdbm, wt6),
Thierry FOURNIER2e4893c2015-03-18 13:37:27 +01001343 * format date,
1344 * json escape,
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02001345 * extract preferred language comparing two lists,
Thierry FOURNIER2e4893c2015-03-18 13:37:27 +01001346 * turn to lower or upper chars,
1347 * deal with stick tables.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001348
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001349.. _channel_class:
1350
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001351Channel class
1352=============
1353
1354.. js:class:: Channel
1355
Christopher Faulet5a2c6612021-08-15 20:35:25 +02001356 **context**: action, sample-fetch, convert, filter
1357
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001358 HAProxy uses two buffers for the processing of the requests. The first one is
1359 used with the request data (from the client to the server) and the second is
1360 used for the response data (from the server to the client).
1361
1362 Each buffer contains two types of data. The first type is the incoming data
1363 waiting for a processing. The second part is the outgoing data already
1364 processed. Usually, the incoming data is processed, after it is tagged as
1365 outgoing data, and finally it is sent. The following functions provides tools
1366 for manipulating these data in a buffer.
1367
1368 The following diagram shows where the channel class function are applied.
1369
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001370 .. image:: _static/channel.png
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001371
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001372 .. warning::
1373 It is not possible to read from the response in request action, and it is
Boyang Li60cfe8b2022-05-10 18:11:00 +00001374 not possible to read from the request channel in response action.
Christopher Faulet09530392021-06-14 11:43:18 +02001375
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001376 .. warning::
1377 It is forbidden to alter the Channels buffer from HTTP contexts. So only
1378 :js:func:`Channel.input`, :js:func:`Channel.output`,
1379 :js:func:`Channel.may_recv`, :js:func:`Channel.is_full` and
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02001380 :js:func:`Channel.is_resp` can be called from a HTTP context.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001381
Christopher Faulet5a2c6612021-08-15 20:35:25 +02001382 All the functions provided by this class are available in the
1383 **sample-fetches**, **actions** and **filters** contexts. For **filters**,
1384 incoming data (offset and length) are relative to the filter. Some functions
Boyang Li60cfe8b2022-05-10 18:11:00 +00001385 may yield, but only for **actions**. Yield is not possible for
Christopher Faulet5a2c6612021-08-15 20:35:25 +02001386 **sample-fetches**, **converters** and **filters**.
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001387
1388.. js:function:: Channel.append(channel, string)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001389
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001390 This function copies the string **string** at the end of incoming data of the
1391 channel buffer. The function returns the copied length on success or -1 if
1392 data cannot be copied.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001393
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001394 Same that :js:func:`Channel.insert(channel, string, channel:input())`.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001395
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001396 :param class_channel channel: The manipulated Channel.
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02001397 :param string string: The data to copy at the end of incoming data.
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001398 :returns: an integer containing the amount of bytes copied or -1.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001399
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001400.. js:function:: Channel.data(channel [, offset [, length]])
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001401
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001402 This function returns **length** bytes of incoming data from the channel
1403 buffer, starting at the offset **offset**. The data are not removed from the
1404 buffer.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001405
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001406 By default, if no length is provided, all incoming data found, starting at the
1407 given offset, are returned. If **length** is set to -1, the function tries to
Christopher Faulet5a2c6612021-08-15 20:35:25 +02001408 retrieve a maximum of data and, if called by an action, it yields if
1409 necessary. It also waits for more data if the requested length exceeds the
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02001410 available amount of incoming data. Not providing an offset is the same as
Ilya Shipitsinff0f2782021-08-22 22:18:07 +05001411 setting it to 0. A positive offset is relative to the beginning of incoming
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02001412 data of the channel buffer while negative offset is relative to the end.
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001413
1414 If there is no incoming data and the channel can't receive more data, a 'nil'
1415 value is returned.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001416
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001417 :param class_channel channel: The manipulated Channel.
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001418 :param integer offset: *optional* The offset in incoming data to start to get
1419 data. 0 by default. May be negative to be relative to
1420 the end of incoming data.
1421 :param integer length: *optional* The expected length of data to retrieve. All
1422 incoming data by default. May be set to -1 to get a
1423 maximum of data.
1424 :returns: a string containing the data found or nil.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001425
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001426.. js:function:: Channel.forward(channel, length)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001427
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001428 This function forwards **length** bytes of data from the channel buffer. If
Christopher Faulet5a2c6612021-08-15 20:35:25 +02001429 the requested length exceeds the available amount of incoming data, and if
1430 called by an action, the function yields, waiting for more data to forward. It
1431 returns the amount of data forwarded.
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001432
1433 :param class_channel channel: The manipulated Channel.
1434 :param integer int: The amount of data to forward.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001435
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001436.. js:function:: Channel.input(channel)
1437
Christopher Faulet5a2c6612021-08-15 20:35:25 +02001438 This function returns the length of incoming data in the channel buffer. When
1439 called by a filter, this value is relative to the filter.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001440
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001441 :param class_channel channel: The manipulated Channel.
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001442 :returns: an integer containing the amount of available bytes.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001443
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001444.. js:function:: Channel.insert(channel, string [, offset])
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001445
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001446 This function copies the string **string** at the offset **offset** in
1447 incoming data of the channel buffer. The function returns the copied length on
1448 success or -1 if data cannot be copied.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001449
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001450 By default, if no offset is provided, the string is copied in front of
Ilya Shipitsinff0f2782021-08-22 22:18:07 +05001451 incoming data. A positive offset is relative to the beginning of incoming data
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001452 of the channel buffer while negative offset is relative to their end.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001453
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001454 :param class_channel channel: The manipulated Channel.
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02001455 :param string string: The data to copy into incoming data.
1456 :param integer offset: *optional* The offset in incoming data where to copy
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001457 data. 0 by default. May be negative to be relative to
1458 the end of incoming data.
Pieter Baauw4d7f7662015-11-08 16:38:08 +01001459 :returns: an integer containing the amount of bytes copied or -1.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001460
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001461.. js:function:: Channel.is_full(channel)
1462
1463 This function returns true if the channel buffer is full.
1464
Christopher Faulet5a2c6612021-08-15 20:35:25 +02001465 :param class_channel channel: The manipulated Channel.
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001466 :returns: a boolean
1467
1468.. js:function:: Channel.is_resp(channel)
1469
1470 This function returns true if the channel is the response one.
1471
Christopher Faulet5a2c6612021-08-15 20:35:25 +02001472 :param class_channel channel: The manipulated Channel.
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001473 :returns: a boolean
1474
1475.. js:function:: Channel.line(channel [, offset [, length]])
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001476
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001477 This function parses **length** bytes of incoming data of the channel buffer,
1478 starting at offset **offset**, and returns the first line found, including the
Christopher Faulet5a2c6612021-08-15 20:35:25 +02001479 '\\n'. The data are not removed from the buffer. If no line is found, all data
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001480 are returned.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001481
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001482 By default, if no length is provided, all incoming data, starting at the given
1483 offset, are evaluated. If **length** is set to -1, the function tries to
Christopher Faulet5a2c6612021-08-15 20:35:25 +02001484 retrieve a maximum of data and, if called by an action, yields if
1485 necessary. It also waits for more data if the requested length exceeds the
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02001486 available amount of incoming data. Not providing an offset is the same as
Ilya Shipitsinff0f2782021-08-22 22:18:07 +05001487 setting it to 0. A positive offset is relative to the beginning of incoming
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02001488 data of the channel buffer while negative offset is relative to the end.
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001489
1490 If there is no incoming data and the channel can't receive more data, a 'nil'
1491 value is returned.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001492
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001493 :param class_channel channel: The manipulated Channel.
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02001494 :param integer offset: *optional* The offset in incoming data to start to
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001495 parse data. 0 by default. May be negative to be
1496 relative to the end of incoming data.
1497 :param integer length: *optional* The length of data to parse. All incoming
1498 data by default. May be set to -1 to get a maximum of
1499 data.
1500 :returns: a string containing the line found or nil.
1501
1502.. js:function:: Channel.may_recv(channel)
1503
1504 This function returns true if the channel may still receive data.
1505
Christopher Faulet5a2c6612021-08-15 20:35:25 +02001506 :param class_channel channel: The manipulated Channel.
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001507 :returns: a boolean
1508
1509.. js:function:: Channel.output(channel)
1510
Christopher Faulet5a2c6612021-08-15 20:35:25 +02001511 This function returns the length of outgoing data of the channel buffer. When
1512 called by a filter, this value is relative to the filter.
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001513
1514 :param class_channel channel: The manipulated Channel.
1515 :returns: an integer containing the amount of available bytes.
1516
1517.. js:function:: Channel.prepend(channel, string)
1518
1519 This function copies the string **string** in front of incoming data of the
1520 channel buffer. The function returns the copied length on success or -1 if
1521 data cannot be copied.
1522
1523 Same that :js:func:`Channel.insert(channel, string, 0)`.
1524
1525 :param class_channel channel: The manipulated Channel.
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02001526 :param string string: The data to copy in front of incoming data.
Pieter Baauw4d7f7662015-11-08 16:38:08 +01001527 :returns: an integer containing the amount of bytes copied or -1.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001528
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001529.. js:function:: Channel.remove(channel [, offset [, length]])
1530
1531 This function removes **length** bytes of incoming data of the channel buffer,
1532 starting at offset **offset**. This function returns number of bytes removed
1533 on success.
1534
1535 By default, if no length is provided, all incoming data, starting at the given
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02001536 offset, are removed. Not providing an offset is the same as setting it
Ilya Shipitsinff0f2782021-08-22 22:18:07 +05001537 to 0. A positive offset is relative to the beginning of incoming data of the
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02001538 channel buffer while negative offset is relative to the end.
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001539
1540 :param class_channel channel: The manipulated Channel.
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02001541 :param integer offset: *optional* The offset in incoming data where to start
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001542 to remove data. 0 by default. May be negative to
1543 be relative to the end of incoming data.
1544 :param integer length: *optional* The length of data to remove. All incoming
1545 data by default.
1546 :returns: an integer containing the amount of bytes removed.
1547
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001548.. js:function:: Channel.send(channel, string)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001549
Christopher Faulet5a2c6612021-08-15 20:35:25 +02001550 This function requires immediate send of the string **string**. It means the
Ilya Shipitsinff0f2782021-08-22 22:18:07 +05001551 string is copied at the beginning of incoming data of the channel buffer and
Christopher Faulet5a2c6612021-08-15 20:35:25 +02001552 immediately forwarded. Unless if the connection is close, and if called by an
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02001553 action, this function yields to copy and forward all the string.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001554
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001555 :param class_channel channel: The manipulated Channel.
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001556 :param string string: The data to send.
Pieter Baauw4d7f7662015-11-08 16:38:08 +01001557 :returns: an integer containing the amount of bytes copied or -1.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001558
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001559.. js:function:: Channel.set(channel, string [, offset [, length]])
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001560
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02001561 This function replaces **length** bytes of incoming data of the channel buffer,
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001562 starting at offset **offset**, by the string **string**. The function returns
1563 the copied length on success or -1 if data cannot be copied.
1564
1565 By default, if no length is provided, all incoming data, starting at the given
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02001566 offset, are replaced. Not providing an offset is the same as setting it
Ilya Shipitsinff0f2782021-08-22 22:18:07 +05001567 to 0. A positive offset is relative to the beginning of incoming data of the
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02001568 channel buffer while negative offset is relative to the end.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001569
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001570 :param class_channel channel: The manipulated Channel.
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02001571 :param string string: The data to copy into incoming data.
1572 :param integer offset: *optional* The offset in incoming data where to start
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001573 the data replacement. 0 by default. May be negative to
1574 be relative to the end of incoming data.
1575 :param integer length: *optional* The length of data to replace. All incoming
1576 data by default.
1577 :returns: an integer containing the amount of bytes copied or -1.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001578
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001579.. js:function:: Channel.dup(channel)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001580
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001581 **DEPRECATED**
1582
1583 This function returns all incoming data found in the channel buffer. The data
Boyang Li60cfe8b2022-05-10 18:11:00 +00001584 are not removed from the buffer and can be reprocessed later.
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001585
1586 If there is no incoming data and the channel can't receive more data, a 'nil'
1587 value is returned.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001588
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001589 :param class_channel channel: The manipulated Channel.
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001590 :returns: a string containing all data found or nil.
1591
1592 .. warning::
1593 This function is deprecated. :js:func:`Channel.data()` must be used
1594 instead.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001595
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001596.. js:function:: Channel.get(channel)
1597
1598 **DEPRECATED**
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001599
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001600 This function returns all incoming data found in the channel buffer and remove
1601 them from the buffer.
1602
1603 If there is no incoming data and the channel can't receive more data, a 'nil'
1604 value is returned.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001605
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001606 :param class_channel channel: The manipulated Channel.
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001607 :returns: a string containing all the data found or nil.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001608
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001609 .. warning::
1610 This function is deprecated. :js:func:`Channel.data()` must be used to
1611 retrieve data followed by a call to :js:func:`Channel:remove()` to remove
1612 data.
Thierry FOURNIER / OZON.IO65192f32016-11-07 15:28:40 +01001613
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001614 .. code-block:: lua
Thierry FOURNIER / OZON.IO65192f32016-11-07 15:28:40 +01001615
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001616 local data = chn:data()
1617 chn:remove(0, data:len())
1618
1619 ..
1620
1621.. js:function:: Channel.getline(channel)
1622
1623 **DEPRECATED**
1624
1625 This function returns the first line found in incoming data of the channel
Christopher Faulet5a2c6612021-08-15 20:35:25 +02001626 buffer, including the '\\n'. The returned data are removed from the buffer. If
1627 no line is found, and if called by an action, this function yields to wait for
1628 more data, except if the channel can't receive more data. In this case all
1629 data are returned.
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001630
1631 If there is no incoming data and the channel can't receive more data, a 'nil'
1632 value is returned.
1633
1634 :param class_channel channel: The manipulated Channel.
1635 :returns: a string containing the line found or nil.
1636
1637 .. warning::
Boyang Li60cfe8b2022-05-10 18:11:00 +00001638 This function is deprecated. :js:func:`Channel.line()` must be used to
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001639 retrieve a line followed by a call to :js:func:`Channel:remove()` to remove
1640 data.
1641
1642 .. code-block:: lua
1643
1644 local line = chn:line(0, -1)
1645 chn:remove(0, line:len())
1646
1647 ..
1648
1649.. js:function:: Channel.get_in_len(channel)
1650
Boyang Li60cfe8b2022-05-10 18:11:00 +00001651 **DEPRECATED**
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001652
Christopher Faulet5a2c6612021-08-15 20:35:25 +02001653 This function returns the length of the input part of the buffer. When called
1654 by a filter, this value is relative to the filter.
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001655
1656 :param class_channel channel: The manipulated Channel.
1657 :returns: an integer containing the amount of available bytes.
1658
1659 .. warning::
1660 This function is deprecated. :js:func:`Channel.input()` must be used
1661 instead.
1662
1663.. js:function:: Channel.get_out_len(channel)
1664
Boyang Li60cfe8b2022-05-10 18:11:00 +00001665 **DEPRECATED**
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001666
Christopher Faulet5a2c6612021-08-15 20:35:25 +02001667 This function returns the length of the output part of the buffer. When called
1668 by a filter, this value is relative to the filter.
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001669
1670 :param class_channel channel: The manipulated Channel.
1671 :returns: an integer containing the amount of available bytes.
1672
1673 .. warning::
1674 This function is deprecated. :js:func:`Channel.output()` must be used
1675 instead.
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001676
1677.. _http_class:
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001678
1679HTTP class
1680==========
1681
1682.. js:class:: HTTP
1683
1684 This class contain all the HTTP manipulation functions.
1685
Pieter Baauw386a1272015-08-16 15:26:24 +02001686.. js:function:: HTTP.req_get_headers(http)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001687
Patrick Hemmerc6a1d712018-05-01 21:30:41 -04001688 Returns a table containing all the request headers.
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001689
1690 :param class_http http: The related http object.
Patrick Hemmerc6a1d712018-05-01 21:30:41 -04001691 :returns: table of headers.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01001692 :see: :js:func:`HTTP.res_get_headers`
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001693
Patrick Hemmerc6a1d712018-05-01 21:30:41 -04001694 This is the form of the returned table:
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001695
1696.. code-block:: lua
1697
1698 HTTP:req_get_headers()['<header-name>'][<header-index>] = "<header-value>"
1699
1700 local hdr = HTTP:req_get_headers()
1701 hdr["host"][0] = "www.test.com"
1702 hdr["accept"][0] = "audio/basic q=1"
1703 hdr["accept"][1] = "audio/*, q=0.2"
1704 hdr["accept"][2] = "*/*, q=0.1"
1705..
1706
Pieter Baauw386a1272015-08-16 15:26:24 +02001707.. js:function:: HTTP.res_get_headers(http)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001708
Patrick Hemmerc6a1d712018-05-01 21:30:41 -04001709 Returns a table containing all the response headers.
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001710
1711 :param class_http http: The related http object.
Patrick Hemmerc6a1d712018-05-01 21:30:41 -04001712 :returns: table of headers.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01001713 :see: :js:func:`HTTP.req_get_headers`
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001714
Patrick Hemmerc6a1d712018-05-01 21:30:41 -04001715 This is the form of the returned table:
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001716
1717.. code-block:: lua
1718
1719 HTTP:res_get_headers()['<header-name>'][<header-index>] = "<header-value>"
1720
1721 local hdr = HTTP:req_get_headers()
1722 hdr["host"][0] = "www.test.com"
1723 hdr["accept"][0] = "audio/basic q=1"
1724 hdr["accept"][1] = "audio/*, q=0.2"
1725 hdr["accept"][2] = "*.*, q=0.1"
1726..
1727
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001728.. js:function:: HTTP.req_add_header(http, name, value)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001729
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02001730 Appends a HTTP header field in the request whose name is
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001731 specified in "name" and whose value is defined in "value".
1732
1733 :param class_http http: The related http object.
1734 :param string name: The header name.
1735 :param string value: The header value.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01001736 :see: :js:func:`HTTP.res_add_header`
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001737
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001738.. js:function:: HTTP.res_add_header(http, name, value)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001739
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02001740 Appends a HTTP header field in the response whose name is
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001741 specified in "name" and whose value is defined in "value".
1742
1743 :param class_http http: The related http object.
1744 :param string name: The header name.
1745 :param string value: The header value.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01001746 :see: :js:func:`HTTP.req_add_header`
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001747
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001748.. js:function:: HTTP.req_del_header(http, name)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001749
1750 Removes all HTTP header fields in the request whose name is
1751 specified in "name".
1752
1753 :param class_http http: The related http object.
1754 :param string name: The header name.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01001755 :see: :js:func:`HTTP.res_del_header`
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001756
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001757.. js:function:: HTTP.res_del_header(http, name)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001758
1759 Removes all HTTP header fields in the response whose name is
1760 specified in "name".
1761
1762 :param class_http http: The related http object.
1763 :param string name: The header name.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01001764 :see: :js:func:`HTTP.req_del_header`
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001765
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001766.. js:function:: HTTP.req_set_header(http, name, value)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001767
Bertrand Jacquin874a35c2018-09-10 21:26:07 +01001768 This variable replace all occurrence of all header "name", by only
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001769 one containing the "value".
1770
1771 :param class_http http: The related http object.
1772 :param string name: The header name.
1773 :param string value: The header value.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01001774 :see: :js:func:`HTTP.res_set_header`
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001775
Bertrand Jacquin874a35c2018-09-10 21:26:07 +01001776 This function does the same work as the following code:
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001777
1778.. code-block:: lua
1779
1780 function fcn(txn)
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001781 TXN.http:req_del_header("header")
1782 TXN.http:req_add_header("header", "value")
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001783 end
1784..
1785
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001786.. js:function:: HTTP.res_set_header(http, name, value)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001787
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02001788 This function replaces all occurrence of all header "name", by only
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001789 one containing the "value".
1790
1791 :param class_http http: The related http object.
1792 :param string name: The header name.
1793 :param string value: The header value.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01001794 :see: :js:func:`HTTP.req_rep_header()`
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001795
Pieter Baauw386a1272015-08-16 15:26:24 +02001796.. js:function:: HTTP.req_rep_header(http, name, regex, replace)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001797
1798 Matches the regular expression in all occurrences of header field "name"
1799 according to "regex", and replaces them with the "replace" argument. The
1800 replacement value can contain back references like \1, \2, ... This
1801 function works with the request.
1802
1803 :param class_http http: The related http object.
1804 :param string name: The header name.
1805 :param string regex: The match regular expression.
1806 :param string replace: The replacement value.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01001807 :see: :js:func:`HTTP.res_rep_header()`
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001808
Pieter Baauw386a1272015-08-16 15:26:24 +02001809.. js:function:: HTTP.res_rep_header(http, name, regex, string)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001810
1811 Matches the regular expression in all occurrences of header field "name"
1812 according to "regex", and replaces them with the "replace" argument. The
1813 replacement value can contain back references like \1, \2, ... This
1814 function works with the request.
1815
1816 :param class_http http: The related http object.
1817 :param string name: The header name.
1818 :param string regex: The match regular expression.
1819 :param string replace: The replacement value.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01001820 :see: :js:func:`HTTP.req_rep_header()`
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001821
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001822.. js:function:: HTTP.req_set_method(http, method)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001823
1824 Rewrites the request method with the parameter "method".
1825
1826 :param class_http http: The related http object.
1827 :param string method: The new method.
1828
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001829.. js:function:: HTTP.req_set_path(http, path)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001830
1831 Rewrites the request path with the "path" parameter.
1832
1833 :param class_http http: The related http object.
1834 :param string path: The new path.
1835
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001836.. js:function:: HTTP.req_set_query(http, query)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001837
1838 Rewrites the request's query string which appears after the first question
1839 mark ("?") with the parameter "query".
1840
1841 :param class_http http: The related http object.
1842 :param string query: The new query.
1843
Thierry FOURNIER0d79cf62015-08-26 14:20:58 +02001844.. js:function:: HTTP.req_set_uri(http, uri)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001845
1846 Rewrites the request URI with the parameter "uri".
1847
1848 :param class_http http: The related http object.
1849 :param string uri: The new uri.
1850
Robin H. Johnson52f5db22017-01-01 13:10:52 -08001851.. js:function:: HTTP.res_set_status(http, status [, reason])
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +02001852
Robin H. Johnson52f5db22017-01-01 13:10:52 -08001853 Rewrites the response status code with the parameter "code".
1854
1855 If no custom reason is provided, it will be generated from the status.
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +02001856
1857 :param class_http http: The related http object.
1858 :param integer status: The new response status code.
Robin H. Johnson52f5db22017-01-01 13:10:52 -08001859 :param string reason: The new response reason (optional).
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +02001860
William Lallemand00a15022021-11-19 16:02:44 +01001861.. _httpclient_class:
1862
1863HTTPClient class
1864================
1865
1866.. js:class:: HTTPClient
1867
1868 The httpclient class allows issue of outbound HTTP requests through a simple
1869 API without the knowledge of HAProxy internals.
1870
1871.. js:function:: HTTPClient.get(httpclient, request)
1872.. js:function:: HTTPClient.head(httpclient, request)
1873.. js:function:: HTTPClient.put(httpclient, request)
1874.. js:function:: HTTPClient.post(httpclient, request)
1875.. js:function:: HTTPClient.delete(httpclient, request)
1876
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02001877 Send a HTTP request and wait for a response. GET, HEAD PUT, POST and DELETE methods can be used.
1878 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 +01001879
1880
1881 :param class httpclient: Is the manipulated HTTPClient.
1882 :param table request: Is a table containing the parameters of the request that will be send.
1883 :param string request.url: Is a mandatory parameter for the request that contains the URL.
1884 :param string request.body: Is an optional parameter for the request that contains the body to send.
1885 :param table request.headers: Is an optional parameter for the request that contains the headers to send.
William Lallemand18340302022-02-23 15:57:45 +01001886 :param string request.dst: Is an optional parameter for the destination in haproxy address format.
William Lallemandb4a4ef62022-02-23 14:18:16 +01001887 :param integer request.timeout: Optional timeout parameter, set a "timeout server" on the connections.
William Lallemand00a15022021-11-19 16:02:44 +01001888 :returns: Lua table containing the response
1889
1890
1891.. code-block:: lua
1892
1893 local httpclient = core.httpclient()
William Lallemand4f4f2b72022-02-17 20:00:23 +01001894 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 +01001895
1896..
1897
1898.. code-block:: lua
1899
1900 response = {
1901 status = 400,
1902 reason = "Bad request",
1903 headers = {
1904 ["content-type"] = { "text/html" },
1905 ["cache-control"] = { "no-cache", "no-store" },
1906 },
William Lallemand4f4f2b72022-02-17 20:00:23 +01001907 body = "<html><body><h1>invalid request<h1></body></html>",
William Lallemand00a15022021-11-19 16:02:44 +01001908 }
1909..
1910
1911
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001912.. _txn_class:
1913
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001914TXN class
1915=========
1916
1917.. js:class:: TXN
1918
1919 The txn class contain all the functions relative to the http or tcp
1920 transaction (Note than a tcp stream is the same than a tcp transaction, but
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02001921 a HTTP transaction is not the same than a tcp stream).
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001922
1923 The usage of this class permits to retrieve data from the requests, alter it
1924 and forward it.
1925
1926 All the functions provided by this class are available in the context
Christopher Faulet5a2c6612021-08-15 20:35:25 +02001927 **sample-fetches**, **actions** and **filters**.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001928
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001929.. js:attribute:: TXN.c
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001930
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001931 :returns: An :ref:`converters_class`.
1932
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001933 This attribute contains a Converters class object.
1934
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001935.. js:attribute:: TXN.sc
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001936
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001937 :returns: An :ref:`converters_class`.
1938
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001939 This attribute contains a Converters class object. The functions of
1940 this object returns always a string.
1941
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001942.. js:attribute:: TXN.f
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001943
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001944 :returns: An :ref:`fetches_class`.
1945
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001946 This attribute contains a Fetches class object.
1947
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001948.. js:attribute:: TXN.sf
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001949
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001950 :returns: An :ref:`fetches_class`.
1951
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001952 This attribute contains a Fetches class object. The functions of
1953 this object returns always a string.
1954
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001955.. js:attribute:: TXN.req
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001956
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001957 :returns: An :ref:`channel_class`.
1958
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001959 This attribute contains a channel class object for the request buffer.
1960
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001961.. js:attribute:: TXN.res
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001962
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001963 :returns: An :ref:`channel_class`.
1964
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001965 This attribute contains a channel class object for the response buffer.
1966
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001967.. js:attribute:: TXN.http
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001968
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001969 :returns: An :ref:`http_class`.
1970
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02001971 This attribute contains a HTTP class object. It is available only if the
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001972 proxy has the "mode http" enabled.
1973
Christopher Faulet5a2c6612021-08-15 20:35:25 +02001974.. js:attribute:: TXN.http_req
1975
1976 :returns: An :ref:`httpmessage_class`.
1977
1978 This attribute contains the request HTTPMessage class object. It is available
1979 only if the proxy has the "mode http" enabled and only in the **filters**
1980 context.
1981
1982.. js:attribute:: TXN.http_res
1983
1984 :returns: An :ref:`httpmessage_class`.
1985
1986 This attribute contains the response HTTPMessage class object. It is available
1987 only if the proxy has the "mode http" enabled and only in the **filters**
1988 context.
1989
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01001990.. js:function:: TXN.log(TXN, loglevel, msg)
1991
1992 This function sends a log. The log is sent, according with the HAProxy
1993 configuration file, on the default syslog server if it is configured and on
1994 the stderr if it is allowed.
1995
1996 :param class_txn txn: The class txn object containing the data.
Bertrand Jacquin874a35c2018-09-10 21:26:07 +01001997 :param integer loglevel: Is the log level associated with the message. It is a
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01001998 number between 0 and 7.
1999 :param string msg: The log content.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01002000 :see: :js:attr:`core.emerg`, :js:attr:`core.alert`, :js:attr:`core.crit`,
2001 :js:attr:`core.err`, :js:attr:`core.warning`, :js:attr:`core.notice`,
2002 :js:attr:`core.info`, :js:attr:`core.debug` (log level definitions)
2003 :see: :js:func:`TXN.deflog`
2004 :see: :js:func:`TXN.Debug`
2005 :see: :js:func:`TXN.Info`
2006 :see: :js:func:`TXN.Warning`
2007 :see: :js:func:`TXN.Alert`
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01002008
2009.. js:function:: TXN.deflog(TXN, msg)
2010
Bertrand Jacquin874a35c2018-09-10 21:26:07 +01002011 Sends a log line with the default loglevel for the proxy associated with the
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01002012 transaction.
2013
2014 :param class_txn txn: The class txn object containing the data.
2015 :param string msg: The log content.
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02002016 :see: :js:func:`TXN.log`
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01002017
2018.. js:function:: TXN.Debug(txn, msg)
2019
2020 :param class_txn txn: The class txn object containing the data.
2021 :param string msg: The log content.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01002022 :see: :js:func:`TXN.log`
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01002023
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02002024 Does the same job as:
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01002025
2026.. code-block:: lua
2027
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002028 function Debug(txn, msg)
2029 TXN.log(txn, core.debug, msg)
2030 end
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01002031..
2032
2033.. js:function:: TXN.Info(txn, msg)
2034
2035 :param class_txn txn: The class txn object containing the data.
2036 :param string msg: The log content.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01002037 :see: :js:func:`TXN.log`
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01002038
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02002039 Does the same job as:
2040
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01002041.. code-block:: lua
2042
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02002043 function Info(txn, msg)
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002044 TXN.log(txn, core.info, msg)
2045 end
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01002046..
2047
2048.. js:function:: TXN.Warning(txn, msg)
2049
2050 :param class_txn txn: The class txn object containing the data.
2051 :param string msg: The log content.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01002052 :see: :js:func:`TXN.log`
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01002053
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02002054 Does the same job as:
2055
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01002056.. code-block:: lua
2057
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02002058 function Warning(txn, msg)
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002059 TXN.log(txn, core.warning, msg)
2060 end
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01002061..
2062
2063.. js:function:: TXN.Alert(txn, msg)
2064
2065 :param class_txn txn: The class txn object containing the data.
2066 :param string msg: The log content.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01002067 :see: :js:func:`TXN.log`
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01002068
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02002069 Does the same job as:
2070
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01002071.. code-block:: lua
2072
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02002073 function Alert(txn, msg)
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002074 TXN.log(txn, core.alert, msg)
2075 end
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01002076..
2077
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002078.. js:function:: TXN.get_priv(txn)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002079
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002080 Return Lua data stored in the current transaction (with the `TXN.set_priv()`)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002081 function. If no data are stored, it returns a nil value.
2082
2083 :param class_txn txn: The class txn object containing the data.
Bertrand Jacquin874a35c2018-09-10 21:26:07 +01002084 :returns: the opaque data previously stored, or nil if nothing is
2085 available.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002086
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002087.. js:function:: TXN.set_priv(txn, data)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002088
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02002089 Store any data in the current HAProxy transaction. This action replaces the
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002090 old stored data.
2091
2092 :param class_txn txn: The class txn object containing the data.
2093 :param opaque data: The data which is stored in the transaction.
2094
Tim Duesterhus4e172c92020-05-19 13:49:42 +02002095.. js:function:: TXN.set_var(TXN, var, value[, ifexist])
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +02002096
David Carlier61fdf8b2015-10-02 11:59:38 +01002097 Converts a Lua type in a HAProxy type and store it in a variable <var>.
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +02002098
2099 :param class_txn txn: The class txn object containing the data.
2100 :param string var: The variable name according with the HAProxy variable syntax.
Thierry FOURNIER / OZON.IOb210bcc2016-12-12 16:24:16 +01002101 :param type value: The value associated to the variable. The type can be string or
2102 integer.
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02002103 :param boolean ifexist: If this parameter is set to true the variable
Tim Duesterhus4e172c92020-05-19 13:49:42 +02002104 will only be set if it was defined elsewhere (i.e. used
Willy Tarreau7978c5c2021-09-07 14:24:07 +02002105 within the configuration). For global variables (using the
2106 "proc" scope), they will only be updated and never created.
2107 It is highly recommended to always set this to true.
Christopher Faulet85d79c92016-11-09 16:54:56 +01002108
2109.. js:function:: TXN.unset_var(TXN, var)
2110
2111 Unset the variable <var>.
2112
2113 :param class_txn txn: The class txn object containing the data.
2114 :param string var: The variable name according with the HAProxy variable syntax.
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +02002115
2116.. js:function:: TXN.get_var(TXN, var)
2117
2118 Returns data stored in the variable <var> converter in Lua type.
2119
2120 :param class_txn txn: The class txn object containing the data.
2121 :param string var: The variable name according with the HAProxy variable syntax.
2122
Christopher Faulet700d9e82020-01-31 12:21:52 +01002123.. js:function:: TXN.reply([reply])
2124
2125 Return a new reply object
2126
2127 :param table reply: A table containing info to initialize the reply fields.
2128 :returns: A :ref:`reply_class` object.
2129
2130 The table used to initialized the reply object may contain following entries :
2131
2132 * status : The reply status code. the code 200 is used by default.
2133 * reason : The reply reason. The reason corresponding to the status code is
2134 used by default.
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02002135 * headers : A list of headers, indexed by header name. Empty by default. For
Christopher Faulet700d9e82020-01-31 12:21:52 +01002136 a given name, multiple values are possible, stored in an ordered list.
2137 * body : The reply body, empty by default.
2138
2139.. code-block:: lua
2140
2141 local reply = txn:reply{
2142 status = 400,
2143 reason = "Bad request",
2144 headers = {
2145 ["content-type"] = { "text/html" },
2146 ["cache-control"] = {"no-cache", "no-store" }
2147 },
2148 body = "<html><body><h1>invalid request<h1></body></html>"
2149 }
2150..
2151 :see: :js:class:`Reply`
2152
2153.. js:function:: TXN.done(txn[, reply])
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002154
Willy Tarreaubc183a62015-08-28 10:39:11 +02002155 This function terminates processing of the transaction and the associated
Christopher Faulet700d9e82020-01-31 12:21:52 +01002156 session and optionally reply to the client for HTTP sessions.
2157
2158 :param class_txn txn: The class txn object containing the data.
2159 :param class_reply reply: The class reply object to return to the client.
2160
2161 This functions can be used when a critical error is detected or to terminate
Willy Tarreaubc183a62015-08-28 10:39:11 +02002162 processing after some data have been returned to the client (eg: a redirect).
Christopher Faulet700d9e82020-01-31 12:21:52 +01002163 To do so, a reply may be provided. This object is optional and may contain a
2164 status code, a reason, a header list and a body. All these fields are
Christopher Faulet7855b192021-11-09 18:39:51 +01002165 optional. When not provided, the default values are used. By default, with an
2166 empty reply object, an empty HTTP 200 response is returned to the client. If
2167 no reply object is provided, the transaction is terminated without any
2168 reply. If a reply object is provided, it must not exceed the buffer size once
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02002169 converted into the internal HTTP representation. Because for now there is no
Christopher Faulet7855b192021-11-09 18:39:51 +01002170 easy way to be sure it fits, it is probably better to keep it reasonably
2171 small.
Christopher Faulet700d9e82020-01-31 12:21:52 +01002172
2173 The reply object may be fully created in lua or the class Reply may be used to
2174 create it.
2175
2176.. code-block:: lua
2177
2178 local reply = txn:reply()
2179 reply:set_status(400, "Bad request")
2180 reply:add_header("content-type", "text/html")
2181 reply:add_header("cache-control", "no-cache")
2182 reply:add_header("cache-control", "no-store")
2183 reply:set_body("<html><body><h1>invalid request<h1></body></html>")
2184 txn:done(reply)
2185..
2186
2187.. code-block:: lua
2188
2189 txn:done{
2190 status = 400,
2191 reason = "Bad request",
2192 headers = {
2193 ["content-type"] = { "text/html" },
2194 ["cache-control"] = { "no-cache", "no-store" },
2195 },
2196 body = "<html><body><h1>invalid request<h1></body></html>"
2197 }
2198..
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002199
Christopher Faulet1e9b1b62021-08-11 10:14:30 +02002200 .. warning::
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02002201 It does not make sense to call this function from sample-fetches. In this case
2202 the behavior is the same than core.done(): it finishes the Lua
Christopher Faulet1e9b1b62021-08-11 10:14:30 +02002203 execution. The transaction is really aborted only from an action registered
2204 function.
Thierry FOURNIERab00df62016-07-14 11:42:37 +02002205
Christopher Faulet700d9e82020-01-31 12:21:52 +01002206 :see: :js:func:`TXN.reply`, :js:class:`Reply`
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002207
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002208.. js:function:: TXN.set_loglevel(txn, loglevel)
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01002209
2210 Is used to change the log level of the current request. The "loglevel" must
2211 be an integer between 0 and 7.
2212
2213 :param class_txn txn: The class txn object containing the data.
2214 :param integer loglevel: The required log level. This variable can be one of
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01002215 :see: :js:attr:`core.emerg`, :js:attr:`core.alert`, :js:attr:`core.crit`,
2216 :js:attr:`core.err`, :js:attr:`core.warning`, :js:attr:`core.notice`,
2217 :js:attr:`core.info`, :js:attr:`core.debug` (log level definitions)
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01002218
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002219.. js:function:: TXN.set_tos(txn, tos)
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01002220
2221 Is used to set the TOS or DSCP field value of packets sent to the client to
2222 the value passed in "tos" on platforms which support this.
2223
2224 :param class_txn txn: The class txn object containing the data.
2225 :param integer tos: The new TOS os DSCP.
2226
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002227.. js:function:: TXN.set_mark(txn, mark)
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01002228
2229 Is used to set the Netfilter MARK on all packets sent to the client to the
2230 value passed in "mark" on platforms which support it.
2231
2232 :param class_txn txn: The class txn object containing the data.
2233 :param integer mark: The mark value.
2234
Patrick Hemmer268a7072018-05-11 12:52:31 -04002235.. js:function:: TXN.set_priority_class(txn, prio)
2236
2237 This function adjusts the priority class of the transaction. The value should
2238 be within the range -2047..2047. Values outside this range will be
2239 truncated.
2240
2241 See the HAProxy configuration.txt file keyword "http-request" action
2242 "set-priority-class" for details.
2243
2244.. js:function:: TXN.set_priority_offset(txn, prio)
2245
2246 This function adjusts the priority offset of the transaction. The value
2247 should be within the range -524287..524287. Values outside this range will be
2248 truncated.
2249
2250 See the HAProxy configuration.txt file keyword "http-request" action
2251 "set-priority-offset" for details.
2252
Christopher Faulet700d9e82020-01-31 12:21:52 +01002253.. _reply_class:
2254
2255Reply class
2256============
2257
2258.. js:class:: Reply
2259
2260 **context**: action
2261
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02002262 This class represents a HTTP response message. It provides some methods to
Christopher Faulet7855b192021-11-09 18:39:51 +01002263 enrich it. Once converted into the internal HTTP representation, the response
2264 message must not exceed the buffer size. Because for now there is no
2265 easy way to be sure it fits, it is probably better to keep it reasonably
2266 small.
2267
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02002268 See tune.bufsize in the configuration manual for details.
Christopher Faulet700d9e82020-01-31 12:21:52 +01002269
2270.. code-block:: lua
2271
2272 local reply = txn:reply({status = 400}) -- default HTTP 400 reason-phase used
2273 reply:add_header("content-type", "text/html")
2274 reply:add_header("cache-control", "no-cache")
2275 reply:add_header("cache-control", "no-store")
2276 reply:set_body("<html><body><h1>invalid request<h1></body></html>")
2277..
2278
2279 :see: :js:func:`TXN.reply`
2280
2281.. js:attribute:: Reply.status
2282
2283 The reply status code. By default, the status code is set to 200.
2284
2285 :returns: integer
2286
2287.. js:attribute:: Reply.reason
2288
2289 The reason string describing the status code.
2290
2291 :returns: string
2292
2293.. js:attribute:: Reply.headers
2294
2295 A table indexing all reply headers by name. To each name is associated an
2296 ordered list of values.
2297
2298 :returns: Lua table
2299
2300.. code-block:: lua
2301
2302 {
2303 ["content-type"] = { "text/html" },
2304 ["cache-control"] = {"no-cache", "no-store" },
2305 x_header_name = { "value1", "value2", ... }
2306 ...
2307 }
2308..
2309
2310.. js:attribute:: Reply.body
2311
2312 The reply payload.
2313
2314 :returns: string
2315
2316.. js:function:: Reply.set_status(REPLY, status[, reason])
2317
2318 Set the reply status code and optionally the reason-phrase. If the reason is
2319 not provided, the default reason corresponding to the status code is used.
2320
2321 :param class_reply reply: The related Reply object.
2322 :param integer status: The reply status code.
2323 :param string reason: The reply status reason (optional).
2324
2325.. js:function:: Reply.add_header(REPLY, name, value)
2326
2327 Add a header to the reply object. If the header does not already exist, a new
2328 entry is created with its name as index and a one-element list containing its
2329 value as value. Otherwise, the header value is appended to the ordered list of
2330 values associated to the header name.
2331
2332 :param class_reply reply: The related Reply object.
2333 :param string name: The header field name.
2334 :param string value: The header field value.
2335
2336.. js:function:: Reply.del_header(REPLY, name)
2337
2338 Remove all occurrences of a header name from the reply object.
2339
2340 :param class_reply reply: The related Reply object.
2341 :param string name: The header field name.
2342
2343.. js:function:: Reply.set_body(REPLY, body)
2344
2345 Set the reply payload.
2346
2347 :param class_reply reply: The related Reply object.
2348 :param string body: The reply payload.
2349
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002350.. _socket_class:
2351
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002352Socket class
2353============
2354
2355.. js:class:: Socket
2356
2357 This class must be compatible with the Lua Socket class. Only the 'client'
2358 functions are available. See the Lua Socket documentation:
2359
2360 `http://w3.impa.br/~diego/software/luasocket/tcp.html
2361 <http://w3.impa.br/~diego/software/luasocket/tcp.html>`_
2362
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002363.. js:function:: Socket.close(socket)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002364
2365 Closes a TCP object. The internal socket used by the object is closed and the
2366 local address to which the object was bound is made available to other
2367 applications. No further operations (except for further calls to the close
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002368 method) are allowed on a closed Socket.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002369
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002370 :param class_socket socket: Is the manipulated Socket.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002371
2372 Note: It is important to close all used sockets once they are not needed,
2373 since, in many systems, each socket uses a file descriptor, which are limited
2374 system resources. Garbage-collected objects are automatically closed before
2375 destruction, though.
2376
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002377.. js:function:: Socket.connect(socket, address[, port])
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002378
2379 Attempts to connect a socket object to a remote host.
2380
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002381
2382 In case of error, the method returns nil followed by a string describing the
2383 error. In case of success, the method returns 1.
2384
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002385 :param class_socket socket: Is the manipulated Socket.
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002386 :param string address: can be an IP address or a host name. See below for more
2387 information.
2388 :param integer port: must be an integer number in the range [1..64K].
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002389 :returns: 1 or nil.
2390
Bertrand Jacquin874a35c2018-09-10 21:26:07 +01002391 An address field extension permits to use the connect() function to connect to
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002392 other stream than TCP. The syntax containing a simpleipv4 or ipv6 address is
2393 the basically expected format. This format requires the port.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002394
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002395 Other format accepted are a socket path like "/socket/path", it permits to
Bertrand Jacquin874a35c2018-09-10 21:26:07 +01002396 connect to a socket. Abstract namespaces are supported with the prefix
Joseph Herlant02cedc42018-11-13 19:45:17 -08002397 "abns@", and finally a file descriptor can be passed with the prefix "fd@".
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002398 The prefix "ipv4@", "ipv6@" and "unix@" are also supported. The port can be
Bertrand Jacquin874a35c2018-09-10 21:26:07 +01002399 passed int the string. The syntax "127.0.0.1:1234" is valid. In this case, the
Tim Duesterhus6edab862018-01-06 19:04:45 +01002400 parameter *port* must not be set.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002401
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002402.. js:function:: Socket.connect_ssl(socket, address, port)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002403
2404 Same behavior than the function socket:connect, but uses SSL.
2405
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002406 :param class_socket socket: Is the manipulated Socket.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002407 :returns: 1 or nil.
2408
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002409.. js:function:: Socket.getpeername(socket)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002410
2411 Returns information about the remote side of a connected client object.
2412
2413 Returns a string with the IP address of the peer, followed by the port number
2414 that peer is using for the connection. In case of error, the method returns
2415 nil.
2416
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002417 :param class_socket socket: Is the manipulated Socket.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002418 :returns: a string containing the server information.
2419
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002420.. js:function:: Socket.getsockname(socket)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002421
2422 Returns the local address information associated to the object.
2423
2424 The method returns a string with local IP address and a number with the port.
2425 In case of error, the method returns nil.
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 :returns: a string containing the client information.
2429
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002430.. js:function:: Socket.receive(socket, [pattern [, prefix]])
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002431
2432 Reads data from a client object, according to the specified read pattern.
2433 Patterns follow the Lua file I/O format, and the difference in performance
2434 between all patterns is negligible.
2435
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002436 :param class_socket socket: Is the manipulated Socket.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002437 :param string|integer pattern: Describe what is required (see below).
2438 :param string prefix: A string which will be prefix the returned data.
2439 :returns: a string containing the required data or nil.
2440
2441 Pattern can be any of the following:
2442
2443 * **`*a`**: reads from the socket until the connection is closed. No
2444 end-of-line translation is performed;
2445
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002446 * **`*l`**: reads a line of text from the Socket. The line is terminated by a
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002447 LF character (ASCII 10), optionally preceded by a CR character
2448 (ASCII 13). The CR and LF characters are not included in the
2449 returned line. In fact, all CR characters are ignored by the
2450 pattern. This is the default pattern.
2451
2452 * **number**: causes the method to read a specified number of bytes from the
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002453 Socket. Prefix is an optional string to be concatenated to the
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002454 beginning of any received data before return.
2455
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002456 * **empty**: If the pattern is left empty, the default option is `*l`.
2457
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002458 If successful, the method returns the received pattern. In case of error, the
2459 method returns nil followed by an error message which can be the string
2460 'closed' in case the connection was closed before the transmission was
2461 completed or the string 'timeout' in case there was a timeout during the
2462 operation. Also, after the error message, the function returns the partial
2463 result of the transmission.
2464
2465 Important note: This function was changed severely. It used to support
2466 multiple patterns (but I have never seen this feature used) and now it
2467 doesn't anymore. Partial results used to be returned in the same way as
2468 successful results. This last feature violated the idea that all functions
2469 should return nil on error. Thus it was changed too.
2470
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002471.. js:function:: Socket.send(socket, data [, start [, end ]])
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002472
2473 Sends data through client object.
2474
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002475 :param class_socket socket: Is the manipulated Socket.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002476 :param string data: The data that will be sent.
2477 :param integer start: The start position in the buffer of the data which will
2478 be sent.
2479 :param integer end: The end position in the buffer of the data which will
2480 be sent.
2481 :returns: see below.
2482
2483 Data is the string to be sent. The optional arguments i and j work exactly
2484 like the standard string.sub Lua function to allow the selection of a
2485 substring to be sent.
2486
2487 If successful, the method returns the index of the last byte within [start,
2488 end] that has been sent. Notice that, if start is 1 or absent, this is
2489 effectively the total number of bytes sent. In case of error, the method
2490 returns nil, followed by an error message, followed by the index of the last
2491 byte within [start, end] that has been sent. You might want to try again from
2492 the byte following that. The error message can be 'closed' in case the
2493 connection was closed before the transmission was completed or the string
2494 'timeout' in case there was a timeout during the operation.
2495
2496 Note: Output is not buffered. For small strings, it is always better to
2497 concatenate them in Lua (with the '..' operator) and send the result in one
2498 call instead of calling the method several times.
2499
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002500.. js:function:: Socket.setoption(socket, option [, value])
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002501
2502 Just implemented for compatibility, this cal does nothing.
2503
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002504.. js:function:: Socket.settimeout(socket, value [, mode])
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002505
2506 Changes the timeout values for the object. All I/O operations are blocking.
2507 That is, any call to the methods send, receive, and accept will block
2508 indefinitely, until the operation completes. The settimeout method defines a
2509 limit on the amount of time the I/O methods can block. When a timeout time
2510 has elapsed, the affected methods give up and fail with an error code.
2511
2512 The amount of time to wait is specified as the value parameter, in seconds.
2513
Mark Lakes56cc1252018-03-27 09:48:06 +02002514 The timeout modes are not implemented, the only settable timeout is the
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002515 inactivity time waiting for complete the internal buffer send or waiting for
2516 receive data.
2517
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002518 :param class_socket socket: Is the manipulated Socket.
Bertrand Jacquin874a35c2018-09-10 21:26:07 +01002519 :param float value: The timeout value. Use floating point to specify
Mark Lakes56cc1252018-03-27 09:48:06 +02002520 milliseconds.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002521
Thierry FOURNIER31904272017-10-25 12:59:51 +02002522.. _regex_class:
2523
2524Regex class
2525===========
2526
2527.. js:class:: Regex
2528
2529 This class allows the usage of HAProxy regexes because classic lua doesn't
2530 provides regexes. This class inherits the HAProxy compilation options, so the
2531 regexes can be libc regex, pcre regex or pcre JIT regex.
2532
2533 The expression matching number is limited to 20 per regex. The only available
2534 option is case sensitive.
2535
2536 Because regexes compilation is a heavy process, it is better to define all
2537 your regexes in the **body context** and use it during the runtime.
2538
2539.. code-block:: lua
2540
2541 -- Create the regex
2542 st, regex = Regex.new("needle (..) (...)", true);
2543
2544 -- Check compilation errors
2545 if st == false then
2546 print "error: " .. regex
2547 end
2548
2549 -- Match the regexes
2550 print(regex:exec("Looking for a needle in the haystack")) -- true
2551 print(regex:exec("Lokking for a cat in the haystack")) -- false
2552
2553 -- Extract words
2554 st, list = regex:match("Looking for a needle in the haystack")
2555 print(st) -- true
2556 print(list[1]) -- needle in the
2557 print(list[2]) -- in
2558 print(list[3]) -- the
2559
2560.. js:function:: Regex.new(regex, case_sensitive)
2561
2562 Create and compile a regex.
2563
2564 :param string regex: The regular expression according with the libc or pcre
2565 standard
2566 :param boolean case_sensitive: Match is case sensitive or not.
2567 :returns: boolean status and :ref:`regex_class` or string containing fail reason.
2568
2569.. js:function:: Regex.exec(regex, str)
2570
2571 Execute the regex.
2572
2573 :param class_regex regex: A :ref:`regex_class` object.
2574 :param string str: The input string will be compared with the compiled regex.
2575 :returns: a boolean status according with the match result.
2576
2577.. js:function:: Regex.match(regex, str)
2578
2579 Execute the regex and return matched expressions.
2580
2581 :param class_map map: A :ref:`regex_class` object.
2582 :param string str: The input string will be compared with the compiled regex.
2583 :returns: a boolean status according with the match result, and
2584 a table containing all the string matched in order of declaration.
2585
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002586.. _map_class:
2587
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002588Map class
2589=========
2590
2591.. js:class:: Map
2592
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02002593 This class permits to do some lookups in HAProxy maps. The declared maps can
Bertrand Jacquin874a35c2018-09-10 21:26:07 +01002594 be modified during the runtime through the HAProxy management socket.
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002595
2596.. code-block:: lua
2597
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002598 default = "usa"
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002599
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002600 -- Create and load map
Thierry FOURNIER4dc71972017-01-28 08:33:08 +01002601 geo = Map.new("geo.map", Map._ip);
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002602
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002603 -- Create new fetch that returns the user country
2604 core.register_fetches("country", function(txn)
2605 local src;
2606 local loc;
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002607
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002608 src = txn.f:fhdr("x-forwarded-for");
2609 if (src == nil) then
2610 src = txn.f:src()
2611 if (src == nil) then
2612 return default;
2613 end
2614 end
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002615
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002616 -- Perform lookup
2617 loc = geo:lookup(src);
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002618
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002619 if (loc == nil) then
2620 return default;
2621 end
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002622
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002623 return loc;
2624 end);
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002625
Thierry FOURNIER4dc71972017-01-28 08:33:08 +01002626.. js:attribute:: Map._int
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002627
2628 See the HAProxy configuration.txt file, chapter "Using ACLs and fetching
Ilya Shipitsin2075ca82020-03-06 23:22:22 +05002629 samples" and subchapter "ACL basics" to understand this pattern matching
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002630 method.
2631
Thierry FOURNIER4dc71972017-01-28 08:33:08 +01002632 Note that :js:attr:`Map.int` is also available for compatibility.
2633
2634.. js:attribute:: Map._ip
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002635
2636 See the HAProxy configuration.txt file, chapter "Using ACLs and fetching
Ilya Shipitsin2075ca82020-03-06 23:22:22 +05002637 samples" and subchapter "ACL basics" to understand this pattern matching
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002638 method.
2639
Thierry FOURNIER4dc71972017-01-28 08:33:08 +01002640 Note that :js:attr:`Map.ip` is also available for compatibility.
2641
2642.. js:attribute:: Map._str
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002643
2644 See the HAProxy configuration.txt file, chapter "Using ACLs and fetching
Ilya Shipitsin2075ca82020-03-06 23:22:22 +05002645 samples" and subchapter "ACL basics" to understand this pattern matching
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002646 method.
2647
Thierry FOURNIER4dc71972017-01-28 08:33:08 +01002648 Note that :js:attr:`Map.str` is also available for compatibility.
2649
2650.. js:attribute:: Map._beg
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002651
2652 See the HAProxy configuration.txt file, chapter "Using ACLs and fetching
Ilya Shipitsin2075ca82020-03-06 23:22:22 +05002653 samples" and subchapter "ACL basics" to understand this pattern matching
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002654 method.
2655
Thierry FOURNIER4dc71972017-01-28 08:33:08 +01002656 Note that :js:attr:`Map.beg` is also available for compatibility.
2657
2658.. js:attribute:: Map._sub
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002659
2660 See the HAProxy configuration.txt file, chapter "Using ACLs and fetching
Ilya Shipitsin2075ca82020-03-06 23:22:22 +05002661 samples" and subchapter "ACL basics" to understand this pattern matching
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002662 method.
2663
Thierry FOURNIER4dc71972017-01-28 08:33:08 +01002664 Note that :js:attr:`Map.sub` is also available for compatibility.
2665
2666.. js:attribute:: Map._dir
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002667
2668 See the HAProxy configuration.txt file, chapter "Using ACLs and fetching
Ilya Shipitsin2075ca82020-03-06 23:22:22 +05002669 samples" and subchapter "ACL basics" to understand this pattern matching
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002670 method.
2671
Thierry FOURNIER4dc71972017-01-28 08:33:08 +01002672 Note that :js:attr:`Map.dir` is also available for compatibility.
2673
2674.. js:attribute:: Map._dom
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002675
2676 See the HAProxy configuration.txt file, chapter "Using ACLs and fetching
Ilya Shipitsin2075ca82020-03-06 23:22:22 +05002677 samples" and subchapter "ACL basics" to understand this pattern matching
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002678 method.
2679
Thierry FOURNIER4dc71972017-01-28 08:33:08 +01002680 Note that :js:attr:`Map.dom` is also available for compatibility.
2681
2682.. js:attribute:: Map._end
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002683
2684 See the HAProxy configuration.txt file, chapter "Using ACLs and fetching
Ilya Shipitsin2075ca82020-03-06 23:22:22 +05002685 samples" and subchapter "ACL basics" to understand this pattern matching
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002686 method.
2687
Thierry FOURNIER4dc71972017-01-28 08:33:08 +01002688.. js:attribute:: Map._reg
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002689
2690 See the HAProxy configuration.txt file, chapter "Using ACLs and fetching
Ilya Shipitsin2075ca82020-03-06 23:22:22 +05002691 samples" and subchapter "ACL basics" to understand this pattern matching
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002692 method.
2693
Thierry FOURNIER4dc71972017-01-28 08:33:08 +01002694 Note that :js:attr:`Map.reg` is also available for compatibility.
2695
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002696
2697.. js:function:: Map.new(file, method)
2698
2699 Creates and load a map.
2700
2701 :param string file: Is the file containing the map.
2702 :param integer method: Is the map pattern matching method. See the attributes
2703 of the Map class.
2704 :returns: a class Map object.
Thierry FOURNIER4dc71972017-01-28 08:33:08 +01002705 :see: The Map attributes: :js:attr:`Map._int`, :js:attr:`Map._ip`,
2706 :js:attr:`Map._str`, :js:attr:`Map._beg`, :js:attr:`Map._sub`,
2707 :js:attr:`Map._dir`, :js:attr:`Map._dom`, :js:attr:`Map._end` and
2708 :js:attr:`Map._reg`.
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002709
2710.. js:function:: Map.lookup(map, str)
2711
2712 Perform a lookup in a map.
2713
2714 :param class_map map: Is the class Map object.
2715 :param string str: Is the string used as key.
2716 :returns: a string containing the result or nil if no match.
2717
2718.. js:function:: Map.slookup(map, str)
2719
2720 Perform a lookup in a map.
2721
2722 :param class_map map: Is the class Map object.
2723 :param string str: Is the string used as key.
2724 :returns: a string containing the result or empty string if no match.
2725
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002726.. _applethttp_class:
2727
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002728AppletHTTP class
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002729================
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002730
2731.. js:class:: AppletHTTP
2732
2733 This class is used with applets that requires the 'http' mode. The http applet
2734 can be registered with the *core.register_service()* function. They are used
2735 for processing an http request like a server in back of HAProxy.
2736
2737 This is an hello world sample code:
2738
2739.. code-block:: lua
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002740
Pieter Baauw4d7f7662015-11-08 16:38:08 +01002741 core.register_service("hello-world", "http", function(applet)
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002742 local response = "Hello World !"
2743 applet:set_status(200)
Pieter Baauw2dcb9bc2015-10-01 22:47:12 +02002744 applet:add_header("content-length", string.len(response))
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002745 applet:add_header("content-type", "text/plain")
Pieter Baauw2dcb9bc2015-10-01 22:47:12 +02002746 applet:start_response()
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002747 applet:send(response)
2748 end)
2749
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002750.. js:attribute:: AppletHTTP.c
2751
2752 :returns: A :ref:`converters_class`
2753
2754 This attribute contains a Converters class object.
2755
2756.. js:attribute:: AppletHTTP.sc
2757
2758 :returns: A :ref:`converters_class`
2759
2760 This attribute contains a Converters class object. The
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02002761 functions of this object always return a string.
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002762
2763.. js:attribute:: AppletHTTP.f
2764
2765 :returns: A :ref:`fetches_class`
2766
2767 This attribute contains a Fetches class object. Note that the
2768 applet execution place cannot access to a valid HAProxy core HTTP
Ilya Shipitsin2075ca82020-03-06 23:22:22 +05002769 transaction, so some sample fetches related to the HTTP dependent
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002770 values (hdr, path, ...) are not available.
2771
2772.. js:attribute:: AppletHTTP.sf
2773
2774 :returns: A :ref:`fetches_class`
2775
2776 This attribute contains a Fetches class object. The functions of
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02002777 this object always return a string. Note that the applet
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002778 execution place cannot access to a valid HAProxy core HTTP
Ilya Shipitsin2075ca82020-03-06 23:22:22 +05002779 transaction, so some sample fetches related to the HTTP dependent
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002780 values (hdr, path, ...) are not available.
2781
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01002782.. js:attribute:: AppletHTTP.method
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002783
2784 :returns: string
2785
2786 The attribute method returns a string containing the HTTP
2787 method.
2788
2789.. js:attribute:: AppletHTTP.version
2790
2791 :returns: string
2792
2793 The attribute version, returns a string containing the HTTP
2794 request version.
2795
2796.. js:attribute:: AppletHTTP.path
2797
2798 :returns: string
2799
2800 The attribute path returns a string containing the HTTP
2801 request path.
2802
2803.. js:attribute:: AppletHTTP.qs
2804
2805 :returns: string
2806
2807 The attribute qs returns a string containing the HTTP
2808 request query string.
2809
2810.. js:attribute:: AppletHTTP.length
2811
2812 :returns: integer
2813
2814 The attribute length returns an integer containing the HTTP
2815 body length.
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002816
Thierry FOURNIER841475e2015-12-11 17:10:09 +01002817.. js:attribute:: AppletHTTP.headers
2818
Patrick Hemmerc6a1d712018-05-01 21:30:41 -04002819 :returns: table
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002820
Patrick Hemmerc6a1d712018-05-01 21:30:41 -04002821 The attribute headers returns a table containing the HTTP
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002822 headers. The header names are always in lower case. As the header name can be
2823 encountered more than once in each request, the value is indexed with 0 as
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02002824 first index value. The table has this form:
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002825
2826.. code-block:: lua
2827
2828 AppletHTTP.headers['<header-name>'][<header-index>] = "<header-value>"
2829
2830 AppletHTTP.headers["host"][0] = "www.test.com"
2831 AppletHTTP.headers["accept"][0] = "audio/basic q=1"
2832 AppletHTTP.headers["accept"][1] = "audio/*, q=0.2"
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01002833 AppletHTTP.headers["accept"][2] = "*/*, q=0.1"
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002834..
2835
Robin H. Johnson52f5db22017-01-01 13:10:52 -08002836.. js:function:: AppletHTTP.set_status(applet, code [, reason])
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002837
2838 This function sets the HTTP status code for the response. The allowed code are
2839 from 100 to 599.
2840
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01002841 :param class_AppletHTTP applet: An :ref:`applethttp_class`
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002842 :param integer code: the status code returned to the client.
Robin H. Johnson52f5db22017-01-01 13:10:52 -08002843 :param string reason: the status reason returned to the client (optional).
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002844
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01002845.. js:function:: AppletHTTP.add_header(applet, name, value)
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002846
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02002847 This function adds a header in the response. Duplicated headers are not
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002848 collapsed. The special header *content-length* is used to determinate the
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02002849 response length. If it does not exist, a *transfer-encoding: chunked* is set, and
Ilya Shipitsin2075ca82020-03-06 23:22:22 +05002850 all the write from the function *AppletHTTP:send()* become a chunk.
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002851
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01002852 :param class_AppletHTTP applet: An :ref:`applethttp_class`
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002853 :param string name: the header name
2854 :param string value: the header value
2855
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01002856.. js:function:: AppletHTTP.start_response(applet)
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002857
2858 This function indicates to the HTTP engine that it can process and send the
2859 response headers. After this called we cannot add headers to the response; We
2860 cannot use the *AppletHTTP:send()* function if the
2861 *AppletHTTP:start_response()* is not called.
2862
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01002863 :param class_AppletHTTP applet: An :ref:`applethttp_class`
2864
2865.. js:function:: AppletHTTP.getline(applet)
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002866
2867 This function returns a string containing one line from the http body. If the
2868 data returned doesn't contains a final '\\n' its assumed than its the last
2869 available data before the end of stream.
2870
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01002871 :param class_AppletHTTP applet: An :ref:`applethttp_class`
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002872 :returns: a string. The string can be empty if we reach the end of the stream.
2873
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01002874.. js:function:: AppletHTTP.receive(applet, [size])
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002875
2876 Reads data from the HTTP body, according to the specified read *size*. If the
2877 *size* is missing, the function tries to read all the content of the stream
2878 until the end. If the *size* is bigger than the http body, it returns the
Bertrand Jacquin874a35c2018-09-10 21:26:07 +01002879 amount of data available.
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002880
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 integer size: the required read size.
Ilya Shipitsin11057a32020-06-21 21:18:27 +05002883 :returns: always return a string,the string can be empty is the connection is
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002884 closed.
2885
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01002886.. js:function:: AppletHTTP.send(applet, msg)
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002887
2888 Send the message *msg* on the http request body.
2889
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01002890 :param class_AppletHTTP applet: An :ref:`applethttp_class`
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002891 :param string msg: the message to send.
2892
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01002893.. js:function:: AppletHTTP.get_priv(applet)
2894
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01002895 Return Lua data stored in the current transaction. If no data are stored,
2896 it returns a nil value.
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01002897
2898 :param class_AppletHTTP applet: An :ref:`applethttp_class`
Bertrand Jacquin874a35c2018-09-10 21:26:07 +01002899 :returns: the opaque data previously stored, or nil if nothing is
2900 available.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01002901 :see: :js:func:`AppletHTTP.set_priv`
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01002902
2903.. js:function:: AppletHTTP.set_priv(applet, data)
2904
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02002905 Store any data in the current HAProxy transaction. This action replaces the
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01002906 old stored data.
2907
2908 :param class_AppletHTTP applet: An :ref:`applethttp_class`
2909 :param opaque data: The data which is stored in the transaction.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01002910 :see: :js:func:`AppletHTTP.get_priv`
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01002911
Tim Duesterhus4e172c92020-05-19 13:49:42 +02002912.. js:function:: AppletHTTP.set_var(applet, var, value[, ifexist])
Thierry FOURNIER / OZON.IOc1edafe2016-12-12 16:25:30 +01002913
2914 Converts a Lua type in a HAProxy type and store it in a variable <var>.
2915
2916 :param class_AppletHTTP applet: An :ref:`applethttp_class`
2917 :param string var: The variable name according with the HAProxy variable syntax.
2918 :param type value: The value associated to the variable. The type ca be string or
2919 integer.
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02002920 :param boolean ifexist: If this parameter is set to true the variable
Tim Duesterhus4e172c92020-05-19 13:49:42 +02002921 will only be set if it was defined elsewhere (i.e. used
Willy Tarreau7978c5c2021-09-07 14:24:07 +02002922 within the configuration). For global variables (using the
2923 "proc" scope), they will only be updated and never created.
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02002924 It is highly recommended to always set this to true.
2925
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01002926 :see: :js:func:`AppletHTTP.unset_var`
2927 :see: :js:func:`AppletHTTP.get_var`
Thierry FOURNIER / OZON.IOc1edafe2016-12-12 16:25:30 +01002928
2929.. js:function:: AppletHTTP.unset_var(applet, var)
2930
2931 Unset the variable <var>.
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.get_var`
Thierry FOURNIER / OZON.IOc1edafe2016-12-12 16:25:30 +01002937
2938.. js:function:: AppletHTTP.get_var(applet, var)
2939
2940 Returns data stored in the variable <var> converter in Lua type.
2941
2942 :param class_AppletHTTP applet: An :ref:`applethttp_class`
2943 :param string var: The variable name according with the HAProxy variable syntax.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01002944 :see: :js:func:`AppletHTTP.set_var`
2945 :see: :js:func:`AppletHTTP.unset_var`
Thierry FOURNIER / OZON.IOc1edafe2016-12-12 16:25:30 +01002946
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002947.. _applettcp_class:
2948
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002949AppletTCP class
2950===============
2951
2952.. js:class:: AppletTCP
2953
2954 This class is used with applets that requires the 'tcp' mode. The tcp applet
2955 can be registered with the *core.register_service()* function. They are used
2956 for processing a tcp stream like a server in back of HAProxy.
2957
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002958.. js:attribute:: AppletTCP.c
2959
2960 :returns: A :ref:`converters_class`
2961
2962 This attribute contains a Converters class object.
2963
2964.. js:attribute:: AppletTCP.sc
2965
2966 :returns: A :ref:`converters_class`
2967
2968 This attribute contains a Converters class object. The
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02002969 functions of this object always return a string.
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002970
2971.. js:attribute:: AppletTCP.f
2972
2973 :returns: A :ref:`fetches_class`
2974
2975 This attribute contains a Fetches class object.
2976
2977.. js:attribute:: AppletTCP.sf
2978
2979 :returns: A :ref:`fetches_class`
2980
2981 This attribute contains a Fetches class object.
2982
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01002983.. js:function:: AppletTCP.getline(applet)
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002984
2985 This function returns a string containing one line from the stream. If the
2986 data returned doesn't contains a final '\\n' its assumed than its the last
2987 available data before the end of stream.
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 :returns: a string. The string can be empty if we reach the end of the stream.
2991
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01002992.. js:function:: AppletTCP.receive(applet, [size])
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002993
2994 Reads data from the TCP stream, according to the specified read *size*. If the
2995 *size* is missing, the function tries to read all the content of the stream
2996 until the end.
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 integer size: the required read size.
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02003000 :returns: always return a string, the string can be empty if the connection is
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02003001 closed.
3002
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01003003.. js:function:: AppletTCP.send(appletmsg)
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02003004
3005 Send the message on the stream.
3006
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01003007 :param class_AppletTCP applet: An :ref:`applettcp_class`
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02003008 :param string msg: the message to send.
3009
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01003010.. js:function:: AppletTCP.get_priv(applet)
3011
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01003012 Return Lua data stored in the current transaction. If no data are stored,
3013 it returns a nil value.
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01003014
3015 :param class_AppletTCP applet: An :ref:`applettcp_class`
Bertrand Jacquin874a35c2018-09-10 21:26:07 +01003016 :returns: the opaque data previously stored, or nil if nothing is
3017 available.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01003018 :see: :js:func:`AppletTCP.set_priv`
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01003019
3020.. js:function:: AppletTCP.set_priv(applet, data)
3021
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02003022 Store any data in the current HAProxy transaction. This action replaces the
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01003023 old stored data.
3024
3025 :param class_AppletTCP applet: An :ref:`applettcp_class`
3026 :param opaque data: The data which is stored in the transaction.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01003027 :see: :js:func:`AppletTCP.get_priv`
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01003028
Tim Duesterhus4e172c92020-05-19 13:49:42 +02003029.. js:function:: AppletTCP.set_var(applet, var, value[, ifexist])
Thierry FOURNIER / OZON.IOc1edafe2016-12-12 16:25:30 +01003030
3031 Converts a Lua type in a HAProxy type and stores it in a variable <var>.
3032
3033 :param class_AppletTCP applet: An :ref:`applettcp_class`
3034 :param string var: The variable name according with the HAProxy variable syntax.
3035 :param type value: The value associated to the variable. The type can be string or
3036 integer.
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02003037 :param boolean ifexist: If this parameter is set to true the variable
Tim Duesterhus4e172c92020-05-19 13:49:42 +02003038 will only be set if it was defined elsewhere (i.e. used
Willy Tarreau7978c5c2021-09-07 14:24:07 +02003039 within the configuration). For global variables (using the
3040 "proc" scope), they will only be updated and never created.
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02003041 It is highly recommended to always set this to true.
3042
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01003043 :see: :js:func:`AppletTCP.unset_var`
3044 :see: :js:func:`AppletTCP.get_var`
Thierry FOURNIER / OZON.IOc1edafe2016-12-12 16:25:30 +01003045
3046.. js:function:: AppletTCP.unset_var(applet, var)
3047
3048 Unsets the variable <var>.
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
3055.. js:function:: AppletTCP.get_var(applet, var)
3056
3057 Returns data stored in the variable <var> converter in Lua type.
3058
3059 :param class_AppletTCP applet: An :ref:`applettcp_class`
3060 :param string var: The variable name according with the HAProxy variable syntax.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01003061 :see: :js:func:`AppletTCP.unset_var`
3062 :see: :js:func:`AppletTCP.set_var`
Thierry FOURNIER / OZON.IOc1edafe2016-12-12 16:25:30 +01003063
Adis Nezirovic8878f8e2018-07-13 12:18:33 +02003064StickTable class
3065================
3066
3067.. js:class:: StickTable
3068
3069 **context**: task, action, sample-fetch
3070
3071 This class can be used to access the HAProxy stick tables from Lua.
3072
3073.. js:function:: StickTable.info()
3074
3075 Returns stick table attributes as a Lua table. See HAProxy documentation for
Ilya Shipitsin2272d8a2020-12-21 01:22:40 +05003076 "stick-table" for canonical info, or check out example below.
Adis Nezirovic8878f8e2018-07-13 12:18:33 +02003077
3078 :returns: Lua table
3079
3080 Assume our table has IPv4 key and gpc0 and conn_rate "columns":
3081
3082.. code-block:: lua
3083
3084 {
3085 expire=<int>, # Value in ms
3086 size=<int>, # Maximum table size
3087 used=<int>, # Actual number of entries in table
3088 data={ # Data columns, with types as key, and periods as values
3089 (-1 if type is not rate counter)
3090 conn_rate=<int>,
3091 gpc0=-1
3092 },
3093 length=<int>, # max string length for string table keys, key length
3094 # otherwise
3095 nopurge=<boolean>, # purge oldest entries when table is full
3096 type="ip" # can be "ip", "ipv6", "integer", "string", "binary"
3097 }
3098
3099.. js:function:: StickTable.lookup(key)
3100
3101 Returns stick table entry for given <key>
3102
3103 :param string key: Stick table key (IP addresses and strings are supported)
3104 :returns: Lua table
3105
3106.. js:function:: StickTable.dump([filter])
3107
3108 Returns all entries in stick table. An optional filter can be used
3109 to extract entries with specific data values. Filter is a table with valid
3110 comparison operators as keys followed by data type name and value pairs.
3111 Check out the HAProxy docs for "show table" for more details. For the
3112 reference, the supported operators are:
3113 "eq", "ne", "le", "lt", "ge", "gt"
3114
3115 For large tables, execution of this function can take a long time (for
3116 HAProxy standards). That's also true when filter is used, so take care and
3117 measure the impact.
3118
3119 :param table filter: Stick table filter
3120 :returns: Stick table entries (table)
3121
3122 See below for example filter, which contains 4 entries (or comparisons).
3123 (Maximum number of filter entries is 4, defined in the source code)
3124
3125.. code-block:: lua
3126
3127 local filter = {
3128 {"gpc0", "gt", 30}, {"gpc1", "gt", 20}}, {"conn_rate", "le", 10}
3129 }
3130
Christopher Faulet0f3c8902020-01-31 18:57:12 +01003131.. _action_class:
3132
3133Action class
3134=============
3135
3136.. js:class:: Act
3137
3138 **context**: action
3139
3140 This class contains all return codes an action may return. It is the lua
3141 equivalent to HAProxy "ACT_RET_*" code.
3142
3143.. code-block:: lua
3144
3145 core.register_action("deny", { "http-req" }, function (txn)
3146 return act.DENY
3147 end)
3148..
3149.. js:attribute:: act.CONTINUE
3150
3151 This attribute is an integer (0). It instructs HAProxy to continue the current
3152 ruleset processing on the message. It is the default return code for a lua
3153 action.
3154
3155 :returns: integer
3156
3157.. js:attribute:: act.STOP
3158
3159 This attribute is an integer (1). It instructs HAProxy to stop the current
3160 ruleset processing on the message.
3161
3162.. js:attribute:: act.YIELD
3163
3164 This attribute is an integer (2). It instructs HAProxy to temporarily pause
3165 the message processing. It will be resumed later on the same rule. The
3166 corresponding lua script is re-executed for the start.
3167
3168.. js:attribute:: act.ERROR
3169
3170 This attribute is an integer (3). It triggers an internal errors The message
3171 processing is stopped and the transaction is terminated. For HTTP streams, an
3172 HTTP 500 error is returned to the client.
3173
3174 :returns: integer
3175
3176.. js:attribute:: act.DONE
3177
3178 This attribute is an integer (4). It instructs HAProxy to stop the message
3179 processing.
3180
3181 :returns: integer
3182
3183.. js:attribute:: act.DENY
3184
3185 This attribute is an integer (5). It denies the current message. The message
3186 processing is stopped and the transaction is terminated. For HTTP streams, an
3187 HTTP 403 error is returned to the client if the deny is returned during the
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02003188 request analysis. During the response analysis, a HTTP 502 error is returned
Christopher Faulet0f3c8902020-01-31 18:57:12 +01003189 and the server response is discarded.
3190
3191 :returns: integer
3192
3193.. js:attribute:: act.ABORT
3194
3195 This attribute is an integer (6). It aborts the current message. The message
3196 processing is stopped and the transaction is terminated. For HTTP streams,
Willy Tarreau714f3452021-05-09 06:47:26 +02003197 HAProxy assumes a response was already sent to the client. From the Lua
Christopher Faulet0f3c8902020-01-31 18:57:12 +01003198 actions point of view, when this code is used, the transaction is terminated
3199 with no reply.
3200
3201 :returns: integer
3202
3203.. js:attribute:: act.INVALID
3204
3205 This attribute is an integer (7). It triggers an internal errors. The message
3206 processing is stopped and the transaction is terminated. For HTTP streams, an
3207 HTTP 400 error is returned to the client if the error is returned during the
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02003208 request analysis. During the response analysis, a HTTP 502 error is returned
Christopher Faulet0f3c8902020-01-31 18:57:12 +01003209 and the server response is discarded.
3210
3211 :returns: integer
Adis Nezirovic8878f8e2018-07-13 12:18:33 +02003212
Christopher Faulet2c2c2e32020-01-31 19:07:52 +01003213.. js:function:: act:wake_time(milliseconds)
3214
3215 **context**: action
3216
3217 Set the script pause timeout to the specified time, defined in
3218 milliseconds.
3219
3220 :param integer milliseconds: the required milliseconds.
3221
3222 This function may be used when a lua action returns `act.YIELD`, to force its
3223 wake-up at most after the specified number of milliseconds.
3224
Christopher Faulet5a2c6612021-08-15 20:35:25 +02003225.. _filter_class:
3226
3227Filter class
3228=============
3229
3230.. js:class:: filter
3231
3232 **context**: filter
3233
3234 This class contains return codes some filter callback functions may return. It
3235 also contains configuration flags and some helper functions. To understand how
3236 the filter API works, see `doc/internal/filters.txt` documentation.
3237
3238.. js:attribute:: filter.CONTINUE
3239
3240 This attribute is an integer (1). It may be returned by some filter callback
3241 functions to instruct this filtering step is finished for this filter.
3242
3243.. js:attribute:: filter.WAIT
3244
3245 This attribute is an integer (0). It may be returned by some filter callback
3246 functions to instruct the filtering must be paused, waiting for more data or
3247 for an external event depending on this filter.
3248
3249.. js:attribute:: filter.ERROR
3250
3251 This attribute is an integer (-1). It may be returned by some filter callback
3252 functions to trigger an error.
3253
3254.. js:attribute:: filter.FLT_CFG_FL_HTX
3255
3256 This attribute is a flag corresponding to the filter flag FLT_CFG_FL_HTX. When
3257 it is set for a filter, it means the filter is able to filter HTTP streams.
3258
3259.. js:function:: filter.register_data_filter(chn)
3260
3261 **context**: filter
3262
3263 Enable the data filtering on the channel **chn** for the current filter. It
Ilya Shipitsinff0f2782021-08-22 22:18:07 +05003264 may be called at any time from any callback functions proceeding the data
Christopher Faulet5a2c6612021-08-15 20:35:25 +02003265 analysis.
3266
3267 :param class_Channel chn: A :ref:`channel_class`.
3268
3269.. js:function:: filter.unregister_data_filter(chn)
3270
3271 **context**: filter
3272
3273 Disable the data filtering on the channel **chn** for the current filter. It
3274 may be called at any time from any callback functions.
3275
3276 :param class_Channel chn: A :ref:`channel_class`.
3277
3278.. js:function:: filter.wake_time(milliseconds)
3279
3280 **context**: filter
3281
3282 Set the script pause timeout to the specified time, defined in
3283 milliseconds.
3284
3285 :param integer milliseconds: the required milliseconds.
3286
3287 This function may be used from any lua filter callback function to force its
3288 wake-up at most after the specified number of milliseconds. Especially, when
3289 `filter.CONTINUE` is returned.
3290
3291
3292A filters is declared using :js:func:`core.register_filter()` function. The
3293provided class will be used to instantiate filters. It may define following
3294attributes:
3295
3296* id: The filter identifier. It is a string that identifies the filter and is
3297 optional.
3298
3299* flags: The filter flags. Only :js:attr:`filter.FLT_CFG_FL_HTX` may be set for now.
3300
3301Such filter class must also define all required callback functions in the
3302following list. Note that :js:func:`Filter.new()` must be defined otherwise the
Ilya Shipitsinff0f2782021-08-22 22:18:07 +05003303filter is ignored. Others are optional.
Christopher Faulet5a2c6612021-08-15 20:35:25 +02003304
3305* .. js:function:: FILTER.new()
3306
3307 Called to instantiate a new filter. This function must be defined.
3308
3309 :returns: a Lua object that will be used as filter instance for the current
3310 stream.
3311
3312* .. js:function:: FILTER.start_analyze(flt, txn, chn)
3313
3314 Called when the analysis starts on the channel **chn**.
3315
3316* .. js:function:: FILTER.end_analyze(flt, txn, chn)
3317
3318 Called when the analysis ends on the channel **chn**.
3319
3320* .. js:function:: FILTER.http_headers(flt, txn, http_msg)
3321
3322 Called just before the HTTP payload analysis and after any processing on the
3323 HTTP message **http_msg**. This callback functions is only called for HTTP
3324 streams.
3325
3326* .. js:function:: FILTER.http_payload(flt, txn, http_msg)
3327
3328 Called during the HTTP payload analysis on the HTTP message **http_msg**. This
3329 callback functions is only called for HTTP streams.
3330
3331* .. js:function:: FILTER.http_end(flt, txn, http_msg)
3332
3333 Called after the HTTP payload analysis on the HTTP message **http_msg**. This
3334 callback functions is only called for HTTP streams.
3335
3336* .. js:function:: FILTER.tcp_payload(flt, txn, chn)
3337
3338 Called during the TCP payload analysis on the channel **chn**.
3339
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02003340Here is a full example:
Christopher Faulet5a2c6612021-08-15 20:35:25 +02003341
3342.. code-block:: lua
3343
3344 Trace = {}
3345 Trace.id = "Lua trace filter"
3346 Trace.flags = filter.FLT_CFG_FL_HTX;
3347 Trace.__index = Trace
3348
3349 function Trace:new()
3350 local trace = {}
3351 setmetatable(trace, Trace)
3352 trace.req_len = 0
3353 trace.res_len = 0
3354 return trace
3355 end
3356
3357 function Trace:start_analyze(txn, chn)
3358 if chn:is_resp() then
3359 print("Start response analysis")
3360 else
3361 print("Start request analysis")
3362 end
3363 filter.register_data_filter(self, chn)
3364 end
3365
3366 function Trace:end_analyze(txn, chn)
3367 if chn:is_resp() then
3368 print("End response analysis: "..self.res_len.." bytes filtered")
3369 else
3370 print("End request analysis: "..self.req_len.." bytes filtered")
3371 end
3372 end
3373
3374 function Trace:http_headers(txn, http_msg)
3375 stline = http_msg:get_stline()
3376 if http_msg.channel:is_resp() then
3377 print("response:")
3378 print(stline.version.." "..stline.code.." "..stline.reason)
3379 else
3380 print("request:")
3381 print(stline.method.." "..stline.uri.." "..stline.version)
3382 end
3383
3384 for n, hdrs in pairs(http_msg:get_headers()) do
3385 for i,v in pairs(hdrs) do
3386 print(n..": "..v)
3387 end
3388 end
3389 return filter.CONTINUE
3390 end
3391
3392 function Trace:http_payload(txn, http_msg)
3393 body = http_msg:body(-20000)
3394 if http_msg.channel:is_resp() then
3395 self.res_len = self.res_len + body:len()
3396 else
3397 self.req_len = self.req_len + body:len()
3398 end
3399 end
3400
3401 core.register_filter("trace", Trace, function(trace, args)
3402 return trace
3403 end)
3404
3405..
3406
3407.. _httpmessage_class:
3408
3409HTTPMessage class
3410===================
3411
3412.. js:class:: HTTPMessage
3413
3414 **context**: filter
3415
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02003416 This class contains all functions to manipulate a HTTP message. For now, this
Christopher Faulet5a2c6612021-08-15 20:35:25 +02003417 class is only available from a filter context.
3418
3419.. js:function:: HTTPMessage.add_header(http_msg, name, value)
3420
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02003421 Appends a HTTP header field in the HTTP message **http_msg** whose name is
Christopher Faulet5a2c6612021-08-15 20:35:25 +02003422 specified in **name** and whose value is defined in **value**.
3423
3424 :param class_httpmessage http_msg: The manipulated HTTP message.
3425 :param string name: The header name.
3426 :param string value: The header value.
3427
3428.. js:function:: HTTPMessage.append(http_msg, string)
3429
3430 This function copies the string **string** at the end of incoming data of the
3431 HTTP message **http_msg**. The function returns the copied length on success
3432 or -1 if data cannot be copied.
3433
3434 Same that :js:func:`HTTPMessage.insert(http_msg, string, http_msg:input())`.
3435
3436 :param class_httpmessage http_msg: The manipulated HTTP message.
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02003437 :param string string: The data to copy at the end of incoming data.
Christopher Faulet5a2c6612021-08-15 20:35:25 +02003438 :returns: an integer containing the amount of bytes copied or -1.
3439
3440.. js:function:: HTTPMessage.body(http_msgl[, offset[, length]])
3441
3442 This function returns **length** bytes of incoming data from the HTTP message
3443 **http_msg**, starting at the offset **offset**. The data are not removed from
3444 the buffer.
3445
3446 By default, if no length is provided, all incoming data found, starting at the
3447 given offset, are returned. If **length** is set to -1, the function tries to
3448 retrieve a maximum of data. Because it is called in the filter context, it
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02003449 never yield. Not providing an offset is the same as setting it to 0. A
Ilya Shipitsinff0f2782021-08-22 22:18:07 +05003450 positive offset is relative to the beginning of incoming data of the
Christopher Faulet5a2c6612021-08-15 20:35:25 +02003451 http_message buffer while negative offset is relative to their end.
3452
3453 If there is no incoming data and the HTTP message can't receive more data, a 'nil'
3454 value is returned.
3455
3456 :param class_httpmessage http_msg: The manipulated HTTP message.
3457 :param integer offset: *optional* The offset in incoming data to start to get
3458 data. 0 by default. May be negative to be relative to
3459 the end of incoming data.
3460 :param integer length: *optional* The expected length of data to retrieve. All
3461 incoming data by default. May be set to -1 to get a
3462 maximum of data.
3463 :returns: a string containing the data found or nil.
3464
3465.. js:function:: HTTPMessage.eom(http_msg)
3466
3467 This function returns true if the end of message is reached for the HTTP
3468 message **http_msg**.
3469
3470 :param class_httpmessage http_msg: The manipulated HTTP message.
3471 :returns: an integer containing the amount of available bytes.
3472
3473.. js:function:: HTTPMessage.del_header(http_msg, name)
3474
3475 Removes all HTTP header fields in the HTTP message **http_msg** whose name is
3476 specified in **name**.
3477
3478 :param class_httpmessage http_msg: The manipulated http message.
3479 :param string name: The header name.
3480
3481.. js:function:: HTTPMessage.get_headers(http_msg)
3482
3483 Returns a table containing all the headers of the HTTP message **http_msg**.
3484
3485 :param class_httpmessage http_msg: The manipulated http message.
3486 :returns: table of headers.
3487
3488 This is the form of the returned table:
3489
3490.. code-block:: lua
3491
3492 http_msg:get_headers()['<header-name>'][<header-index>] = "<header-value>"
3493
3494 local hdr = http_msg:get_headers()
3495 hdr["host"][0] = "www.test.com"
3496 hdr["accept"][0] = "audio/basic q=1"
3497 hdr["accept"][1] = "audio/*, q=0.2"
3498 hdr["accept"][2] = "*.*, q=0.1"
3499..
3500
3501.. js:function:: HTTPMessage.get_stline(http_msg)
3502
3503 Returns a table containing the start-line of the HTTP message **http_msg**.
3504
3505 :param class_httpmessage http_msg: The manipulated http message.
3506 :returns: the start-line.
3507
3508 This is the form of the returned table:
3509
3510.. code-block:: lua
3511
3512 -- for the request :
3513 {"method" = string, "uri" = string, "version" = string}
3514
3515 -- for the response:
3516 {"version" = string, "code" = string, "reason" = string}
3517..
3518
3519.. js:function:: HTTPMessage.forward(http_msg, length)
3520
3521 This function forwards **length** bytes of data from the HTTP message
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02003522 **http_msg**. Because it is called in the filter context, it never yields. Only
Christopher Faulet5a2c6612021-08-15 20:35:25 +02003523 available incoming data may be forwarded, event if the requested length
3524 exceeds the available amount of incoming data. It returns the amount of data
3525 forwarded.
3526
3527 :param class_httpmessage http_msg: The manipulated HTTP message.
3528 :param integer int: The amount of data to forward.
3529
3530.. js:function:: HTTPMessage.input(http_msg)
3531
3532 This function returns the length of incoming data in the HTTP message
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02003533 **http_msg** from the filter point of view.
Christopher Faulet5a2c6612021-08-15 20:35:25 +02003534
3535 :param class_httpmessage http_msg: The manipulated HTTP message.
3536 :returns: an integer containing the amount of available bytes.
3537
3538.. js:function:: HTTPMessage.insert(http_msg, string[, offset])
3539
3540 This function copies the string **string** at the offset **offset** in
3541 incoming data of the HTTP message **http_msg**. The function returns the
3542 copied length on success or -1 if data cannot be copied.
3543
3544 By default, if no offset is provided, the string is copied in front of
Ilya Shipitsinff0f2782021-08-22 22:18:07 +05003545 incoming data. A positive offset is relative to the beginning of incoming data
Christopher Faulet5a2c6612021-08-15 20:35:25 +02003546 of the HTTP message while negative offset is relative to their end.
3547
3548 :param class_httpmessage http_msg: The manipulated HTTP message.
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02003549 :param string string: The data to copy into incoming data.
3550 :param integer offset: *optional* The offset in incoming data where to copy
Christopher Faulet5a2c6612021-08-15 20:35:25 +02003551 data. 0 by default. May be negative to be relative to
3552 the end of incoming data.
3553 :returns: an integer containing the amount of bytes copied or -1.
3554
3555.. js:function:: HTTPMessage.is_full(http_msg)
3556
3557 This function returns true if the HTTP message **http_msg** is full.
3558
3559 :param class_httpmessage http_msg: The manipulated HTTP message.
3560 :returns: a boolean
3561
3562.. js:function:: HTTPMessage.is_resp(http_msg)
3563
3564 This function returns true if the HTTP message **http_msg** is the response
3565 one.
3566
3567 :param class_httpmessage http_msg: The manipulated HTTP message.
3568 :returns: a boolean
3569
3570.. js:function:: HTTPMessage.may_recv(http_msg)
3571
3572 This function returns true if the HTTP message **http_msg** may still receive
3573 data.
3574
3575 :param class_httpmessage http_msg: The manipulated HTTP message.
3576 :returns: a boolean
3577
3578.. js:function:: HTTPMessage.output(http_msg)
3579
3580 This function returns the length of outgoing data of the HTTP message
3581 **http_msg**.
3582
3583 :param class_httpmessage http_msg: The manipulated HTTP message.
3584 :returns: an integer containing the amount of available bytes.
3585
3586.. js:function:: HTTPMessage.prepend(http_msg, string)
3587
3588 This function copies the string **string** in front of incoming data of the
3589 HTTP message **http_msg**. The function returns the copied length on success
3590 or -1 if data cannot be copied.
3591
3592 Same that :js:func:`HTTPMessage.insert(http_msg, string, 0)`.
3593
3594 :param class_httpmessage http_msg: The manipulated HTTP message.
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02003595 :param string string: The data to copy in front of incoming data.
Christopher Faulet5a2c6612021-08-15 20:35:25 +02003596 :returns: an integer containing the amount of bytes copied or -1.
3597
3598.. js:function:: HTTPMessage.remove(http_msg[, offset[, length]])
3599
3600 This function removes **length** bytes of incoming data of the HTTP message
3601 **http_msg**, starting at offset **offset**. This function returns number of
3602 bytes removed on success.
3603
3604 By default, if no length is provided, all incoming data, starting at the given
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02003605 offset, are removed. Not providing an offset is the same that setting it
Ilya Shipitsinff0f2782021-08-22 22:18:07 +05003606 to 0. A positive offset is relative to the beginning of incoming data of the
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02003607 HTTP message while negative offset is relative to the end.
Christopher Faulet5a2c6612021-08-15 20:35:25 +02003608
3609 :param class_httpmessage http_msg: The manipulated HTTP message.
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02003610 :param integer offset: *optional* The offset in incoming data where to start
Christopher Faulet5a2c6612021-08-15 20:35:25 +02003611 to remove data. 0 by default. May be negative to
3612 be relative to the end of incoming data.
3613 :param integer length: *optional* The length of data to remove. All incoming
3614 data by default.
3615 :returns: an integer containing the amount of bytes removed.
3616
3617.. js:function:: HTTPMessage.rep_header(http_msg, name, regex, replace)
3618
3619 Matches the regular expression in all occurrences of header field **name**
3620 according to regex **regex**, and replaces them with the string **replace**.
3621 The replacement value can contain back references like \1, \2, ... This
3622 function acts on whole header lines, regardless of the number of values they
3623 may contain.
3624
3625 :param class_httpmessage http_msg: The manipulated HTTP message.
3626 :param string name: The header name.
3627 :param string regex: The match regular expression.
3628 :param string replace: The replacement value.
3629
3630.. js:function:: HTTPMessage.rep_value(http_msg, name, regex, replace)
3631
3632 Matches the regular expression on every comma-delimited value of header field
3633 **name** according to regex **regex**, and replaces them with the string
3634 **replace**. The replacement value can contain back references like \1, \2,
3635 ...
3636
3637 :param class_httpmessage http_msg: The manipulated HTTP message.
3638 :param string name: The header name.
3639 :param string regex: The match regular expression.
3640 :param string replace: The replacement value.
3641
3642.. js:function:: HTTPMessage.send(http_msg, string)
3643
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02003644 This function requires immediate send of the string **string**. It means the
Ilya Shipitsinff0f2782021-08-22 22:18:07 +05003645 string is copied at the beginning of incoming data of the HTTP message
Christopher Faulet5a2c6612021-08-15 20:35:25 +02003646 **http_msg** and immediately forwarded. Because it is called in the filter
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02003647 context, it never yields.
Christopher Faulet5a2c6612021-08-15 20:35:25 +02003648
3649 :param class_httpmessage http_msg: The manipulated HTTP message.
3650 :param string string: The data to send.
3651 :returns: an integer containing the amount of bytes copied or -1.
3652
3653.. js:function:: HTTPMessage.set(http_msg, string[, offset[, length]])
3654
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02003655 This function replaces **length** bytes of incoming data of the HTTP message
Christopher Faulet5a2c6612021-08-15 20:35:25 +02003656 **http_msg**, starting at offset **offset**, by the string **string**. The
3657 function returns the copied length on success or -1 if data cannot be copied.
3658
3659 By default, if no length is provided, all incoming data, starting at the given
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02003660 offset, are replaced. Not providing an offset is the same as setting it
Ilya Shipitsinff0f2782021-08-22 22:18:07 +05003661 to 0. A positive offset is relative to the beginning of incoming data of the
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02003662 HTTP message while negative offset is relative to the end.
Christopher Faulet5a2c6612021-08-15 20:35:25 +02003663
3664 :param class_httpmessage http_msg: The manipulated HTTP message.
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02003665 :param string string: The data to copy into incoming data.
3666 :param integer offset: *optional* The offset in incoming data where to start
Christopher Faulet5a2c6612021-08-15 20:35:25 +02003667 the data replacement. 0 by default. May be negative to
3668 be relative to the end of incoming data.
3669 :param integer length: *optional* The length of data to replace. All incoming
3670 data by default.
3671 :returns: an integer containing the amount of bytes copied or -1.
3672
3673.. js:function:: HTTPMessage.set_eom(http_msg)
3674
3675 This function set the end of message for the HTTP message **http_msg**.
3676
3677 :param class_httpmessage http_msg: The manipulated HTTP message.
3678
3679.. js:function:: HTTPMessage.set_header(http_msg, name, value)
3680
3681 This variable replace all occurrence of all header matching the name **name**,
3682 by only one containing the value **value**.
3683
3684 :param class_httpmessage http_msg: The manipulated HTTP message.
3685 :param string name: The header name.
3686 :param string value: The header value.
3687
3688 This function does the same work as the following code:
3689
3690.. code-block:: lua
3691
3692 http_msg:del_header("header")
3693 http_msg:add_header("header", "value")
3694..
3695
3696.. js:function:: HTTPMessage.set_method(http_msg, method)
3697
3698 Rewrites the request method with the string **method**. The HTTP message
3699 **http_msg** must be the request.
3700
3701 :param class_httpmessage http_msg: The manipulated HTTP message.
3702 :param string method: The new method.
3703
3704.. js:function:: HTTPMessage.set_path(http_msg, path)
3705
3706 Rewrites the request path with the string **path**. The HTTP message
3707 **http_msg** must be the request.
3708
3709 :param class_httpmessage http_msg: The manipulated HTTP message.
3710 :param string method: The new method.
3711
3712.. js:function:: HTTPMessage.set_query(http_msg, query)
3713
3714 Rewrites the request's query string which appears after the first question
3715 mark ("?") with the string **query**. The HTTP message **http_msg** must be
3716 the request.
3717
3718 :param class_httpmessage http_msg: The manipulated HTTP message.
3719 :param string query: The new query.
3720
3721.. js:function:: HTTPMessage.set_status(http_msg, status[, reason])
3722
Ilya Shipitsinff0f2782021-08-22 22:18:07 +05003723 Rewrites the response status code with the integer **code** and optional the
Christopher Faulet5a2c6612021-08-15 20:35:25 +02003724 reason **reason**. If no custom reason is provided, it will be generated from
3725 the status. The HTTP message **http_msg** must be the response.
3726
3727 :param class_httpmessage http_msg: The manipulated HTTP message.
3728 :param integer status: The new response status code.
3729 :param string reason: The new response reason (optional).
3730
3731.. js:function:: HTTPMessage.set_uri(http_msg, uri)
3732
3733 Rewrites the request URI with the string **uri**. The HTTP message
3734 **http_msg** must be the request.
3735
3736 :param class_httpmessage http_msg: The manipulated HTTP message.
3737 :param string uri: The new uri.
3738
3739.. js:function:: HTTPMessage.unset_eom(http_msg)
3740
3741 This function remove the end of message for the HTTP message **http_msg**.
3742
3743 :param class_httpmessage http_msg: The manipulated HTTP message.
3744
William Lallemand10cea5c2022-03-30 16:02:43 +02003745.. _CertCache_class:
3746
3747CertCache class
3748================
3749
3750.. js:class:: CertCache
3751
3752 This class allows to update an SSL certificate file in the memory of the
3753 current HAProxy process. It will do the same as "set ssl cert" + "commit ssl
3754 cert" over the HAProxy CLI.
3755
3756.. js:function:: CertCache.set(certificate)
3757
3758 This function updates a certificate in memory.
3759
3760 :param table certificate: A table containing the fields to update.
3761 :param string certificate.filename: The mandatory filename of the certificate
3762 to update, it must already exist in memory.
3763 :param string certificate.crt: A certificate in the PEM format. It can also
3764 contain a private key.
3765 :param string certificate.key: A private key in the PEM format.
3766 :param string certificate.ocsp: An OCSP response in base64. (cf management.txt)
3767 :param string certificate.issuer: The certificate of the OCSP issuer.
3768 :param string certificate.sctl: An SCTL file.
3769
3770.. code-block:: lua
3771
3772 CertCache.set{filename="certs/localhost9994.pem.rsa", crt=crt}
3773
3774
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01003775External Lua libraries
3776======================
3777
3778A lot of useful lua libraries can be found here:
3779
3780* `https://lua-toolbox.com/ <https://lua-toolbox.com/>`_
3781
Ilya Shipitsin2075ca82020-03-06 23:22:22 +05003782Redis client library:
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01003783
3784* `https://github.com/nrk/redis-lua <https://github.com/nrk/redis-lua>`_
3785
Aurelien DARRAGON53901f42022-10-13 19:49:42 +02003786This is an example about the usage of the Redis library within HAProxy. Note that
3787each call to any function of this library can throw an error if the socket
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01003788connection fails.
3789
3790.. code-block:: lua
3791
3792 -- load the redis library
3793 local redis = require("redis");
3794
3795 function do_something(txn)
3796
3797 -- create and connect new tcp socket
3798 local tcp = core.tcp();
3799 tcp:settimeout(1);
3800 tcp:connect("127.0.0.1", 6379);
3801
3802 -- use the redis library with this new socket
3803 local client = redis.connect({socket=tcp});
3804 client:ping();
3805
3806 end
3807
3808OpenSSL:
3809
3810* `http://mkottman.github.io/luacrypto/index.html
3811 <http://mkottman.github.io/luacrypto/index.html>`_
3812
3813* `https://github.com/brunoos/luasec/wiki
3814 <https://github.com/brunoos/luasec/wiki>`_