blob: 2bca4d07f512ea440f245b7180f362b5b9e0affc [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
522.. js:function:: core.set_nice(nice)
523
524 **context**: task, action, sample-fetch, converter
525
526 Change the nice of the current task or current session.
Thierry FOURNIER2e4893c2015-03-18 13:37:27 +0100527
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100528 :param integer nice: the nice value, it must be between -1024 and 1024.
529
530.. js:function:: core.set_map(filename, key, value)
531
532 **context**: init, task, action, sample-fetch, converter
533
534 set the value *value* associated to the key *key* in the map referenced by
535 *filename*.
536
Thierry FOURNIERdc595002015-12-21 11:13:52 +0100537 :param string filename: the Map reference
538 :param string key: the key to set or replace
539 :param string value: the associated value
540
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100541.. js:function:: core.sleep(int seconds)
542
543 **context**: body, init, task, action
544
545 The `core.sleep()` functions stop the Lua execution between specified seconds.
546
547 :param integer seconds: the required seconds.
548
549.. js:function:: core.tcp()
550
551 **context**: init, task, action
552
553 This function returns a new object of a *socket* class.
554
Thierry FOURNIERdc595002015-12-21 11:13:52 +0100555 :returns: A :ref:`socket_class` object.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100556
Thierry Fournier1de16592016-01-27 09:49:07 +0100557.. js:function:: core.concat()
558
559 **context**: body, init, task, action, sample-fetch, converter
560
561 This function retruns a new concat object.
562
563 :returns: A :ref:`concat_class` object.
564
Thierry FOURNIER0a99b892015-08-26 00:14:17 +0200565.. js:function:: core.done(data)
566
567 **context**: body, init, task, action, sample-fetch, converter
568
569 :param any data: Return some data for the caller. It is useful with
570 sample-fetches and sample-converters.
571
572 Immediately stops the current Lua execution and returns to the caller which
573 may be a sample fetch, a converter or an action and returns the specified
574 value (ignored for actions). It is used when the LUA process finishes its
575 work and wants to give back the control to HAProxy without executing the
576 remaining code. It can be seen as a multi-level "return".
577
Thierry FOURNIER486f5a02015-03-16 15:13:03 +0100578.. js:function:: core.yield()
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100579
580 **context**: task, action, sample-fetch, converter
581
582 Give back the hand at the HAProxy scheduler. It is used when the LUA
583 processing consumes a lot of processing time.
584
Thierry Fournierf61aa632016-02-19 20:56:00 +0100585.. _proxy_class:
586
587Proxy class
588============
589
590.. js:class:: Proxy
591
592 This class provides a way for manipulating proxy and retrieving information
593 like statistics.
594
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +0100595.. js:attribute:: Proxy.servers
596
597 Contain an array with the attached servers. Each server entry is an object of
598 type :ref:`server_class`.
599
Thierry Fournierff480422016-02-25 08:36:46 +0100600.. js:attribute:: Proxy.listeners
601
602 Contain an array with the attached listeners. Each listeners entry is an
603 object of type :ref:`listener_class`.
604
Thierry Fournierf61aa632016-02-19 20:56:00 +0100605.. js:function:: Proxy.pause(px)
606
607 Pause the proxy. See the management socket documentation for more information.
608
609 :param class_proxy px: A :ref:`proxy_class` which indicates the manipulated
610 proxy.
611
612.. js:function:: Proxy.resume(px)
613
614 Resume the proxy. See the management socket documentation for more
615 information.
616
617 :param class_proxy px: A :ref:`proxy_class` which indicates the manipulated
618 proxy.
619
620.. js:function:: Proxy.stop(px)
621
622 Stop the proxy. See the management socket documentation for more information.
623
624 :param class_proxy px: A :ref:`proxy_class` which indicates the manipulated
625 proxy.
626
627.. js:function:: Proxy.shut_bcksess(px)
628
629 Kill the session attached to a backup server. See the management socket
630 documentation for more information.
631
632 :param class_proxy px: A :ref:`proxy_class` which indicates the manipulated
633 proxy.
634
635.. js:function:: Proxy.get_cap(px)
636
637 Returns a string describing the capabilities of the proxy.
638
639 :param class_proxy px: A :ref:`proxy_class` which indicates the manipulated
640 proxy.
641 :returns: a string "frontend", "backend", "proxy" or "ruleset".
642
643.. js:function:: Proxy.get_mode(px)
644
645 Returns a string describing the mode of the current proxy.
646
647 :param class_proxy px: A :ref:`proxy_class` which indicates the manipulated
648 proxy.
649 :returns: a string "tcp", "http", "health" or "unknown"
650
651.. js:function:: Proxy.get_stats(px)
652
653 Returns an array containg the proxy statistics. The statistics returned are
654 not the same if the proxy is frontend or a backend.
655
656 :param class_proxy px: A :ref:`proxy_class` which indicates the manipulated
657 proxy.
658 :returns: a key/value array containing stats
659
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +0100660.. _server_class:
661
662Server class
663============
664
665.. js:function:: Server.is_draining(sv)
666
667 Return true if the server is currently draining stiky connections.
668
669 :param class_server sv: A :ref:`server_class` which indicates the manipulated
670 server.
671 :returns: a boolean
672
673.. js:function:: Server.set_weight(sv, weight)
674
675 Dynamically change the weight of the serveur. See the management socket
676 documentation for more information about the format of the string.
677
678 :param class_server sv: A :ref:`server_class` which indicates the manipulated
679 server.
680 :param string weight: A string describing the server weight.
681
682.. js:function:: Server.get_weight(sv)
683
684 This function returns an integer representing the serveur weight.
685
686 :param class_server sv: A :ref:`server_class` which indicates the manipulated
687 server.
688 :returns: an integer.
689
690.. js:function:: Server.set_addr(sv, addr)
691
692 Dynamically change the address of the serveur. See the management socket
693 documentation for more information about the format of the string.
694
695 :param class_server sv: A :ref:`server_class` which indicates the manipulated
696 server.
697 :param string weight: A string describing the server address.
698
699.. js:function:: Server.get_addr(sv)
700
701 Returns a string describing the address of the serveur.
702
703 :param class_server sv: A :ref:`server_class` which indicates the manipulated
704 server.
705 :returns: A string
706
707.. js:function:: Server.get_stats(sv)
708
709 Returns server statistics.
710
711 :param class_server sv: A :ref:`server_class` which indicates the manipulated
712 server.
713 :returns: a key/value array containing stats
714
715.. js:function:: Server.shut_sess(sv)
716
717 Shutdown all the sessions attached to the server. See the management socket
718 documentation for more information about this function.
719
720 :param class_server sv: A :ref:`server_class` which indicates the manipulated
721 server.
722
723.. js:function:: Server.set_drain(sv)
724
725 Drain sticky sessions. See the management socket documentation for more
726 information about this function.
727
728 :param class_server sv: A :ref:`server_class` which indicates the manipulated
729 server.
730
731.. js:function:: Server.set_maint(sv)
732
733 Set maintenance mode. See the management socket documentation for more
734 information about this function.
735
736 :param class_server sv: A :ref:`server_class` which indicates the manipulated
737 server.
738
739.. js:function:: Server.set_ready(sv)
740
741 Set normal mode. See the management socket documentation for more information
742 about this function.
743
744 :param class_server sv: A :ref:`server_class` which indicates the manipulated
745 server.
746
747.. js:function:: Server.check_enable(sv)
748
749 Enable health checks. See the management socket documentation for more
750 information about this function.
751
752 :param class_server sv: A :ref:`server_class` which indicates the manipulated
753 server.
754
755.. js:function:: Server.check_disable(sv)
756
757 Disable health checks. See the management socket documentation for more
758 information about this function.
759
760 :param class_server sv: A :ref:`server_class` which indicates the manipulated
761 server.
762
763.. js:function:: Server.check_force_up(sv)
764
765 Force health-check up. See the management socket documentation for more
766 information about this function.
767
768 :param class_server sv: A :ref:`server_class` which indicates the manipulated
769 server.
770
771.. js:function:: Server.check_force_nolb(sv)
772
773 Force health-check nolb mode. See the management socket documentation for more
774 information about this function.
775
776 :param class_server sv: A :ref:`server_class` which indicates the manipulated
777 server.
778
779.. js:function:: Server.check_force_down(sv)
780
781 Force health-check down. See the management socket documentation for more
782 information about this function.
783
784 :param class_server sv: A :ref:`server_class` which indicates the manipulated
785 server.
786
787.. js:function:: Server.agent_enable(sv)
788
789 Enable agent check. See the management socket documentation for more
790 information about this function.
791
792 :param class_server sv: A :ref:`server_class` which indicates the manipulated
793 server.
794
795.. js:function:: Server.agent_disable(sv)
796
797 Disable agent check. See the management socket documentation for more
798 information about this function.
799
800 :param class_server sv: A :ref:`server_class` which indicates the manipulated
801 server.
802
803.. js:function:: Server.agent_force_up(sv)
804
805 Force agent check up. See the management socket documentation for more
806 information about this function.
807
808 :param class_server sv: A :ref:`server_class` which indicates the manipulated
809 server.
810
811.. js:function:: Server.agent_force_down(sv)
812
813 Force agent check down. See the management socket documentation for more
814 information about this function.
815
816 :param class_server sv: A :ref:`server_class` which indicates the manipulated
817 server.
818
Thierry Fournierff480422016-02-25 08:36:46 +0100819.. _listener_class:
820
821Listener class
822==============
823
824.. js:function:: Listener.get_stats(ls)
825
826 Returns server statistics.
827
828 :param class_listener ls: A :ref:`listener_class` which indicates the
829 manipulated listener.
830 :returns: a key/value array containing stats
831
Thierry Fournier1de16592016-01-27 09:49:07 +0100832.. _concat_class:
833
834Concat class
835============
836
837.. js:class:: Concat
838
839 This class provides a fast way for string concatenation. The way using native
840 Lua concatenation like the code below is slow for some reasons.
841
842.. code-block:: lua
843
844 str = "string1"
845 str = str .. ", string2"
846 str = str .. ", string3"
847..
848
849 For each concatenation, Lua:
850 * allocate memory for the result,
851 * catenate the two string copying the strings in the new memory bloc,
852 * free the old memory block containing the string whoch is no longer used.
853 This process does many memory move, allocation and free. In addition, the
854 memory is not really freed, it is just mark mark as unsused and wait for the
855 garbage collector.
856
857 The Concat class provide an alternative way for catenating strings. It uses
858 the internal Lua mechanism (it does not allocate memory), but it doesn't copy
859 the data more than once.
860
861 On my computer, the following loops spends 0.2s for the Concat method and
862 18.5s for the pure Lua implementation. So, the Concat class is about 1000x
863 faster than the embedded solution.
864
865.. code-block:: lua
866
867 for j = 1, 100 do
868 c = core.concat()
869 for i = 1, 20000 do
870 c:add("#####")
871 end
872 end
873..
874
875.. code-block:: lua
876
877 for j = 1, 100 do
878 c = ""
879 for i = 1, 20000 do
880 c = c .. "#####"
881 end
882 end
883..
884
885.. js:function:: Concat.add(concat, string)
886
887 This function adds a string to the current concatenated string.
888
889 :param class_concat concat: A :ref:`concat_class` which contains the currently
890 builded string.
891 :param string string: A new string to concatenate to the current builded
892 string.
893
894.. js:function:: Concat.dump(concat)
895
896 This function returns the concanated string.
897
898 :param class_concat concat: A :ref:`concat_class` which contains the currently
899 builded string.
900 :returns: the concatenated string
901
Thierry FOURNIERdc595002015-12-21 11:13:52 +0100902.. _fetches_class:
903
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100904Fetches class
905=============
906
907.. js:class:: Fetches
908
909 This class contains a lot of internal HAProxy sample fetches. See the
Thierry FOURNIER2e4893c2015-03-18 13:37:27 +0100910 HAProxy "configuration.txt" documentation for more information about her
911 usage. they are the chapters 7.3.2 to 7.3.6.
912
Thierry FOURNIERdc595002015-12-21 11:13:52 +0100913 **warning** some sample fetches are not available in some context. These
914 limitations are specified in this documentation when theire useful.
915
Thierry FOURNIER2e4893c2015-03-18 13:37:27 +0100916 :see: TXN.f
917 :see: TXN.sf
918
919 Fetches are useful for:
920
921 * get system time,
922 * get environment variable,
923 * get random numbers,
924 * known backend status like the number of users in queue or the number of
925 connections established,
926 * client information like ip source or destination,
927 * deal with stick tables,
928 * Established SSL informations,
929 * HTTP information like headers or method.
930
931.. code-block:: lua
932
Thierry FOURNIERdc595002015-12-21 11:13:52 +0100933 function action(txn)
934 -- Get source IP
935 local clientip = txn.f:src()
936 end
Thierry FOURNIER2e4893c2015-03-18 13:37:27 +0100937..
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100938
Thierry FOURNIERdc595002015-12-21 11:13:52 +0100939.. _converters_class:
940
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100941Converters class
942================
943
944.. js:class:: Converters
945
946 This class contains a lot of internal HAProxy sample converters. See the
Thierry FOURNIER2e4893c2015-03-18 13:37:27 +0100947 HAProxy documentation "configuration.txt" for more information about her
948 usage. Its the chapter 7.3.1.
949
950 :see: TXN.c
951 :see: TXN.sc
952
953 Converters provides statefull transformation. They are useful for:
954
955 * converting input to base64,
956 * applying hash on input string (djb2, crc32, sdbm, wt6),
957 * format date,
958 * json escape,
Pieter Baauw4d7f7662015-11-08 16:38:08 +0100959 * extracting preferred language comparing two lists,
Thierry FOURNIER2e4893c2015-03-18 13:37:27 +0100960 * turn to lower or upper chars,
961 * deal with stick tables.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100962
Thierry FOURNIERdc595002015-12-21 11:13:52 +0100963.. _channel_class:
964
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100965Channel class
966=============
967
968.. js:class:: Channel
969
970 HAProxy uses two buffers for the processing of the requests. The first one is
971 used with the request data (from the client to the server) and the second is
972 used for the response data (from the server to the client).
973
974 Each buffer contains two types of data. The first type is the incoming data
975 waiting for a processing. The second part is the outgoing data already
976 processed. Usually, the incoming data is processed, after it is tagged as
977 outgoing data, and finally it is sent. The following functions provides tools
978 for manipulating these data in a buffer.
979
980 The following diagram shows where the channel class function are applied.
981
982 **Warning**: It is not possible to read from the response in request action,
983 and it is not possible to read for the request channel in response action.
984
Thierry FOURNIER2e4893c2015-03-18 13:37:27 +0100985.. image:: _static/channel.png
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100986
Thierry FOURNIER486f5a02015-03-16 15:13:03 +0100987.. js:function:: Channel.dup(channel)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100988
989 This function returns a string that contain the entire buffer. The data is
990 not remove from the buffer and can be reprocessed later.
991
992 If the buffer cant receive more data, a 'nil' value is returned.
993
Thierry FOURNIER486f5a02015-03-16 15:13:03 +0100994 :param class_channel channel: The manipulated Channel.
Pieter Baauw4d7f7662015-11-08 16:38:08 +0100995 :returns: a string containing all the available data or nil.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100996
Thierry FOURNIER486f5a02015-03-16 15:13:03 +0100997.. js:function:: Channel.get(channel)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100998
999 This function returns a string that contain the entire buffer. The data is
1000 consumed from the buffer.
1001
1002 If the buffer cant receive more data, a 'nil' value is returned.
1003
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001004 :param class_channel channel: The manipulated Channel.
Pieter Baauw4d7f7662015-11-08 16:38:08 +01001005 :returns: a string containing all the available data or nil.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001006
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02001007.. js:function:: Channel.getline(channel)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001008
1009 This function returns a string that contain the first line of the buffer. The
1010 data is consumed. If the data returned doesn't contains a final '\n' its
1011 assumed than its the last available data in the buffer.
1012
1013 If the buffer cant receive more data, a 'nil' value is returned.
1014
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001015 :param class_channel channel: The manipulated Channel.
Pieter Baauw386a1272015-08-16 15:26:24 +02001016 :returns: a string containing the available line or nil.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001017
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001018.. js:function:: Channel.set(channel, string)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001019
1020 This function replace the content of the buffer by the string. The function
1021 returns the copied length, otherwise, it returns -1.
1022
1023 The data set with this function are not send. They wait for the end of
1024 HAProxy processing, so the buffer can be full.
1025
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001026 :param class_channel channel: The manipulated Channel.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001027 :param string string: The data which will sent.
Pieter Baauw4d7f7662015-11-08 16:38:08 +01001028 :returns: an integer containing the amount of bytes copied or -1.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001029
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001030.. js:function:: Channel.append(channel, string)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001031
1032 This function append the string argument to the content of the buffer. The
1033 function returns the copied length, otherwise, it returns -1.
1034
1035 The data set with this function are not send. They wait for the end of
1036 HAProxy processing, so the buffer can be full.
1037
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001038 :param class_channel channel: The manipulated Channel.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001039 :param string string: The data which will sent.
Pieter Baauw4d7f7662015-11-08 16:38:08 +01001040 :returns: an integer containing the amount of bytes copied or -1.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001041
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001042.. js:function:: Channel.send(channel, string)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001043
1044 This function required immediate send of the data. Unless if the connection
1045 is close, the buffer is regularly flushed and all the string can be sent.
1046
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001047 :param class_channel channel: The manipulated Channel.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001048 :param string string: The data which will sent.
Pieter Baauw4d7f7662015-11-08 16:38:08 +01001049 :returns: an integer containing the amount of bytes copied or -1.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001050
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001051.. js:function:: Channel.get_in_length(channel)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001052
1053 This function returns the length of the input part of the buffer.
1054
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001055 :param class_channel channel: The manipulated Channel.
Pieter Baauw4d7f7662015-11-08 16:38:08 +01001056 :returns: an integer containing the amount of available bytes.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001057
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001058.. js:function:: Channel.get_out_length(channel)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001059
1060 This function returns the length of the output part of the buffer.
1061
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001062 :param class_channel channel: The manipulated Channel.
Pieter Baauw4d7f7662015-11-08 16:38:08 +01001063 :returns: an integer containing the amount of available bytes.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001064
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001065.. js:function:: Channel.forward(channel, int)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001066
1067 This function transfer bytes from the input part of the buffer to the output
1068 part.
1069
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001070 :param class_channel channel: The manipulated Channel.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001071 :param integer int: The amount of data which will be forwarded.
1072
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001073
1074.. _http_class:
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001075
1076HTTP class
1077==========
1078
1079.. js:class:: HTTP
1080
1081 This class contain all the HTTP manipulation functions.
1082
Pieter Baauw386a1272015-08-16 15:26:24 +02001083.. js:function:: HTTP.req_get_headers(http)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001084
1085 Returns an array containing all the request headers.
1086
1087 :param class_http http: The related http object.
1088 :returns: array of headers.
Pieter Baauw386a1272015-08-16 15:26:24 +02001089 :see: HTTP.res_get_headers()
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001090
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001091 This is the form of the returned array:
1092
1093.. code-block:: lua
1094
1095 HTTP:req_get_headers()['<header-name>'][<header-index>] = "<header-value>"
1096
1097 local hdr = HTTP:req_get_headers()
1098 hdr["host"][0] = "www.test.com"
1099 hdr["accept"][0] = "audio/basic q=1"
1100 hdr["accept"][1] = "audio/*, q=0.2"
1101 hdr["accept"][2] = "*/*, q=0.1"
1102..
1103
Pieter Baauw386a1272015-08-16 15:26:24 +02001104.. js:function:: HTTP.res_get_headers(http)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001105
1106 Returns an array containing all the response headers.
1107
1108 :param class_http http: The related http object.
1109 :returns: array of headers.
Pieter Baauw386a1272015-08-16 15:26:24 +02001110 :see: HTTP.req_get_headers()
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001111
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001112 This is the form of the returned array:
1113
1114.. code-block:: lua
1115
1116 HTTP:res_get_headers()['<header-name>'][<header-index>] = "<header-value>"
1117
1118 local hdr = HTTP:req_get_headers()
1119 hdr["host"][0] = "www.test.com"
1120 hdr["accept"][0] = "audio/basic q=1"
1121 hdr["accept"][1] = "audio/*, q=0.2"
1122 hdr["accept"][2] = "*.*, q=0.1"
1123..
1124
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001125.. js:function:: HTTP.req_add_header(http, name, value)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001126
1127 Appends an HTTP header field in the request whose name is
1128 specified in "name" and whose value is defined in "value".
1129
1130 :param class_http http: The related http object.
1131 :param string name: The header name.
1132 :param string value: The header value.
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001133 :see: HTTP.res_add_header()
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001134
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001135.. js:function:: HTTP.res_add_header(http, name, value)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001136
1137 appends an HTTP header field in the response whose name is
1138 specified in "name" and whose value is defined in "value".
1139
1140 :param class_http http: The related http object.
1141 :param string name: The header name.
1142 :param string value: The header value.
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001143 :see: HTTP.req_add_header()
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001144
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001145.. js:function:: HTTP.req_del_header(http, name)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001146
1147 Removes all HTTP header fields in the request whose name is
1148 specified in "name".
1149
1150 :param class_http http: The related http object.
1151 :param string name: The header name.
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001152 :see: HTTP.res_del_header()
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001153
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001154.. js:function:: HTTP.res_del_header(http, name)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001155
1156 Removes all HTTP header fields in the response whose name is
1157 specified in "name".
1158
1159 :param class_http http: The related http object.
1160 :param string name: The header name.
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001161 :see: HTTP.req_del_header()
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001162
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001163.. js:function:: HTTP.req_set_header(http, name, value)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001164
1165 This variable replace all occurence of all header "name", by only
1166 one containing the "value".
1167
1168 :param class_http http: The related http object.
1169 :param string name: The header name.
1170 :param string value: The header value.
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001171 :see: HTTP.res_set_header()
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001172
1173 This function does the same work as the folowwing code:
1174
1175.. code-block:: lua
1176
1177 function fcn(txn)
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001178 TXN.http:req_del_header("header")
1179 TXN.http:req_add_header("header", "value")
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001180 end
1181..
1182
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001183.. js:function:: HTTP.res_set_header(http, name, value)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001184
1185 This variable replace all occurence of all header "name", by only
1186 one containing the "value".
1187
1188 :param class_http http: The related http object.
1189 :param string name: The header name.
1190 :param string value: The header value.
Pieter Baauw386a1272015-08-16 15:26:24 +02001191 :see: HTTP.req_rep_header()
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001192
Pieter Baauw386a1272015-08-16 15:26:24 +02001193.. js:function:: HTTP.req_rep_header(http, name, regex, replace)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001194
1195 Matches the regular expression in all occurrences of header field "name"
1196 according to "regex", and replaces them with the "replace" argument. The
1197 replacement value can contain back references like \1, \2, ... This
1198 function works with the request.
1199
1200 :param class_http http: The related http object.
1201 :param string name: The header name.
1202 :param string regex: The match regular expression.
1203 :param string replace: The replacement value.
Pieter Baauw386a1272015-08-16 15:26:24 +02001204 :see: HTTP.res_rep_header()
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001205
Pieter Baauw386a1272015-08-16 15:26:24 +02001206.. js:function:: HTTP.res_rep_header(http, name, regex, string)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001207
1208 Matches the regular expression in all occurrences of header field "name"
1209 according to "regex", and replaces them with the "replace" argument. The
1210 replacement value can contain back references like \1, \2, ... This
1211 function works with the request.
1212
1213 :param class_http http: The related http object.
1214 :param string name: The header name.
1215 :param string regex: The match regular expression.
1216 :param string replace: The replacement value.
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001217 :see: HTTP.req_replace_header()
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001218
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001219.. js:function:: HTTP.req_replace_value(http, name, regex, replace)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001220
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001221 Works like "HTTP.req_replace_header()" except that it matches the regex
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001222 against every comma-delimited value of the header field "name" instead of the
1223 entire header.
1224
1225 :param class_http http: The related http object.
1226 :param string name: The header name.
1227 :param string regex: The match regular expression.
1228 :param string replace: The replacement value.
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001229 :see: HTTP.req_replace_header()
1230 :see: HTTP.res_replace_value()
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001231
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001232.. js:function:: HTTP.res_replace_value(http, name, regex, replace)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001233
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001234 Works like "HTTP.res_replace_header()" except that it matches the regex
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001235 against every comma-delimited value of the header field "name" instead of the
1236 entire header.
1237
1238 :param class_http http: The related http object.
1239 :param string name: The header name.
1240 :param string regex: The match regular expression.
1241 :param string replace: The replacement value.
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001242 :see: HTTP.res_replace_header()
1243 :see: HTTP.req_replace_value()
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001244
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001245.. js:function:: HTTP.req_set_method(http, method)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001246
1247 Rewrites the request method with the parameter "method".
1248
1249 :param class_http http: The related http object.
1250 :param string method: The new method.
1251
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001252.. js:function:: HTTP.req_set_path(http, path)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001253
1254 Rewrites the request path with the "path" parameter.
1255
1256 :param class_http http: The related http object.
1257 :param string path: The new path.
1258
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001259.. js:function:: HTTP.req_set_query(http, query)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001260
1261 Rewrites the request's query string which appears after the first question
1262 mark ("?") with the parameter "query".
1263
1264 :param class_http http: The related http object.
1265 :param string query: The new query.
1266
Thierry FOURNIER0d79cf62015-08-26 14:20:58 +02001267.. js:function:: HTTP.req_set_uri(http, uri)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001268
1269 Rewrites the request URI with the parameter "uri".
1270
1271 :param class_http http: The related http object.
1272 :param string uri: The new uri.
1273
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +02001274.. js:function:: HTTP.res_set_status(http, status)
1275
1276 Rewrites the response status code with the parameter "code". Note that the
1277 reason is automatically adapted to the new code.
1278
1279 :param class_http http: The related http object.
1280 :param integer status: The new response status code.
1281
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001282.. _txn_class:
1283
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001284TXN class
1285=========
1286
1287.. js:class:: TXN
1288
1289 The txn class contain all the functions relative to the http or tcp
1290 transaction (Note than a tcp stream is the same than a tcp transaction, but
1291 an HTTP transaction is not the same than a tcp stream).
1292
1293 The usage of this class permits to retrieve data from the requests, alter it
1294 and forward it.
1295
1296 All the functions provided by this class are available in the context
1297 **sample-fetches** and **actions**.
1298
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001299.. js:attribute:: TXN.c
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001300
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001301 :returns: An :ref:`converters_class`.
1302
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001303 This attribute contains a Converters class object.
1304
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001305.. js:attribute:: TXN.sc
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001306
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001307 :returns: An :ref:`converters_class`.
1308
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001309 This attribute contains a Converters class object. The functions of
1310 this object returns always a string.
1311
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001312.. js:attribute:: TXN.f
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001313
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001314 :returns: An :ref:`fetches_class`.
1315
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001316 This attribute contains a Fetches class object.
1317
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001318.. js:attribute:: TXN.sf
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001319
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001320 :returns: An :ref:`fetches_class`.
1321
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001322 This attribute contains a Fetches class object. The functions of
1323 this object returns always a string.
1324
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001325.. js:attribute:: TXN.req
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001326
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001327 :returns: An :ref:`channel_class`.
1328
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001329 This attribute contains a channel class object for the request buffer.
1330
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001331.. js:attribute:: TXN.res
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001332
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001333 :returns: An :ref:`channel_class`.
1334
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001335 This attribute contains a channel class object for the response buffer.
1336
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001337.. js:attribute:: TXN.http
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001338
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001339 :returns: An :ref:`http_class`.
1340
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001341 This attribute contains an HTTP class object. It is avalaible only if the
1342 proxy has the "mode http" enabled.
1343
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01001344.. js:function:: TXN.log(TXN, loglevel, msg)
1345
1346 This function sends a log. The log is sent, according with the HAProxy
1347 configuration file, on the default syslog server if it is configured and on
1348 the stderr if it is allowed.
1349
1350 :param class_txn txn: The class txn object containing the data.
1351 :param integer loglevel: Is the log level asociated with the message. It is a
1352 number between 0 and 7.
1353 :param string msg: The log content.
1354 :see: core.emerg, core.alert, core.crit, core.err, core.warning, core.notice,
1355 core.info, core.debug (log level definitions)
1356 :see: TXN.deflog
1357 :see: TXN.Debug
1358 :see: TXN.Info
1359 :see: TXN.Warning
1360 :see: TXN.Alert
1361
1362.. js:function:: TXN.deflog(TXN, msg)
1363
1364 Sends a log line with the default loglevel for the proxy ssociated with the
1365 transaction.
1366
1367 :param class_txn txn: The class txn object containing the data.
1368 :param string msg: The log content.
1369 :see: TXN.log
1370
1371.. js:function:: TXN.Debug(txn, msg)
1372
1373 :param class_txn txn: The class txn object containing the data.
1374 :param string msg: The log content.
1375 :see: TXN.log
1376
1377 Does the same job than:
1378
1379.. code-block:: lua
1380
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001381 function Debug(txn, msg)
1382 TXN.log(txn, core.debug, msg)
1383 end
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01001384..
1385
1386.. js:function:: TXN.Info(txn, msg)
1387
1388 :param class_txn txn: The class txn object containing the data.
1389 :param string msg: The log content.
1390 :see: TXN.log
1391
1392.. code-block:: lua
1393
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001394 function Debug(txn, msg)
1395 TXN.log(txn, core.info, msg)
1396 end
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01001397..
1398
1399.. js:function:: TXN.Warning(txn, msg)
1400
1401 :param class_txn txn: The class txn object containing the data.
1402 :param string msg: The log content.
1403 :see: TXN.log
1404
1405.. code-block:: lua
1406
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001407 function Debug(txn, msg)
1408 TXN.log(txn, core.warning, msg)
1409 end
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01001410..
1411
1412.. js:function:: TXN.Alert(txn, msg)
1413
1414 :param class_txn txn: The class txn object containing the data.
1415 :param string msg: The log content.
1416 :see: TXN.log
1417
1418.. code-block:: lua
1419
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001420 function Debug(txn, msg)
1421 TXN.log(txn, core.alert, msg)
1422 end
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01001423..
1424
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001425.. js:function:: TXN.get_priv(txn)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001426
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001427 Return Lua data stored in the current transaction (with the `TXN.set_priv()`)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001428 function. If no data are stored, it returns a nil value.
1429
1430 :param class_txn txn: The class txn object containing the data.
1431 :returns: the opaque data previsously stored, or nil if nothing is
1432 avalaible.
1433
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001434.. js:function:: TXN.set_priv(txn, data)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001435
1436 Store any data in the current HAProxy transaction. This action replace the
1437 old stored data.
1438
1439 :param class_txn txn: The class txn object containing the data.
1440 :param opaque data: The data which is stored in the transaction.
1441
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +02001442.. js:function:: TXN.set_var(TXN, var, value)
1443
David Carlier61fdf8b2015-10-02 11:59:38 +01001444 Converts a Lua type in a HAProxy type and store it in a variable <var>.
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +02001445
1446 :param class_txn txn: The class txn object containing the data.
1447 :param string var: The variable name according with the HAProxy variable syntax.
1448 :param opaque value: The data which is stored in the variable.
1449
1450.. js:function:: TXN.get_var(TXN, var)
1451
1452 Returns data stored in the variable <var> converter in Lua type.
1453
1454 :param class_txn txn: The class txn object containing the data.
1455 :param string var: The variable name according with the HAProxy variable syntax.
1456
Willy Tarreaubc183a62015-08-28 10:39:11 +02001457.. js:function:: TXN.done(txn)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001458
Willy Tarreaubc183a62015-08-28 10:39:11 +02001459 This function terminates processing of the transaction and the associated
1460 session. It can be used when a critical error is detected or to terminate
1461 processing after some data have been returned to the client (eg: a redirect).
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001462
1463 :param class_txn txn: The class txn object containing the data.
1464
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001465.. js:function:: TXN.set_loglevel(txn, loglevel)
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01001466
1467 Is used to change the log level of the current request. The "loglevel" must
1468 be an integer between 0 and 7.
1469
1470 :param class_txn txn: The class txn object containing the data.
1471 :param integer loglevel: The required log level. This variable can be one of
1472 :see: core.<loglevel>
1473
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001474.. js:function:: TXN.set_tos(txn, tos)
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01001475
1476 Is used to set the TOS or DSCP field value of packets sent to the client to
1477 the value passed in "tos" on platforms which support this.
1478
1479 :param class_txn txn: The class txn object containing the data.
1480 :param integer tos: The new TOS os DSCP.
1481
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001482.. js:function:: TXN.set_mark(txn, mark)
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01001483
1484 Is used to set the Netfilter MARK on all packets sent to the client to the
1485 value passed in "mark" on platforms which support it.
1486
1487 :param class_txn txn: The class txn object containing the data.
1488 :param integer mark: The mark value.
1489
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001490.. _socket_class:
1491
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001492Socket class
1493============
1494
1495.. js:class:: Socket
1496
1497 This class must be compatible with the Lua Socket class. Only the 'client'
1498 functions are available. See the Lua Socket documentation:
1499
1500 `http://w3.impa.br/~diego/software/luasocket/tcp.html
1501 <http://w3.impa.br/~diego/software/luasocket/tcp.html>`_
1502
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001503.. js:function:: Socket.close(socket)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001504
1505 Closes a TCP object. The internal socket used by the object is closed and the
1506 local address to which the object was bound is made available to other
1507 applications. No further operations (except for further calls to the close
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001508 method) are allowed on a closed Socket.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001509
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001510 :param class_socket socket: Is the manipulated Socket.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001511
1512 Note: It is important to close all used sockets once they are not needed,
1513 since, in many systems, each socket uses a file descriptor, which are limited
1514 system resources. Garbage-collected objects are automatically closed before
1515 destruction, though.
1516
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02001517.. js:function:: Socket.connect(socket, address[, port])
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001518
1519 Attempts to connect a socket object to a remote host.
1520
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001521
1522 In case of error, the method returns nil followed by a string describing the
1523 error. In case of success, the method returns 1.
1524
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001525 :param class_socket socket: Is the manipulated Socket.
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02001526 :param string address: can be an IP address or a host name. See below for more
1527 information.
1528 :param integer port: must be an integer number in the range [1..64K].
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001529 :returns: 1 or nil.
1530
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02001531 an address field extension permits to use the connect() function to connect to
1532 other stream than TCP. The syntax containing a simpleipv4 or ipv6 address is
1533 the basically expected format. This format requires the port.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001534
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02001535 Other format accepted are a socket path like "/socket/path", it permits to
1536 connect to a socket. abstract namespaces are supported with the prefix
1537 "abns@", and finaly a filedescriotr can be passed with the prefix "fd@".
1538 The prefix "ipv4@", "ipv6@" and "unix@" are also supported. The port can be
1539 passed int the string. The syntax "127.0.0.1:1234" is valid. in this case, the
1540 parameter *port* is ignored.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001541
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001542.. js:function:: Socket.connect_ssl(socket, address, port)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001543
1544 Same behavior than the function socket:connect, but uses SSL.
1545
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001546 :param class_socket socket: Is the manipulated Socket.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001547 :returns: 1 or nil.
1548
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001549.. js:function:: Socket.getpeername(socket)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001550
1551 Returns information about the remote side of a connected client object.
1552
1553 Returns a string with the IP address of the peer, followed by the port number
1554 that peer is using for the connection. In case of error, the method returns
1555 nil.
1556
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001557 :param class_socket socket: Is the manipulated Socket.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001558 :returns: a string containing the server information.
1559
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001560.. js:function:: Socket.getsockname(socket)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001561
1562 Returns the local address information associated to the object.
1563
1564 The method returns a string with local IP address and a number with the port.
1565 In case of error, the method returns nil.
1566
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001567 :param class_socket socket: Is the manipulated Socket.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001568 :returns: a string containing the client information.
1569
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001570.. js:function:: Socket.receive(socket, [pattern [, prefix]])
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001571
1572 Reads data from a client object, according to the specified read pattern.
1573 Patterns follow the Lua file I/O format, and the difference in performance
1574 between all patterns is negligible.
1575
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001576 :param class_socket socket: Is the manipulated Socket.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001577 :param string|integer pattern: Describe what is required (see below).
1578 :param string prefix: A string which will be prefix the returned data.
1579 :returns: a string containing the required data or nil.
1580
1581 Pattern can be any of the following:
1582
1583 * **`*a`**: reads from the socket until the connection is closed. No
1584 end-of-line translation is performed;
1585
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001586 * **`*l`**: reads a line of text from the Socket. The line is terminated by a
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001587 LF character (ASCII 10), optionally preceded by a CR character
1588 (ASCII 13). The CR and LF characters are not included in the
1589 returned line. In fact, all CR characters are ignored by the
1590 pattern. This is the default pattern.
1591
1592 * **number**: causes the method to read a specified number of bytes from the
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001593 Socket. Prefix is an optional string to be concatenated to the
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001594 beginning of any received data before return.
1595
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02001596 * **empty**: If the pattern is left empty, the default option is `*l`.
1597
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001598 If successful, the method returns the received pattern. In case of error, the
1599 method returns nil followed by an error message which can be the string
1600 'closed' in case the connection was closed before the transmission was
1601 completed or the string 'timeout' in case there was a timeout during the
1602 operation. Also, after the error message, the function returns the partial
1603 result of the transmission.
1604
1605 Important note: This function was changed severely. It used to support
1606 multiple patterns (but I have never seen this feature used) and now it
1607 doesn't anymore. Partial results used to be returned in the same way as
1608 successful results. This last feature violated the idea that all functions
1609 should return nil on error. Thus it was changed too.
1610
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001611.. js:function:: Socket.send(socket, data [, start [, end ]])
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001612
1613 Sends data through client object.
1614
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001615 :param class_socket socket: Is the manipulated Socket.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001616 :param string data: The data that will be sent.
1617 :param integer start: The start position in the buffer of the data which will
1618 be sent.
1619 :param integer end: The end position in the buffer of the data which will
1620 be sent.
1621 :returns: see below.
1622
1623 Data is the string to be sent. The optional arguments i and j work exactly
1624 like the standard string.sub Lua function to allow the selection of a
1625 substring to be sent.
1626
1627 If successful, the method returns the index of the last byte within [start,
1628 end] that has been sent. Notice that, if start is 1 or absent, this is
1629 effectively the total number of bytes sent. In case of error, the method
1630 returns nil, followed by an error message, followed by the index of the last
1631 byte within [start, end] that has been sent. You might want to try again from
1632 the byte following that. The error message can be 'closed' in case the
1633 connection was closed before the transmission was completed or the string
1634 'timeout' in case there was a timeout during the operation.
1635
1636 Note: Output is not buffered. For small strings, it is always better to
1637 concatenate them in Lua (with the '..' operator) and send the result in one
1638 call instead of calling the method several times.
1639
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001640.. js:function:: Socket.setoption(socket, option [, value])
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001641
1642 Just implemented for compatibility, this cal does nothing.
1643
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001644.. js:function:: Socket.settimeout(socket, value [, mode])
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001645
1646 Changes the timeout values for the object. All I/O operations are blocking.
1647 That is, any call to the methods send, receive, and accept will block
1648 indefinitely, until the operation completes. The settimeout method defines a
1649 limit on the amount of time the I/O methods can block. When a timeout time
1650 has elapsed, the affected methods give up and fail with an error code.
1651
1652 The amount of time to wait is specified as the value parameter, in seconds.
1653
1654 The timeout modes are bot implemented, the only settable timeout is the
1655 inactivity time waiting for complete the internal buffer send or waiting for
1656 receive data.
1657
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001658 :param class_socket socket: Is the manipulated Socket.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001659 :param integer value: The timeout value.
1660
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001661.. _map_class:
1662
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001663Map class
1664=========
1665
1666.. js:class:: Map
1667
1668 This class permits to do some lookup in HAProxy maps. The declared maps can
1669 be modified during the runtime throught the HAProxy management socket.
1670
1671.. code-block:: lua
1672
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001673 default = "usa"
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001674
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001675 -- Create and load map
1676 geo = Map.new("geo.map", Map.ip);
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001677
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001678 -- Create new fetch that returns the user country
1679 core.register_fetches("country", function(txn)
1680 local src;
1681 local loc;
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001682
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001683 src = txn.f:fhdr("x-forwarded-for");
1684 if (src == nil) then
1685 src = txn.f:src()
1686 if (src == nil) then
1687 return default;
1688 end
1689 end
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001690
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001691 -- Perform lookup
1692 loc = geo:lookup(src);
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001693
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001694 if (loc == nil) then
1695 return default;
1696 end
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001697
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001698 return loc;
1699 end);
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001700
1701.. js:attribute:: Map.int
1702
1703 See the HAProxy configuration.txt file, chapter "Using ACLs and fetching
1704 samples" ans subchapter "ACL basics" to understand this pattern matching
1705 method.
1706
1707.. js:attribute:: Map.ip
1708
1709 See the HAProxy configuration.txt file, chapter "Using ACLs and fetching
1710 samples" ans subchapter "ACL basics" to understand this pattern matching
1711 method.
1712
1713.. js:attribute:: Map.str
1714
1715 See the HAProxy configuration.txt file, chapter "Using ACLs and fetching
1716 samples" ans subchapter "ACL basics" to understand this pattern matching
1717 method.
1718
1719.. js:attribute:: Map.beg
1720
1721 See the HAProxy configuration.txt file, chapter "Using ACLs and fetching
1722 samples" ans subchapter "ACL basics" to understand this pattern matching
1723 method.
1724
1725.. js:attribute:: Map.sub
1726
1727 See the HAProxy configuration.txt file, chapter "Using ACLs and fetching
1728 samples" ans subchapter "ACL basics" to understand this pattern matching
1729 method.
1730
1731.. js:attribute:: Map.dir
1732
1733 See the HAProxy configuration.txt file, chapter "Using ACLs and fetching
1734 samples" ans subchapter "ACL basics" to understand this pattern matching
1735 method.
1736
1737.. js:attribute:: Map.dom
1738
1739 See the HAProxy configuration.txt file, chapter "Using ACLs and fetching
1740 samples" ans subchapter "ACL basics" to understand this pattern matching
1741 method.
1742
1743.. js:attribute:: Map.end
1744
1745 See the HAProxy configuration.txt file, chapter "Using ACLs and fetching
1746 samples" ans subchapter "ACL basics" to understand this pattern matching
1747 method.
1748
1749.. js:attribute:: Map.reg
1750
1751 See the HAProxy configuration.txt file, chapter "Using ACLs and fetching
1752 samples" ans subchapter "ACL basics" to understand this pattern matching
1753 method.
1754
1755
1756.. js:function:: Map.new(file, method)
1757
1758 Creates and load a map.
1759
1760 :param string file: Is the file containing the map.
1761 :param integer method: Is the map pattern matching method. See the attributes
1762 of the Map class.
1763 :returns: a class Map object.
1764 :see: The Map attributes.
1765
1766.. js:function:: Map.lookup(map, str)
1767
1768 Perform a lookup in a map.
1769
1770 :param class_map map: Is the class Map object.
1771 :param string str: Is the string used as key.
1772 :returns: a string containing the result or nil if no match.
1773
1774.. js:function:: Map.slookup(map, str)
1775
1776 Perform a lookup in a map.
1777
1778 :param class_map map: Is the class Map object.
1779 :param string str: Is the string used as key.
1780 :returns: a string containing the result or empty string if no match.
1781
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001782.. _applethttp_class:
1783
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02001784AppletHTTP class
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001785================
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02001786
1787.. js:class:: AppletHTTP
1788
1789 This class is used with applets that requires the 'http' mode. The http applet
1790 can be registered with the *core.register_service()* function. They are used
1791 for processing an http request like a server in back of HAProxy.
1792
1793 This is an hello world sample code:
1794
1795.. code-block:: lua
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001796
Pieter Baauw4d7f7662015-11-08 16:38:08 +01001797 core.register_service("hello-world", "http", function(applet)
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02001798 local response = "Hello World !"
1799 applet:set_status(200)
Pieter Baauw2dcb9bc2015-10-01 22:47:12 +02001800 applet:add_header("content-length", string.len(response))
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02001801 applet:add_header("content-type", "text/plain")
Pieter Baauw2dcb9bc2015-10-01 22:47:12 +02001802 applet:start_response()
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02001803 applet:send(response)
1804 end)
1805
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001806.. js:attribute:: AppletHTTP.c
1807
1808 :returns: A :ref:`converters_class`
1809
1810 This attribute contains a Converters class object.
1811
1812.. js:attribute:: AppletHTTP.sc
1813
1814 :returns: A :ref:`converters_class`
1815
1816 This attribute contains a Converters class object. The
1817 functions of this object returns always a string.
1818
1819.. js:attribute:: AppletHTTP.f
1820
1821 :returns: A :ref:`fetches_class`
1822
1823 This attribute contains a Fetches class object. Note that the
1824 applet execution place cannot access to a valid HAProxy core HTTP
1825 transaction, so some sample fecthes related to the HTTP dependant
1826 values (hdr, path, ...) are not available.
1827
1828.. js:attribute:: AppletHTTP.sf
1829
1830 :returns: A :ref:`fetches_class`
1831
1832 This attribute contains a Fetches class object. The functions of
1833 this object returns always a string. Note that the applet
1834 execution place cannot access to a valid HAProxy core HTTP
1835 transaction, so some sample fecthes related to the HTTP dependant
1836 values (hdr, path, ...) are not available.
1837
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01001838.. js:attribute:: AppletHTTP.method
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001839
1840 :returns: string
1841
1842 The attribute method returns a string containing the HTTP
1843 method.
1844
1845.. js:attribute:: AppletHTTP.version
1846
1847 :returns: string
1848
1849 The attribute version, returns a string containing the HTTP
1850 request version.
1851
1852.. js:attribute:: AppletHTTP.path
1853
1854 :returns: string
1855
1856 The attribute path returns a string containing the HTTP
1857 request path.
1858
1859.. js:attribute:: AppletHTTP.qs
1860
1861 :returns: string
1862
1863 The attribute qs returns a string containing the HTTP
1864 request query string.
1865
1866.. js:attribute:: AppletHTTP.length
1867
1868 :returns: integer
1869
1870 The attribute length returns an integer containing the HTTP
1871 body length.
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02001872
Thierry FOURNIER841475e2015-12-11 17:10:09 +01001873.. js:attribute:: AppletHTTP.headers
1874
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001875 :returns: array
1876
1877 The attribute headers returns an array containing the HTTP
1878 headers. The header names are always in lower case. As the header name can be
1879 encountered more than once in each request, the value is indexed with 0 as
1880 first index value. The array have this form:
1881
1882.. code-block:: lua
1883
1884 AppletHTTP.headers['<header-name>'][<header-index>] = "<header-value>"
1885
1886 AppletHTTP.headers["host"][0] = "www.test.com"
1887 AppletHTTP.headers["accept"][0] = "audio/basic q=1"
1888 AppletHTTP.headers["accept"][1] = "audio/*, q=0.2"
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01001889 AppletHTTP.headers["accept"][2] = "*/*, q=0.1"
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001890..
1891
1892.. js:attribute:: AppletHTTP.headers
1893
Thierry FOURNIER841475e2015-12-11 17:10:09 +01001894 Contains an array containing all the request headers.
1895
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01001896.. js:function:: AppletHTTP.set_status(applet, code)
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02001897
1898 This function sets the HTTP status code for the response. The allowed code are
1899 from 100 to 599.
1900
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01001901 :param class_AppletHTTP applet: An :ref:`applethttp_class`
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02001902 :param integer code: the status code returned to the client.
1903
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01001904.. js:function:: AppletHTTP.add_header(applet, name, value)
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02001905
1906 This function add an header in the response. Duplicated headers are not
1907 collapsed. The special header *content-length* is used to determinate the
1908 response length. If it not exists, a *transfer-encoding: chunked* is set, and
1909 all the write from the funcion *AppletHTTP:send()* become a chunk.
1910
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01001911 :param class_AppletHTTP applet: An :ref:`applethttp_class`
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02001912 :param string name: the header name
1913 :param string value: the header value
1914
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01001915.. js:function:: AppletHTTP.start_response(applet)
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02001916
1917 This function indicates to the HTTP engine that it can process and send the
1918 response headers. After this called we cannot add headers to the response; We
1919 cannot use the *AppletHTTP:send()* function if the
1920 *AppletHTTP:start_response()* is not called.
1921
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01001922 :param class_AppletHTTP applet: An :ref:`applethttp_class`
1923
1924.. js:function:: AppletHTTP.getline(applet)
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02001925
1926 This function returns a string containing one line from the http body. If the
1927 data returned doesn't contains a final '\\n' its assumed than its the last
1928 available data before the end of stream.
1929
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01001930 :param class_AppletHTTP applet: An :ref:`applethttp_class`
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02001931 :returns: a string. The string can be empty if we reach the end of the stream.
1932
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01001933.. js:function:: AppletHTTP.receive(applet, [size])
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02001934
1935 Reads data from the HTTP body, according to the specified read *size*. If the
1936 *size* is missing, the function tries to read all the content of the stream
1937 until the end. If the *size* is bigger than the http body, it returns the
1938 amount of data avalaible.
1939
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01001940 :param class_AppletHTTP applet: An :ref:`applethttp_class`
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02001941 :param integer size: the required read size.
1942 :returns: always return a string,the string can be empty is the connexion is
1943 closed.
1944
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01001945.. js:function:: AppletHTTP.send(applet, msg)
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02001946
1947 Send the message *msg* on the http request body.
1948
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01001949 :param class_AppletHTTP applet: An :ref:`applethttp_class`
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02001950 :param string msg: the message to send.
1951
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01001952.. js:function:: AppletHTTP.get_priv(applet)
1953
1954 Return Lua data stored in the current transaction (with the
1955 `AppletHTTP.set_priv()`) function. If no data are stored, it returns a nil
1956 value.
1957
1958 :param class_AppletHTTP applet: An :ref:`applethttp_class`
1959 :returns: the opaque data previsously stored, or nil if nothing is
1960 avalaible.
1961
1962.. js:function:: AppletHTTP.set_priv(applet, data)
1963
1964 Store any data in the current HAProxy transaction. This action replace the
1965 old stored data.
1966
1967 :param class_AppletHTTP applet: An :ref:`applethttp_class`
1968 :param opaque data: The data which is stored in the transaction.
1969
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001970.. _applettcp_class:
1971
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02001972AppletTCP class
1973===============
1974
1975.. js:class:: AppletTCP
1976
1977 This class is used with applets that requires the 'tcp' mode. The tcp applet
1978 can be registered with the *core.register_service()* function. They are used
1979 for processing a tcp stream like a server in back of HAProxy.
1980
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001981.. js:attribute:: AppletTCP.c
1982
1983 :returns: A :ref:`converters_class`
1984
1985 This attribute contains a Converters class object.
1986
1987.. js:attribute:: AppletTCP.sc
1988
1989 :returns: A :ref:`converters_class`
1990
1991 This attribute contains a Converters class object. The
1992 functions of this object returns always a string.
1993
1994.. js:attribute:: AppletTCP.f
1995
1996 :returns: A :ref:`fetches_class`
1997
1998 This attribute contains a Fetches class object.
1999
2000.. js:attribute:: AppletTCP.sf
2001
2002 :returns: A :ref:`fetches_class`
2003
2004 This attribute contains a Fetches class object.
2005
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01002006.. js:function:: AppletTCP.getline(applet)
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002007
2008 This function returns a string containing one line from the stream. If the
2009 data returned doesn't contains a final '\\n' its assumed than its the last
2010 available data before the end of stream.
2011
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01002012 :param class_AppletTCP applet: An :ref:`applettcp_class`
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002013 :returns: a string. The string can be empty if we reach the end of the stream.
2014
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01002015.. js:function:: AppletTCP.receive(applet, [size])
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002016
2017 Reads data from the TCP stream, according to the specified read *size*. If the
2018 *size* is missing, the function tries to read all the content of the stream
2019 until the end.
2020
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01002021 :param class_AppletTCP applet: An :ref:`applettcp_class`
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002022 :param integer size: the required read size.
2023 :returns: always return a string,the string can be empty is the connexion is
2024 closed.
2025
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01002026.. js:function:: AppletTCP.send(appletmsg)
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002027
2028 Send the message on the stream.
2029
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01002030 :param class_AppletTCP applet: An :ref:`applettcp_class`
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002031 :param string msg: the message to send.
2032
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01002033.. js:function:: AppletTCP.get_priv(applet)
2034
2035 Return Lua data stored in the current transaction (with the
2036 `AppletTCP.set_priv()`) function. If no data are stored, it returns a nil
2037 value.
2038
2039 :param class_AppletTCP applet: An :ref:`applettcp_class`
2040 :returns: the opaque data previsously stored, or nil if nothing is
2041 avalaible.
2042
2043.. js:function:: AppletTCP.set_priv(applet, data)
2044
2045 Store any data in the current HAProxy transaction. This action replace the
2046 old stored data.
2047
2048 :param class_AppletTCP applet: An :ref:`applettcp_class`
2049 :param opaque data: The data which is stored in the transaction.
2050
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002051External Lua libraries
2052======================
2053
2054A lot of useful lua libraries can be found here:
2055
2056* `https://lua-toolbox.com/ <https://lua-toolbox.com/>`_
2057
2058Redis acces:
2059
2060* `https://github.com/nrk/redis-lua <https://github.com/nrk/redis-lua>`_
2061
2062This is an example about the usage of the Redis library with HAProxy. Note that
2063each call of any function of this library can throw an error if the socket
2064connection fails.
2065
2066.. code-block:: lua
2067
2068 -- load the redis library
2069 local redis = require("redis");
2070
2071 function do_something(txn)
2072
2073 -- create and connect new tcp socket
2074 local tcp = core.tcp();
2075 tcp:settimeout(1);
2076 tcp:connect("127.0.0.1", 6379);
2077
2078 -- use the redis library with this new socket
2079 local client = redis.connect({socket=tcp});
2080 client:ping();
2081
2082 end
2083
2084OpenSSL:
2085
2086* `http://mkottman.github.io/luacrypto/index.html
2087 <http://mkottman.github.io/luacrypto/index.html>`_
2088
2089* `https://github.com/brunoos/luasec/wiki
2090 <https://github.com/brunoos/luasec/wiki>`_
Thierry FOURNIER2e4893c2015-03-18 13:37:27 +01002091