blob: c42b2f3085a2d805ee36e7716324614bd8f2fdb5 [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_set_method(http, method)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001220
1221 Rewrites the request method with the parameter "method".
1222
1223 :param class_http http: The related http object.
1224 :param string method: The new method.
1225
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001226.. js:function:: HTTP.req_set_path(http, path)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001227
1228 Rewrites the request path with the "path" parameter.
1229
1230 :param class_http http: The related http object.
1231 :param string path: The new path.
1232
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001233.. js:function:: HTTP.req_set_query(http, query)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001234
1235 Rewrites the request's query string which appears after the first question
1236 mark ("?") with the parameter "query".
1237
1238 :param class_http http: The related http object.
1239 :param string query: The new query.
1240
Thierry FOURNIER0d79cf62015-08-26 14:20:58 +02001241.. js:function:: HTTP.req_set_uri(http, uri)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001242
1243 Rewrites the request URI with the parameter "uri".
1244
1245 :param class_http http: The related http object.
1246 :param string uri: The new uri.
1247
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +02001248.. js:function:: HTTP.res_set_status(http, status)
1249
1250 Rewrites the response status code with the parameter "code". Note that the
1251 reason is automatically adapted to the new code.
1252
1253 :param class_http http: The related http object.
1254 :param integer status: The new response status code.
1255
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001256.. _txn_class:
1257
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001258TXN class
1259=========
1260
1261.. js:class:: TXN
1262
1263 The txn class contain all the functions relative to the http or tcp
1264 transaction (Note than a tcp stream is the same than a tcp transaction, but
1265 an HTTP transaction is not the same than a tcp stream).
1266
1267 The usage of this class permits to retrieve data from the requests, alter it
1268 and forward it.
1269
1270 All the functions provided by this class are available in the context
1271 **sample-fetches** and **actions**.
1272
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001273.. js:attribute:: TXN.c
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001274
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001275 :returns: An :ref:`converters_class`.
1276
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001277 This attribute contains a Converters class object.
1278
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001279.. js:attribute:: TXN.sc
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001280
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001281 :returns: An :ref:`converters_class`.
1282
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001283 This attribute contains a Converters class object. The functions of
1284 this object returns always a string.
1285
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001286.. js:attribute:: TXN.f
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001287
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001288 :returns: An :ref:`fetches_class`.
1289
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001290 This attribute contains a Fetches class object.
1291
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001292.. js:attribute:: TXN.sf
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001293
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001294 :returns: An :ref:`fetches_class`.
1295
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001296 This attribute contains a Fetches class object. The functions of
1297 this object returns always a string.
1298
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001299.. js:attribute:: TXN.req
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001300
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001301 :returns: An :ref:`channel_class`.
1302
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001303 This attribute contains a channel class object for the request buffer.
1304
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001305.. js:attribute:: TXN.res
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001306
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001307 :returns: An :ref:`channel_class`.
1308
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001309 This attribute contains a channel class object for the response buffer.
1310
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001311.. js:attribute:: TXN.http
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001312
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001313 :returns: An :ref:`http_class`.
1314
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001315 This attribute contains an HTTP class object. It is avalaible only if the
1316 proxy has the "mode http" enabled.
1317
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01001318.. js:function:: TXN.log(TXN, loglevel, msg)
1319
1320 This function sends a log. The log is sent, according with the HAProxy
1321 configuration file, on the default syslog server if it is configured and on
1322 the stderr if it is allowed.
1323
1324 :param class_txn txn: The class txn object containing the data.
1325 :param integer loglevel: Is the log level asociated with the message. It is a
1326 number between 0 and 7.
1327 :param string msg: The log content.
1328 :see: core.emerg, core.alert, core.crit, core.err, core.warning, core.notice,
1329 core.info, core.debug (log level definitions)
1330 :see: TXN.deflog
1331 :see: TXN.Debug
1332 :see: TXN.Info
1333 :see: TXN.Warning
1334 :see: TXN.Alert
1335
1336.. js:function:: TXN.deflog(TXN, msg)
1337
1338 Sends a log line with the default loglevel for the proxy ssociated with the
1339 transaction.
1340
1341 :param class_txn txn: The class txn object containing the data.
1342 :param string msg: The log content.
1343 :see: TXN.log
1344
1345.. js:function:: TXN.Debug(txn, msg)
1346
1347 :param class_txn txn: The class txn object containing the data.
1348 :param string msg: The log content.
1349 :see: TXN.log
1350
1351 Does the same job than:
1352
1353.. code-block:: lua
1354
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001355 function Debug(txn, msg)
1356 TXN.log(txn, core.debug, msg)
1357 end
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01001358..
1359
1360.. js:function:: TXN.Info(txn, msg)
1361
1362 :param class_txn txn: The class txn object containing the data.
1363 :param string msg: The log content.
1364 :see: TXN.log
1365
1366.. code-block:: lua
1367
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001368 function Debug(txn, msg)
1369 TXN.log(txn, core.info, msg)
1370 end
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01001371..
1372
1373.. js:function:: TXN.Warning(txn, msg)
1374
1375 :param class_txn txn: The class txn object containing the data.
1376 :param string msg: The log content.
1377 :see: TXN.log
1378
1379.. code-block:: lua
1380
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001381 function Debug(txn, msg)
1382 TXN.log(txn, core.warning, msg)
1383 end
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01001384..
1385
1386.. js:function:: TXN.Alert(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.alert, msg)
1396 end
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01001397..
1398
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001399.. js:function:: TXN.get_priv(txn)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001400
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001401 Return Lua data stored in the current transaction (with the `TXN.set_priv()`)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001402 function. If no data are stored, it returns a nil value.
1403
1404 :param class_txn txn: The class txn object containing the data.
1405 :returns: the opaque data previsously stored, or nil if nothing is
1406 avalaible.
1407
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001408.. js:function:: TXN.set_priv(txn, data)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001409
1410 Store any data in the current HAProxy transaction. This action replace the
1411 old stored data.
1412
1413 :param class_txn txn: The class txn object containing the data.
1414 :param opaque data: The data which is stored in the transaction.
1415
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +02001416.. js:function:: TXN.set_var(TXN, var, value)
1417
David Carlier61fdf8b2015-10-02 11:59:38 +01001418 Converts a Lua type in a HAProxy type and store it in a variable <var>.
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +02001419
1420 :param class_txn txn: The class txn object containing the data.
1421 :param string var: The variable name according with the HAProxy variable syntax.
Christopher Faulet85d79c92016-11-09 16:54:56 +01001422
1423.. js:function:: TXN.unset_var(TXN, var)
1424
1425 Unset the variable <var>.
1426
1427 :param class_txn txn: The class txn object containing the data.
1428 :param string var: The variable name according with the HAProxy variable syntax.
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +02001429
1430.. js:function:: TXN.get_var(TXN, var)
1431
1432 Returns data stored in the variable <var> converter in Lua type.
1433
1434 :param class_txn txn: The class txn object containing the data.
1435 :param string var: The variable name according with the HAProxy variable syntax.
1436
Willy Tarreaubc183a62015-08-28 10:39:11 +02001437.. js:function:: TXN.done(txn)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001438
Willy Tarreaubc183a62015-08-28 10:39:11 +02001439 This function terminates processing of the transaction and the associated
1440 session. It can be used when a critical error is detected or to terminate
1441 processing after some data have been returned to the client (eg: a redirect).
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001442
Thierry FOURNIERab00df62016-07-14 11:42:37 +02001443 *Warning*: It not make sense to call this function from sample-fetches. In
1444 this case the behaviour of this one is the same than core.done(): it quit
1445 the Lua execution. The transaction is really aborted only from an action
1446 registered function.
1447
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001448 :param class_txn txn: The class txn object containing the data.
1449
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001450.. js:function:: TXN.set_loglevel(txn, loglevel)
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01001451
1452 Is used to change the log level of the current request. The "loglevel" must
1453 be an integer between 0 and 7.
1454
1455 :param class_txn txn: The class txn object containing the data.
1456 :param integer loglevel: The required log level. This variable can be one of
1457 :see: core.<loglevel>
1458
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001459.. js:function:: TXN.set_tos(txn, tos)
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01001460
1461 Is used to set the TOS or DSCP field value of packets sent to the client to
1462 the value passed in "tos" on platforms which support this.
1463
1464 :param class_txn txn: The class txn object containing the data.
1465 :param integer tos: The new TOS os DSCP.
1466
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001467.. js:function:: TXN.set_mark(txn, mark)
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01001468
1469 Is used to set the Netfilter MARK on all packets sent to the client to the
1470 value passed in "mark" on platforms which support it.
1471
1472 :param class_txn txn: The class txn object containing the data.
1473 :param integer mark: The mark value.
1474
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001475.. _socket_class:
1476
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001477Socket class
1478============
1479
1480.. js:class:: Socket
1481
1482 This class must be compatible with the Lua Socket class. Only the 'client'
1483 functions are available. See the Lua Socket documentation:
1484
1485 `http://w3.impa.br/~diego/software/luasocket/tcp.html
1486 <http://w3.impa.br/~diego/software/luasocket/tcp.html>`_
1487
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001488.. js:function:: Socket.close(socket)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001489
1490 Closes a TCP object. The internal socket used by the object is closed and the
1491 local address to which the object was bound is made available to other
1492 applications. No further operations (except for further calls to the close
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001493 method) are allowed on a closed Socket.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001494
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001495 :param class_socket socket: Is the manipulated Socket.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001496
1497 Note: It is important to close all used sockets once they are not needed,
1498 since, in many systems, each socket uses a file descriptor, which are limited
1499 system resources. Garbage-collected objects are automatically closed before
1500 destruction, though.
1501
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02001502.. js:function:: Socket.connect(socket, address[, port])
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001503
1504 Attempts to connect a socket object to a remote host.
1505
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001506
1507 In case of error, the method returns nil followed by a string describing the
1508 error. In case of success, the method returns 1.
1509
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001510 :param class_socket socket: Is the manipulated Socket.
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02001511 :param string address: can be an IP address or a host name. See below for more
1512 information.
1513 :param integer port: must be an integer number in the range [1..64K].
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001514 :returns: 1 or nil.
1515
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02001516 an address field extension permits to use the connect() function to connect to
1517 other stream than TCP. The syntax containing a simpleipv4 or ipv6 address is
1518 the basically expected format. This format requires the port.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001519
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02001520 Other format accepted are a socket path like "/socket/path", it permits to
1521 connect to a socket. abstract namespaces are supported with the prefix
1522 "abns@", and finaly a filedescriotr can be passed with the prefix "fd@".
1523 The prefix "ipv4@", "ipv6@" and "unix@" are also supported. The port can be
1524 passed int the string. The syntax "127.0.0.1:1234" is valid. in this case, the
1525 parameter *port* is ignored.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001526
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001527.. js:function:: Socket.connect_ssl(socket, address, port)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001528
1529 Same behavior than the function socket:connect, but uses SSL.
1530
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001531 :param class_socket socket: Is the manipulated Socket.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001532 :returns: 1 or nil.
1533
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001534.. js:function:: Socket.getpeername(socket)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001535
1536 Returns information about the remote side of a connected client object.
1537
1538 Returns a string with the IP address of the peer, followed by the port number
1539 that peer is using for the connection. In case of error, the method returns
1540 nil.
1541
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001542 :param class_socket socket: Is the manipulated Socket.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001543 :returns: a string containing the server information.
1544
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001545.. js:function:: Socket.getsockname(socket)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001546
1547 Returns the local address information associated to the object.
1548
1549 The method returns a string with local IP address and a number with the port.
1550 In case of error, the method returns nil.
1551
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001552 :param class_socket socket: Is the manipulated Socket.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001553 :returns: a string containing the client information.
1554
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001555.. js:function:: Socket.receive(socket, [pattern [, prefix]])
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001556
1557 Reads data from a client object, according to the specified read pattern.
1558 Patterns follow the Lua file I/O format, and the difference in performance
1559 between all patterns is negligible.
1560
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001561 :param class_socket socket: Is the manipulated Socket.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001562 :param string|integer pattern: Describe what is required (see below).
1563 :param string prefix: A string which will be prefix the returned data.
1564 :returns: a string containing the required data or nil.
1565
1566 Pattern can be any of the following:
1567
1568 * **`*a`**: reads from the socket until the connection is closed. No
1569 end-of-line translation is performed;
1570
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001571 * **`*l`**: reads a line of text from the Socket. The line is terminated by a
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001572 LF character (ASCII 10), optionally preceded by a CR character
1573 (ASCII 13). The CR and LF characters are not included in the
1574 returned line. In fact, all CR characters are ignored by the
1575 pattern. This is the default pattern.
1576
1577 * **number**: causes the method to read a specified number of bytes from the
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001578 Socket. Prefix is an optional string to be concatenated to the
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001579 beginning of any received data before return.
1580
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02001581 * **empty**: If the pattern is left empty, the default option is `*l`.
1582
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001583 If successful, the method returns the received pattern. In case of error, the
1584 method returns nil followed by an error message which can be the string
1585 'closed' in case the connection was closed before the transmission was
1586 completed or the string 'timeout' in case there was a timeout during the
1587 operation. Also, after the error message, the function returns the partial
1588 result of the transmission.
1589
1590 Important note: This function was changed severely. It used to support
1591 multiple patterns (but I have never seen this feature used) and now it
1592 doesn't anymore. Partial results used to be returned in the same way as
1593 successful results. This last feature violated the idea that all functions
1594 should return nil on error. Thus it was changed too.
1595
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001596.. js:function:: Socket.send(socket, data [, start [, end ]])
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001597
1598 Sends data through client object.
1599
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001600 :param class_socket socket: Is the manipulated Socket.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001601 :param string data: The data that will be sent.
1602 :param integer start: The start position in the buffer of the data which will
1603 be sent.
1604 :param integer end: The end position in the buffer of the data which will
1605 be sent.
1606 :returns: see below.
1607
1608 Data is the string to be sent. The optional arguments i and j work exactly
1609 like the standard string.sub Lua function to allow the selection of a
1610 substring to be sent.
1611
1612 If successful, the method returns the index of the last byte within [start,
1613 end] that has been sent. Notice that, if start is 1 or absent, this is
1614 effectively the total number of bytes sent. In case of error, the method
1615 returns nil, followed by an error message, followed by the index of the last
1616 byte within [start, end] that has been sent. You might want to try again from
1617 the byte following that. The error message can be 'closed' in case the
1618 connection was closed before the transmission was completed or the string
1619 'timeout' in case there was a timeout during the operation.
1620
1621 Note: Output is not buffered. For small strings, it is always better to
1622 concatenate them in Lua (with the '..' operator) and send the result in one
1623 call instead of calling the method several times.
1624
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001625.. js:function:: Socket.setoption(socket, option [, value])
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001626
1627 Just implemented for compatibility, this cal does nothing.
1628
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001629.. js:function:: Socket.settimeout(socket, value [, mode])
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001630
1631 Changes the timeout values for the object. All I/O operations are blocking.
1632 That is, any call to the methods send, receive, and accept will block
1633 indefinitely, until the operation completes. The settimeout method defines a
1634 limit on the amount of time the I/O methods can block. When a timeout time
1635 has elapsed, the affected methods give up and fail with an error code.
1636
1637 The amount of time to wait is specified as the value parameter, in seconds.
1638
1639 The timeout modes are bot implemented, the only settable timeout is the
1640 inactivity time waiting for complete the internal buffer send or waiting for
1641 receive data.
1642
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001643 :param class_socket socket: Is the manipulated Socket.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001644 :param integer value: The timeout value.
1645
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001646.. _map_class:
1647
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001648Map class
1649=========
1650
1651.. js:class:: Map
1652
1653 This class permits to do some lookup in HAProxy maps. The declared maps can
1654 be modified during the runtime throught the HAProxy management socket.
1655
1656.. code-block:: lua
1657
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001658 default = "usa"
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001659
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001660 -- Create and load map
1661 geo = Map.new("geo.map", Map.ip);
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001662
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001663 -- Create new fetch that returns the user country
1664 core.register_fetches("country", function(txn)
1665 local src;
1666 local loc;
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001667
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001668 src = txn.f:fhdr("x-forwarded-for");
1669 if (src == nil) then
1670 src = txn.f:src()
1671 if (src == nil) then
1672 return default;
1673 end
1674 end
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001675
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001676 -- Perform lookup
1677 loc = geo:lookup(src);
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001678
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001679 if (loc == nil) then
1680 return default;
1681 end
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001682
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001683 return loc;
1684 end);
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001685
1686.. js:attribute:: Map.int
1687
1688 See the HAProxy configuration.txt file, chapter "Using ACLs and fetching
1689 samples" ans subchapter "ACL basics" to understand this pattern matching
1690 method.
1691
1692.. js:attribute:: Map.ip
1693
1694 See the HAProxy configuration.txt file, chapter "Using ACLs and fetching
1695 samples" ans subchapter "ACL basics" to understand this pattern matching
1696 method.
1697
1698.. js:attribute:: Map.str
1699
1700 See the HAProxy configuration.txt file, chapter "Using ACLs and fetching
1701 samples" ans subchapter "ACL basics" to understand this pattern matching
1702 method.
1703
1704.. js:attribute:: Map.beg
1705
1706 See the HAProxy configuration.txt file, chapter "Using ACLs and fetching
1707 samples" ans subchapter "ACL basics" to understand this pattern matching
1708 method.
1709
1710.. js:attribute:: Map.sub
1711
1712 See the HAProxy configuration.txt file, chapter "Using ACLs and fetching
1713 samples" ans subchapter "ACL basics" to understand this pattern matching
1714 method.
1715
1716.. js:attribute:: Map.dir
1717
1718 See the HAProxy configuration.txt file, chapter "Using ACLs and fetching
1719 samples" ans subchapter "ACL basics" to understand this pattern matching
1720 method.
1721
1722.. js:attribute:: Map.dom
1723
1724 See the HAProxy configuration.txt file, chapter "Using ACLs and fetching
1725 samples" ans subchapter "ACL basics" to understand this pattern matching
1726 method.
1727
1728.. js:attribute:: Map.end
1729
1730 See the HAProxy configuration.txt file, chapter "Using ACLs and fetching
1731 samples" ans subchapter "ACL basics" to understand this pattern matching
1732 method.
1733
1734.. js:attribute:: Map.reg
1735
1736 See the HAProxy configuration.txt file, chapter "Using ACLs and fetching
1737 samples" ans subchapter "ACL basics" to understand this pattern matching
1738 method.
1739
1740
1741.. js:function:: Map.new(file, method)
1742
1743 Creates and load a map.
1744
1745 :param string file: Is the file containing the map.
1746 :param integer method: Is the map pattern matching method. See the attributes
1747 of the Map class.
1748 :returns: a class Map object.
1749 :see: The Map attributes.
1750
1751.. js:function:: Map.lookup(map, str)
1752
1753 Perform a lookup in a map.
1754
1755 :param class_map map: Is the class Map object.
1756 :param string str: Is the string used as key.
1757 :returns: a string containing the result or nil if no match.
1758
1759.. js:function:: Map.slookup(map, str)
1760
1761 Perform a lookup in a map.
1762
1763 :param class_map map: Is the class Map object.
1764 :param string str: Is the string used as key.
1765 :returns: a string containing the result or empty string if no match.
1766
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001767.. _applethttp_class:
1768
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02001769AppletHTTP class
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001770================
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02001771
1772.. js:class:: AppletHTTP
1773
1774 This class is used with applets that requires the 'http' mode. The http applet
1775 can be registered with the *core.register_service()* function. They are used
1776 for processing an http request like a server in back of HAProxy.
1777
1778 This is an hello world sample code:
1779
1780.. code-block:: lua
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001781
Pieter Baauw4d7f7662015-11-08 16:38:08 +01001782 core.register_service("hello-world", "http", function(applet)
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02001783 local response = "Hello World !"
1784 applet:set_status(200)
Pieter Baauw2dcb9bc2015-10-01 22:47:12 +02001785 applet:add_header("content-length", string.len(response))
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02001786 applet:add_header("content-type", "text/plain")
Pieter Baauw2dcb9bc2015-10-01 22:47:12 +02001787 applet:start_response()
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02001788 applet:send(response)
1789 end)
1790
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001791.. js:attribute:: AppletHTTP.c
1792
1793 :returns: A :ref:`converters_class`
1794
1795 This attribute contains a Converters class object.
1796
1797.. js:attribute:: AppletHTTP.sc
1798
1799 :returns: A :ref:`converters_class`
1800
1801 This attribute contains a Converters class object. The
1802 functions of this object returns always a string.
1803
1804.. js:attribute:: AppletHTTP.f
1805
1806 :returns: A :ref:`fetches_class`
1807
1808 This attribute contains a Fetches class object. Note that the
1809 applet execution place cannot access to a valid HAProxy core HTTP
1810 transaction, so some sample fecthes related to the HTTP dependant
1811 values (hdr, path, ...) are not available.
1812
1813.. js:attribute:: AppletHTTP.sf
1814
1815 :returns: A :ref:`fetches_class`
1816
1817 This attribute contains a Fetches class object. The functions of
1818 this object returns always a string. Note that the applet
1819 execution place cannot access to a valid HAProxy core HTTP
1820 transaction, so some sample fecthes related to the HTTP dependant
1821 values (hdr, path, ...) are not available.
1822
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01001823.. js:attribute:: AppletHTTP.method
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001824
1825 :returns: string
1826
1827 The attribute method returns a string containing the HTTP
1828 method.
1829
1830.. js:attribute:: AppletHTTP.version
1831
1832 :returns: string
1833
1834 The attribute version, returns a string containing the HTTP
1835 request version.
1836
1837.. js:attribute:: AppletHTTP.path
1838
1839 :returns: string
1840
1841 The attribute path returns a string containing the HTTP
1842 request path.
1843
1844.. js:attribute:: AppletHTTP.qs
1845
1846 :returns: string
1847
1848 The attribute qs returns a string containing the HTTP
1849 request query string.
1850
1851.. js:attribute:: AppletHTTP.length
1852
1853 :returns: integer
1854
1855 The attribute length returns an integer containing the HTTP
1856 body length.
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02001857
Thierry FOURNIER841475e2015-12-11 17:10:09 +01001858.. js:attribute:: AppletHTTP.headers
1859
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001860 :returns: array
1861
1862 The attribute headers returns an array containing the HTTP
1863 headers. The header names are always in lower case. As the header name can be
1864 encountered more than once in each request, the value is indexed with 0 as
1865 first index value. The array have this form:
1866
1867.. code-block:: lua
1868
1869 AppletHTTP.headers['<header-name>'][<header-index>] = "<header-value>"
1870
1871 AppletHTTP.headers["host"][0] = "www.test.com"
1872 AppletHTTP.headers["accept"][0] = "audio/basic q=1"
1873 AppletHTTP.headers["accept"][1] = "audio/*, q=0.2"
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01001874 AppletHTTP.headers["accept"][2] = "*/*, q=0.1"
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001875..
1876
1877.. js:attribute:: AppletHTTP.headers
1878
Thierry FOURNIER841475e2015-12-11 17:10:09 +01001879 Contains an array containing all the request headers.
1880
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01001881.. js:function:: AppletHTTP.set_status(applet, code)
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02001882
1883 This function sets the HTTP status code for the response. The allowed code are
1884 from 100 to 599.
1885
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01001886 :param class_AppletHTTP applet: An :ref:`applethttp_class`
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02001887 :param integer code: the status code returned to the client.
1888
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01001889.. js:function:: AppletHTTP.add_header(applet, name, value)
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02001890
1891 This function add an header in the response. Duplicated headers are not
1892 collapsed. The special header *content-length* is used to determinate the
1893 response length. If it not exists, a *transfer-encoding: chunked* is set, and
1894 all the write from the funcion *AppletHTTP:send()* become a chunk.
1895
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01001896 :param class_AppletHTTP applet: An :ref:`applethttp_class`
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02001897 :param string name: the header name
1898 :param string value: the header value
1899
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01001900.. js:function:: AppletHTTP.start_response(applet)
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02001901
1902 This function indicates to the HTTP engine that it can process and send the
1903 response headers. After this called we cannot add headers to the response; We
1904 cannot use the *AppletHTTP:send()* function if the
1905 *AppletHTTP:start_response()* is not called.
1906
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01001907 :param class_AppletHTTP applet: An :ref:`applethttp_class`
1908
1909.. js:function:: AppletHTTP.getline(applet)
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02001910
1911 This function returns a string containing one line from the http body. If the
1912 data returned doesn't contains a final '\\n' its assumed than its the last
1913 available data before the end of stream.
1914
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01001915 :param class_AppletHTTP applet: An :ref:`applethttp_class`
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02001916 :returns: a string. The string can be empty if we reach the end of the stream.
1917
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01001918.. js:function:: AppletHTTP.receive(applet, [size])
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02001919
1920 Reads data from the HTTP body, according to the specified read *size*. If the
1921 *size* is missing, the function tries to read all the content of the stream
1922 until the end. If the *size* is bigger than the http body, it returns the
1923 amount of data avalaible.
1924
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01001925 :param class_AppletHTTP applet: An :ref:`applethttp_class`
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02001926 :param integer size: the required read size.
1927 :returns: always return a string,the string can be empty is the connexion is
1928 closed.
1929
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01001930.. js:function:: AppletHTTP.send(applet, msg)
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02001931
1932 Send the message *msg* on the http request body.
1933
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01001934 :param class_AppletHTTP applet: An :ref:`applethttp_class`
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02001935 :param string msg: the message to send.
1936
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01001937.. js:function:: AppletHTTP.get_priv(applet)
1938
1939 Return Lua data stored in the current transaction (with the
1940 `AppletHTTP.set_priv()`) function. If no data are stored, it returns a nil
1941 value.
1942
1943 :param class_AppletHTTP applet: An :ref:`applethttp_class`
1944 :returns: the opaque data previsously stored, or nil if nothing is
1945 avalaible.
1946
1947.. js:function:: AppletHTTP.set_priv(applet, data)
1948
1949 Store any data in the current HAProxy transaction. This action replace the
1950 old stored data.
1951
1952 :param class_AppletHTTP applet: An :ref:`applethttp_class`
1953 :param opaque data: The data which is stored in the transaction.
1954
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001955.. _applettcp_class:
1956
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02001957AppletTCP class
1958===============
1959
1960.. js:class:: AppletTCP
1961
1962 This class is used with applets that requires the 'tcp' mode. The tcp applet
1963 can be registered with the *core.register_service()* function. They are used
1964 for processing a tcp stream like a server in back of HAProxy.
1965
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001966.. js:attribute:: AppletTCP.c
1967
1968 :returns: A :ref:`converters_class`
1969
1970 This attribute contains a Converters class object.
1971
1972.. js:attribute:: AppletTCP.sc
1973
1974 :returns: A :ref:`converters_class`
1975
1976 This attribute contains a Converters class object. The
1977 functions of this object returns always a string.
1978
1979.. js:attribute:: AppletTCP.f
1980
1981 :returns: A :ref:`fetches_class`
1982
1983 This attribute contains a Fetches class object.
1984
1985.. js:attribute:: AppletTCP.sf
1986
1987 :returns: A :ref:`fetches_class`
1988
1989 This attribute contains a Fetches class object.
1990
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01001991.. js:function:: AppletTCP.getline(applet)
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02001992
1993 This function returns a string containing one line from the stream. If the
1994 data returned doesn't contains a final '\\n' its assumed than its the last
1995 available data before the end of stream.
1996
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01001997 :param class_AppletTCP applet: An :ref:`applettcp_class`
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02001998 :returns: a string. The string can be empty if we reach the end of the stream.
1999
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01002000.. js:function:: AppletTCP.receive(applet, [size])
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002001
2002 Reads data from the TCP stream, according to the specified read *size*. If the
2003 *size* is missing, the function tries to read all the content of the stream
2004 until the end.
2005
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01002006 :param class_AppletTCP applet: An :ref:`applettcp_class`
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002007 :param integer size: the required read size.
2008 :returns: always return a string,the string can be empty is the connexion is
2009 closed.
2010
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01002011.. js:function:: AppletTCP.send(appletmsg)
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002012
2013 Send the message on the stream.
2014
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01002015 :param class_AppletTCP applet: An :ref:`applettcp_class`
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002016 :param string msg: the message to send.
2017
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01002018.. js:function:: AppletTCP.get_priv(applet)
2019
2020 Return Lua data stored in the current transaction (with the
2021 `AppletTCP.set_priv()`) function. If no data are stored, it returns a nil
2022 value.
2023
2024 :param class_AppletTCP applet: An :ref:`applettcp_class`
2025 :returns: the opaque data previsously stored, or nil if nothing is
2026 avalaible.
2027
2028.. js:function:: AppletTCP.set_priv(applet, data)
2029
2030 Store any data in the current HAProxy transaction. This action replace the
2031 old stored data.
2032
2033 :param class_AppletTCP applet: An :ref:`applettcp_class`
2034 :param opaque data: The data which is stored in the transaction.
2035
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002036External Lua libraries
2037======================
2038
2039A lot of useful lua libraries can be found here:
2040
2041* `https://lua-toolbox.com/ <https://lua-toolbox.com/>`_
2042
2043Redis acces:
2044
2045* `https://github.com/nrk/redis-lua <https://github.com/nrk/redis-lua>`_
2046
2047This is an example about the usage of the Redis library with HAProxy. Note that
2048each call of any function of this library can throw an error if the socket
2049connection fails.
2050
2051.. code-block:: lua
2052
2053 -- load the redis library
2054 local redis = require("redis");
2055
2056 function do_something(txn)
2057
2058 -- create and connect new tcp socket
2059 local tcp = core.tcp();
2060 tcp:settimeout(1);
2061 tcp:connect("127.0.0.1", 6379);
2062
2063 -- use the redis library with this new socket
2064 local client = redis.connect({socket=tcp});
2065 client:ping();
2066
2067 end
2068
2069OpenSSL:
2070
2071* `http://mkottman.github.io/luacrypto/index.html
2072 <http://mkottman.github.io/luacrypto/index.html>`_
2073
2074* `https://github.com/brunoos/luasec/wiki
2075 <https://github.com/brunoos/luasec/wiki>`_
Thierry FOURNIER2e4893c2015-03-18 13:37:27 +01002076