blob: 530cd59cc447be57bd00dad9704927b8cd0a88a9 [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 FOURNIER17bd1522015-03-11 20:31:00 +0100300.. js:function:: core.msleep(milliseconds)
301
302 **context**: body, init, task, action
303
304 The `core.msleep()` stops the Lua execution between specified milliseconds.
305
306 :param integer milliseconds: the required milliseconds.
307
Thierry Fournierf61aa632016-02-19 20:56:00 +0100308.. js:attribute:: core.proxies
309
310 **context**: body, init, task, action, sample-fetch, converter
311
312 proxies is an array containing the list of all proxies declared in the
313 configuration file. Each entry of the proxies array is an object of type
314 :ref:`proxy_class`
315
Thierry FOURNIER8255a752015-09-23 21:03:35 +0200316.. js:function:: core.register_action(name, actions, func)
317
318 **context**: body
319
David Carlier61fdf8b2015-10-02 11:59:38 +0100320 Register a Lua function executed as action. All the registered action can be
Thierry FOURNIER8255a752015-09-23 21:03:35 +0200321 used in HAProxy with the prefix "lua.". An action gets a TXN object class as
322 input.
323
324 :param string name: is the name of the converter.
Willy Tarreau61add3c2015-09-28 15:39:10 +0200325 :param table actions: is a table of string describing the HAProxy actions who
Thierry FOURNIER8255a752015-09-23 21:03:35 +0200326 want to register to. The expected actions are 'tcp-req',
327 'tcp-res', 'http-req' or 'http-res'.
328 :param function func: is the Lua function called to work as converter.
329
330 The prototype of the Lua function used as argument is:
331
332.. code-block:: lua
333
334 function(txn)
335..
336
Thierry FOURNIERdc595002015-12-21 11:13:52 +0100337 * **txn** (:ref:`txn_class`): this is a TXN object used for manipulating the
Thierry FOURNIER8255a752015-09-23 21:03:35 +0200338 current request or TCP stream.
339
Willy Tarreau61add3c2015-09-28 15:39:10 +0200340 Here, an exemple of action registration. the action juste send an 'Hello world'
Thierry FOURNIER8255a752015-09-23 21:03:35 +0200341 in the logs.
342
343.. code-block:: lua
344
345 core.register_action("hello-world", { "tcp-req", "http-req" }, function(txn)
346 txn:Info("Hello world")
347 end)
348..
349
350 This example code is used in HAproxy configuration like this:
351
352::
353
354 frontend tcp_frt
355 mode tcp
356 tcp-request content lua.hello-world
357
358 frontend http_frt
359 mode http
360 http-request lua.hello-world
361
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100362.. js:function:: core.register_converters(name, func)
363
364 **context**: body
365
David Carlier61fdf8b2015-10-02 11:59:38 +0100366 Register a Lua function executed as converter. All the registered converters
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100367 can be used in HAProxy with the prefix "lua.". An converter get a string as
368 input and return a string as output. The registered function can take up to 9
369 values as parameter. All the value are strings.
370
371 :param string name: is the name of the converter.
372 :param function func: is the Lua function called to work as converter.
373
374 The prototype of the Lua function used as argument is:
375
376.. code-block:: lua
377
378 function(str, [p1 [, p2 [, ... [, p5]]]])
379..
380
381 * **str** (*string*): this is the input value automatically converted in
382 string.
383 * **p1** .. **p5** (*string*): this is a list of string arguments declared in
384 the haroxy configuration file. The number of arguments doesn't exceed 5.
385 The order and the nature of these is conventionally choose by the
386 developper.
387
388.. js:function:: core.register_fetches(name, func)
389
390 **context**: body
391
David Carlier61fdf8b2015-10-02 11:59:38 +0100392 Register a Lua function executed as sample fetch. All the registered sample
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100393 fetchs can be used in HAProxy with the prefix "lua.". A Lua sample fetch
394 return a string as output. The registered function can take up to 9 values as
395 parameter. All the value are strings.
396
397 :param string name: is the name of the converter.
398 :param function func: is the Lua function called to work as sample fetch.
399
400 The prototype of the Lua function used as argument is:
401
402.. code-block:: lua
403
404 string function(txn, [p1 [, p2 [, ... [, p5]]]])
405..
406
Thierry FOURNIERdc595002015-12-21 11:13:52 +0100407 * **txn** (:ref:`txn_class`): this is the txn object associated with the current
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100408 request.
409 * **p1** .. **p5** (*string*): this is a list of string arguments declared in
410 the haroxy configuration file. The number of arguments doesn't exceed 5.
411 The order and the nature of these is conventionally choose by the
412 developper.
413 * **Returns**: A string containing some data, ot nil if the value cannot be
414 returned now.
415
416 lua example code:
417
418.. code-block:: lua
419
420 core.register_fetches("hello", function(txn)
421 return "hello"
422 end)
423..
424
425 HAProxy example configuration:
426
427::
428
429 frontend example
430 http-request redirect location /%[lua.hello]
431
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +0200432.. js:function:: core.register_service(name, mode, func)
433
434 **context**: body
435
David Carlier61fdf8b2015-10-02 11:59:38 +0100436 Register a Lua function executed as a service. All the registered service can
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +0200437 be used in HAProxy with the prefix "lua.". A service gets an object class as
438 input according with the required mode.
439
440 :param string name: is the name of the converter.
Willy Tarreau61add3c2015-09-28 15:39:10 +0200441 :param string mode: is string describing the required mode. Only 'tcp' or
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +0200442 'http' are allowed.
443 :param function func: is the Lua function called to work as converter.
444
445 The prototype of the Lua function used as argument is:
446
447.. code-block:: lua
448
449 function(applet)
450..
451
Thierry FOURNIERdc595002015-12-21 11:13:52 +0100452 * **applet** *applet* will be a :ref:`applettcp_class` or a
453 :ref:`applethttp_class`. It depends the type of registered applet. An applet
454 registered with the 'http' value for the *mode* parameter will gets a
455 :ref:`applethttp_class`. If the *mode* value is 'tcp', the applet will gets
456 a :ref:`applettcp_class`.
457
458 **warning**: Applets of type 'http' cannot be called from 'tcp-*'
459 rulesets. Only the 'http-*' rulesets are authorized, this means
460 that is not possible to call an HTTP applet from a proxy in tcp
461 mode. Applets of type 'tcp' can be called from anywhre.
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +0200462
Willy Tarreau61add3c2015-09-28 15:39:10 +0200463 Here, an exemple of service registration. the service just send an 'Hello world'
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +0200464 as an http response.
465
466.. code-block:: lua
467
Pieter Baauw4d7f7662015-11-08 16:38:08 +0100468 core.register_service("hello-world", "http", function(applet)
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +0200469 local response = "Hello World !"
470 applet:set_status(200)
Pieter Baauw2dcb9bc2015-10-01 22:47:12 +0200471 applet:add_header("content-length", string.len(response))
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +0200472 applet:add_header("content-type", "text/plain")
Pieter Baauw2dcb9bc2015-10-01 22:47:12 +0200473 applet:start_response()
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +0200474 applet:send(response)
475 end)
476..
477
478 This example code is used in HAproxy configuration like this:
479
480::
481
482 frontend example
483 http-request use-service lua.hello-world
484
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100485.. js:function:: core.register_init(func)
486
487 **context**: body
488
489 Register a function executed after the configuration parsing. This is useful
490 to check any parameters.
491
Pieter Baauw4d7f7662015-11-08 16:38:08 +0100492 :param function func: is the Lua function called to work as initializer.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100493
494 The prototype of the Lua function used as argument is:
495
496.. code-block:: lua
497
498 function()
499..
500
501 It takes no input, and no output is expected.
502
503.. js:function:: core.register_task(func)
504
505 **context**: body, init, task, action, sample-fetch, converter
506
507 Register and start independent task. The task is started when the HAProxy
508 main scheduler starts. For example this type of tasks can be executed to
Thierry FOURNIER2e4893c2015-03-18 13:37:27 +0100509 perform complex health checks.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100510
Pieter Baauw4d7f7662015-11-08 16:38:08 +0100511 :param function func: is the Lua function called to work as initializer.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100512
513 The prototype of the Lua function used as argument is:
514
515.. code-block:: lua
516
517 function()
518..
519
520 It takes no input, and no output is expected.
521
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +0100522.. js:function:: core.register_cli([path], usage, func)
523
524 **context**: body
525
526 Register and start independent task. The task is started when the HAProxy
527 main scheduler starts. For example this type of tasks can be executed to
528 perform complex health checks.
529
530 :param array path: is the sequence of word for which the cli execute the Lua
531 binding.
532 :param string usage: is the usage message displayed in the help.
533 :param function func: is the Lua function called to handle the CLI commands.
534
535 The prototype of the Lua function used as argument is:
536
537.. code-block:: lua
538
539 function(AppletTCP, [arg1, [arg2, [...]]])
540..
541
542 I/O are managed with the :ref:`applettcp_class` object. Args are given as
543 paramter. The args embbed the registred path. If the path is declared like
544 this:
545
546.. code-block:: lua
547
548 core.register_cli({"show", "ssl", "stats"}, "Display SSL stats..", function(applet, arg1, arg2, arg3, arg4, arg5)
549 end)
550..
551
552 And we execute this in the prompt:
553
554.. code-block:: text
555
556 > prompt
557 > show ssl stats all
558..
559
560 Then, arg1, arg2 and arg3 will contains respectivey "show", "ssl" and "stats".
561 arg4 will contain "all". arg5 contains nil.
562
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100563.. js:function:: core.set_nice(nice)
564
565 **context**: task, action, sample-fetch, converter
566
567 Change the nice of the current task or current session.
Thierry FOURNIER2e4893c2015-03-18 13:37:27 +0100568
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100569 :param integer nice: the nice value, it must be between -1024 and 1024.
570
571.. js:function:: core.set_map(filename, key, value)
572
573 **context**: init, task, action, sample-fetch, converter
574
575 set the value *value* associated to the key *key* in the map referenced by
576 *filename*.
577
Thierry FOURNIERdc595002015-12-21 11:13:52 +0100578 :param string filename: the Map reference
579 :param string key: the key to set or replace
580 :param string value: the associated value
581
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100582.. js:function:: core.sleep(int seconds)
583
584 **context**: body, init, task, action
585
586 The `core.sleep()` functions stop the Lua execution between specified seconds.
587
588 :param integer seconds: the required seconds.
589
590.. js:function:: core.tcp()
591
592 **context**: init, task, action
593
594 This function returns a new object of a *socket* class.
595
Thierry FOURNIERdc595002015-12-21 11:13:52 +0100596 :returns: A :ref:`socket_class` object.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100597
Thierry Fournier1de16592016-01-27 09:49:07 +0100598.. js:function:: core.concat()
599
600 **context**: body, init, task, action, sample-fetch, converter
601
602 This function retruns a new concat object.
603
604 :returns: A :ref:`concat_class` object.
605
Thierry FOURNIER0a99b892015-08-26 00:14:17 +0200606.. js:function:: core.done(data)
607
608 **context**: body, init, task, action, sample-fetch, converter
609
610 :param any data: Return some data for the caller. It is useful with
611 sample-fetches and sample-converters.
612
613 Immediately stops the current Lua execution and returns to the caller which
614 may be a sample fetch, a converter or an action and returns the specified
615 value (ignored for actions). It is used when the LUA process finishes its
616 work and wants to give back the control to HAProxy without executing the
617 remaining code. It can be seen as a multi-level "return".
618
Thierry FOURNIER486f5a02015-03-16 15:13:03 +0100619.. js:function:: core.yield()
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100620
621 **context**: task, action, sample-fetch, converter
622
623 Give back the hand at the HAProxy scheduler. It is used when the LUA
624 processing consumes a lot of processing time.
625
Thierry FOURNIER / OZON.IO62fec752016-11-10 20:38:11 +0100626.. js:function:: core.parse_addr(address)
627
628 **context**: body, init, task, action, sample-fetch, converter
629
630 :param network: is a string describing an ipv4 or ipv6 address and optionally
631 its network length, like this: "127.0.0.1/8" or "aaaa::1234/32".
632 :returns: a userdata containing network or nil if an error occurs.
633
634 Parse ipv4 or ipv6 adresses and its facultative associated network.
635
636.. js:function:: core.match_addr(addr1, addr2)
637
638 **context**: body, init, task, action, sample-fetch, converter
639
640 :param addr1: is an address created with "core.parse_addr".
641 :param addr2: is an address created with "core.parse_addr".
642 :returns: boolean, true if the network of the addresses matche, else returns
643 false.
644
645 Match two networks. For example "127.0.0.1/32" matchs "127.0.0.0/8". The order
646 of network is not important.
647
Thierry Fournierf61aa632016-02-19 20:56:00 +0100648.. _proxy_class:
649
650Proxy class
651============
652
653.. js:class:: Proxy
654
655 This class provides a way for manipulating proxy and retrieving information
656 like statistics.
657
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +0100658.. js:attribute:: Proxy.servers
659
660 Contain an array with the attached servers. Each server entry is an object of
661 type :ref:`server_class`.
662
Thierry Fournierff480422016-02-25 08:36:46 +0100663.. js:attribute:: Proxy.listeners
664
665 Contain an array with the attached listeners. Each listeners entry is an
666 object of type :ref:`listener_class`.
667
Thierry Fournierf61aa632016-02-19 20:56:00 +0100668.. js:function:: Proxy.pause(px)
669
670 Pause the proxy. See the management socket documentation for more information.
671
672 :param class_proxy px: A :ref:`proxy_class` which indicates the manipulated
673 proxy.
674
675.. js:function:: Proxy.resume(px)
676
677 Resume the proxy. See the management socket documentation for more
678 information.
679
680 :param class_proxy px: A :ref:`proxy_class` which indicates the manipulated
681 proxy.
682
683.. js:function:: Proxy.stop(px)
684
685 Stop the proxy. See the management socket documentation for more information.
686
687 :param class_proxy px: A :ref:`proxy_class` which indicates the manipulated
688 proxy.
689
690.. js:function:: Proxy.shut_bcksess(px)
691
692 Kill the session attached to a backup server. See the management socket
693 documentation for more information.
694
695 :param class_proxy px: A :ref:`proxy_class` which indicates the manipulated
696 proxy.
697
698.. js:function:: Proxy.get_cap(px)
699
700 Returns a string describing the capabilities of the proxy.
701
702 :param class_proxy px: A :ref:`proxy_class` which indicates the manipulated
703 proxy.
704 :returns: a string "frontend", "backend", "proxy" or "ruleset".
705
706.. js:function:: Proxy.get_mode(px)
707
708 Returns a string describing the mode of the current proxy.
709
710 :param class_proxy px: A :ref:`proxy_class` which indicates the manipulated
711 proxy.
712 :returns: a string "tcp", "http", "health" or "unknown"
713
714.. js:function:: Proxy.get_stats(px)
715
716 Returns an array containg the proxy statistics. The statistics returned are
717 not the same if the proxy is frontend or a backend.
718
719 :param class_proxy px: A :ref:`proxy_class` which indicates the manipulated
720 proxy.
721 :returns: a key/value array containing stats
722
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +0100723.. _server_class:
724
725Server class
726============
727
728.. js:function:: Server.is_draining(sv)
729
730 Return true if the server is currently draining stiky connections.
731
732 :param class_server sv: A :ref:`server_class` which indicates the manipulated
733 server.
734 :returns: a boolean
735
736.. js:function:: Server.set_weight(sv, weight)
737
738 Dynamically change the weight of the serveur. See the management socket
739 documentation for more information about the format of the string.
740
741 :param class_server sv: A :ref:`server_class` which indicates the manipulated
742 server.
743 :param string weight: A string describing the server weight.
744
745.. js:function:: Server.get_weight(sv)
746
747 This function returns an integer representing the serveur weight.
748
749 :param class_server sv: A :ref:`server_class` which indicates the manipulated
750 server.
751 :returns: an integer.
752
753.. js:function:: Server.set_addr(sv, addr)
754
755 Dynamically change the address of the serveur. See the management socket
756 documentation for more information about the format of the string.
757
758 :param class_server sv: A :ref:`server_class` which indicates the manipulated
759 server.
760 :param string weight: A string describing the server address.
761
762.. js:function:: Server.get_addr(sv)
763
764 Returns a string describing the address of the serveur.
765
766 :param class_server sv: A :ref:`server_class` which indicates the manipulated
767 server.
768 :returns: A string
769
770.. js:function:: Server.get_stats(sv)
771
772 Returns server statistics.
773
774 :param class_server sv: A :ref:`server_class` which indicates the manipulated
775 server.
776 :returns: a key/value array containing stats
777
778.. js:function:: Server.shut_sess(sv)
779
780 Shutdown all the sessions attached to the server. See the management socket
781 documentation for more information about this function.
782
783 :param class_server sv: A :ref:`server_class` which indicates the manipulated
784 server.
785
786.. js:function:: Server.set_drain(sv)
787
788 Drain sticky sessions. See the management socket documentation for more
789 information about this function.
790
791 :param class_server sv: A :ref:`server_class` which indicates the manipulated
792 server.
793
794.. js:function:: Server.set_maint(sv)
795
796 Set maintenance mode. See the management socket documentation for more
797 information about this function.
798
799 :param class_server sv: A :ref:`server_class` which indicates the manipulated
800 server.
801
802.. js:function:: Server.set_ready(sv)
803
804 Set normal mode. See the management socket documentation for more information
805 about this function.
806
807 :param class_server sv: A :ref:`server_class` which indicates the manipulated
808 server.
809
810.. js:function:: Server.check_enable(sv)
811
812 Enable health checks. See the management socket documentation for more
813 information about this function.
814
815 :param class_server sv: A :ref:`server_class` which indicates the manipulated
816 server.
817
818.. js:function:: Server.check_disable(sv)
819
820 Disable health checks. See the management socket documentation for more
821 information about this function.
822
823 :param class_server sv: A :ref:`server_class` which indicates the manipulated
824 server.
825
826.. js:function:: Server.check_force_up(sv)
827
828 Force health-check up. See the management socket documentation for more
829 information about this function.
830
831 :param class_server sv: A :ref:`server_class` which indicates the manipulated
832 server.
833
834.. js:function:: Server.check_force_nolb(sv)
835
836 Force health-check nolb mode. See the management socket documentation for more
837 information about this function.
838
839 :param class_server sv: A :ref:`server_class` which indicates the manipulated
840 server.
841
842.. js:function:: Server.check_force_down(sv)
843
844 Force health-check down. See the management socket documentation for more
845 information about this function.
846
847 :param class_server sv: A :ref:`server_class` which indicates the manipulated
848 server.
849
850.. js:function:: Server.agent_enable(sv)
851
852 Enable agent check. See the management socket documentation for more
853 information about this function.
854
855 :param class_server sv: A :ref:`server_class` which indicates the manipulated
856 server.
857
858.. js:function:: Server.agent_disable(sv)
859
860 Disable agent check. See the management socket documentation for more
861 information about this function.
862
863 :param class_server sv: A :ref:`server_class` which indicates the manipulated
864 server.
865
866.. js:function:: Server.agent_force_up(sv)
867
868 Force agent check up. See the management socket documentation for more
869 information about this function.
870
871 :param class_server sv: A :ref:`server_class` which indicates the manipulated
872 server.
873
874.. js:function:: Server.agent_force_down(sv)
875
876 Force agent check down. See the management socket documentation for more
877 information about this function.
878
879 :param class_server sv: A :ref:`server_class` which indicates the manipulated
880 server.
881
Thierry Fournierff480422016-02-25 08:36:46 +0100882.. _listener_class:
883
884Listener class
885==============
886
887.. js:function:: Listener.get_stats(ls)
888
889 Returns server statistics.
890
891 :param class_listener ls: A :ref:`listener_class` which indicates the
892 manipulated listener.
893 :returns: a key/value array containing stats
894
Thierry Fournier1de16592016-01-27 09:49:07 +0100895.. _concat_class:
896
897Concat class
898============
899
900.. js:class:: Concat
901
902 This class provides a fast way for string concatenation. The way using native
903 Lua concatenation like the code below is slow for some reasons.
904
905.. code-block:: lua
906
907 str = "string1"
908 str = str .. ", string2"
909 str = str .. ", string3"
910..
911
912 For each concatenation, Lua:
913 * allocate memory for the result,
914 * catenate the two string copying the strings in the new memory bloc,
915 * free the old memory block containing the string whoch is no longer used.
916 This process does many memory move, allocation and free. In addition, the
917 memory is not really freed, it is just mark mark as unsused and wait for the
918 garbage collector.
919
920 The Concat class provide an alternative way for catenating strings. It uses
921 the internal Lua mechanism (it does not allocate memory), but it doesn't copy
922 the data more than once.
923
924 On my computer, the following loops spends 0.2s for the Concat method and
925 18.5s for the pure Lua implementation. So, the Concat class is about 1000x
926 faster than the embedded solution.
927
928.. code-block:: lua
929
930 for j = 1, 100 do
931 c = core.concat()
932 for i = 1, 20000 do
933 c:add("#####")
934 end
935 end
936..
937
938.. code-block:: lua
939
940 for j = 1, 100 do
941 c = ""
942 for i = 1, 20000 do
943 c = c .. "#####"
944 end
945 end
946..
947
948.. js:function:: Concat.add(concat, string)
949
950 This function adds a string to the current concatenated string.
951
952 :param class_concat concat: A :ref:`concat_class` which contains the currently
953 builded string.
954 :param string string: A new string to concatenate to the current builded
955 string.
956
957.. js:function:: Concat.dump(concat)
958
959 This function returns the concanated string.
960
961 :param class_concat concat: A :ref:`concat_class` which contains the currently
962 builded string.
963 :returns: the concatenated string
964
Thierry FOURNIERdc595002015-12-21 11:13:52 +0100965.. _fetches_class:
966
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100967Fetches class
968=============
969
970.. js:class:: Fetches
971
972 This class contains a lot of internal HAProxy sample fetches. See the
Thierry FOURNIER2e4893c2015-03-18 13:37:27 +0100973 HAProxy "configuration.txt" documentation for more information about her
974 usage. they are the chapters 7.3.2 to 7.3.6.
975
Thierry FOURNIERdc595002015-12-21 11:13:52 +0100976 **warning** some sample fetches are not available in some context. These
977 limitations are specified in this documentation when theire useful.
978
Thierry FOURNIER2e4893c2015-03-18 13:37:27 +0100979 :see: TXN.f
980 :see: TXN.sf
981
982 Fetches are useful for:
983
984 * get system time,
985 * get environment variable,
986 * get random numbers,
987 * known backend status like the number of users in queue or the number of
988 connections established,
989 * client information like ip source or destination,
990 * deal with stick tables,
991 * Established SSL informations,
992 * HTTP information like headers or method.
993
994.. code-block:: lua
995
Thierry FOURNIERdc595002015-12-21 11:13:52 +0100996 function action(txn)
997 -- Get source IP
998 local clientip = txn.f:src()
999 end
Thierry FOURNIER2e4893c2015-03-18 13:37:27 +01001000..
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001001
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001002.. _converters_class:
1003
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001004Converters class
1005================
1006
1007.. js:class:: Converters
1008
1009 This class contains a lot of internal HAProxy sample converters. See the
Thierry FOURNIER2e4893c2015-03-18 13:37:27 +01001010 HAProxy documentation "configuration.txt" for more information about her
1011 usage. Its the chapter 7.3.1.
1012
1013 :see: TXN.c
1014 :see: TXN.sc
1015
1016 Converters provides statefull transformation. They are useful for:
1017
1018 * converting input to base64,
1019 * applying hash on input string (djb2, crc32, sdbm, wt6),
1020 * format date,
1021 * json escape,
Pieter Baauw4d7f7662015-11-08 16:38:08 +01001022 * extracting preferred language comparing two lists,
Thierry FOURNIER2e4893c2015-03-18 13:37:27 +01001023 * turn to lower or upper chars,
1024 * deal with stick tables.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001025
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001026.. _channel_class:
1027
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001028Channel class
1029=============
1030
1031.. js:class:: Channel
1032
1033 HAProxy uses two buffers for the processing of the requests. The first one is
1034 used with the request data (from the client to the server) and the second is
1035 used for the response data (from the server to the client).
1036
1037 Each buffer contains two types of data. The first type is the incoming data
1038 waiting for a processing. The second part is the outgoing data already
1039 processed. Usually, the incoming data is processed, after it is tagged as
1040 outgoing data, and finally it is sent. The following functions provides tools
1041 for manipulating these data in a buffer.
1042
1043 The following diagram shows where the channel class function are applied.
1044
1045 **Warning**: It is not possible to read from the response in request action,
1046 and it is not possible to read for the request channel in response action.
1047
Thierry FOURNIER2e4893c2015-03-18 13:37:27 +01001048.. image:: _static/channel.png
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001049
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001050.. js:function:: Channel.dup(channel)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001051
1052 This function returns a string that contain the entire buffer. The data is
1053 not remove from the buffer and can be reprocessed later.
1054
1055 If the buffer cant receive more data, a 'nil' value is returned.
1056
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001057 :param class_channel channel: The manipulated Channel.
Pieter Baauw4d7f7662015-11-08 16:38:08 +01001058 :returns: a string containing all the available data or nil.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001059
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001060.. js:function:: Channel.get(channel)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001061
1062 This function returns a string that contain the entire buffer. The data is
1063 consumed from the buffer.
1064
1065 If the buffer cant receive more data, a 'nil' value is returned.
1066
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001067 :param class_channel channel: The manipulated Channel.
Pieter Baauw4d7f7662015-11-08 16:38:08 +01001068 :returns: a string containing all the available data or nil.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001069
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02001070.. js:function:: Channel.getline(channel)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001071
1072 This function returns a string that contain the first line of the buffer. The
1073 data is consumed. If the data returned doesn't contains a final '\n' its
1074 assumed than its the last available data in the buffer.
1075
1076 If the buffer cant receive more data, a 'nil' value is returned.
1077
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001078 :param class_channel channel: The manipulated Channel.
Pieter Baauw386a1272015-08-16 15:26:24 +02001079 :returns: a string containing the available line or nil.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001080
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001081.. js:function:: Channel.set(channel, string)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001082
1083 This function replace the content of the buffer by the string. The function
1084 returns the copied length, otherwise, it returns -1.
1085
1086 The data set with this function are not send. They wait for the end of
1087 HAProxy processing, so the buffer can be full.
1088
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001089 :param class_channel channel: The manipulated Channel.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001090 :param string string: The data which will sent.
Pieter Baauw4d7f7662015-11-08 16:38:08 +01001091 :returns: an integer containing the amount of bytes copied or -1.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001092
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001093.. js:function:: Channel.append(channel, string)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001094
1095 This function append the string argument to the content of the buffer. The
1096 function returns the copied length, otherwise, it returns -1.
1097
1098 The data set with this function are not send. They wait for the end of
1099 HAProxy processing, so the buffer can be full.
1100
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001101 :param class_channel channel: The manipulated Channel.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001102 :param string string: The data which will sent.
Pieter Baauw4d7f7662015-11-08 16:38:08 +01001103 :returns: an integer containing the amount of bytes copied or -1.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001104
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001105.. js:function:: Channel.send(channel, string)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001106
1107 This function required immediate send of the data. Unless if the connection
1108 is close, the buffer is regularly flushed and all the string can be sent.
1109
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001110 :param class_channel channel: The manipulated Channel.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001111 :param string string: The data which will sent.
Pieter Baauw4d7f7662015-11-08 16:38:08 +01001112 :returns: an integer containing the amount of bytes copied or -1.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001113
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001114.. js:function:: Channel.get_in_length(channel)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001115
1116 This function returns the length of the input part of the buffer.
1117
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001118 :param class_channel channel: The manipulated Channel.
Pieter Baauw4d7f7662015-11-08 16:38:08 +01001119 :returns: an integer containing the amount of available bytes.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001120
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001121.. js:function:: Channel.get_out_length(channel)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001122
1123 This function returns the length of the output part of the buffer.
1124
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001125 :param class_channel channel: The manipulated Channel.
Pieter Baauw4d7f7662015-11-08 16:38:08 +01001126 :returns: an integer containing the amount of available bytes.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001127
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001128.. js:function:: Channel.forward(channel, int)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001129
1130 This function transfer bytes from the input part of the buffer to the output
1131 part.
1132
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001133 :param class_channel channel: The manipulated Channel.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001134 :param integer int: The amount of data which will be forwarded.
1135
Thierry FOURNIER / OZON.IO65192f32016-11-07 15:28:40 +01001136.. js:function:: Channel.is_full(channel)
1137
1138 This function returns true if the buffer channel is full.
1139
1140 :returns: a boolean
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001141
1142.. _http_class:
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001143
1144HTTP class
1145==========
1146
1147.. js:class:: HTTP
1148
1149 This class contain all the HTTP manipulation functions.
1150
Pieter Baauw386a1272015-08-16 15:26:24 +02001151.. js:function:: HTTP.req_get_headers(http)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001152
1153 Returns an array containing all the request headers.
1154
1155 :param class_http http: The related http object.
1156 :returns: array of headers.
Pieter Baauw386a1272015-08-16 15:26:24 +02001157 :see: HTTP.res_get_headers()
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001158
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001159 This is the form of the returned array:
1160
1161.. code-block:: lua
1162
1163 HTTP:req_get_headers()['<header-name>'][<header-index>] = "<header-value>"
1164
1165 local hdr = HTTP:req_get_headers()
1166 hdr["host"][0] = "www.test.com"
1167 hdr["accept"][0] = "audio/basic q=1"
1168 hdr["accept"][1] = "audio/*, q=0.2"
1169 hdr["accept"][2] = "*/*, q=0.1"
1170..
1171
Pieter Baauw386a1272015-08-16 15:26:24 +02001172.. js:function:: HTTP.res_get_headers(http)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001173
1174 Returns an array containing all the response headers.
1175
1176 :param class_http http: The related http object.
1177 :returns: array of headers.
Pieter Baauw386a1272015-08-16 15:26:24 +02001178 :see: HTTP.req_get_headers()
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001179
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001180 This is the form of the returned array:
1181
1182.. code-block:: lua
1183
1184 HTTP:res_get_headers()['<header-name>'][<header-index>] = "<header-value>"
1185
1186 local hdr = HTTP:req_get_headers()
1187 hdr["host"][0] = "www.test.com"
1188 hdr["accept"][0] = "audio/basic q=1"
1189 hdr["accept"][1] = "audio/*, q=0.2"
1190 hdr["accept"][2] = "*.*, q=0.1"
1191..
1192
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001193.. js:function:: HTTP.req_add_header(http, name, value)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001194
1195 Appends an HTTP header field in the request whose name is
1196 specified in "name" and whose value is defined in "value".
1197
1198 :param class_http http: The related http object.
1199 :param string name: The header name.
1200 :param string value: The header value.
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001201 :see: HTTP.res_add_header()
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001202
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001203.. js:function:: HTTP.res_add_header(http, name, value)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001204
1205 appends an HTTP header field in the response whose name is
1206 specified in "name" and whose value is defined in "value".
1207
1208 :param class_http http: The related http object.
1209 :param string name: The header name.
1210 :param string value: The header value.
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001211 :see: HTTP.req_add_header()
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001212
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001213.. js:function:: HTTP.req_del_header(http, name)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001214
1215 Removes all HTTP header fields in the request whose name is
1216 specified in "name".
1217
1218 :param class_http http: The related http object.
1219 :param string name: The header name.
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001220 :see: HTTP.res_del_header()
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001221
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001222.. js:function:: HTTP.res_del_header(http, name)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001223
1224 Removes all HTTP header fields in the response whose name is
1225 specified in "name".
1226
1227 :param class_http http: The related http object.
1228 :param string name: The header name.
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001229 :see: HTTP.req_del_header()
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001230
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001231.. js:function:: HTTP.req_set_header(http, name, value)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001232
1233 This variable replace all occurence of all header "name", by only
1234 one containing the "value".
1235
1236 :param class_http http: The related http object.
1237 :param string name: The header name.
1238 :param string value: The header value.
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001239 :see: HTTP.res_set_header()
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001240
1241 This function does the same work as the folowwing code:
1242
1243.. code-block:: lua
1244
1245 function fcn(txn)
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001246 TXN.http:req_del_header("header")
1247 TXN.http:req_add_header("header", "value")
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001248 end
1249..
1250
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001251.. js:function:: HTTP.res_set_header(http, name, value)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001252
1253 This variable replace all occurence of all header "name", by only
1254 one containing the "value".
1255
1256 :param class_http http: The related http object.
1257 :param string name: The header name.
1258 :param string value: The header value.
Pieter Baauw386a1272015-08-16 15:26:24 +02001259 :see: HTTP.req_rep_header()
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001260
Pieter Baauw386a1272015-08-16 15:26:24 +02001261.. js:function:: HTTP.req_rep_header(http, name, regex, replace)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001262
1263 Matches the regular expression in all occurrences of header field "name"
1264 according to "regex", and replaces them with the "replace" argument. The
1265 replacement value can contain back references like \1, \2, ... This
1266 function works with the request.
1267
1268 :param class_http http: The related http object.
1269 :param string name: The header name.
1270 :param string regex: The match regular expression.
1271 :param string replace: The replacement value.
Pieter Baauw386a1272015-08-16 15:26:24 +02001272 :see: HTTP.res_rep_header()
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001273
Pieter Baauw386a1272015-08-16 15:26:24 +02001274.. js:function:: HTTP.res_rep_header(http, name, regex, string)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001275
1276 Matches the regular expression in all occurrences of header field "name"
1277 according to "regex", and replaces them with the "replace" argument. The
1278 replacement value can contain back references like \1, \2, ... This
1279 function works with the request.
1280
1281 :param class_http http: The related http object.
1282 :param string name: The header name.
1283 :param string regex: The match regular expression.
1284 :param string replace: The replacement value.
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001285 :see: HTTP.req_replace_header()
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001286
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001287.. js:function:: HTTP.req_set_method(http, method)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001288
1289 Rewrites the request method with the parameter "method".
1290
1291 :param class_http http: The related http object.
1292 :param string method: The new method.
1293
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001294.. js:function:: HTTP.req_set_path(http, path)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001295
1296 Rewrites the request path with the "path" parameter.
1297
1298 :param class_http http: The related http object.
1299 :param string path: The new path.
1300
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001301.. js:function:: HTTP.req_set_query(http, query)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001302
1303 Rewrites the request's query string which appears after the first question
1304 mark ("?") with the parameter "query".
1305
1306 :param class_http http: The related http object.
1307 :param string query: The new query.
1308
Thierry FOURNIER0d79cf62015-08-26 14:20:58 +02001309.. js:function:: HTTP.req_set_uri(http, uri)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001310
1311 Rewrites the request URI with the parameter "uri".
1312
1313 :param class_http http: The related http object.
1314 :param string uri: The new uri.
1315
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +02001316.. js:function:: HTTP.res_set_status(http, status)
1317
1318 Rewrites the response status code with the parameter "code". Note that the
1319 reason is automatically adapted to the new code.
1320
1321 :param class_http http: The related http object.
1322 :param integer status: The new response status code.
1323
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001324.. _txn_class:
1325
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001326TXN class
1327=========
1328
1329.. js:class:: TXN
1330
1331 The txn class contain all the functions relative to the http or tcp
1332 transaction (Note than a tcp stream is the same than a tcp transaction, but
1333 an HTTP transaction is not the same than a tcp stream).
1334
1335 The usage of this class permits to retrieve data from the requests, alter it
1336 and forward it.
1337
1338 All the functions provided by this class are available in the context
1339 **sample-fetches** and **actions**.
1340
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001341.. js:attribute:: TXN.c
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001342
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001343 :returns: An :ref:`converters_class`.
1344
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001345 This attribute contains a Converters class object.
1346
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001347.. js:attribute:: TXN.sc
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001348
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001349 :returns: An :ref:`converters_class`.
1350
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001351 This attribute contains a Converters class object. The functions of
1352 this object returns always a string.
1353
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001354.. js:attribute:: TXN.f
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001355
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001356 :returns: An :ref:`fetches_class`.
1357
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001358 This attribute contains a Fetches class object.
1359
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001360.. js:attribute:: TXN.sf
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001361
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001362 :returns: An :ref:`fetches_class`.
1363
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001364 This attribute contains a Fetches class object. The functions of
1365 this object returns always a string.
1366
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001367.. js:attribute:: TXN.req
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001368
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001369 :returns: An :ref:`channel_class`.
1370
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001371 This attribute contains a channel class object for the request buffer.
1372
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001373.. js:attribute:: TXN.res
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001374
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001375 :returns: An :ref:`channel_class`.
1376
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001377 This attribute contains a channel class object for the response buffer.
1378
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001379.. js:attribute:: TXN.http
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001380
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001381 :returns: An :ref:`http_class`.
1382
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001383 This attribute contains an HTTP class object. It is avalaible only if the
1384 proxy has the "mode http" enabled.
1385
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01001386.. js:function:: TXN.log(TXN, loglevel, msg)
1387
1388 This function sends a log. The log is sent, according with the HAProxy
1389 configuration file, on the default syslog server if it is configured and on
1390 the stderr if it is allowed.
1391
1392 :param class_txn txn: The class txn object containing the data.
1393 :param integer loglevel: Is the log level asociated with the message. It is a
1394 number between 0 and 7.
1395 :param string msg: The log content.
1396 :see: core.emerg, core.alert, core.crit, core.err, core.warning, core.notice,
1397 core.info, core.debug (log level definitions)
1398 :see: TXN.deflog
1399 :see: TXN.Debug
1400 :see: TXN.Info
1401 :see: TXN.Warning
1402 :see: TXN.Alert
1403
1404.. js:function:: TXN.deflog(TXN, msg)
1405
1406 Sends a log line with the default loglevel for the proxy ssociated with the
1407 transaction.
1408
1409 :param class_txn txn: The class txn object containing the data.
1410 :param string msg: The log content.
1411 :see: TXN.log
1412
1413.. js:function:: TXN.Debug(txn, msg)
1414
1415 :param class_txn txn: The class txn object containing the data.
1416 :param string msg: The log content.
1417 :see: TXN.log
1418
1419 Does the same job than:
1420
1421.. code-block:: lua
1422
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001423 function Debug(txn, msg)
1424 TXN.log(txn, core.debug, msg)
1425 end
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01001426..
1427
1428.. js:function:: TXN.Info(txn, msg)
1429
1430 :param class_txn txn: The class txn object containing the data.
1431 :param string msg: The log content.
1432 :see: TXN.log
1433
1434.. code-block:: lua
1435
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001436 function Debug(txn, msg)
1437 TXN.log(txn, core.info, msg)
1438 end
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01001439..
1440
1441.. js:function:: TXN.Warning(txn, msg)
1442
1443 :param class_txn txn: The class txn object containing the data.
1444 :param string msg: The log content.
1445 :see: TXN.log
1446
1447.. code-block:: lua
1448
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001449 function Debug(txn, msg)
1450 TXN.log(txn, core.warning, msg)
1451 end
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01001452..
1453
1454.. js:function:: TXN.Alert(txn, msg)
1455
1456 :param class_txn txn: The class txn object containing the data.
1457 :param string msg: The log content.
1458 :see: TXN.log
1459
1460.. code-block:: lua
1461
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001462 function Debug(txn, msg)
1463 TXN.log(txn, core.alert, msg)
1464 end
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01001465..
1466
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001467.. js:function:: TXN.get_priv(txn)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001468
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001469 Return Lua data stored in the current transaction (with the `TXN.set_priv()`)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001470 function. If no data are stored, it returns a nil value.
1471
1472 :param class_txn txn: The class txn object containing the data.
1473 :returns: the opaque data previsously stored, or nil if nothing is
1474 avalaible.
1475
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001476.. js:function:: TXN.set_priv(txn, data)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001477
1478 Store any data in the current HAProxy transaction. This action replace the
1479 old stored data.
1480
1481 :param class_txn txn: The class txn object containing the data.
1482 :param opaque data: The data which is stored in the transaction.
1483
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +02001484.. js:function:: TXN.set_var(TXN, var, value)
1485
David Carlier61fdf8b2015-10-02 11:59:38 +01001486 Converts a Lua type in a HAProxy type and store it in a variable <var>.
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +02001487
1488 :param class_txn txn: The class txn object containing the data.
1489 :param string var: The variable name according with the HAProxy variable syntax.
Christopher Faulet85d79c92016-11-09 16:54:56 +01001490
1491.. js:function:: TXN.unset_var(TXN, var)
1492
1493 Unset the variable <var>.
1494
1495 :param class_txn txn: The class txn object containing the data.
1496 :param string var: The variable name according with the HAProxy variable syntax.
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +02001497
1498.. js:function:: TXN.get_var(TXN, var)
1499
1500 Returns data stored in the variable <var> converter in Lua type.
1501
1502 :param class_txn txn: The class txn object containing the data.
1503 :param string var: The variable name according with the HAProxy variable syntax.
1504
Willy Tarreaubc183a62015-08-28 10:39:11 +02001505.. js:function:: TXN.done(txn)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001506
Willy Tarreaubc183a62015-08-28 10:39:11 +02001507 This function terminates processing of the transaction and the associated
1508 session. It can be used when a critical error is detected or to terminate
1509 processing after some data have been returned to the client (eg: a redirect).
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001510
Thierry FOURNIERab00df62016-07-14 11:42:37 +02001511 *Warning*: It not make sense to call this function from sample-fetches. In
1512 this case the behaviour of this one is the same than core.done(): it quit
1513 the Lua execution. The transaction is really aborted only from an action
1514 registered function.
1515
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001516 :param class_txn txn: The class txn object containing the data.
1517
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001518.. js:function:: TXN.set_loglevel(txn, loglevel)
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01001519
1520 Is used to change the log level of the current request. The "loglevel" must
1521 be an integer between 0 and 7.
1522
1523 :param class_txn txn: The class txn object containing the data.
1524 :param integer loglevel: The required log level. This variable can be one of
1525 :see: core.<loglevel>
1526
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001527.. js:function:: TXN.set_tos(txn, tos)
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01001528
1529 Is used to set the TOS or DSCP field value of packets sent to the client to
1530 the value passed in "tos" on platforms which support this.
1531
1532 :param class_txn txn: The class txn object containing the data.
1533 :param integer tos: The new TOS os DSCP.
1534
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001535.. js:function:: TXN.set_mark(txn, mark)
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01001536
1537 Is used to set the Netfilter MARK on all packets sent to the client to the
1538 value passed in "mark" on platforms which support it.
1539
1540 :param class_txn txn: The class txn object containing the data.
1541 :param integer mark: The mark value.
1542
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001543.. _socket_class:
1544
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001545Socket class
1546============
1547
1548.. js:class:: Socket
1549
1550 This class must be compatible with the Lua Socket class. Only the 'client'
1551 functions are available. See the Lua Socket documentation:
1552
1553 `http://w3.impa.br/~diego/software/luasocket/tcp.html
1554 <http://w3.impa.br/~diego/software/luasocket/tcp.html>`_
1555
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001556.. js:function:: Socket.close(socket)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001557
1558 Closes a TCP object. The internal socket used by the object is closed and the
1559 local address to which the object was bound is made available to other
1560 applications. No further operations (except for further calls to the close
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001561 method) are allowed on a closed Socket.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001562
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001563 :param class_socket socket: Is the manipulated Socket.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001564
1565 Note: It is important to close all used sockets once they are not needed,
1566 since, in many systems, each socket uses a file descriptor, which are limited
1567 system resources. Garbage-collected objects are automatically closed before
1568 destruction, though.
1569
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02001570.. js:function:: Socket.connect(socket, address[, port])
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001571
1572 Attempts to connect a socket object to a remote host.
1573
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001574
1575 In case of error, the method returns nil followed by a string describing the
1576 error. In case of success, the method returns 1.
1577
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001578 :param class_socket socket: Is the manipulated Socket.
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02001579 :param string address: can be an IP address or a host name. See below for more
1580 information.
1581 :param integer port: must be an integer number in the range [1..64K].
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001582 :returns: 1 or nil.
1583
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02001584 an address field extension permits to use the connect() function to connect to
1585 other stream than TCP. The syntax containing a simpleipv4 or ipv6 address is
1586 the basically expected format. This format requires the port.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001587
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02001588 Other format accepted are a socket path like "/socket/path", it permits to
1589 connect to a socket. abstract namespaces are supported with the prefix
1590 "abns@", and finaly a filedescriotr can be passed with the prefix "fd@".
1591 The prefix "ipv4@", "ipv6@" and "unix@" are also supported. The port can be
1592 passed int the string. The syntax "127.0.0.1:1234" is valid. in this case, the
1593 parameter *port* is ignored.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001594
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001595.. js:function:: Socket.connect_ssl(socket, address, port)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001596
1597 Same behavior than the function socket:connect, but uses SSL.
1598
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001599 :param class_socket socket: Is the manipulated Socket.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001600 :returns: 1 or nil.
1601
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001602.. js:function:: Socket.getpeername(socket)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001603
1604 Returns information about the remote side of a connected client object.
1605
1606 Returns a string with the IP address of the peer, followed by the port number
1607 that peer is using for the connection. In case of error, the method returns
1608 nil.
1609
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001610 :param class_socket socket: Is the manipulated Socket.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001611 :returns: a string containing the server information.
1612
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001613.. js:function:: Socket.getsockname(socket)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001614
1615 Returns the local address information associated to the object.
1616
1617 The method returns a string with local IP address and a number with the port.
1618 In case of error, the method returns nil.
1619
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001620 :param class_socket socket: Is the manipulated Socket.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001621 :returns: a string containing the client information.
1622
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001623.. js:function:: Socket.receive(socket, [pattern [, prefix]])
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001624
1625 Reads data from a client object, according to the specified read pattern.
1626 Patterns follow the Lua file I/O format, and the difference in performance
1627 between all patterns is negligible.
1628
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001629 :param class_socket socket: Is the manipulated Socket.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001630 :param string|integer pattern: Describe what is required (see below).
1631 :param string prefix: A string which will be prefix the returned data.
1632 :returns: a string containing the required data or nil.
1633
1634 Pattern can be any of the following:
1635
1636 * **`*a`**: reads from the socket until the connection is closed. No
1637 end-of-line translation is performed;
1638
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001639 * **`*l`**: reads a line of text from the Socket. The line is terminated by a
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001640 LF character (ASCII 10), optionally preceded by a CR character
1641 (ASCII 13). The CR and LF characters are not included in the
1642 returned line. In fact, all CR characters are ignored by the
1643 pattern. This is the default pattern.
1644
1645 * **number**: causes the method to read a specified number of bytes from the
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001646 Socket. Prefix is an optional string to be concatenated to the
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001647 beginning of any received data before return.
1648
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02001649 * **empty**: If the pattern is left empty, the default option is `*l`.
1650
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001651 If successful, the method returns the received pattern. In case of error, the
1652 method returns nil followed by an error message which can be the string
1653 'closed' in case the connection was closed before the transmission was
1654 completed or the string 'timeout' in case there was a timeout during the
1655 operation. Also, after the error message, the function returns the partial
1656 result of the transmission.
1657
1658 Important note: This function was changed severely. It used to support
1659 multiple patterns (but I have never seen this feature used) and now it
1660 doesn't anymore. Partial results used to be returned in the same way as
1661 successful results. This last feature violated the idea that all functions
1662 should return nil on error. Thus it was changed too.
1663
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001664.. js:function:: Socket.send(socket, data [, start [, end ]])
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001665
1666 Sends data through client object.
1667
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001668 :param class_socket socket: Is the manipulated Socket.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001669 :param string data: The data that will be sent.
1670 :param integer start: The start position in the buffer of the data which will
1671 be sent.
1672 :param integer end: The end position in the buffer of the data which will
1673 be sent.
1674 :returns: see below.
1675
1676 Data is the string to be sent. The optional arguments i and j work exactly
1677 like the standard string.sub Lua function to allow the selection of a
1678 substring to be sent.
1679
1680 If successful, the method returns the index of the last byte within [start,
1681 end] that has been sent. Notice that, if start is 1 or absent, this is
1682 effectively the total number of bytes sent. In case of error, the method
1683 returns nil, followed by an error message, followed by the index of the last
1684 byte within [start, end] that has been sent. You might want to try again from
1685 the byte following that. The error message can be 'closed' in case the
1686 connection was closed before the transmission was completed or the string
1687 'timeout' in case there was a timeout during the operation.
1688
1689 Note: Output is not buffered. For small strings, it is always better to
1690 concatenate them in Lua (with the '..' operator) and send the result in one
1691 call instead of calling the method several times.
1692
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001693.. js:function:: Socket.setoption(socket, option [, value])
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001694
1695 Just implemented for compatibility, this cal does nothing.
1696
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001697.. js:function:: Socket.settimeout(socket, value [, mode])
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001698
1699 Changes the timeout values for the object. All I/O operations are blocking.
1700 That is, any call to the methods send, receive, and accept will block
1701 indefinitely, until the operation completes. The settimeout method defines a
1702 limit on the amount of time the I/O methods can block. When a timeout time
1703 has elapsed, the affected methods give up and fail with an error code.
1704
1705 The amount of time to wait is specified as the value parameter, in seconds.
1706
1707 The timeout modes are bot implemented, the only settable timeout is the
1708 inactivity time waiting for complete the internal buffer send or waiting for
1709 receive data.
1710
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001711 :param class_socket socket: Is the manipulated Socket.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001712 :param integer value: The timeout value.
1713
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001714.. _map_class:
1715
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001716Map class
1717=========
1718
1719.. js:class:: Map
1720
1721 This class permits to do some lookup in HAProxy maps. The declared maps can
1722 be modified during the runtime throught the HAProxy management socket.
1723
1724.. code-block:: lua
1725
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001726 default = "usa"
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001727
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001728 -- Create and load map
1729 geo = Map.new("geo.map", Map.ip);
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001730
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001731 -- Create new fetch that returns the user country
1732 core.register_fetches("country", function(txn)
1733 local src;
1734 local loc;
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001735
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001736 src = txn.f:fhdr("x-forwarded-for");
1737 if (src == nil) then
1738 src = txn.f:src()
1739 if (src == nil) then
1740 return default;
1741 end
1742 end
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001743
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001744 -- Perform lookup
1745 loc = geo:lookup(src);
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001746
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001747 if (loc == nil) then
1748 return default;
1749 end
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001750
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001751 return loc;
1752 end);
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001753
1754.. js:attribute:: Map.int
1755
1756 See the HAProxy configuration.txt file, chapter "Using ACLs and fetching
1757 samples" ans subchapter "ACL basics" to understand this pattern matching
1758 method.
1759
1760.. js:attribute:: Map.ip
1761
1762 See the HAProxy configuration.txt file, chapter "Using ACLs and fetching
1763 samples" ans subchapter "ACL basics" to understand this pattern matching
1764 method.
1765
1766.. js:attribute:: Map.str
1767
1768 See the HAProxy configuration.txt file, chapter "Using ACLs and fetching
1769 samples" ans subchapter "ACL basics" to understand this pattern matching
1770 method.
1771
1772.. js:attribute:: Map.beg
1773
1774 See the HAProxy configuration.txt file, chapter "Using ACLs and fetching
1775 samples" ans subchapter "ACL basics" to understand this pattern matching
1776 method.
1777
1778.. js:attribute:: Map.sub
1779
1780 See the HAProxy configuration.txt file, chapter "Using ACLs and fetching
1781 samples" ans subchapter "ACL basics" to understand this pattern matching
1782 method.
1783
1784.. js:attribute:: Map.dir
1785
1786 See the HAProxy configuration.txt file, chapter "Using ACLs and fetching
1787 samples" ans subchapter "ACL basics" to understand this pattern matching
1788 method.
1789
1790.. js:attribute:: Map.dom
1791
1792 See the HAProxy configuration.txt file, chapter "Using ACLs and fetching
1793 samples" ans subchapter "ACL basics" to understand this pattern matching
1794 method.
1795
1796.. js:attribute:: Map.end
1797
1798 See the HAProxy configuration.txt file, chapter "Using ACLs and fetching
1799 samples" ans subchapter "ACL basics" to understand this pattern matching
1800 method.
1801
1802.. js:attribute:: Map.reg
1803
1804 See the HAProxy configuration.txt file, chapter "Using ACLs and fetching
1805 samples" ans subchapter "ACL basics" to understand this pattern matching
1806 method.
1807
1808
1809.. js:function:: Map.new(file, method)
1810
1811 Creates and load a map.
1812
1813 :param string file: Is the file containing the map.
1814 :param integer method: Is the map pattern matching method. See the attributes
1815 of the Map class.
1816 :returns: a class Map object.
1817 :see: The Map attributes.
1818
1819.. js:function:: Map.lookup(map, str)
1820
1821 Perform a lookup in a map.
1822
1823 :param class_map map: Is the class Map object.
1824 :param string str: Is the string used as key.
1825 :returns: a string containing the result or nil if no match.
1826
1827.. js:function:: Map.slookup(map, str)
1828
1829 Perform a lookup in a map.
1830
1831 :param class_map map: Is the class Map object.
1832 :param string str: Is the string used as key.
1833 :returns: a string containing the result or empty string if no match.
1834
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001835.. _applethttp_class:
1836
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02001837AppletHTTP class
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001838================
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02001839
1840.. js:class:: AppletHTTP
1841
1842 This class is used with applets that requires the 'http' mode. The http applet
1843 can be registered with the *core.register_service()* function. They are used
1844 for processing an http request like a server in back of HAProxy.
1845
1846 This is an hello world sample code:
1847
1848.. code-block:: lua
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001849
Pieter Baauw4d7f7662015-11-08 16:38:08 +01001850 core.register_service("hello-world", "http", function(applet)
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02001851 local response = "Hello World !"
1852 applet:set_status(200)
Pieter Baauw2dcb9bc2015-10-01 22:47:12 +02001853 applet:add_header("content-length", string.len(response))
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02001854 applet:add_header("content-type", "text/plain")
Pieter Baauw2dcb9bc2015-10-01 22:47:12 +02001855 applet:start_response()
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02001856 applet:send(response)
1857 end)
1858
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001859.. js:attribute:: AppletHTTP.c
1860
1861 :returns: A :ref:`converters_class`
1862
1863 This attribute contains a Converters class object.
1864
1865.. js:attribute:: AppletHTTP.sc
1866
1867 :returns: A :ref:`converters_class`
1868
1869 This attribute contains a Converters class object. The
1870 functions of this object returns always a string.
1871
1872.. js:attribute:: AppletHTTP.f
1873
1874 :returns: A :ref:`fetches_class`
1875
1876 This attribute contains a Fetches class object. Note that the
1877 applet execution place cannot access to a valid HAProxy core HTTP
1878 transaction, so some sample fecthes related to the HTTP dependant
1879 values (hdr, path, ...) are not available.
1880
1881.. js:attribute:: AppletHTTP.sf
1882
1883 :returns: A :ref:`fetches_class`
1884
1885 This attribute contains a Fetches class object. The functions of
1886 this object returns always a string. Note that the applet
1887 execution place cannot access to a valid HAProxy core HTTP
1888 transaction, so some sample fecthes related to the HTTP dependant
1889 values (hdr, path, ...) are not available.
1890
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01001891.. js:attribute:: AppletHTTP.method
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001892
1893 :returns: string
1894
1895 The attribute method returns a string containing the HTTP
1896 method.
1897
1898.. js:attribute:: AppletHTTP.version
1899
1900 :returns: string
1901
1902 The attribute version, returns a string containing the HTTP
1903 request version.
1904
1905.. js:attribute:: AppletHTTP.path
1906
1907 :returns: string
1908
1909 The attribute path returns a string containing the HTTP
1910 request path.
1911
1912.. js:attribute:: AppletHTTP.qs
1913
1914 :returns: string
1915
1916 The attribute qs returns a string containing the HTTP
1917 request query string.
1918
1919.. js:attribute:: AppletHTTP.length
1920
1921 :returns: integer
1922
1923 The attribute length returns an integer containing the HTTP
1924 body length.
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02001925
Thierry FOURNIER841475e2015-12-11 17:10:09 +01001926.. js:attribute:: AppletHTTP.headers
1927
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001928 :returns: array
1929
1930 The attribute headers returns an array containing the HTTP
1931 headers. The header names are always in lower case. As the header name can be
1932 encountered more than once in each request, the value is indexed with 0 as
1933 first index value. The array have this form:
1934
1935.. code-block:: lua
1936
1937 AppletHTTP.headers['<header-name>'][<header-index>] = "<header-value>"
1938
1939 AppletHTTP.headers["host"][0] = "www.test.com"
1940 AppletHTTP.headers["accept"][0] = "audio/basic q=1"
1941 AppletHTTP.headers["accept"][1] = "audio/*, q=0.2"
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01001942 AppletHTTP.headers["accept"][2] = "*/*, q=0.1"
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001943..
1944
1945.. js:attribute:: AppletHTTP.headers
1946
Thierry FOURNIER841475e2015-12-11 17:10:09 +01001947 Contains an array containing all the request headers.
1948
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01001949.. js:function:: AppletHTTP.set_status(applet, code)
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02001950
1951 This function sets the HTTP status code for the response. The allowed code are
1952 from 100 to 599.
1953
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01001954 :param class_AppletHTTP applet: An :ref:`applethttp_class`
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02001955 :param integer code: the status code returned to the client.
1956
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01001957.. js:function:: AppletHTTP.add_header(applet, name, value)
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02001958
1959 This function add an header in the response. Duplicated headers are not
1960 collapsed. The special header *content-length* is used to determinate the
1961 response length. If it not exists, a *transfer-encoding: chunked* is set, and
1962 all the write from the funcion *AppletHTTP:send()* become a chunk.
1963
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01001964 :param class_AppletHTTP applet: An :ref:`applethttp_class`
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02001965 :param string name: the header name
1966 :param string value: the header value
1967
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01001968.. js:function:: AppletHTTP.start_response(applet)
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02001969
1970 This function indicates to the HTTP engine that it can process and send the
1971 response headers. After this called we cannot add headers to the response; We
1972 cannot use the *AppletHTTP:send()* function if the
1973 *AppletHTTP:start_response()* is not called.
1974
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01001975 :param class_AppletHTTP applet: An :ref:`applethttp_class`
1976
1977.. js:function:: AppletHTTP.getline(applet)
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02001978
1979 This function returns a string containing one line from the http body. If the
1980 data returned doesn't contains a final '\\n' its assumed than its the last
1981 available data before the end of stream.
1982
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01001983 :param class_AppletHTTP applet: An :ref:`applethttp_class`
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02001984 :returns: a string. The string can be empty if we reach the end of the stream.
1985
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01001986.. js:function:: AppletHTTP.receive(applet, [size])
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02001987
1988 Reads data from the HTTP body, according to the specified read *size*. If the
1989 *size* is missing, the function tries to read all the content of the stream
1990 until the end. If the *size* is bigger than the http body, it returns the
1991 amount of data avalaible.
1992
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01001993 :param class_AppletHTTP applet: An :ref:`applethttp_class`
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02001994 :param integer size: the required read size.
1995 :returns: always return a string,the string can be empty is the connexion is
1996 closed.
1997
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01001998.. js:function:: AppletHTTP.send(applet, msg)
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02001999
2000 Send the message *msg* on the http request body.
2001
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01002002 :param class_AppletHTTP applet: An :ref:`applethttp_class`
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002003 :param string msg: the message to send.
2004
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01002005.. js:function:: AppletHTTP.get_priv(applet)
2006
2007 Return Lua data stored in the current transaction (with the
2008 `AppletHTTP.set_priv()`) function. If no data are stored, it returns a nil
2009 value.
2010
2011 :param class_AppletHTTP applet: An :ref:`applethttp_class`
2012 :returns: the opaque data previsously stored, or nil if nothing is
2013 avalaible.
2014
2015.. js:function:: AppletHTTP.set_priv(applet, data)
2016
2017 Store any data in the current HAProxy transaction. This action replace the
2018 old stored data.
2019
2020 :param class_AppletHTTP applet: An :ref:`applethttp_class`
2021 :param opaque data: The data which is stored in the transaction.
2022
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002023.. _applettcp_class:
2024
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002025AppletTCP class
2026===============
2027
2028.. js:class:: AppletTCP
2029
2030 This class is used with applets that requires the 'tcp' mode. The tcp applet
2031 can be registered with the *core.register_service()* function. They are used
2032 for processing a tcp stream like a server in back of HAProxy.
2033
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002034.. js:attribute:: AppletTCP.c
2035
2036 :returns: A :ref:`converters_class`
2037
2038 This attribute contains a Converters class object.
2039
2040.. js:attribute:: AppletTCP.sc
2041
2042 :returns: A :ref:`converters_class`
2043
2044 This attribute contains a Converters class object. The
2045 functions of this object returns always a string.
2046
2047.. js:attribute:: AppletTCP.f
2048
2049 :returns: A :ref:`fetches_class`
2050
2051 This attribute contains a Fetches class object.
2052
2053.. js:attribute:: AppletTCP.sf
2054
2055 :returns: A :ref:`fetches_class`
2056
2057 This attribute contains a Fetches class object.
2058
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01002059.. js:function:: AppletTCP.getline(applet)
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002060
2061 This function returns a string containing one line from the stream. If the
2062 data returned doesn't contains a final '\\n' its assumed than its the last
2063 available data before the end of stream.
2064
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01002065 :param class_AppletTCP applet: An :ref:`applettcp_class`
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002066 :returns: a string. The string can be empty if we reach the end of the stream.
2067
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01002068.. js:function:: AppletTCP.receive(applet, [size])
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002069
2070 Reads data from the TCP stream, according to the specified read *size*. If the
2071 *size* is missing, the function tries to read all the content of the stream
2072 until the end.
2073
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01002074 :param class_AppletTCP applet: An :ref:`applettcp_class`
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002075 :param integer size: the required read size.
2076 :returns: always return a string,the string can be empty is the connexion is
2077 closed.
2078
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01002079.. js:function:: AppletTCP.send(appletmsg)
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002080
2081 Send the message on the stream.
2082
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01002083 :param class_AppletTCP applet: An :ref:`applettcp_class`
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002084 :param string msg: the message to send.
2085
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01002086.. js:function:: AppletTCP.get_priv(applet)
2087
2088 Return Lua data stored in the current transaction (with the
2089 `AppletTCP.set_priv()`) function. If no data are stored, it returns a nil
2090 value.
2091
2092 :param class_AppletTCP applet: An :ref:`applettcp_class`
2093 :returns: the opaque data previsously stored, or nil if nothing is
2094 avalaible.
2095
2096.. js:function:: AppletTCP.set_priv(applet, data)
2097
2098 Store any data in the current HAProxy transaction. This action replace the
2099 old stored data.
2100
2101 :param class_AppletTCP applet: An :ref:`applettcp_class`
2102 :param opaque data: The data which is stored in the transaction.
2103
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002104External Lua libraries
2105======================
2106
2107A lot of useful lua libraries can be found here:
2108
2109* `https://lua-toolbox.com/ <https://lua-toolbox.com/>`_
2110
2111Redis acces:
2112
2113* `https://github.com/nrk/redis-lua <https://github.com/nrk/redis-lua>`_
2114
2115This is an example about the usage of the Redis library with HAProxy. Note that
2116each call of any function of this library can throw an error if the socket
2117connection fails.
2118
2119.. code-block:: lua
2120
2121 -- load the redis library
2122 local redis = require("redis");
2123
2124 function do_something(txn)
2125
2126 -- create and connect new tcp socket
2127 local tcp = core.tcp();
2128 tcp:settimeout(1);
2129 tcp:connect("127.0.0.1", 6379);
2130
2131 -- use the redis library with this new socket
2132 local client = redis.connect({socket=tcp});
2133 client:ping();
2134
2135 end
2136
2137OpenSSL:
2138
2139* `http://mkottman.github.io/luacrypto/index.html
2140 <http://mkottman.github.io/luacrypto/index.html>`_
2141
2142* `https://github.com/brunoos/luasec/wiki
2143 <https://github.com/brunoos/luasec/wiki>`_
Thierry FOURNIER2e4893c2015-03-18 13:37:27 +01002144