blob: 541d960a5601e220bf70a29373e46ea437dd1fa1 [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
23functions. Lua have 6 execution context.
24
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
58 **NOTE**: It is possible that this function cannot found the required data
59 in the original HAProxy sample-fetches, in this case, it cannot return the
60 result. This case is not yet supported
61
David Carlier61fdf8b2015-10-02 11:59:38 +0100626. The **converter context**. It is a Lua function that takes a string as input
Thierry FOURNIER17bd1522015-03-11 20:31:00 +010063 and returns another string as output. These types of function are stateless,
64 it cannot access to any context. They don't execute any blocking function.
65 The call prototype is `function string fcn(string)`. This function can be
66 registered with the Lua function `core.register_converters()`. Each declared
67 converter is prefixed by the string "lua.".
68
69HAProxy Lua Hello world
70-----------------------
71
72HAProxy configuration file (`hello_world.conf`):
73
74::
75
76 global
77 lua-load hello_world.lua
78
79 listen proxy
80 bind 127.0.0.1:10001
Thierry FOURNIERa2d02252015-10-01 15:00:42 +020081 tcp-request inspect-delay 1s
82 tcp-request content use-service lua.hello_world
Thierry FOURNIER17bd1522015-03-11 20:31:00 +010083
84HAProxy Lua file (`hello_world.lua`):
85
86.. code-block:: lua
87
Thierry FOURNIERa2d02252015-10-01 15:00:42 +020088 core.register_service("hello_world", "tcp", function(applet)
89 applet:send("hello world\n")
90 end)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +010091
92How to start HAProxy for testing this configuration:
93
94::
95
96 ./haproxy -f hello_world.conf
97
98On other terminal, you can test with telnet:
99
100::
101
102 #:~ telnet 127.0.0.1 10001
103 hello world
104
105Core class
106==========
107
108.. js:class:: core
109
110 The "core" class contains all the HAProxy core functions. These function are
111 useful for the controlling the execution flow, registering hooks, manipulating
112 global maps or ACL, ...
113
114 "core" class is basically provided with HAProxy. No `require` line is
115 required to uses these function.
116
David Carlier61fdf8b2015-10-02 11:59:38 +0100117 The "core" class is static, it is not possible to create a new object of this
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100118 type.
119
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100120.. js:attribute:: core.emerg
121
Thierry FOURNIERdc595002015-12-21 11:13:52 +0100122 :returns: integer
123
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100124 This attribute is an integer, it contains the value of the loglevel "emergency" (0).
125
126.. js:attribute:: core.alert
127
Thierry FOURNIERdc595002015-12-21 11:13:52 +0100128 :returns: integer
129
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100130 This attribute is an integer, it contains the value of the loglevel "alert" (1).
131
132.. js:attribute:: core.crit
133
Thierry FOURNIERdc595002015-12-21 11:13:52 +0100134 :returns: integer
135
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100136 This attribute is an integer, it contains the value of the loglevel "critical" (2).
137
138.. js:attribute:: core.err
139
Thierry FOURNIERdc595002015-12-21 11:13:52 +0100140 :returns: integer
141
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100142 This attribute is an integer, it contains the value of the loglevel "error" (3).
143
144.. js:attribute:: core.warning
145
Thierry FOURNIERdc595002015-12-21 11:13:52 +0100146 :returns: integer
147
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100148 This attribute is an integer, it contains the value of the loglevel "warning" (4).
149
150.. js:attribute:: core.notice
151
Thierry FOURNIERdc595002015-12-21 11:13:52 +0100152 :returns: integer
153
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100154 This attribute is an integer, it contains the value of the loglevel "notice" (5).
155
156.. js:attribute:: core.info
157
Thierry FOURNIERdc595002015-12-21 11:13:52 +0100158 :returns: integer
159
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100160 This attribute is an integer, it contains the value of the loglevel "info" (6).
161
162.. js:attribute:: core.debug
163
Thierry FOURNIERdc595002015-12-21 11:13:52 +0100164 :returns: integer
165
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100166 This attribute is an integer, it contains the value of the loglevel "debug" (7).
167
Thierry FOURNIER12a865d2016-12-14 19:40:37 +0100168.. js:attribute:: core.proxies
169
170 **context**: task, action, sample-fetch, converter
171
172 This attribute is an array of declared proxies (frontend and backends). Each
173 proxy give an access to his list of listeners and servers. Each entry is of
174 type :ref:`proxy_class`
175
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100176.. js:function:: core.log(loglevel, msg)
177
178 **context**: body, init, task, action, sample-fetch, converter
179
David Carlier61fdf8b2015-10-02 11:59:38 +0100180 This function sends a log. The log is sent, according with the HAProxy
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100181 configuration file, on the default syslog server if it is configured and on
182 the stderr if it is allowed.
183
184 :param integer loglevel: Is the log level asociated with the message. It is a
185 number between 0 and 7.
186 :param string msg: The log content.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +0100187 :see: :js:attr:`core.emerg`, :js:attr:`core.alert`, :js:attr:`core.crit`,
188 :js:attr:`core.err`, :js:attr:`core.warning`, :js:attr:`core.notice`,
189 :js:attr:`core.info`, :js:attr:`core.debug` (log level definitions)
190 :see: :js:func:`core.Debug`
191 :see: :js:func:`core.Info`
192 :see: :js:func:`core.Warning`
193 :see: :js:func:`core.Alert`
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100194
195.. js:function:: core.Debug(msg)
196
197 **context**: body, init, task, action, sample-fetch, converter
198
199 :param string msg: The log content.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +0100200 :see: :js:func:`core.log`
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100201
202 Does the same job than:
203
204.. code-block:: lua
205
Thierry FOURNIERdc595002015-12-21 11:13:52 +0100206 function Debug(msg)
207 core.log(core.debug, msg)
208 end
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100209..
210
211.. js:function:: core.Info(msg)
212
213 **context**: body, init, task, action, sample-fetch, converter
214
215 :param string msg: The log content.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +0100216 :see: :js:func:`core.log`
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100217
218.. code-block:: lua
219
Thierry FOURNIERdc595002015-12-21 11:13:52 +0100220 function Info(msg)
221 core.log(core.info, msg)
222 end
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100223..
224
225.. js:function:: core.Warning(msg)
226
227 **context**: body, init, task, action, sample-fetch, converter
228
229 :param string msg: The log content.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +0100230 :see: :js:func:`core.log`
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100231
232.. code-block:: lua
233
Thierry FOURNIERdc595002015-12-21 11:13:52 +0100234 function Warning(msg)
235 core.log(core.warning, msg)
236 end
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100237..
238
239.. js:function:: core.Alert(msg)
240
241 **context**: body, init, task, action, sample-fetch, converter
242
243 :param string msg: The log content.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +0100244 :see: :js:func:`core.log`
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100245
246.. code-block:: lua
247
Thierry FOURNIERdc595002015-12-21 11:13:52 +0100248 function Alert(msg)
249 core.log(core.alert, msg)
250 end
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100251..
252
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100253.. js:function:: core.add_acl(filename, key)
254
255 **context**: init, task, action, sample-fetch, converter
256
257 Add the ACL *key* in the ACLs list referenced by the file *filename*.
258
259 :param string filename: the filename that reference the ACL entries.
260 :param string key: the key which will be added.
261
262.. js:function:: core.del_acl(filename, key)
263
264 **context**: init, task, action, sample-fetch, converter
265
266 Delete the ACL entry referenced by the key *key* in the list of ACLs
267 referenced by *filename*.
268
269 :param string filename: the filename that reference the ACL entries.
270 :param string key: the key which will be deleted.
271
272.. js:function:: core.del_map(filename, key)
273
274 **context**: init, task, action, sample-fetch, converter
275
276 Delete the map entry indexed with the specified key in the list of maps
277 referenced by his filename.
278
279 :param string filename: the filename that reference the map entries.
280 :param string key: the key which will be deleted.
281
Thierry Fourniereea77c02016-03-18 08:47:13 +0100282.. js:function:: core.get_info()
283
284 **context**: body, init, task, action, sample-fetch, converter
285
286 Returns HAProxy core informations. We can found information like the uptime,
287 the pid, memory pool usage, tasks number, ...
288
289 These information are also returned by the management sockat via the command
290 "show info". See the management socket documentation fpor more information
291 about the content of these variables.
292
293 :returns: an array of values.
294
Thierry Fournierb1f46562016-01-21 09:46:15 +0100295.. js:function:: core.now()
296
297 **context**: body, init, task, action
298
299 This function returns the current time. The time returned is fixed by the
300 HAProxy core and assures than the hour will be monotnic and that the system
301 call 'gettimeofday' will not be called too. The time is refreshed between each
302 Lua execution or resume, so two consecutive call to the function "now" will
303 probably returns the same result.
304
305 :returns: an array which contains two entries "sec" and "usec". "sec"
306 contains the current at the epoch format, and "usec" contains the
307 current microseconds.
308
Thierry FOURNIERa78f0372016-12-14 19:04:41 +0100309.. js:function:: core.http_date(date)
310
311 **context**: body, init, task, action
312
313 This function take a string repsenting http date, and returns an integer
314 containing the corresponding date with a epoch format. A valid http date
315 me respect the format IMF, RFC850 or ASCTIME.
316
317 :param string date: a date http-date formatted
318 :returns: integer containing epoch date
319 :see: :js:func:`core.imf_date`.
320 :see: :js:func:`core.rfc850_date`.
321 :see: :js:func:`core.asctime_date`.
322 :see: https://tools.ietf.org/html/rfc7231#section-7.1.1.1
323
324.. js:function:: core.imf_date(date)
325
326 **context**: body, init, task, action
327
328 This function take a string repsenting IMF date, and returns an integer
329 containing the corresponding date with a epoch format.
330
331 :param string date: a date IMF formatted
332 :returns: integer containing epoch date
333 :see: https://tools.ietf.org/html/rfc7231#section-7.1.1.1
334
335 The IMF format is like this:
336
337.. code-block:: text
338
339 Sun, 06 Nov 1994 08:49:37 GMT
340..
341
342.. js:function:: core.rfc850_date(date)
343
344 **context**: body, init, task, action
345
346 This function take a string repsenting RFC850 date, and returns an integer
347 containing the corresponding date with a epoch format.
348
349 :param string date: a date RFC859 formatted
350 :returns: integer containing epoch date
351 :see: https://tools.ietf.org/html/rfc7231#section-7.1.1.1
352
353 The RFC850 format is like this:
354
355.. code-block:: text
356
357 Sunday, 06-Nov-94 08:49:37 GMT
358..
359
360.. js:function:: core.asctime_date(date)
361
362 **context**: body, init, task, action
363
364 This function take a string repsenting ASCTIME date, and returns an integer
365 containing the corresponding date with a epoch format.
366
367 :param string date: a date ASCTIME formatted
368 :returns: integer containing epoch date
369 :see: https://tools.ietf.org/html/rfc7231#section-7.1.1.1
370
371 The ASCTIME format is like this:
372
373.. code-block:: text
374
375 Sun Nov 6 08:49:37 1994
376..
377
378.. js:function:: core.rfc850_date(date)
379
380 **context**: body, init, task, action
381
382 This function take a string repsenting http date, and returns an integer
383 containing the corresponding date with a epoch format.
384
385 :param string date: a date http-date formatted
386
387.. js:function:: core.asctime_date(date)
388
389 **context**: body, init, task, action
390
391 This function take a string repsenting http date, and returns an integer
392 containing the corresponding date with a epoch format.
393
394 :param string date: a date http-date formatted
395
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100396.. js:function:: core.msleep(milliseconds)
397
398 **context**: body, init, task, action
399
400 The `core.msleep()` stops the Lua execution between specified milliseconds.
401
402 :param integer milliseconds: the required milliseconds.
403
Thierry Fournierf61aa632016-02-19 20:56:00 +0100404.. js:attribute:: core.proxies
405
406 **context**: body, init, task, action, sample-fetch, converter
407
408 proxies is an array containing the list of all proxies declared in the
409 configuration file. Each entry of the proxies array is an object of type
410 :ref:`proxy_class`
411
Thierry FOURNIER8255a752015-09-23 21:03:35 +0200412.. js:function:: core.register_action(name, actions, func)
413
414 **context**: body
415
David Carlier61fdf8b2015-10-02 11:59:38 +0100416 Register a Lua function executed as action. All the registered action can be
Thierry FOURNIER8255a752015-09-23 21:03:35 +0200417 used in HAProxy with the prefix "lua.". An action gets a TXN object class as
418 input.
419
420 :param string name: is the name of the converter.
Willy Tarreau61add3c2015-09-28 15:39:10 +0200421 :param table actions: is a table of string describing the HAProxy actions who
Thierry FOURNIER8255a752015-09-23 21:03:35 +0200422 want to register to. The expected actions are 'tcp-req',
423 'tcp-res', 'http-req' or 'http-res'.
424 :param function func: is the Lua function called to work as converter.
425
426 The prototype of the Lua function used as argument is:
427
428.. code-block:: lua
429
430 function(txn)
431..
432
Thierry FOURNIERdc595002015-12-21 11:13:52 +0100433 * **txn** (:ref:`txn_class`): this is a TXN object used for manipulating the
Thierry FOURNIER8255a752015-09-23 21:03:35 +0200434 current request or TCP stream.
435
Willy Tarreau61add3c2015-09-28 15:39:10 +0200436 Here, an exemple of action registration. the action juste send an 'Hello world'
Thierry FOURNIER8255a752015-09-23 21:03:35 +0200437 in the logs.
438
439.. code-block:: lua
440
441 core.register_action("hello-world", { "tcp-req", "http-req" }, function(txn)
442 txn:Info("Hello world")
443 end)
444..
445
446 This example code is used in HAproxy configuration like this:
447
448::
449
450 frontend tcp_frt
451 mode tcp
452 tcp-request content lua.hello-world
453
454 frontend http_frt
455 mode http
456 http-request lua.hello-world
457
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100458.. js:function:: core.register_converters(name, func)
459
460 **context**: body
461
David Carlier61fdf8b2015-10-02 11:59:38 +0100462 Register a Lua function executed as converter. All the registered converters
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100463 can be used in HAProxy with the prefix "lua.". An converter get a string as
464 input and return a string as output. The registered function can take up to 9
465 values as parameter. All the value are strings.
466
467 :param string name: is the name of the converter.
468 :param function func: is the Lua function called to work as converter.
469
470 The prototype of the Lua function used as argument is:
471
472.. code-block:: lua
473
474 function(str, [p1 [, p2 [, ... [, p5]]]])
475..
476
477 * **str** (*string*): this is the input value automatically converted in
478 string.
479 * **p1** .. **p5** (*string*): this is a list of string arguments declared in
480 the haroxy configuration file. The number of arguments doesn't exceed 5.
481 The order and the nature of these is conventionally choose by the
482 developper.
483
484.. js:function:: core.register_fetches(name, func)
485
486 **context**: body
487
David Carlier61fdf8b2015-10-02 11:59:38 +0100488 Register a Lua function executed as sample fetch. All the registered sample
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100489 fetchs can be used in HAProxy with the prefix "lua.". A Lua sample fetch
490 return a string as output. The registered function can take up to 9 values as
491 parameter. All the value are strings.
492
493 :param string name: is the name of the converter.
494 :param function func: is the Lua function called to work as sample fetch.
495
496 The prototype of the Lua function used as argument is:
497
498.. code-block:: lua
499
500 string function(txn, [p1 [, p2 [, ... [, p5]]]])
501..
502
Thierry FOURNIERdc595002015-12-21 11:13:52 +0100503 * **txn** (:ref:`txn_class`): this is the txn object associated with the current
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100504 request.
505 * **p1** .. **p5** (*string*): this is a list of string arguments declared in
506 the haroxy configuration file. The number of arguments doesn't exceed 5.
507 The order and the nature of these is conventionally choose by the
508 developper.
509 * **Returns**: A string containing some data, ot nil if the value cannot be
510 returned now.
511
512 lua example code:
513
514.. code-block:: lua
515
516 core.register_fetches("hello", function(txn)
517 return "hello"
518 end)
519..
520
521 HAProxy example configuration:
522
523::
524
525 frontend example
526 http-request redirect location /%[lua.hello]
527
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +0200528.. js:function:: core.register_service(name, mode, func)
529
530 **context**: body
531
David Carlier61fdf8b2015-10-02 11:59:38 +0100532 Register a Lua function executed as a service. All the registered service can
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +0200533 be used in HAProxy with the prefix "lua.". A service gets an object class as
534 input according with the required mode.
535
536 :param string name: is the name of the converter.
Willy Tarreau61add3c2015-09-28 15:39:10 +0200537 :param string mode: is string describing the required mode. Only 'tcp' or
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +0200538 'http' are allowed.
539 :param function func: is the Lua function called to work as converter.
540
541 The prototype of the Lua function used as argument is:
542
543.. code-block:: lua
544
545 function(applet)
546..
547
Thierry FOURNIERdc595002015-12-21 11:13:52 +0100548 * **applet** *applet* will be a :ref:`applettcp_class` or a
549 :ref:`applethttp_class`. It depends the type of registered applet. An applet
550 registered with the 'http' value for the *mode* parameter will gets a
551 :ref:`applethttp_class`. If the *mode* value is 'tcp', the applet will gets
552 a :ref:`applettcp_class`.
553
554 **warning**: Applets of type 'http' cannot be called from 'tcp-*'
555 rulesets. Only the 'http-*' rulesets are authorized, this means
556 that is not possible to call an HTTP applet from a proxy in tcp
557 mode. Applets of type 'tcp' can be called from anywhre.
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +0200558
Willy Tarreau61add3c2015-09-28 15:39:10 +0200559 Here, an exemple of service registration. the service just send an 'Hello world'
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +0200560 as an http response.
561
562.. code-block:: lua
563
Pieter Baauw4d7f7662015-11-08 16:38:08 +0100564 core.register_service("hello-world", "http", function(applet)
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +0200565 local response = "Hello World !"
566 applet:set_status(200)
Pieter Baauw2dcb9bc2015-10-01 22:47:12 +0200567 applet:add_header("content-length", string.len(response))
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +0200568 applet:add_header("content-type", "text/plain")
Pieter Baauw2dcb9bc2015-10-01 22:47:12 +0200569 applet:start_response()
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +0200570 applet:send(response)
571 end)
572..
573
574 This example code is used in HAproxy configuration like this:
575
576::
577
578 frontend example
579 http-request use-service lua.hello-world
580
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100581.. js:function:: core.register_init(func)
582
583 **context**: body
584
585 Register a function executed after the configuration parsing. This is useful
586 to check any parameters.
587
Pieter Baauw4d7f7662015-11-08 16:38:08 +0100588 :param function func: is the Lua function called to work as initializer.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100589
590 The prototype of the Lua function used as argument is:
591
592.. code-block:: lua
593
594 function()
595..
596
597 It takes no input, and no output is expected.
598
599.. js:function:: core.register_task(func)
600
601 **context**: body, init, task, action, sample-fetch, converter
602
603 Register and start independent task. The task is started when the HAProxy
604 main scheduler starts. For example this type of tasks can be executed to
Thierry FOURNIER2e4893c2015-03-18 13:37:27 +0100605 perform complex health checks.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100606
Pieter Baauw4d7f7662015-11-08 16:38:08 +0100607 :param function func: is the Lua function called to work as initializer.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100608
609 The prototype of the Lua function used as argument is:
610
611.. code-block:: lua
612
613 function()
614..
615
616 It takes no input, and no output is expected.
617
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +0100618.. js:function:: core.register_cli([path], usage, func)
619
620 **context**: body
621
622 Register and start independent task. The task is started when the HAProxy
623 main scheduler starts. For example this type of tasks can be executed to
624 perform complex health checks.
625
626 :param array path: is the sequence of word for which the cli execute the Lua
627 binding.
628 :param string usage: is the usage message displayed in the help.
629 :param function func: is the Lua function called to handle the CLI commands.
630
631 The prototype of the Lua function used as argument is:
632
633.. code-block:: lua
634
635 function(AppletTCP, [arg1, [arg2, [...]]])
636..
637
638 I/O are managed with the :ref:`applettcp_class` object. Args are given as
639 paramter. The args embbed the registred path. If the path is declared like
640 this:
641
642.. code-block:: lua
643
644 core.register_cli({"show", "ssl", "stats"}, "Display SSL stats..", function(applet, arg1, arg2, arg3, arg4, arg5)
645 end)
646..
647
648 And we execute this in the prompt:
649
650.. code-block:: text
651
652 > prompt
653 > show ssl stats all
654..
655
656 Then, arg1, arg2 and arg3 will contains respectivey "show", "ssl" and "stats".
657 arg4 will contain "all". arg5 contains nil.
658
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100659.. js:function:: core.set_nice(nice)
660
661 **context**: task, action, sample-fetch, converter
662
663 Change the nice of the current task or current session.
Thierry FOURNIER2e4893c2015-03-18 13:37:27 +0100664
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100665 :param integer nice: the nice value, it must be between -1024 and 1024.
666
667.. js:function:: core.set_map(filename, key, value)
668
669 **context**: init, task, action, sample-fetch, converter
670
671 set the value *value* associated to the key *key* in the map referenced by
672 *filename*.
673
Thierry FOURNIERdc595002015-12-21 11:13:52 +0100674 :param string filename: the Map reference
675 :param string key: the key to set or replace
676 :param string value: the associated value
677
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100678.. js:function:: core.sleep(int seconds)
679
680 **context**: body, init, task, action
681
682 The `core.sleep()` functions stop the Lua execution between specified seconds.
683
684 :param integer seconds: the required seconds.
685
686.. js:function:: core.tcp()
687
688 **context**: init, task, action
689
690 This function returns a new object of a *socket* class.
691
Thierry FOURNIERdc595002015-12-21 11:13:52 +0100692 :returns: A :ref:`socket_class` object.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100693
Thierry Fournier1de16592016-01-27 09:49:07 +0100694.. js:function:: core.concat()
695
696 **context**: body, init, task, action, sample-fetch, converter
697
698 This function retruns a new concat object.
699
700 :returns: A :ref:`concat_class` object.
701
Thierry FOURNIER0a99b892015-08-26 00:14:17 +0200702.. js:function:: core.done(data)
703
704 **context**: body, init, task, action, sample-fetch, converter
705
706 :param any data: Return some data for the caller. It is useful with
707 sample-fetches and sample-converters.
708
709 Immediately stops the current Lua execution and returns to the caller which
710 may be a sample fetch, a converter or an action and returns the specified
711 value (ignored for actions). It is used when the LUA process finishes its
712 work and wants to give back the control to HAProxy without executing the
713 remaining code. It can be seen as a multi-level "return".
714
Thierry FOURNIER486f5a02015-03-16 15:13:03 +0100715.. js:function:: core.yield()
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100716
717 **context**: task, action, sample-fetch, converter
718
719 Give back the hand at the HAProxy scheduler. It is used when the LUA
720 processing consumes a lot of processing time.
721
Thierry FOURNIER / OZON.IO62fec752016-11-10 20:38:11 +0100722.. js:function:: core.parse_addr(address)
723
724 **context**: body, init, task, action, sample-fetch, converter
725
726 :param network: is a string describing an ipv4 or ipv6 address and optionally
727 its network length, like this: "127.0.0.1/8" or "aaaa::1234/32".
728 :returns: a userdata containing network or nil if an error occurs.
729
730 Parse ipv4 or ipv6 adresses and its facultative associated network.
731
732.. js:function:: core.match_addr(addr1, addr2)
733
734 **context**: body, init, task, action, sample-fetch, converter
735
736 :param addr1: is an address created with "core.parse_addr".
737 :param addr2: is an address created with "core.parse_addr".
738 :returns: boolean, true if the network of the addresses matche, else returns
739 false.
740
741 Match two networks. For example "127.0.0.1/32" matchs "127.0.0.0/8". The order
742 of network is not important.
743
Thierry FOURNIER / OZON.IO8a1027a2016-11-24 20:48:38 +0100744.. js:function:: core.tokenize(str, separators [, noblank])
745
746 **context**: body, init, task, action, sample-fetch, converter
747
748 This function is useful for tokenizing an entry, or splitting some messages.
749 :param string str: The string which will be split.
750 :param string separators: A string containing a list of separators.
751 :param boolean noblank: Ignore empty entries.
752 :returns: an array of string.
753
754 For example:
755
756.. code-block:: lua
757
758 local array = core.tokenize("This function is useful, for tokenizing an entry.", "., ", true)
759 print_r(array)
760..
761
762 Returns this array:
763
764.. code-block:: text
765
766 (table) table: 0x21c01e0 [
767 1: (string) "This"
768 2: (string) "function"
769 3: (string) "is"
770 4: (string) "useful"
771 5: (string) "for"
772 6: (string) "tokenizing"
773 7: (string) "an"
774 8: (string) "entry"
775 ]
776..
777
Thierry Fournierf61aa632016-02-19 20:56:00 +0100778.. _proxy_class:
779
780Proxy class
781============
782
783.. js:class:: Proxy
784
785 This class provides a way for manipulating proxy and retrieving information
786 like statistics.
787
Thierry FOURNIER817e7592017-07-24 14:35:04 +0200788.. js:attribute:: Proxy.name
789
790 Contain the name of the proxy.
791
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +0100792.. js:attribute:: Proxy.servers
793
794 Contain an array with the attached servers. Each server entry is an object of
795 type :ref:`server_class`.
796
Thierry Fournierff480422016-02-25 08:36:46 +0100797.. js:attribute:: Proxy.listeners
798
799 Contain an array with the attached listeners. Each listeners entry is an
800 object of type :ref:`listener_class`.
801
Thierry Fournierf61aa632016-02-19 20:56:00 +0100802.. js:function:: Proxy.pause(px)
803
804 Pause the proxy. See the management socket documentation for more information.
805
806 :param class_proxy px: A :ref:`proxy_class` which indicates the manipulated
807 proxy.
808
809.. js:function:: Proxy.resume(px)
810
811 Resume the proxy. See the management socket documentation for more
812 information.
813
814 :param class_proxy px: A :ref:`proxy_class` which indicates the manipulated
815 proxy.
816
817.. js:function:: Proxy.stop(px)
818
819 Stop the proxy. See the management socket documentation for more information.
820
821 :param class_proxy px: A :ref:`proxy_class` which indicates the manipulated
822 proxy.
823
824.. js:function:: Proxy.shut_bcksess(px)
825
826 Kill the session attached to a backup server. See the management socket
827 documentation for more information.
828
829 :param class_proxy px: A :ref:`proxy_class` which indicates the manipulated
830 proxy.
831
832.. js:function:: Proxy.get_cap(px)
833
834 Returns a string describing the capabilities of the proxy.
835
836 :param class_proxy px: A :ref:`proxy_class` which indicates the manipulated
837 proxy.
838 :returns: a string "frontend", "backend", "proxy" or "ruleset".
839
840.. js:function:: Proxy.get_mode(px)
841
842 Returns a string describing the mode of the current proxy.
843
844 :param class_proxy px: A :ref:`proxy_class` which indicates the manipulated
845 proxy.
846 :returns: a string "tcp", "http", "health" or "unknown"
847
848.. js:function:: Proxy.get_stats(px)
849
850 Returns an array containg the proxy statistics. The statistics returned are
851 not the same if the proxy is frontend or a backend.
852
853 :param class_proxy px: A :ref:`proxy_class` which indicates the manipulated
854 proxy.
855 :returns: a key/value array containing stats
856
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +0100857.. _server_class:
858
859Server class
860============
861
862.. js:function:: Server.is_draining(sv)
863
864 Return true if the server is currently draining stiky connections.
865
866 :param class_server sv: A :ref:`server_class` which indicates the manipulated
867 server.
868 :returns: a boolean
869
870.. js:function:: Server.set_weight(sv, weight)
871
872 Dynamically change the weight of the serveur. See the management socket
873 documentation for more information about the format of the string.
874
875 :param class_server sv: A :ref:`server_class` which indicates the manipulated
876 server.
877 :param string weight: A string describing the server weight.
878
879.. js:function:: Server.get_weight(sv)
880
881 This function returns an integer representing the serveur weight.
882
883 :param class_server sv: A :ref:`server_class` which indicates the manipulated
884 server.
885 :returns: an integer.
886
887.. js:function:: Server.set_addr(sv, addr)
888
889 Dynamically change the address of the serveur. See the management socket
890 documentation for more information about the format of the string.
891
892 :param class_server sv: A :ref:`server_class` which indicates the manipulated
893 server.
894 :param string weight: A string describing the server address.
895
896.. js:function:: Server.get_addr(sv)
897
898 Returns a string describing the address of the serveur.
899
900 :param class_server sv: A :ref:`server_class` which indicates the manipulated
901 server.
902 :returns: A string
903
904.. js:function:: Server.get_stats(sv)
905
906 Returns server statistics.
907
908 :param class_server sv: A :ref:`server_class` which indicates the manipulated
909 server.
910 :returns: a key/value array containing stats
911
912.. js:function:: Server.shut_sess(sv)
913
914 Shutdown all the sessions attached to the server. See the management socket
915 documentation for more information about this function.
916
917 :param class_server sv: A :ref:`server_class` which indicates the manipulated
918 server.
919
920.. js:function:: Server.set_drain(sv)
921
922 Drain sticky sessions. See the management socket documentation for more
923 information about this function.
924
925 :param class_server sv: A :ref:`server_class` which indicates the manipulated
926 server.
927
928.. js:function:: Server.set_maint(sv)
929
930 Set maintenance mode. See the management socket documentation for more
931 information about this function.
932
933 :param class_server sv: A :ref:`server_class` which indicates the manipulated
934 server.
935
936.. js:function:: Server.set_ready(sv)
937
938 Set normal mode. See the management socket documentation for more information
939 about this function.
940
941 :param class_server sv: A :ref:`server_class` which indicates the manipulated
942 server.
943
944.. js:function:: Server.check_enable(sv)
945
946 Enable health checks. See the management socket documentation for more
947 information about this function.
948
949 :param class_server sv: A :ref:`server_class` which indicates the manipulated
950 server.
951
952.. js:function:: Server.check_disable(sv)
953
954 Disable health checks. See the management socket documentation for more
955 information about this function.
956
957 :param class_server sv: A :ref:`server_class` which indicates the manipulated
958 server.
959
960.. js:function:: Server.check_force_up(sv)
961
962 Force health-check up. See the management socket documentation for more
963 information about this function.
964
965 :param class_server sv: A :ref:`server_class` which indicates the manipulated
966 server.
967
968.. js:function:: Server.check_force_nolb(sv)
969
970 Force health-check nolb mode. See the management socket documentation for more
971 information about this function.
972
973 :param class_server sv: A :ref:`server_class` which indicates the manipulated
974 server.
975
976.. js:function:: Server.check_force_down(sv)
977
978 Force health-check down. See the management socket documentation for more
979 information about this function.
980
981 :param class_server sv: A :ref:`server_class` which indicates the manipulated
982 server.
983
984.. js:function:: Server.agent_enable(sv)
985
986 Enable agent check. See the management socket documentation for more
987 information about this function.
988
989 :param class_server sv: A :ref:`server_class` which indicates the manipulated
990 server.
991
992.. js:function:: Server.agent_disable(sv)
993
994 Disable agent check. See the management socket documentation for more
995 information about this function.
996
997 :param class_server sv: A :ref:`server_class` which indicates the manipulated
998 server.
999
1000.. js:function:: Server.agent_force_up(sv)
1001
1002 Force agent check up. See the management socket documentation for more
1003 information about this function.
1004
1005 :param class_server sv: A :ref:`server_class` which indicates the manipulated
1006 server.
1007
1008.. js:function:: Server.agent_force_down(sv)
1009
1010 Force agent check down. See the management socket documentation for more
1011 information about this function.
1012
1013 :param class_server sv: A :ref:`server_class` which indicates the manipulated
1014 server.
1015
Thierry Fournierff480422016-02-25 08:36:46 +01001016.. _listener_class:
1017
1018Listener class
1019==============
1020
1021.. js:function:: Listener.get_stats(ls)
1022
1023 Returns server statistics.
1024
1025 :param class_listener ls: A :ref:`listener_class` which indicates the
1026 manipulated listener.
1027 :returns: a key/value array containing stats
1028
Thierry Fournier1de16592016-01-27 09:49:07 +01001029.. _concat_class:
1030
1031Concat class
1032============
1033
1034.. js:class:: Concat
1035
1036 This class provides a fast way for string concatenation. The way using native
1037 Lua concatenation like the code below is slow for some reasons.
1038
1039.. code-block:: lua
1040
1041 str = "string1"
1042 str = str .. ", string2"
1043 str = str .. ", string3"
1044..
1045
1046 For each concatenation, Lua:
1047 * allocate memory for the result,
1048 * catenate the two string copying the strings in the new memory bloc,
1049 * free the old memory block containing the string whoch is no longer used.
1050 This process does many memory move, allocation and free. In addition, the
1051 memory is not really freed, it is just mark mark as unsused and wait for the
1052 garbage collector.
1053
1054 The Concat class provide an alternative way for catenating strings. It uses
1055 the internal Lua mechanism (it does not allocate memory), but it doesn't copy
1056 the data more than once.
1057
1058 On my computer, the following loops spends 0.2s for the Concat method and
1059 18.5s for the pure Lua implementation. So, the Concat class is about 1000x
1060 faster than the embedded solution.
1061
1062.. code-block:: lua
1063
1064 for j = 1, 100 do
1065 c = core.concat()
1066 for i = 1, 20000 do
1067 c:add("#####")
1068 end
1069 end
1070..
1071
1072.. code-block:: lua
1073
1074 for j = 1, 100 do
1075 c = ""
1076 for i = 1, 20000 do
1077 c = c .. "#####"
1078 end
1079 end
1080..
1081
1082.. js:function:: Concat.add(concat, string)
1083
1084 This function adds a string to the current concatenated string.
1085
1086 :param class_concat concat: A :ref:`concat_class` which contains the currently
1087 builded string.
1088 :param string string: A new string to concatenate to the current builded
1089 string.
1090
1091.. js:function:: Concat.dump(concat)
1092
1093 This function returns the concanated string.
1094
1095 :param class_concat concat: A :ref:`concat_class` which contains the currently
1096 builded string.
1097 :returns: the concatenated string
1098
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001099.. _fetches_class:
1100
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001101Fetches class
1102=============
1103
1104.. js:class:: Fetches
1105
1106 This class contains a lot of internal HAProxy sample fetches. See the
Thierry FOURNIER2e4893c2015-03-18 13:37:27 +01001107 HAProxy "configuration.txt" documentation for more information about her
1108 usage. they are the chapters 7.3.2 to 7.3.6.
1109
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001110 **warning** some sample fetches are not available in some context. These
1111 limitations are specified in this documentation when theire useful.
1112
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01001113 :see: :js:attr:`TXN.f`
1114 :see: :js:attr:`TXN.sf`
Thierry FOURNIER2e4893c2015-03-18 13:37:27 +01001115
1116 Fetches are useful for:
1117
1118 * get system time,
1119 * get environment variable,
1120 * get random numbers,
1121 * known backend status like the number of users in queue or the number of
1122 connections established,
1123 * client information like ip source or destination,
1124 * deal with stick tables,
1125 * Established SSL informations,
1126 * HTTP information like headers or method.
1127
1128.. code-block:: lua
1129
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001130 function action(txn)
1131 -- Get source IP
1132 local clientip = txn.f:src()
1133 end
Thierry FOURNIER2e4893c2015-03-18 13:37:27 +01001134..
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001135
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001136.. _converters_class:
1137
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001138Converters class
1139================
1140
1141.. js:class:: Converters
1142
1143 This class contains a lot of internal HAProxy sample converters. See the
Thierry FOURNIER2e4893c2015-03-18 13:37:27 +01001144 HAProxy documentation "configuration.txt" for more information about her
1145 usage. Its the chapter 7.3.1.
1146
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01001147 :see: :js:attr:`TXN.c`
1148 :see: :js:attr:`TXN.sc`
Thierry FOURNIER2e4893c2015-03-18 13:37:27 +01001149
1150 Converters provides statefull transformation. They are useful for:
1151
1152 * converting input to base64,
1153 * applying hash on input string (djb2, crc32, sdbm, wt6),
1154 * format date,
1155 * json escape,
Pieter Baauw4d7f7662015-11-08 16:38:08 +01001156 * extracting preferred language comparing two lists,
Thierry FOURNIER2e4893c2015-03-18 13:37:27 +01001157 * turn to lower or upper chars,
1158 * deal with stick tables.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001159
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001160.. _channel_class:
1161
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001162Channel class
1163=============
1164
1165.. js:class:: Channel
1166
1167 HAProxy uses two buffers for the processing of the requests. The first one is
1168 used with the request data (from the client to the server) and the second is
1169 used for the response data (from the server to the client).
1170
1171 Each buffer contains two types of data. The first type is the incoming data
1172 waiting for a processing. The second part is the outgoing data already
1173 processed. Usually, the incoming data is processed, after it is tagged as
1174 outgoing data, and finally it is sent. The following functions provides tools
1175 for manipulating these data in a buffer.
1176
1177 The following diagram shows where the channel class function are applied.
1178
1179 **Warning**: It is not possible to read from the response in request action,
1180 and it is not possible to read for the request channel in response action.
1181
Thierry FOURNIER2e4893c2015-03-18 13:37:27 +01001182.. image:: _static/channel.png
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001183
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001184.. js:function:: Channel.dup(channel)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001185
1186 This function returns a string that contain the entire buffer. The data is
1187 not remove from the buffer and can be reprocessed later.
1188
1189 If the buffer cant receive more data, a 'nil' value is returned.
1190
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001191 :param class_channel channel: The manipulated Channel.
Pieter Baauw4d7f7662015-11-08 16:38:08 +01001192 :returns: a string containing all the available data or nil.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001193
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001194.. js:function:: Channel.get(channel)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001195
1196 This function returns a string that contain the entire buffer. The data is
1197 consumed from the buffer.
1198
1199 If the buffer cant receive more data, a 'nil' value is returned.
1200
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001201 :param class_channel channel: The manipulated Channel.
Pieter Baauw4d7f7662015-11-08 16:38:08 +01001202 :returns: a string containing all the available data or nil.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001203
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02001204.. js:function:: Channel.getline(channel)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001205
1206 This function returns a string that contain the first line of the buffer. The
1207 data is consumed. If the data returned doesn't contains a final '\n' its
1208 assumed than its the last available data in the buffer.
1209
1210 If the buffer cant receive more data, a 'nil' value is returned.
1211
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001212 :param class_channel channel: The manipulated Channel.
Pieter Baauw386a1272015-08-16 15:26:24 +02001213 :returns: a string containing the available line or nil.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001214
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001215.. js:function:: Channel.set(channel, string)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001216
1217 This function replace the content of the buffer by the string. The function
1218 returns the copied length, otherwise, it returns -1.
1219
1220 The data set with this function are not send. They wait for the end of
1221 HAProxy processing, so the buffer can be full.
1222
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001223 :param class_channel channel: The manipulated Channel.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001224 :param string string: The data which will sent.
Pieter Baauw4d7f7662015-11-08 16:38:08 +01001225 :returns: an integer containing the amount of bytes copied or -1.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001226
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001227.. js:function:: Channel.append(channel, string)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001228
1229 This function append the string argument to the content of the buffer. The
1230 function returns the copied length, otherwise, it returns -1.
1231
1232 The data set with this function are not send. They wait for the end of
1233 HAProxy processing, so the buffer can be full.
1234
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001235 :param class_channel channel: The manipulated Channel.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001236 :param string string: The data which will sent.
Pieter Baauw4d7f7662015-11-08 16:38:08 +01001237 :returns: an integer containing the amount of bytes copied or -1.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001238
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001239.. js:function:: Channel.send(channel, string)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001240
1241 This function required immediate send of the data. Unless if the connection
1242 is close, the buffer is regularly flushed and all the string can be sent.
1243
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001244 :param class_channel channel: The manipulated Channel.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001245 :param string string: The data which will sent.
Pieter Baauw4d7f7662015-11-08 16:38:08 +01001246 :returns: an integer containing the amount of bytes copied or -1.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001247
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001248.. js:function:: Channel.get_in_length(channel)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001249
1250 This function returns the length of the input part of the buffer.
1251
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001252 :param class_channel channel: The manipulated Channel.
Pieter Baauw4d7f7662015-11-08 16:38:08 +01001253 :returns: an integer containing the amount of available bytes.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001254
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001255.. js:function:: Channel.get_out_length(channel)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001256
1257 This function returns the length of the output part of the buffer.
1258
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001259 :param class_channel channel: The manipulated Channel.
Pieter Baauw4d7f7662015-11-08 16:38:08 +01001260 :returns: an integer containing the amount of available bytes.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001261
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001262.. js:function:: Channel.forward(channel, int)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001263
1264 This function transfer bytes from the input part of the buffer to the output
1265 part.
1266
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001267 :param class_channel channel: The manipulated Channel.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001268 :param integer int: The amount of data which will be forwarded.
1269
Thierry FOURNIER / OZON.IO65192f32016-11-07 15:28:40 +01001270.. js:function:: Channel.is_full(channel)
1271
1272 This function returns true if the buffer channel is full.
1273
1274 :returns: a boolean
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001275
1276.. _http_class:
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001277
1278HTTP class
1279==========
1280
1281.. js:class:: HTTP
1282
1283 This class contain all the HTTP manipulation functions.
1284
Pieter Baauw386a1272015-08-16 15:26:24 +02001285.. js:function:: HTTP.req_get_headers(http)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001286
1287 Returns an array containing all the request headers.
1288
1289 :param class_http http: The related http object.
1290 :returns: array of headers.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01001291 :see: :js:func:`HTTP.res_get_headers`
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001292
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001293 This is the form of the returned array:
1294
1295.. code-block:: lua
1296
1297 HTTP:req_get_headers()['<header-name>'][<header-index>] = "<header-value>"
1298
1299 local hdr = HTTP:req_get_headers()
1300 hdr["host"][0] = "www.test.com"
1301 hdr["accept"][0] = "audio/basic q=1"
1302 hdr["accept"][1] = "audio/*, q=0.2"
1303 hdr["accept"][2] = "*/*, q=0.1"
1304..
1305
Pieter Baauw386a1272015-08-16 15:26:24 +02001306.. js:function:: HTTP.res_get_headers(http)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001307
1308 Returns an array containing all the response headers.
1309
1310 :param class_http http: The related http object.
1311 :returns: array of headers.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01001312 :see: :js:func:`HTTP.req_get_headers`
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001313
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001314 This is the form of the returned array:
1315
1316.. code-block:: lua
1317
1318 HTTP:res_get_headers()['<header-name>'][<header-index>] = "<header-value>"
1319
1320 local hdr = HTTP:req_get_headers()
1321 hdr["host"][0] = "www.test.com"
1322 hdr["accept"][0] = "audio/basic q=1"
1323 hdr["accept"][1] = "audio/*, q=0.2"
1324 hdr["accept"][2] = "*.*, q=0.1"
1325..
1326
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001327.. js:function:: HTTP.req_add_header(http, name, value)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001328
1329 Appends an HTTP header field in the request whose name is
1330 specified in "name" and whose value is defined in "value".
1331
1332 :param class_http http: The related http object.
1333 :param string name: The header name.
1334 :param string value: The header value.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01001335 :see: :js:func:`HTTP.res_add_header`
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001336
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001337.. js:function:: HTTP.res_add_header(http, name, value)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001338
1339 appends an HTTP header field in the response whose name is
1340 specified in "name" and whose value is defined in "value".
1341
1342 :param class_http http: The related http object.
1343 :param string name: The header name.
1344 :param string value: The header value.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01001345 :see: :js:func:`HTTP.req_add_header`
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001346
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001347.. js:function:: HTTP.req_del_header(http, name)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001348
1349 Removes all HTTP header fields in the request whose name is
1350 specified in "name".
1351
1352 :param class_http http: The related http object.
1353 :param string name: The header name.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01001354 :see: :js:func:`HTTP.res_del_header`
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001355
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001356.. js:function:: HTTP.res_del_header(http, name)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001357
1358 Removes all HTTP header fields in the response whose name is
1359 specified in "name".
1360
1361 :param class_http http: The related http object.
1362 :param string name: The header name.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01001363 :see: :js:func:`HTTP.req_del_header`
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001364
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001365.. js:function:: HTTP.req_set_header(http, name, value)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001366
1367 This variable replace all occurence of all header "name", by only
1368 one containing the "value".
1369
1370 :param class_http http: The related http object.
1371 :param string name: The header name.
1372 :param string value: The header value.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01001373 :see: :js:func:`HTTP.res_set_header`
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001374
1375 This function does the same work as the folowwing code:
1376
1377.. code-block:: lua
1378
1379 function fcn(txn)
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001380 TXN.http:req_del_header("header")
1381 TXN.http:req_add_header("header", "value")
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001382 end
1383..
1384
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001385.. js:function:: HTTP.res_set_header(http, name, value)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001386
1387 This variable replace all occurence of all header "name", by only
1388 one containing the "value".
1389
1390 :param class_http http: The related http object.
1391 :param string name: The header name.
1392 :param string value: The header value.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01001393 :see: :js:func:`HTTP.req_rep_header()`
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001394
Pieter Baauw386a1272015-08-16 15:26:24 +02001395.. js:function:: HTTP.req_rep_header(http, name, regex, replace)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001396
1397 Matches the regular expression in all occurrences of header field "name"
1398 according to "regex", and replaces them with the "replace" argument. The
1399 replacement value can contain back references like \1, \2, ... This
1400 function works with the request.
1401
1402 :param class_http http: The related http object.
1403 :param string name: The header name.
1404 :param string regex: The match regular expression.
1405 :param string replace: The replacement value.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01001406 :see: :js:func:`HTTP.res_rep_header()`
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001407
Pieter Baauw386a1272015-08-16 15:26:24 +02001408.. js:function:: HTTP.res_rep_header(http, name, regex, string)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001409
1410 Matches the regular expression in all occurrences of header field "name"
1411 according to "regex", and replaces them with the "replace" argument. The
1412 replacement value can contain back references like \1, \2, ... This
1413 function works with the request.
1414
1415 :param class_http http: The related http object.
1416 :param string name: The header name.
1417 :param string regex: The match regular expression.
1418 :param string replace: The replacement value.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01001419 :see: :js:func:`HTTP.req_rep_header()`
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001420
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001421.. js:function:: HTTP.req_set_method(http, method)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001422
1423 Rewrites the request method with the parameter "method".
1424
1425 :param class_http http: The related http object.
1426 :param string method: The new method.
1427
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001428.. js:function:: HTTP.req_set_path(http, path)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001429
1430 Rewrites the request path with the "path" parameter.
1431
1432 :param class_http http: The related http object.
1433 :param string path: The new path.
1434
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001435.. js:function:: HTTP.req_set_query(http, query)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001436
1437 Rewrites the request's query string which appears after the first question
1438 mark ("?") with the parameter "query".
1439
1440 :param class_http http: The related http object.
1441 :param string query: The new query.
1442
Thierry FOURNIER0d79cf62015-08-26 14:20:58 +02001443.. js:function:: HTTP.req_set_uri(http, uri)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001444
1445 Rewrites the request URI with the parameter "uri".
1446
1447 :param class_http http: The related http object.
1448 :param string uri: The new uri.
1449
Robin H. Johnson52f5db22017-01-01 13:10:52 -08001450.. js:function:: HTTP.res_set_status(http, status [, reason])
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +02001451
Robin H. Johnson52f5db22017-01-01 13:10:52 -08001452 Rewrites the response status code with the parameter "code".
1453
1454 If no custom reason is provided, it will be generated from the status.
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +02001455
1456 :param class_http http: The related http object.
1457 :param integer status: The new response status code.
Robin H. Johnson52f5db22017-01-01 13:10:52 -08001458 :param string reason: The new response reason (optional).
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +02001459
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001460.. _txn_class:
1461
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001462TXN class
1463=========
1464
1465.. js:class:: TXN
1466
1467 The txn class contain all the functions relative to the http or tcp
1468 transaction (Note than a tcp stream is the same than a tcp transaction, but
1469 an HTTP transaction is not the same than a tcp stream).
1470
1471 The usage of this class permits to retrieve data from the requests, alter it
1472 and forward it.
1473
1474 All the functions provided by this class are available in the context
1475 **sample-fetches** and **actions**.
1476
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001477.. js:attribute:: TXN.c
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001478
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001479 :returns: An :ref:`converters_class`.
1480
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001481 This attribute contains a Converters class object.
1482
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001483.. js:attribute:: TXN.sc
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001484
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001485 :returns: An :ref:`converters_class`.
1486
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001487 This attribute contains a Converters class object. The functions of
1488 this object returns always a string.
1489
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001490.. js:attribute:: TXN.f
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001491
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001492 :returns: An :ref:`fetches_class`.
1493
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001494 This attribute contains a Fetches class object.
1495
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001496.. js:attribute:: TXN.sf
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001497
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001498 :returns: An :ref:`fetches_class`.
1499
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001500 This attribute contains a Fetches class object. The functions of
1501 this object returns always a string.
1502
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001503.. js:attribute:: TXN.req
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001504
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001505 :returns: An :ref:`channel_class`.
1506
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001507 This attribute contains a channel class object for the request buffer.
1508
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001509.. js:attribute:: TXN.res
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001510
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001511 :returns: An :ref:`channel_class`.
1512
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001513 This attribute contains a channel class object for the response buffer.
1514
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001515.. js:attribute:: TXN.http
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001516
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001517 :returns: An :ref:`http_class`.
1518
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001519 This attribute contains an HTTP class object. It is avalaible only if the
1520 proxy has the "mode http" enabled.
1521
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01001522.. js:function:: TXN.log(TXN, loglevel, msg)
1523
1524 This function sends a log. The log is sent, according with the HAProxy
1525 configuration file, on the default syslog server if it is configured and on
1526 the stderr if it is allowed.
1527
1528 :param class_txn txn: The class txn object containing the data.
1529 :param integer loglevel: Is the log level asociated with the message. It is a
1530 number between 0 and 7.
1531 :param string msg: The log content.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01001532 :see: :js:attr:`core.emerg`, :js:attr:`core.alert`, :js:attr:`core.crit`,
1533 :js:attr:`core.err`, :js:attr:`core.warning`, :js:attr:`core.notice`,
1534 :js:attr:`core.info`, :js:attr:`core.debug` (log level definitions)
1535 :see: :js:func:`TXN.deflog`
1536 :see: :js:func:`TXN.Debug`
1537 :see: :js:func:`TXN.Info`
1538 :see: :js:func:`TXN.Warning`
1539 :see: :js:func:`TXN.Alert`
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01001540
1541.. js:function:: TXN.deflog(TXN, msg)
1542
1543 Sends a log line with the default loglevel for the proxy ssociated with the
1544 transaction.
1545
1546 :param class_txn txn: The class txn object containing the data.
1547 :param string msg: The log content.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01001548 :see: :js:func:`TXN.log
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01001549
1550.. js:function:: TXN.Debug(txn, msg)
1551
1552 :param class_txn txn: The class txn object containing the data.
1553 :param string msg: The log content.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01001554 :see: :js:func:`TXN.log`
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01001555
1556 Does the same job than:
1557
1558.. code-block:: lua
1559
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001560 function Debug(txn, msg)
1561 TXN.log(txn, core.debug, msg)
1562 end
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01001563..
1564
1565.. js:function:: TXN.Info(txn, msg)
1566
1567 :param class_txn txn: The class txn object containing the data.
1568 :param string msg: The log content.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01001569 :see: :js:func:`TXN.log`
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01001570
1571.. code-block:: lua
1572
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001573 function Debug(txn, msg)
1574 TXN.log(txn, core.info, msg)
1575 end
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01001576..
1577
1578.. js:function:: TXN.Warning(txn, msg)
1579
1580 :param class_txn txn: The class txn object containing the data.
1581 :param string msg: The log content.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01001582 :see: :js:func:`TXN.log`
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01001583
1584.. code-block:: lua
1585
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001586 function Debug(txn, msg)
1587 TXN.log(txn, core.warning, msg)
1588 end
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01001589..
1590
1591.. js:function:: TXN.Alert(txn, msg)
1592
1593 :param class_txn txn: The class txn object containing the data.
1594 :param string msg: The log content.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01001595 :see: :js:func:`TXN.log`
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01001596
1597.. code-block:: lua
1598
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001599 function Debug(txn, msg)
1600 TXN.log(txn, core.alert, msg)
1601 end
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01001602..
1603
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001604.. js:function:: TXN.get_priv(txn)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001605
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001606 Return Lua data stored in the current transaction (with the `TXN.set_priv()`)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001607 function. If no data are stored, it returns a nil value.
1608
1609 :param class_txn txn: The class txn object containing the data.
1610 :returns: the opaque data previsously stored, or nil if nothing is
1611 avalaible.
1612
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001613.. js:function:: TXN.set_priv(txn, data)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001614
1615 Store any data in the current HAProxy transaction. This action replace the
1616 old stored data.
1617
1618 :param class_txn txn: The class txn object containing the data.
1619 :param opaque data: The data which is stored in the transaction.
1620
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +02001621.. js:function:: TXN.set_var(TXN, var, value)
1622
David Carlier61fdf8b2015-10-02 11:59:38 +01001623 Converts a Lua type in a HAProxy type and store it in a variable <var>.
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +02001624
1625 :param class_txn txn: The class txn object containing the data.
1626 :param string var: The variable name according with the HAProxy variable syntax.
Thierry FOURNIER / OZON.IOb210bcc2016-12-12 16:24:16 +01001627 :param type value: The value associated to the variable. The type can be string or
1628 integer.
Christopher Faulet85d79c92016-11-09 16:54:56 +01001629
1630.. js:function:: TXN.unset_var(TXN, var)
1631
1632 Unset the variable <var>.
1633
1634 :param class_txn txn: The class txn object containing the data.
1635 :param string var: The variable name according with the HAProxy variable syntax.
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +02001636
1637.. js:function:: TXN.get_var(TXN, var)
1638
1639 Returns data stored in the variable <var> converter in Lua type.
1640
1641 :param class_txn txn: The class txn object containing the data.
1642 :param string var: The variable name according with the HAProxy variable syntax.
1643
Willy Tarreaubc183a62015-08-28 10:39:11 +02001644.. js:function:: TXN.done(txn)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001645
Willy Tarreaubc183a62015-08-28 10:39:11 +02001646 This function terminates processing of the transaction and the associated
1647 session. It can be used when a critical error is detected or to terminate
1648 processing after some data have been returned to the client (eg: a redirect).
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001649
Thierry FOURNIERab00df62016-07-14 11:42:37 +02001650 *Warning*: It not make sense to call this function from sample-fetches. In
1651 this case the behaviour of this one is the same than core.done(): it quit
1652 the Lua execution. The transaction is really aborted only from an action
1653 registered function.
1654
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001655 :param class_txn txn: The class txn object containing the data.
1656
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001657.. js:function:: TXN.set_loglevel(txn, loglevel)
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01001658
1659 Is used to change the log level of the current request. The "loglevel" must
1660 be an integer between 0 and 7.
1661
1662 :param class_txn txn: The class txn object containing the data.
1663 :param integer loglevel: The required log level. This variable can be one of
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01001664 :see: :js:attr:`core.emerg`, :js:attr:`core.alert`, :js:attr:`core.crit`,
1665 :js:attr:`core.err`, :js:attr:`core.warning`, :js:attr:`core.notice`,
1666 :js:attr:`core.info`, :js:attr:`core.debug` (log level definitions)
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01001667
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001668.. js:function:: TXN.set_tos(txn, tos)
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01001669
1670 Is used to set the TOS or DSCP field value of packets sent to the client to
1671 the value passed in "tos" on platforms which support this.
1672
1673 :param class_txn txn: The class txn object containing the data.
1674 :param integer tos: The new TOS os DSCP.
1675
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001676.. js:function:: TXN.set_mark(txn, mark)
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01001677
1678 Is used to set the Netfilter MARK on all packets sent to the client to the
1679 value passed in "mark" on platforms which support it.
1680
1681 :param class_txn txn: The class txn object containing the data.
1682 :param integer mark: The mark value.
1683
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001684.. _socket_class:
1685
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001686Socket class
1687============
1688
1689.. js:class:: Socket
1690
1691 This class must be compatible with the Lua Socket class. Only the 'client'
1692 functions are available. See the Lua Socket documentation:
1693
1694 `http://w3.impa.br/~diego/software/luasocket/tcp.html
1695 <http://w3.impa.br/~diego/software/luasocket/tcp.html>`_
1696
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001697.. js:function:: Socket.close(socket)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001698
1699 Closes a TCP object. The internal socket used by the object is closed and the
1700 local address to which the object was bound is made available to other
1701 applications. No further operations (except for further calls to the close
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001702 method) are allowed on a closed Socket.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001703
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001704 :param class_socket socket: Is the manipulated Socket.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001705
1706 Note: It is important to close all used sockets once they are not needed,
1707 since, in many systems, each socket uses a file descriptor, which are limited
1708 system resources. Garbage-collected objects are automatically closed before
1709 destruction, though.
1710
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02001711.. js:function:: Socket.connect(socket, address[, port])
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001712
1713 Attempts to connect a socket object to a remote host.
1714
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001715
1716 In case of error, the method returns nil followed by a string describing the
1717 error. In case of success, the method returns 1.
1718
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001719 :param class_socket socket: Is the manipulated Socket.
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02001720 :param string address: can be an IP address or a host name. See below for more
1721 information.
1722 :param integer port: must be an integer number in the range [1..64K].
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001723 :returns: 1 or nil.
1724
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02001725 an address field extension permits to use the connect() function to connect to
1726 other stream than TCP. The syntax containing a simpleipv4 or ipv6 address is
1727 the basically expected format. This format requires the port.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001728
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02001729 Other format accepted are a socket path like "/socket/path", it permits to
1730 connect to a socket. abstract namespaces are supported with the prefix
1731 "abns@", and finaly a filedescriotr can be passed with the prefix "fd@".
1732 The prefix "ipv4@", "ipv6@" and "unix@" are also supported. The port can be
1733 passed int the string. The syntax "127.0.0.1:1234" is valid. in this case, the
1734 parameter *port* is ignored.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001735
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001736.. js:function:: Socket.connect_ssl(socket, address, port)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001737
1738 Same behavior than the function socket:connect, but uses SSL.
1739
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001740 :param class_socket socket: Is the manipulated Socket.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001741 :returns: 1 or nil.
1742
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001743.. js:function:: Socket.getpeername(socket)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001744
1745 Returns information about the remote side of a connected client object.
1746
1747 Returns a string with the IP address of the peer, followed by the port number
1748 that peer is using for the connection. In case of error, the method returns
1749 nil.
1750
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001751 :param class_socket socket: Is the manipulated Socket.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001752 :returns: a string containing the server information.
1753
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001754.. js:function:: Socket.getsockname(socket)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001755
1756 Returns the local address information associated to the object.
1757
1758 The method returns a string with local IP address and a number with the port.
1759 In case of error, the method returns nil.
1760
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001761 :param class_socket socket: Is the manipulated Socket.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001762 :returns: a string containing the client information.
1763
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001764.. js:function:: Socket.receive(socket, [pattern [, prefix]])
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001765
1766 Reads data from a client object, according to the specified read pattern.
1767 Patterns follow the Lua file I/O format, and the difference in performance
1768 between all patterns is negligible.
1769
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001770 :param class_socket socket: Is the manipulated Socket.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001771 :param string|integer pattern: Describe what is required (see below).
1772 :param string prefix: A string which will be prefix the returned data.
1773 :returns: a string containing the required data or nil.
1774
1775 Pattern can be any of the following:
1776
1777 * **`*a`**: reads from the socket until the connection is closed. No
1778 end-of-line translation is performed;
1779
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001780 * **`*l`**: reads a line of text from the Socket. The line is terminated by a
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001781 LF character (ASCII 10), optionally preceded by a CR character
1782 (ASCII 13). The CR and LF characters are not included in the
1783 returned line. In fact, all CR characters are ignored by the
1784 pattern. This is the default pattern.
1785
1786 * **number**: causes the method to read a specified number of bytes from the
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001787 Socket. Prefix is an optional string to be concatenated to the
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001788 beginning of any received data before return.
1789
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02001790 * **empty**: If the pattern is left empty, the default option is `*l`.
1791
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001792 If successful, the method returns the received pattern. In case of error, the
1793 method returns nil followed by an error message which can be the string
1794 'closed' in case the connection was closed before the transmission was
1795 completed or the string 'timeout' in case there was a timeout during the
1796 operation. Also, after the error message, the function returns the partial
1797 result of the transmission.
1798
1799 Important note: This function was changed severely. It used to support
1800 multiple patterns (but I have never seen this feature used) and now it
1801 doesn't anymore. Partial results used to be returned in the same way as
1802 successful results. This last feature violated the idea that all functions
1803 should return nil on error. Thus it was changed too.
1804
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001805.. js:function:: Socket.send(socket, data [, start [, end ]])
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001806
1807 Sends data through client object.
1808
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001809 :param class_socket socket: Is the manipulated Socket.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001810 :param string data: The data that will be sent.
1811 :param integer start: The start position in the buffer of the data which will
1812 be sent.
1813 :param integer end: The end position in the buffer of the data which will
1814 be sent.
1815 :returns: see below.
1816
1817 Data is the string to be sent. The optional arguments i and j work exactly
1818 like the standard string.sub Lua function to allow the selection of a
1819 substring to be sent.
1820
1821 If successful, the method returns the index of the last byte within [start,
1822 end] that has been sent. Notice that, if start is 1 or absent, this is
1823 effectively the total number of bytes sent. In case of error, the method
1824 returns nil, followed by an error message, followed by the index of the last
1825 byte within [start, end] that has been sent. You might want to try again from
1826 the byte following that. The error message can be 'closed' in case the
1827 connection was closed before the transmission was completed or the string
1828 'timeout' in case there was a timeout during the operation.
1829
1830 Note: Output is not buffered. For small strings, it is always better to
1831 concatenate them in Lua (with the '..' operator) and send the result in one
1832 call instead of calling the method several times.
1833
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001834.. js:function:: Socket.setoption(socket, option [, value])
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001835
1836 Just implemented for compatibility, this cal does nothing.
1837
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001838.. js:function:: Socket.settimeout(socket, value [, mode])
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001839
1840 Changes the timeout values for the object. All I/O operations are blocking.
1841 That is, any call to the methods send, receive, and accept will block
1842 indefinitely, until the operation completes. The settimeout method defines a
1843 limit on the amount of time the I/O methods can block. When a timeout time
1844 has elapsed, the affected methods give up and fail with an error code.
1845
1846 The amount of time to wait is specified as the value parameter, in seconds.
1847
1848 The timeout modes are bot implemented, the only settable timeout is the
1849 inactivity time waiting for complete the internal buffer send or waiting for
1850 receive data.
1851
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001852 :param class_socket socket: Is the manipulated Socket.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001853 :param integer value: The timeout value.
1854
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001855.. _map_class:
1856
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001857Map class
1858=========
1859
1860.. js:class:: Map
1861
1862 This class permits to do some lookup in HAProxy maps. The declared maps can
1863 be modified during the runtime throught the HAProxy management socket.
1864
1865.. code-block:: lua
1866
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001867 default = "usa"
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001868
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001869 -- Create and load map
Thierry FOURNIER4dc71972017-01-28 08:33:08 +01001870 geo = Map.new("geo.map", Map._ip);
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001871
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001872 -- Create new fetch that returns the user country
1873 core.register_fetches("country", function(txn)
1874 local src;
1875 local loc;
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001876
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001877 src = txn.f:fhdr("x-forwarded-for");
1878 if (src == nil) then
1879 src = txn.f:src()
1880 if (src == nil) then
1881 return default;
1882 end
1883 end
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001884
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001885 -- Perform lookup
1886 loc = geo:lookup(src);
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001887
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001888 if (loc == nil) then
1889 return default;
1890 end
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001891
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001892 return loc;
1893 end);
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001894
Thierry FOURNIER4dc71972017-01-28 08:33:08 +01001895.. js:attribute:: Map._int
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001896
1897 See the HAProxy configuration.txt file, chapter "Using ACLs and fetching
1898 samples" ans subchapter "ACL basics" to understand this pattern matching
1899 method.
1900
Thierry FOURNIER4dc71972017-01-28 08:33:08 +01001901 Note that :js:attr:`Map.int` is also available for compatibility.
1902
1903.. js:attribute:: Map._ip
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001904
1905 See the HAProxy configuration.txt file, chapter "Using ACLs and fetching
1906 samples" ans subchapter "ACL basics" to understand this pattern matching
1907 method.
1908
Thierry FOURNIER4dc71972017-01-28 08:33:08 +01001909 Note that :js:attr:`Map.ip` is also available for compatibility.
1910
1911.. js:attribute:: Map._str
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001912
1913 See the HAProxy configuration.txt file, chapter "Using ACLs and fetching
1914 samples" ans subchapter "ACL basics" to understand this pattern matching
1915 method.
1916
Thierry FOURNIER4dc71972017-01-28 08:33:08 +01001917 Note that :js:attr:`Map.str` is also available for compatibility.
1918
1919.. js:attribute:: Map._beg
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001920
1921 See the HAProxy configuration.txt file, chapter "Using ACLs and fetching
1922 samples" ans subchapter "ACL basics" to understand this pattern matching
1923 method.
1924
Thierry FOURNIER4dc71972017-01-28 08:33:08 +01001925 Note that :js:attr:`Map.beg` is also available for compatibility.
1926
1927.. js:attribute:: Map._sub
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001928
1929 See the HAProxy configuration.txt file, chapter "Using ACLs and fetching
1930 samples" ans subchapter "ACL basics" to understand this pattern matching
1931 method.
1932
Thierry FOURNIER4dc71972017-01-28 08:33:08 +01001933 Note that :js:attr:`Map.sub` is also available for compatibility.
1934
1935.. js:attribute:: Map._dir
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001936
1937 See the HAProxy configuration.txt file, chapter "Using ACLs and fetching
1938 samples" ans subchapter "ACL basics" to understand this pattern matching
1939 method.
1940
Thierry FOURNIER4dc71972017-01-28 08:33:08 +01001941 Note that :js:attr:`Map.dir` is also available for compatibility.
1942
1943.. js:attribute:: Map._dom
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001944
1945 See the HAProxy configuration.txt file, chapter "Using ACLs and fetching
1946 samples" ans subchapter "ACL basics" to understand this pattern matching
1947 method.
1948
Thierry FOURNIER4dc71972017-01-28 08:33:08 +01001949 Note that :js:attr:`Map.dom` is also available for compatibility.
1950
1951.. js:attribute:: Map._end
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001952
1953 See the HAProxy configuration.txt file, chapter "Using ACLs and fetching
1954 samples" ans subchapter "ACL basics" to understand this pattern matching
1955 method.
1956
Thierry FOURNIER4dc71972017-01-28 08:33:08 +01001957.. js:attribute:: Map._reg
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001958
1959 See the HAProxy configuration.txt file, chapter "Using ACLs and fetching
1960 samples" ans subchapter "ACL basics" to understand this pattern matching
1961 method.
1962
Thierry FOURNIER4dc71972017-01-28 08:33:08 +01001963 Note that :js:attr:`Map.reg` is also available for compatibility.
1964
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001965
1966.. js:function:: Map.new(file, method)
1967
1968 Creates and load a map.
1969
1970 :param string file: Is the file containing the map.
1971 :param integer method: Is the map pattern matching method. See the attributes
1972 of the Map class.
1973 :returns: a class Map object.
Thierry FOURNIER4dc71972017-01-28 08:33:08 +01001974 :see: The Map attributes: :js:attr:`Map._int`, :js:attr:`Map._ip`,
1975 :js:attr:`Map._str`, :js:attr:`Map._beg`, :js:attr:`Map._sub`,
1976 :js:attr:`Map._dir`, :js:attr:`Map._dom`, :js:attr:`Map._end` and
1977 :js:attr:`Map._reg`.
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001978
1979.. js:function:: Map.lookup(map, str)
1980
1981 Perform a lookup in a map.
1982
1983 :param class_map map: Is the class Map object.
1984 :param string str: Is the string used as key.
1985 :returns: a string containing the result or nil if no match.
1986
1987.. js:function:: Map.slookup(map, str)
1988
1989 Perform a lookup in a map.
1990
1991 :param class_map map: Is the class Map object.
1992 :param string str: Is the string used as key.
1993 :returns: a string containing the result or empty string if no match.
1994
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001995.. _applethttp_class:
1996
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02001997AppletHTTP class
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001998================
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02001999
2000.. js:class:: AppletHTTP
2001
2002 This class is used with applets that requires the 'http' mode. The http applet
2003 can be registered with the *core.register_service()* function. They are used
2004 for processing an http request like a server in back of HAProxy.
2005
2006 This is an hello world sample code:
2007
2008.. code-block:: lua
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002009
Pieter Baauw4d7f7662015-11-08 16:38:08 +01002010 core.register_service("hello-world", "http", function(applet)
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002011 local response = "Hello World !"
2012 applet:set_status(200)
Pieter Baauw2dcb9bc2015-10-01 22:47:12 +02002013 applet:add_header("content-length", string.len(response))
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002014 applet:add_header("content-type", "text/plain")
Pieter Baauw2dcb9bc2015-10-01 22:47:12 +02002015 applet:start_response()
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002016 applet:send(response)
2017 end)
2018
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002019.. js:attribute:: AppletHTTP.c
2020
2021 :returns: A :ref:`converters_class`
2022
2023 This attribute contains a Converters class object.
2024
2025.. js:attribute:: AppletHTTP.sc
2026
2027 :returns: A :ref:`converters_class`
2028
2029 This attribute contains a Converters class object. The
2030 functions of this object returns always a string.
2031
2032.. js:attribute:: AppletHTTP.f
2033
2034 :returns: A :ref:`fetches_class`
2035
2036 This attribute contains a Fetches class object. Note that the
2037 applet execution place cannot access to a valid HAProxy core HTTP
2038 transaction, so some sample fecthes related to the HTTP dependant
2039 values (hdr, path, ...) are not available.
2040
2041.. js:attribute:: AppletHTTP.sf
2042
2043 :returns: A :ref:`fetches_class`
2044
2045 This attribute contains a Fetches class object. The functions of
2046 this object returns always a string. Note that the applet
2047 execution place cannot access to a valid HAProxy core HTTP
2048 transaction, so some sample fecthes related to the HTTP dependant
2049 values (hdr, path, ...) are not available.
2050
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01002051.. js:attribute:: AppletHTTP.method
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002052
2053 :returns: string
2054
2055 The attribute method returns a string containing the HTTP
2056 method.
2057
2058.. js:attribute:: AppletHTTP.version
2059
2060 :returns: string
2061
2062 The attribute version, returns a string containing the HTTP
2063 request version.
2064
2065.. js:attribute:: AppletHTTP.path
2066
2067 :returns: string
2068
2069 The attribute path returns a string containing the HTTP
2070 request path.
2071
2072.. js:attribute:: AppletHTTP.qs
2073
2074 :returns: string
2075
2076 The attribute qs returns a string containing the HTTP
2077 request query string.
2078
2079.. js:attribute:: AppletHTTP.length
2080
2081 :returns: integer
2082
2083 The attribute length returns an integer containing the HTTP
2084 body length.
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002085
Thierry FOURNIER841475e2015-12-11 17:10:09 +01002086.. js:attribute:: AppletHTTP.headers
2087
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002088 :returns: array
2089
2090 The attribute headers returns an array containing the HTTP
2091 headers. The header names are always in lower case. As the header name can be
2092 encountered more than once in each request, the value is indexed with 0 as
2093 first index value. The array have this form:
2094
2095.. code-block:: lua
2096
2097 AppletHTTP.headers['<header-name>'][<header-index>] = "<header-value>"
2098
2099 AppletHTTP.headers["host"][0] = "www.test.com"
2100 AppletHTTP.headers["accept"][0] = "audio/basic q=1"
2101 AppletHTTP.headers["accept"][1] = "audio/*, q=0.2"
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01002102 AppletHTTP.headers["accept"][2] = "*/*, q=0.1"
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002103..
2104
Robin H. Johnson52f5db22017-01-01 13:10:52 -08002105.. js:function:: AppletHTTP.set_status(applet, code [, reason])
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002106
2107 This function sets the HTTP status code for the response. The allowed code are
2108 from 100 to 599.
2109
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01002110 :param class_AppletHTTP applet: An :ref:`applethttp_class`
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002111 :param integer code: the status code returned to the client.
Robin H. Johnson52f5db22017-01-01 13:10:52 -08002112 :param string reason: the status reason returned to the client (optional).
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002113
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01002114.. js:function:: AppletHTTP.add_header(applet, name, value)
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002115
2116 This function add an header in the response. Duplicated headers are not
2117 collapsed. The special header *content-length* is used to determinate the
2118 response length. If it not exists, a *transfer-encoding: chunked* is set, and
2119 all the write from the funcion *AppletHTTP:send()* become a chunk.
2120
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01002121 :param class_AppletHTTP applet: An :ref:`applethttp_class`
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002122 :param string name: the header name
2123 :param string value: the header value
2124
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01002125.. js:function:: AppletHTTP.start_response(applet)
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002126
2127 This function indicates to the HTTP engine that it can process and send the
2128 response headers. After this called we cannot add headers to the response; We
2129 cannot use the *AppletHTTP:send()* function if the
2130 *AppletHTTP:start_response()* is not called.
2131
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01002132 :param class_AppletHTTP applet: An :ref:`applethttp_class`
2133
2134.. js:function:: AppletHTTP.getline(applet)
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002135
2136 This function returns a string containing one line from the http body. If the
2137 data returned doesn't contains a final '\\n' its assumed than its the last
2138 available data before the end of stream.
2139
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01002140 :param class_AppletHTTP applet: An :ref:`applethttp_class`
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002141 :returns: a string. The string can be empty if we reach the end of the stream.
2142
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01002143.. js:function:: AppletHTTP.receive(applet, [size])
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002144
2145 Reads data from the HTTP body, according to the specified read *size*. If the
2146 *size* is missing, the function tries to read all the content of the stream
2147 until the end. If the *size* is bigger than the http body, it returns the
2148 amount of data avalaible.
2149
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01002150 :param class_AppletHTTP applet: An :ref:`applethttp_class`
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002151 :param integer size: the required read size.
2152 :returns: always return a string,the string can be empty is the connexion is
2153 closed.
2154
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01002155.. js:function:: AppletHTTP.send(applet, msg)
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002156
2157 Send the message *msg* on the http request body.
2158
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01002159 :param class_AppletHTTP applet: An :ref:`applethttp_class`
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002160 :param string msg: the message to send.
2161
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01002162.. js:function:: AppletHTTP.get_priv(applet)
2163
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01002164 Return Lua data stored in the current transaction. If no data are stored,
2165 it returns a nil value.
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01002166
2167 :param class_AppletHTTP applet: An :ref:`applethttp_class`
2168 :returns: the opaque data previsously stored, or nil if nothing is
2169 avalaible.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01002170 :see: :js:func:`AppletHTTP.set_priv`
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01002171
2172.. js:function:: AppletHTTP.set_priv(applet, data)
2173
2174 Store any data in the current HAProxy transaction. This action replace the
2175 old stored data.
2176
2177 :param class_AppletHTTP applet: An :ref:`applethttp_class`
2178 :param opaque data: The data which is stored in the transaction.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01002179 :see: :js:func:`AppletHTTP.get_priv`
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01002180
Thierry FOURNIER / OZON.IOc1edafe2016-12-12 16:25:30 +01002181.. js:function:: AppletHTTP.set_var(applet, var, value)
2182
2183 Converts a Lua type in a HAProxy type and store it in a variable <var>.
2184
2185 :param class_AppletHTTP applet: An :ref:`applethttp_class`
2186 :param string var: The variable name according with the HAProxy variable syntax.
2187 :param type value: The value associated to the variable. The type ca be string or
2188 integer.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01002189 :see: :js:func:`AppletHTTP.unset_var`
2190 :see: :js:func:`AppletHTTP.get_var`
Thierry FOURNIER / OZON.IOc1edafe2016-12-12 16:25:30 +01002191
2192.. js:function:: AppletHTTP.unset_var(applet, var)
2193
2194 Unset the variable <var>.
2195
2196 :param class_AppletHTTP applet: An :ref:`applethttp_class`
2197 :param string var: The variable name according with the HAProxy variable syntax.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01002198 :see: :js:func:`AppletHTTP.set_var`
2199 :see: :js:func:`AppletHTTP.get_var`
Thierry FOURNIER / OZON.IOc1edafe2016-12-12 16:25:30 +01002200
2201.. js:function:: AppletHTTP.get_var(applet, var)
2202
2203 Returns data stored in the variable <var> converter in Lua type.
2204
2205 :param class_AppletHTTP applet: An :ref:`applethttp_class`
2206 :param string var: The variable name according with the HAProxy variable syntax.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01002207 :see: :js:func:`AppletHTTP.set_var`
2208 :see: :js:func:`AppletHTTP.unset_var`
Thierry FOURNIER / OZON.IOc1edafe2016-12-12 16:25:30 +01002209
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002210.. _applettcp_class:
2211
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002212AppletTCP class
2213===============
2214
2215.. js:class:: AppletTCP
2216
2217 This class is used with applets that requires the 'tcp' mode. The tcp applet
2218 can be registered with the *core.register_service()* function. They are used
2219 for processing a tcp stream like a server in back of HAProxy.
2220
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002221.. js:attribute:: AppletTCP.c
2222
2223 :returns: A :ref:`converters_class`
2224
2225 This attribute contains a Converters class object.
2226
2227.. js:attribute:: AppletTCP.sc
2228
2229 :returns: A :ref:`converters_class`
2230
2231 This attribute contains a Converters class object. The
2232 functions of this object returns always a string.
2233
2234.. js:attribute:: AppletTCP.f
2235
2236 :returns: A :ref:`fetches_class`
2237
2238 This attribute contains a Fetches class object.
2239
2240.. js:attribute:: AppletTCP.sf
2241
2242 :returns: A :ref:`fetches_class`
2243
2244 This attribute contains a Fetches class object.
2245
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01002246.. js:function:: AppletTCP.getline(applet)
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002247
2248 This function returns a string containing one line from the stream. If the
2249 data returned doesn't contains a final '\\n' its assumed than its the last
2250 available data before the end of stream.
2251
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01002252 :param class_AppletTCP applet: An :ref:`applettcp_class`
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002253 :returns: a string. The string can be empty if we reach the end of the stream.
2254
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01002255.. js:function:: AppletTCP.receive(applet, [size])
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002256
2257 Reads data from the TCP stream, according to the specified read *size*. If the
2258 *size* is missing, the function tries to read all the content of the stream
2259 until the end.
2260
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01002261 :param class_AppletTCP applet: An :ref:`applettcp_class`
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002262 :param integer size: the required read size.
2263 :returns: always return a string,the string can be empty is the connexion is
2264 closed.
2265
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01002266.. js:function:: AppletTCP.send(appletmsg)
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002267
2268 Send the message on the stream.
2269
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01002270 :param class_AppletTCP applet: An :ref:`applettcp_class`
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002271 :param string msg: the message to send.
2272
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01002273.. js:function:: AppletTCP.get_priv(applet)
2274
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01002275 Return Lua data stored in the current transaction. If no data are stored,
2276 it returns a nil value.
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01002277
2278 :param class_AppletTCP applet: An :ref:`applettcp_class`
2279 :returns: the opaque data previsously stored, or nil if nothing is
2280 avalaible.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01002281 :see: :js:func:`AppletTCP.set_priv`
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01002282
2283.. js:function:: AppletTCP.set_priv(applet, data)
2284
2285 Store any data in the current HAProxy transaction. This action replace the
2286 old stored data.
2287
2288 :param class_AppletTCP applet: An :ref:`applettcp_class`
2289 :param opaque data: The data which is stored in the transaction.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01002290 :see: :js:func:`AppletTCP.get_priv`
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01002291
Thierry FOURNIER / OZON.IOc1edafe2016-12-12 16:25:30 +01002292.. js:function:: AppletTCP.set_var(applet, var, value)
2293
2294 Converts a Lua type in a HAProxy type and stores it in a variable <var>.
2295
2296 :param class_AppletTCP applet: An :ref:`applettcp_class`
2297 :param string var: The variable name according with the HAProxy variable syntax.
2298 :param type value: The value associated to the variable. The type can be string or
2299 integer.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01002300 :see: :js:func:`AppletTCP.unset_var`
2301 :see: :js:func:`AppletTCP.get_var`
Thierry FOURNIER / OZON.IOc1edafe2016-12-12 16:25:30 +01002302
2303.. js:function:: AppletTCP.unset_var(applet, var)
2304
2305 Unsets the variable <var>.
2306
2307 :param class_AppletTCP applet: An :ref:`applettcp_class`
2308 :param string var: The variable name according with the HAProxy variable syntax.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01002309 :see: :js:func:`AppletTCP.unset_var`
2310 :see: :js:func:`AppletTCP.set_var`
Thierry FOURNIER / OZON.IOc1edafe2016-12-12 16:25:30 +01002311
2312.. js:function:: AppletTCP.get_var(applet, var)
2313
2314 Returns data stored in the variable <var> converter in Lua type.
2315
2316 :param class_AppletTCP applet: An :ref:`applettcp_class`
2317 :param string var: The variable name according with the HAProxy variable syntax.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01002318 :see: :js:func:`AppletTCP.unset_var`
2319 :see: :js:func:`AppletTCP.set_var`
Thierry FOURNIER / OZON.IOc1edafe2016-12-12 16:25:30 +01002320
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002321External Lua libraries
2322======================
2323
2324A lot of useful lua libraries can be found here:
2325
2326* `https://lua-toolbox.com/ <https://lua-toolbox.com/>`_
2327
2328Redis acces:
2329
2330* `https://github.com/nrk/redis-lua <https://github.com/nrk/redis-lua>`_
2331
2332This is an example about the usage of the Redis library with HAProxy. Note that
2333each call of any function of this library can throw an error if the socket
2334connection fails.
2335
2336.. code-block:: lua
2337
2338 -- load the redis library
2339 local redis = require("redis");
2340
2341 function do_something(txn)
2342
2343 -- create and connect new tcp socket
2344 local tcp = core.tcp();
2345 tcp:settimeout(1);
2346 tcp:connect("127.0.0.1", 6379);
2347
2348 -- use the redis library with this new socket
2349 local client = redis.connect({socket=tcp});
2350 client:ping();
2351
2352 end
2353
2354OpenSSL:
2355
2356* `http://mkottman.github.io/luacrypto/index.html
2357 <http://mkottman.github.io/luacrypto/index.html>`_
2358
2359* `https://github.com/brunoos/luasec/wiki
2360 <https://github.com/brunoos/luasec/wiki>`_
Thierry FOURNIER2e4893c2015-03-18 13:37:27 +01002361