blob: dccf415620f57ca9b78acb27f7aaa9500f3a5f96 [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
Thierry FOURNIERab00df62016-07-14 11:42:37 +02001463 *Warning*: It not make sense to call this function from sample-fetches. In
1464 this case the behaviour of this one is the same than core.done(): it quit
1465 the Lua execution. The transaction is really aborted only from an action
1466 registered function.
1467
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001468 :param class_txn txn: The class txn object containing the data.
1469
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001470.. js:function:: TXN.set_loglevel(txn, loglevel)
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01001471
1472 Is used to change the log level of the current request. The "loglevel" must
1473 be an integer between 0 and 7.
1474
1475 :param class_txn txn: The class txn object containing the data.
1476 :param integer loglevel: The required log level. This variable can be one of
1477 :see: core.<loglevel>
1478
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001479.. js:function:: TXN.set_tos(txn, tos)
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01001480
1481 Is used to set the TOS or DSCP field value of packets sent to the client to
1482 the value passed in "tos" on platforms which support this.
1483
1484 :param class_txn txn: The class txn object containing the data.
1485 :param integer tos: The new TOS os DSCP.
1486
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001487.. js:function:: TXN.set_mark(txn, mark)
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01001488
1489 Is used to set the Netfilter MARK on all packets sent to the client to the
1490 value passed in "mark" on platforms which support it.
1491
1492 :param class_txn txn: The class txn object containing the data.
1493 :param integer mark: The mark value.
1494
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001495.. _socket_class:
1496
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001497Socket class
1498============
1499
1500.. js:class:: Socket
1501
1502 This class must be compatible with the Lua Socket class. Only the 'client'
1503 functions are available. See the Lua Socket documentation:
1504
1505 `http://w3.impa.br/~diego/software/luasocket/tcp.html
1506 <http://w3.impa.br/~diego/software/luasocket/tcp.html>`_
1507
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001508.. js:function:: Socket.close(socket)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001509
1510 Closes a TCP object. The internal socket used by the object is closed and the
1511 local address to which the object was bound is made available to other
1512 applications. No further operations (except for further calls to the close
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001513 method) are allowed on a closed Socket.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001514
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001515 :param class_socket socket: Is the manipulated Socket.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001516
1517 Note: It is important to close all used sockets once they are not needed,
1518 since, in many systems, each socket uses a file descriptor, which are limited
1519 system resources. Garbage-collected objects are automatically closed before
1520 destruction, though.
1521
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02001522.. js:function:: Socket.connect(socket, address[, port])
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001523
1524 Attempts to connect a socket object to a remote host.
1525
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001526
1527 In case of error, the method returns nil followed by a string describing the
1528 error. In case of success, the method returns 1.
1529
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001530 :param class_socket socket: Is the manipulated Socket.
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02001531 :param string address: can be an IP address or a host name. See below for more
1532 information.
1533 :param integer port: must be an integer number in the range [1..64K].
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001534 :returns: 1 or nil.
1535
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02001536 an address field extension permits to use the connect() function to connect to
1537 other stream than TCP. The syntax containing a simpleipv4 or ipv6 address is
1538 the basically expected format. This format requires the port.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001539
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02001540 Other format accepted are a socket path like "/socket/path", it permits to
1541 connect to a socket. abstract namespaces are supported with the prefix
1542 "abns@", and finaly a filedescriotr can be passed with the prefix "fd@".
1543 The prefix "ipv4@", "ipv6@" and "unix@" are also supported. The port can be
1544 passed int the string. The syntax "127.0.0.1:1234" is valid. in this case, the
1545 parameter *port* is ignored.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001546
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001547.. js:function:: Socket.connect_ssl(socket, address, port)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001548
1549 Same behavior than the function socket:connect, but uses SSL.
1550
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001551 :param class_socket socket: Is the manipulated Socket.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001552 :returns: 1 or nil.
1553
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001554.. js:function:: Socket.getpeername(socket)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001555
1556 Returns information about the remote side of a connected client object.
1557
1558 Returns a string with the IP address of the peer, followed by the port number
1559 that peer is using for the connection. In case of error, the method returns
1560 nil.
1561
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001562 :param class_socket socket: Is the manipulated Socket.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001563 :returns: a string containing the server information.
1564
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001565.. js:function:: Socket.getsockname(socket)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001566
1567 Returns the local address information associated to the object.
1568
1569 The method returns a string with local IP address and a number with the port.
1570 In case of error, the method returns nil.
1571
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001572 :param class_socket socket: Is the manipulated Socket.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001573 :returns: a string containing the client information.
1574
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001575.. js:function:: Socket.receive(socket, [pattern [, prefix]])
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001576
1577 Reads data from a client object, according to the specified read pattern.
1578 Patterns follow the Lua file I/O format, and the difference in performance
1579 between all patterns is negligible.
1580
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001581 :param class_socket socket: Is the manipulated Socket.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001582 :param string|integer pattern: Describe what is required (see below).
1583 :param string prefix: A string which will be prefix the returned data.
1584 :returns: a string containing the required data or nil.
1585
1586 Pattern can be any of the following:
1587
1588 * **`*a`**: reads from the socket until the connection is closed. No
1589 end-of-line translation is performed;
1590
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001591 * **`*l`**: reads a line of text from the Socket. The line is terminated by a
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001592 LF character (ASCII 10), optionally preceded by a CR character
1593 (ASCII 13). The CR and LF characters are not included in the
1594 returned line. In fact, all CR characters are ignored by the
1595 pattern. This is the default pattern.
1596
1597 * **number**: causes the method to read a specified number of bytes from the
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001598 Socket. Prefix is an optional string to be concatenated to the
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001599 beginning of any received data before return.
1600
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02001601 * **empty**: If the pattern is left empty, the default option is `*l`.
1602
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001603 If successful, the method returns the received pattern. In case of error, the
1604 method returns nil followed by an error message which can be the string
1605 'closed' in case the connection was closed before the transmission was
1606 completed or the string 'timeout' in case there was a timeout during the
1607 operation. Also, after the error message, the function returns the partial
1608 result of the transmission.
1609
1610 Important note: This function was changed severely. It used to support
1611 multiple patterns (but I have never seen this feature used) and now it
1612 doesn't anymore. Partial results used to be returned in the same way as
1613 successful results. This last feature violated the idea that all functions
1614 should return nil on error. Thus it was changed too.
1615
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001616.. js:function:: Socket.send(socket, data [, start [, end ]])
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001617
1618 Sends data through client object.
1619
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001620 :param class_socket socket: Is the manipulated Socket.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001621 :param string data: The data that will be sent.
1622 :param integer start: The start position in the buffer of the data which will
1623 be sent.
1624 :param integer end: The end position in the buffer of the data which will
1625 be sent.
1626 :returns: see below.
1627
1628 Data is the string to be sent. The optional arguments i and j work exactly
1629 like the standard string.sub Lua function to allow the selection of a
1630 substring to be sent.
1631
1632 If successful, the method returns the index of the last byte within [start,
1633 end] that has been sent. Notice that, if start is 1 or absent, this is
1634 effectively the total number of bytes sent. In case of error, the method
1635 returns nil, followed by an error message, followed by the index of the last
1636 byte within [start, end] that has been sent. You might want to try again from
1637 the byte following that. The error message can be 'closed' in case the
1638 connection was closed before the transmission was completed or the string
1639 'timeout' in case there was a timeout during the operation.
1640
1641 Note: Output is not buffered. For small strings, it is always better to
1642 concatenate them in Lua (with the '..' operator) and send the result in one
1643 call instead of calling the method several times.
1644
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001645.. js:function:: Socket.setoption(socket, option [, value])
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001646
1647 Just implemented for compatibility, this cal does nothing.
1648
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001649.. js:function:: Socket.settimeout(socket, value [, mode])
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001650
1651 Changes the timeout values for the object. All I/O operations are blocking.
1652 That is, any call to the methods send, receive, and accept will block
1653 indefinitely, until the operation completes. The settimeout method defines a
1654 limit on the amount of time the I/O methods can block. When a timeout time
1655 has elapsed, the affected methods give up and fail with an error code.
1656
1657 The amount of time to wait is specified as the value parameter, in seconds.
1658
1659 The timeout modes are bot implemented, the only settable timeout is the
1660 inactivity time waiting for complete the internal buffer send or waiting for
1661 receive data.
1662
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001663 :param class_socket socket: Is the manipulated Socket.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001664 :param integer value: The timeout value.
1665
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001666.. _map_class:
1667
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001668Map class
1669=========
1670
1671.. js:class:: Map
1672
1673 This class permits to do some lookup in HAProxy maps. The declared maps can
1674 be modified during the runtime throught the HAProxy management socket.
1675
1676.. code-block:: lua
1677
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001678 default = "usa"
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001679
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001680 -- Create and load map
1681 geo = Map.new("geo.map", Map.ip);
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001682
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001683 -- Create new fetch that returns the user country
1684 core.register_fetches("country", function(txn)
1685 local src;
1686 local loc;
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001687
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001688 src = txn.f:fhdr("x-forwarded-for");
1689 if (src == nil) then
1690 src = txn.f:src()
1691 if (src == nil) then
1692 return default;
1693 end
1694 end
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001695
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001696 -- Perform lookup
1697 loc = geo:lookup(src);
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001698
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001699 if (loc == nil) then
1700 return default;
1701 end
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001702
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001703 return loc;
1704 end);
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001705
1706.. js:attribute:: Map.int
1707
1708 See the HAProxy configuration.txt file, chapter "Using ACLs and fetching
1709 samples" ans subchapter "ACL basics" to understand this pattern matching
1710 method.
1711
1712.. js:attribute:: Map.ip
1713
1714 See the HAProxy configuration.txt file, chapter "Using ACLs and fetching
1715 samples" ans subchapter "ACL basics" to understand this pattern matching
1716 method.
1717
1718.. js:attribute:: Map.str
1719
1720 See the HAProxy configuration.txt file, chapter "Using ACLs and fetching
1721 samples" ans subchapter "ACL basics" to understand this pattern matching
1722 method.
1723
1724.. js:attribute:: Map.beg
1725
1726 See the HAProxy configuration.txt file, chapter "Using ACLs and fetching
1727 samples" ans subchapter "ACL basics" to understand this pattern matching
1728 method.
1729
1730.. js:attribute:: Map.sub
1731
1732 See the HAProxy configuration.txt file, chapter "Using ACLs and fetching
1733 samples" ans subchapter "ACL basics" to understand this pattern matching
1734 method.
1735
1736.. js:attribute:: Map.dir
1737
1738 See the HAProxy configuration.txt file, chapter "Using ACLs and fetching
1739 samples" ans subchapter "ACL basics" to understand this pattern matching
1740 method.
1741
1742.. js:attribute:: Map.dom
1743
1744 See the HAProxy configuration.txt file, chapter "Using ACLs and fetching
1745 samples" ans subchapter "ACL basics" to understand this pattern matching
1746 method.
1747
1748.. js:attribute:: Map.end
1749
1750 See the HAProxy configuration.txt file, chapter "Using ACLs and fetching
1751 samples" ans subchapter "ACL basics" to understand this pattern matching
1752 method.
1753
1754.. js:attribute:: Map.reg
1755
1756 See the HAProxy configuration.txt file, chapter "Using ACLs and fetching
1757 samples" ans subchapter "ACL basics" to understand this pattern matching
1758 method.
1759
1760
1761.. js:function:: Map.new(file, method)
1762
1763 Creates and load a map.
1764
1765 :param string file: Is the file containing the map.
1766 :param integer method: Is the map pattern matching method. See the attributes
1767 of the Map class.
1768 :returns: a class Map object.
1769 :see: The Map attributes.
1770
1771.. js:function:: Map.lookup(map, str)
1772
1773 Perform a lookup in a map.
1774
1775 :param class_map map: Is the class Map object.
1776 :param string str: Is the string used as key.
1777 :returns: a string containing the result or nil if no match.
1778
1779.. js:function:: Map.slookup(map, str)
1780
1781 Perform a lookup in a map.
1782
1783 :param class_map map: Is the class Map object.
1784 :param string str: Is the string used as key.
1785 :returns: a string containing the result or empty string if no match.
1786
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001787.. _applethttp_class:
1788
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02001789AppletHTTP class
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001790================
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02001791
1792.. js:class:: AppletHTTP
1793
1794 This class is used with applets that requires the 'http' mode. The http applet
1795 can be registered with the *core.register_service()* function. They are used
1796 for processing an http request like a server in back of HAProxy.
1797
1798 This is an hello world sample code:
1799
1800.. code-block:: lua
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001801
Pieter Baauw4d7f7662015-11-08 16:38:08 +01001802 core.register_service("hello-world", "http", function(applet)
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02001803 local response = "Hello World !"
1804 applet:set_status(200)
Pieter Baauw2dcb9bc2015-10-01 22:47:12 +02001805 applet:add_header("content-length", string.len(response))
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02001806 applet:add_header("content-type", "text/plain")
Pieter Baauw2dcb9bc2015-10-01 22:47:12 +02001807 applet:start_response()
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02001808 applet:send(response)
1809 end)
1810
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001811.. js:attribute:: AppletHTTP.c
1812
1813 :returns: A :ref:`converters_class`
1814
1815 This attribute contains a Converters class object.
1816
1817.. js:attribute:: AppletHTTP.sc
1818
1819 :returns: A :ref:`converters_class`
1820
1821 This attribute contains a Converters class object. The
1822 functions of this object returns always a string.
1823
1824.. js:attribute:: AppletHTTP.f
1825
1826 :returns: A :ref:`fetches_class`
1827
1828 This attribute contains a Fetches class object. Note that the
1829 applet execution place cannot access to a valid HAProxy core HTTP
1830 transaction, so some sample fecthes related to the HTTP dependant
1831 values (hdr, path, ...) are not available.
1832
1833.. js:attribute:: AppletHTTP.sf
1834
1835 :returns: A :ref:`fetches_class`
1836
1837 This attribute contains a Fetches class object. The functions of
1838 this object returns always a string. Note that the applet
1839 execution place cannot access to a valid HAProxy core HTTP
1840 transaction, so some sample fecthes related to the HTTP dependant
1841 values (hdr, path, ...) are not available.
1842
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01001843.. js:attribute:: AppletHTTP.method
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001844
1845 :returns: string
1846
1847 The attribute method returns a string containing the HTTP
1848 method.
1849
1850.. js:attribute:: AppletHTTP.version
1851
1852 :returns: string
1853
1854 The attribute version, returns a string containing the HTTP
1855 request version.
1856
1857.. js:attribute:: AppletHTTP.path
1858
1859 :returns: string
1860
1861 The attribute path returns a string containing the HTTP
1862 request path.
1863
1864.. js:attribute:: AppletHTTP.qs
1865
1866 :returns: string
1867
1868 The attribute qs returns a string containing the HTTP
1869 request query string.
1870
1871.. js:attribute:: AppletHTTP.length
1872
1873 :returns: integer
1874
1875 The attribute length returns an integer containing the HTTP
1876 body length.
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02001877
Thierry FOURNIER841475e2015-12-11 17:10:09 +01001878.. js:attribute:: AppletHTTP.headers
1879
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001880 :returns: array
1881
1882 The attribute headers returns an array containing the HTTP
1883 headers. The header names are always in lower case. As the header name can be
1884 encountered more than once in each request, the value is indexed with 0 as
1885 first index value. The array have this form:
1886
1887.. code-block:: lua
1888
1889 AppletHTTP.headers['<header-name>'][<header-index>] = "<header-value>"
1890
1891 AppletHTTP.headers["host"][0] = "www.test.com"
1892 AppletHTTP.headers["accept"][0] = "audio/basic q=1"
1893 AppletHTTP.headers["accept"][1] = "audio/*, q=0.2"
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01001894 AppletHTTP.headers["accept"][2] = "*/*, q=0.1"
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001895..
1896
1897.. js:attribute:: AppletHTTP.headers
1898
Thierry FOURNIER841475e2015-12-11 17:10:09 +01001899 Contains an array containing all the request headers.
1900
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01001901.. js:function:: AppletHTTP.set_status(applet, code)
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02001902
1903 This function sets the HTTP status code for the response. The allowed code are
1904 from 100 to 599.
1905
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01001906 :param class_AppletHTTP applet: An :ref:`applethttp_class`
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02001907 :param integer code: the status code returned to the client.
1908
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01001909.. js:function:: AppletHTTP.add_header(applet, name, value)
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02001910
1911 This function add an header in the response. Duplicated headers are not
1912 collapsed. The special header *content-length* is used to determinate the
1913 response length. If it not exists, a *transfer-encoding: chunked* is set, and
1914 all the write from the funcion *AppletHTTP:send()* become a chunk.
1915
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01001916 :param class_AppletHTTP applet: An :ref:`applethttp_class`
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02001917 :param string name: the header name
1918 :param string value: the header value
1919
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01001920.. js:function:: AppletHTTP.start_response(applet)
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02001921
1922 This function indicates to the HTTP engine that it can process and send the
1923 response headers. After this called we cannot add headers to the response; We
1924 cannot use the *AppletHTTP:send()* function if the
1925 *AppletHTTP:start_response()* is not called.
1926
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01001927 :param class_AppletHTTP applet: An :ref:`applethttp_class`
1928
1929.. js:function:: AppletHTTP.getline(applet)
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02001930
1931 This function returns a string containing one line from the http body. If the
1932 data returned doesn't contains a final '\\n' its assumed than its the last
1933 available data before the end of stream.
1934
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01001935 :param class_AppletHTTP applet: An :ref:`applethttp_class`
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02001936 :returns: a string. The string can be empty if we reach the end of the stream.
1937
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01001938.. js:function:: AppletHTTP.receive(applet, [size])
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02001939
1940 Reads data from the HTTP body, according to the specified read *size*. If the
1941 *size* is missing, the function tries to read all the content of the stream
1942 until the end. If the *size* is bigger than the http body, it returns the
1943 amount of data avalaible.
1944
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01001945 :param class_AppletHTTP applet: An :ref:`applethttp_class`
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02001946 :param integer size: the required read size.
1947 :returns: always return a string,the string can be empty is the connexion is
1948 closed.
1949
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01001950.. js:function:: AppletHTTP.send(applet, msg)
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02001951
1952 Send the message *msg* on the http request body.
1953
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01001954 :param class_AppletHTTP applet: An :ref:`applethttp_class`
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02001955 :param string msg: the message to send.
1956
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01001957.. js:function:: AppletHTTP.get_priv(applet)
1958
1959 Return Lua data stored in the current transaction (with the
1960 `AppletHTTP.set_priv()`) function. If no data are stored, it returns a nil
1961 value.
1962
1963 :param class_AppletHTTP applet: An :ref:`applethttp_class`
1964 :returns: the opaque data previsously stored, or nil if nothing is
1965 avalaible.
1966
1967.. js:function:: AppletHTTP.set_priv(applet, data)
1968
1969 Store any data in the current HAProxy transaction. This action replace the
1970 old stored data.
1971
1972 :param class_AppletHTTP applet: An :ref:`applethttp_class`
1973 :param opaque data: The data which is stored in the transaction.
1974
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001975.. _applettcp_class:
1976
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02001977AppletTCP class
1978===============
1979
1980.. js:class:: AppletTCP
1981
1982 This class is used with applets that requires the 'tcp' mode. The tcp applet
1983 can be registered with the *core.register_service()* function. They are used
1984 for processing a tcp stream like a server in back of HAProxy.
1985
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001986.. js:attribute:: AppletTCP.c
1987
1988 :returns: A :ref:`converters_class`
1989
1990 This attribute contains a Converters class object.
1991
1992.. js:attribute:: AppletTCP.sc
1993
1994 :returns: A :ref:`converters_class`
1995
1996 This attribute contains a Converters class object. The
1997 functions of this object returns always a string.
1998
1999.. js:attribute:: AppletTCP.f
2000
2001 :returns: A :ref:`fetches_class`
2002
2003 This attribute contains a Fetches class object.
2004
2005.. js:attribute:: AppletTCP.sf
2006
2007 :returns: A :ref:`fetches_class`
2008
2009 This attribute contains a Fetches class object.
2010
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01002011.. js:function:: AppletTCP.getline(applet)
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002012
2013 This function returns a string containing one line from the stream. If the
2014 data returned doesn't contains a final '\\n' its assumed than its the last
2015 available data before the end of stream.
2016
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01002017 :param class_AppletTCP applet: An :ref:`applettcp_class`
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002018 :returns: a string. The string can be empty if we reach the end of the stream.
2019
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01002020.. js:function:: AppletTCP.receive(applet, [size])
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002021
2022 Reads data from the TCP stream, according to the specified read *size*. If the
2023 *size* is missing, the function tries to read all the content of the stream
2024 until the end.
2025
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01002026 :param class_AppletTCP applet: An :ref:`applettcp_class`
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002027 :param integer size: the required read size.
2028 :returns: always return a string,the string can be empty is the connexion is
2029 closed.
2030
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01002031.. js:function:: AppletTCP.send(appletmsg)
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002032
2033 Send the message on the stream.
2034
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01002035 :param class_AppletTCP applet: An :ref:`applettcp_class`
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002036 :param string msg: the message to send.
2037
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01002038.. js:function:: AppletTCP.get_priv(applet)
2039
2040 Return Lua data stored in the current transaction (with the
2041 `AppletTCP.set_priv()`) function. If no data are stored, it returns a nil
2042 value.
2043
2044 :param class_AppletTCP applet: An :ref:`applettcp_class`
2045 :returns: the opaque data previsously stored, or nil if nothing is
2046 avalaible.
2047
2048.. js:function:: AppletTCP.set_priv(applet, data)
2049
2050 Store any data in the current HAProxy transaction. This action replace the
2051 old stored data.
2052
2053 :param class_AppletTCP applet: An :ref:`applettcp_class`
2054 :param opaque data: The data which is stored in the transaction.
2055
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002056External Lua libraries
2057======================
2058
2059A lot of useful lua libraries can be found here:
2060
2061* `https://lua-toolbox.com/ <https://lua-toolbox.com/>`_
2062
2063Redis acces:
2064
2065* `https://github.com/nrk/redis-lua <https://github.com/nrk/redis-lua>`_
2066
2067This is an example about the usage of the Redis library with HAProxy. Note that
2068each call of any function of this library can throw an error if the socket
2069connection fails.
2070
2071.. code-block:: lua
2072
2073 -- load the redis library
2074 local redis = require("redis");
2075
2076 function do_something(txn)
2077
2078 -- create and connect new tcp socket
2079 local tcp = core.tcp();
2080 tcp:settimeout(1);
2081 tcp:connect("127.0.0.1", 6379);
2082
2083 -- use the redis library with this new socket
2084 local client = redis.connect({socket=tcp});
2085 client:ping();
2086
2087 end
2088
2089OpenSSL:
2090
2091* `http://mkottman.github.io/luacrypto/index.html
2092 <http://mkottman.github.io/luacrypto/index.html>`_
2093
2094* `https://github.com/brunoos/luasec/wiki
2095 <https://github.com/brunoos/luasec/wiki>`_
Thierry FOURNIER2e4893c2015-03-18 13:37:27 +01002096