blob: 847ab521e9d719510657797a8abe75a94649947f [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
157 useful for the controlling the execution flow, registering hooks, manipulating
158 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::
223 if you are declared frontend and backend with the same name, only one of
224 these are 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
Ilya Shipitsin2075ca82020-03-06 23:22:22 +0500371 Returns HAProxy core information. We can found information like the uptime,
Thierry Fourniereea77c02016-03-18 08:47:13 +0100372 the pid, memory pool usage, tasks number, ...
373
Bertrand Jacquin874a35c2018-09-10 21:26:07 +0100374 These information are also returned by the management socket via the command
375 "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
463.. js:function:: core.rfc850_date(date)
464
465 **context**: body, init, task, action
466
Bertrand Jacquin874a35c2018-09-10 21:26:07 +0100467 This function take a string representing http date, and returns an integer
Thierry FOURNIERa78f0372016-12-14 19:04:41 +0100468 containing the corresponding date with a epoch format.
469
470 :param string date: a date http-date formatted
471
472.. js:function:: core.asctime_date(date)
473
474 **context**: body, init, task, action
475
Bertrand Jacquin874a35c2018-09-10 21:26:07 +0100476 This function take a string representing http date, and returns an integer
Thierry FOURNIERa78f0372016-12-14 19:04:41 +0100477 containing the corresponding date with a epoch format.
478
479 :param string date: a date http-date formatted
480
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100481.. js:function:: core.msleep(milliseconds)
482
483 **context**: body, init, task, action
484
485 The `core.msleep()` stops the Lua execution between specified milliseconds.
486
487 :param integer milliseconds: the required milliseconds.
488
Thierry Fournierf61aa632016-02-19 20:56:00 +0100489.. js:attribute:: core.proxies
490
491 **context**: body, init, task, action, sample-fetch, converter
492
Bertrand Jacquin874a35c2018-09-10 21:26:07 +0100493 Proxies is a table containing the list of all proxies declared in the
Patrick Hemmerc6a1d712018-05-01 21:30:41 -0400494 configuration file. The table is indexed by the proxy name, and each entry
495 of the proxies table is an object of type :ref:`proxy_class`.
496
Christopher Faulet1e9b1b62021-08-11 10:14:30 +0200497 .. warning::
498 if you have declared a frontend and backend with the same name, only one of
499 these are listed.
Thierry Fournierf61aa632016-02-19 20:56:00 +0100500
Thierry FOURNIERc5d11c62018-02-12 14:46:54 +0100501.. js:function:: core.register_action(name, actions, func [, nb_args])
Thierry FOURNIER8255a752015-09-23 21:03:35 +0200502
503 **context**: body
504
David Carlier61fdf8b2015-10-02 11:59:38 +0100505 Register a Lua function executed as action. All the registered action can be
Thierry FOURNIER8255a752015-09-23 21:03:35 +0200506 used in HAProxy with the prefix "lua.". An action gets a TXN object class as
507 input.
508
509 :param string name: is the name of the converter.
Willy Tarreau61add3c2015-09-28 15:39:10 +0200510 :param table actions: is a table of string describing the HAProxy actions who
Thierry FOURNIER8255a752015-09-23 21:03:35 +0200511 want to register to. The expected actions are 'tcp-req',
512 'tcp-res', 'http-req' or 'http-res'.
Thierry FOURNIERc5d11c62018-02-12 14:46:54 +0100513 :param integer nb_args: is the expected number of argument for the action.
514 By default the value is 0.
Thierry FOURNIER8255a752015-09-23 21:03:35 +0200515 :param function func: is the Lua function called to work as converter.
516
517 The prototype of the Lua function used as argument is:
518
519.. code-block:: lua
520
Thierry FOURNIERc5d11c62018-02-12 14:46:54 +0100521 function(txn [, arg1 [, arg2]])
Thierry FOURNIER8255a752015-09-23 21:03:35 +0200522..
523
Thierry FOURNIERdc595002015-12-21 11:13:52 +0100524 * **txn** (:ref:`txn_class`): this is a TXN object used for manipulating the
Thierry FOURNIER8255a752015-09-23 21:03:35 +0200525 current request or TCP stream.
526
Bertrand Jacquin874a35c2018-09-10 21:26:07 +0100527 * **argX**: this is argument provided through the HAProxy configuration file.
Thierry FOURNIERc5d11c62018-02-12 14:46:54 +0100528
Bertrand Jacquin874a35c2018-09-10 21:26:07 +0100529 Here, an example of action registration. The action just send an 'Hello world'
Thierry FOURNIER8255a752015-09-23 21:03:35 +0200530 in the logs.
531
532.. code-block:: lua
533
534 core.register_action("hello-world", { "tcp-req", "http-req" }, function(txn)
535 txn:Info("Hello world")
536 end)
537..
538
Willy Tarreau714f3452021-05-09 06:47:26 +0200539 This example code is used in HAProxy configuration like this:
Thierry FOURNIER8255a752015-09-23 21:03:35 +0200540
541::
542
543 frontend tcp_frt
544 mode tcp
545 tcp-request content lua.hello-world
546
547 frontend http_frt
548 mode http
549 http-request lua.hello-world
Thierry FOURNIERc5d11c62018-02-12 14:46:54 +0100550..
551
Bertrand Jacquin874a35c2018-09-10 21:26:07 +0100552 A second example using arguments
Thierry FOURNIERc5d11c62018-02-12 14:46:54 +0100553
554.. code-block:: lua
555
556 function hello_world(txn, arg)
557 txn:Info("Hello world for " .. arg)
558 end
559 core.register_action("hello-world", { "tcp-req", "http-req" }, hello_world, 2)
560..
Thierry FOURNIER8255a752015-09-23 21:03:35 +0200561
Willy Tarreau714f3452021-05-09 06:47:26 +0200562 This example code is used in HAProxy configuration like this:
Thierry FOURNIERc5d11c62018-02-12 14:46:54 +0100563
564::
565
566 frontend tcp_frt
567 mode tcp
568 tcp-request content lua.hello-world everybody
569..
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100570.. js:function:: core.register_converters(name, func)
571
572 **context**: body
573
David Carlier61fdf8b2015-10-02 11:59:38 +0100574 Register a Lua function executed as converter. All the registered converters
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100575 can be used in HAProxy with the prefix "lua.". An converter get a string as
576 input and return a string as output. The registered function can take up to 9
577 values as parameter. All the value are strings.
578
579 :param string name: is the name of the converter.
580 :param function func: is the Lua function called to work as converter.
581
582 The prototype of the Lua function used as argument is:
583
584.. code-block:: lua
585
586 function(str, [p1 [, p2 [, ... [, p5]]]])
587..
588
589 * **str** (*string*): this is the input value automatically converted in
590 string.
591 * **p1** .. **p5** (*string*): this is a list of string arguments declared in
Bertrand Jacquin874a35c2018-09-10 21:26:07 +0100592 the HAProxy configuration file. The number of arguments doesn't exceed 5.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100593 The order and the nature of these is conventionally choose by the
Bertrand Jacquin874a35c2018-09-10 21:26:07 +0100594 developer.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100595
596.. js:function:: core.register_fetches(name, func)
597
598 **context**: body
599
David Carlier61fdf8b2015-10-02 11:59:38 +0100600 Register a Lua function executed as sample fetch. All the registered sample
Bertrand Jacquin874a35c2018-09-10 21:26:07 +0100601 fetch can be used in HAProxy with the prefix "lua.". A Lua sample fetch
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100602 return a string as output. The registered function can take up to 9 values as
603 parameter. All the value are strings.
604
605 :param string name: is the name of the converter.
606 :param function func: is the Lua function called to work as sample fetch.
607
608 The prototype of the Lua function used as argument is:
609
610.. code-block:: lua
611
612 string function(txn, [p1 [, p2 [, ... [, p5]]]])
613..
614
Thierry FOURNIERdc595002015-12-21 11:13:52 +0100615 * **txn** (:ref:`txn_class`): this is the txn object associated with the current
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100616 request.
617 * **p1** .. **p5** (*string*): this is a list of string arguments declared in
Bertrand Jacquin874a35c2018-09-10 21:26:07 +0100618 the HAProxy configuration file. The number of arguments doesn't exceed 5.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100619 The order and the nature of these is conventionally choose by the
Bertrand Jacquin874a35c2018-09-10 21:26:07 +0100620 developer.
621 * **Returns**: A string containing some data, or nil if the value cannot be
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100622 returned now.
623
624 lua example code:
625
626.. code-block:: lua
627
628 core.register_fetches("hello", function(txn)
629 return "hello"
630 end)
631..
632
633 HAProxy example configuration:
634
635::
636
637 frontend example
638 http-request redirect location /%[lua.hello]
639
Christopher Faulet5a2c6612021-08-15 20:35:25 +0200640.. js:function:: core.register_filter(name, Flt, func)
641
642 **context**: body
643
644 Register a Lua function used to declare a filter. All the registered filters
645 can by used in HAProxy with the prefix "lua.".
646
647 :param string name: is the name of the filter.
648 :param table Flt: is a Lua class containing the filter definition (id, flags,
649 callbacks).
650 :param function func: is the Lua function called to create the Lua filter.
651
652 The prototype of the Lua function used as argument is:
653
654.. code-block:: lua
655
656 function(flt, args)
657..
658
659 * **flt** : Is a filter object based on the class provided in
660 :js:func:`core.register_filter()` function.
661
662 * **args**: Is a table of strings containing all arguments provided through
663 the HAProxy configuration file, on the filter line.
664
665 It must return the filter to use or nil to ignore it. Here, an example of
666 filter registration.
667
668.. code-block:: lua
669
670 core.register_filter("my-filter", MyFilter, function(flt, args)
671 flt.args = args -- Save arguments
672 return flt
673 end)
674..
675
676 This example code is used in HAProxy configuration like this:
677
678::
679
680 frontend http
681 mode http
682 filter lua.my-filter arg1 arg2 arg3
683..
684
685 :see: :js:class:`Filter`
686
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +0200687.. js:function:: core.register_service(name, mode, func)
688
689 **context**: body
690
David Carlier61fdf8b2015-10-02 11:59:38 +0100691 Register a Lua function executed as a service. All the registered service can
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +0200692 be used in HAProxy with the prefix "lua.". A service gets an object class as
693 input according with the required mode.
694
695 :param string name: is the name of the converter.
Willy Tarreau61add3c2015-09-28 15:39:10 +0200696 :param string mode: is string describing the required mode. Only 'tcp' or
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +0200697 'http' are allowed.
698 :param function func: is the Lua function called to work as converter.
699
700 The prototype of the Lua function used as argument is:
701
702.. code-block:: lua
703
704 function(applet)
705..
706
Thierry FOURNIERdc595002015-12-21 11:13:52 +0100707 * **applet** *applet* will be a :ref:`applettcp_class` or a
708 :ref:`applethttp_class`. It depends the type of registered applet. An applet
709 registered with the 'http' value for the *mode* parameter will gets a
710 :ref:`applethttp_class`. If the *mode* value is 'tcp', the applet will gets
711 a :ref:`applettcp_class`.
712
Christopher Faulet1e9b1b62021-08-11 10:14:30 +0200713 .. warning::
714 Applets of type 'http' cannot be called from 'tcp-*' rulesets. Only the
715 'http-*' rulesets are authorized, this means that is not possible to call
716 an HTTP applet from a proxy in tcp mode. Applets of type 'tcp' can be
717 called from anywhere.
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +0200718
Bertrand Jacquin874a35c2018-09-10 21:26:07 +0100719 Here, an example of service registration. The service just send an 'Hello world'
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +0200720 as an http response.
721
722.. code-block:: lua
723
Pieter Baauw4d7f7662015-11-08 16:38:08 +0100724 core.register_service("hello-world", "http", function(applet)
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +0200725 local response = "Hello World !"
726 applet:set_status(200)
Pieter Baauw2dcb9bc2015-10-01 22:47:12 +0200727 applet:add_header("content-length", string.len(response))
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +0200728 applet:add_header("content-type", "text/plain")
Pieter Baauw2dcb9bc2015-10-01 22:47:12 +0200729 applet:start_response()
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +0200730 applet:send(response)
731 end)
732..
733
Willy Tarreau714f3452021-05-09 06:47:26 +0200734 This example code is used in HAProxy configuration like this:
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +0200735
736::
737
738 frontend example
739 http-request use-service lua.hello-world
740
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100741.. js:function:: core.register_init(func)
742
743 **context**: body
744
745 Register a function executed after the configuration parsing. This is useful
746 to check any parameters.
747
Pieter Baauw4d7f7662015-11-08 16:38:08 +0100748 :param function func: is the Lua function called to work as initializer.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100749
750 The prototype of the Lua function used as argument is:
751
752.. code-block:: lua
753
754 function()
755..
756
757 It takes no input, and no output is expected.
758
759.. js:function:: core.register_task(func)
760
761 **context**: body, init, task, action, sample-fetch, converter
762
763 Register and start independent task. The task is started when the HAProxy
764 main scheduler starts. For example this type of tasks can be executed to
Thierry FOURNIER2e4893c2015-03-18 13:37:27 +0100765 perform complex health checks.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100766
Pieter Baauw4d7f7662015-11-08 16:38:08 +0100767 :param function func: is the Lua function called to work as initializer.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100768
769 The prototype of the Lua function used as argument is:
770
771.. code-block:: lua
772
773 function()
774..
775
776 It takes no input, and no output is expected.
777
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +0100778.. js:function:: core.register_cli([path], usage, func)
779
780 **context**: body
781
782 Register and start independent task. The task is started when the HAProxy
783 main scheduler starts. For example this type of tasks can be executed to
784 perform complex health checks.
785
786 :param array path: is the sequence of word for which the cli execute the Lua
787 binding.
788 :param string usage: is the usage message displayed in the help.
789 :param function func: is the Lua function called to handle the CLI commands.
790
791 The prototype of the Lua function used as argument is:
792
793.. code-block:: lua
794
795 function(AppletTCP, [arg1, [arg2, [...]]])
796..
797
798 I/O are managed with the :ref:`applettcp_class` object. Args are given as
Bertrand Jacquin874a35c2018-09-10 21:26:07 +0100799 parameter. The args embed the registered path. If the path is declared like
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +0100800 this:
801
802.. code-block:: lua
803
804 core.register_cli({"show", "ssl", "stats"}, "Display SSL stats..", function(applet, arg1, arg2, arg3, arg4, arg5)
805 end)
806..
807
808 And we execute this in the prompt:
809
810.. code-block:: text
811
812 > prompt
813 > show ssl stats all
814..
815
Bertrand Jacquin874a35c2018-09-10 21:26:07 +0100816 Then, arg1, arg2 and arg3 will contains respectively "show", "ssl" and "stats".
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +0100817 arg4 will contain "all". arg5 contains nil.
818
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100819.. js:function:: core.set_nice(nice)
820
821 **context**: task, action, sample-fetch, converter
822
823 Change the nice of the current task or current session.
Thierry FOURNIER2e4893c2015-03-18 13:37:27 +0100824
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100825 :param integer nice: the nice value, it must be between -1024 and 1024.
826
827.. js:function:: core.set_map(filename, key, value)
828
829 **context**: init, task, action, sample-fetch, converter
830
Bertrand Jacquin874a35c2018-09-10 21:26:07 +0100831 Set the value *value* associated to the key *key* in the map referenced by
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100832 *filename*.
833
Thierry FOURNIERdc595002015-12-21 11:13:52 +0100834 :param string filename: the Map reference
835 :param string key: the key to set or replace
836 :param string value: the associated value
837
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100838.. js:function:: core.sleep(int seconds)
839
840 **context**: body, init, task, action
841
842 The `core.sleep()` functions stop the Lua execution between specified seconds.
843
844 :param integer seconds: the required seconds.
845
846.. js:function:: core.tcp()
847
848 **context**: init, task, action
849
850 This function returns a new object of a *socket* class.
851
Thierry FOURNIERdc595002015-12-21 11:13:52 +0100852 :returns: A :ref:`socket_class` object.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100853
William Lallemand00a15022021-11-19 16:02:44 +0100854.. js:function:: core.httpclient()
855
856 **context**: init, task, action
857
858 This function returns a new object of a *httpclient* class.
859
860 :returns: A :ref:`httpclient_class` object.
861
Thierry Fournier1de16592016-01-27 09:49:07 +0100862.. js:function:: core.concat()
863
864 **context**: body, init, task, action, sample-fetch, converter
865
Bertrand Jacquin874a35c2018-09-10 21:26:07 +0100866 This function returns a new concat object.
Thierry Fournier1de16592016-01-27 09:49:07 +0100867
868 :returns: A :ref:`concat_class` object.
869
Thierry FOURNIER0a99b892015-08-26 00:14:17 +0200870.. js:function:: core.done(data)
871
872 **context**: body, init, task, action, sample-fetch, converter
873
874 :param any data: Return some data for the caller. It is useful with
875 sample-fetches and sample-converters.
876
877 Immediately stops the current Lua execution and returns to the caller which
878 may be a sample fetch, a converter or an action and returns the specified
Thierry Fournier4234dbd2020-11-28 13:18:23 +0100879 value (ignored for actions and init). It is used when the LUA process finishes
880 its work and wants to give back the control to HAProxy without executing the
Thierry FOURNIER0a99b892015-08-26 00:14:17 +0200881 remaining code. It can be seen as a multi-level "return".
882
Thierry FOURNIER486f5a02015-03-16 15:13:03 +0100883.. js:function:: core.yield()
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100884
885 **context**: task, action, sample-fetch, converter
886
887 Give back the hand at the HAProxy scheduler. It is used when the LUA
888 processing consumes a lot of processing time.
889
Thierry FOURNIER / OZON.IO62fec752016-11-10 20:38:11 +0100890.. js:function:: core.parse_addr(address)
891
892 **context**: body, init, task, action, sample-fetch, converter
893
894 :param network: is a string describing an ipv4 or ipv6 address and optionally
895 its network length, like this: "127.0.0.1/8" or "aaaa::1234/32".
896 :returns: a userdata containing network or nil if an error occurs.
897
Bertrand Jacquin874a35c2018-09-10 21:26:07 +0100898 Parse ipv4 or ipv6 addresses and its facultative associated network.
Thierry FOURNIER / OZON.IO62fec752016-11-10 20:38:11 +0100899
900.. js:function:: core.match_addr(addr1, addr2)
901
902 **context**: body, init, task, action, sample-fetch, converter
903
904 :param addr1: is an address created with "core.parse_addr".
905 :param addr2: is an address created with "core.parse_addr".
Bertrand Jacquin874a35c2018-09-10 21:26:07 +0100906 :returns: boolean, true if the network of the addresses match, else returns
Thierry FOURNIER / OZON.IO62fec752016-11-10 20:38:11 +0100907 false.
908
Ilya Shipitsin2075ca82020-03-06 23:22:22 +0500909 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 +0100910 of network is not important.
911
Thierry FOURNIER / OZON.IO8a1027a2016-11-24 20:48:38 +0100912.. js:function:: core.tokenize(str, separators [, noblank])
913
914 **context**: body, init, task, action, sample-fetch, converter
915
916 This function is useful for tokenizing an entry, or splitting some messages.
917 :param string str: The string which will be split.
918 :param string separators: A string containing a list of separators.
919 :param boolean noblank: Ignore empty entries.
920 :returns: an array of string.
921
922 For example:
923
924.. code-block:: lua
925
926 local array = core.tokenize("This function is useful, for tokenizing an entry.", "., ", true)
927 print_r(array)
928..
929
930 Returns this array:
931
932.. code-block:: text
933
934 (table) table: 0x21c01e0 [
935 1: (string) "This"
936 2: (string) "function"
937 3: (string) "is"
938 4: (string) "useful"
939 5: (string) "for"
940 6: (string) "tokenizing"
941 7: (string) "an"
942 8: (string) "entry"
943 ]
944..
945
Thierry Fournierf61aa632016-02-19 20:56:00 +0100946.. _proxy_class:
947
948Proxy class
949============
950
951.. js:class:: Proxy
952
953 This class provides a way for manipulating proxy and retrieving information
954 like statistics.
955
Thierry FOURNIER817e7592017-07-24 14:35:04 +0200956.. js:attribute:: Proxy.name
957
958 Contain the name of the proxy.
959
Baptiste Assmann46c72552017-10-26 21:51:58 +0200960.. js:attribute:: Proxy.uuid
961
962 Contain the unique identifier of the proxy.
963
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +0100964.. js:attribute:: Proxy.servers
965
Patrick Hemmerc6a1d712018-05-01 21:30:41 -0400966 Contain a table with the attached servers. The table is indexed by server
967 name, and each server entry is an object of type :ref:`server_class`.
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +0100968
Adis Nezirovic8878f8e2018-07-13 12:18:33 +0200969.. js:attribute:: Proxy.stktable
970
971 Contains a stick table object attached to the proxy.
972
Thierry Fournierff480422016-02-25 08:36:46 +0100973.. js:attribute:: Proxy.listeners
974
Patrick Hemmerc6a1d712018-05-01 21:30:41 -0400975 Contain a table with the attached listeners. The table is indexed by listener
976 name, and each each listeners entry is an object of type
977 :ref:`listener_class`.
Thierry Fournierff480422016-02-25 08:36:46 +0100978
Thierry Fournierf61aa632016-02-19 20:56:00 +0100979.. js:function:: Proxy.pause(px)
980
981 Pause the proxy. See the management socket documentation for more information.
982
983 :param class_proxy px: A :ref:`proxy_class` which indicates the manipulated
984 proxy.
985
986.. js:function:: Proxy.resume(px)
987
988 Resume the proxy. See the management socket documentation for more
989 information.
990
991 :param class_proxy px: A :ref:`proxy_class` which indicates the manipulated
992 proxy.
993
994.. js:function:: Proxy.stop(px)
995
996 Stop the proxy. See the management socket documentation for more information.
997
998 :param class_proxy px: A :ref:`proxy_class` which indicates the manipulated
999 proxy.
1000
1001.. js:function:: Proxy.shut_bcksess(px)
1002
1003 Kill the session attached to a backup server. See the management socket
1004 documentation for more information.
1005
1006 :param class_proxy px: A :ref:`proxy_class` which indicates the manipulated
1007 proxy.
1008
1009.. js:function:: Proxy.get_cap(px)
1010
1011 Returns a string describing the capabilities of the proxy.
1012
1013 :param class_proxy px: A :ref:`proxy_class` which indicates the manipulated
1014 proxy.
1015 :returns: a string "frontend", "backend", "proxy" or "ruleset".
1016
1017.. js:function:: Proxy.get_mode(px)
1018
1019 Returns a string describing the mode of the current proxy.
1020
1021 :param class_proxy px: A :ref:`proxy_class` which indicates the manipulated
1022 proxy.
1023 :returns: a string "tcp", "http", "health" or "unknown"
1024
1025.. js:function:: Proxy.get_stats(px)
1026
Bertrand Jacquin874a35c2018-09-10 21:26:07 +01001027 Returns a table containing the proxy statistics. The statistics returned are
Thierry Fournierf61aa632016-02-19 20:56:00 +01001028 not the same if the proxy is frontend or a backend.
1029
1030 :param class_proxy px: A :ref:`proxy_class` which indicates the manipulated
1031 proxy.
Patrick Hemmerc6a1d712018-05-01 21:30:41 -04001032 :returns: a key/value table containing stats
Thierry Fournierf61aa632016-02-19 20:56:00 +01001033
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001034.. _server_class:
1035
1036Server class
1037============
1038
Patrick Hemmerc6a1d712018-05-01 21:30:41 -04001039.. js:class:: Server
1040
1041 This class provides a way for manipulating servers and retrieving information.
1042
Patrick Hemmera62ae7e2018-04-29 14:23:48 -04001043.. js:attribute:: Server.name
1044
1045 Contain the name of the server.
1046
1047.. js:attribute:: Server.puid
1048
1049 Contain the proxy unique identifier of the server.
1050
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001051.. js:function:: Server.is_draining(sv)
1052
Patrick Hemmerc6a1d712018-05-01 21:30:41 -04001053 Return true if the server is currently draining sticky connections.
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001054
1055 :param class_server sv: A :ref:`server_class` which indicates the manipulated
1056 server.
1057 :returns: a boolean
1058
Patrick Hemmer32d539f2018-04-29 14:25:46 -04001059.. js:function:: Server.set_maxconn(sv, weight)
1060
1061 Dynamically change the maximum connections of the server. See the management
1062 socket documentation for more information about the format of the string.
1063
1064 :param class_server sv: A :ref:`server_class` which indicates the manipulated
1065 server.
1066 :param string maxconn: A string describing the server maximum connections.
1067
1068.. js:function:: Server.get_maxconn(sv, weight)
1069
1070 This function returns an integer representing the server maximum connections.
1071
1072 :param class_server sv: A :ref:`server_class` which indicates the manipulated
1073 server.
1074 :returns: an integer.
1075
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001076.. js:function:: Server.set_weight(sv, weight)
1077
Patrick Hemmerc6a1d712018-05-01 21:30:41 -04001078 Dynamically change the weight of the server. See the management socket
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001079 documentation for more information about the format of the string.
1080
1081 :param class_server sv: A :ref:`server_class` which indicates the manipulated
1082 server.
1083 :param string weight: A string describing the server weight.
1084
1085.. js:function:: Server.get_weight(sv)
1086
Patrick Hemmerc6a1d712018-05-01 21:30:41 -04001087 This function returns an integer representing the server weight.
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001088
1089 :param class_server sv: A :ref:`server_class` which indicates the manipulated
1090 server.
1091 :returns: an integer.
1092
Joseph C. Sible49bbf522020-05-04 22:20:32 -04001093.. js:function:: Server.set_addr(sv, addr[, port])
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001094
Patrick Hemmerc6a1d712018-05-01 21:30:41 -04001095 Dynamically change the address of the server. See the management socket
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001096 documentation for more information about the format of the string.
1097
1098 :param class_server sv: A :ref:`server_class` which indicates the manipulated
1099 server.
Patrick Hemmerc6a1d712018-05-01 21:30:41 -04001100 :param string addr: A string describing the server address.
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001101
1102.. js:function:: Server.get_addr(sv)
1103
Patrick Hemmerc6a1d712018-05-01 21:30:41 -04001104 Returns a string describing the address of the server.
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001105
1106 :param class_server sv: A :ref:`server_class` which indicates the manipulated
1107 server.
1108 :returns: A string
1109
1110.. js:function:: Server.get_stats(sv)
1111
1112 Returns server statistics.
1113
1114 :param class_server sv: A :ref:`server_class` which indicates the manipulated
1115 server.
Patrick Hemmerc6a1d712018-05-01 21:30:41 -04001116 :returns: a key/value table containing stats
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +01001117
1118.. js:function:: Server.shut_sess(sv)
1119
1120 Shutdown all the sessions attached to the server. See the management socket
1121 documentation for more information about this function.
1122
1123 :param class_server sv: A :ref:`server_class` which indicates the manipulated
1124 server.
1125
1126.. js:function:: Server.set_drain(sv)
1127
1128 Drain sticky sessions. See the management socket documentation for more
1129 information about this function.
1130
1131 :param class_server sv: A :ref:`server_class` which indicates the manipulated
1132 server.
1133
1134.. js:function:: Server.set_maint(sv)
1135
1136 Set maintenance mode. See the management socket documentation for more
1137 information about this function.
1138
1139 :param class_server sv: A :ref:`server_class` which indicates the manipulated
1140 server.
1141
1142.. js:function:: Server.set_ready(sv)
1143
1144 Set normal mode. See the management socket documentation for more information
1145 about this function.
1146
1147 :param class_server sv: A :ref:`server_class` which indicates the manipulated
1148 server.
1149
1150.. js:function:: Server.check_enable(sv)
1151
1152 Enable health checks. See the management socket documentation for more
1153 information about this function.
1154
1155 :param class_server sv: A :ref:`server_class` which indicates the manipulated
1156 server.
1157
1158.. js:function:: Server.check_disable(sv)
1159
1160 Disable health checks. See the management socket documentation for more
1161 information about this function.
1162
1163 :param class_server sv: A :ref:`server_class` which indicates the manipulated
1164 server.
1165
1166.. js:function:: Server.check_force_up(sv)
1167
1168 Force health-check up. See the management socket documentation for more
1169 information about this function.
1170
1171 :param class_server sv: A :ref:`server_class` which indicates the manipulated
1172 server.
1173
1174.. js:function:: Server.check_force_nolb(sv)
1175
1176 Force health-check nolb mode. See the management socket documentation for more
1177 information about this function.
1178
1179 :param class_server sv: A :ref:`server_class` which indicates the manipulated
1180 server.
1181
1182.. js:function:: Server.check_force_down(sv)
1183
1184 Force health-check down. See the management socket documentation for more
1185 information about this function.
1186
1187 :param class_server sv: A :ref:`server_class` which indicates the manipulated
1188 server.
1189
1190.. js:function:: Server.agent_enable(sv)
1191
1192 Enable agent check. See the management socket documentation for more
1193 information about this function.
1194
1195 :param class_server sv: A :ref:`server_class` which indicates the manipulated
1196 server.
1197
1198.. js:function:: Server.agent_disable(sv)
1199
1200 Disable agent check. See the management socket documentation for more
1201 information about this function.
1202
1203 :param class_server sv: A :ref:`server_class` which indicates the manipulated
1204 server.
1205
1206.. js:function:: Server.agent_force_up(sv)
1207
1208 Force agent check up. See the management socket documentation for more
1209 information about this function.
1210
1211 :param class_server sv: A :ref:`server_class` which indicates the manipulated
1212 server.
1213
1214.. js:function:: Server.agent_force_down(sv)
1215
1216 Force agent check down. See the management socket documentation for more
1217 information about this function.
1218
1219 :param class_server sv: A :ref:`server_class` which indicates the manipulated
1220 server.
1221
Thierry Fournierff480422016-02-25 08:36:46 +01001222.. _listener_class:
1223
1224Listener class
1225==============
1226
1227.. js:function:: Listener.get_stats(ls)
1228
1229 Returns server statistics.
1230
1231 :param class_listener ls: A :ref:`listener_class` which indicates the
1232 manipulated listener.
Patrick Hemmerc6a1d712018-05-01 21:30:41 -04001233 :returns: a key/value table containing stats
Thierry Fournierff480422016-02-25 08:36:46 +01001234
Thierry Fournier1de16592016-01-27 09:49:07 +01001235.. _concat_class:
1236
1237Concat class
1238============
1239
1240.. js:class:: Concat
1241
1242 This class provides a fast way for string concatenation. The way using native
1243 Lua concatenation like the code below is slow for some reasons.
1244
1245.. code-block:: lua
1246
1247 str = "string1"
1248 str = str .. ", string2"
1249 str = str .. ", string3"
1250..
1251
1252 For each concatenation, Lua:
1253 * allocate memory for the result,
Ilya Shipitsin2075ca82020-03-06 23:22:22 +05001254 * catenate the two string copying the strings in the new memory block,
Bertrand Jacquin874a35c2018-09-10 21:26:07 +01001255 * free the old memory block containing the string which is no longer used.
Thierry Fournier1de16592016-01-27 09:49:07 +01001256 This process does many memory move, allocation and free. In addition, the
Bertrand Jacquin874a35c2018-09-10 21:26:07 +01001257 memory is not really freed, it is just mark mark as unused and wait for the
Thierry Fournier1de16592016-01-27 09:49:07 +01001258 garbage collector.
1259
Bertrand Jacquin874a35c2018-09-10 21:26:07 +01001260 The Concat class provide an alternative way to concatenate strings. It uses
Thierry Fournier1de16592016-01-27 09:49:07 +01001261 the internal Lua mechanism (it does not allocate memory), but it doesn't copy
1262 the data more than once.
1263
1264 On my computer, the following loops spends 0.2s for the Concat method and
1265 18.5s for the pure Lua implementation. So, the Concat class is about 1000x
1266 faster than the embedded solution.
1267
1268.. code-block:: lua
1269
1270 for j = 1, 100 do
1271 c = core.concat()
1272 for i = 1, 20000 do
1273 c:add("#####")
1274 end
1275 end
1276..
1277
1278.. code-block:: lua
1279
1280 for j = 1, 100 do
1281 c = ""
1282 for i = 1, 20000 do
1283 c = c .. "#####"
1284 end
1285 end
1286..
1287
1288.. js:function:: Concat.add(concat, string)
1289
1290 This function adds a string to the current concatenated string.
1291
1292 :param class_concat concat: A :ref:`concat_class` which contains the currently
Ilya Shipitsin2075ca82020-03-06 23:22:22 +05001293 built string.
Bertrand Jacquin874a35c2018-09-10 21:26:07 +01001294 :param string string: A new string to concatenate to the current built
Thierry Fournier1de16592016-01-27 09:49:07 +01001295 string.
1296
1297.. js:function:: Concat.dump(concat)
1298
Bertrand Jacquin874a35c2018-09-10 21:26:07 +01001299 This function returns the concatenated string.
Thierry Fournier1de16592016-01-27 09:49:07 +01001300
1301 :param class_concat concat: A :ref:`concat_class` which contains the currently
Ilya Shipitsin2075ca82020-03-06 23:22:22 +05001302 built string.
Thierry Fournier1de16592016-01-27 09:49:07 +01001303 :returns: the concatenated string
1304
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001305.. _fetches_class:
1306
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001307Fetches class
1308=============
1309
1310.. js:class:: Fetches
1311
1312 This class contains a lot of internal HAProxy sample fetches. See the
Thierry FOURNIER2e4893c2015-03-18 13:37:27 +01001313 HAProxy "configuration.txt" documentation for more information about her
Bertrand Jacquin874a35c2018-09-10 21:26:07 +01001314 usage. They are the chapters 7.3.2 to 7.3.6.
Thierry FOURNIER2e4893c2015-03-18 13:37:27 +01001315
Christopher Faulet1e9b1b62021-08-11 10:14:30 +02001316 .. warning::
1317 some sample fetches are not available in some context. These limitations
1318 are specified in this documentation when they're useful.
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001319
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01001320 :see: :js:attr:`TXN.f`
1321 :see: :js:attr:`TXN.sf`
Thierry FOURNIER2e4893c2015-03-18 13:37:27 +01001322
1323 Fetches are useful for:
1324
1325 * get system time,
1326 * get environment variable,
1327 * get random numbers,
1328 * known backend status like the number of users in queue or the number of
1329 connections established,
1330 * client information like ip source or destination,
1331 * deal with stick tables,
Ilya Shipitsin2075ca82020-03-06 23:22:22 +05001332 * Established SSL information,
Thierry FOURNIER2e4893c2015-03-18 13:37:27 +01001333 * HTTP information like headers or method.
1334
1335.. code-block:: lua
1336
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001337 function action(txn)
1338 -- Get source IP
1339 local clientip = txn.f:src()
1340 end
Thierry FOURNIER2e4893c2015-03-18 13:37:27 +01001341..
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001342
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001343.. _converters_class:
1344
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001345Converters class
1346================
1347
1348.. js:class:: Converters
1349
1350 This class contains a lot of internal HAProxy sample converters. See the
Thierry FOURNIER2e4893c2015-03-18 13:37:27 +01001351 HAProxy documentation "configuration.txt" for more information about her
1352 usage. Its the chapter 7.3.1.
1353
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01001354 :see: :js:attr:`TXN.c`
1355 :see: :js:attr:`TXN.sc`
Thierry FOURNIER2e4893c2015-03-18 13:37:27 +01001356
1357 Converters provides statefull transformation. They are useful for:
1358
1359 * converting input to base64,
1360 * applying hash on input string (djb2, crc32, sdbm, wt6),
1361 * format date,
1362 * json escape,
Pieter Baauw4d7f7662015-11-08 16:38:08 +01001363 * extracting preferred language comparing two lists,
Thierry FOURNIER2e4893c2015-03-18 13:37:27 +01001364 * turn to lower or upper chars,
1365 * deal with stick tables.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001366
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001367.. _channel_class:
1368
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001369Channel class
1370=============
1371
1372.. js:class:: Channel
1373
Christopher Faulet5a2c6612021-08-15 20:35:25 +02001374 **context**: action, sample-fetch, convert, filter
1375
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001376 HAProxy uses two buffers for the processing of the requests. The first one is
1377 used with the request data (from the client to the server) and the second is
1378 used for the response data (from the server to the client).
1379
1380 Each buffer contains two types of data. The first type is the incoming data
1381 waiting for a processing. The second part is the outgoing data already
1382 processed. Usually, the incoming data is processed, after it is tagged as
1383 outgoing data, and finally it is sent. The following functions provides tools
1384 for manipulating these data in a buffer.
1385
1386 The following diagram shows where the channel class function are applied.
1387
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001388 .. image:: _static/channel.png
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001389
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001390 .. warning::
1391 It is not possible to read from the response in request action, and it is
Boyang Li60cfe8b2022-05-10 18:11:00 +00001392 not possible to read from the request channel in response action.
Christopher Faulet09530392021-06-14 11:43:18 +02001393
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001394 .. warning::
1395 It is forbidden to alter the Channels buffer from HTTP contexts. So only
1396 :js:func:`Channel.input`, :js:func:`Channel.output`,
1397 :js:func:`Channel.may_recv`, :js:func:`Channel.is_full` and
1398 :js:func:`Channel.is_resp` can be called from an HTTP conetext.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001399
Christopher Faulet5a2c6612021-08-15 20:35:25 +02001400 All the functions provided by this class are available in the
1401 **sample-fetches**, **actions** and **filters** contexts. For **filters**,
1402 incoming data (offset and length) are relative to the filter. Some functions
Boyang Li60cfe8b2022-05-10 18:11:00 +00001403 may yield, but only for **actions**. Yield is not possible for
Christopher Faulet5a2c6612021-08-15 20:35:25 +02001404 **sample-fetches**, **converters** and **filters**.
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001405
1406.. js:function:: Channel.append(channel, string)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001407
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001408 This function copies the string **string** at the end of incoming data of the
1409 channel buffer. The function returns the copied length on success or -1 if
1410 data cannot be copied.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001411
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001412 Same that :js:func:`Channel.insert(channel, string, channel:input())`.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001413
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001414 :param class_channel channel: The manipulated Channel.
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001415 :param string string: The data to copied into incoming data.
1416 :returns: an integer containing the amount of bytes copied or -1.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001417
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001418.. js:function:: Channel.data(channel [, offset [, length]])
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001419
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001420 This function returns **length** bytes of incoming data from the channel
1421 buffer, starting at the offset **offset**. The data are not removed from the
1422 buffer.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001423
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001424 By default, if no length is provided, all incoming data found, starting at the
1425 given offset, are returned. If **length** is set to -1, the function tries to
Christopher Faulet5a2c6612021-08-15 20:35:25 +02001426 retrieve a maximum of data and, if called by an action, it yields if
1427 necessary. It also waits for more data if the requested length exceeds the
1428 available amount of incoming data. Do not providing an offset is the same that
Ilya Shipitsinff0f2782021-08-22 22:18:07 +05001429 setting it to 0. A positive offset is relative to the beginning of incoming
Christopher Faulet5a2c6612021-08-15 20:35:25 +02001430 data of the channel buffer while negative offset is relative to their end.
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001431
1432 If there is no incoming data and the channel can't receive more data, a 'nil'
1433 value is returned.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001434
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001435 :param class_channel channel: The manipulated Channel.
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001436 :param integer offset: *optional* The offset in incoming data to start to get
1437 data. 0 by default. May be negative to be relative to
1438 the end of incoming data.
1439 :param integer length: *optional* The expected length of data to retrieve. All
1440 incoming data by default. May be set to -1 to get a
1441 maximum of data.
1442 :returns: a string containing the data found or nil.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001443
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001444.. js:function:: Channel.forward(channel, length)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001445
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001446 This function forwards **length** bytes of data from the channel buffer. If
Christopher Faulet5a2c6612021-08-15 20:35:25 +02001447 the requested length exceeds the available amount of incoming data, and if
1448 called by an action, the function yields, waiting for more data to forward. It
1449 returns the amount of data forwarded.
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001450
1451 :param class_channel channel: The manipulated Channel.
1452 :param integer int: The amount of data to forward.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001453
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001454.. js:function:: Channel.input(channel)
1455
Christopher Faulet5a2c6612021-08-15 20:35:25 +02001456 This function returns the length of incoming data in the channel buffer. When
1457 called by a filter, this value is relative to the filter.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001458
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001459 :param class_channel channel: The manipulated Channel.
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001460 :returns: an integer containing the amount of available bytes.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001461
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001462.. js:function:: Channel.insert(channel, string [, offset])
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001463
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001464 This function copies the string **string** at the offset **offset** in
1465 incoming data of the channel buffer. The function returns the copied length on
1466 success or -1 if data cannot be copied.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001467
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001468 By default, if no offset is provided, the string is copied in front of
Ilya Shipitsinff0f2782021-08-22 22:18:07 +05001469 incoming data. A positive offset is relative to the beginning of incoming data
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001470 of the channel buffer while negative offset is relative to their end.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001471
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001472 :param class_channel channel: The manipulated Channel.
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001473 :param string string: The data to copied into incoming data.
1474 :param integer offset: *optional* The offset in incomding data where to copied
1475 data. 0 by default. May be negative to be relative to
1476 the end of incoming data.
Pieter Baauw4d7f7662015-11-08 16:38:08 +01001477 :returns: an integer containing the amount of bytes copied or -1.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001478
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001479.. js:function:: Channel.is_full(channel)
1480
1481 This function returns true if the channel buffer is full.
1482
Christopher Faulet5a2c6612021-08-15 20:35:25 +02001483 :param class_channel channel: The manipulated Channel.
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001484 :returns: a boolean
1485
1486.. js:function:: Channel.is_resp(channel)
1487
1488 This function returns true if the channel is the response one.
1489
Christopher Faulet5a2c6612021-08-15 20:35:25 +02001490 :param class_channel channel: The manipulated Channel.
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001491 :returns: a boolean
1492
1493.. js:function:: Channel.line(channel [, offset [, length]])
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001494
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001495 This function parses **length** bytes of incoming data of the channel buffer,
1496 starting at offset **offset**, and returns the first line found, including the
Christopher Faulet5a2c6612021-08-15 20:35:25 +02001497 '\\n'. The data are not removed from the buffer. If no line is found, all data
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001498 are returned.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001499
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001500 By default, if no length is provided, all incoming data, starting at the given
1501 offset, are evaluated. If **length** is set to -1, the function tries to
Christopher Faulet5a2c6612021-08-15 20:35:25 +02001502 retrieve a maximum of data and, if called by an action, yields if
1503 necessary. It also waits for more data if the requested length exceeds the
1504 available amount of incoming data. Do not providing an offset is the same that
Ilya Shipitsinff0f2782021-08-22 22:18:07 +05001505 setting it to 0. A positive offset is relative to the beginning of incoming
Christopher Faulet5a2c6612021-08-15 20:35:25 +02001506 data of the channel buffer while negative offset is relative to their end.
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001507
1508 If there is no incoming data and the channel can't receive more data, a 'nil'
1509 value is returned.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001510
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001511 :param class_channel channel: The manipulated Channel.
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001512 :param integer offset: *optional* The offset in incomding data to start to
1513 parse data. 0 by default. May be negative to be
1514 relative to the end of incoming data.
1515 :param integer length: *optional* The length of data to parse. All incoming
1516 data by default. May be set to -1 to get a maximum of
1517 data.
1518 :returns: a string containing the line found or nil.
1519
1520.. js:function:: Channel.may_recv(channel)
1521
1522 This function returns true if the channel may still receive data.
1523
Christopher Faulet5a2c6612021-08-15 20:35:25 +02001524 :param class_channel channel: The manipulated Channel.
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001525 :returns: a boolean
1526
1527.. js:function:: Channel.output(channel)
1528
Christopher Faulet5a2c6612021-08-15 20:35:25 +02001529 This function returns the length of outgoing data of the channel buffer. When
1530 called by a filter, this value is relative to the filter.
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001531
1532 :param class_channel channel: The manipulated Channel.
1533 :returns: an integer containing the amount of available bytes.
1534
1535.. js:function:: Channel.prepend(channel, string)
1536
1537 This function copies the string **string** in front of incoming data of the
1538 channel buffer. The function returns the copied length on success or -1 if
1539 data cannot be copied.
1540
1541 Same that :js:func:`Channel.insert(channel, string, 0)`.
1542
1543 :param class_channel channel: The manipulated Channel.
1544 :param string string: The data to copied into incoming data.
Pieter Baauw4d7f7662015-11-08 16:38:08 +01001545 :returns: an integer containing the amount of bytes copied or -1.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001546
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001547.. js:function:: Channel.remove(channel [, offset [, length]])
1548
1549 This function removes **length** bytes of incoming data of the channel buffer,
1550 starting at offset **offset**. This function returns number of bytes removed
1551 on success.
1552
1553 By default, if no length is provided, all incoming data, starting at the given
1554 offset, are removed. Do not providing an offset is the same that setting it
Ilya Shipitsinff0f2782021-08-22 22:18:07 +05001555 to 0. A positive offset is relative to the beginning of incoming data of the
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001556 channel buffer while negative offset is relative to their end.
1557
1558 :param class_channel channel: The manipulated Channel.
1559 :param integer offset: *optional* The offset in incomding data where to start
1560 to remove data. 0 by default. May be negative to
1561 be relative to the end of incoming data.
1562 :param integer length: *optional* The length of data to remove. All incoming
1563 data by default.
1564 :returns: an integer containing the amount of bytes removed.
1565
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001566.. js:function:: Channel.send(channel, string)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001567
Christopher Faulet5a2c6612021-08-15 20:35:25 +02001568 This function requires immediate send of the string **string**. It means the
Ilya Shipitsinff0f2782021-08-22 22:18:07 +05001569 string is copied at the beginning of incoming data of the channel buffer and
Christopher Faulet5a2c6612021-08-15 20:35:25 +02001570 immediately forwarded. Unless if the connection is close, and if called by an
1571 action, this function yields to copied and forward all the string.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001572
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001573 :param class_channel channel: The manipulated Channel.
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001574 :param string string: The data to send.
Pieter Baauw4d7f7662015-11-08 16:38:08 +01001575 :returns: an integer containing the amount of bytes copied or -1.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001576
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001577.. js:function:: Channel.set(channel, string [, offset [, length]])
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001578
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001579 This function replace **length** bytes of incoming data of the channel buffer,
1580 starting at offset **offset**, by the string **string**. The function returns
1581 the copied length on success or -1 if data cannot be copied.
1582
1583 By default, if no length is provided, all incoming data, starting at the given
1584 offset, are replaced. Do not providing an offset is the same that setting it
Ilya Shipitsinff0f2782021-08-22 22:18:07 +05001585 to 0. A positive offset is relative to the beginning of incoming data of the
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001586 channel buffer while negative offset is relative to their end.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001587
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001588 :param class_channel channel: The manipulated Channel.
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001589 :param string string: The data to copied into incoming data.
1590 :param integer offset: *optional* The offset in incomding data where to start
1591 the data replacement. 0 by default. May be negative to
1592 be relative to the end of incoming data.
1593 :param integer length: *optional* The length of data to replace. All incoming
1594 data by default.
1595 :returns: an integer containing the amount of bytes copied or -1.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001596
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001597.. js:function:: Channel.dup(channel)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001598
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001599 **DEPRECATED**
1600
1601 This function returns all incoming data found in the channel buffer. The data
Boyang Li60cfe8b2022-05-10 18:11:00 +00001602 are not removed from the buffer and can be reprocessed later.
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001603
1604 If there is no incoming data and the channel can't receive more data, a 'nil'
1605 value is returned.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001606
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001607 :param class_channel channel: The manipulated Channel.
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001608 :returns: a string containing all data found or nil.
1609
1610 .. warning::
1611 This function is deprecated. :js:func:`Channel.data()` must be used
1612 instead.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001613
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001614.. js:function:: Channel.get(channel)
1615
1616 **DEPRECATED**
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001617
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001618 This function returns all incoming data found in the channel buffer and remove
1619 them from the buffer.
1620
1621 If there is no incoming data and the channel can't receive more data, a 'nil'
1622 value is returned.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001623
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001624 :param class_channel channel: The manipulated Channel.
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001625 :returns: a string containing all the data found or nil.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001626
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001627 .. warning::
1628 This function is deprecated. :js:func:`Channel.data()` must be used to
1629 retrieve data followed by a call to :js:func:`Channel:remove()` to remove
1630 data.
Thierry FOURNIER / OZON.IO65192f32016-11-07 15:28:40 +01001631
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001632 .. code-block:: lua
Thierry FOURNIER / OZON.IO65192f32016-11-07 15:28:40 +01001633
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001634 local data = chn:data()
1635 chn:remove(0, data:len())
1636
1637 ..
1638
1639.. js:function:: Channel.getline(channel)
1640
1641 **DEPRECATED**
1642
1643 This function returns the first line found in incoming data of the channel
Christopher Faulet5a2c6612021-08-15 20:35:25 +02001644 buffer, including the '\\n'. The returned data are removed from the buffer. If
1645 no line is found, and if called by an action, this function yields to wait for
1646 more data, except if the channel can't receive more data. In this case all
1647 data are returned.
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001648
1649 If there is no incoming data and the channel can't receive more data, a 'nil'
1650 value is returned.
1651
1652 :param class_channel channel: The manipulated Channel.
1653 :returns: a string containing the line found or nil.
1654
1655 .. warning::
Boyang Li60cfe8b2022-05-10 18:11:00 +00001656 This function is deprecated. :js:func:`Channel.line()` must be used to
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001657 retrieve a line followed by a call to :js:func:`Channel:remove()` to remove
1658 data.
1659
1660 .. code-block:: lua
1661
1662 local line = chn:line(0, -1)
1663 chn:remove(0, line:len())
1664
1665 ..
1666
1667.. js:function:: Channel.get_in_len(channel)
1668
Boyang Li60cfe8b2022-05-10 18:11:00 +00001669 **DEPRECATED**
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001670
Christopher Faulet5a2c6612021-08-15 20:35:25 +02001671 This function returns the length of the input part of the buffer. When called
1672 by a filter, this value is relative to the filter.
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001673
1674 :param class_channel channel: The manipulated Channel.
1675 :returns: an integer containing the amount of available bytes.
1676
1677 .. warning::
1678 This function is deprecated. :js:func:`Channel.input()` must be used
1679 instead.
1680
1681.. js:function:: Channel.get_out_len(channel)
1682
Boyang Li60cfe8b2022-05-10 18:11:00 +00001683 **DEPRECATED**
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001684
Christopher Faulet5a2c6612021-08-15 20:35:25 +02001685 This function returns the length of the output part of the buffer. When called
1686 by a filter, this value is relative to the filter.
Christopher Faulet6a79fc12021-08-06 16:02:36 +02001687
1688 :param class_channel channel: The manipulated Channel.
1689 :returns: an integer containing the amount of available bytes.
1690
1691 .. warning::
1692 This function is deprecated. :js:func:`Channel.output()` must be used
1693 instead.
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001694
1695.. _http_class:
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001696
1697HTTP class
1698==========
1699
1700.. js:class:: HTTP
1701
1702 This class contain all the HTTP manipulation functions.
1703
Pieter Baauw386a1272015-08-16 15:26:24 +02001704.. js:function:: HTTP.req_get_headers(http)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001705
Patrick Hemmerc6a1d712018-05-01 21:30:41 -04001706 Returns a table containing all the request headers.
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001707
1708 :param class_http http: The related http object.
Patrick Hemmerc6a1d712018-05-01 21:30:41 -04001709 :returns: table of headers.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01001710 :see: :js:func:`HTTP.res_get_headers`
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001711
Patrick Hemmerc6a1d712018-05-01 21:30:41 -04001712 This is the form of the returned table:
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001713
1714.. code-block:: lua
1715
1716 HTTP:req_get_headers()['<header-name>'][<header-index>] = "<header-value>"
1717
1718 local hdr = HTTP:req_get_headers()
1719 hdr["host"][0] = "www.test.com"
1720 hdr["accept"][0] = "audio/basic q=1"
1721 hdr["accept"][1] = "audio/*, q=0.2"
1722 hdr["accept"][2] = "*/*, q=0.1"
1723..
1724
Pieter Baauw386a1272015-08-16 15:26:24 +02001725.. js:function:: HTTP.res_get_headers(http)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001726
Patrick Hemmerc6a1d712018-05-01 21:30:41 -04001727 Returns a table containing all the response headers.
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001728
1729 :param class_http http: The related http object.
Patrick Hemmerc6a1d712018-05-01 21:30:41 -04001730 :returns: table of headers.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01001731 :see: :js:func:`HTTP.req_get_headers`
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001732
Patrick Hemmerc6a1d712018-05-01 21:30:41 -04001733 This is the form of the returned table:
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001734
1735.. code-block:: lua
1736
1737 HTTP:res_get_headers()['<header-name>'][<header-index>] = "<header-value>"
1738
1739 local hdr = HTTP:req_get_headers()
1740 hdr["host"][0] = "www.test.com"
1741 hdr["accept"][0] = "audio/basic q=1"
1742 hdr["accept"][1] = "audio/*, q=0.2"
1743 hdr["accept"][2] = "*.*, q=0.1"
1744..
1745
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001746.. js:function:: HTTP.req_add_header(http, name, value)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001747
1748 Appends an HTTP header field in the request whose name is
1749 specified in "name" and whose value is defined in "value".
1750
1751 :param class_http http: The related http object.
1752 :param string name: The header name.
1753 :param string value: The header value.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01001754 :see: :js:func:`HTTP.res_add_header`
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001755
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001756.. js:function:: HTTP.res_add_header(http, name, value)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001757
Bertrand Jacquin874a35c2018-09-10 21:26:07 +01001758 Appends an HTTP header field in the response whose name is
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001759 specified in "name" and whose value is defined in "value".
1760
1761 :param class_http http: The related http object.
1762 :param string name: The header name.
1763 :param string value: The header value.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01001764 :see: :js:func:`HTTP.req_add_header`
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001765
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001766.. js:function:: HTTP.req_del_header(http, name)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001767
1768 Removes all HTTP header fields in the request whose name is
1769 specified in "name".
1770
1771 :param class_http http: The related http object.
1772 :param string name: The header name.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01001773 :see: :js:func:`HTTP.res_del_header`
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001774
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001775.. js:function:: HTTP.res_del_header(http, name)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001776
1777 Removes all HTTP header fields in the response whose name is
1778 specified in "name".
1779
1780 :param class_http http: The related http object.
1781 :param string name: The header name.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01001782 :see: :js:func:`HTTP.req_del_header`
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001783
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001784.. js:function:: HTTP.req_set_header(http, name, value)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001785
Bertrand Jacquin874a35c2018-09-10 21:26:07 +01001786 This variable replace all occurrence of all header "name", by only
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001787 one containing the "value".
1788
1789 :param class_http http: The related http object.
1790 :param string name: The header name.
1791 :param string value: The header value.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01001792 :see: :js:func:`HTTP.res_set_header`
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001793
Bertrand Jacquin874a35c2018-09-10 21:26:07 +01001794 This function does the same work as the following code:
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001795
1796.. code-block:: lua
1797
1798 function fcn(txn)
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001799 TXN.http:req_del_header("header")
1800 TXN.http:req_add_header("header", "value")
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001801 end
1802..
1803
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001804.. js:function:: HTTP.res_set_header(http, name, value)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001805
Bertrand Jacquin874a35c2018-09-10 21:26:07 +01001806 This variable replace all occurrence of all header "name", by only
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001807 one containing the "value".
1808
1809 :param class_http http: The related http object.
1810 :param string name: The header name.
1811 :param string value: The header value.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01001812 :see: :js:func:`HTTP.req_rep_header()`
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001813
Pieter Baauw386a1272015-08-16 15:26:24 +02001814.. js:function:: HTTP.req_rep_header(http, name, regex, replace)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001815
1816 Matches the regular expression in all occurrences of header field "name"
1817 according to "regex", and replaces them with the "replace" argument. The
1818 replacement value can contain back references like \1, \2, ... This
1819 function works with the request.
1820
1821 :param class_http http: The related http object.
1822 :param string name: The header name.
1823 :param string regex: The match regular expression.
1824 :param string replace: The replacement value.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01001825 :see: :js:func:`HTTP.res_rep_header()`
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001826
Pieter Baauw386a1272015-08-16 15:26:24 +02001827.. js:function:: HTTP.res_rep_header(http, name, regex, string)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001828
1829 Matches the regular expression in all occurrences of header field "name"
1830 according to "regex", and replaces them with the "replace" argument. The
1831 replacement value can contain back references like \1, \2, ... This
1832 function works with the request.
1833
1834 :param class_http http: The related http object.
1835 :param string name: The header name.
1836 :param string regex: The match regular expression.
1837 :param string replace: The replacement value.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01001838 :see: :js:func:`HTTP.req_rep_header()`
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001839
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001840.. js:function:: HTTP.req_set_method(http, method)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001841
1842 Rewrites the request method with the parameter "method".
1843
1844 :param class_http http: The related http object.
1845 :param string method: The new method.
1846
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001847.. js:function:: HTTP.req_set_path(http, path)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001848
1849 Rewrites the request path with the "path" parameter.
1850
1851 :param class_http http: The related http object.
1852 :param string path: The new path.
1853
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001854.. js:function:: HTTP.req_set_query(http, query)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001855
1856 Rewrites the request's query string which appears after the first question
1857 mark ("?") with the parameter "query".
1858
1859 :param class_http http: The related http object.
1860 :param string query: The new query.
1861
Thierry FOURNIER0d79cf62015-08-26 14:20:58 +02001862.. js:function:: HTTP.req_set_uri(http, uri)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001863
1864 Rewrites the request URI with the parameter "uri".
1865
1866 :param class_http http: The related http object.
1867 :param string uri: The new uri.
1868
Robin H. Johnson52f5db22017-01-01 13:10:52 -08001869.. js:function:: HTTP.res_set_status(http, status [, reason])
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +02001870
Robin H. Johnson52f5db22017-01-01 13:10:52 -08001871 Rewrites the response status code with the parameter "code".
1872
1873 If no custom reason is provided, it will be generated from the status.
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +02001874
1875 :param class_http http: The related http object.
1876 :param integer status: The new response status code.
Robin H. Johnson52f5db22017-01-01 13:10:52 -08001877 :param string reason: The new response reason (optional).
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +02001878
William Lallemand00a15022021-11-19 16:02:44 +01001879.. _httpclient_class:
1880
1881HTTPClient class
1882================
1883
1884.. js:class:: HTTPClient
1885
1886 The httpclient class allows issue of outbound HTTP requests through a simple
1887 API without the knowledge of HAProxy internals.
1888
1889.. js:function:: HTTPClient.get(httpclient, request)
1890.. js:function:: HTTPClient.head(httpclient, request)
1891.. js:function:: HTTPClient.put(httpclient, request)
1892.. js:function:: HTTPClient.post(httpclient, request)
1893.. js:function:: HTTPClient.delete(httpclient, request)
1894
1895 Send an HTTP request and wait for a response. GET, HEAD PUT, POST and DELETE methods can be used.
1896 The HTTPClient will send asynchronously the data and is able to send and receive more than an HAProxy bufsize.
1897
1898
1899 :param class httpclient: Is the manipulated HTTPClient.
1900 :param table request: Is a table containing the parameters of the request that will be send.
1901 :param string request.url: Is a mandatory parameter for the request that contains the URL.
1902 :param string request.body: Is an optional parameter for the request that contains the body to send.
1903 :param table request.headers: Is an optional parameter for the request that contains the headers to send.
William Lallemand18340302022-02-23 15:57:45 +01001904 :param string request.dst: Is an optional parameter for the destination in haproxy address format.
William Lallemandb4a4ef62022-02-23 14:18:16 +01001905 :param integer request.timeout: Optional timeout parameter, set a "timeout server" on the connections.
William Lallemand00a15022021-11-19 16:02:44 +01001906 :returns: Lua table containing the response
1907
1908
1909.. code-block:: lua
1910
1911 local httpclient = core.httpclient()
William Lallemand4f4f2b72022-02-17 20:00:23 +01001912 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 +01001913
1914..
1915
1916.. code-block:: lua
1917
1918 response = {
1919 status = 400,
1920 reason = "Bad request",
1921 headers = {
1922 ["content-type"] = { "text/html" },
1923 ["cache-control"] = { "no-cache", "no-store" },
1924 },
William Lallemand4f4f2b72022-02-17 20:00:23 +01001925 body = "<html><body><h1>invalid request<h1></body></html>",
William Lallemand00a15022021-11-19 16:02:44 +01001926 }
1927..
1928
1929
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001930.. _txn_class:
1931
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001932TXN class
1933=========
1934
1935.. js:class:: TXN
1936
1937 The txn class contain all the functions relative to the http or tcp
1938 transaction (Note than a tcp stream is the same than a tcp transaction, but
1939 an HTTP transaction is not the same than a tcp stream).
1940
1941 The usage of this class permits to retrieve data from the requests, alter it
1942 and forward it.
1943
1944 All the functions provided by this class are available in the context
Christopher Faulet5a2c6612021-08-15 20:35:25 +02001945 **sample-fetches**, **actions** and **filters**.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001946
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001947.. js:attribute:: TXN.c
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001948
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001949 :returns: An :ref:`converters_class`.
1950
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001951 This attribute contains a Converters class object.
1952
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001953.. js:attribute:: TXN.sc
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001954
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001955 :returns: An :ref:`converters_class`.
1956
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001957 This attribute contains a Converters class object. The functions of
1958 this object returns always a string.
1959
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001960.. js:attribute:: TXN.f
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001961
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001962 :returns: An :ref:`fetches_class`.
1963
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001964 This attribute contains a Fetches class object.
1965
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001966.. js:attribute:: TXN.sf
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001967
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001968 :returns: An :ref:`fetches_class`.
1969
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001970 This attribute contains a Fetches class object. The functions of
1971 this object returns always a string.
1972
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001973.. js:attribute:: TXN.req
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001974
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001975 :returns: An :ref:`channel_class`.
1976
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001977 This attribute contains a channel class object for the request buffer.
1978
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001979.. js:attribute:: TXN.res
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001980
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001981 :returns: An :ref:`channel_class`.
1982
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001983 This attribute contains a channel class object for the response buffer.
1984
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001985.. js:attribute:: TXN.http
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001986
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001987 :returns: An :ref:`http_class`.
1988
Bertrand Jacquin874a35c2018-09-10 21:26:07 +01001989 This attribute contains an HTTP class object. It is available only if the
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001990 proxy has the "mode http" enabled.
1991
Christopher Faulet5a2c6612021-08-15 20:35:25 +02001992.. js:attribute:: TXN.http_req
1993
1994 :returns: An :ref:`httpmessage_class`.
1995
1996 This attribute contains the request HTTPMessage class object. It is available
1997 only if the proxy has the "mode http" enabled and only in the **filters**
1998 context.
1999
2000.. js:attribute:: TXN.http_res
2001
2002 :returns: An :ref:`httpmessage_class`.
2003
2004 This attribute contains the response HTTPMessage class object. It is available
2005 only if the proxy has the "mode http" enabled and only in the **filters**
2006 context.
2007
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01002008.. js:function:: TXN.log(TXN, loglevel, msg)
2009
2010 This function sends a log. The log is sent, according with the HAProxy
2011 configuration file, on the default syslog server if it is configured and on
2012 the stderr if it is allowed.
2013
2014 :param class_txn txn: The class txn object containing the data.
Bertrand Jacquin874a35c2018-09-10 21:26:07 +01002015 :param integer loglevel: Is the log level associated with the message. It is a
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01002016 number between 0 and 7.
2017 :param string msg: The log content.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01002018 :see: :js:attr:`core.emerg`, :js:attr:`core.alert`, :js:attr:`core.crit`,
2019 :js:attr:`core.err`, :js:attr:`core.warning`, :js:attr:`core.notice`,
2020 :js:attr:`core.info`, :js:attr:`core.debug` (log level definitions)
2021 :see: :js:func:`TXN.deflog`
2022 :see: :js:func:`TXN.Debug`
2023 :see: :js:func:`TXN.Info`
2024 :see: :js:func:`TXN.Warning`
2025 :see: :js:func:`TXN.Alert`
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01002026
2027.. js:function:: TXN.deflog(TXN, msg)
2028
Bertrand Jacquin874a35c2018-09-10 21:26:07 +01002029 Sends a log line with the default loglevel for the proxy associated with the
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01002030 transaction.
2031
2032 :param class_txn txn: The class txn object containing the data.
2033 :param string msg: The log content.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01002034 :see: :js:func:`TXN.log
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01002035
2036.. js:function:: TXN.Debug(txn, msg)
2037
2038 :param class_txn txn: The class txn object containing the data.
2039 :param string msg: The log content.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01002040 :see: :js:func:`TXN.log`
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01002041
2042 Does the same job than:
2043
2044.. code-block:: lua
2045
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002046 function Debug(txn, msg)
2047 TXN.log(txn, core.debug, msg)
2048 end
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01002049..
2050
2051.. js:function:: TXN.Info(txn, msg)
2052
2053 :param class_txn txn: The class txn object containing the data.
2054 :param string msg: The log content.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01002055 :see: :js:func:`TXN.log`
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01002056
2057.. code-block:: lua
2058
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002059 function Debug(txn, msg)
2060 TXN.log(txn, core.info, msg)
2061 end
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01002062..
2063
2064.. js:function:: TXN.Warning(txn, msg)
2065
2066 :param class_txn txn: The class txn object containing the data.
2067 :param string msg: The log content.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01002068 :see: :js:func:`TXN.log`
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01002069
2070.. code-block:: lua
2071
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002072 function Debug(txn, msg)
2073 TXN.log(txn, core.warning, msg)
2074 end
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01002075..
2076
2077.. js:function:: TXN.Alert(txn, msg)
2078
2079 :param class_txn txn: The class txn object containing the data.
2080 :param string msg: The log content.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01002081 :see: :js:func:`TXN.log`
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01002082
2083.. code-block:: lua
2084
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002085 function Debug(txn, msg)
2086 TXN.log(txn, core.alert, msg)
2087 end
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01002088..
2089
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002090.. js:function:: TXN.get_priv(txn)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002091
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002092 Return Lua data stored in the current transaction (with the `TXN.set_priv()`)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002093 function. If no data are stored, it returns a nil value.
2094
2095 :param class_txn txn: The class txn object containing the data.
Bertrand Jacquin874a35c2018-09-10 21:26:07 +01002096 :returns: the opaque data previously stored, or nil if nothing is
2097 available.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002098
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002099.. js:function:: TXN.set_priv(txn, data)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002100
2101 Store any data in the current HAProxy transaction. This action replace the
2102 old stored data.
2103
2104 :param class_txn txn: The class txn object containing the data.
2105 :param opaque data: The data which is stored in the transaction.
2106
Tim Duesterhus4e172c92020-05-19 13:49:42 +02002107.. js:function:: TXN.set_var(TXN, var, value[, ifexist])
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +02002108
David Carlier61fdf8b2015-10-02 11:59:38 +01002109 Converts a Lua type in a HAProxy type and store it in a variable <var>.
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +02002110
2111 :param class_txn txn: The class txn object containing the data.
2112 :param string var: The variable name according with the HAProxy variable syntax.
Thierry FOURNIER / OZON.IOb210bcc2016-12-12 16:24:16 +01002113 :param type value: The value associated to the variable. The type can be string or
2114 integer.
Tim Duesterhus4e172c92020-05-19 13:49:42 +02002115 :param boolean ifexist: If this parameter is set to a truthy value the variable
2116 will only be set if it was defined elsewhere (i.e. used
Willy Tarreau7978c5c2021-09-07 14:24:07 +02002117 within the configuration). For global variables (using the
2118 "proc" scope), they will only be updated and never created.
2119 It is highly recommended to always set this to true.
Christopher Faulet85d79c92016-11-09 16:54:56 +01002120
2121.. js:function:: TXN.unset_var(TXN, var)
2122
2123 Unset the variable <var>.
2124
2125 :param class_txn txn: The class txn object containing the data.
2126 :param string var: The variable name according with the HAProxy variable syntax.
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +02002127
2128.. js:function:: TXN.get_var(TXN, var)
2129
2130 Returns data stored in the variable <var> converter in Lua type.
2131
2132 :param class_txn txn: The class txn object containing the data.
2133 :param string var: The variable name according with the HAProxy variable syntax.
2134
Christopher Faulet700d9e82020-01-31 12:21:52 +01002135.. js:function:: TXN.reply([reply])
2136
2137 Return a new reply object
2138
2139 :param table reply: A table containing info to initialize the reply fields.
2140 :returns: A :ref:`reply_class` object.
2141
2142 The table used to initialized the reply object may contain following entries :
2143
2144 * status : The reply status code. the code 200 is used by default.
2145 * reason : The reply reason. The reason corresponding to the status code is
2146 used by default.
2147 * headers : An list of headers, indexed by header name. Empty by default. For
2148 a given name, multiple values are possible, stored in an ordered list.
2149 * body : The reply body, empty by default.
2150
2151.. code-block:: lua
2152
2153 local reply = txn:reply{
2154 status = 400,
2155 reason = "Bad request",
2156 headers = {
2157 ["content-type"] = { "text/html" },
2158 ["cache-control"] = {"no-cache", "no-store" }
2159 },
2160 body = "<html><body><h1>invalid request<h1></body></html>"
2161 }
2162..
2163 :see: :js:class:`Reply`
2164
2165.. js:function:: TXN.done(txn[, reply])
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002166
Willy Tarreaubc183a62015-08-28 10:39:11 +02002167 This function terminates processing of the transaction and the associated
Christopher Faulet700d9e82020-01-31 12:21:52 +01002168 session and optionally reply to the client for HTTP sessions.
2169
2170 :param class_txn txn: The class txn object containing the data.
2171 :param class_reply reply: The class reply object to return to the client.
2172
2173 This functions can be used when a critical error is detected or to terminate
Willy Tarreaubc183a62015-08-28 10:39:11 +02002174 processing after some data have been returned to the client (eg: a redirect).
Christopher Faulet700d9e82020-01-31 12:21:52 +01002175 To do so, a reply may be provided. This object is optional and may contain a
2176 status code, a reason, a header list and a body. All these fields are
Christopher Faulet7855b192021-11-09 18:39:51 +01002177 optional. When not provided, the default values are used. By default, with an
2178 empty reply object, an empty HTTP 200 response is returned to the client. If
2179 no reply object is provided, the transaction is terminated without any
2180 reply. If a reply object is provided, it must not exceed the buffer size once
2181 converted into the internal HTTP representing. Because for now there is no
2182 easy way to be sure it fits, it is probably better to keep it reasonably
2183 small.
Christopher Faulet700d9e82020-01-31 12:21:52 +01002184
2185 The reply object may be fully created in lua or the class Reply may be used to
2186 create it.
2187
2188.. code-block:: lua
2189
2190 local reply = txn:reply()
2191 reply:set_status(400, "Bad request")
2192 reply:add_header("content-type", "text/html")
2193 reply:add_header("cache-control", "no-cache")
2194 reply:add_header("cache-control", "no-store")
2195 reply:set_body("<html><body><h1>invalid request<h1></body></html>")
2196 txn:done(reply)
2197..
2198
2199.. code-block:: lua
2200
2201 txn:done{
2202 status = 400,
2203 reason = "Bad request",
2204 headers = {
2205 ["content-type"] = { "text/html" },
2206 ["cache-control"] = { "no-cache", "no-store" },
2207 },
2208 body = "<html><body><h1>invalid request<h1></body></html>"
2209 }
2210..
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002211
Christopher Faulet1e9b1b62021-08-11 10:14:30 +02002212 .. warning::
2213 It not make sense to call this function from sample-fetches. In this case
2214 the behaviour of this one is the same than core.done(): it quit the Lua
2215 execution. The transaction is really aborted only from an action registered
2216 function.
Thierry FOURNIERab00df62016-07-14 11:42:37 +02002217
Christopher Faulet700d9e82020-01-31 12:21:52 +01002218 :see: :js:func:`TXN.reply`, :js:class:`Reply`
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002219
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002220.. js:function:: TXN.set_loglevel(txn, loglevel)
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01002221
2222 Is used to change the log level of the current request. The "loglevel" must
2223 be an integer between 0 and 7.
2224
2225 :param class_txn txn: The class txn object containing the data.
2226 :param integer loglevel: The required log level. This variable can be one of
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01002227 :see: :js:attr:`core.emerg`, :js:attr:`core.alert`, :js:attr:`core.crit`,
2228 :js:attr:`core.err`, :js:attr:`core.warning`, :js:attr:`core.notice`,
2229 :js:attr:`core.info`, :js:attr:`core.debug` (log level definitions)
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01002230
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002231.. js:function:: TXN.set_tos(txn, tos)
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01002232
2233 Is used to set the TOS or DSCP field value of packets sent to the client to
2234 the value passed in "tos" on platforms which support this.
2235
2236 :param class_txn txn: The class txn object containing the data.
2237 :param integer tos: The new TOS os DSCP.
2238
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002239.. js:function:: TXN.set_mark(txn, mark)
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01002240
2241 Is used to set the Netfilter MARK on all packets sent to the client to the
2242 value passed in "mark" on platforms which support it.
2243
2244 :param class_txn txn: The class txn object containing the data.
2245 :param integer mark: The mark value.
2246
Patrick Hemmer268a7072018-05-11 12:52:31 -04002247.. js:function:: TXN.set_priority_class(txn, prio)
2248
2249 This function adjusts the priority class of the transaction. The value should
2250 be within the range -2047..2047. Values outside this range will be
2251 truncated.
2252
2253 See the HAProxy configuration.txt file keyword "http-request" action
2254 "set-priority-class" for details.
2255
2256.. js:function:: TXN.set_priority_offset(txn, prio)
2257
2258 This function adjusts the priority offset of the transaction. The value
2259 should be within the range -524287..524287. Values outside this range will be
2260 truncated.
2261
2262 See the HAProxy configuration.txt file keyword "http-request" action
2263 "set-priority-offset" for details.
2264
Christopher Faulet700d9e82020-01-31 12:21:52 +01002265.. _reply_class:
2266
2267Reply class
2268============
2269
2270.. js:class:: Reply
2271
2272 **context**: action
2273
2274 This class represents an HTTP response message. It provides some methods to
Christopher Faulet7855b192021-11-09 18:39:51 +01002275 enrich it. Once converted into the internal HTTP representation, the response
2276 message must not exceed the buffer size. Because for now there is no
2277 easy way to be sure it fits, it is probably better to keep it reasonably
2278 small.
2279
2280 See tune.bufsize in the configuration manual for dettails.
Christopher Faulet700d9e82020-01-31 12:21:52 +01002281
2282.. code-block:: lua
2283
2284 local reply = txn:reply({status = 400}) -- default HTTP 400 reason-phase used
2285 reply:add_header("content-type", "text/html")
2286 reply:add_header("cache-control", "no-cache")
2287 reply:add_header("cache-control", "no-store")
2288 reply:set_body("<html><body><h1>invalid request<h1></body></html>")
2289..
2290
2291 :see: :js:func:`TXN.reply`
2292
2293.. js:attribute:: Reply.status
2294
2295 The reply status code. By default, the status code is set to 200.
2296
2297 :returns: integer
2298
2299.. js:attribute:: Reply.reason
2300
2301 The reason string describing the status code.
2302
2303 :returns: string
2304
2305.. js:attribute:: Reply.headers
2306
2307 A table indexing all reply headers by name. To each name is associated an
2308 ordered list of values.
2309
2310 :returns: Lua table
2311
2312.. code-block:: lua
2313
2314 {
2315 ["content-type"] = { "text/html" },
2316 ["cache-control"] = {"no-cache", "no-store" },
2317 x_header_name = { "value1", "value2", ... }
2318 ...
2319 }
2320..
2321
2322.. js:attribute:: Reply.body
2323
2324 The reply payload.
2325
2326 :returns: string
2327
2328.. js:function:: Reply.set_status(REPLY, status[, reason])
2329
2330 Set the reply status code and optionally the reason-phrase. If the reason is
2331 not provided, the default reason corresponding to the status code is used.
2332
2333 :param class_reply reply: The related Reply object.
2334 :param integer status: The reply status code.
2335 :param string reason: The reply status reason (optional).
2336
2337.. js:function:: Reply.add_header(REPLY, name, value)
2338
2339 Add a header to the reply object. If the header does not already exist, a new
2340 entry is created with its name as index and a one-element list containing its
2341 value as value. Otherwise, the header value is appended to the ordered list of
2342 values associated to the header name.
2343
2344 :param class_reply reply: The related Reply object.
2345 :param string name: The header field name.
2346 :param string value: The header field value.
2347
2348.. js:function:: Reply.del_header(REPLY, name)
2349
2350 Remove all occurrences of a header name from the reply object.
2351
2352 :param class_reply reply: The related Reply object.
2353 :param string name: The header field name.
2354
2355.. js:function:: Reply.set_body(REPLY, body)
2356
2357 Set the reply payload.
2358
2359 :param class_reply reply: The related Reply object.
2360 :param string body: The reply payload.
2361
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002362.. _socket_class:
2363
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002364Socket class
2365============
2366
2367.. js:class:: Socket
2368
2369 This class must be compatible with the Lua Socket class. Only the 'client'
2370 functions are available. See the Lua Socket documentation:
2371
2372 `http://w3.impa.br/~diego/software/luasocket/tcp.html
2373 <http://w3.impa.br/~diego/software/luasocket/tcp.html>`_
2374
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002375.. js:function:: Socket.close(socket)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002376
2377 Closes a TCP object. The internal socket used by the object is closed and the
2378 local address to which the object was bound is made available to other
2379 applications. No further operations (except for further calls to the close
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002380 method) are allowed on a closed Socket.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002381
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002382 :param class_socket socket: Is the manipulated Socket.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002383
2384 Note: It is important to close all used sockets once they are not needed,
2385 since, in many systems, each socket uses a file descriptor, which are limited
2386 system resources. Garbage-collected objects are automatically closed before
2387 destruction, though.
2388
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002389.. js:function:: Socket.connect(socket, address[, port])
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002390
2391 Attempts to connect a socket object to a remote host.
2392
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002393
2394 In case of error, the method returns nil followed by a string describing the
2395 error. In case of success, the method returns 1.
2396
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002397 :param class_socket socket: Is the manipulated Socket.
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002398 :param string address: can be an IP address or a host name. See below for more
2399 information.
2400 :param integer port: must be an integer number in the range [1..64K].
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002401 :returns: 1 or nil.
2402
Bertrand Jacquin874a35c2018-09-10 21:26:07 +01002403 An address field extension permits to use the connect() function to connect to
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002404 other stream than TCP. The syntax containing a simpleipv4 or ipv6 address is
2405 the basically expected format. This format requires the port.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002406
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002407 Other format accepted are a socket path like "/socket/path", it permits to
Bertrand Jacquin874a35c2018-09-10 21:26:07 +01002408 connect to a socket. Abstract namespaces are supported with the prefix
Joseph Herlant02cedc42018-11-13 19:45:17 -08002409 "abns@", and finally a file descriptor can be passed with the prefix "fd@".
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002410 The prefix "ipv4@", "ipv6@" and "unix@" are also supported. The port can be
Bertrand Jacquin874a35c2018-09-10 21:26:07 +01002411 passed int the string. The syntax "127.0.0.1:1234" is valid. In this case, the
Tim Duesterhus6edab862018-01-06 19:04:45 +01002412 parameter *port* must not be set.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002413
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002414.. js:function:: Socket.connect_ssl(socket, address, port)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002415
2416 Same behavior than the function socket:connect, but uses SSL.
2417
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002418 :param class_socket socket: Is the manipulated Socket.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002419 :returns: 1 or nil.
2420
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002421.. js:function:: Socket.getpeername(socket)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002422
2423 Returns information about the remote side of a connected client object.
2424
2425 Returns a string with the IP address of the peer, followed by the port number
2426 that peer is using for the connection. In case of error, the method returns
2427 nil.
2428
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002429 :param class_socket socket: Is the manipulated Socket.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002430 :returns: a string containing the server information.
2431
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002432.. js:function:: Socket.getsockname(socket)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002433
2434 Returns the local address information associated to the object.
2435
2436 The method returns a string with local IP address and a number with the port.
2437 In case of error, the method returns nil.
2438
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002439 :param class_socket socket: Is the manipulated Socket.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002440 :returns: a string containing the client information.
2441
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002442.. js:function:: Socket.receive(socket, [pattern [, prefix]])
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002443
2444 Reads data from a client object, according to the specified read pattern.
2445 Patterns follow the Lua file I/O format, and the difference in performance
2446 between all patterns is negligible.
2447
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002448 :param class_socket socket: Is the manipulated Socket.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002449 :param string|integer pattern: Describe what is required (see below).
2450 :param string prefix: A string which will be prefix the returned data.
2451 :returns: a string containing the required data or nil.
2452
2453 Pattern can be any of the following:
2454
2455 * **`*a`**: reads from the socket until the connection is closed. No
2456 end-of-line translation is performed;
2457
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002458 * **`*l`**: reads a line of text from the Socket. The line is terminated by a
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002459 LF character (ASCII 10), optionally preceded by a CR character
2460 (ASCII 13). The CR and LF characters are not included in the
2461 returned line. In fact, all CR characters are ignored by the
2462 pattern. This is the default pattern.
2463
2464 * **number**: causes the method to read a specified number of bytes from the
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002465 Socket. Prefix is an optional string to be concatenated to the
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002466 beginning of any received data before return.
2467
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002468 * **empty**: If the pattern is left empty, the default option is `*l`.
2469
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002470 If successful, the method returns the received pattern. In case of error, the
2471 method returns nil followed by an error message which can be the string
2472 'closed' in case the connection was closed before the transmission was
2473 completed or the string 'timeout' in case there was a timeout during the
2474 operation. Also, after the error message, the function returns the partial
2475 result of the transmission.
2476
2477 Important note: This function was changed severely. It used to support
2478 multiple patterns (but I have never seen this feature used) and now it
2479 doesn't anymore. Partial results used to be returned in the same way as
2480 successful results. This last feature violated the idea that all functions
2481 should return nil on error. Thus it was changed too.
2482
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002483.. js:function:: Socket.send(socket, data [, start [, end ]])
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002484
2485 Sends data through client object.
2486
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002487 :param class_socket socket: Is the manipulated Socket.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002488 :param string data: The data that will be sent.
2489 :param integer start: The start position in the buffer of the data which will
2490 be sent.
2491 :param integer end: The end position in the buffer of the data which will
2492 be sent.
2493 :returns: see below.
2494
2495 Data is the string to be sent. The optional arguments i and j work exactly
2496 like the standard string.sub Lua function to allow the selection of a
2497 substring to be sent.
2498
2499 If successful, the method returns the index of the last byte within [start,
2500 end] that has been sent. Notice that, if start is 1 or absent, this is
2501 effectively the total number of bytes sent. In case of error, the method
2502 returns nil, followed by an error message, followed by the index of the last
2503 byte within [start, end] that has been sent. You might want to try again from
2504 the byte following that. The error message can be 'closed' in case the
2505 connection was closed before the transmission was completed or the string
2506 'timeout' in case there was a timeout during the operation.
2507
2508 Note: Output is not buffered. For small strings, it is always better to
2509 concatenate them in Lua (with the '..' operator) and send the result in one
2510 call instead of calling the method several times.
2511
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002512.. js:function:: Socket.setoption(socket, option [, value])
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002513
2514 Just implemented for compatibility, this cal does nothing.
2515
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002516.. js:function:: Socket.settimeout(socket, value [, mode])
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002517
2518 Changes the timeout values for the object. All I/O operations are blocking.
2519 That is, any call to the methods send, receive, and accept will block
2520 indefinitely, until the operation completes. The settimeout method defines a
2521 limit on the amount of time the I/O methods can block. When a timeout time
2522 has elapsed, the affected methods give up and fail with an error code.
2523
2524 The amount of time to wait is specified as the value parameter, in seconds.
2525
Mark Lakes56cc1252018-03-27 09:48:06 +02002526 The timeout modes are not implemented, the only settable timeout is the
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002527 inactivity time waiting for complete the internal buffer send or waiting for
2528 receive data.
2529
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01002530 :param class_socket socket: Is the manipulated Socket.
Bertrand Jacquin874a35c2018-09-10 21:26:07 +01002531 :param float value: The timeout value. Use floating point to specify
Mark Lakes56cc1252018-03-27 09:48:06 +02002532 milliseconds.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002533
Thierry FOURNIER31904272017-10-25 12:59:51 +02002534.. _regex_class:
2535
2536Regex class
2537===========
2538
2539.. js:class:: Regex
2540
2541 This class allows the usage of HAProxy regexes because classic lua doesn't
2542 provides regexes. This class inherits the HAProxy compilation options, so the
2543 regexes can be libc regex, pcre regex or pcre JIT regex.
2544
2545 The expression matching number is limited to 20 per regex. The only available
2546 option is case sensitive.
2547
2548 Because regexes compilation is a heavy process, it is better to define all
2549 your regexes in the **body context** and use it during the runtime.
2550
2551.. code-block:: lua
2552
2553 -- Create the regex
2554 st, regex = Regex.new("needle (..) (...)", true);
2555
2556 -- Check compilation errors
2557 if st == false then
2558 print "error: " .. regex
2559 end
2560
2561 -- Match the regexes
2562 print(regex:exec("Looking for a needle in the haystack")) -- true
2563 print(regex:exec("Lokking for a cat in the haystack")) -- false
2564
2565 -- Extract words
2566 st, list = regex:match("Looking for a needle in the haystack")
2567 print(st) -- true
2568 print(list[1]) -- needle in the
2569 print(list[2]) -- in
2570 print(list[3]) -- the
2571
2572.. js:function:: Regex.new(regex, case_sensitive)
2573
2574 Create and compile a regex.
2575
2576 :param string regex: The regular expression according with the libc or pcre
2577 standard
2578 :param boolean case_sensitive: Match is case sensitive or not.
2579 :returns: boolean status and :ref:`regex_class` or string containing fail reason.
2580
2581.. js:function:: Regex.exec(regex, str)
2582
2583 Execute the regex.
2584
2585 :param class_regex regex: A :ref:`regex_class` object.
2586 :param string str: The input string will be compared with the compiled regex.
2587 :returns: a boolean status according with the match result.
2588
2589.. js:function:: Regex.match(regex, str)
2590
2591 Execute the regex and return matched expressions.
2592
2593 :param class_map map: A :ref:`regex_class` object.
2594 :param string str: The input string will be compared with the compiled regex.
2595 :returns: a boolean status according with the match result, and
2596 a table containing all the string matched in order of declaration.
2597
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002598.. _map_class:
2599
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002600Map class
2601=========
2602
2603.. js:class:: Map
2604
2605 This class permits to do some lookup in HAProxy maps. The declared maps can
Bertrand Jacquin874a35c2018-09-10 21:26:07 +01002606 be modified during the runtime through the HAProxy management socket.
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002607
2608.. code-block:: lua
2609
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002610 default = "usa"
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002611
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002612 -- Create and load map
Thierry FOURNIER4dc71972017-01-28 08:33:08 +01002613 geo = Map.new("geo.map", Map._ip);
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002614
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002615 -- Create new fetch that returns the user country
2616 core.register_fetches("country", function(txn)
2617 local src;
2618 local loc;
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002619
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002620 src = txn.f:fhdr("x-forwarded-for");
2621 if (src == nil) then
2622 src = txn.f:src()
2623 if (src == nil) then
2624 return default;
2625 end
2626 end
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002627
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002628 -- Perform lookup
2629 loc = geo:lookup(src);
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002630
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002631 if (loc == nil) then
2632 return default;
2633 end
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002634
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002635 return loc;
2636 end);
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002637
Thierry FOURNIER4dc71972017-01-28 08:33:08 +01002638.. js:attribute:: Map._int
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002639
2640 See the HAProxy configuration.txt file, chapter "Using ACLs and fetching
Ilya Shipitsin2075ca82020-03-06 23:22:22 +05002641 samples" and subchapter "ACL basics" to understand this pattern matching
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002642 method.
2643
Thierry FOURNIER4dc71972017-01-28 08:33:08 +01002644 Note that :js:attr:`Map.int` is also available for compatibility.
2645
2646.. js:attribute:: Map._ip
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002647
2648 See the HAProxy configuration.txt file, chapter "Using ACLs and fetching
Ilya Shipitsin2075ca82020-03-06 23:22:22 +05002649 samples" and subchapter "ACL basics" to understand this pattern matching
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002650 method.
2651
Thierry FOURNIER4dc71972017-01-28 08:33:08 +01002652 Note that :js:attr:`Map.ip` is also available for compatibility.
2653
2654.. js:attribute:: Map._str
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002655
2656 See the HAProxy configuration.txt file, chapter "Using ACLs and fetching
Ilya Shipitsin2075ca82020-03-06 23:22:22 +05002657 samples" and subchapter "ACL basics" to understand this pattern matching
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002658 method.
2659
Thierry FOURNIER4dc71972017-01-28 08:33:08 +01002660 Note that :js:attr:`Map.str` is also available for compatibility.
2661
2662.. js:attribute:: Map._beg
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002663
2664 See the HAProxy configuration.txt file, chapter "Using ACLs and fetching
Ilya Shipitsin2075ca82020-03-06 23:22:22 +05002665 samples" and subchapter "ACL basics" to understand this pattern matching
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002666 method.
2667
Thierry FOURNIER4dc71972017-01-28 08:33:08 +01002668 Note that :js:attr:`Map.beg` is also available for compatibility.
2669
2670.. js:attribute:: Map._sub
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002671
2672 See the HAProxy configuration.txt file, chapter "Using ACLs and fetching
Ilya Shipitsin2075ca82020-03-06 23:22:22 +05002673 samples" and subchapter "ACL basics" to understand this pattern matching
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002674 method.
2675
Thierry FOURNIER4dc71972017-01-28 08:33:08 +01002676 Note that :js:attr:`Map.sub` is also available for compatibility.
2677
2678.. js:attribute:: Map._dir
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002679
2680 See the HAProxy configuration.txt file, chapter "Using ACLs and fetching
Ilya Shipitsin2075ca82020-03-06 23:22:22 +05002681 samples" and subchapter "ACL basics" to understand this pattern matching
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002682 method.
2683
Thierry FOURNIER4dc71972017-01-28 08:33:08 +01002684 Note that :js:attr:`Map.dir` is also available for compatibility.
2685
2686.. js:attribute:: Map._dom
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002687
2688 See the HAProxy configuration.txt file, chapter "Using ACLs and fetching
Ilya Shipitsin2075ca82020-03-06 23:22:22 +05002689 samples" and subchapter "ACL basics" to understand this pattern matching
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002690 method.
2691
Thierry FOURNIER4dc71972017-01-28 08:33:08 +01002692 Note that :js:attr:`Map.dom` is also available for compatibility.
2693
2694.. js:attribute:: Map._end
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002695
2696 See the HAProxy configuration.txt file, chapter "Using ACLs and fetching
Ilya Shipitsin2075ca82020-03-06 23:22:22 +05002697 samples" and subchapter "ACL basics" to understand this pattern matching
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002698 method.
2699
Thierry FOURNIER4dc71972017-01-28 08:33:08 +01002700.. js:attribute:: Map._reg
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002701
2702 See the HAProxy configuration.txt file, chapter "Using ACLs and fetching
Ilya Shipitsin2075ca82020-03-06 23:22:22 +05002703 samples" and subchapter "ACL basics" to understand this pattern matching
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002704 method.
2705
Thierry FOURNIER4dc71972017-01-28 08:33:08 +01002706 Note that :js:attr:`Map.reg` is also available for compatibility.
2707
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002708
2709.. js:function:: Map.new(file, method)
2710
2711 Creates and load a map.
2712
2713 :param string file: Is the file containing the map.
2714 :param integer method: Is the map pattern matching method. See the attributes
2715 of the Map class.
2716 :returns: a class Map object.
Thierry FOURNIER4dc71972017-01-28 08:33:08 +01002717 :see: The Map attributes: :js:attr:`Map._int`, :js:attr:`Map._ip`,
2718 :js:attr:`Map._str`, :js:attr:`Map._beg`, :js:attr:`Map._sub`,
2719 :js:attr:`Map._dir`, :js:attr:`Map._dom`, :js:attr:`Map._end` and
2720 :js:attr:`Map._reg`.
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002721
2722.. js:function:: Map.lookup(map, str)
2723
2724 Perform a lookup in a map.
2725
2726 :param class_map map: Is the class Map object.
2727 :param string str: Is the string used as key.
2728 :returns: a string containing the result or nil if no match.
2729
2730.. js:function:: Map.slookup(map, str)
2731
2732 Perform a lookup in a map.
2733
2734 :param class_map map: Is the class Map object.
2735 :param string str: Is the string used as key.
2736 :returns: a string containing the result or empty string if no match.
2737
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002738.. _applethttp_class:
2739
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002740AppletHTTP class
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002741================
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002742
2743.. js:class:: AppletHTTP
2744
2745 This class is used with applets that requires the 'http' mode. The http applet
2746 can be registered with the *core.register_service()* function. They are used
2747 for processing an http request like a server in back of HAProxy.
2748
2749 This is an hello world sample code:
2750
2751.. code-block:: lua
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002752
Pieter Baauw4d7f7662015-11-08 16:38:08 +01002753 core.register_service("hello-world", "http", function(applet)
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002754 local response = "Hello World !"
2755 applet:set_status(200)
Pieter Baauw2dcb9bc2015-10-01 22:47:12 +02002756 applet:add_header("content-length", string.len(response))
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002757 applet:add_header("content-type", "text/plain")
Pieter Baauw2dcb9bc2015-10-01 22:47:12 +02002758 applet:start_response()
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002759 applet:send(response)
2760 end)
2761
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002762.. js:attribute:: AppletHTTP.c
2763
2764 :returns: A :ref:`converters_class`
2765
2766 This attribute contains a Converters class object.
2767
2768.. js:attribute:: AppletHTTP.sc
2769
2770 :returns: A :ref:`converters_class`
2771
2772 This attribute contains a Converters class object. The
2773 functions of this object returns always a string.
2774
2775.. js:attribute:: AppletHTTP.f
2776
2777 :returns: A :ref:`fetches_class`
2778
2779 This attribute contains a Fetches class object. Note that the
2780 applet execution place cannot access to a valid HAProxy core HTTP
Ilya Shipitsin2075ca82020-03-06 23:22:22 +05002781 transaction, so some sample fetches related to the HTTP dependent
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002782 values (hdr, path, ...) are not available.
2783
2784.. js:attribute:: AppletHTTP.sf
2785
2786 :returns: A :ref:`fetches_class`
2787
2788 This attribute contains a Fetches class object. The functions of
2789 this object returns always a string. Note that the applet
2790 execution place cannot access to a valid HAProxy core HTTP
Ilya Shipitsin2075ca82020-03-06 23:22:22 +05002791 transaction, so some sample fetches related to the HTTP dependent
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002792 values (hdr, path, ...) are not available.
2793
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01002794.. js:attribute:: AppletHTTP.method
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002795
2796 :returns: string
2797
2798 The attribute method returns a string containing the HTTP
2799 method.
2800
2801.. js:attribute:: AppletHTTP.version
2802
2803 :returns: string
2804
2805 The attribute version, returns a string containing the HTTP
2806 request version.
2807
2808.. js:attribute:: AppletHTTP.path
2809
2810 :returns: string
2811
2812 The attribute path returns a string containing the HTTP
2813 request path.
2814
2815.. js:attribute:: AppletHTTP.qs
2816
2817 :returns: string
2818
2819 The attribute qs returns a string containing the HTTP
2820 request query string.
2821
2822.. js:attribute:: AppletHTTP.length
2823
2824 :returns: integer
2825
2826 The attribute length returns an integer containing the HTTP
2827 body length.
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002828
Thierry FOURNIER841475e2015-12-11 17:10:09 +01002829.. js:attribute:: AppletHTTP.headers
2830
Patrick Hemmerc6a1d712018-05-01 21:30:41 -04002831 :returns: table
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002832
Patrick Hemmerc6a1d712018-05-01 21:30:41 -04002833 The attribute headers returns a table containing the HTTP
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002834 headers. The header names are always in lower case. As the header name can be
2835 encountered more than once in each request, the value is indexed with 0 as
Patrick Hemmerc6a1d712018-05-01 21:30:41 -04002836 first index value. The table have this form:
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002837
2838.. code-block:: lua
2839
2840 AppletHTTP.headers['<header-name>'][<header-index>] = "<header-value>"
2841
2842 AppletHTTP.headers["host"][0] = "www.test.com"
2843 AppletHTTP.headers["accept"][0] = "audio/basic q=1"
2844 AppletHTTP.headers["accept"][1] = "audio/*, q=0.2"
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01002845 AppletHTTP.headers["accept"][2] = "*/*, q=0.1"
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002846..
2847
Robin H. Johnson52f5db22017-01-01 13:10:52 -08002848.. js:function:: AppletHTTP.set_status(applet, code [, reason])
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002849
2850 This function sets the HTTP status code for the response. The allowed code are
2851 from 100 to 599.
2852
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01002853 :param class_AppletHTTP applet: An :ref:`applethttp_class`
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002854 :param integer code: the status code returned to the client.
Robin H. Johnson52f5db22017-01-01 13:10:52 -08002855 :param string reason: the status reason returned to the client (optional).
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002856
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01002857.. js:function:: AppletHTTP.add_header(applet, name, value)
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002858
2859 This function add an header in the response. Duplicated headers are not
2860 collapsed. The special header *content-length* is used to determinate the
2861 response length. If it not exists, a *transfer-encoding: chunked* is set, and
Ilya Shipitsin2075ca82020-03-06 23:22:22 +05002862 all the write from the function *AppletHTTP:send()* become a chunk.
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002863
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01002864 :param class_AppletHTTP applet: An :ref:`applethttp_class`
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002865 :param string name: the header name
2866 :param string value: the header value
2867
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01002868.. js:function:: AppletHTTP.start_response(applet)
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002869
2870 This function indicates to the HTTP engine that it can process and send the
2871 response headers. After this called we cannot add headers to the response; We
2872 cannot use the *AppletHTTP:send()* function if the
2873 *AppletHTTP:start_response()* is not called.
2874
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01002875 :param class_AppletHTTP applet: An :ref:`applethttp_class`
2876
2877.. js:function:: AppletHTTP.getline(applet)
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002878
2879 This function returns a string containing one line from the http body. If the
2880 data returned doesn't contains a final '\\n' its assumed than its the last
2881 available data before the end of stream.
2882
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01002883 :param class_AppletHTTP applet: An :ref:`applethttp_class`
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002884 :returns: a string. The string can be empty if we reach the end of the stream.
2885
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01002886.. js:function:: AppletHTTP.receive(applet, [size])
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002887
2888 Reads data from the HTTP body, according to the specified read *size*. If the
2889 *size* is missing, the function tries to read all the content of the stream
2890 until the end. If the *size* is bigger than the http body, it returns the
Bertrand Jacquin874a35c2018-09-10 21:26:07 +01002891 amount of data available.
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002892
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01002893 :param class_AppletHTTP applet: An :ref:`applethttp_class`
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002894 :param integer size: the required read size.
Ilya Shipitsin11057a32020-06-21 21:18:27 +05002895 :returns: always return a string,the string can be empty is the connection is
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002896 closed.
2897
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01002898.. js:function:: AppletHTTP.send(applet, msg)
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002899
2900 Send the message *msg* on the http request body.
2901
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01002902 :param class_AppletHTTP applet: An :ref:`applethttp_class`
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002903 :param string msg: the message to send.
2904
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01002905.. js:function:: AppletHTTP.get_priv(applet)
2906
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01002907 Return Lua data stored in the current transaction. If no data are stored,
2908 it returns a nil value.
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01002909
2910 :param class_AppletHTTP applet: An :ref:`applethttp_class`
Bertrand Jacquin874a35c2018-09-10 21:26:07 +01002911 :returns: the opaque data previously stored, or nil if nothing is
2912 available.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01002913 :see: :js:func:`AppletHTTP.set_priv`
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01002914
2915.. js:function:: AppletHTTP.set_priv(applet, data)
2916
2917 Store any data in the current HAProxy transaction. This action replace the
2918 old stored data.
2919
2920 :param class_AppletHTTP applet: An :ref:`applethttp_class`
2921 :param opaque data: The data which is stored in the transaction.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01002922 :see: :js:func:`AppletHTTP.get_priv`
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01002923
Tim Duesterhus4e172c92020-05-19 13:49:42 +02002924.. js:function:: AppletHTTP.set_var(applet, var, value[, ifexist])
Thierry FOURNIER / OZON.IOc1edafe2016-12-12 16:25:30 +01002925
2926 Converts a Lua type in a HAProxy type and store it in a variable <var>.
2927
2928 :param class_AppletHTTP applet: An :ref:`applethttp_class`
2929 :param string var: The variable name according with the HAProxy variable syntax.
2930 :param type value: The value associated to the variable. The type ca be string or
2931 integer.
Tim Duesterhus4e172c92020-05-19 13:49:42 +02002932 :param boolean ifexist: If this parameter is set to a truthy value the variable
2933 will only be set if it was defined elsewhere (i.e. used
Willy Tarreau7978c5c2021-09-07 14:24:07 +02002934 within the configuration). For global variables (using the
2935 "proc" scope), they will only be updated and never created.
2936 It is highly recommended to
Tim Duesterhus4e172c92020-05-19 13:49:42 +02002937 always set this to true.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01002938 :see: :js:func:`AppletHTTP.unset_var`
2939 :see: :js:func:`AppletHTTP.get_var`
Thierry FOURNIER / OZON.IOc1edafe2016-12-12 16:25:30 +01002940
2941.. js:function:: AppletHTTP.unset_var(applet, var)
2942
2943 Unset the variable <var>.
2944
2945 :param class_AppletHTTP applet: An :ref:`applethttp_class`
2946 :param string var: The variable name according with the HAProxy variable syntax.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01002947 :see: :js:func:`AppletHTTP.set_var`
2948 :see: :js:func:`AppletHTTP.get_var`
Thierry FOURNIER / OZON.IOc1edafe2016-12-12 16:25:30 +01002949
2950.. js:function:: AppletHTTP.get_var(applet, var)
2951
2952 Returns data stored in the variable <var> converter in Lua type.
2953
2954 :param class_AppletHTTP applet: An :ref:`applethttp_class`
2955 :param string var: The variable name according with the HAProxy variable syntax.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01002956 :see: :js:func:`AppletHTTP.set_var`
2957 :see: :js:func:`AppletHTTP.unset_var`
Thierry FOURNIER / OZON.IOc1edafe2016-12-12 16:25:30 +01002958
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002959.. _applettcp_class:
2960
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002961AppletTCP class
2962===============
2963
2964.. js:class:: AppletTCP
2965
2966 This class is used with applets that requires the 'tcp' mode. The tcp applet
2967 can be registered with the *core.register_service()* function. They are used
2968 for processing a tcp stream like a server in back of HAProxy.
2969
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002970.. js:attribute:: AppletTCP.c
2971
2972 :returns: A :ref:`converters_class`
2973
2974 This attribute contains a Converters class object.
2975
2976.. js:attribute:: AppletTCP.sc
2977
2978 :returns: A :ref:`converters_class`
2979
2980 This attribute contains a Converters class object. The
2981 functions of this object returns always a string.
2982
2983.. js:attribute:: AppletTCP.f
2984
2985 :returns: A :ref:`fetches_class`
2986
2987 This attribute contains a Fetches class object.
2988
2989.. js:attribute:: AppletTCP.sf
2990
2991 :returns: A :ref:`fetches_class`
2992
2993 This attribute contains a Fetches class object.
2994
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01002995.. js:function:: AppletTCP.getline(applet)
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002996
2997 This function returns a string containing one line from the stream. If the
2998 data returned doesn't contains a final '\\n' its assumed than its the last
2999 available data before the end of stream.
3000
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01003001 :param class_AppletTCP applet: An :ref:`applettcp_class`
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02003002 :returns: a string. The string can be empty if we reach the end of the stream.
3003
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01003004.. js:function:: AppletTCP.receive(applet, [size])
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02003005
3006 Reads data from the TCP stream, according to the specified read *size*. If the
3007 *size* is missing, the function tries to read all the content of the stream
3008 until the end.
3009
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01003010 :param class_AppletTCP applet: An :ref:`applettcp_class`
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02003011 :param integer size: the required read size.
Ilya Shipitsin11057a32020-06-21 21:18:27 +05003012 :returns: always return a string,the string can be empty is the connection is
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02003013 closed.
3014
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01003015.. js:function:: AppletTCP.send(appletmsg)
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02003016
3017 Send the message on the stream.
3018
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01003019 :param class_AppletTCP applet: An :ref:`applettcp_class`
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02003020 :param string msg: the message to send.
3021
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01003022.. js:function:: AppletTCP.get_priv(applet)
3023
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01003024 Return Lua data stored in the current transaction. If no data are stored,
3025 it returns a nil value.
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01003026
3027 :param class_AppletTCP applet: An :ref:`applettcp_class`
Bertrand Jacquin874a35c2018-09-10 21:26:07 +01003028 :returns: the opaque data previously stored, or nil if nothing is
3029 available.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01003030 :see: :js:func:`AppletTCP.set_priv`
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01003031
3032.. js:function:: AppletTCP.set_priv(applet, data)
3033
3034 Store any data in the current HAProxy transaction. This action replace the
3035 old stored data.
3036
3037 :param class_AppletTCP applet: An :ref:`applettcp_class`
3038 :param opaque data: The data which is stored in the transaction.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01003039 :see: :js:func:`AppletTCP.get_priv`
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01003040
Tim Duesterhus4e172c92020-05-19 13:49:42 +02003041.. js:function:: AppletTCP.set_var(applet, var, value[, ifexist])
Thierry FOURNIER / OZON.IOc1edafe2016-12-12 16:25:30 +01003042
3043 Converts a Lua type in a HAProxy type and stores it in a variable <var>.
3044
3045 :param class_AppletTCP applet: An :ref:`applettcp_class`
3046 :param string var: The variable name according with the HAProxy variable syntax.
3047 :param type value: The value associated to the variable. The type can be string or
3048 integer.
Tim Duesterhus4e172c92020-05-19 13:49:42 +02003049 :param boolean ifexist: If this parameter is set to a truthy value the variable
3050 will only be set if it was defined elsewhere (i.e. used
Willy Tarreau7978c5c2021-09-07 14:24:07 +02003051 within the configuration). For global variables (using the
3052 "proc" scope), they will only be updated and never created.
3053 It is highly recommended to
Tim Duesterhus4e172c92020-05-19 13:49:42 +02003054 always set this to true.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01003055 :see: :js:func:`AppletTCP.unset_var`
3056 :see: :js:func:`AppletTCP.get_var`
Thierry FOURNIER / OZON.IOc1edafe2016-12-12 16:25:30 +01003057
3058.. js:function:: AppletTCP.unset_var(applet, var)
3059
3060 Unsets the variable <var>.
3061
3062 :param class_AppletTCP applet: An :ref:`applettcp_class`
3063 :param string var: The variable name according with the HAProxy variable syntax.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01003064 :see: :js:func:`AppletTCP.unset_var`
3065 :see: :js:func:`AppletTCP.set_var`
Thierry FOURNIER / OZON.IOc1edafe2016-12-12 16:25:30 +01003066
3067.. js:function:: AppletTCP.get_var(applet, var)
3068
3069 Returns data stored in the variable <var> converter in Lua type.
3070
3071 :param class_AppletTCP applet: An :ref:`applettcp_class`
3072 :param string var: The variable name according with the HAProxy variable syntax.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01003073 :see: :js:func:`AppletTCP.unset_var`
3074 :see: :js:func:`AppletTCP.set_var`
Thierry FOURNIER / OZON.IOc1edafe2016-12-12 16:25:30 +01003075
Adis Nezirovic8878f8e2018-07-13 12:18:33 +02003076StickTable class
3077================
3078
3079.. js:class:: StickTable
3080
3081 **context**: task, action, sample-fetch
3082
3083 This class can be used to access the HAProxy stick tables from Lua.
3084
3085.. js:function:: StickTable.info()
3086
3087 Returns stick table attributes as a Lua table. See HAProxy documentation for
Ilya Shipitsin2272d8a2020-12-21 01:22:40 +05003088 "stick-table" for canonical info, or check out example below.
Adis Nezirovic8878f8e2018-07-13 12:18:33 +02003089
3090 :returns: Lua table
3091
3092 Assume our table has IPv4 key and gpc0 and conn_rate "columns":
3093
3094.. code-block:: lua
3095
3096 {
3097 expire=<int>, # Value in ms
3098 size=<int>, # Maximum table size
3099 used=<int>, # Actual number of entries in table
3100 data={ # Data columns, with types as key, and periods as values
3101 (-1 if type is not rate counter)
3102 conn_rate=<int>,
3103 gpc0=-1
3104 },
3105 length=<int>, # max string length for string table keys, key length
3106 # otherwise
3107 nopurge=<boolean>, # purge oldest entries when table is full
3108 type="ip" # can be "ip", "ipv6", "integer", "string", "binary"
3109 }
3110
3111.. js:function:: StickTable.lookup(key)
3112
3113 Returns stick table entry for given <key>
3114
3115 :param string key: Stick table key (IP addresses and strings are supported)
3116 :returns: Lua table
3117
3118.. js:function:: StickTable.dump([filter])
3119
3120 Returns all entries in stick table. An optional filter can be used
3121 to extract entries with specific data values. Filter is a table with valid
3122 comparison operators as keys followed by data type name and value pairs.
3123 Check out the HAProxy docs for "show table" for more details. For the
3124 reference, the supported operators are:
3125 "eq", "ne", "le", "lt", "ge", "gt"
3126
3127 For large tables, execution of this function can take a long time (for
3128 HAProxy standards). That's also true when filter is used, so take care and
3129 measure the impact.
3130
3131 :param table filter: Stick table filter
3132 :returns: Stick table entries (table)
3133
3134 See below for example filter, which contains 4 entries (or comparisons).
3135 (Maximum number of filter entries is 4, defined in the source code)
3136
3137.. code-block:: lua
3138
3139 local filter = {
3140 {"gpc0", "gt", 30}, {"gpc1", "gt", 20}}, {"conn_rate", "le", 10}
3141 }
3142
Christopher Faulet0f3c8902020-01-31 18:57:12 +01003143.. _action_class:
3144
3145Action class
3146=============
3147
3148.. js:class:: Act
3149
3150 **context**: action
3151
3152 This class contains all return codes an action may return. It is the lua
3153 equivalent to HAProxy "ACT_RET_*" code.
3154
3155.. code-block:: lua
3156
3157 core.register_action("deny", { "http-req" }, function (txn)
3158 return act.DENY
3159 end)
3160..
3161.. js:attribute:: act.CONTINUE
3162
3163 This attribute is an integer (0). It instructs HAProxy to continue the current
3164 ruleset processing on the message. It is the default return code for a lua
3165 action.
3166
3167 :returns: integer
3168
3169.. js:attribute:: act.STOP
3170
3171 This attribute is an integer (1). It instructs HAProxy to stop the current
3172 ruleset processing on the message.
3173
3174.. js:attribute:: act.YIELD
3175
3176 This attribute is an integer (2). It instructs HAProxy to temporarily pause
3177 the message processing. It will be resumed later on the same rule. The
3178 corresponding lua script is re-executed for the start.
3179
3180.. js:attribute:: act.ERROR
3181
3182 This attribute is an integer (3). It triggers an internal errors The message
3183 processing is stopped and the transaction is terminated. For HTTP streams, an
3184 HTTP 500 error is returned to the client.
3185
3186 :returns: integer
3187
3188.. js:attribute:: act.DONE
3189
3190 This attribute is an integer (4). It instructs HAProxy to stop the message
3191 processing.
3192
3193 :returns: integer
3194
3195.. js:attribute:: act.DENY
3196
3197 This attribute is an integer (5). It denies the current message. The message
3198 processing is stopped and the transaction is terminated. For HTTP streams, an
3199 HTTP 403 error is returned to the client if the deny is returned during the
3200 request analysis. During the response analysis, an HTTP 502 error is returned
3201 and the server response is discarded.
3202
3203 :returns: integer
3204
3205.. js:attribute:: act.ABORT
3206
3207 This attribute is an integer (6). It aborts the current message. The message
3208 processing is stopped and the transaction is terminated. For HTTP streams,
Willy Tarreau714f3452021-05-09 06:47:26 +02003209 HAProxy assumes a response was already sent to the client. From the Lua
Christopher Faulet0f3c8902020-01-31 18:57:12 +01003210 actions point of view, when this code is used, the transaction is terminated
3211 with no reply.
3212
3213 :returns: integer
3214
3215.. js:attribute:: act.INVALID
3216
3217 This attribute is an integer (7). It triggers an internal errors. The message
3218 processing is stopped and the transaction is terminated. For HTTP streams, an
3219 HTTP 400 error is returned to the client if the error is returned during the
3220 request analysis. During the response analysis, an HTTP 502 error is returned
3221 and the server response is discarded.
3222
3223 :returns: integer
Adis Nezirovic8878f8e2018-07-13 12:18:33 +02003224
Christopher Faulet2c2c2e32020-01-31 19:07:52 +01003225.. js:function:: act:wake_time(milliseconds)
3226
3227 **context**: action
3228
3229 Set the script pause timeout to the specified time, defined in
3230 milliseconds.
3231
3232 :param integer milliseconds: the required milliseconds.
3233
3234 This function may be used when a lua action returns `act.YIELD`, to force its
3235 wake-up at most after the specified number of milliseconds.
3236
Christopher Faulet5a2c6612021-08-15 20:35:25 +02003237.. _filter_class:
3238
3239Filter class
3240=============
3241
3242.. js:class:: filter
3243
3244 **context**: filter
3245
3246 This class contains return codes some filter callback functions may return. It
3247 also contains configuration flags and some helper functions. To understand how
3248 the filter API works, see `doc/internal/filters.txt` documentation.
3249
3250.. js:attribute:: filter.CONTINUE
3251
3252 This attribute is an integer (1). It may be returned by some filter callback
3253 functions to instruct this filtering step is finished for this filter.
3254
3255.. js:attribute:: filter.WAIT
3256
3257 This attribute is an integer (0). It may be returned by some filter callback
3258 functions to instruct the filtering must be paused, waiting for more data or
3259 for an external event depending on this filter.
3260
3261.. js:attribute:: filter.ERROR
3262
3263 This attribute is an integer (-1). It may be returned by some filter callback
3264 functions to trigger an error.
3265
3266.. js:attribute:: filter.FLT_CFG_FL_HTX
3267
3268 This attribute is a flag corresponding to the filter flag FLT_CFG_FL_HTX. When
3269 it is set for a filter, it means the filter is able to filter HTTP streams.
3270
3271.. js:function:: filter.register_data_filter(chn)
3272
3273 **context**: filter
3274
3275 Enable the data filtering on the channel **chn** for the current filter. It
Ilya Shipitsinff0f2782021-08-22 22:18:07 +05003276 may be called at any time from any callback functions proceeding the data
Christopher Faulet5a2c6612021-08-15 20:35:25 +02003277 analysis.
3278
3279 :param class_Channel chn: A :ref:`channel_class`.
3280
3281.. js:function:: filter.unregister_data_filter(chn)
3282
3283 **context**: filter
3284
3285 Disable the data filtering on the channel **chn** for the current filter. It
3286 may be called at any time from any callback functions.
3287
3288 :param class_Channel chn: A :ref:`channel_class`.
3289
3290.. js:function:: filter.wake_time(milliseconds)
3291
3292 **context**: filter
3293
3294 Set the script pause timeout to the specified time, defined in
3295 milliseconds.
3296
3297 :param integer milliseconds: the required milliseconds.
3298
3299 This function may be used from any lua filter callback function to force its
3300 wake-up at most after the specified number of milliseconds. Especially, when
3301 `filter.CONTINUE` is returned.
3302
3303
3304A filters is declared using :js:func:`core.register_filter()` function. The
3305provided class will be used to instantiate filters. It may define following
3306attributes:
3307
3308* id: The filter identifier. It is a string that identifies the filter and is
3309 optional.
3310
3311* flags: The filter flags. Only :js:attr:`filter.FLT_CFG_FL_HTX` may be set for now.
3312
3313Such filter class must also define all required callback functions in the
3314following list. Note that :js:func:`Filter.new()` must be defined otherwise the
Ilya Shipitsinff0f2782021-08-22 22:18:07 +05003315filter is ignored. Others are optional.
Christopher Faulet5a2c6612021-08-15 20:35:25 +02003316
3317* .. js:function:: FILTER.new()
3318
3319 Called to instantiate a new filter. This function must be defined.
3320
3321 :returns: a Lua object that will be used as filter instance for the current
3322 stream.
3323
3324* .. js:function:: FILTER.start_analyze(flt, txn, chn)
3325
3326 Called when the analysis starts on the channel **chn**.
3327
3328* .. js:function:: FILTER.end_analyze(flt, txn, chn)
3329
3330 Called when the analysis ends on the channel **chn**.
3331
3332* .. js:function:: FILTER.http_headers(flt, txn, http_msg)
3333
3334 Called just before the HTTP payload analysis and after any processing on the
3335 HTTP message **http_msg**. This callback functions is only called for HTTP
3336 streams.
3337
3338* .. js:function:: FILTER.http_payload(flt, txn, http_msg)
3339
3340 Called during the HTTP payload analysis on the HTTP message **http_msg**. This
3341 callback functions is only called for HTTP streams.
3342
3343* .. js:function:: FILTER.http_end(flt, txn, http_msg)
3344
3345 Called after the HTTP payload analysis on the HTTP message **http_msg**. This
3346 callback functions is only called for HTTP streams.
3347
3348* .. js:function:: FILTER.tcp_payload(flt, txn, chn)
3349
3350 Called during the TCP payload analysis on the channel **chn**.
3351
3352Here is an full example:
3353
3354.. code-block:: lua
3355
3356 Trace = {}
3357 Trace.id = "Lua trace filter"
3358 Trace.flags = filter.FLT_CFG_FL_HTX;
3359 Trace.__index = Trace
3360
3361 function Trace:new()
3362 local trace = {}
3363 setmetatable(trace, Trace)
3364 trace.req_len = 0
3365 trace.res_len = 0
3366 return trace
3367 end
3368
3369 function Trace:start_analyze(txn, chn)
3370 if chn:is_resp() then
3371 print("Start response analysis")
3372 else
3373 print("Start request analysis")
3374 end
3375 filter.register_data_filter(self, chn)
3376 end
3377
3378 function Trace:end_analyze(txn, chn)
3379 if chn:is_resp() then
3380 print("End response analysis: "..self.res_len.." bytes filtered")
3381 else
3382 print("End request analysis: "..self.req_len.." bytes filtered")
3383 end
3384 end
3385
3386 function Trace:http_headers(txn, http_msg)
3387 stline = http_msg:get_stline()
3388 if http_msg.channel:is_resp() then
3389 print("response:")
3390 print(stline.version.." "..stline.code.." "..stline.reason)
3391 else
3392 print("request:")
3393 print(stline.method.." "..stline.uri.." "..stline.version)
3394 end
3395
3396 for n, hdrs in pairs(http_msg:get_headers()) do
3397 for i,v in pairs(hdrs) do
3398 print(n..": "..v)
3399 end
3400 end
3401 return filter.CONTINUE
3402 end
3403
3404 function Trace:http_payload(txn, http_msg)
3405 body = http_msg:body(-20000)
3406 if http_msg.channel:is_resp() then
3407 self.res_len = self.res_len + body:len()
3408 else
3409 self.req_len = self.req_len + body:len()
3410 end
3411 end
3412
3413 core.register_filter("trace", Trace, function(trace, args)
3414 return trace
3415 end)
3416
3417..
3418
3419.. _httpmessage_class:
3420
3421HTTPMessage class
3422===================
3423
3424.. js:class:: HTTPMessage
3425
3426 **context**: filter
3427
3428 This class contains all functions to manipulate an HTTP message. For now, this
3429 class is only available from a filter context.
3430
3431.. js:function:: HTTPMessage.add_header(http_msg, name, value)
3432
3433 Appends an HTTP header field in the HTTP message **http_msg** whose name is
3434 specified in **name** and whose value is defined in **value**.
3435
3436 :param class_httpmessage http_msg: The manipulated HTTP message.
3437 :param string name: The header name.
3438 :param string value: The header value.
3439
3440.. js:function:: HTTPMessage.append(http_msg, string)
3441
3442 This function copies the string **string** at the end of incoming data of the
3443 HTTP message **http_msg**. The function returns the copied length on success
3444 or -1 if data cannot be copied.
3445
3446 Same that :js:func:`HTTPMessage.insert(http_msg, string, http_msg:input())`.
3447
3448 :param class_httpmessage http_msg: The manipulated HTTP message.
3449 :param string string: The data to copied into incoming data.
3450 :returns: an integer containing the amount of bytes copied or -1.
3451
3452.. js:function:: HTTPMessage.body(http_msgl[, offset[, length]])
3453
3454 This function returns **length** bytes of incoming data from the HTTP message
3455 **http_msg**, starting at the offset **offset**. The data are not removed from
3456 the buffer.
3457
3458 By default, if no length is provided, all incoming data found, starting at the
3459 given offset, are returned. If **length** is set to -1, the function tries to
3460 retrieve a maximum of data. Because it is called in the filter context, it
3461 never yield. Do not providing an offset is the same that setting it to 0. A
Ilya Shipitsinff0f2782021-08-22 22:18:07 +05003462 positive offset is relative to the beginning of incoming data of the
Christopher Faulet5a2c6612021-08-15 20:35:25 +02003463 http_message buffer while negative offset is relative to their end.
3464
3465 If there is no incoming data and the HTTP message can't receive more data, a 'nil'
3466 value is returned.
3467
3468 :param class_httpmessage http_msg: The manipulated HTTP message.
3469 :param integer offset: *optional* The offset in incoming data to start to get
3470 data. 0 by default. May be negative to be relative to
3471 the end of incoming data.
3472 :param integer length: *optional* The expected length of data to retrieve. All
3473 incoming data by default. May be set to -1 to get a
3474 maximum of data.
3475 :returns: a string containing the data found or nil.
3476
3477.. js:function:: HTTPMessage.eom(http_msg)
3478
3479 This function returns true if the end of message is reached for the HTTP
3480 message **http_msg**.
3481
3482 :param class_httpmessage http_msg: The manipulated HTTP message.
3483 :returns: an integer containing the amount of available bytes.
3484
3485.. js:function:: HTTPMessage.del_header(http_msg, name)
3486
3487 Removes all HTTP header fields in the HTTP message **http_msg** whose name is
3488 specified in **name**.
3489
3490 :param class_httpmessage http_msg: The manipulated http message.
3491 :param string name: The header name.
3492
3493.. js:function:: HTTPMessage.get_headers(http_msg)
3494
3495 Returns a table containing all the headers of the HTTP message **http_msg**.
3496
3497 :param class_httpmessage http_msg: The manipulated http message.
3498 :returns: table of headers.
3499
3500 This is the form of the returned table:
3501
3502.. code-block:: lua
3503
3504 http_msg:get_headers()['<header-name>'][<header-index>] = "<header-value>"
3505
3506 local hdr = http_msg:get_headers()
3507 hdr["host"][0] = "www.test.com"
3508 hdr["accept"][0] = "audio/basic q=1"
3509 hdr["accept"][1] = "audio/*, q=0.2"
3510 hdr["accept"][2] = "*.*, q=0.1"
3511..
3512
3513.. js:function:: HTTPMessage.get_stline(http_msg)
3514
3515 Returns a table containing the start-line of the HTTP message **http_msg**.
3516
3517 :param class_httpmessage http_msg: The manipulated http message.
3518 :returns: the start-line.
3519
3520 This is the form of the returned table:
3521
3522.. code-block:: lua
3523
3524 -- for the request :
3525 {"method" = string, "uri" = string, "version" = string}
3526
3527 -- for the response:
3528 {"version" = string, "code" = string, "reason" = string}
3529..
3530
3531.. js:function:: HTTPMessage.forward(http_msg, length)
3532
3533 This function forwards **length** bytes of data from the HTTP message
3534 **http_msg**. Because it is called in the filter context, it never yield. Only
3535 available incoming data may be forwarded, event if the requested length
3536 exceeds the available amount of incoming data. It returns the amount of data
3537 forwarded.
3538
3539 :param class_httpmessage http_msg: The manipulated HTTP message.
3540 :param integer int: The amount of data to forward.
3541
3542.. js:function:: HTTPMessage.input(http_msg)
3543
3544 This function returns the length of incoming data in the HTTP message
3545 **http_msg** from the calling filter point of view.
3546
3547 :param class_httpmessage http_msg: The manipulated HTTP message.
3548 :returns: an integer containing the amount of available bytes.
3549
3550.. js:function:: HTTPMessage.insert(http_msg, string[, offset])
3551
3552 This function copies the string **string** at the offset **offset** in
3553 incoming data of the HTTP message **http_msg**. The function returns the
3554 copied length on success or -1 if data cannot be copied.
3555
3556 By default, if no offset is provided, the string is copied in front of
Ilya Shipitsinff0f2782021-08-22 22:18:07 +05003557 incoming data. A positive offset is relative to the beginning of incoming data
Christopher Faulet5a2c6612021-08-15 20:35:25 +02003558 of the HTTP message while negative offset is relative to their end.
3559
3560 :param class_httpmessage http_msg: The manipulated HTTP message.
3561 :param string string: The data to copied into incoming data.
3562 :param integer offset: *optional* The offset in incomding data where to copied
3563 data. 0 by default. May be negative to be relative to
3564 the end of incoming data.
3565 :returns: an integer containing the amount of bytes copied or -1.
3566
3567.. js:function:: HTTPMessage.is_full(http_msg)
3568
3569 This function returns true if the HTTP message **http_msg** is full.
3570
3571 :param class_httpmessage http_msg: The manipulated HTTP message.
3572 :returns: a boolean
3573
3574.. js:function:: HTTPMessage.is_resp(http_msg)
3575
3576 This function returns true if the HTTP message **http_msg** is the response
3577 one.
3578
3579 :param class_httpmessage http_msg: The manipulated HTTP message.
3580 :returns: a boolean
3581
3582.. js:function:: HTTPMessage.may_recv(http_msg)
3583
3584 This function returns true if the HTTP message **http_msg** may still receive
3585 data.
3586
3587 :param class_httpmessage http_msg: The manipulated HTTP message.
3588 :returns: a boolean
3589
3590.. js:function:: HTTPMessage.output(http_msg)
3591
3592 This function returns the length of outgoing data of the HTTP message
3593 **http_msg**.
3594
3595 :param class_httpmessage http_msg: The manipulated HTTP message.
3596 :returns: an integer containing the amount of available bytes.
3597
3598.. js:function:: HTTPMessage.prepend(http_msg, string)
3599
3600 This function copies the string **string** in front of incoming data of the
3601 HTTP message **http_msg**. The function returns the copied length on success
3602 or -1 if data cannot be copied.
3603
3604 Same that :js:func:`HTTPMessage.insert(http_msg, string, 0)`.
3605
3606 :param class_httpmessage http_msg: The manipulated HTTP message.
3607 :param string string: The data to copied into incoming data.
3608 :returns: an integer containing the amount of bytes copied or -1.
3609
3610.. js:function:: HTTPMessage.remove(http_msg[, offset[, length]])
3611
3612 This function removes **length** bytes of incoming data of the HTTP message
3613 **http_msg**, starting at offset **offset**. This function returns number of
3614 bytes removed on success.
3615
3616 By default, if no length is provided, all incoming data, starting at the given
3617 offset, are removed. Do not providing an offset is the same that setting it
Ilya Shipitsinff0f2782021-08-22 22:18:07 +05003618 to 0. A positive offset is relative to the beginning of incoming data of the
Christopher Faulet5a2c6612021-08-15 20:35:25 +02003619 HTTP message while negative offset is relative to their end.
3620
3621 :param class_httpmessage http_msg: The manipulated HTTP message.
3622 :param integer offset: *optional* The offset in incomding data where to start
3623 to remove data. 0 by default. May be negative to
3624 be relative to the end of incoming data.
3625 :param integer length: *optional* The length of data to remove. All incoming
3626 data by default.
3627 :returns: an integer containing the amount of bytes removed.
3628
3629.. js:function:: HTTPMessage.rep_header(http_msg, name, regex, replace)
3630
3631 Matches the regular expression in all occurrences of header field **name**
3632 according to regex **regex**, and replaces them with the string **replace**.
3633 The replacement value can contain back references like \1, \2, ... This
3634 function acts on whole header lines, regardless of the number of values they
3635 may contain.
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.rep_value(http_msg, name, regex, replace)
3643
3644 Matches the regular expression on every comma-delimited value of header field
3645 **name** according to regex **regex**, and replaces them with the string
3646 **replace**. The replacement value can contain back references like \1, \2,
3647 ...
3648
3649 :param class_httpmessage http_msg: The manipulated HTTP message.
3650 :param string name: The header name.
3651 :param string regex: The match regular expression.
3652 :param string replace: The replacement value.
3653
3654.. js:function:: HTTPMessage.send(http_msg, string)
3655
3656 This function required immediate send of the string **string**. It means the
Ilya Shipitsinff0f2782021-08-22 22:18:07 +05003657 string is copied at the beginning of incoming data of the HTTP message
Christopher Faulet5a2c6612021-08-15 20:35:25 +02003658 **http_msg** and immediately forwarded. Because it is called in the filter
3659 context, it never yield.
3660
3661 :param class_httpmessage http_msg: The manipulated HTTP message.
3662 :param string string: The data to send.
3663 :returns: an integer containing the amount of bytes copied or -1.
3664
3665.. js:function:: HTTPMessage.set(http_msg, string[, offset[, length]])
3666
3667 This function replace **length** bytes of incoming data of the HTTP message
3668 **http_msg**, starting at offset **offset**, by the string **string**. The
3669 function returns the copied length on success or -1 if data cannot be copied.
3670
3671 By default, if no length is provided, all incoming data, starting at the given
3672 offset, are replaced. Do not providing an offset is the same that setting it
Ilya Shipitsinff0f2782021-08-22 22:18:07 +05003673 to 0. A positive offset is relative to the beginning of incoming data of the
Christopher Faulet5a2c6612021-08-15 20:35:25 +02003674 HTTP message while negative offset is relative to their end.
3675
3676 :param class_httpmessage http_msg: The manipulated HTTP message.
3677 :param string string: The data to copied into incoming data.
3678 :param integer offset: *optional* The offset in incomding data where to start
3679 the data replacement. 0 by default. May be negative to
3680 be relative to the end of incoming data.
3681 :param integer length: *optional* The length of data to replace. All incoming
3682 data by default.
3683 :returns: an integer containing the amount of bytes copied or -1.
3684
3685.. js:function:: HTTPMessage.set_eom(http_msg)
3686
3687 This function set the end of message for the HTTP message **http_msg**.
3688
3689 :param class_httpmessage http_msg: The manipulated HTTP message.
3690
3691.. js:function:: HTTPMessage.set_header(http_msg, name, value)
3692
3693 This variable replace all occurrence of all header matching the name **name**,
3694 by only one containing the value **value**.
3695
3696 :param class_httpmessage http_msg: The manipulated HTTP message.
3697 :param string name: The header name.
3698 :param string value: The header value.
3699
3700 This function does the same work as the following code:
3701
3702.. code-block:: lua
3703
3704 http_msg:del_header("header")
3705 http_msg:add_header("header", "value")
3706..
3707
3708.. js:function:: HTTPMessage.set_method(http_msg, method)
3709
3710 Rewrites the request method with the string **method**. The HTTP message
3711 **http_msg** must be the request.
3712
3713 :param class_httpmessage http_msg: The manipulated HTTP message.
3714 :param string method: The new method.
3715
3716.. js:function:: HTTPMessage.set_path(http_msg, path)
3717
3718 Rewrites the request path with the string **path**. The HTTP message
3719 **http_msg** must be the request.
3720
3721 :param class_httpmessage http_msg: The manipulated HTTP message.
3722 :param string method: The new method.
3723
3724.. js:function:: HTTPMessage.set_query(http_msg, query)
3725
3726 Rewrites the request's query string which appears after the first question
3727 mark ("?") with the string **query**. The HTTP message **http_msg** must be
3728 the request.
3729
3730 :param class_httpmessage http_msg: The manipulated HTTP message.
3731 :param string query: The new query.
3732
3733.. js:function:: HTTPMessage.set_status(http_msg, status[, reason])
3734
Ilya Shipitsinff0f2782021-08-22 22:18:07 +05003735 Rewrites the response status code with the integer **code** and optional the
Christopher Faulet5a2c6612021-08-15 20:35:25 +02003736 reason **reason**. If no custom reason is provided, it will be generated from
3737 the status. The HTTP message **http_msg** must be the response.
3738
3739 :param class_httpmessage http_msg: The manipulated HTTP message.
3740 :param integer status: The new response status code.
3741 :param string reason: The new response reason (optional).
3742
3743.. js:function:: HTTPMessage.set_uri(http_msg, uri)
3744
3745 Rewrites the request URI with the string **uri**. The HTTP message
3746 **http_msg** must be the request.
3747
3748 :param class_httpmessage http_msg: The manipulated HTTP message.
3749 :param string uri: The new uri.
3750
3751.. js:function:: HTTPMessage.unset_eom(http_msg)
3752
3753 This function remove the end of message for the HTTP message **http_msg**.
3754
3755 :param class_httpmessage http_msg: The manipulated HTTP message.
3756
William Lallemand10cea5c2022-03-30 16:02:43 +02003757.. _CertCache_class:
3758
3759CertCache class
3760================
3761
3762.. js:class:: CertCache
3763
3764 This class allows to update an SSL certificate file in the memory of the
3765 current HAProxy process. It will do the same as "set ssl cert" + "commit ssl
3766 cert" over the HAProxy CLI.
3767
3768.. js:function:: CertCache.set(certificate)
3769
3770 This function updates a certificate in memory.
3771
3772 :param table certificate: A table containing the fields to update.
3773 :param string certificate.filename: The mandatory filename of the certificate
3774 to update, it must already exist in memory.
3775 :param string certificate.crt: A certificate in the PEM format. It can also
3776 contain a private key.
3777 :param string certificate.key: A private key in the PEM format.
3778 :param string certificate.ocsp: An OCSP response in base64. (cf management.txt)
3779 :param string certificate.issuer: The certificate of the OCSP issuer.
3780 :param string certificate.sctl: An SCTL file.
3781
3782.. code-block:: lua
3783
3784 CertCache.set{filename="certs/localhost9994.pem.rsa", crt=crt}
3785
3786
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01003787External Lua libraries
3788======================
3789
3790A lot of useful lua libraries can be found here:
3791
3792* `https://lua-toolbox.com/ <https://lua-toolbox.com/>`_
3793
Ilya Shipitsin2075ca82020-03-06 23:22:22 +05003794Redis client library:
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01003795
3796* `https://github.com/nrk/redis-lua <https://github.com/nrk/redis-lua>`_
3797
3798This is an example about the usage of the Redis library with HAProxy. Note that
3799each call of any function of this library can throw an error if the socket
3800connection fails.
3801
3802.. code-block:: lua
3803
3804 -- load the redis library
3805 local redis = require("redis");
3806
3807 function do_something(txn)
3808
3809 -- create and connect new tcp socket
3810 local tcp = core.tcp();
3811 tcp:settimeout(1);
3812 tcp:connect("127.0.0.1", 6379);
3813
3814 -- use the redis library with this new socket
3815 local client = redis.connect({socket=tcp});
3816 client:ping();
3817
3818 end
3819
3820OpenSSL:
3821
3822* `http://mkottman.github.io/luacrypto/index.html
3823 <http://mkottman.github.io/luacrypto/index.html>`_
3824
3825* `https://github.com/brunoos/luasec/wiki
3826 <https://github.com/brunoos/luasec/wiki>`_