blob: 77c22662c9119b0a4e9fb0fe02bd6ceb7284736d [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 FOURNIER / OZON.IO65192f32016-11-07 15:28:40 +01001073.. js:function:: Channel.is_full(channel)
1074
1075 This function returns true if the buffer channel is full.
1076
1077 :returns: a boolean
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001078
1079.. _http_class:
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001080
1081HTTP class
1082==========
1083
1084.. js:class:: HTTP
1085
1086 This class contain all the HTTP manipulation functions.
1087
Pieter Baauw386a1272015-08-16 15:26:24 +02001088.. js:function:: HTTP.req_get_headers(http)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001089
1090 Returns an array containing all the request headers.
1091
1092 :param class_http http: The related http object.
1093 :returns: array of headers.
Pieter Baauw386a1272015-08-16 15:26:24 +02001094 :see: HTTP.res_get_headers()
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001095
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001096 This is the form of the returned array:
1097
1098.. code-block:: lua
1099
1100 HTTP:req_get_headers()['<header-name>'][<header-index>] = "<header-value>"
1101
1102 local hdr = HTTP:req_get_headers()
1103 hdr["host"][0] = "www.test.com"
1104 hdr["accept"][0] = "audio/basic q=1"
1105 hdr["accept"][1] = "audio/*, q=0.2"
1106 hdr["accept"][2] = "*/*, q=0.1"
1107..
1108
Pieter Baauw386a1272015-08-16 15:26:24 +02001109.. js:function:: HTTP.res_get_headers(http)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001110
1111 Returns an array containing all the response headers.
1112
1113 :param class_http http: The related http object.
1114 :returns: array of headers.
Pieter Baauw386a1272015-08-16 15:26:24 +02001115 :see: HTTP.req_get_headers()
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001116
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001117 This is the form of the returned array:
1118
1119.. code-block:: lua
1120
1121 HTTP:res_get_headers()['<header-name>'][<header-index>] = "<header-value>"
1122
1123 local hdr = HTTP:req_get_headers()
1124 hdr["host"][0] = "www.test.com"
1125 hdr["accept"][0] = "audio/basic q=1"
1126 hdr["accept"][1] = "audio/*, q=0.2"
1127 hdr["accept"][2] = "*.*, q=0.1"
1128..
1129
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001130.. js:function:: HTTP.req_add_header(http, name, value)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001131
1132 Appends an HTTP header field in the request whose name is
1133 specified in "name" and whose value is defined in "value".
1134
1135 :param class_http http: The related http object.
1136 :param string name: The header name.
1137 :param string value: The header value.
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001138 :see: HTTP.res_add_header()
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001139
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001140.. js:function:: HTTP.res_add_header(http, name, value)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001141
1142 appends an HTTP header field in the response whose name is
1143 specified in "name" and whose value is defined in "value".
1144
1145 :param class_http http: The related http object.
1146 :param string name: The header name.
1147 :param string value: The header value.
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001148 :see: HTTP.req_add_header()
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001149
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001150.. js:function:: HTTP.req_del_header(http, name)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001151
1152 Removes all HTTP header fields in the request whose name is
1153 specified in "name".
1154
1155 :param class_http http: The related http object.
1156 :param string name: The header name.
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001157 :see: HTTP.res_del_header()
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001158
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001159.. js:function:: HTTP.res_del_header(http, name)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001160
1161 Removes all HTTP header fields in the response whose name is
1162 specified in "name".
1163
1164 :param class_http http: The related http object.
1165 :param string name: The header name.
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001166 :see: HTTP.req_del_header()
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001167
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001168.. js:function:: HTTP.req_set_header(http, name, value)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001169
1170 This variable replace all occurence of all header "name", by only
1171 one containing the "value".
1172
1173 :param class_http http: The related http object.
1174 :param string name: The header name.
1175 :param string value: The header value.
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001176 :see: HTTP.res_set_header()
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001177
1178 This function does the same work as the folowwing code:
1179
1180.. code-block:: lua
1181
1182 function fcn(txn)
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001183 TXN.http:req_del_header("header")
1184 TXN.http:req_add_header("header", "value")
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001185 end
1186..
1187
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001188.. js:function:: HTTP.res_set_header(http, name, value)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001189
1190 This variable replace all occurence of all header "name", by only
1191 one containing the "value".
1192
1193 :param class_http http: The related http object.
1194 :param string name: The header name.
1195 :param string value: The header value.
Pieter Baauw386a1272015-08-16 15:26:24 +02001196 :see: HTTP.req_rep_header()
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001197
Pieter Baauw386a1272015-08-16 15:26:24 +02001198.. js:function:: HTTP.req_rep_header(http, name, regex, replace)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001199
1200 Matches the regular expression in all occurrences of header field "name"
1201 according to "regex", and replaces them with the "replace" argument. The
1202 replacement value can contain back references like \1, \2, ... This
1203 function works with the request.
1204
1205 :param class_http http: The related http object.
1206 :param string name: The header name.
1207 :param string regex: The match regular expression.
1208 :param string replace: The replacement value.
Pieter Baauw386a1272015-08-16 15:26:24 +02001209 :see: HTTP.res_rep_header()
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001210
Pieter Baauw386a1272015-08-16 15:26:24 +02001211.. js:function:: HTTP.res_rep_header(http, name, regex, string)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001212
1213 Matches the regular expression in all occurrences of header field "name"
1214 according to "regex", and replaces them with the "replace" argument. The
1215 replacement value can contain back references like \1, \2, ... This
1216 function works with the request.
1217
1218 :param class_http http: The related http object.
1219 :param string name: The header name.
1220 :param string regex: The match regular expression.
1221 :param string replace: The replacement value.
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001222 :see: HTTP.req_replace_header()
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001223
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001224.. js:function:: HTTP.req_set_method(http, method)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001225
1226 Rewrites the request method with the parameter "method".
1227
1228 :param class_http http: The related http object.
1229 :param string method: The new method.
1230
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001231.. js:function:: HTTP.req_set_path(http, path)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001232
1233 Rewrites the request path with the "path" parameter.
1234
1235 :param class_http http: The related http object.
1236 :param string path: The new path.
1237
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001238.. js:function:: HTTP.req_set_query(http, query)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001239
1240 Rewrites the request's query string which appears after the first question
1241 mark ("?") with the parameter "query".
1242
1243 :param class_http http: The related http object.
1244 :param string query: The new query.
1245
Thierry FOURNIER0d79cf62015-08-26 14:20:58 +02001246.. js:function:: HTTP.req_set_uri(http, uri)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001247
1248 Rewrites the request URI with the parameter "uri".
1249
1250 :param class_http http: The related http object.
1251 :param string uri: The new uri.
1252
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +02001253.. js:function:: HTTP.res_set_status(http, status)
1254
1255 Rewrites the response status code with the parameter "code". Note that the
1256 reason is automatically adapted to the new code.
1257
1258 :param class_http http: The related http object.
1259 :param integer status: The new response status code.
1260
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001261.. _txn_class:
1262
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001263TXN class
1264=========
1265
1266.. js:class:: TXN
1267
1268 The txn class contain all the functions relative to the http or tcp
1269 transaction (Note than a tcp stream is the same than a tcp transaction, but
1270 an HTTP transaction is not the same than a tcp stream).
1271
1272 The usage of this class permits to retrieve data from the requests, alter it
1273 and forward it.
1274
1275 All the functions provided by this class are available in the context
1276 **sample-fetches** and **actions**.
1277
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001278.. js:attribute:: TXN.c
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001279
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001280 :returns: An :ref:`converters_class`.
1281
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001282 This attribute contains a Converters class object.
1283
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001284.. js:attribute:: TXN.sc
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001285
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001286 :returns: An :ref:`converters_class`.
1287
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001288 This attribute contains a Converters class object. The functions of
1289 this object returns always a string.
1290
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001291.. js:attribute:: TXN.f
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001292
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001293 :returns: An :ref:`fetches_class`.
1294
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001295 This attribute contains a Fetches class object.
1296
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001297.. js:attribute:: TXN.sf
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001298
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001299 :returns: An :ref:`fetches_class`.
1300
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001301 This attribute contains a Fetches class object. The functions of
1302 this object returns always a string.
1303
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001304.. js:attribute:: TXN.req
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001305
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001306 :returns: An :ref:`channel_class`.
1307
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001308 This attribute contains a channel class object for the request buffer.
1309
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001310.. js:attribute:: TXN.res
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001311
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001312 :returns: An :ref:`channel_class`.
1313
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001314 This attribute contains a channel class object for the response buffer.
1315
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001316.. js:attribute:: TXN.http
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001317
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001318 :returns: An :ref:`http_class`.
1319
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001320 This attribute contains an HTTP class object. It is avalaible only if the
1321 proxy has the "mode http" enabled.
1322
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01001323.. js:function:: TXN.log(TXN, loglevel, msg)
1324
1325 This function sends a log. The log is sent, according with the HAProxy
1326 configuration file, on the default syslog server if it is configured and on
1327 the stderr if it is allowed.
1328
1329 :param class_txn txn: The class txn object containing the data.
1330 :param integer loglevel: Is the log level asociated with the message. It is a
1331 number between 0 and 7.
1332 :param string msg: The log content.
1333 :see: core.emerg, core.alert, core.crit, core.err, core.warning, core.notice,
1334 core.info, core.debug (log level definitions)
1335 :see: TXN.deflog
1336 :see: TXN.Debug
1337 :see: TXN.Info
1338 :see: TXN.Warning
1339 :see: TXN.Alert
1340
1341.. js:function:: TXN.deflog(TXN, msg)
1342
1343 Sends a log line with the default loglevel for the proxy ssociated with the
1344 transaction.
1345
1346 :param class_txn txn: The class txn object containing the data.
1347 :param string msg: The log content.
1348 :see: TXN.log
1349
1350.. js:function:: TXN.Debug(txn, msg)
1351
1352 :param class_txn txn: The class txn object containing the data.
1353 :param string msg: The log content.
1354 :see: TXN.log
1355
1356 Does the same job than:
1357
1358.. code-block:: lua
1359
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001360 function Debug(txn, msg)
1361 TXN.log(txn, core.debug, msg)
1362 end
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01001363..
1364
1365.. js:function:: TXN.Info(txn, msg)
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.. code-block:: lua
1372
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001373 function Debug(txn, msg)
1374 TXN.log(txn, core.info, msg)
1375 end
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01001376..
1377
1378.. js:function:: TXN.Warning(txn, msg)
1379
1380 :param class_txn txn: The class txn object containing the data.
1381 :param string msg: The log content.
1382 :see: TXN.log
1383
1384.. code-block:: lua
1385
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001386 function Debug(txn, msg)
1387 TXN.log(txn, core.warning, msg)
1388 end
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01001389..
1390
1391.. js:function:: TXN.Alert(txn, msg)
1392
1393 :param class_txn txn: The class txn object containing the data.
1394 :param string msg: The log content.
1395 :see: TXN.log
1396
1397.. code-block:: lua
1398
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001399 function Debug(txn, msg)
1400 TXN.log(txn, core.alert, msg)
1401 end
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01001402..
1403
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001404.. js:function:: TXN.get_priv(txn)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001405
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001406 Return Lua data stored in the current transaction (with the `TXN.set_priv()`)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001407 function. If no data are stored, it returns a nil value.
1408
1409 :param class_txn txn: The class txn object containing the data.
1410 :returns: the opaque data previsously stored, or nil if nothing is
1411 avalaible.
1412
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001413.. js:function:: TXN.set_priv(txn, data)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001414
1415 Store any data in the current HAProxy transaction. This action replace the
1416 old stored data.
1417
1418 :param class_txn txn: The class txn object containing the data.
1419 :param opaque data: The data which is stored in the transaction.
1420
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +02001421.. js:function:: TXN.set_var(TXN, var, value)
1422
David Carlier61fdf8b2015-10-02 11:59:38 +01001423 Converts a Lua type in a HAProxy type and store it in a variable <var>.
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +02001424
1425 :param class_txn txn: The class txn object containing the data.
1426 :param string var: The variable name according with the HAProxy variable syntax.
Christopher Faulet85d79c92016-11-09 16:54:56 +01001427
1428.. js:function:: TXN.unset_var(TXN, var)
1429
1430 Unset the variable <var>.
1431
1432 :param class_txn txn: The class txn object containing the data.
1433 :param string var: The variable name according with the HAProxy variable syntax.
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +02001434
1435.. js:function:: TXN.get_var(TXN, var)
1436
1437 Returns data stored in the variable <var> converter in Lua type.
1438
1439 :param class_txn txn: The class txn object containing the data.
1440 :param string var: The variable name according with the HAProxy variable syntax.
1441
Willy Tarreaubc183a62015-08-28 10:39:11 +02001442.. js:function:: TXN.done(txn)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001443
Willy Tarreaubc183a62015-08-28 10:39:11 +02001444 This function terminates processing of the transaction and the associated
1445 session. It can be used when a critical error is detected or to terminate
1446 processing after some data have been returned to the client (eg: a redirect).
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001447
Thierry FOURNIERab00df62016-07-14 11:42:37 +02001448 *Warning*: It not make sense to call this function from sample-fetches. In
1449 this case the behaviour of this one is the same than core.done(): it quit
1450 the Lua execution. The transaction is really aborted only from an action
1451 registered function.
1452
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001453 :param class_txn txn: The class txn object containing the data.
1454
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001455.. js:function:: TXN.set_loglevel(txn, loglevel)
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01001456
1457 Is used to change the log level of the current request. The "loglevel" must
1458 be an integer between 0 and 7.
1459
1460 :param class_txn txn: The class txn object containing the data.
1461 :param integer loglevel: The required log level. This variable can be one of
1462 :see: core.<loglevel>
1463
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001464.. js:function:: TXN.set_tos(txn, tos)
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01001465
1466 Is used to set the TOS or DSCP field value of packets sent to the client to
1467 the value passed in "tos" on platforms which support this.
1468
1469 :param class_txn txn: The class txn object containing the data.
1470 :param integer tos: The new TOS os DSCP.
1471
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001472.. js:function:: TXN.set_mark(txn, mark)
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01001473
1474 Is used to set the Netfilter MARK on all packets sent to the client to the
1475 value passed in "mark" on platforms which support it.
1476
1477 :param class_txn txn: The class txn object containing the data.
1478 :param integer mark: The mark value.
1479
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001480.. _socket_class:
1481
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001482Socket class
1483============
1484
1485.. js:class:: Socket
1486
1487 This class must be compatible with the Lua Socket class. Only the 'client'
1488 functions are available. See the Lua Socket documentation:
1489
1490 `http://w3.impa.br/~diego/software/luasocket/tcp.html
1491 <http://w3.impa.br/~diego/software/luasocket/tcp.html>`_
1492
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001493.. js:function:: Socket.close(socket)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001494
1495 Closes a TCP object. The internal socket used by the object is closed and the
1496 local address to which the object was bound is made available to other
1497 applications. No further operations (except for further calls to the close
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001498 method) are allowed on a closed Socket.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001499
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001500 :param class_socket socket: Is the manipulated Socket.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001501
1502 Note: It is important to close all used sockets once they are not needed,
1503 since, in many systems, each socket uses a file descriptor, which are limited
1504 system resources. Garbage-collected objects are automatically closed before
1505 destruction, though.
1506
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02001507.. js:function:: Socket.connect(socket, address[, port])
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001508
1509 Attempts to connect a socket object to a remote host.
1510
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001511
1512 In case of error, the method returns nil followed by a string describing the
1513 error. In case of success, the method returns 1.
1514
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001515 :param class_socket socket: Is the manipulated Socket.
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02001516 :param string address: can be an IP address or a host name. See below for more
1517 information.
1518 :param integer port: must be an integer number in the range [1..64K].
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001519 :returns: 1 or nil.
1520
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02001521 an address field extension permits to use the connect() function to connect to
1522 other stream than TCP. The syntax containing a simpleipv4 or ipv6 address is
1523 the basically expected format. This format requires the port.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001524
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02001525 Other format accepted are a socket path like "/socket/path", it permits to
1526 connect to a socket. abstract namespaces are supported with the prefix
1527 "abns@", and finaly a filedescriotr can be passed with the prefix "fd@".
1528 The prefix "ipv4@", "ipv6@" and "unix@" are also supported. The port can be
1529 passed int the string. The syntax "127.0.0.1:1234" is valid. in this case, the
1530 parameter *port* is ignored.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001531
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001532.. js:function:: Socket.connect_ssl(socket, address, port)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001533
1534 Same behavior than the function socket:connect, but uses SSL.
1535
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001536 :param class_socket socket: Is the manipulated Socket.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001537 :returns: 1 or nil.
1538
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001539.. js:function:: Socket.getpeername(socket)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001540
1541 Returns information about the remote side of a connected client object.
1542
1543 Returns a string with the IP address of the peer, followed by the port number
1544 that peer is using for the connection. In case of error, the method returns
1545 nil.
1546
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001547 :param class_socket socket: Is the manipulated Socket.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001548 :returns: a string containing the server information.
1549
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001550.. js:function:: Socket.getsockname(socket)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001551
1552 Returns the local address information associated to the object.
1553
1554 The method returns a string with local IP address and a number with the port.
1555 In case of error, the method returns nil.
1556
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001557 :param class_socket socket: Is the manipulated Socket.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001558 :returns: a string containing the client information.
1559
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001560.. js:function:: Socket.receive(socket, [pattern [, prefix]])
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001561
1562 Reads data from a client object, according to the specified read pattern.
1563 Patterns follow the Lua file I/O format, and the difference in performance
1564 between all patterns is negligible.
1565
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001566 :param class_socket socket: Is the manipulated Socket.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001567 :param string|integer pattern: Describe what is required (see below).
1568 :param string prefix: A string which will be prefix the returned data.
1569 :returns: a string containing the required data or nil.
1570
1571 Pattern can be any of the following:
1572
1573 * **`*a`**: reads from the socket until the connection is closed. No
1574 end-of-line translation is performed;
1575
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001576 * **`*l`**: reads a line of text from the Socket. The line is terminated by a
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001577 LF character (ASCII 10), optionally preceded by a CR character
1578 (ASCII 13). The CR and LF characters are not included in the
1579 returned line. In fact, all CR characters are ignored by the
1580 pattern. This is the default pattern.
1581
1582 * **number**: causes the method to read a specified number of bytes from the
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001583 Socket. Prefix is an optional string to be concatenated to the
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001584 beginning of any received data before return.
1585
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02001586 * **empty**: If the pattern is left empty, the default option is `*l`.
1587
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001588 If successful, the method returns the received pattern. In case of error, the
1589 method returns nil followed by an error message which can be the string
1590 'closed' in case the connection was closed before the transmission was
1591 completed or the string 'timeout' in case there was a timeout during the
1592 operation. Also, after the error message, the function returns the partial
1593 result of the transmission.
1594
1595 Important note: This function was changed severely. It used to support
1596 multiple patterns (but I have never seen this feature used) and now it
1597 doesn't anymore. Partial results used to be returned in the same way as
1598 successful results. This last feature violated the idea that all functions
1599 should return nil on error. Thus it was changed too.
1600
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001601.. js:function:: Socket.send(socket, data [, start [, end ]])
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001602
1603 Sends data through client object.
1604
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001605 :param class_socket socket: Is the manipulated Socket.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001606 :param string data: The data that will be sent.
1607 :param integer start: The start position in the buffer of the data which will
1608 be sent.
1609 :param integer end: The end position in the buffer of the data which will
1610 be sent.
1611 :returns: see below.
1612
1613 Data is the string to be sent. The optional arguments i and j work exactly
1614 like the standard string.sub Lua function to allow the selection of a
1615 substring to be sent.
1616
1617 If successful, the method returns the index of the last byte within [start,
1618 end] that has been sent. Notice that, if start is 1 or absent, this is
1619 effectively the total number of bytes sent. In case of error, the method
1620 returns nil, followed by an error message, followed by the index of the last
1621 byte within [start, end] that has been sent. You might want to try again from
1622 the byte following that. The error message can be 'closed' in case the
1623 connection was closed before the transmission was completed or the string
1624 'timeout' in case there was a timeout during the operation.
1625
1626 Note: Output is not buffered. For small strings, it is always better to
1627 concatenate them in Lua (with the '..' operator) and send the result in one
1628 call instead of calling the method several times.
1629
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001630.. js:function:: Socket.setoption(socket, option [, value])
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001631
1632 Just implemented for compatibility, this cal does nothing.
1633
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001634.. js:function:: Socket.settimeout(socket, value [, mode])
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001635
1636 Changes the timeout values for the object. All I/O operations are blocking.
1637 That is, any call to the methods send, receive, and accept will block
1638 indefinitely, until the operation completes. The settimeout method defines a
1639 limit on the amount of time the I/O methods can block. When a timeout time
1640 has elapsed, the affected methods give up and fail with an error code.
1641
1642 The amount of time to wait is specified as the value parameter, in seconds.
1643
1644 The timeout modes are bot implemented, the only settable timeout is the
1645 inactivity time waiting for complete the internal buffer send or waiting for
1646 receive data.
1647
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001648 :param class_socket socket: Is the manipulated Socket.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001649 :param integer value: The timeout value.
1650
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001651.. _map_class:
1652
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001653Map class
1654=========
1655
1656.. js:class:: Map
1657
1658 This class permits to do some lookup in HAProxy maps. The declared maps can
1659 be modified during the runtime throught the HAProxy management socket.
1660
1661.. code-block:: lua
1662
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001663 default = "usa"
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001664
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001665 -- Create and load map
1666 geo = Map.new("geo.map", Map.ip);
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001667
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001668 -- Create new fetch that returns the user country
1669 core.register_fetches("country", function(txn)
1670 local src;
1671 local loc;
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001672
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001673 src = txn.f:fhdr("x-forwarded-for");
1674 if (src == nil) then
1675 src = txn.f:src()
1676 if (src == nil) then
1677 return default;
1678 end
1679 end
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001680
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001681 -- Perform lookup
1682 loc = geo:lookup(src);
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001683
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001684 if (loc == nil) then
1685 return default;
1686 end
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001687
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001688 return loc;
1689 end);
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001690
1691.. js:attribute:: Map.int
1692
1693 See the HAProxy configuration.txt file, chapter "Using ACLs and fetching
1694 samples" ans subchapter "ACL basics" to understand this pattern matching
1695 method.
1696
1697.. js:attribute:: Map.ip
1698
1699 See the HAProxy configuration.txt file, chapter "Using ACLs and fetching
1700 samples" ans subchapter "ACL basics" to understand this pattern matching
1701 method.
1702
1703.. js:attribute:: Map.str
1704
1705 See the HAProxy configuration.txt file, chapter "Using ACLs and fetching
1706 samples" ans subchapter "ACL basics" to understand this pattern matching
1707 method.
1708
1709.. js:attribute:: Map.beg
1710
1711 See the HAProxy configuration.txt file, chapter "Using ACLs and fetching
1712 samples" ans subchapter "ACL basics" to understand this pattern matching
1713 method.
1714
1715.. js:attribute:: Map.sub
1716
1717 See the HAProxy configuration.txt file, chapter "Using ACLs and fetching
1718 samples" ans subchapter "ACL basics" to understand this pattern matching
1719 method.
1720
1721.. js:attribute:: Map.dir
1722
1723 See the HAProxy configuration.txt file, chapter "Using ACLs and fetching
1724 samples" ans subchapter "ACL basics" to understand this pattern matching
1725 method.
1726
1727.. js:attribute:: Map.dom
1728
1729 See the HAProxy configuration.txt file, chapter "Using ACLs and fetching
1730 samples" ans subchapter "ACL basics" to understand this pattern matching
1731 method.
1732
1733.. js:attribute:: Map.end
1734
1735 See the HAProxy configuration.txt file, chapter "Using ACLs and fetching
1736 samples" ans subchapter "ACL basics" to understand this pattern matching
1737 method.
1738
1739.. js:attribute:: Map.reg
1740
1741 See the HAProxy configuration.txt file, chapter "Using ACLs and fetching
1742 samples" ans subchapter "ACL basics" to understand this pattern matching
1743 method.
1744
1745
1746.. js:function:: Map.new(file, method)
1747
1748 Creates and load a map.
1749
1750 :param string file: Is the file containing the map.
1751 :param integer method: Is the map pattern matching method. See the attributes
1752 of the Map class.
1753 :returns: a class Map object.
1754 :see: The Map attributes.
1755
1756.. js:function:: Map.lookup(map, str)
1757
1758 Perform a lookup in a map.
1759
1760 :param class_map map: Is the class Map object.
1761 :param string str: Is the string used as key.
1762 :returns: a string containing the result or nil if no match.
1763
1764.. js:function:: Map.slookup(map, str)
1765
1766 Perform a lookup in a map.
1767
1768 :param class_map map: Is the class Map object.
1769 :param string str: Is the string used as key.
1770 :returns: a string containing the result or empty string if no match.
1771
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001772.. _applethttp_class:
1773
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02001774AppletHTTP class
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001775================
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02001776
1777.. js:class:: AppletHTTP
1778
1779 This class is used with applets that requires the 'http' mode. The http applet
1780 can be registered with the *core.register_service()* function. They are used
1781 for processing an http request like a server in back of HAProxy.
1782
1783 This is an hello world sample code:
1784
1785.. code-block:: lua
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001786
Pieter Baauw4d7f7662015-11-08 16:38:08 +01001787 core.register_service("hello-world", "http", function(applet)
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02001788 local response = "Hello World !"
1789 applet:set_status(200)
Pieter Baauw2dcb9bc2015-10-01 22:47:12 +02001790 applet:add_header("content-length", string.len(response))
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02001791 applet:add_header("content-type", "text/plain")
Pieter Baauw2dcb9bc2015-10-01 22:47:12 +02001792 applet:start_response()
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02001793 applet:send(response)
1794 end)
1795
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001796.. js:attribute:: AppletHTTP.c
1797
1798 :returns: A :ref:`converters_class`
1799
1800 This attribute contains a Converters class object.
1801
1802.. js:attribute:: AppletHTTP.sc
1803
1804 :returns: A :ref:`converters_class`
1805
1806 This attribute contains a Converters class object. The
1807 functions of this object returns always a string.
1808
1809.. js:attribute:: AppletHTTP.f
1810
1811 :returns: A :ref:`fetches_class`
1812
1813 This attribute contains a Fetches class object. Note that the
1814 applet execution place cannot access to a valid HAProxy core HTTP
1815 transaction, so some sample fecthes related to the HTTP dependant
1816 values (hdr, path, ...) are not available.
1817
1818.. js:attribute:: AppletHTTP.sf
1819
1820 :returns: A :ref:`fetches_class`
1821
1822 This attribute contains a Fetches class object. The functions of
1823 this object returns always a string. Note that the applet
1824 execution place cannot access to a valid HAProxy core HTTP
1825 transaction, so some sample fecthes related to the HTTP dependant
1826 values (hdr, path, ...) are not available.
1827
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01001828.. js:attribute:: AppletHTTP.method
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001829
1830 :returns: string
1831
1832 The attribute method returns a string containing the HTTP
1833 method.
1834
1835.. js:attribute:: AppletHTTP.version
1836
1837 :returns: string
1838
1839 The attribute version, returns a string containing the HTTP
1840 request version.
1841
1842.. js:attribute:: AppletHTTP.path
1843
1844 :returns: string
1845
1846 The attribute path returns a string containing the HTTP
1847 request path.
1848
1849.. js:attribute:: AppletHTTP.qs
1850
1851 :returns: string
1852
1853 The attribute qs returns a string containing the HTTP
1854 request query string.
1855
1856.. js:attribute:: AppletHTTP.length
1857
1858 :returns: integer
1859
1860 The attribute length returns an integer containing the HTTP
1861 body length.
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02001862
Thierry FOURNIER841475e2015-12-11 17:10:09 +01001863.. js:attribute:: AppletHTTP.headers
1864
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001865 :returns: array
1866
1867 The attribute headers returns an array containing the HTTP
1868 headers. The header names are always in lower case. As the header name can be
1869 encountered more than once in each request, the value is indexed with 0 as
1870 first index value. The array have this form:
1871
1872.. code-block:: lua
1873
1874 AppletHTTP.headers['<header-name>'][<header-index>] = "<header-value>"
1875
1876 AppletHTTP.headers["host"][0] = "www.test.com"
1877 AppletHTTP.headers["accept"][0] = "audio/basic q=1"
1878 AppletHTTP.headers["accept"][1] = "audio/*, q=0.2"
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01001879 AppletHTTP.headers["accept"][2] = "*/*, q=0.1"
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001880..
1881
1882.. js:attribute:: AppletHTTP.headers
1883
Thierry FOURNIER841475e2015-12-11 17:10:09 +01001884 Contains an array containing all the request headers.
1885
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01001886.. js:function:: AppletHTTP.set_status(applet, code)
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02001887
1888 This function sets the HTTP status code for the response. The allowed code are
1889 from 100 to 599.
1890
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01001891 :param class_AppletHTTP applet: An :ref:`applethttp_class`
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02001892 :param integer code: the status code returned to the client.
1893
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01001894.. js:function:: AppletHTTP.add_header(applet, name, value)
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02001895
1896 This function add an header in the response. Duplicated headers are not
1897 collapsed. The special header *content-length* is used to determinate the
1898 response length. If it not exists, a *transfer-encoding: chunked* is set, and
1899 all the write from the funcion *AppletHTTP:send()* become a chunk.
1900
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01001901 :param class_AppletHTTP applet: An :ref:`applethttp_class`
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02001902 :param string name: the header name
1903 :param string value: the header value
1904
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01001905.. js:function:: AppletHTTP.start_response(applet)
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02001906
1907 This function indicates to the HTTP engine that it can process and send the
1908 response headers. After this called we cannot add headers to the response; We
1909 cannot use the *AppletHTTP:send()* function if the
1910 *AppletHTTP:start_response()* is not called.
1911
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01001912 :param class_AppletHTTP applet: An :ref:`applethttp_class`
1913
1914.. js:function:: AppletHTTP.getline(applet)
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02001915
1916 This function returns a string containing one line from the http body. If the
1917 data returned doesn't contains a final '\\n' its assumed than its the last
1918 available data before the end of stream.
1919
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01001920 :param class_AppletHTTP applet: An :ref:`applethttp_class`
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02001921 :returns: a string. The string can be empty if we reach the end of the stream.
1922
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01001923.. js:function:: AppletHTTP.receive(applet, [size])
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02001924
1925 Reads data from the HTTP body, according to the specified read *size*. If the
1926 *size* is missing, the function tries to read all the content of the stream
1927 until the end. If the *size* is bigger than the http body, it returns the
1928 amount of data avalaible.
1929
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01001930 :param class_AppletHTTP applet: An :ref:`applethttp_class`
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02001931 :param integer size: the required read size.
1932 :returns: always return a string,the string can be empty is the connexion is
1933 closed.
1934
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01001935.. js:function:: AppletHTTP.send(applet, msg)
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02001936
1937 Send the message *msg* on the http request body.
1938
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01001939 :param class_AppletHTTP applet: An :ref:`applethttp_class`
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02001940 :param string msg: the message to send.
1941
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01001942.. js:function:: AppletHTTP.get_priv(applet)
1943
1944 Return Lua data stored in the current transaction (with the
1945 `AppletHTTP.set_priv()`) function. If no data are stored, it returns a nil
1946 value.
1947
1948 :param class_AppletHTTP applet: An :ref:`applethttp_class`
1949 :returns: the opaque data previsously stored, or nil if nothing is
1950 avalaible.
1951
1952.. js:function:: AppletHTTP.set_priv(applet, data)
1953
1954 Store any data in the current HAProxy transaction. This action replace the
1955 old stored data.
1956
1957 :param class_AppletHTTP applet: An :ref:`applethttp_class`
1958 :param opaque data: The data which is stored in the transaction.
1959
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001960.. _applettcp_class:
1961
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02001962AppletTCP class
1963===============
1964
1965.. js:class:: AppletTCP
1966
1967 This class is used with applets that requires the 'tcp' mode. The tcp applet
1968 can be registered with the *core.register_service()* function. They are used
1969 for processing a tcp stream like a server in back of HAProxy.
1970
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001971.. js:attribute:: AppletTCP.c
1972
1973 :returns: A :ref:`converters_class`
1974
1975 This attribute contains a Converters class object.
1976
1977.. js:attribute:: AppletTCP.sc
1978
1979 :returns: A :ref:`converters_class`
1980
1981 This attribute contains a Converters class object. The
1982 functions of this object returns always a string.
1983
1984.. js:attribute:: AppletTCP.f
1985
1986 :returns: A :ref:`fetches_class`
1987
1988 This attribute contains a Fetches class object.
1989
1990.. js:attribute:: AppletTCP.sf
1991
1992 :returns: A :ref:`fetches_class`
1993
1994 This attribute contains a Fetches class object.
1995
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01001996.. js:function:: AppletTCP.getline(applet)
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02001997
1998 This function returns a string containing one line from the stream. If the
1999 data returned doesn't contains a final '\\n' its assumed than its the last
2000 available data before the end of stream.
2001
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01002002 :param class_AppletTCP applet: An :ref:`applettcp_class`
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002003 :returns: a string. The string can be empty if we reach the end of the stream.
2004
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01002005.. js:function:: AppletTCP.receive(applet, [size])
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002006
2007 Reads data from the TCP stream, according to the specified read *size*. If the
2008 *size* is missing, the function tries to read all the content of the stream
2009 until the end.
2010
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01002011 :param class_AppletTCP applet: An :ref:`applettcp_class`
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002012 :param integer size: the required read size.
2013 :returns: always return a string,the string can be empty is the connexion is
2014 closed.
2015
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01002016.. js:function:: AppletTCP.send(appletmsg)
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002017
2018 Send the message on the stream.
2019
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01002020 :param class_AppletTCP applet: An :ref:`applettcp_class`
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002021 :param string msg: the message to send.
2022
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01002023.. js:function:: AppletTCP.get_priv(applet)
2024
2025 Return Lua data stored in the current transaction (with the
2026 `AppletTCP.set_priv()`) function. If no data are stored, it returns a nil
2027 value.
2028
2029 :param class_AppletTCP applet: An :ref:`applettcp_class`
2030 :returns: the opaque data previsously stored, or nil if nothing is
2031 avalaible.
2032
2033.. js:function:: AppletTCP.set_priv(applet, data)
2034
2035 Store any data in the current HAProxy transaction. This action replace the
2036 old stored data.
2037
2038 :param class_AppletTCP applet: An :ref:`applettcp_class`
2039 :param opaque data: The data which is stored in the transaction.
2040
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002041External Lua libraries
2042======================
2043
2044A lot of useful lua libraries can be found here:
2045
2046* `https://lua-toolbox.com/ <https://lua-toolbox.com/>`_
2047
2048Redis acces:
2049
2050* `https://github.com/nrk/redis-lua <https://github.com/nrk/redis-lua>`_
2051
2052This is an example about the usage of the Redis library with HAProxy. Note that
2053each call of any function of this library can throw an error if the socket
2054connection fails.
2055
2056.. code-block:: lua
2057
2058 -- load the redis library
2059 local redis = require("redis");
2060
2061 function do_something(txn)
2062
2063 -- create and connect new tcp socket
2064 local tcp = core.tcp();
2065 tcp:settimeout(1);
2066 tcp:connect("127.0.0.1", 6379);
2067
2068 -- use the redis library with this new socket
2069 local client = redis.connect({socket=tcp});
2070 client:ping();
2071
2072 end
2073
2074OpenSSL:
2075
2076* `http://mkottman.github.io/luacrypto/index.html
2077 <http://mkottman.github.io/luacrypto/index.html>`_
2078
2079* `https://github.com/brunoos/luasec/wiki
2080 <https://github.com/brunoos/luasec/wiki>`_
Thierry FOURNIER2e4893c2015-03-18 13:37:27 +01002081