blob: d5a66d2f424369f032229473b3659d4bca266f02 [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
168.. js:function:: core.log(loglevel, msg)
169
170 **context**: body, init, task, action, sample-fetch, converter
171
David Carlier61fdf8b2015-10-02 11:59:38 +0100172 This function sends a log. The log is sent, according with the HAProxy
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100173 configuration file, on the default syslog server if it is configured and on
174 the stderr if it is allowed.
175
176 :param integer loglevel: Is the log level asociated with the message. It is a
177 number between 0 and 7.
178 :param string msg: The log content.
179 :see: core.emerg, core.alert, core.crit, core.err, core.warning, core.notice,
180 core.info, core.debug (log level definitions)
181 :see: code.Debug
182 :see: core.Info
183 :see: core.Warning
184 :see: core.Alert
185
186.. js:function:: core.Debug(msg)
187
188 **context**: body, init, task, action, sample-fetch, converter
189
190 :param string msg: The log content.
191 :see: log
192
193 Does the same job than:
194
195.. code-block:: lua
196
Thierry FOURNIERdc595002015-12-21 11:13:52 +0100197 function Debug(msg)
198 core.log(core.debug, msg)
199 end
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100200..
201
202.. js:function:: core.Info(msg)
203
204 **context**: body, init, task, action, sample-fetch, converter
205
206 :param string msg: The log content.
207 :see: log
208
209.. code-block:: lua
210
Thierry FOURNIERdc595002015-12-21 11:13:52 +0100211 function Info(msg)
212 core.log(core.info, msg)
213 end
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100214..
215
216.. js:function:: core.Warning(msg)
217
218 **context**: body, init, task, action, sample-fetch, converter
219
220 :param string msg: The log content.
221 :see: log
222
223.. code-block:: lua
224
Thierry FOURNIERdc595002015-12-21 11:13:52 +0100225 function Warning(msg)
226 core.log(core.warning, msg)
227 end
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100228..
229
230.. js:function:: core.Alert(msg)
231
232 **context**: body, init, task, action, sample-fetch, converter
233
234 :param string msg: The log content.
235 :see: log
236
237.. code-block:: lua
238
Thierry FOURNIERdc595002015-12-21 11:13:52 +0100239 function Alert(msg)
240 core.log(core.alert, msg)
241 end
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100242..
243
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100244.. js:function:: core.add_acl(filename, key)
245
246 **context**: init, task, action, sample-fetch, converter
247
248 Add the ACL *key* in the ACLs list referenced by the file *filename*.
249
250 :param string filename: the filename that reference the ACL entries.
251 :param string key: the key which will be added.
252
253.. js:function:: core.del_acl(filename, key)
254
255 **context**: init, task, action, sample-fetch, converter
256
257 Delete the ACL entry referenced by the key *key* in the list of ACLs
258 referenced by *filename*.
259
260 :param string filename: the filename that reference the ACL entries.
261 :param string key: the key which will be deleted.
262
263.. js:function:: core.del_map(filename, key)
264
265 **context**: init, task, action, sample-fetch, converter
266
267 Delete the map entry indexed with the specified key in the list of maps
268 referenced by his filename.
269
270 :param string filename: the filename that reference the map entries.
271 :param string key: the key which will be deleted.
272
Thierry Fourniereea77c02016-03-18 08:47:13 +0100273.. js:function:: core.get_info()
274
275 **context**: body, init, task, action, sample-fetch, converter
276
277 Returns HAProxy core informations. We can found information like the uptime,
278 the pid, memory pool usage, tasks number, ...
279
280 These information are also returned by the management sockat via the command
281 "show info". See the management socket documentation fpor more information
282 about the content of these variables.
283
284 :returns: an array of values.
285
Thierry Fournierb1f46562016-01-21 09:46:15 +0100286.. js:function:: core.now()
287
288 **context**: body, init, task, action
289
290 This function returns the current time. The time returned is fixed by the
291 HAProxy core and assures than the hour will be monotnic and that the system
292 call 'gettimeofday' will not be called too. The time is refreshed between each
293 Lua execution or resume, so two consecutive call to the function "now" will
294 probably returns the same result.
295
296 :returns: an array which contains two entries "sec" and "usec". "sec"
297 contains the current at the epoch format, and "usec" contains the
298 current microseconds.
299
Thierry FOURNIERa78f0372016-12-14 19:04:41 +0100300.. js:function:: core.http_date(date)
301
302 **context**: body, init, task, action
303
304 This function take a string repsenting http date, and returns an integer
305 containing the corresponding date with a epoch format. A valid http date
306 me respect the format IMF, RFC850 or ASCTIME.
307
308 :param string date: a date http-date formatted
309 :returns: integer containing epoch date
310 :see: :js:func:`core.imf_date`.
311 :see: :js:func:`core.rfc850_date`.
312 :see: :js:func:`core.asctime_date`.
313 :see: https://tools.ietf.org/html/rfc7231#section-7.1.1.1
314
315.. js:function:: core.imf_date(date)
316
317 **context**: body, init, task, action
318
319 This function take a string repsenting IMF date, and returns an integer
320 containing the corresponding date with a epoch format.
321
322 :param string date: a date IMF formatted
323 :returns: integer containing epoch date
324 :see: https://tools.ietf.org/html/rfc7231#section-7.1.1.1
325
326 The IMF format is like this:
327
328.. code-block:: text
329
330 Sun, 06 Nov 1994 08:49:37 GMT
331..
332
333.. js:function:: core.rfc850_date(date)
334
335 **context**: body, init, task, action
336
337 This function take a string repsenting RFC850 date, and returns an integer
338 containing the corresponding date with a epoch format.
339
340 :param string date: a date RFC859 formatted
341 :returns: integer containing epoch date
342 :see: https://tools.ietf.org/html/rfc7231#section-7.1.1.1
343
344 The RFC850 format is like this:
345
346.. code-block:: text
347
348 Sunday, 06-Nov-94 08:49:37 GMT
349..
350
351.. js:function:: core.asctime_date(date)
352
353 **context**: body, init, task, action
354
355 This function take a string repsenting ASCTIME date, and returns an integer
356 containing the corresponding date with a epoch format.
357
358 :param string date: a date ASCTIME formatted
359 :returns: integer containing epoch date
360 :see: https://tools.ietf.org/html/rfc7231#section-7.1.1.1
361
362 The ASCTIME format is like this:
363
364.. code-block:: text
365
366 Sun Nov 6 08:49:37 1994
367..
368
369.. js:function:: core.rfc850_date(date)
370
371 **context**: body, init, task, action
372
373 This function take a string repsenting http date, and returns an integer
374 containing the corresponding date with a epoch format.
375
376 :param string date: a date http-date formatted
377
378.. js:function:: core.asctime_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
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100387.. js:function:: core.msleep(milliseconds)
388
389 **context**: body, init, task, action
390
391 The `core.msleep()` stops the Lua execution between specified milliseconds.
392
393 :param integer milliseconds: the required milliseconds.
394
Thierry Fournierf61aa632016-02-19 20:56:00 +0100395.. js:attribute:: core.proxies
396
397 **context**: body, init, task, action, sample-fetch, converter
398
399 proxies is an array containing the list of all proxies declared in the
400 configuration file. Each entry of the proxies array is an object of type
401 :ref:`proxy_class`
402
Thierry FOURNIER8255a752015-09-23 21:03:35 +0200403.. js:function:: core.register_action(name, actions, func)
404
405 **context**: body
406
David Carlier61fdf8b2015-10-02 11:59:38 +0100407 Register a Lua function executed as action. All the registered action can be
Thierry FOURNIER8255a752015-09-23 21:03:35 +0200408 used in HAProxy with the prefix "lua.". An action gets a TXN object class as
409 input.
410
411 :param string name: is the name of the converter.
Willy Tarreau61add3c2015-09-28 15:39:10 +0200412 :param table actions: is a table of string describing the HAProxy actions who
Thierry FOURNIER8255a752015-09-23 21:03:35 +0200413 want to register to. The expected actions are 'tcp-req',
414 'tcp-res', 'http-req' or 'http-res'.
415 :param function func: is the Lua function called to work as converter.
416
417 The prototype of the Lua function used as argument is:
418
419.. code-block:: lua
420
421 function(txn)
422..
423
Thierry FOURNIERdc595002015-12-21 11:13:52 +0100424 * **txn** (:ref:`txn_class`): this is a TXN object used for manipulating the
Thierry FOURNIER8255a752015-09-23 21:03:35 +0200425 current request or TCP stream.
426
Willy Tarreau61add3c2015-09-28 15:39:10 +0200427 Here, an exemple of action registration. the action juste send an 'Hello world'
Thierry FOURNIER8255a752015-09-23 21:03:35 +0200428 in the logs.
429
430.. code-block:: lua
431
432 core.register_action("hello-world", { "tcp-req", "http-req" }, function(txn)
433 txn:Info("Hello world")
434 end)
435..
436
437 This example code is used in HAproxy configuration like this:
438
439::
440
441 frontend tcp_frt
442 mode tcp
443 tcp-request content lua.hello-world
444
445 frontend http_frt
446 mode http
447 http-request lua.hello-world
448
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100449.. js:function:: core.register_converters(name, func)
450
451 **context**: body
452
David Carlier61fdf8b2015-10-02 11:59:38 +0100453 Register a Lua function executed as converter. All the registered converters
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100454 can be used in HAProxy with the prefix "lua.". An converter get a string as
455 input and return a string as output. The registered function can take up to 9
456 values as parameter. All the value are strings.
457
458 :param string name: is the name of the converter.
459 :param function func: is the Lua function called to work as converter.
460
461 The prototype of the Lua function used as argument is:
462
463.. code-block:: lua
464
465 function(str, [p1 [, p2 [, ... [, p5]]]])
466..
467
468 * **str** (*string*): this is the input value automatically converted in
469 string.
470 * **p1** .. **p5** (*string*): this is a list of string arguments declared in
471 the haroxy configuration file. The number of arguments doesn't exceed 5.
472 The order and the nature of these is conventionally choose by the
473 developper.
474
475.. js:function:: core.register_fetches(name, func)
476
477 **context**: body
478
David Carlier61fdf8b2015-10-02 11:59:38 +0100479 Register a Lua function executed as sample fetch. All the registered sample
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100480 fetchs can be used in HAProxy with the prefix "lua.". A Lua sample fetch
481 return a string as output. The registered function can take up to 9 values as
482 parameter. All the value are strings.
483
484 :param string name: is the name of the converter.
485 :param function func: is the Lua function called to work as sample fetch.
486
487 The prototype of the Lua function used as argument is:
488
489.. code-block:: lua
490
491 string function(txn, [p1 [, p2 [, ... [, p5]]]])
492..
493
Thierry FOURNIERdc595002015-12-21 11:13:52 +0100494 * **txn** (:ref:`txn_class`): this is the txn object associated with the current
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100495 request.
496 * **p1** .. **p5** (*string*): this is a list of string arguments declared in
497 the haroxy configuration file. The number of arguments doesn't exceed 5.
498 The order and the nature of these is conventionally choose by the
499 developper.
500 * **Returns**: A string containing some data, ot nil if the value cannot be
501 returned now.
502
503 lua example code:
504
505.. code-block:: lua
506
507 core.register_fetches("hello", function(txn)
508 return "hello"
509 end)
510..
511
512 HAProxy example configuration:
513
514::
515
516 frontend example
517 http-request redirect location /%[lua.hello]
518
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +0200519.. js:function:: core.register_service(name, mode, func)
520
521 **context**: body
522
David Carlier61fdf8b2015-10-02 11:59:38 +0100523 Register a Lua function executed as a service. All the registered service can
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +0200524 be used in HAProxy with the prefix "lua.". A service gets an object class as
525 input according with the required mode.
526
527 :param string name: is the name of the converter.
Willy Tarreau61add3c2015-09-28 15:39:10 +0200528 :param string mode: is string describing the required mode. Only 'tcp' or
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +0200529 'http' are allowed.
530 :param function func: is the Lua function called to work as converter.
531
532 The prototype of the Lua function used as argument is:
533
534.. code-block:: lua
535
536 function(applet)
537..
538
Thierry FOURNIERdc595002015-12-21 11:13:52 +0100539 * **applet** *applet* will be a :ref:`applettcp_class` or a
540 :ref:`applethttp_class`. It depends the type of registered applet. An applet
541 registered with the 'http' value for the *mode* parameter will gets a
542 :ref:`applethttp_class`. If the *mode* value is 'tcp', the applet will gets
543 a :ref:`applettcp_class`.
544
545 **warning**: Applets of type 'http' cannot be called from 'tcp-*'
546 rulesets. Only the 'http-*' rulesets are authorized, this means
547 that is not possible to call an HTTP applet from a proxy in tcp
548 mode. Applets of type 'tcp' can be called from anywhre.
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +0200549
Willy Tarreau61add3c2015-09-28 15:39:10 +0200550 Here, an exemple of service registration. the service just send an 'Hello world'
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +0200551 as an http response.
552
553.. code-block:: lua
554
Pieter Baauw4d7f7662015-11-08 16:38:08 +0100555 core.register_service("hello-world", "http", function(applet)
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +0200556 local response = "Hello World !"
557 applet:set_status(200)
Pieter Baauw2dcb9bc2015-10-01 22:47:12 +0200558 applet:add_header("content-length", string.len(response))
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +0200559 applet:add_header("content-type", "text/plain")
Pieter Baauw2dcb9bc2015-10-01 22:47:12 +0200560 applet:start_response()
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +0200561 applet:send(response)
562 end)
563..
564
565 This example code is used in HAproxy configuration like this:
566
567::
568
569 frontend example
570 http-request use-service lua.hello-world
571
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100572.. js:function:: core.register_init(func)
573
574 **context**: body
575
576 Register a function executed after the configuration parsing. This is useful
577 to check any parameters.
578
Pieter Baauw4d7f7662015-11-08 16:38:08 +0100579 :param function func: is the Lua function called to work as initializer.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100580
581 The prototype of the Lua function used as argument is:
582
583.. code-block:: lua
584
585 function()
586..
587
588 It takes no input, and no output is expected.
589
590.. js:function:: core.register_task(func)
591
592 **context**: body, init, task, action, sample-fetch, converter
593
594 Register and start independent task. The task is started when the HAProxy
595 main scheduler starts. For example this type of tasks can be executed to
Thierry FOURNIER2e4893c2015-03-18 13:37:27 +0100596 perform complex health checks.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100597
Pieter Baauw4d7f7662015-11-08 16:38:08 +0100598 :param function func: is the Lua function called to work as initializer.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100599
600 The prototype of the Lua function used as argument is:
601
602.. code-block:: lua
603
604 function()
605..
606
607 It takes no input, and no output is expected.
608
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +0100609.. js:function:: core.register_cli([path], usage, func)
610
611 **context**: body
612
613 Register and start independent task. The task is started when the HAProxy
614 main scheduler starts. For example this type of tasks can be executed to
615 perform complex health checks.
616
617 :param array path: is the sequence of word for which the cli execute the Lua
618 binding.
619 :param string usage: is the usage message displayed in the help.
620 :param function func: is the Lua function called to handle the CLI commands.
621
622 The prototype of the Lua function used as argument is:
623
624.. code-block:: lua
625
626 function(AppletTCP, [arg1, [arg2, [...]]])
627..
628
629 I/O are managed with the :ref:`applettcp_class` object. Args are given as
630 paramter. The args embbed the registred path. If the path is declared like
631 this:
632
633.. code-block:: lua
634
635 core.register_cli({"show", "ssl", "stats"}, "Display SSL stats..", function(applet, arg1, arg2, arg3, arg4, arg5)
636 end)
637..
638
639 And we execute this in the prompt:
640
641.. code-block:: text
642
643 > prompt
644 > show ssl stats all
645..
646
647 Then, arg1, arg2 and arg3 will contains respectivey "show", "ssl" and "stats".
648 arg4 will contain "all". arg5 contains nil.
649
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100650.. js:function:: core.set_nice(nice)
651
652 **context**: task, action, sample-fetch, converter
653
654 Change the nice of the current task or current session.
Thierry FOURNIER2e4893c2015-03-18 13:37:27 +0100655
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100656 :param integer nice: the nice value, it must be between -1024 and 1024.
657
658.. js:function:: core.set_map(filename, key, value)
659
660 **context**: init, task, action, sample-fetch, converter
661
662 set the value *value* associated to the key *key* in the map referenced by
663 *filename*.
664
Thierry FOURNIERdc595002015-12-21 11:13:52 +0100665 :param string filename: the Map reference
666 :param string key: the key to set or replace
667 :param string value: the associated value
668
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100669.. js:function:: core.sleep(int seconds)
670
671 **context**: body, init, task, action
672
673 The `core.sleep()` functions stop the Lua execution between specified seconds.
674
675 :param integer seconds: the required seconds.
676
677.. js:function:: core.tcp()
678
679 **context**: init, task, action
680
681 This function returns a new object of a *socket* class.
682
Thierry FOURNIERdc595002015-12-21 11:13:52 +0100683 :returns: A :ref:`socket_class` object.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100684
Thierry Fournier1de16592016-01-27 09:49:07 +0100685.. js:function:: core.concat()
686
687 **context**: body, init, task, action, sample-fetch, converter
688
689 This function retruns a new concat object.
690
691 :returns: A :ref:`concat_class` object.
692
Thierry FOURNIER0a99b892015-08-26 00:14:17 +0200693.. js:function:: core.done(data)
694
695 **context**: body, init, task, action, sample-fetch, converter
696
697 :param any data: Return some data for the caller. It is useful with
698 sample-fetches and sample-converters.
699
700 Immediately stops the current Lua execution and returns to the caller which
701 may be a sample fetch, a converter or an action and returns the specified
702 value (ignored for actions). It is used when the LUA process finishes its
703 work and wants to give back the control to HAProxy without executing the
704 remaining code. It can be seen as a multi-level "return".
705
Thierry FOURNIER486f5a02015-03-16 15:13:03 +0100706.. js:function:: core.yield()
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100707
708 **context**: task, action, sample-fetch, converter
709
710 Give back the hand at the HAProxy scheduler. It is used when the LUA
711 processing consumes a lot of processing time.
712
Thierry FOURNIER / OZON.IO62fec752016-11-10 20:38:11 +0100713.. js:function:: core.parse_addr(address)
714
715 **context**: body, init, task, action, sample-fetch, converter
716
717 :param network: is a string describing an ipv4 or ipv6 address and optionally
718 its network length, like this: "127.0.0.1/8" or "aaaa::1234/32".
719 :returns: a userdata containing network or nil if an error occurs.
720
721 Parse ipv4 or ipv6 adresses and its facultative associated network.
722
723.. js:function:: core.match_addr(addr1, addr2)
724
725 **context**: body, init, task, action, sample-fetch, converter
726
727 :param addr1: is an address created with "core.parse_addr".
728 :param addr2: is an address created with "core.parse_addr".
729 :returns: boolean, true if the network of the addresses matche, else returns
730 false.
731
732 Match two networks. For example "127.0.0.1/32" matchs "127.0.0.0/8". The order
733 of network is not important.
734
Thierry FOURNIER / OZON.IO8a1027a2016-11-24 20:48:38 +0100735.. js:function:: core.tokenize(str, separators [, noblank])
736
737 **context**: body, init, task, action, sample-fetch, converter
738
739 This function is useful for tokenizing an entry, or splitting some messages.
740 :param string str: The string which will be split.
741 :param string separators: A string containing a list of separators.
742 :param boolean noblank: Ignore empty entries.
743 :returns: an array of string.
744
745 For example:
746
747.. code-block:: lua
748
749 local array = core.tokenize("This function is useful, for tokenizing an entry.", "., ", true)
750 print_r(array)
751..
752
753 Returns this array:
754
755.. code-block:: text
756
757 (table) table: 0x21c01e0 [
758 1: (string) "This"
759 2: (string) "function"
760 3: (string) "is"
761 4: (string) "useful"
762 5: (string) "for"
763 6: (string) "tokenizing"
764 7: (string) "an"
765 8: (string) "entry"
766 ]
767..
768
Thierry Fournierf61aa632016-02-19 20:56:00 +0100769.. _proxy_class:
770
771Proxy class
772============
773
774.. js:class:: Proxy
775
776 This class provides a way for manipulating proxy and retrieving information
777 like statistics.
778
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +0100779.. js:attribute:: Proxy.servers
780
781 Contain an array with the attached servers. Each server entry is an object of
782 type :ref:`server_class`.
783
Thierry Fournierff480422016-02-25 08:36:46 +0100784.. js:attribute:: Proxy.listeners
785
786 Contain an array with the attached listeners. Each listeners entry is an
787 object of type :ref:`listener_class`.
788
Thierry Fournierf61aa632016-02-19 20:56:00 +0100789.. js:function:: Proxy.pause(px)
790
791 Pause the proxy. See the management socket documentation for more information.
792
793 :param class_proxy px: A :ref:`proxy_class` which indicates the manipulated
794 proxy.
795
796.. js:function:: Proxy.resume(px)
797
798 Resume the proxy. See the management socket documentation for more
799 information.
800
801 :param class_proxy px: A :ref:`proxy_class` which indicates the manipulated
802 proxy.
803
804.. js:function:: Proxy.stop(px)
805
806 Stop the proxy. See the management socket documentation for more information.
807
808 :param class_proxy px: A :ref:`proxy_class` which indicates the manipulated
809 proxy.
810
811.. js:function:: Proxy.shut_bcksess(px)
812
813 Kill the session attached to a backup server. See the management socket
814 documentation for more information.
815
816 :param class_proxy px: A :ref:`proxy_class` which indicates the manipulated
817 proxy.
818
819.. js:function:: Proxy.get_cap(px)
820
821 Returns a string describing the capabilities of the proxy.
822
823 :param class_proxy px: A :ref:`proxy_class` which indicates the manipulated
824 proxy.
825 :returns: a string "frontend", "backend", "proxy" or "ruleset".
826
827.. js:function:: Proxy.get_mode(px)
828
829 Returns a string describing the mode of the current proxy.
830
831 :param class_proxy px: A :ref:`proxy_class` which indicates the manipulated
832 proxy.
833 :returns: a string "tcp", "http", "health" or "unknown"
834
835.. js:function:: Proxy.get_stats(px)
836
837 Returns an array containg the proxy statistics. The statistics returned are
838 not the same if the proxy is frontend or a backend.
839
840 :param class_proxy px: A :ref:`proxy_class` which indicates the manipulated
841 proxy.
842 :returns: a key/value array containing stats
843
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +0100844.. _server_class:
845
846Server class
847============
848
849.. js:function:: Server.is_draining(sv)
850
851 Return true if the server is currently draining stiky connections.
852
853 :param class_server sv: A :ref:`server_class` which indicates the manipulated
854 server.
855 :returns: a boolean
856
857.. js:function:: Server.set_weight(sv, weight)
858
859 Dynamically change the weight of the serveur. See the management socket
860 documentation for more information about the format of the string.
861
862 :param class_server sv: A :ref:`server_class` which indicates the manipulated
863 server.
864 :param string weight: A string describing the server weight.
865
866.. js:function:: Server.get_weight(sv)
867
868 This function returns an integer representing the serveur weight.
869
870 :param class_server sv: A :ref:`server_class` which indicates the manipulated
871 server.
872 :returns: an integer.
873
874.. js:function:: Server.set_addr(sv, addr)
875
876 Dynamically change the address of the serveur. See the management socket
877 documentation for more information about the format of the string.
878
879 :param class_server sv: A :ref:`server_class` which indicates the manipulated
880 server.
881 :param string weight: A string describing the server address.
882
883.. js:function:: Server.get_addr(sv)
884
885 Returns a string describing the address of the serveur.
886
887 :param class_server sv: A :ref:`server_class` which indicates the manipulated
888 server.
889 :returns: A string
890
891.. js:function:: Server.get_stats(sv)
892
893 Returns server statistics.
894
895 :param class_server sv: A :ref:`server_class` which indicates the manipulated
896 server.
897 :returns: a key/value array containing stats
898
899.. js:function:: Server.shut_sess(sv)
900
901 Shutdown all the sessions attached to the server. See the management socket
902 documentation for more information about this function.
903
904 :param class_server sv: A :ref:`server_class` which indicates the manipulated
905 server.
906
907.. js:function:: Server.set_drain(sv)
908
909 Drain sticky sessions. See the management socket documentation for more
910 information about this function.
911
912 :param class_server sv: A :ref:`server_class` which indicates the manipulated
913 server.
914
915.. js:function:: Server.set_maint(sv)
916
917 Set maintenance mode. See the management socket documentation for more
918 information about this function.
919
920 :param class_server sv: A :ref:`server_class` which indicates the manipulated
921 server.
922
923.. js:function:: Server.set_ready(sv)
924
925 Set normal mode. See the management socket documentation for more information
926 about this function.
927
928 :param class_server sv: A :ref:`server_class` which indicates the manipulated
929 server.
930
931.. js:function:: Server.check_enable(sv)
932
933 Enable health checks. See the management socket documentation for more
934 information about this function.
935
936 :param class_server sv: A :ref:`server_class` which indicates the manipulated
937 server.
938
939.. js:function:: Server.check_disable(sv)
940
941 Disable health checks. See the management socket documentation for more
942 information about this function.
943
944 :param class_server sv: A :ref:`server_class` which indicates the manipulated
945 server.
946
947.. js:function:: Server.check_force_up(sv)
948
949 Force health-check up. See the management socket documentation for more
950 information about this function.
951
952 :param class_server sv: A :ref:`server_class` which indicates the manipulated
953 server.
954
955.. js:function:: Server.check_force_nolb(sv)
956
957 Force health-check nolb mode. See the management socket documentation for more
958 information about this function.
959
960 :param class_server sv: A :ref:`server_class` which indicates the manipulated
961 server.
962
963.. js:function:: Server.check_force_down(sv)
964
965 Force health-check down. See the management socket documentation for more
966 information about this function.
967
968 :param class_server sv: A :ref:`server_class` which indicates the manipulated
969 server.
970
971.. js:function:: Server.agent_enable(sv)
972
973 Enable agent check. See the management socket documentation for more
974 information about this function.
975
976 :param class_server sv: A :ref:`server_class` which indicates the manipulated
977 server.
978
979.. js:function:: Server.agent_disable(sv)
980
981 Disable agent check. See the management socket documentation for more
982 information about this function.
983
984 :param class_server sv: A :ref:`server_class` which indicates the manipulated
985 server.
986
987.. js:function:: Server.agent_force_up(sv)
988
989 Force agent check up. See the management socket documentation for more
990 information about this function.
991
992 :param class_server sv: A :ref:`server_class` which indicates the manipulated
993 server.
994
995.. js:function:: Server.agent_force_down(sv)
996
997 Force agent check down. See the management socket documentation for more
998 information about this function.
999
1000 :param class_server sv: A :ref:`server_class` which indicates the manipulated
1001 server.
1002
Thierry Fournierff480422016-02-25 08:36:46 +01001003.. _listener_class:
1004
1005Listener class
1006==============
1007
1008.. js:function:: Listener.get_stats(ls)
1009
1010 Returns server statistics.
1011
1012 :param class_listener ls: A :ref:`listener_class` which indicates the
1013 manipulated listener.
1014 :returns: a key/value array containing stats
1015
Thierry Fournier1de16592016-01-27 09:49:07 +01001016.. _concat_class:
1017
1018Concat class
1019============
1020
1021.. js:class:: Concat
1022
1023 This class provides a fast way for string concatenation. The way using native
1024 Lua concatenation like the code below is slow for some reasons.
1025
1026.. code-block:: lua
1027
1028 str = "string1"
1029 str = str .. ", string2"
1030 str = str .. ", string3"
1031..
1032
1033 For each concatenation, Lua:
1034 * allocate memory for the result,
1035 * catenate the two string copying the strings in the new memory bloc,
1036 * free the old memory block containing the string whoch is no longer used.
1037 This process does many memory move, allocation and free. In addition, the
1038 memory is not really freed, it is just mark mark as unsused and wait for the
1039 garbage collector.
1040
1041 The Concat class provide an alternative way for catenating strings. It uses
1042 the internal Lua mechanism (it does not allocate memory), but it doesn't copy
1043 the data more than once.
1044
1045 On my computer, the following loops spends 0.2s for the Concat method and
1046 18.5s for the pure Lua implementation. So, the Concat class is about 1000x
1047 faster than the embedded solution.
1048
1049.. code-block:: lua
1050
1051 for j = 1, 100 do
1052 c = core.concat()
1053 for i = 1, 20000 do
1054 c:add("#####")
1055 end
1056 end
1057..
1058
1059.. code-block:: lua
1060
1061 for j = 1, 100 do
1062 c = ""
1063 for i = 1, 20000 do
1064 c = c .. "#####"
1065 end
1066 end
1067..
1068
1069.. js:function:: Concat.add(concat, string)
1070
1071 This function adds a string to the current concatenated string.
1072
1073 :param class_concat concat: A :ref:`concat_class` which contains the currently
1074 builded string.
1075 :param string string: A new string to concatenate to the current builded
1076 string.
1077
1078.. js:function:: Concat.dump(concat)
1079
1080 This function returns the concanated string.
1081
1082 :param class_concat concat: A :ref:`concat_class` which contains the currently
1083 builded string.
1084 :returns: the concatenated string
1085
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001086.. _fetches_class:
1087
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001088Fetches class
1089=============
1090
1091.. js:class:: Fetches
1092
1093 This class contains a lot of internal HAProxy sample fetches. See the
Thierry FOURNIER2e4893c2015-03-18 13:37:27 +01001094 HAProxy "configuration.txt" documentation for more information about her
1095 usage. they are the chapters 7.3.2 to 7.3.6.
1096
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001097 **warning** some sample fetches are not available in some context. These
1098 limitations are specified in this documentation when theire useful.
1099
Thierry FOURNIER2e4893c2015-03-18 13:37:27 +01001100 :see: TXN.f
1101 :see: TXN.sf
1102
1103 Fetches are useful for:
1104
1105 * get system time,
1106 * get environment variable,
1107 * get random numbers,
1108 * known backend status like the number of users in queue or the number of
1109 connections established,
1110 * client information like ip source or destination,
1111 * deal with stick tables,
1112 * Established SSL informations,
1113 * HTTP information like headers or method.
1114
1115.. code-block:: lua
1116
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001117 function action(txn)
1118 -- Get source IP
1119 local clientip = txn.f:src()
1120 end
Thierry FOURNIER2e4893c2015-03-18 13:37:27 +01001121..
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001122
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001123.. _converters_class:
1124
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001125Converters class
1126================
1127
1128.. js:class:: Converters
1129
1130 This class contains a lot of internal HAProxy sample converters. See the
Thierry FOURNIER2e4893c2015-03-18 13:37:27 +01001131 HAProxy documentation "configuration.txt" for more information about her
1132 usage. Its the chapter 7.3.1.
1133
1134 :see: TXN.c
1135 :see: TXN.sc
1136
1137 Converters provides statefull transformation. They are useful for:
1138
1139 * converting input to base64,
1140 * applying hash on input string (djb2, crc32, sdbm, wt6),
1141 * format date,
1142 * json escape,
Pieter Baauw4d7f7662015-11-08 16:38:08 +01001143 * extracting preferred language comparing two lists,
Thierry FOURNIER2e4893c2015-03-18 13:37:27 +01001144 * turn to lower or upper chars,
1145 * deal with stick tables.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001146
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001147.. _channel_class:
1148
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001149Channel class
1150=============
1151
1152.. js:class:: Channel
1153
1154 HAProxy uses two buffers for the processing of the requests. The first one is
1155 used with the request data (from the client to the server) and the second is
1156 used for the response data (from the server to the client).
1157
1158 Each buffer contains two types of data. The first type is the incoming data
1159 waiting for a processing. The second part is the outgoing data already
1160 processed. Usually, the incoming data is processed, after it is tagged as
1161 outgoing data, and finally it is sent. The following functions provides tools
1162 for manipulating these data in a buffer.
1163
1164 The following diagram shows where the channel class function are applied.
1165
1166 **Warning**: It is not possible to read from the response in request action,
1167 and it is not possible to read for the request channel in response action.
1168
Thierry FOURNIER2e4893c2015-03-18 13:37:27 +01001169.. image:: _static/channel.png
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001170
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001171.. js:function:: Channel.dup(channel)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001172
1173 This function returns a string that contain the entire buffer. The data is
1174 not remove from the buffer and can be reprocessed later.
1175
1176 If the buffer cant receive more data, a 'nil' value is returned.
1177
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001178 :param class_channel channel: The manipulated Channel.
Pieter Baauw4d7f7662015-11-08 16:38:08 +01001179 :returns: a string containing all the available data or nil.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001180
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001181.. js:function:: Channel.get(channel)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001182
1183 This function returns a string that contain the entire buffer. The data is
1184 consumed from the buffer.
1185
1186 If the buffer cant receive more data, a 'nil' value is returned.
1187
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001188 :param class_channel channel: The manipulated Channel.
Pieter Baauw4d7f7662015-11-08 16:38:08 +01001189 :returns: a string containing all the available data or nil.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001190
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02001191.. js:function:: Channel.getline(channel)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001192
1193 This function returns a string that contain the first line of the buffer. The
1194 data is consumed. If the data returned doesn't contains a final '\n' its
1195 assumed than its the last available data in the buffer.
1196
1197 If the buffer cant receive more data, a 'nil' value is returned.
1198
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001199 :param class_channel channel: The manipulated Channel.
Pieter Baauw386a1272015-08-16 15:26:24 +02001200 :returns: a string containing the available line or nil.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001201
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001202.. js:function:: Channel.set(channel, string)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001203
1204 This function replace the content of the buffer by the string. The function
1205 returns the copied length, otherwise, it returns -1.
1206
1207 The data set with this function are not send. They wait for the end of
1208 HAProxy processing, so the buffer can be full.
1209
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001210 :param class_channel channel: The manipulated Channel.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001211 :param string string: The data which will sent.
Pieter Baauw4d7f7662015-11-08 16:38:08 +01001212 :returns: an integer containing the amount of bytes copied or -1.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001213
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001214.. js:function:: Channel.append(channel, string)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001215
1216 This function append the string argument to the content of the buffer. The
1217 function returns the copied length, otherwise, it returns -1.
1218
1219 The data set with this function are not send. They wait for the end of
1220 HAProxy processing, so the buffer can be full.
1221
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001222 :param class_channel channel: The manipulated Channel.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001223 :param string string: The data which will sent.
Pieter Baauw4d7f7662015-11-08 16:38:08 +01001224 :returns: an integer containing the amount of bytes copied or -1.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001225
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001226.. js:function:: Channel.send(channel, string)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001227
1228 This function required immediate send of the data. Unless if the connection
1229 is close, the buffer is regularly flushed and all the string can be sent.
1230
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001231 :param class_channel channel: The manipulated Channel.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001232 :param string string: The data which will sent.
Pieter Baauw4d7f7662015-11-08 16:38:08 +01001233 :returns: an integer containing the amount of bytes copied or -1.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001234
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001235.. js:function:: Channel.get_in_length(channel)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001236
1237 This function returns the length of the input part of the buffer.
1238
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001239 :param class_channel channel: The manipulated Channel.
Pieter Baauw4d7f7662015-11-08 16:38:08 +01001240 :returns: an integer containing the amount of available bytes.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001241
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001242.. js:function:: Channel.get_out_length(channel)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001243
1244 This function returns the length of the output part of the buffer.
1245
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001246 :param class_channel channel: The manipulated Channel.
Pieter Baauw4d7f7662015-11-08 16:38:08 +01001247 :returns: an integer containing the amount of available bytes.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001248
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001249.. js:function:: Channel.forward(channel, int)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001250
1251 This function transfer bytes from the input part of the buffer to the output
1252 part.
1253
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001254 :param class_channel channel: The manipulated Channel.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001255 :param integer int: The amount of data which will be forwarded.
1256
Thierry FOURNIER / OZON.IO65192f32016-11-07 15:28:40 +01001257.. js:function:: Channel.is_full(channel)
1258
1259 This function returns true if the buffer channel is full.
1260
1261 :returns: a boolean
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001262
1263.. _http_class:
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001264
1265HTTP class
1266==========
1267
1268.. js:class:: HTTP
1269
1270 This class contain all the HTTP manipulation functions.
1271
Pieter Baauw386a1272015-08-16 15:26:24 +02001272.. js:function:: HTTP.req_get_headers(http)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001273
1274 Returns an array containing all the request headers.
1275
1276 :param class_http http: The related http object.
1277 :returns: array of headers.
Pieter Baauw386a1272015-08-16 15:26:24 +02001278 :see: HTTP.res_get_headers()
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001279
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001280 This is the form of the returned array:
1281
1282.. code-block:: lua
1283
1284 HTTP:req_get_headers()['<header-name>'][<header-index>] = "<header-value>"
1285
1286 local hdr = HTTP:req_get_headers()
1287 hdr["host"][0] = "www.test.com"
1288 hdr["accept"][0] = "audio/basic q=1"
1289 hdr["accept"][1] = "audio/*, q=0.2"
1290 hdr["accept"][2] = "*/*, q=0.1"
1291..
1292
Pieter Baauw386a1272015-08-16 15:26:24 +02001293.. js:function:: HTTP.res_get_headers(http)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001294
1295 Returns an array containing all the response headers.
1296
1297 :param class_http http: The related http object.
1298 :returns: array of headers.
Pieter Baauw386a1272015-08-16 15:26:24 +02001299 :see: HTTP.req_get_headers()
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001300
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001301 This is the form of the returned array:
1302
1303.. code-block:: lua
1304
1305 HTTP:res_get_headers()['<header-name>'][<header-index>] = "<header-value>"
1306
1307 local hdr = HTTP:req_get_headers()
1308 hdr["host"][0] = "www.test.com"
1309 hdr["accept"][0] = "audio/basic q=1"
1310 hdr["accept"][1] = "audio/*, q=0.2"
1311 hdr["accept"][2] = "*.*, q=0.1"
1312..
1313
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001314.. js:function:: HTTP.req_add_header(http, name, value)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001315
1316 Appends an HTTP header field in the request whose name is
1317 specified in "name" and whose value is defined in "value".
1318
1319 :param class_http http: The related http object.
1320 :param string name: The header name.
1321 :param string value: The header value.
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001322 :see: HTTP.res_add_header()
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001323
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001324.. js:function:: HTTP.res_add_header(http, name, value)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001325
1326 appends an HTTP header field in the response whose name is
1327 specified in "name" and whose value is defined in "value".
1328
1329 :param class_http http: The related http object.
1330 :param string name: The header name.
1331 :param string value: The header value.
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001332 :see: HTTP.req_add_header()
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001333
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001334.. js:function:: HTTP.req_del_header(http, name)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001335
1336 Removes all HTTP header fields in the request whose name is
1337 specified in "name".
1338
1339 :param class_http http: The related http object.
1340 :param string name: The header name.
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001341 :see: HTTP.res_del_header()
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001342
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001343.. js:function:: HTTP.res_del_header(http, name)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001344
1345 Removes all HTTP header fields in the response whose name is
1346 specified in "name".
1347
1348 :param class_http http: The related http object.
1349 :param string name: The header name.
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001350 :see: HTTP.req_del_header()
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001351
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001352.. js:function:: HTTP.req_set_header(http, name, value)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001353
1354 This variable replace all occurence of all header "name", by only
1355 one containing the "value".
1356
1357 :param class_http http: The related http object.
1358 :param string name: The header name.
1359 :param string value: The header value.
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001360 :see: HTTP.res_set_header()
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001361
1362 This function does the same work as the folowwing code:
1363
1364.. code-block:: lua
1365
1366 function fcn(txn)
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001367 TXN.http:req_del_header("header")
1368 TXN.http:req_add_header("header", "value")
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001369 end
1370..
1371
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001372.. js:function:: HTTP.res_set_header(http, name, value)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001373
1374 This variable replace all occurence of all header "name", by only
1375 one containing the "value".
1376
1377 :param class_http http: The related http object.
1378 :param string name: The header name.
1379 :param string value: The header value.
Pieter Baauw386a1272015-08-16 15:26:24 +02001380 :see: HTTP.req_rep_header()
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001381
Pieter Baauw386a1272015-08-16 15:26:24 +02001382.. js:function:: HTTP.req_rep_header(http, name, regex, replace)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001383
1384 Matches the regular expression in all occurrences of header field "name"
1385 according to "regex", and replaces them with the "replace" argument. The
1386 replacement value can contain back references like \1, \2, ... This
1387 function works with the request.
1388
1389 :param class_http http: The related http object.
1390 :param string name: The header name.
1391 :param string regex: The match regular expression.
1392 :param string replace: The replacement value.
Pieter Baauw386a1272015-08-16 15:26:24 +02001393 :see: HTTP.res_rep_header()
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001394
Pieter Baauw386a1272015-08-16 15:26:24 +02001395.. js:function:: HTTP.res_rep_header(http, name, regex, string)
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 FOURNIER486f5a02015-03-16 15:13:03 +01001406 :see: HTTP.req_replace_header()
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001407
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001408.. js:function:: HTTP.req_set_method(http, method)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001409
1410 Rewrites the request method with the parameter "method".
1411
1412 :param class_http http: The related http object.
1413 :param string method: The new method.
1414
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001415.. js:function:: HTTP.req_set_path(http, path)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001416
1417 Rewrites the request path with the "path" parameter.
1418
1419 :param class_http http: The related http object.
1420 :param string path: The new path.
1421
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001422.. js:function:: HTTP.req_set_query(http, query)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001423
1424 Rewrites the request's query string which appears after the first question
1425 mark ("?") with the parameter "query".
1426
1427 :param class_http http: The related http object.
1428 :param string query: The new query.
1429
Thierry FOURNIER0d79cf62015-08-26 14:20:58 +02001430.. js:function:: HTTP.req_set_uri(http, uri)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001431
1432 Rewrites the request URI with the parameter "uri".
1433
1434 :param class_http http: The related http object.
1435 :param string uri: The new uri.
1436
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +02001437.. js:function:: HTTP.res_set_status(http, status)
1438
1439 Rewrites the response status code with the parameter "code". Note that the
1440 reason is automatically adapted to the new code.
1441
1442 :param class_http http: The related http object.
1443 :param integer status: The new response status code.
1444
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001445.. _txn_class:
1446
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001447TXN class
1448=========
1449
1450.. js:class:: TXN
1451
1452 The txn class contain all the functions relative to the http or tcp
1453 transaction (Note than a tcp stream is the same than a tcp transaction, but
1454 an HTTP transaction is not the same than a tcp stream).
1455
1456 The usage of this class permits to retrieve data from the requests, alter it
1457 and forward it.
1458
1459 All the functions provided by this class are available in the context
1460 **sample-fetches** and **actions**.
1461
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001462.. js:attribute:: TXN.c
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001463
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001464 :returns: An :ref:`converters_class`.
1465
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001466 This attribute contains a Converters class object.
1467
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001468.. js:attribute:: TXN.sc
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001469
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001470 :returns: An :ref:`converters_class`.
1471
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001472 This attribute contains a Converters class object. The functions of
1473 this object returns always a string.
1474
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001475.. js:attribute:: TXN.f
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001476
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001477 :returns: An :ref:`fetches_class`.
1478
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001479 This attribute contains a Fetches class object.
1480
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001481.. js:attribute:: TXN.sf
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001482
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001483 :returns: An :ref:`fetches_class`.
1484
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001485 This attribute contains a Fetches class object. The functions of
1486 this object returns always a string.
1487
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001488.. js:attribute:: TXN.req
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001489
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001490 :returns: An :ref:`channel_class`.
1491
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001492 This attribute contains a channel class object for the request buffer.
1493
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001494.. js:attribute:: TXN.res
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001495
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001496 :returns: An :ref:`channel_class`.
1497
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001498 This attribute contains a channel class object for the response buffer.
1499
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001500.. js:attribute:: TXN.http
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001501
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001502 :returns: An :ref:`http_class`.
1503
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001504 This attribute contains an HTTP class object. It is avalaible only if the
1505 proxy has the "mode http" enabled.
1506
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01001507.. js:function:: TXN.log(TXN, loglevel, msg)
1508
1509 This function sends a log. The log is sent, according with the HAProxy
1510 configuration file, on the default syslog server if it is configured and on
1511 the stderr if it is allowed.
1512
1513 :param class_txn txn: The class txn object containing the data.
1514 :param integer loglevel: Is the log level asociated with the message. It is a
1515 number between 0 and 7.
1516 :param string msg: The log content.
1517 :see: core.emerg, core.alert, core.crit, core.err, core.warning, core.notice,
1518 core.info, core.debug (log level definitions)
1519 :see: TXN.deflog
1520 :see: TXN.Debug
1521 :see: TXN.Info
1522 :see: TXN.Warning
1523 :see: TXN.Alert
1524
1525.. js:function:: TXN.deflog(TXN, msg)
1526
1527 Sends a log line with the default loglevel for the proxy ssociated with the
1528 transaction.
1529
1530 :param class_txn txn: The class txn object containing the data.
1531 :param string msg: The log content.
1532 :see: TXN.log
1533
1534.. js:function:: TXN.Debug(txn, msg)
1535
1536 :param class_txn txn: The class txn object containing the data.
1537 :param string msg: The log content.
1538 :see: TXN.log
1539
1540 Does the same job than:
1541
1542.. code-block:: lua
1543
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001544 function Debug(txn, msg)
1545 TXN.log(txn, core.debug, msg)
1546 end
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01001547..
1548
1549.. js:function:: TXN.Info(txn, msg)
1550
1551 :param class_txn txn: The class txn object containing the data.
1552 :param string msg: The log content.
1553 :see: TXN.log
1554
1555.. code-block:: lua
1556
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001557 function Debug(txn, msg)
1558 TXN.log(txn, core.info, msg)
1559 end
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01001560..
1561
1562.. js:function:: TXN.Warning(txn, msg)
1563
1564 :param class_txn txn: The class txn object containing the data.
1565 :param string msg: The log content.
1566 :see: TXN.log
1567
1568.. code-block:: lua
1569
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001570 function Debug(txn, msg)
1571 TXN.log(txn, core.warning, msg)
1572 end
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01001573..
1574
1575.. js:function:: TXN.Alert(txn, msg)
1576
1577 :param class_txn txn: The class txn object containing the data.
1578 :param string msg: The log content.
1579 :see: TXN.log
1580
1581.. code-block:: lua
1582
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001583 function Debug(txn, msg)
1584 TXN.log(txn, core.alert, msg)
1585 end
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01001586..
1587
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001588.. js:function:: TXN.get_priv(txn)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001589
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001590 Return Lua data stored in the current transaction (with the `TXN.set_priv()`)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001591 function. If no data are stored, it returns a nil value.
1592
1593 :param class_txn txn: The class txn object containing the data.
1594 :returns: the opaque data previsously stored, or nil if nothing is
1595 avalaible.
1596
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001597.. js:function:: TXN.set_priv(txn, data)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001598
1599 Store any data in the current HAProxy transaction. This action replace the
1600 old stored data.
1601
1602 :param class_txn txn: The class txn object containing the data.
1603 :param opaque data: The data which is stored in the transaction.
1604
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +02001605.. js:function:: TXN.set_var(TXN, var, value)
1606
David Carlier61fdf8b2015-10-02 11:59:38 +01001607 Converts a Lua type in a HAProxy type and store it in a variable <var>.
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +02001608
1609 :param class_txn txn: The class txn object containing the data.
1610 :param string var: The variable name according with the HAProxy variable syntax.
Thierry FOURNIER / OZON.IOb210bcc2016-12-12 16:24:16 +01001611 :param type value: The value associated to the variable. The type can be string or
1612 integer.
Christopher Faulet85d79c92016-11-09 16:54:56 +01001613
1614.. js:function:: TXN.unset_var(TXN, var)
1615
1616 Unset the variable <var>.
1617
1618 :param class_txn txn: The class txn object containing the data.
1619 :param string var: The variable name according with the HAProxy variable syntax.
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +02001620
1621.. js:function:: TXN.get_var(TXN, var)
1622
1623 Returns data stored in the variable <var> converter in Lua type.
1624
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.
1627
Willy Tarreaubc183a62015-08-28 10:39:11 +02001628.. js:function:: TXN.done(txn)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001629
Willy Tarreaubc183a62015-08-28 10:39:11 +02001630 This function terminates processing of the transaction and the associated
1631 session. It can be used when a critical error is detected or to terminate
1632 processing after some data have been returned to the client (eg: a redirect).
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001633
Thierry FOURNIERab00df62016-07-14 11:42:37 +02001634 *Warning*: It not make sense to call this function from sample-fetches. In
1635 this case the behaviour of this one is the same than core.done(): it quit
1636 the Lua execution. The transaction is really aborted only from an action
1637 registered function.
1638
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001639 :param class_txn txn: The class txn object containing the data.
1640
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001641.. js:function:: TXN.set_loglevel(txn, loglevel)
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01001642
1643 Is used to change the log level of the current request. The "loglevel" must
1644 be an integer between 0 and 7.
1645
1646 :param class_txn txn: The class txn object containing the data.
1647 :param integer loglevel: The required log level. This variable can be one of
1648 :see: core.<loglevel>
1649
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001650.. js:function:: TXN.set_tos(txn, tos)
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01001651
1652 Is used to set the TOS or DSCP field value of packets sent to the client to
1653 the value passed in "tos" on platforms which support this.
1654
1655 :param class_txn txn: The class txn object containing the data.
1656 :param integer tos: The new TOS os DSCP.
1657
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001658.. js:function:: TXN.set_mark(txn, mark)
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01001659
1660 Is used to set the Netfilter MARK on all packets sent to the client to the
1661 value passed in "mark" on platforms which support it.
1662
1663 :param class_txn txn: The class txn object containing the data.
1664 :param integer mark: The mark value.
1665
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001666.. _socket_class:
1667
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001668Socket class
1669============
1670
1671.. js:class:: Socket
1672
1673 This class must be compatible with the Lua Socket class. Only the 'client'
1674 functions are available. See the Lua Socket documentation:
1675
1676 `http://w3.impa.br/~diego/software/luasocket/tcp.html
1677 <http://w3.impa.br/~diego/software/luasocket/tcp.html>`_
1678
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001679.. js:function:: Socket.close(socket)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001680
1681 Closes a TCP object. The internal socket used by the object is closed and the
1682 local address to which the object was bound is made available to other
1683 applications. No further operations (except for further calls to the close
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001684 method) are allowed on a closed Socket.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001685
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001686 :param class_socket socket: Is the manipulated Socket.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001687
1688 Note: It is important to close all used sockets once they are not needed,
1689 since, in many systems, each socket uses a file descriptor, which are limited
1690 system resources. Garbage-collected objects are automatically closed before
1691 destruction, though.
1692
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02001693.. js:function:: Socket.connect(socket, address[, port])
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001694
1695 Attempts to connect a socket object to a remote host.
1696
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001697
1698 In case of error, the method returns nil followed by a string describing the
1699 error. In case of success, the method returns 1.
1700
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001701 :param class_socket socket: Is the manipulated Socket.
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02001702 :param string address: can be an IP address or a host name. See below for more
1703 information.
1704 :param integer port: must be an integer number in the range [1..64K].
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001705 :returns: 1 or nil.
1706
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02001707 an address field extension permits to use the connect() function to connect to
1708 other stream than TCP. The syntax containing a simpleipv4 or ipv6 address is
1709 the basically expected format. This format requires the port.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001710
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02001711 Other format accepted are a socket path like "/socket/path", it permits to
1712 connect to a socket. abstract namespaces are supported with the prefix
1713 "abns@", and finaly a filedescriotr can be passed with the prefix "fd@".
1714 The prefix "ipv4@", "ipv6@" and "unix@" are also supported. The port can be
1715 passed int the string. The syntax "127.0.0.1:1234" is valid. in this case, the
1716 parameter *port* is ignored.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001717
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001718.. js:function:: Socket.connect_ssl(socket, address, port)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001719
1720 Same behavior than the function socket:connect, but uses SSL.
1721
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001722 :param class_socket socket: Is the manipulated Socket.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001723 :returns: 1 or nil.
1724
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001725.. js:function:: Socket.getpeername(socket)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001726
1727 Returns information about the remote side of a connected client object.
1728
1729 Returns a string with the IP address of the peer, followed by the port number
1730 that peer is using for the connection. In case of error, the method returns
1731 nil.
1732
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001733 :param class_socket socket: Is the manipulated Socket.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001734 :returns: a string containing the server information.
1735
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001736.. js:function:: Socket.getsockname(socket)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001737
1738 Returns the local address information associated to the object.
1739
1740 The method returns a string with local IP address and a number with the port.
1741 In case of error, the method returns nil.
1742
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001743 :param class_socket socket: Is the manipulated Socket.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001744 :returns: a string containing the client information.
1745
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001746.. js:function:: Socket.receive(socket, [pattern [, prefix]])
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001747
1748 Reads data from a client object, according to the specified read pattern.
1749 Patterns follow the Lua file I/O format, and the difference in performance
1750 between all patterns is negligible.
1751
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001752 :param class_socket socket: Is the manipulated Socket.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001753 :param string|integer pattern: Describe what is required (see below).
1754 :param string prefix: A string which will be prefix the returned data.
1755 :returns: a string containing the required data or nil.
1756
1757 Pattern can be any of the following:
1758
1759 * **`*a`**: reads from the socket until the connection is closed. No
1760 end-of-line translation is performed;
1761
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001762 * **`*l`**: reads a line of text from the Socket. The line is terminated by a
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001763 LF character (ASCII 10), optionally preceded by a CR character
1764 (ASCII 13). The CR and LF characters are not included in the
1765 returned line. In fact, all CR characters are ignored by the
1766 pattern. This is the default pattern.
1767
1768 * **number**: causes the method to read a specified number of bytes from the
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001769 Socket. Prefix is an optional string to be concatenated to the
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001770 beginning of any received data before return.
1771
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02001772 * **empty**: If the pattern is left empty, the default option is `*l`.
1773
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001774 If successful, the method returns the received pattern. In case of error, the
1775 method returns nil followed by an error message which can be the string
1776 'closed' in case the connection was closed before the transmission was
1777 completed or the string 'timeout' in case there was a timeout during the
1778 operation. Also, after the error message, the function returns the partial
1779 result of the transmission.
1780
1781 Important note: This function was changed severely. It used to support
1782 multiple patterns (but I have never seen this feature used) and now it
1783 doesn't anymore. Partial results used to be returned in the same way as
1784 successful results. This last feature violated the idea that all functions
1785 should return nil on error. Thus it was changed too.
1786
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001787.. js:function:: Socket.send(socket, data [, start [, end ]])
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001788
1789 Sends data through client object.
1790
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001791 :param class_socket socket: Is the manipulated Socket.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001792 :param string data: The data that will be sent.
1793 :param integer start: The start position in the buffer of the data which will
1794 be sent.
1795 :param integer end: The end position in the buffer of the data which will
1796 be sent.
1797 :returns: see below.
1798
1799 Data is the string to be sent. The optional arguments i and j work exactly
1800 like the standard string.sub Lua function to allow the selection of a
1801 substring to be sent.
1802
1803 If successful, the method returns the index of the last byte within [start,
1804 end] that has been sent. Notice that, if start is 1 or absent, this is
1805 effectively the total number of bytes sent. In case of error, the method
1806 returns nil, followed by an error message, followed by the index of the last
1807 byte within [start, end] that has been sent. You might want to try again from
1808 the byte following that. The error message can be 'closed' in case the
1809 connection was closed before the transmission was completed or the string
1810 'timeout' in case there was a timeout during the operation.
1811
1812 Note: Output is not buffered. For small strings, it is always better to
1813 concatenate them in Lua (with the '..' operator) and send the result in one
1814 call instead of calling the method several times.
1815
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001816.. js:function:: Socket.setoption(socket, option [, value])
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001817
1818 Just implemented for compatibility, this cal does nothing.
1819
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001820.. js:function:: Socket.settimeout(socket, value [, mode])
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001821
1822 Changes the timeout values for the object. All I/O operations are blocking.
1823 That is, any call to the methods send, receive, and accept will block
1824 indefinitely, until the operation completes. The settimeout method defines a
1825 limit on the amount of time the I/O methods can block. When a timeout time
1826 has elapsed, the affected methods give up and fail with an error code.
1827
1828 The amount of time to wait is specified as the value parameter, in seconds.
1829
1830 The timeout modes are bot implemented, the only settable timeout is the
1831 inactivity time waiting for complete the internal buffer send or waiting for
1832 receive data.
1833
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001834 :param class_socket socket: Is the manipulated Socket.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001835 :param integer value: The timeout value.
1836
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001837.. _map_class:
1838
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001839Map class
1840=========
1841
1842.. js:class:: Map
1843
1844 This class permits to do some lookup in HAProxy maps. The declared maps can
1845 be modified during the runtime throught the HAProxy management socket.
1846
1847.. code-block:: lua
1848
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001849 default = "usa"
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001850
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001851 -- Create and load map
1852 geo = Map.new("geo.map", Map.ip);
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001853
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001854 -- Create new fetch that returns the user country
1855 core.register_fetches("country", function(txn)
1856 local src;
1857 local loc;
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001858
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001859 src = txn.f:fhdr("x-forwarded-for");
1860 if (src == nil) then
1861 src = txn.f:src()
1862 if (src == nil) then
1863 return default;
1864 end
1865 end
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001866
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001867 -- Perform lookup
1868 loc = geo:lookup(src);
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001869
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001870 if (loc == nil) then
1871 return default;
1872 end
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001873
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001874 return loc;
1875 end);
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001876
1877.. js:attribute:: Map.int
1878
1879 See the HAProxy configuration.txt file, chapter "Using ACLs and fetching
1880 samples" ans subchapter "ACL basics" to understand this pattern matching
1881 method.
1882
1883.. js:attribute:: Map.ip
1884
1885 See the HAProxy configuration.txt file, chapter "Using ACLs and fetching
1886 samples" ans subchapter "ACL basics" to understand this pattern matching
1887 method.
1888
1889.. js:attribute:: Map.str
1890
1891 See the HAProxy configuration.txt file, chapter "Using ACLs and fetching
1892 samples" ans subchapter "ACL basics" to understand this pattern matching
1893 method.
1894
1895.. js:attribute:: Map.beg
1896
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
1901.. js:attribute:: Map.sub
1902
1903 See the HAProxy configuration.txt file, chapter "Using ACLs and fetching
1904 samples" ans subchapter "ACL basics" to understand this pattern matching
1905 method.
1906
1907.. js:attribute:: Map.dir
1908
1909 See the HAProxy configuration.txt file, chapter "Using ACLs and fetching
1910 samples" ans subchapter "ACL basics" to understand this pattern matching
1911 method.
1912
1913.. js:attribute:: Map.dom
1914
1915 See the HAProxy configuration.txt file, chapter "Using ACLs and fetching
1916 samples" ans subchapter "ACL basics" to understand this pattern matching
1917 method.
1918
1919.. js:attribute:: Map.end
1920
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
1925.. js:attribute:: Map.reg
1926
1927 See the HAProxy configuration.txt file, chapter "Using ACLs and fetching
1928 samples" ans subchapter "ACL basics" to understand this pattern matching
1929 method.
1930
1931
1932.. js:function:: Map.new(file, method)
1933
1934 Creates and load a map.
1935
1936 :param string file: Is the file containing the map.
1937 :param integer method: Is the map pattern matching method. See the attributes
1938 of the Map class.
1939 :returns: a class Map object.
1940 :see: The Map attributes.
1941
1942.. js:function:: Map.lookup(map, str)
1943
1944 Perform a lookup in a map.
1945
1946 :param class_map map: Is the class Map object.
1947 :param string str: Is the string used as key.
1948 :returns: a string containing the result or nil if no match.
1949
1950.. js:function:: Map.slookup(map, str)
1951
1952 Perform a lookup in a map.
1953
1954 :param class_map map: Is the class Map object.
1955 :param string str: Is the string used as key.
1956 :returns: a string containing the result or empty string if no match.
1957
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001958.. _applethttp_class:
1959
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02001960AppletHTTP class
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001961================
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02001962
1963.. js:class:: AppletHTTP
1964
1965 This class is used with applets that requires the 'http' mode. The http applet
1966 can be registered with the *core.register_service()* function. They are used
1967 for processing an http request like a server in back of HAProxy.
1968
1969 This is an hello world sample code:
1970
1971.. code-block:: lua
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001972
Pieter Baauw4d7f7662015-11-08 16:38:08 +01001973 core.register_service("hello-world", "http", function(applet)
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02001974 local response = "Hello World !"
1975 applet:set_status(200)
Pieter Baauw2dcb9bc2015-10-01 22:47:12 +02001976 applet:add_header("content-length", string.len(response))
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02001977 applet:add_header("content-type", "text/plain")
Pieter Baauw2dcb9bc2015-10-01 22:47:12 +02001978 applet:start_response()
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02001979 applet:send(response)
1980 end)
1981
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001982.. js:attribute:: AppletHTTP.c
1983
1984 :returns: A :ref:`converters_class`
1985
1986 This attribute contains a Converters class object.
1987
1988.. js:attribute:: AppletHTTP.sc
1989
1990 :returns: A :ref:`converters_class`
1991
1992 This attribute contains a Converters class object. The
1993 functions of this object returns always a string.
1994
1995.. js:attribute:: AppletHTTP.f
1996
1997 :returns: A :ref:`fetches_class`
1998
1999 This attribute contains a Fetches class object. Note that the
2000 applet execution place cannot access to a valid HAProxy core HTTP
2001 transaction, so some sample fecthes related to the HTTP dependant
2002 values (hdr, path, ...) are not available.
2003
2004.. js:attribute:: AppletHTTP.sf
2005
2006 :returns: A :ref:`fetches_class`
2007
2008 This attribute contains a Fetches class object. The functions of
2009 this object returns always a string. Note that the applet
2010 execution place cannot access to a valid HAProxy core HTTP
2011 transaction, so some sample fecthes related to the HTTP dependant
2012 values (hdr, path, ...) are not available.
2013
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01002014.. js:attribute:: AppletHTTP.method
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002015
2016 :returns: string
2017
2018 The attribute method returns a string containing the HTTP
2019 method.
2020
2021.. js:attribute:: AppletHTTP.version
2022
2023 :returns: string
2024
2025 The attribute version, returns a string containing the HTTP
2026 request version.
2027
2028.. js:attribute:: AppletHTTP.path
2029
2030 :returns: string
2031
2032 The attribute path returns a string containing the HTTP
2033 request path.
2034
2035.. js:attribute:: AppletHTTP.qs
2036
2037 :returns: string
2038
2039 The attribute qs returns a string containing the HTTP
2040 request query string.
2041
2042.. js:attribute:: AppletHTTP.length
2043
2044 :returns: integer
2045
2046 The attribute length returns an integer containing the HTTP
2047 body length.
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002048
Thierry FOURNIER841475e2015-12-11 17:10:09 +01002049.. js:attribute:: AppletHTTP.headers
2050
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002051 :returns: array
2052
2053 The attribute headers returns an array containing the HTTP
2054 headers. The header names are always in lower case. As the header name can be
2055 encountered more than once in each request, the value is indexed with 0 as
2056 first index value. The array have this form:
2057
2058.. code-block:: lua
2059
2060 AppletHTTP.headers['<header-name>'][<header-index>] = "<header-value>"
2061
2062 AppletHTTP.headers["host"][0] = "www.test.com"
2063 AppletHTTP.headers["accept"][0] = "audio/basic q=1"
2064 AppletHTTP.headers["accept"][1] = "audio/*, q=0.2"
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01002065 AppletHTTP.headers["accept"][2] = "*/*, q=0.1"
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002066..
2067
2068.. js:attribute:: AppletHTTP.headers
2069
Thierry FOURNIER841475e2015-12-11 17:10:09 +01002070 Contains an array containing all the request headers.
2071
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01002072.. js:function:: AppletHTTP.set_status(applet, code)
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002073
2074 This function sets the HTTP status code for the response. The allowed code are
2075 from 100 to 599.
2076
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01002077 :param class_AppletHTTP applet: An :ref:`applethttp_class`
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002078 :param integer code: the status code returned to the client.
2079
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01002080.. js:function:: AppletHTTP.add_header(applet, name, value)
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002081
2082 This function add an header in the response. Duplicated headers are not
2083 collapsed. The special header *content-length* is used to determinate the
2084 response length. If it not exists, a *transfer-encoding: chunked* is set, and
2085 all the write from the funcion *AppletHTTP:send()* become a chunk.
2086
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01002087 :param class_AppletHTTP applet: An :ref:`applethttp_class`
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002088 :param string name: the header name
2089 :param string value: the header value
2090
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01002091.. js:function:: AppletHTTP.start_response(applet)
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002092
2093 This function indicates to the HTTP engine that it can process and send the
2094 response headers. After this called we cannot add headers to the response; We
2095 cannot use the *AppletHTTP:send()* function if the
2096 *AppletHTTP:start_response()* is not called.
2097
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01002098 :param class_AppletHTTP applet: An :ref:`applethttp_class`
2099
2100.. js:function:: AppletHTTP.getline(applet)
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002101
2102 This function returns a string containing one line from the http body. If the
2103 data returned doesn't contains a final '\\n' its assumed than its the last
2104 available data before the end of stream.
2105
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01002106 :param class_AppletHTTP applet: An :ref:`applethttp_class`
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002107 :returns: a string. The string can be empty if we reach the end of the stream.
2108
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01002109.. js:function:: AppletHTTP.receive(applet, [size])
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002110
2111 Reads data from the HTTP body, according to the specified read *size*. If the
2112 *size* is missing, the function tries to read all the content of the stream
2113 until the end. If the *size* is bigger than the http body, it returns the
2114 amount of data avalaible.
2115
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01002116 :param class_AppletHTTP applet: An :ref:`applethttp_class`
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002117 :param integer size: the required read size.
2118 :returns: always return a string,the string can be empty is the connexion is
2119 closed.
2120
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01002121.. js:function:: AppletHTTP.send(applet, msg)
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002122
2123 Send the message *msg* on the http request body.
2124
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01002125 :param class_AppletHTTP applet: An :ref:`applethttp_class`
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002126 :param string msg: the message to send.
2127
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01002128.. js:function:: AppletHTTP.get_priv(applet)
2129
2130 Return Lua data stored in the current transaction (with the
2131 `AppletHTTP.set_priv()`) function. If no data are stored, it returns a nil
2132 value.
2133
2134 :param class_AppletHTTP applet: An :ref:`applethttp_class`
2135 :returns: the opaque data previsously stored, or nil if nothing is
2136 avalaible.
2137
2138.. js:function:: AppletHTTP.set_priv(applet, data)
2139
2140 Store any data in the current HAProxy transaction. This action replace the
2141 old stored data.
2142
2143 :param class_AppletHTTP applet: An :ref:`applethttp_class`
2144 :param opaque data: The data which is stored in the transaction.
2145
Thierry FOURNIER / OZON.IOc1edafe2016-12-12 16:25:30 +01002146.. js:function:: AppletHTTP.set_var(applet, var, value)
2147
2148 Converts a Lua type in a HAProxy type and store it in a variable <var>.
2149
2150 :param class_AppletHTTP applet: An :ref:`applethttp_class`
2151 :param string var: The variable name according with the HAProxy variable syntax.
2152 :param type value: The value associated to the variable. The type ca be string or
2153 integer.
2154
2155.. js:function:: AppletHTTP.unset_var(applet, var)
2156
2157 Unset the variable <var>.
2158
2159 :param class_AppletHTTP applet: An :ref:`applethttp_class`
2160 :param string var: The variable name according with the HAProxy variable syntax.
2161
2162.. js:function:: AppletHTTP.get_var(applet, var)
2163
2164 Returns data stored in the variable <var> converter in Lua type.
2165
2166 :param class_AppletHTTP applet: An :ref:`applethttp_class`
2167 :param string var: The variable name according with the HAProxy variable syntax.
2168
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002169.. _applettcp_class:
2170
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002171AppletTCP class
2172===============
2173
2174.. js:class:: AppletTCP
2175
2176 This class is used with applets that requires the 'tcp' mode. The tcp applet
2177 can be registered with the *core.register_service()* function. They are used
2178 for processing a tcp stream like a server in back of HAProxy.
2179
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002180.. js:attribute:: AppletTCP.c
2181
2182 :returns: A :ref:`converters_class`
2183
2184 This attribute contains a Converters class object.
2185
2186.. js:attribute:: AppletTCP.sc
2187
2188 :returns: A :ref:`converters_class`
2189
2190 This attribute contains a Converters class object. The
2191 functions of this object returns always a string.
2192
2193.. js:attribute:: AppletTCP.f
2194
2195 :returns: A :ref:`fetches_class`
2196
2197 This attribute contains a Fetches class object.
2198
2199.. js:attribute:: AppletTCP.sf
2200
2201 :returns: A :ref:`fetches_class`
2202
2203 This attribute contains a Fetches class object.
2204
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01002205.. js:function:: AppletTCP.getline(applet)
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002206
2207 This function returns a string containing one line from the stream. If the
2208 data returned doesn't contains a final '\\n' its assumed than its the last
2209 available data before the end of stream.
2210
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01002211 :param class_AppletTCP applet: An :ref:`applettcp_class`
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002212 :returns: a string. The string can be empty if we reach the end of the stream.
2213
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01002214.. js:function:: AppletTCP.receive(applet, [size])
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002215
2216 Reads data from the TCP stream, according to the specified read *size*. If the
2217 *size* is missing, the function tries to read all the content of the stream
2218 until the end.
2219
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01002220 :param class_AppletTCP applet: An :ref:`applettcp_class`
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002221 :param integer size: the required read size.
2222 :returns: always return a string,the string can be empty is the connexion is
2223 closed.
2224
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01002225.. js:function:: AppletTCP.send(appletmsg)
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002226
2227 Send the message on the stream.
2228
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01002229 :param class_AppletTCP applet: An :ref:`applettcp_class`
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002230 :param string msg: the message to send.
2231
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01002232.. js:function:: AppletTCP.get_priv(applet)
2233
2234 Return Lua data stored in the current transaction (with the
2235 `AppletTCP.set_priv()`) function. If no data are stored, it returns a nil
2236 value.
2237
2238 :param class_AppletTCP applet: An :ref:`applettcp_class`
2239 :returns: the opaque data previsously stored, or nil if nothing is
2240 avalaible.
2241
2242.. js:function:: AppletTCP.set_priv(applet, data)
2243
2244 Store any data in the current HAProxy transaction. This action replace the
2245 old stored data.
2246
2247 :param class_AppletTCP applet: An :ref:`applettcp_class`
2248 :param opaque data: The data which is stored in the transaction.
2249
Thierry FOURNIER / OZON.IOc1edafe2016-12-12 16:25:30 +01002250.. js:function:: AppletTCP.set_var(applet, var, value)
2251
2252 Converts a Lua type in a HAProxy type and stores it in a variable <var>.
2253
2254 :param class_AppletTCP applet: An :ref:`applettcp_class`
2255 :param string var: The variable name according with the HAProxy variable syntax.
2256 :param type value: The value associated to the variable. The type can be string or
2257 integer.
2258
2259.. js:function:: AppletTCP.unset_var(applet, var)
2260
2261 Unsets the variable <var>.
2262
2263 :param class_AppletTCP applet: An :ref:`applettcp_class`
2264 :param string var: The variable name according with the HAProxy variable syntax.
2265
2266.. js:function:: AppletTCP.get_var(applet, var)
2267
2268 Returns data stored in the variable <var> converter in Lua type.
2269
2270 :param class_AppletTCP applet: An :ref:`applettcp_class`
2271 :param string var: The variable name according with the HAProxy variable syntax.
2272
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002273External Lua libraries
2274======================
2275
2276A lot of useful lua libraries can be found here:
2277
2278* `https://lua-toolbox.com/ <https://lua-toolbox.com/>`_
2279
2280Redis acces:
2281
2282* `https://github.com/nrk/redis-lua <https://github.com/nrk/redis-lua>`_
2283
2284This is an example about the usage of the Redis library with HAProxy. Note that
2285each call of any function of this library can throw an error if the socket
2286connection fails.
2287
2288.. code-block:: lua
2289
2290 -- load the redis library
2291 local redis = require("redis");
2292
2293 function do_something(txn)
2294
2295 -- create and connect new tcp socket
2296 local tcp = core.tcp();
2297 tcp:settimeout(1);
2298 tcp:connect("127.0.0.1", 6379);
2299
2300 -- use the redis library with this new socket
2301 local client = redis.connect({socket=tcp});
2302 client:ping();
2303
2304 end
2305
2306OpenSSL:
2307
2308* `http://mkottman.github.io/luacrypto/index.html
2309 <http://mkottman.github.io/luacrypto/index.html>`_
2310
2311* `https://github.com/brunoos/luasec/wiki
2312 <https://github.com/brunoos/luasec/wiki>`_
Thierry FOURNIER2e4893c2015-03-18 13:37:27 +01002313