blob: 22c053884a4de6cb8f8f3a21a501c2e3b2dd3cdd [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 FOURNIER / OZON.IO8a1027a2016-11-24 20:48:38 +0100648.. js:function:: core.tokenize(str, separators [, noblank])
649
650 **context**: body, init, task, action, sample-fetch, converter
651
652 This function is useful for tokenizing an entry, or splitting some messages.
653 :param string str: The string which will be split.
654 :param string separators: A string containing a list of separators.
655 :param boolean noblank: Ignore empty entries.
656 :returns: an array of string.
657
658 For example:
659
660.. code-block:: lua
661
662 local array = core.tokenize("This function is useful, for tokenizing an entry.", "., ", true)
663 print_r(array)
664..
665
666 Returns this array:
667
668.. code-block:: text
669
670 (table) table: 0x21c01e0 [
671 1: (string) "This"
672 2: (string) "function"
673 3: (string) "is"
674 4: (string) "useful"
675 5: (string) "for"
676 6: (string) "tokenizing"
677 7: (string) "an"
678 8: (string) "entry"
679 ]
680..
681
Thierry Fournierf61aa632016-02-19 20:56:00 +0100682.. _proxy_class:
683
684Proxy class
685============
686
687.. js:class:: Proxy
688
689 This class provides a way for manipulating proxy and retrieving information
690 like statistics.
691
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +0100692.. js:attribute:: Proxy.servers
693
694 Contain an array with the attached servers. Each server entry is an object of
695 type :ref:`server_class`.
696
Thierry Fournierff480422016-02-25 08:36:46 +0100697.. js:attribute:: Proxy.listeners
698
699 Contain an array with the attached listeners. Each listeners entry is an
700 object of type :ref:`listener_class`.
701
Thierry Fournierf61aa632016-02-19 20:56:00 +0100702.. js:function:: Proxy.pause(px)
703
704 Pause the proxy. See the management socket documentation for more information.
705
706 :param class_proxy px: A :ref:`proxy_class` which indicates the manipulated
707 proxy.
708
709.. js:function:: Proxy.resume(px)
710
711 Resume the proxy. See the management socket documentation for more
712 information.
713
714 :param class_proxy px: A :ref:`proxy_class` which indicates the manipulated
715 proxy.
716
717.. js:function:: Proxy.stop(px)
718
719 Stop the proxy. See the management socket documentation for more information.
720
721 :param class_proxy px: A :ref:`proxy_class` which indicates the manipulated
722 proxy.
723
724.. js:function:: Proxy.shut_bcksess(px)
725
726 Kill the session attached to a backup server. See the management socket
727 documentation for more information.
728
729 :param class_proxy px: A :ref:`proxy_class` which indicates the manipulated
730 proxy.
731
732.. js:function:: Proxy.get_cap(px)
733
734 Returns a string describing the capabilities of the proxy.
735
736 :param class_proxy px: A :ref:`proxy_class` which indicates the manipulated
737 proxy.
738 :returns: a string "frontend", "backend", "proxy" or "ruleset".
739
740.. js:function:: Proxy.get_mode(px)
741
742 Returns a string describing the mode of the current proxy.
743
744 :param class_proxy px: A :ref:`proxy_class` which indicates the manipulated
745 proxy.
746 :returns: a string "tcp", "http", "health" or "unknown"
747
748.. js:function:: Proxy.get_stats(px)
749
750 Returns an array containg the proxy statistics. The statistics returned are
751 not the same if the proxy is frontend or a backend.
752
753 :param class_proxy px: A :ref:`proxy_class` which indicates the manipulated
754 proxy.
755 :returns: a key/value array containing stats
756
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +0100757.. _server_class:
758
759Server class
760============
761
762.. js:function:: Server.is_draining(sv)
763
764 Return true if the server is currently draining stiky connections.
765
766 :param class_server sv: A :ref:`server_class` which indicates the manipulated
767 server.
768 :returns: a boolean
769
770.. js:function:: Server.set_weight(sv, weight)
771
772 Dynamically change the weight of the serveur. See the management socket
773 documentation for more information about the format of the string.
774
775 :param class_server sv: A :ref:`server_class` which indicates the manipulated
776 server.
777 :param string weight: A string describing the server weight.
778
779.. js:function:: Server.get_weight(sv)
780
781 This function returns an integer representing the serveur weight.
782
783 :param class_server sv: A :ref:`server_class` which indicates the manipulated
784 server.
785 :returns: an integer.
786
787.. js:function:: Server.set_addr(sv, addr)
788
789 Dynamically change the address of the serveur. See the management socket
790 documentation for more information about the format of the string.
791
792 :param class_server sv: A :ref:`server_class` which indicates the manipulated
793 server.
794 :param string weight: A string describing the server address.
795
796.. js:function:: Server.get_addr(sv)
797
798 Returns a string describing the address of the serveur.
799
800 :param class_server sv: A :ref:`server_class` which indicates the manipulated
801 server.
802 :returns: A string
803
804.. js:function:: Server.get_stats(sv)
805
806 Returns server statistics.
807
808 :param class_server sv: A :ref:`server_class` which indicates the manipulated
809 server.
810 :returns: a key/value array containing stats
811
812.. js:function:: Server.shut_sess(sv)
813
814 Shutdown all the sessions attached to the server. See the management socket
815 documentation for more information about this function.
816
817 :param class_server sv: A :ref:`server_class` which indicates the manipulated
818 server.
819
820.. js:function:: Server.set_drain(sv)
821
822 Drain sticky sessions. See the management socket documentation for more
823 information about this function.
824
825 :param class_server sv: A :ref:`server_class` which indicates the manipulated
826 server.
827
828.. js:function:: Server.set_maint(sv)
829
830 Set maintenance mode. See the management socket documentation for more
831 information about this function.
832
833 :param class_server sv: A :ref:`server_class` which indicates the manipulated
834 server.
835
836.. js:function:: Server.set_ready(sv)
837
838 Set normal mode. See the management socket documentation for more information
839 about this function.
840
841 :param class_server sv: A :ref:`server_class` which indicates the manipulated
842 server.
843
844.. js:function:: Server.check_enable(sv)
845
846 Enable health checks. See the management socket documentation for more
847 information about this function.
848
849 :param class_server sv: A :ref:`server_class` which indicates the manipulated
850 server.
851
852.. js:function:: Server.check_disable(sv)
853
854 Disable health checks. See the management socket documentation for more
855 information about this function.
856
857 :param class_server sv: A :ref:`server_class` which indicates the manipulated
858 server.
859
860.. js:function:: Server.check_force_up(sv)
861
862 Force health-check up. See the management socket documentation for more
863 information about this function.
864
865 :param class_server sv: A :ref:`server_class` which indicates the manipulated
866 server.
867
868.. js:function:: Server.check_force_nolb(sv)
869
870 Force health-check nolb mode. See the management socket documentation for more
871 information about this function.
872
873 :param class_server sv: A :ref:`server_class` which indicates the manipulated
874 server.
875
876.. js:function:: Server.check_force_down(sv)
877
878 Force health-check down. See the management socket documentation for more
879 information about this function.
880
881 :param class_server sv: A :ref:`server_class` which indicates the manipulated
882 server.
883
884.. js:function:: Server.agent_enable(sv)
885
886 Enable agent check. See the management socket documentation for more
887 information about this function.
888
889 :param class_server sv: A :ref:`server_class` which indicates the manipulated
890 server.
891
892.. js:function:: Server.agent_disable(sv)
893
894 Disable agent check. See the management socket documentation for more
895 information about this function.
896
897 :param class_server sv: A :ref:`server_class` which indicates the manipulated
898 server.
899
900.. js:function:: Server.agent_force_up(sv)
901
902 Force agent check up. See the management socket documentation for more
903 information about this function.
904
905 :param class_server sv: A :ref:`server_class` which indicates the manipulated
906 server.
907
908.. js:function:: Server.agent_force_down(sv)
909
910 Force agent check down. See the management socket documentation for more
911 information about this function.
912
913 :param class_server sv: A :ref:`server_class` which indicates the manipulated
914 server.
915
Thierry Fournierff480422016-02-25 08:36:46 +0100916.. _listener_class:
917
918Listener class
919==============
920
921.. js:function:: Listener.get_stats(ls)
922
923 Returns server statistics.
924
925 :param class_listener ls: A :ref:`listener_class` which indicates the
926 manipulated listener.
927 :returns: a key/value array containing stats
928
Thierry Fournier1de16592016-01-27 09:49:07 +0100929.. _concat_class:
930
931Concat class
932============
933
934.. js:class:: Concat
935
936 This class provides a fast way for string concatenation. The way using native
937 Lua concatenation like the code below is slow for some reasons.
938
939.. code-block:: lua
940
941 str = "string1"
942 str = str .. ", string2"
943 str = str .. ", string3"
944..
945
946 For each concatenation, Lua:
947 * allocate memory for the result,
948 * catenate the two string copying the strings in the new memory bloc,
949 * free the old memory block containing the string whoch is no longer used.
950 This process does many memory move, allocation and free. In addition, the
951 memory is not really freed, it is just mark mark as unsused and wait for the
952 garbage collector.
953
954 The Concat class provide an alternative way for catenating strings. It uses
955 the internal Lua mechanism (it does not allocate memory), but it doesn't copy
956 the data more than once.
957
958 On my computer, the following loops spends 0.2s for the Concat method and
959 18.5s for the pure Lua implementation. So, the Concat class is about 1000x
960 faster than the embedded solution.
961
962.. code-block:: lua
963
964 for j = 1, 100 do
965 c = core.concat()
966 for i = 1, 20000 do
967 c:add("#####")
968 end
969 end
970..
971
972.. code-block:: lua
973
974 for j = 1, 100 do
975 c = ""
976 for i = 1, 20000 do
977 c = c .. "#####"
978 end
979 end
980..
981
982.. js:function:: Concat.add(concat, string)
983
984 This function adds a string to the current concatenated string.
985
986 :param class_concat concat: A :ref:`concat_class` which contains the currently
987 builded string.
988 :param string string: A new string to concatenate to the current builded
989 string.
990
991.. js:function:: Concat.dump(concat)
992
993 This function returns the concanated string.
994
995 :param class_concat concat: A :ref:`concat_class` which contains the currently
996 builded string.
997 :returns: the concatenated string
998
Thierry FOURNIERdc595002015-12-21 11:13:52 +0100999.. _fetches_class:
1000
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001001Fetches class
1002=============
1003
1004.. js:class:: Fetches
1005
1006 This class contains a lot of internal HAProxy sample fetches. See the
Thierry FOURNIER2e4893c2015-03-18 13:37:27 +01001007 HAProxy "configuration.txt" documentation for more information about her
1008 usage. they are the chapters 7.3.2 to 7.3.6.
1009
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001010 **warning** some sample fetches are not available in some context. These
1011 limitations are specified in this documentation when theire useful.
1012
Thierry FOURNIER2e4893c2015-03-18 13:37:27 +01001013 :see: TXN.f
1014 :see: TXN.sf
1015
1016 Fetches are useful for:
1017
1018 * get system time,
1019 * get environment variable,
1020 * get random numbers,
1021 * known backend status like the number of users in queue or the number of
1022 connections established,
1023 * client information like ip source or destination,
1024 * deal with stick tables,
1025 * Established SSL informations,
1026 * HTTP information like headers or method.
1027
1028.. code-block:: lua
1029
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001030 function action(txn)
1031 -- Get source IP
1032 local clientip = txn.f:src()
1033 end
Thierry FOURNIER2e4893c2015-03-18 13:37:27 +01001034..
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001035
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001036.. _converters_class:
1037
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001038Converters class
1039================
1040
1041.. js:class:: Converters
1042
1043 This class contains a lot of internal HAProxy sample converters. See the
Thierry FOURNIER2e4893c2015-03-18 13:37:27 +01001044 HAProxy documentation "configuration.txt" for more information about her
1045 usage. Its the chapter 7.3.1.
1046
1047 :see: TXN.c
1048 :see: TXN.sc
1049
1050 Converters provides statefull transformation. They are useful for:
1051
1052 * converting input to base64,
1053 * applying hash on input string (djb2, crc32, sdbm, wt6),
1054 * format date,
1055 * json escape,
Pieter Baauw4d7f7662015-11-08 16:38:08 +01001056 * extracting preferred language comparing two lists,
Thierry FOURNIER2e4893c2015-03-18 13:37:27 +01001057 * turn to lower or upper chars,
1058 * deal with stick tables.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001059
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001060.. _channel_class:
1061
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001062Channel class
1063=============
1064
1065.. js:class:: Channel
1066
1067 HAProxy uses two buffers for the processing of the requests. The first one is
1068 used with the request data (from the client to the server) and the second is
1069 used for the response data (from the server to the client).
1070
1071 Each buffer contains two types of data. The first type is the incoming data
1072 waiting for a processing. The second part is the outgoing data already
1073 processed. Usually, the incoming data is processed, after it is tagged as
1074 outgoing data, and finally it is sent. The following functions provides tools
1075 for manipulating these data in a buffer.
1076
1077 The following diagram shows where the channel class function are applied.
1078
1079 **Warning**: It is not possible to read from the response in request action,
1080 and it is not possible to read for the request channel in response action.
1081
Thierry FOURNIER2e4893c2015-03-18 13:37:27 +01001082.. image:: _static/channel.png
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001083
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001084.. js:function:: Channel.dup(channel)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001085
1086 This function returns a string that contain the entire buffer. The data is
1087 not remove from the buffer and can be reprocessed later.
1088
1089 If the buffer cant receive more data, a 'nil' value is returned.
1090
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001091 :param class_channel channel: The manipulated Channel.
Pieter Baauw4d7f7662015-11-08 16:38:08 +01001092 :returns: a string containing all the available data or nil.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001093
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001094.. js:function:: Channel.get(channel)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001095
1096 This function returns a string that contain the entire buffer. The data is
1097 consumed from the buffer.
1098
1099 If the buffer cant receive more data, a 'nil' value is returned.
1100
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001101 :param class_channel channel: The manipulated Channel.
Pieter Baauw4d7f7662015-11-08 16:38:08 +01001102 :returns: a string containing all the available data or nil.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001103
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02001104.. js:function:: Channel.getline(channel)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001105
1106 This function returns a string that contain the first line of the buffer. The
1107 data is consumed. If the data returned doesn't contains a final '\n' its
1108 assumed than its the last available data in the buffer.
1109
1110 If the buffer cant receive more data, a 'nil' value is returned.
1111
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001112 :param class_channel channel: The manipulated Channel.
Pieter Baauw386a1272015-08-16 15:26:24 +02001113 :returns: a string containing the available line or nil.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001114
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001115.. js:function:: Channel.set(channel, string)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001116
1117 This function replace the content of the buffer by the string. The function
1118 returns the copied length, otherwise, it returns -1.
1119
1120 The data set with this function are not send. They wait for the end of
1121 HAProxy processing, so the buffer can be full.
1122
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001123 :param class_channel channel: The manipulated Channel.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001124 :param string string: The data which will sent.
Pieter Baauw4d7f7662015-11-08 16:38:08 +01001125 :returns: an integer containing the amount of bytes copied or -1.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001126
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001127.. js:function:: Channel.append(channel, string)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001128
1129 This function append the string argument to the content of the buffer. The
1130 function returns the copied length, otherwise, it returns -1.
1131
1132 The data set with this function are not send. They wait for the end of
1133 HAProxy processing, so the buffer can be full.
1134
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001135 :param class_channel channel: The manipulated Channel.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001136 :param string string: The data which will sent.
Pieter Baauw4d7f7662015-11-08 16:38:08 +01001137 :returns: an integer containing the amount of bytes copied or -1.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001138
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001139.. js:function:: Channel.send(channel, string)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001140
1141 This function required immediate send of the data. Unless if the connection
1142 is close, the buffer is regularly flushed and all the string can be sent.
1143
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001144 :param class_channel channel: The manipulated Channel.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001145 :param string string: The data which will sent.
Pieter Baauw4d7f7662015-11-08 16:38:08 +01001146 :returns: an integer containing the amount of bytes copied or -1.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001147
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001148.. js:function:: Channel.get_in_length(channel)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001149
1150 This function returns the length of the input part of the buffer.
1151
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001152 :param class_channel channel: The manipulated Channel.
Pieter Baauw4d7f7662015-11-08 16:38:08 +01001153 :returns: an integer containing the amount of available bytes.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001154
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001155.. js:function:: Channel.get_out_length(channel)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001156
1157 This function returns the length of the output part of the buffer.
1158
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001159 :param class_channel channel: The manipulated Channel.
Pieter Baauw4d7f7662015-11-08 16:38:08 +01001160 :returns: an integer containing the amount of available bytes.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001161
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001162.. js:function:: Channel.forward(channel, int)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001163
1164 This function transfer bytes from the input part of the buffer to the output
1165 part.
1166
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001167 :param class_channel channel: The manipulated Channel.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001168 :param integer int: The amount of data which will be forwarded.
1169
Thierry FOURNIER / OZON.IO65192f32016-11-07 15:28:40 +01001170.. js:function:: Channel.is_full(channel)
1171
1172 This function returns true if the buffer channel is full.
1173
1174 :returns: a boolean
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001175
1176.. _http_class:
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001177
1178HTTP class
1179==========
1180
1181.. js:class:: HTTP
1182
1183 This class contain all the HTTP manipulation functions.
1184
Pieter Baauw386a1272015-08-16 15:26:24 +02001185.. js:function:: HTTP.req_get_headers(http)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001186
1187 Returns an array containing all the request headers.
1188
1189 :param class_http http: The related http object.
1190 :returns: array of headers.
Pieter Baauw386a1272015-08-16 15:26:24 +02001191 :see: HTTP.res_get_headers()
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001192
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001193 This is the form of the returned array:
1194
1195.. code-block:: lua
1196
1197 HTTP:req_get_headers()['<header-name>'][<header-index>] = "<header-value>"
1198
1199 local hdr = HTTP:req_get_headers()
1200 hdr["host"][0] = "www.test.com"
1201 hdr["accept"][0] = "audio/basic q=1"
1202 hdr["accept"][1] = "audio/*, q=0.2"
1203 hdr["accept"][2] = "*/*, q=0.1"
1204..
1205
Pieter Baauw386a1272015-08-16 15:26:24 +02001206.. js:function:: HTTP.res_get_headers(http)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001207
1208 Returns an array containing all the response headers.
1209
1210 :param class_http http: The related http object.
1211 :returns: array of headers.
Pieter Baauw386a1272015-08-16 15:26:24 +02001212 :see: HTTP.req_get_headers()
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001213
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001214 This is the form of the returned array:
1215
1216.. code-block:: lua
1217
1218 HTTP:res_get_headers()['<header-name>'][<header-index>] = "<header-value>"
1219
1220 local hdr = HTTP:req_get_headers()
1221 hdr["host"][0] = "www.test.com"
1222 hdr["accept"][0] = "audio/basic q=1"
1223 hdr["accept"][1] = "audio/*, q=0.2"
1224 hdr["accept"][2] = "*.*, q=0.1"
1225..
1226
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001227.. js:function:: HTTP.req_add_header(http, name, value)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001228
1229 Appends an HTTP header field in the request whose name is
1230 specified in "name" and whose value is defined in "value".
1231
1232 :param class_http http: The related http object.
1233 :param string name: The header name.
1234 :param string value: The header value.
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001235 :see: HTTP.res_add_header()
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001236
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001237.. js:function:: HTTP.res_add_header(http, name, value)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001238
1239 appends an HTTP header field in the response whose name is
1240 specified in "name" and whose value is defined in "value".
1241
1242 :param class_http http: The related http object.
1243 :param string name: The header name.
1244 :param string value: The header value.
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001245 :see: HTTP.req_add_header()
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001246
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001247.. js:function:: HTTP.req_del_header(http, name)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001248
1249 Removes all HTTP header fields in the request whose name is
1250 specified in "name".
1251
1252 :param class_http http: The related http object.
1253 :param string name: The header name.
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001254 :see: HTTP.res_del_header()
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001255
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001256.. js:function:: HTTP.res_del_header(http, name)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001257
1258 Removes all HTTP header fields in the response whose name is
1259 specified in "name".
1260
1261 :param class_http http: The related http object.
1262 :param string name: The header name.
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001263 :see: HTTP.req_del_header()
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001264
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001265.. js:function:: HTTP.req_set_header(http, name, value)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001266
1267 This variable replace all occurence of all header "name", by only
1268 one containing the "value".
1269
1270 :param class_http http: The related http object.
1271 :param string name: The header name.
1272 :param string value: The header value.
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001273 :see: HTTP.res_set_header()
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001274
1275 This function does the same work as the folowwing code:
1276
1277.. code-block:: lua
1278
1279 function fcn(txn)
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001280 TXN.http:req_del_header("header")
1281 TXN.http:req_add_header("header", "value")
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001282 end
1283..
1284
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001285.. js:function:: HTTP.res_set_header(http, name, value)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001286
1287 This variable replace all occurence of all header "name", by only
1288 one containing the "value".
1289
1290 :param class_http http: The related http object.
1291 :param string name: The header name.
1292 :param string value: The header value.
Pieter Baauw386a1272015-08-16 15:26:24 +02001293 :see: HTTP.req_rep_header()
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001294
Pieter Baauw386a1272015-08-16 15:26:24 +02001295.. js:function:: HTTP.req_rep_header(http, name, regex, replace)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001296
1297 Matches the regular expression in all occurrences of header field "name"
1298 according to "regex", and replaces them with the "replace" argument. The
1299 replacement value can contain back references like \1, \2, ... This
1300 function works with the request.
1301
1302 :param class_http http: The related http object.
1303 :param string name: The header name.
1304 :param string regex: The match regular expression.
1305 :param string replace: The replacement value.
Pieter Baauw386a1272015-08-16 15:26:24 +02001306 :see: HTTP.res_rep_header()
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001307
Pieter Baauw386a1272015-08-16 15:26:24 +02001308.. js:function:: HTTP.res_rep_header(http, name, regex, string)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001309
1310 Matches the regular expression in all occurrences of header field "name"
1311 according to "regex", and replaces them with the "replace" argument. The
1312 replacement value can contain back references like \1, \2, ... This
1313 function works with the request.
1314
1315 :param class_http http: The related http object.
1316 :param string name: The header name.
1317 :param string regex: The match regular expression.
1318 :param string replace: The replacement value.
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001319 :see: HTTP.req_replace_header()
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001320
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001321.. js:function:: HTTP.req_set_method(http, method)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001322
1323 Rewrites the request method with the parameter "method".
1324
1325 :param class_http http: The related http object.
1326 :param string method: The new method.
1327
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001328.. js:function:: HTTP.req_set_path(http, path)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001329
1330 Rewrites the request path with the "path" parameter.
1331
1332 :param class_http http: The related http object.
1333 :param string path: The new path.
1334
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001335.. js:function:: HTTP.req_set_query(http, query)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001336
1337 Rewrites the request's query string which appears after the first question
1338 mark ("?") with the parameter "query".
1339
1340 :param class_http http: The related http object.
1341 :param string query: The new query.
1342
Thierry FOURNIER0d79cf62015-08-26 14:20:58 +02001343.. js:function:: HTTP.req_set_uri(http, uri)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001344
1345 Rewrites the request URI with the parameter "uri".
1346
1347 :param class_http http: The related http object.
1348 :param string uri: The new uri.
1349
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +02001350.. js:function:: HTTP.res_set_status(http, status)
1351
1352 Rewrites the response status code with the parameter "code". Note that the
1353 reason is automatically adapted to the new code.
1354
1355 :param class_http http: The related http object.
1356 :param integer status: The new response status code.
1357
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001358.. _txn_class:
1359
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001360TXN class
1361=========
1362
1363.. js:class:: TXN
1364
1365 The txn class contain all the functions relative to the http or tcp
1366 transaction (Note than a tcp stream is the same than a tcp transaction, but
1367 an HTTP transaction is not the same than a tcp stream).
1368
1369 The usage of this class permits to retrieve data from the requests, alter it
1370 and forward it.
1371
1372 All the functions provided by this class are available in the context
1373 **sample-fetches** and **actions**.
1374
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001375.. js:attribute:: TXN.c
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001376
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001377 :returns: An :ref:`converters_class`.
1378
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001379 This attribute contains a Converters class object.
1380
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001381.. js:attribute:: TXN.sc
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001382
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001383 :returns: An :ref:`converters_class`.
1384
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001385 This attribute contains a Converters class object. The functions of
1386 this object returns always a string.
1387
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001388.. js:attribute:: TXN.f
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001389
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001390 :returns: An :ref:`fetches_class`.
1391
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001392 This attribute contains a Fetches class object.
1393
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001394.. js:attribute:: TXN.sf
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001395
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001396 :returns: An :ref:`fetches_class`.
1397
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001398 This attribute contains a Fetches class object. The functions of
1399 this object returns always a string.
1400
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001401.. js:attribute:: TXN.req
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001402
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001403 :returns: An :ref:`channel_class`.
1404
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001405 This attribute contains a channel class object for the request buffer.
1406
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001407.. js:attribute:: TXN.res
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001408
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001409 :returns: An :ref:`channel_class`.
1410
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001411 This attribute contains a channel class object for the response buffer.
1412
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001413.. js:attribute:: TXN.http
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001414
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001415 :returns: An :ref:`http_class`.
1416
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001417 This attribute contains an HTTP class object. It is avalaible only if the
1418 proxy has the "mode http" enabled.
1419
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01001420.. js:function:: TXN.log(TXN, loglevel, msg)
1421
1422 This function sends a log. The log is sent, according with the HAProxy
1423 configuration file, on the default syslog server if it is configured and on
1424 the stderr if it is allowed.
1425
1426 :param class_txn txn: The class txn object containing the data.
1427 :param integer loglevel: Is the log level asociated with the message. It is a
1428 number between 0 and 7.
1429 :param string msg: The log content.
1430 :see: core.emerg, core.alert, core.crit, core.err, core.warning, core.notice,
1431 core.info, core.debug (log level definitions)
1432 :see: TXN.deflog
1433 :see: TXN.Debug
1434 :see: TXN.Info
1435 :see: TXN.Warning
1436 :see: TXN.Alert
1437
1438.. js:function:: TXN.deflog(TXN, msg)
1439
1440 Sends a log line with the default loglevel for the proxy ssociated with the
1441 transaction.
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.. js:function:: TXN.Debug(txn, msg)
1448
1449 :param class_txn txn: The class txn object containing the data.
1450 :param string msg: The log content.
1451 :see: TXN.log
1452
1453 Does the same job than:
1454
1455.. code-block:: lua
1456
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001457 function Debug(txn, msg)
1458 TXN.log(txn, core.debug, msg)
1459 end
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01001460..
1461
1462.. js:function:: TXN.Info(txn, msg)
1463
1464 :param class_txn txn: The class txn object containing the data.
1465 :param string msg: The log content.
1466 :see: TXN.log
1467
1468.. code-block:: lua
1469
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001470 function Debug(txn, msg)
1471 TXN.log(txn, core.info, msg)
1472 end
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01001473..
1474
1475.. js:function:: TXN.Warning(txn, msg)
1476
1477 :param class_txn txn: The class txn object containing the data.
1478 :param string msg: The log content.
1479 :see: TXN.log
1480
1481.. code-block:: lua
1482
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001483 function Debug(txn, msg)
1484 TXN.log(txn, core.warning, msg)
1485 end
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01001486..
1487
1488.. js:function:: TXN.Alert(txn, msg)
1489
1490 :param class_txn txn: The class txn object containing the data.
1491 :param string msg: The log content.
1492 :see: TXN.log
1493
1494.. code-block:: lua
1495
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001496 function Debug(txn, msg)
1497 TXN.log(txn, core.alert, msg)
1498 end
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01001499..
1500
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001501.. js:function:: TXN.get_priv(txn)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001502
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001503 Return Lua data stored in the current transaction (with the `TXN.set_priv()`)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001504 function. If no data are stored, it returns a nil value.
1505
1506 :param class_txn txn: The class txn object containing the data.
1507 :returns: the opaque data previsously stored, or nil if nothing is
1508 avalaible.
1509
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001510.. js:function:: TXN.set_priv(txn, data)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001511
1512 Store any data in the current HAProxy transaction. This action replace the
1513 old stored data.
1514
1515 :param class_txn txn: The class txn object containing the data.
1516 :param opaque data: The data which is stored in the transaction.
1517
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +02001518.. js:function:: TXN.set_var(TXN, var, value)
1519
David Carlier61fdf8b2015-10-02 11:59:38 +01001520 Converts a Lua type in a HAProxy type and store it in a variable <var>.
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +02001521
1522 :param class_txn txn: The class txn object containing the data.
1523 :param string var: The variable name according with the HAProxy variable syntax.
Thierry FOURNIER / OZON.IOb210bcc2016-12-12 16:24:16 +01001524 :param type value: The value associated to the variable. The type can be string or
1525 integer.
Christopher Faulet85d79c92016-11-09 16:54:56 +01001526
1527.. js:function:: TXN.unset_var(TXN, var)
1528
1529 Unset the variable <var>.
1530
1531 :param class_txn txn: The class txn object containing the data.
1532 :param string var: The variable name according with the HAProxy variable syntax.
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +02001533
1534.. js:function:: TXN.get_var(TXN, var)
1535
1536 Returns data stored in the variable <var> converter in Lua type.
1537
1538 :param class_txn txn: The class txn object containing the data.
1539 :param string var: The variable name according with the HAProxy variable syntax.
1540
Willy Tarreaubc183a62015-08-28 10:39:11 +02001541.. js:function:: TXN.done(txn)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001542
Willy Tarreaubc183a62015-08-28 10:39:11 +02001543 This function terminates processing of the transaction and the associated
1544 session. It can be used when a critical error is detected or to terminate
1545 processing after some data have been returned to the client (eg: a redirect).
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001546
Thierry FOURNIERab00df62016-07-14 11:42:37 +02001547 *Warning*: It not make sense to call this function from sample-fetches. In
1548 this case the behaviour of this one is the same than core.done(): it quit
1549 the Lua execution. The transaction is really aborted only from an action
1550 registered function.
1551
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001552 :param class_txn txn: The class txn object containing the data.
1553
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001554.. js:function:: TXN.set_loglevel(txn, loglevel)
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01001555
1556 Is used to change the log level of the current request. The "loglevel" must
1557 be an integer between 0 and 7.
1558
1559 :param class_txn txn: The class txn object containing the data.
1560 :param integer loglevel: The required log level. This variable can be one of
1561 :see: core.<loglevel>
1562
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001563.. js:function:: TXN.set_tos(txn, tos)
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01001564
1565 Is used to set the TOS or DSCP field value of packets sent to the client to
1566 the value passed in "tos" on platforms which support this.
1567
1568 :param class_txn txn: The class txn object containing the data.
1569 :param integer tos: The new TOS os DSCP.
1570
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001571.. js:function:: TXN.set_mark(txn, mark)
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01001572
1573 Is used to set the Netfilter MARK on all packets sent to the client to the
1574 value passed in "mark" on platforms which support it.
1575
1576 :param class_txn txn: The class txn object containing the data.
1577 :param integer mark: The mark value.
1578
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001579.. _socket_class:
1580
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001581Socket class
1582============
1583
1584.. js:class:: Socket
1585
1586 This class must be compatible with the Lua Socket class. Only the 'client'
1587 functions are available. See the Lua Socket documentation:
1588
1589 `http://w3.impa.br/~diego/software/luasocket/tcp.html
1590 <http://w3.impa.br/~diego/software/luasocket/tcp.html>`_
1591
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001592.. js:function:: Socket.close(socket)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001593
1594 Closes a TCP object. The internal socket used by the object is closed and the
1595 local address to which the object was bound is made available to other
1596 applications. No further operations (except for further calls to the close
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001597 method) are allowed on a closed Socket.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001598
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001599 :param class_socket socket: Is the manipulated Socket.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001600
1601 Note: It is important to close all used sockets once they are not needed,
1602 since, in many systems, each socket uses a file descriptor, which are limited
1603 system resources. Garbage-collected objects are automatically closed before
1604 destruction, though.
1605
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02001606.. js:function:: Socket.connect(socket, address[, port])
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001607
1608 Attempts to connect a socket object to a remote host.
1609
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001610
1611 In case of error, the method returns nil followed by a string describing the
1612 error. In case of success, the method returns 1.
1613
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001614 :param class_socket socket: Is the manipulated Socket.
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02001615 :param string address: can be an IP address or a host name. See below for more
1616 information.
1617 :param integer port: must be an integer number in the range [1..64K].
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001618 :returns: 1 or nil.
1619
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02001620 an address field extension permits to use the connect() function to connect to
1621 other stream than TCP. The syntax containing a simpleipv4 or ipv6 address is
1622 the basically expected format. This format requires the port.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001623
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02001624 Other format accepted are a socket path like "/socket/path", it permits to
1625 connect to a socket. abstract namespaces are supported with the prefix
1626 "abns@", and finaly a filedescriotr can be passed with the prefix "fd@".
1627 The prefix "ipv4@", "ipv6@" and "unix@" are also supported. The port can be
1628 passed int the string. The syntax "127.0.0.1:1234" is valid. in this case, the
1629 parameter *port* is ignored.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001630
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001631.. js:function:: Socket.connect_ssl(socket, address, port)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001632
1633 Same behavior than the function socket:connect, but uses SSL.
1634
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001635 :param class_socket socket: Is the manipulated Socket.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001636 :returns: 1 or nil.
1637
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001638.. js:function:: Socket.getpeername(socket)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001639
1640 Returns information about the remote side of a connected client object.
1641
1642 Returns a string with the IP address of the peer, followed by the port number
1643 that peer is using for the connection. In case of error, the method returns
1644 nil.
1645
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001646 :param class_socket socket: Is the manipulated Socket.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001647 :returns: a string containing the server information.
1648
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001649.. js:function:: Socket.getsockname(socket)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001650
1651 Returns the local address information associated to the object.
1652
1653 The method returns a string with local IP address and a number with the port.
1654 In case of error, the method returns nil.
1655
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001656 :param class_socket socket: Is the manipulated Socket.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001657 :returns: a string containing the client information.
1658
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001659.. js:function:: Socket.receive(socket, [pattern [, prefix]])
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001660
1661 Reads data from a client object, according to the specified read pattern.
1662 Patterns follow the Lua file I/O format, and the difference in performance
1663 between all patterns is negligible.
1664
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001665 :param class_socket socket: Is the manipulated Socket.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001666 :param string|integer pattern: Describe what is required (see below).
1667 :param string prefix: A string which will be prefix the returned data.
1668 :returns: a string containing the required data or nil.
1669
1670 Pattern can be any of the following:
1671
1672 * **`*a`**: reads from the socket until the connection is closed. No
1673 end-of-line translation is performed;
1674
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001675 * **`*l`**: reads a line of text from the Socket. The line is terminated by a
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001676 LF character (ASCII 10), optionally preceded by a CR character
1677 (ASCII 13). The CR and LF characters are not included in the
1678 returned line. In fact, all CR characters are ignored by the
1679 pattern. This is the default pattern.
1680
1681 * **number**: causes the method to read a specified number of bytes from the
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001682 Socket. Prefix is an optional string to be concatenated to the
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001683 beginning of any received data before return.
1684
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02001685 * **empty**: If the pattern is left empty, the default option is `*l`.
1686
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001687 If successful, the method returns the received pattern. In case of error, the
1688 method returns nil followed by an error message which can be the string
1689 'closed' in case the connection was closed before the transmission was
1690 completed or the string 'timeout' in case there was a timeout during the
1691 operation. Also, after the error message, the function returns the partial
1692 result of the transmission.
1693
1694 Important note: This function was changed severely. It used to support
1695 multiple patterns (but I have never seen this feature used) and now it
1696 doesn't anymore. Partial results used to be returned in the same way as
1697 successful results. This last feature violated the idea that all functions
1698 should return nil on error. Thus it was changed too.
1699
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001700.. js:function:: Socket.send(socket, data [, start [, end ]])
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001701
1702 Sends data through client object.
1703
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001704 :param class_socket socket: Is the manipulated Socket.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001705 :param string data: The data that will be sent.
1706 :param integer start: The start position in the buffer of the data which will
1707 be sent.
1708 :param integer end: The end position in the buffer of the data which will
1709 be sent.
1710 :returns: see below.
1711
1712 Data is the string to be sent. The optional arguments i and j work exactly
1713 like the standard string.sub Lua function to allow the selection of a
1714 substring to be sent.
1715
1716 If successful, the method returns the index of the last byte within [start,
1717 end] that has been sent. Notice that, if start is 1 or absent, this is
1718 effectively the total number of bytes sent. In case of error, the method
1719 returns nil, followed by an error message, followed by the index of the last
1720 byte within [start, end] that has been sent. You might want to try again from
1721 the byte following that. The error message can be 'closed' in case the
1722 connection was closed before the transmission was completed or the string
1723 'timeout' in case there was a timeout during the operation.
1724
1725 Note: Output is not buffered. For small strings, it is always better to
1726 concatenate them in Lua (with the '..' operator) and send the result in one
1727 call instead of calling the method several times.
1728
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001729.. js:function:: Socket.setoption(socket, option [, value])
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001730
1731 Just implemented for compatibility, this cal does nothing.
1732
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001733.. js:function:: Socket.settimeout(socket, value [, mode])
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001734
1735 Changes the timeout values for the object. All I/O operations are blocking.
1736 That is, any call to the methods send, receive, and accept will block
1737 indefinitely, until the operation completes. The settimeout method defines a
1738 limit on the amount of time the I/O methods can block. When a timeout time
1739 has elapsed, the affected methods give up and fail with an error code.
1740
1741 The amount of time to wait is specified as the value parameter, in seconds.
1742
1743 The timeout modes are bot implemented, the only settable timeout is the
1744 inactivity time waiting for complete the internal buffer send or waiting for
1745 receive data.
1746
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001747 :param class_socket socket: Is the manipulated Socket.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001748 :param integer value: The timeout value.
1749
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001750.. _map_class:
1751
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001752Map class
1753=========
1754
1755.. js:class:: Map
1756
1757 This class permits to do some lookup in HAProxy maps. The declared maps can
1758 be modified during the runtime throught the HAProxy management socket.
1759
1760.. code-block:: lua
1761
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001762 default = "usa"
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001763
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001764 -- Create and load map
1765 geo = Map.new("geo.map", Map.ip);
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001766
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001767 -- Create new fetch that returns the user country
1768 core.register_fetches("country", function(txn)
1769 local src;
1770 local loc;
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001771
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001772 src = txn.f:fhdr("x-forwarded-for");
1773 if (src == nil) then
1774 src = txn.f:src()
1775 if (src == nil) then
1776 return default;
1777 end
1778 end
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001779
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001780 -- Perform lookup
1781 loc = geo:lookup(src);
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001782
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001783 if (loc == nil) then
1784 return default;
1785 end
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001786
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001787 return loc;
1788 end);
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001789
1790.. js:attribute:: Map.int
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.ip
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.str
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.. js:attribute:: Map.beg
1809
1810 See the HAProxy configuration.txt file, chapter "Using ACLs and fetching
1811 samples" ans subchapter "ACL basics" to understand this pattern matching
1812 method.
1813
1814.. js:attribute:: Map.sub
1815
1816 See the HAProxy configuration.txt file, chapter "Using ACLs and fetching
1817 samples" ans subchapter "ACL basics" to understand this pattern matching
1818 method.
1819
1820.. js:attribute:: Map.dir
1821
1822 See the HAProxy configuration.txt file, chapter "Using ACLs and fetching
1823 samples" ans subchapter "ACL basics" to understand this pattern matching
1824 method.
1825
1826.. js:attribute:: Map.dom
1827
1828 See the HAProxy configuration.txt file, chapter "Using ACLs and fetching
1829 samples" ans subchapter "ACL basics" to understand this pattern matching
1830 method.
1831
1832.. js:attribute:: Map.end
1833
1834 See the HAProxy configuration.txt file, chapter "Using ACLs and fetching
1835 samples" ans subchapter "ACL basics" to understand this pattern matching
1836 method.
1837
1838.. js:attribute:: Map.reg
1839
1840 See the HAProxy configuration.txt file, chapter "Using ACLs and fetching
1841 samples" ans subchapter "ACL basics" to understand this pattern matching
1842 method.
1843
1844
1845.. js:function:: Map.new(file, method)
1846
1847 Creates and load a map.
1848
1849 :param string file: Is the file containing the map.
1850 :param integer method: Is the map pattern matching method. See the attributes
1851 of the Map class.
1852 :returns: a class Map object.
1853 :see: The Map attributes.
1854
1855.. js:function:: Map.lookup(map, str)
1856
1857 Perform a lookup in a map.
1858
1859 :param class_map map: Is the class Map object.
1860 :param string str: Is the string used as key.
1861 :returns: a string containing the result or nil if no match.
1862
1863.. js:function:: Map.slookup(map, str)
1864
1865 Perform a lookup in a map.
1866
1867 :param class_map map: Is the class Map object.
1868 :param string str: Is the string used as key.
1869 :returns: a string containing the result or empty string if no match.
1870
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001871.. _applethttp_class:
1872
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02001873AppletHTTP class
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001874================
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02001875
1876.. js:class:: AppletHTTP
1877
1878 This class is used with applets that requires the 'http' mode. The http applet
1879 can be registered with the *core.register_service()* function. They are used
1880 for processing an http request like a server in back of HAProxy.
1881
1882 This is an hello world sample code:
1883
1884.. code-block:: lua
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001885
Pieter Baauw4d7f7662015-11-08 16:38:08 +01001886 core.register_service("hello-world", "http", function(applet)
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02001887 local response = "Hello World !"
1888 applet:set_status(200)
Pieter Baauw2dcb9bc2015-10-01 22:47:12 +02001889 applet:add_header("content-length", string.len(response))
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02001890 applet:add_header("content-type", "text/plain")
Pieter Baauw2dcb9bc2015-10-01 22:47:12 +02001891 applet:start_response()
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02001892 applet:send(response)
1893 end)
1894
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001895.. js:attribute:: AppletHTTP.c
1896
1897 :returns: A :ref:`converters_class`
1898
1899 This attribute contains a Converters class object.
1900
1901.. js:attribute:: AppletHTTP.sc
1902
1903 :returns: A :ref:`converters_class`
1904
1905 This attribute contains a Converters class object. The
1906 functions of this object returns always a string.
1907
1908.. js:attribute:: AppletHTTP.f
1909
1910 :returns: A :ref:`fetches_class`
1911
1912 This attribute contains a Fetches class object. Note that the
1913 applet execution place cannot access to a valid HAProxy core HTTP
1914 transaction, so some sample fecthes related to the HTTP dependant
1915 values (hdr, path, ...) are not available.
1916
1917.. js:attribute:: AppletHTTP.sf
1918
1919 :returns: A :ref:`fetches_class`
1920
1921 This attribute contains a Fetches class object. The functions of
1922 this object returns always a string. Note that the applet
1923 execution place cannot access to a valid HAProxy core HTTP
1924 transaction, so some sample fecthes related to the HTTP dependant
1925 values (hdr, path, ...) are not available.
1926
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01001927.. js:attribute:: AppletHTTP.method
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001928
1929 :returns: string
1930
1931 The attribute method returns a string containing the HTTP
1932 method.
1933
1934.. js:attribute:: AppletHTTP.version
1935
1936 :returns: string
1937
1938 The attribute version, returns a string containing the HTTP
1939 request version.
1940
1941.. js:attribute:: AppletHTTP.path
1942
1943 :returns: string
1944
1945 The attribute path returns a string containing the HTTP
1946 request path.
1947
1948.. js:attribute:: AppletHTTP.qs
1949
1950 :returns: string
1951
1952 The attribute qs returns a string containing the HTTP
1953 request query string.
1954
1955.. js:attribute:: AppletHTTP.length
1956
1957 :returns: integer
1958
1959 The attribute length returns an integer containing the HTTP
1960 body length.
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02001961
Thierry FOURNIER841475e2015-12-11 17:10:09 +01001962.. js:attribute:: AppletHTTP.headers
1963
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001964 :returns: array
1965
1966 The attribute headers returns an array containing the HTTP
1967 headers. The header names are always in lower case. As the header name can be
1968 encountered more than once in each request, the value is indexed with 0 as
1969 first index value. The array have this form:
1970
1971.. code-block:: lua
1972
1973 AppletHTTP.headers['<header-name>'][<header-index>] = "<header-value>"
1974
1975 AppletHTTP.headers["host"][0] = "www.test.com"
1976 AppletHTTP.headers["accept"][0] = "audio/basic q=1"
1977 AppletHTTP.headers["accept"][1] = "audio/*, q=0.2"
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01001978 AppletHTTP.headers["accept"][2] = "*/*, q=0.1"
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001979..
1980
1981.. js:attribute:: AppletHTTP.headers
1982
Thierry FOURNIER841475e2015-12-11 17:10:09 +01001983 Contains an array containing all the request headers.
1984
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01001985.. js:function:: AppletHTTP.set_status(applet, code)
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02001986
1987 This function sets the HTTP status code for the response. The allowed code are
1988 from 100 to 599.
1989
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01001990 :param class_AppletHTTP applet: An :ref:`applethttp_class`
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02001991 :param integer code: the status code returned to the client.
1992
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01001993.. js:function:: AppletHTTP.add_header(applet, name, value)
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02001994
1995 This function add an header in the response. Duplicated headers are not
1996 collapsed. The special header *content-length* is used to determinate the
1997 response length. If it not exists, a *transfer-encoding: chunked* is set, and
1998 all the write from the funcion *AppletHTTP:send()* become a chunk.
1999
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01002000 :param class_AppletHTTP applet: An :ref:`applethttp_class`
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002001 :param string name: the header name
2002 :param string value: the header value
2003
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01002004.. js:function:: AppletHTTP.start_response(applet)
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002005
2006 This function indicates to the HTTP engine that it can process and send the
2007 response headers. After this called we cannot add headers to the response; We
2008 cannot use the *AppletHTTP:send()* function if the
2009 *AppletHTTP:start_response()* is not called.
2010
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01002011 :param class_AppletHTTP applet: An :ref:`applethttp_class`
2012
2013.. js:function:: AppletHTTP.getline(applet)
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002014
2015 This function returns a string containing one line from the http body. If the
2016 data returned doesn't contains a final '\\n' its assumed than its the last
2017 available data before the end of stream.
2018
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01002019 :param class_AppletHTTP applet: An :ref:`applethttp_class`
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002020 :returns: a string. The string can be empty if we reach the end of the stream.
2021
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01002022.. js:function:: AppletHTTP.receive(applet, [size])
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002023
2024 Reads data from the HTTP body, according to the specified read *size*. If the
2025 *size* is missing, the function tries to read all the content of the stream
2026 until the end. If the *size* is bigger than the http body, it returns the
2027 amount of data avalaible.
2028
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01002029 :param class_AppletHTTP applet: An :ref:`applethttp_class`
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002030 :param integer size: the required read size.
2031 :returns: always return a string,the string can be empty is the connexion is
2032 closed.
2033
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01002034.. js:function:: AppletHTTP.send(applet, msg)
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002035
2036 Send the message *msg* on the http request body.
2037
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01002038 :param class_AppletHTTP applet: An :ref:`applethttp_class`
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002039 :param string msg: the message to send.
2040
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01002041.. js:function:: AppletHTTP.get_priv(applet)
2042
2043 Return Lua data stored in the current transaction (with the
2044 `AppletHTTP.set_priv()`) function. If no data are stored, it returns a nil
2045 value.
2046
2047 :param class_AppletHTTP applet: An :ref:`applethttp_class`
2048 :returns: the opaque data previsously stored, or nil if nothing is
2049 avalaible.
2050
2051.. js:function:: AppletHTTP.set_priv(applet, data)
2052
2053 Store any data in the current HAProxy transaction. This action replace the
2054 old stored data.
2055
2056 :param class_AppletHTTP applet: An :ref:`applethttp_class`
2057 :param opaque data: The data which is stored in the transaction.
2058
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002059.. _applettcp_class:
2060
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002061AppletTCP class
2062===============
2063
2064.. js:class:: AppletTCP
2065
2066 This class is used with applets that requires the 'tcp' mode. The tcp applet
2067 can be registered with the *core.register_service()* function. They are used
2068 for processing a tcp stream like a server in back of HAProxy.
2069
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002070.. js:attribute:: AppletTCP.c
2071
2072 :returns: A :ref:`converters_class`
2073
2074 This attribute contains a Converters class object.
2075
2076.. js:attribute:: AppletTCP.sc
2077
2078 :returns: A :ref:`converters_class`
2079
2080 This attribute contains a Converters class object. The
2081 functions of this object returns always a string.
2082
2083.. js:attribute:: AppletTCP.f
2084
2085 :returns: A :ref:`fetches_class`
2086
2087 This attribute contains a Fetches class object.
2088
2089.. js:attribute:: AppletTCP.sf
2090
2091 :returns: A :ref:`fetches_class`
2092
2093 This attribute contains a Fetches class object.
2094
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01002095.. js:function:: AppletTCP.getline(applet)
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002096
2097 This function returns a string containing one line from the stream. If the
2098 data returned doesn't contains a final '\\n' its assumed than its the last
2099 available data before the end of stream.
2100
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01002101 :param class_AppletTCP applet: An :ref:`applettcp_class`
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002102 :returns: a string. The string can be empty if we reach the end of the stream.
2103
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01002104.. js:function:: AppletTCP.receive(applet, [size])
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002105
2106 Reads data from the TCP stream, according to the specified read *size*. If the
2107 *size* is missing, the function tries to read all the content of the stream
2108 until the end.
2109
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01002110 :param class_AppletTCP applet: An :ref:`applettcp_class`
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002111 :param integer size: the required read size.
2112 :returns: always return a string,the string can be empty is the connexion is
2113 closed.
2114
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01002115.. js:function:: AppletTCP.send(appletmsg)
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002116
2117 Send the message on the stream.
2118
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01002119 :param class_AppletTCP applet: An :ref:`applettcp_class`
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002120 :param string msg: the message to send.
2121
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01002122.. js:function:: AppletTCP.get_priv(applet)
2123
2124 Return Lua data stored in the current transaction (with the
2125 `AppletTCP.set_priv()`) function. If no data are stored, it returns a nil
2126 value.
2127
2128 :param class_AppletTCP applet: An :ref:`applettcp_class`
2129 :returns: the opaque data previsously stored, or nil if nothing is
2130 avalaible.
2131
2132.. js:function:: AppletTCP.set_priv(applet, data)
2133
2134 Store any data in the current HAProxy transaction. This action replace the
2135 old stored data.
2136
2137 :param class_AppletTCP applet: An :ref:`applettcp_class`
2138 :param opaque data: The data which is stored in the transaction.
2139
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002140External Lua libraries
2141======================
2142
2143A lot of useful lua libraries can be found here:
2144
2145* `https://lua-toolbox.com/ <https://lua-toolbox.com/>`_
2146
2147Redis acces:
2148
2149* `https://github.com/nrk/redis-lua <https://github.com/nrk/redis-lua>`_
2150
2151This is an example about the usage of the Redis library with HAProxy. Note that
2152each call of any function of this library can throw an error if the socket
2153connection fails.
2154
2155.. code-block:: lua
2156
2157 -- load the redis library
2158 local redis = require("redis");
2159
2160 function do_something(txn)
2161
2162 -- create and connect new tcp socket
2163 local tcp = core.tcp();
2164 tcp:settimeout(1);
2165 tcp:connect("127.0.0.1", 6379);
2166
2167 -- use the redis library with this new socket
2168 local client = redis.connect({socket=tcp});
2169 client:ping();
2170
2171 end
2172
2173OpenSSL:
2174
2175* `http://mkottman.github.io/luacrypto/index.html
2176 <http://mkottman.github.io/luacrypto/index.html>`_
2177
2178* `https://github.com/brunoos/luasec/wiki
2179 <https://github.com/brunoos/luasec/wiki>`_
Thierry FOURNIER2e4893c2015-03-18 13:37:27 +01002180