blob: 32ef1436b96cbb78bb098e48b19a5233c8170942 [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 FOURNIER / OZON.IO62fec752016-11-10 20:38:11 +0100585.. js:function:: core.parse_addr(address)
586
587 **context**: body, init, task, action, sample-fetch, converter
588
589 :param network: is a string describing an ipv4 or ipv6 address and optionally
590 its network length, like this: "127.0.0.1/8" or "aaaa::1234/32".
591 :returns: a userdata containing network or nil if an error occurs.
592
593 Parse ipv4 or ipv6 adresses and its facultative associated network.
594
595.. js:function:: core.match_addr(addr1, addr2)
596
597 **context**: body, init, task, action, sample-fetch, converter
598
599 :param addr1: is an address created with "core.parse_addr".
600 :param addr2: is an address created with "core.parse_addr".
601 :returns: boolean, true if the network of the addresses matche, else returns
602 false.
603
604 Match two networks. For example "127.0.0.1/32" matchs "127.0.0.0/8". The order
605 of network is not important.
606
Thierry Fournierf61aa632016-02-19 20:56:00 +0100607.. _proxy_class:
608
609Proxy class
610============
611
612.. js:class:: Proxy
613
614 This class provides a way for manipulating proxy and retrieving information
615 like statistics.
616
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +0100617.. js:attribute:: Proxy.servers
618
619 Contain an array with the attached servers. Each server entry is an object of
620 type :ref:`server_class`.
621
Thierry Fournierff480422016-02-25 08:36:46 +0100622.. js:attribute:: Proxy.listeners
623
624 Contain an array with the attached listeners. Each listeners entry is an
625 object of type :ref:`listener_class`.
626
Thierry Fournierf61aa632016-02-19 20:56:00 +0100627.. js:function:: Proxy.pause(px)
628
629 Pause the proxy. See the management socket documentation for more information.
630
631 :param class_proxy px: A :ref:`proxy_class` which indicates the manipulated
632 proxy.
633
634.. js:function:: Proxy.resume(px)
635
636 Resume the proxy. See the management socket documentation for more
637 information.
638
639 :param class_proxy px: A :ref:`proxy_class` which indicates the manipulated
640 proxy.
641
642.. js:function:: Proxy.stop(px)
643
644 Stop the proxy. See the management socket documentation for more information.
645
646 :param class_proxy px: A :ref:`proxy_class` which indicates the manipulated
647 proxy.
648
649.. js:function:: Proxy.shut_bcksess(px)
650
651 Kill the session attached to a backup server. See the management socket
652 documentation for more information.
653
654 :param class_proxy px: A :ref:`proxy_class` which indicates the manipulated
655 proxy.
656
657.. js:function:: Proxy.get_cap(px)
658
659 Returns a string describing the capabilities of the proxy.
660
661 :param class_proxy px: A :ref:`proxy_class` which indicates the manipulated
662 proxy.
663 :returns: a string "frontend", "backend", "proxy" or "ruleset".
664
665.. js:function:: Proxy.get_mode(px)
666
667 Returns a string describing the mode of the current proxy.
668
669 :param class_proxy px: A :ref:`proxy_class` which indicates the manipulated
670 proxy.
671 :returns: a string "tcp", "http", "health" or "unknown"
672
673.. js:function:: Proxy.get_stats(px)
674
675 Returns an array containg the proxy statistics. The statistics returned are
676 not the same if the proxy is frontend or a backend.
677
678 :param class_proxy px: A :ref:`proxy_class` which indicates the manipulated
679 proxy.
680 :returns: a key/value array containing stats
681
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +0100682.. _server_class:
683
684Server class
685============
686
687.. js:function:: Server.is_draining(sv)
688
689 Return true if the server is currently draining stiky connections.
690
691 :param class_server sv: A :ref:`server_class` which indicates the manipulated
692 server.
693 :returns: a boolean
694
695.. js:function:: Server.set_weight(sv, weight)
696
697 Dynamically change the weight of the serveur. See the management socket
698 documentation for more information about the format of the string.
699
700 :param class_server sv: A :ref:`server_class` which indicates the manipulated
701 server.
702 :param string weight: A string describing the server weight.
703
704.. js:function:: Server.get_weight(sv)
705
706 This function returns an integer representing the serveur weight.
707
708 :param class_server sv: A :ref:`server_class` which indicates the manipulated
709 server.
710 :returns: an integer.
711
712.. js:function:: Server.set_addr(sv, addr)
713
714 Dynamically change the address of the serveur. See the management socket
715 documentation for more information about the format of the string.
716
717 :param class_server sv: A :ref:`server_class` which indicates the manipulated
718 server.
719 :param string weight: A string describing the server address.
720
721.. js:function:: Server.get_addr(sv)
722
723 Returns a string describing the address of the serveur.
724
725 :param class_server sv: A :ref:`server_class` which indicates the manipulated
726 server.
727 :returns: A string
728
729.. js:function:: Server.get_stats(sv)
730
731 Returns server statistics.
732
733 :param class_server sv: A :ref:`server_class` which indicates the manipulated
734 server.
735 :returns: a key/value array containing stats
736
737.. js:function:: Server.shut_sess(sv)
738
739 Shutdown all the sessions attached to the server. See the management socket
740 documentation for more information about this function.
741
742 :param class_server sv: A :ref:`server_class` which indicates the manipulated
743 server.
744
745.. js:function:: Server.set_drain(sv)
746
747 Drain sticky sessions. See the management socket documentation for more
748 information about this function.
749
750 :param class_server sv: A :ref:`server_class` which indicates the manipulated
751 server.
752
753.. js:function:: Server.set_maint(sv)
754
755 Set maintenance mode. See the management socket documentation for more
756 information about this function.
757
758 :param class_server sv: A :ref:`server_class` which indicates the manipulated
759 server.
760
761.. js:function:: Server.set_ready(sv)
762
763 Set normal mode. See the management socket documentation for more information
764 about this function.
765
766 :param class_server sv: A :ref:`server_class` which indicates the manipulated
767 server.
768
769.. js:function:: Server.check_enable(sv)
770
771 Enable health checks. See the management socket documentation for more
772 information about this function.
773
774 :param class_server sv: A :ref:`server_class` which indicates the manipulated
775 server.
776
777.. js:function:: Server.check_disable(sv)
778
779 Disable health checks. See the management socket documentation for more
780 information about this function.
781
782 :param class_server sv: A :ref:`server_class` which indicates the manipulated
783 server.
784
785.. js:function:: Server.check_force_up(sv)
786
787 Force health-check up. See the management socket documentation for more
788 information about this function.
789
790 :param class_server sv: A :ref:`server_class` which indicates the manipulated
791 server.
792
793.. js:function:: Server.check_force_nolb(sv)
794
795 Force health-check nolb mode. See the management socket documentation for more
796 information about this function.
797
798 :param class_server sv: A :ref:`server_class` which indicates the manipulated
799 server.
800
801.. js:function:: Server.check_force_down(sv)
802
803 Force health-check down. See the management socket documentation for more
804 information about this function.
805
806 :param class_server sv: A :ref:`server_class` which indicates the manipulated
807 server.
808
809.. js:function:: Server.agent_enable(sv)
810
811 Enable agent check. See the management socket documentation for more
812 information about this function.
813
814 :param class_server sv: A :ref:`server_class` which indicates the manipulated
815 server.
816
817.. js:function:: Server.agent_disable(sv)
818
819 Disable agent check. See the management socket documentation for more
820 information about this function.
821
822 :param class_server sv: A :ref:`server_class` which indicates the manipulated
823 server.
824
825.. js:function:: Server.agent_force_up(sv)
826
827 Force agent check up. See the management socket documentation for more
828 information about this function.
829
830 :param class_server sv: A :ref:`server_class` which indicates the manipulated
831 server.
832
833.. js:function:: Server.agent_force_down(sv)
834
835 Force agent check down. See the management socket documentation for more
836 information about this function.
837
838 :param class_server sv: A :ref:`server_class` which indicates the manipulated
839 server.
840
Thierry Fournierff480422016-02-25 08:36:46 +0100841.. _listener_class:
842
843Listener class
844==============
845
846.. js:function:: Listener.get_stats(ls)
847
848 Returns server statistics.
849
850 :param class_listener ls: A :ref:`listener_class` which indicates the
851 manipulated listener.
852 :returns: a key/value array containing stats
853
Thierry Fournier1de16592016-01-27 09:49:07 +0100854.. _concat_class:
855
856Concat class
857============
858
859.. js:class:: Concat
860
861 This class provides a fast way for string concatenation. The way using native
862 Lua concatenation like the code below is slow for some reasons.
863
864.. code-block:: lua
865
866 str = "string1"
867 str = str .. ", string2"
868 str = str .. ", string3"
869..
870
871 For each concatenation, Lua:
872 * allocate memory for the result,
873 * catenate the two string copying the strings in the new memory bloc,
874 * free the old memory block containing the string whoch is no longer used.
875 This process does many memory move, allocation and free. In addition, the
876 memory is not really freed, it is just mark mark as unsused and wait for the
877 garbage collector.
878
879 The Concat class provide an alternative way for catenating strings. It uses
880 the internal Lua mechanism (it does not allocate memory), but it doesn't copy
881 the data more than once.
882
883 On my computer, the following loops spends 0.2s for the Concat method and
884 18.5s for the pure Lua implementation. So, the Concat class is about 1000x
885 faster than the embedded solution.
886
887.. code-block:: lua
888
889 for j = 1, 100 do
890 c = core.concat()
891 for i = 1, 20000 do
892 c:add("#####")
893 end
894 end
895..
896
897.. code-block:: lua
898
899 for j = 1, 100 do
900 c = ""
901 for i = 1, 20000 do
902 c = c .. "#####"
903 end
904 end
905..
906
907.. js:function:: Concat.add(concat, string)
908
909 This function adds a string to the current concatenated string.
910
911 :param class_concat concat: A :ref:`concat_class` which contains the currently
912 builded string.
913 :param string string: A new string to concatenate to the current builded
914 string.
915
916.. js:function:: Concat.dump(concat)
917
918 This function returns the concanated string.
919
920 :param class_concat concat: A :ref:`concat_class` which contains the currently
921 builded string.
922 :returns: the concatenated string
923
Thierry FOURNIERdc595002015-12-21 11:13:52 +0100924.. _fetches_class:
925
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100926Fetches class
927=============
928
929.. js:class:: Fetches
930
931 This class contains a lot of internal HAProxy sample fetches. See the
Thierry FOURNIER2e4893c2015-03-18 13:37:27 +0100932 HAProxy "configuration.txt" documentation for more information about her
933 usage. they are the chapters 7.3.2 to 7.3.6.
934
Thierry FOURNIERdc595002015-12-21 11:13:52 +0100935 **warning** some sample fetches are not available in some context. These
936 limitations are specified in this documentation when theire useful.
937
Thierry FOURNIER2e4893c2015-03-18 13:37:27 +0100938 :see: TXN.f
939 :see: TXN.sf
940
941 Fetches are useful for:
942
943 * get system time,
944 * get environment variable,
945 * get random numbers,
946 * known backend status like the number of users in queue or the number of
947 connections established,
948 * client information like ip source or destination,
949 * deal with stick tables,
950 * Established SSL informations,
951 * HTTP information like headers or method.
952
953.. code-block:: lua
954
Thierry FOURNIERdc595002015-12-21 11:13:52 +0100955 function action(txn)
956 -- Get source IP
957 local clientip = txn.f:src()
958 end
Thierry FOURNIER2e4893c2015-03-18 13:37:27 +0100959..
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100960
Thierry FOURNIERdc595002015-12-21 11:13:52 +0100961.. _converters_class:
962
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100963Converters class
964================
965
966.. js:class:: Converters
967
968 This class contains a lot of internal HAProxy sample converters. See the
Thierry FOURNIER2e4893c2015-03-18 13:37:27 +0100969 HAProxy documentation "configuration.txt" for more information about her
970 usage. Its the chapter 7.3.1.
971
972 :see: TXN.c
973 :see: TXN.sc
974
975 Converters provides statefull transformation. They are useful for:
976
977 * converting input to base64,
978 * applying hash on input string (djb2, crc32, sdbm, wt6),
979 * format date,
980 * json escape,
Pieter Baauw4d7f7662015-11-08 16:38:08 +0100981 * extracting preferred language comparing two lists,
Thierry FOURNIER2e4893c2015-03-18 13:37:27 +0100982 * turn to lower or upper chars,
983 * deal with stick tables.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100984
Thierry FOURNIERdc595002015-12-21 11:13:52 +0100985.. _channel_class:
986
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100987Channel class
988=============
989
990.. js:class:: Channel
991
992 HAProxy uses two buffers for the processing of the requests. The first one is
993 used with the request data (from the client to the server) and the second is
994 used for the response data (from the server to the client).
995
996 Each buffer contains two types of data. The first type is the incoming data
997 waiting for a processing. The second part is the outgoing data already
998 processed. Usually, the incoming data is processed, after it is tagged as
999 outgoing data, and finally it is sent. The following functions provides tools
1000 for manipulating these data in a buffer.
1001
1002 The following diagram shows where the channel class function are applied.
1003
1004 **Warning**: It is not possible to read from the response in request action,
1005 and it is not possible to read for the request channel in response action.
1006
Thierry FOURNIER2e4893c2015-03-18 13:37:27 +01001007.. image:: _static/channel.png
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001008
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001009.. js:function:: Channel.dup(channel)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001010
1011 This function returns a string that contain the entire buffer. The data is
1012 not remove from the buffer and can be reprocessed later.
1013
1014 If the buffer cant receive more data, a 'nil' value is returned.
1015
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001016 :param class_channel channel: The manipulated Channel.
Pieter Baauw4d7f7662015-11-08 16:38:08 +01001017 :returns: a string containing all the available data or nil.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001018
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001019.. js:function:: Channel.get(channel)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001020
1021 This function returns a string that contain the entire buffer. The data is
1022 consumed from the buffer.
1023
1024 If the buffer cant receive more data, a 'nil' value is returned.
1025
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001026 :param class_channel channel: The manipulated Channel.
Pieter Baauw4d7f7662015-11-08 16:38:08 +01001027 :returns: a string containing all the available data or nil.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001028
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02001029.. js:function:: Channel.getline(channel)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001030
1031 This function returns a string that contain the first line of the buffer. The
1032 data is consumed. If the data returned doesn't contains a final '\n' its
1033 assumed than its the last available data in the buffer.
1034
1035 If the buffer cant receive more data, a 'nil' value is returned.
1036
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001037 :param class_channel channel: The manipulated Channel.
Pieter Baauw386a1272015-08-16 15:26:24 +02001038 :returns: a string containing the available line or nil.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001039
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001040.. js:function:: Channel.set(channel, string)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001041
1042 This function replace the content of the buffer by the string. The function
1043 returns the copied length, otherwise, it returns -1.
1044
1045 The data set with this function are not send. They wait for the end of
1046 HAProxy processing, so the buffer can be full.
1047
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001048 :param class_channel channel: The manipulated Channel.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001049 :param string string: The data which will sent.
Pieter Baauw4d7f7662015-11-08 16:38:08 +01001050 :returns: an integer containing the amount of bytes copied or -1.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001051
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001052.. js:function:: Channel.append(channel, string)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001053
1054 This function append the string argument to the content of the buffer. The
1055 function returns the copied length, otherwise, it returns -1.
1056
1057 The data set with this function are not send. They wait for the end of
1058 HAProxy processing, so the buffer can be full.
1059
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001060 :param class_channel channel: The manipulated Channel.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001061 :param string string: The data which will sent.
Pieter Baauw4d7f7662015-11-08 16:38:08 +01001062 :returns: an integer containing the amount of bytes copied or -1.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001063
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001064.. js:function:: Channel.send(channel, string)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001065
1066 This function required immediate send of the data. Unless if the connection
1067 is close, the buffer is regularly flushed and all the string can be sent.
1068
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001069 :param class_channel channel: The manipulated Channel.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001070 :param string string: The data which will sent.
Pieter Baauw4d7f7662015-11-08 16:38:08 +01001071 :returns: an integer containing the amount of bytes copied or -1.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001072
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001073.. js:function:: Channel.get_in_length(channel)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001074
1075 This function returns the length of the input part of the buffer.
1076
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001077 :param class_channel channel: The manipulated Channel.
Pieter Baauw4d7f7662015-11-08 16:38:08 +01001078 :returns: an integer containing the amount of available bytes.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001079
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001080.. js:function:: Channel.get_out_length(channel)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001081
1082 This function returns the length of the output part of the buffer.
1083
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001084 :param class_channel channel: The manipulated Channel.
Pieter Baauw4d7f7662015-11-08 16:38:08 +01001085 :returns: an integer containing the amount of available bytes.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001086
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001087.. js:function:: Channel.forward(channel, int)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001088
1089 This function transfer bytes from the input part of the buffer to the output
1090 part.
1091
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001092 :param class_channel channel: The manipulated Channel.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001093 :param integer int: The amount of data which will be forwarded.
1094
Thierry FOURNIER / OZON.IO65192f32016-11-07 15:28:40 +01001095.. js:function:: Channel.is_full(channel)
1096
1097 This function returns true if the buffer channel is full.
1098
1099 :returns: a boolean
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001100
1101.. _http_class:
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001102
1103HTTP class
1104==========
1105
1106.. js:class:: HTTP
1107
1108 This class contain all the HTTP manipulation functions.
1109
Pieter Baauw386a1272015-08-16 15:26:24 +02001110.. js:function:: HTTP.req_get_headers(http)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001111
1112 Returns an array containing all the request headers.
1113
1114 :param class_http http: The related http object.
1115 :returns: array of headers.
Pieter Baauw386a1272015-08-16 15:26:24 +02001116 :see: HTTP.res_get_headers()
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001117
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001118 This is the form of the returned array:
1119
1120.. code-block:: lua
1121
1122 HTTP:req_get_headers()['<header-name>'][<header-index>] = "<header-value>"
1123
1124 local hdr = HTTP:req_get_headers()
1125 hdr["host"][0] = "www.test.com"
1126 hdr["accept"][0] = "audio/basic q=1"
1127 hdr["accept"][1] = "audio/*, q=0.2"
1128 hdr["accept"][2] = "*/*, q=0.1"
1129..
1130
Pieter Baauw386a1272015-08-16 15:26:24 +02001131.. js:function:: HTTP.res_get_headers(http)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001132
1133 Returns an array containing all the response headers.
1134
1135 :param class_http http: The related http object.
1136 :returns: array of headers.
Pieter Baauw386a1272015-08-16 15:26:24 +02001137 :see: HTTP.req_get_headers()
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001138
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001139 This is the form of the returned array:
1140
1141.. code-block:: lua
1142
1143 HTTP:res_get_headers()['<header-name>'][<header-index>] = "<header-value>"
1144
1145 local hdr = HTTP:req_get_headers()
1146 hdr["host"][0] = "www.test.com"
1147 hdr["accept"][0] = "audio/basic q=1"
1148 hdr["accept"][1] = "audio/*, q=0.2"
1149 hdr["accept"][2] = "*.*, q=0.1"
1150..
1151
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001152.. js:function:: HTTP.req_add_header(http, name, value)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001153
1154 Appends an HTTP header field in the request whose name is
1155 specified in "name" and whose value is defined in "value".
1156
1157 :param class_http http: The related http object.
1158 :param string name: The header name.
1159 :param string value: The header value.
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001160 :see: HTTP.res_add_header()
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001161
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001162.. js:function:: HTTP.res_add_header(http, name, value)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001163
1164 appends an HTTP header field in the response whose name is
1165 specified in "name" and whose value is defined in "value".
1166
1167 :param class_http http: The related http object.
1168 :param string name: The header name.
1169 :param string value: The header value.
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001170 :see: HTTP.req_add_header()
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001171
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001172.. js:function:: HTTP.req_del_header(http, name)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001173
1174 Removes all HTTP header fields in the request whose name is
1175 specified in "name".
1176
1177 :param class_http http: The related http object.
1178 :param string name: The header name.
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001179 :see: HTTP.res_del_header()
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001180
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001181.. js:function:: HTTP.res_del_header(http, name)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001182
1183 Removes all HTTP header fields in the response whose name is
1184 specified in "name".
1185
1186 :param class_http http: The related http object.
1187 :param string name: The header name.
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001188 :see: HTTP.req_del_header()
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001189
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001190.. js:function:: HTTP.req_set_header(http, name, value)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001191
1192 This variable replace all occurence of all header "name", by only
1193 one containing the "value".
1194
1195 :param class_http http: The related http object.
1196 :param string name: The header name.
1197 :param string value: The header value.
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001198 :see: HTTP.res_set_header()
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001199
1200 This function does the same work as the folowwing code:
1201
1202.. code-block:: lua
1203
1204 function fcn(txn)
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001205 TXN.http:req_del_header("header")
1206 TXN.http:req_add_header("header", "value")
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001207 end
1208..
1209
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001210.. js:function:: HTTP.res_set_header(http, name, value)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001211
1212 This variable replace all occurence of all header "name", by only
1213 one containing the "value".
1214
1215 :param class_http http: The related http object.
1216 :param string name: The header name.
1217 :param string value: The header value.
Pieter Baauw386a1272015-08-16 15:26:24 +02001218 :see: HTTP.req_rep_header()
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001219
Pieter Baauw386a1272015-08-16 15:26:24 +02001220.. js:function:: HTTP.req_rep_header(http, name, regex, replace)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001221
1222 Matches the regular expression in all occurrences of header field "name"
1223 according to "regex", and replaces them with the "replace" argument. The
1224 replacement value can contain back references like \1, \2, ... This
1225 function works with the request.
1226
1227 :param class_http http: The related http object.
1228 :param string name: The header name.
1229 :param string regex: The match regular expression.
1230 :param string replace: The replacement value.
Pieter Baauw386a1272015-08-16 15:26:24 +02001231 :see: HTTP.res_rep_header()
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001232
Pieter Baauw386a1272015-08-16 15:26:24 +02001233.. js:function:: HTTP.res_rep_header(http, name, regex, string)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001234
1235 Matches the regular expression in all occurrences of header field "name"
1236 according to "regex", and replaces them with the "replace" argument. The
1237 replacement value can contain back references like \1, \2, ... This
1238 function works with the request.
1239
1240 :param class_http http: The related http object.
1241 :param string name: The header name.
1242 :param string regex: The match regular expression.
1243 :param string replace: The replacement value.
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001244 :see: HTTP.req_replace_header()
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001245
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001246.. js:function:: HTTP.req_set_method(http, method)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001247
1248 Rewrites the request method with the parameter "method".
1249
1250 :param class_http http: The related http object.
1251 :param string method: The new method.
1252
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001253.. js:function:: HTTP.req_set_path(http, path)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001254
1255 Rewrites the request path with the "path" parameter.
1256
1257 :param class_http http: The related http object.
1258 :param string path: The new path.
1259
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001260.. js:function:: HTTP.req_set_query(http, query)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001261
1262 Rewrites the request's query string which appears after the first question
1263 mark ("?") with the parameter "query".
1264
1265 :param class_http http: The related http object.
1266 :param string query: The new query.
1267
Thierry FOURNIER0d79cf62015-08-26 14:20:58 +02001268.. js:function:: HTTP.req_set_uri(http, uri)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001269
1270 Rewrites the request URI with the parameter "uri".
1271
1272 :param class_http http: The related http object.
1273 :param string uri: The new uri.
1274
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +02001275.. js:function:: HTTP.res_set_status(http, status)
1276
1277 Rewrites the response status code with the parameter "code". Note that the
1278 reason is automatically adapted to the new code.
1279
1280 :param class_http http: The related http object.
1281 :param integer status: The new response status code.
1282
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001283.. _txn_class:
1284
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001285TXN class
1286=========
1287
1288.. js:class:: TXN
1289
1290 The txn class contain all the functions relative to the http or tcp
1291 transaction (Note than a tcp stream is the same than a tcp transaction, but
1292 an HTTP transaction is not the same than a tcp stream).
1293
1294 The usage of this class permits to retrieve data from the requests, alter it
1295 and forward it.
1296
1297 All the functions provided by this class are available in the context
1298 **sample-fetches** and **actions**.
1299
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001300.. js:attribute:: TXN.c
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001301
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001302 :returns: An :ref:`converters_class`.
1303
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001304 This attribute contains a Converters class object.
1305
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001306.. js:attribute:: TXN.sc
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001307
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001308 :returns: An :ref:`converters_class`.
1309
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001310 This attribute contains a Converters class object. The functions of
1311 this object returns always a string.
1312
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001313.. js:attribute:: TXN.f
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001314
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001315 :returns: An :ref:`fetches_class`.
1316
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001317 This attribute contains a Fetches class object.
1318
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001319.. js:attribute:: TXN.sf
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001320
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001321 :returns: An :ref:`fetches_class`.
1322
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001323 This attribute contains a Fetches class object. The functions of
1324 this object returns always a string.
1325
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001326.. js:attribute:: TXN.req
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001327
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001328 :returns: An :ref:`channel_class`.
1329
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001330 This attribute contains a channel class object for the request buffer.
1331
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001332.. js:attribute:: TXN.res
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001333
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001334 :returns: An :ref:`channel_class`.
1335
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001336 This attribute contains a channel class object for the response buffer.
1337
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001338.. js:attribute:: TXN.http
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001339
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001340 :returns: An :ref:`http_class`.
1341
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001342 This attribute contains an HTTP class object. It is avalaible only if the
1343 proxy has the "mode http" enabled.
1344
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01001345.. js:function:: TXN.log(TXN, loglevel, msg)
1346
1347 This function sends a log. The log is sent, according with the HAProxy
1348 configuration file, on the default syslog server if it is configured and on
1349 the stderr if it is allowed.
1350
1351 :param class_txn txn: The class txn object containing the data.
1352 :param integer loglevel: Is the log level asociated with the message. It is a
1353 number between 0 and 7.
1354 :param string msg: The log content.
1355 :see: core.emerg, core.alert, core.crit, core.err, core.warning, core.notice,
1356 core.info, core.debug (log level definitions)
1357 :see: TXN.deflog
1358 :see: TXN.Debug
1359 :see: TXN.Info
1360 :see: TXN.Warning
1361 :see: TXN.Alert
1362
1363.. js:function:: TXN.deflog(TXN, msg)
1364
1365 Sends a log line with the default loglevel for the proxy ssociated with the
1366 transaction.
1367
1368 :param class_txn txn: The class txn object containing the data.
1369 :param string msg: The log content.
1370 :see: TXN.log
1371
1372.. js:function:: TXN.Debug(txn, msg)
1373
1374 :param class_txn txn: The class txn object containing the data.
1375 :param string msg: The log content.
1376 :see: TXN.log
1377
1378 Does the same job than:
1379
1380.. code-block:: lua
1381
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001382 function Debug(txn, msg)
1383 TXN.log(txn, core.debug, msg)
1384 end
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01001385..
1386
1387.. js:function:: TXN.Info(txn, msg)
1388
1389 :param class_txn txn: The class txn object containing the data.
1390 :param string msg: The log content.
1391 :see: TXN.log
1392
1393.. code-block:: lua
1394
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001395 function Debug(txn, msg)
1396 TXN.log(txn, core.info, msg)
1397 end
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01001398..
1399
1400.. js:function:: TXN.Warning(txn, msg)
1401
1402 :param class_txn txn: The class txn object containing the data.
1403 :param string msg: The log content.
1404 :see: TXN.log
1405
1406.. code-block:: lua
1407
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001408 function Debug(txn, msg)
1409 TXN.log(txn, core.warning, msg)
1410 end
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01001411..
1412
1413.. js:function:: TXN.Alert(txn, msg)
1414
1415 :param class_txn txn: The class txn object containing the data.
1416 :param string msg: The log content.
1417 :see: TXN.log
1418
1419.. code-block:: lua
1420
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001421 function Debug(txn, msg)
1422 TXN.log(txn, core.alert, msg)
1423 end
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01001424..
1425
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001426.. js:function:: TXN.get_priv(txn)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001427
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001428 Return Lua data stored in the current transaction (with the `TXN.set_priv()`)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001429 function. If no data are stored, it returns a nil value.
1430
1431 :param class_txn txn: The class txn object containing the data.
1432 :returns: the opaque data previsously stored, or nil if nothing is
1433 avalaible.
1434
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001435.. js:function:: TXN.set_priv(txn, data)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001436
1437 Store any data in the current HAProxy transaction. This action replace the
1438 old stored data.
1439
1440 :param class_txn txn: The class txn object containing the data.
1441 :param opaque data: The data which is stored in the transaction.
1442
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +02001443.. js:function:: TXN.set_var(TXN, var, value)
1444
David Carlier61fdf8b2015-10-02 11:59:38 +01001445 Converts a Lua type in a HAProxy type and store it in a variable <var>.
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +02001446
1447 :param class_txn txn: The class txn object containing the data.
1448 :param string var: The variable name according with the HAProxy variable syntax.
Christopher Faulet85d79c92016-11-09 16:54:56 +01001449
1450.. js:function:: TXN.unset_var(TXN, var)
1451
1452 Unset the variable <var>.
1453
1454 :param class_txn txn: The class txn object containing the data.
1455 :param string var: The variable name according with the HAProxy variable syntax.
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +02001456
1457.. js:function:: TXN.get_var(TXN, var)
1458
1459 Returns data stored in the variable <var> converter in Lua type.
1460
1461 :param class_txn txn: The class txn object containing the data.
1462 :param string var: The variable name according with the HAProxy variable syntax.
1463
Willy Tarreaubc183a62015-08-28 10:39:11 +02001464.. js:function:: TXN.done(txn)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001465
Willy Tarreaubc183a62015-08-28 10:39:11 +02001466 This function terminates processing of the transaction and the associated
1467 session. It can be used when a critical error is detected or to terminate
1468 processing after some data have been returned to the client (eg: a redirect).
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001469
Thierry FOURNIERab00df62016-07-14 11:42:37 +02001470 *Warning*: It not make sense to call this function from sample-fetches. In
1471 this case the behaviour of this one is the same than core.done(): it quit
1472 the Lua execution. The transaction is really aborted only from an action
1473 registered function.
1474
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001475 :param class_txn txn: The class txn object containing the data.
1476
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001477.. js:function:: TXN.set_loglevel(txn, loglevel)
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01001478
1479 Is used to change the log level of the current request. The "loglevel" must
1480 be an integer between 0 and 7.
1481
1482 :param class_txn txn: The class txn object containing the data.
1483 :param integer loglevel: The required log level. This variable can be one of
1484 :see: core.<loglevel>
1485
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001486.. js:function:: TXN.set_tos(txn, tos)
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01001487
1488 Is used to set the TOS or DSCP field value of packets sent to the client to
1489 the value passed in "tos" on platforms which support this.
1490
1491 :param class_txn txn: The class txn object containing the data.
1492 :param integer tos: The new TOS os DSCP.
1493
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001494.. js:function:: TXN.set_mark(txn, mark)
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01001495
1496 Is used to set the Netfilter MARK on all packets sent to the client to the
1497 value passed in "mark" on platforms which support it.
1498
1499 :param class_txn txn: The class txn object containing the data.
1500 :param integer mark: The mark value.
1501
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001502.. _socket_class:
1503
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001504Socket class
1505============
1506
1507.. js:class:: Socket
1508
1509 This class must be compatible with the Lua Socket class. Only the 'client'
1510 functions are available. See the Lua Socket documentation:
1511
1512 `http://w3.impa.br/~diego/software/luasocket/tcp.html
1513 <http://w3.impa.br/~diego/software/luasocket/tcp.html>`_
1514
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001515.. js:function:: Socket.close(socket)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001516
1517 Closes a TCP object. The internal socket used by the object is closed and the
1518 local address to which the object was bound is made available to other
1519 applications. No further operations (except for further calls to the close
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001520 method) are allowed on a closed Socket.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001521
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001522 :param class_socket socket: Is the manipulated Socket.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001523
1524 Note: It is important to close all used sockets once they are not needed,
1525 since, in many systems, each socket uses a file descriptor, which are limited
1526 system resources. Garbage-collected objects are automatically closed before
1527 destruction, though.
1528
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02001529.. js:function:: Socket.connect(socket, address[, port])
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001530
1531 Attempts to connect a socket object to a remote host.
1532
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001533
1534 In case of error, the method returns nil followed by a string describing the
1535 error. In case of success, the method returns 1.
1536
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001537 :param class_socket socket: Is the manipulated Socket.
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02001538 :param string address: can be an IP address or a host name. See below for more
1539 information.
1540 :param integer port: must be an integer number in the range [1..64K].
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001541 :returns: 1 or nil.
1542
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02001543 an address field extension permits to use the connect() function to connect to
1544 other stream than TCP. The syntax containing a simpleipv4 or ipv6 address is
1545 the basically expected format. This format requires the port.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001546
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02001547 Other format accepted are a socket path like "/socket/path", it permits to
1548 connect to a socket. abstract namespaces are supported with the prefix
1549 "abns@", and finaly a filedescriotr can be passed with the prefix "fd@".
1550 The prefix "ipv4@", "ipv6@" and "unix@" are also supported. The port can be
1551 passed int the string. The syntax "127.0.0.1:1234" is valid. in this case, the
1552 parameter *port* is ignored.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001553
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001554.. js:function:: Socket.connect_ssl(socket, address, port)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001555
1556 Same behavior than the function socket:connect, but uses SSL.
1557
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001558 :param class_socket socket: Is the manipulated Socket.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001559 :returns: 1 or nil.
1560
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001561.. js:function:: Socket.getpeername(socket)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001562
1563 Returns information about the remote side of a connected client object.
1564
1565 Returns a string with the IP address of the peer, followed by the port number
1566 that peer is using for the connection. In case of error, the method returns
1567 nil.
1568
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001569 :param class_socket socket: Is the manipulated Socket.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001570 :returns: a string containing the server information.
1571
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001572.. js:function:: Socket.getsockname(socket)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001573
1574 Returns the local address information associated to the object.
1575
1576 The method returns a string with local IP address and a number with the port.
1577 In case of error, the method returns nil.
1578
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001579 :param class_socket socket: Is the manipulated Socket.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001580 :returns: a string containing the client information.
1581
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001582.. js:function:: Socket.receive(socket, [pattern [, prefix]])
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001583
1584 Reads data from a client object, according to the specified read pattern.
1585 Patterns follow the Lua file I/O format, and the difference in performance
1586 between all patterns is negligible.
1587
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001588 :param class_socket socket: Is the manipulated Socket.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001589 :param string|integer pattern: Describe what is required (see below).
1590 :param string prefix: A string which will be prefix the returned data.
1591 :returns: a string containing the required data or nil.
1592
1593 Pattern can be any of the following:
1594
1595 * **`*a`**: reads from the socket until the connection is closed. No
1596 end-of-line translation is performed;
1597
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001598 * **`*l`**: reads a line of text from the Socket. The line is terminated by a
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001599 LF character (ASCII 10), optionally preceded by a CR character
1600 (ASCII 13). The CR and LF characters are not included in the
1601 returned line. In fact, all CR characters are ignored by the
1602 pattern. This is the default pattern.
1603
1604 * **number**: causes the method to read a specified number of bytes from the
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001605 Socket. Prefix is an optional string to be concatenated to the
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001606 beginning of any received data before return.
1607
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02001608 * **empty**: If the pattern is left empty, the default option is `*l`.
1609
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001610 If successful, the method returns the received pattern. In case of error, the
1611 method returns nil followed by an error message which can be the string
1612 'closed' in case the connection was closed before the transmission was
1613 completed or the string 'timeout' in case there was a timeout during the
1614 operation. Also, after the error message, the function returns the partial
1615 result of the transmission.
1616
1617 Important note: This function was changed severely. It used to support
1618 multiple patterns (but I have never seen this feature used) and now it
1619 doesn't anymore. Partial results used to be returned in the same way as
1620 successful results. This last feature violated the idea that all functions
1621 should return nil on error. Thus it was changed too.
1622
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001623.. js:function:: Socket.send(socket, data [, start [, end ]])
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001624
1625 Sends data through client object.
1626
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001627 :param class_socket socket: Is the manipulated Socket.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001628 :param string data: The data that will be sent.
1629 :param integer start: The start position in the buffer of the data which will
1630 be sent.
1631 :param integer end: The end position in the buffer of the data which will
1632 be sent.
1633 :returns: see below.
1634
1635 Data is the string to be sent. The optional arguments i and j work exactly
1636 like the standard string.sub Lua function to allow the selection of a
1637 substring to be sent.
1638
1639 If successful, the method returns the index of the last byte within [start,
1640 end] that has been sent. Notice that, if start is 1 or absent, this is
1641 effectively the total number of bytes sent. In case of error, the method
1642 returns nil, followed by an error message, followed by the index of the last
1643 byte within [start, end] that has been sent. You might want to try again from
1644 the byte following that. The error message can be 'closed' in case the
1645 connection was closed before the transmission was completed or the string
1646 'timeout' in case there was a timeout during the operation.
1647
1648 Note: Output is not buffered. For small strings, it is always better to
1649 concatenate them in Lua (with the '..' operator) and send the result in one
1650 call instead of calling the method several times.
1651
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001652.. js:function:: Socket.setoption(socket, option [, value])
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001653
1654 Just implemented for compatibility, this cal does nothing.
1655
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001656.. js:function:: Socket.settimeout(socket, value [, mode])
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001657
1658 Changes the timeout values for the object. All I/O operations are blocking.
1659 That is, any call to the methods send, receive, and accept will block
1660 indefinitely, until the operation completes. The settimeout method defines a
1661 limit on the amount of time the I/O methods can block. When a timeout time
1662 has elapsed, the affected methods give up and fail with an error code.
1663
1664 The amount of time to wait is specified as the value parameter, in seconds.
1665
1666 The timeout modes are bot implemented, the only settable timeout is the
1667 inactivity time waiting for complete the internal buffer send or waiting for
1668 receive data.
1669
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001670 :param class_socket socket: Is the manipulated Socket.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001671 :param integer value: The timeout value.
1672
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001673.. _map_class:
1674
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001675Map class
1676=========
1677
1678.. js:class:: Map
1679
1680 This class permits to do some lookup in HAProxy maps. The declared maps can
1681 be modified during the runtime throught the HAProxy management socket.
1682
1683.. code-block:: lua
1684
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001685 default = "usa"
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001686
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001687 -- Create and load map
1688 geo = Map.new("geo.map", Map.ip);
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001689
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001690 -- Create new fetch that returns the user country
1691 core.register_fetches("country", function(txn)
1692 local src;
1693 local loc;
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001694
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001695 src = txn.f:fhdr("x-forwarded-for");
1696 if (src == nil) then
1697 src = txn.f:src()
1698 if (src == nil) then
1699 return default;
1700 end
1701 end
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001702
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001703 -- Perform lookup
1704 loc = geo:lookup(src);
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001705
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001706 if (loc == nil) then
1707 return default;
1708 end
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001709
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001710 return loc;
1711 end);
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001712
1713.. js:attribute:: Map.int
1714
1715 See the HAProxy configuration.txt file, chapter "Using ACLs and fetching
1716 samples" ans subchapter "ACL basics" to understand this pattern matching
1717 method.
1718
1719.. js:attribute:: Map.ip
1720
1721 See the HAProxy configuration.txt file, chapter "Using ACLs and fetching
1722 samples" ans subchapter "ACL basics" to understand this pattern matching
1723 method.
1724
1725.. js:attribute:: Map.str
1726
1727 See the HAProxy configuration.txt file, chapter "Using ACLs and fetching
1728 samples" ans subchapter "ACL basics" to understand this pattern matching
1729 method.
1730
1731.. js:attribute:: Map.beg
1732
1733 See the HAProxy configuration.txt file, chapter "Using ACLs and fetching
1734 samples" ans subchapter "ACL basics" to understand this pattern matching
1735 method.
1736
1737.. js:attribute:: Map.sub
1738
1739 See the HAProxy configuration.txt file, chapter "Using ACLs and fetching
1740 samples" ans subchapter "ACL basics" to understand this pattern matching
1741 method.
1742
1743.. js:attribute:: Map.dir
1744
1745 See the HAProxy configuration.txt file, chapter "Using ACLs and fetching
1746 samples" ans subchapter "ACL basics" to understand this pattern matching
1747 method.
1748
1749.. js:attribute:: Map.dom
1750
1751 See the HAProxy configuration.txt file, chapter "Using ACLs and fetching
1752 samples" ans subchapter "ACL basics" to understand this pattern matching
1753 method.
1754
1755.. js:attribute:: Map.end
1756
1757 See the HAProxy configuration.txt file, chapter "Using ACLs and fetching
1758 samples" ans subchapter "ACL basics" to understand this pattern matching
1759 method.
1760
1761.. js:attribute:: Map.reg
1762
1763 See the HAProxy configuration.txt file, chapter "Using ACLs and fetching
1764 samples" ans subchapter "ACL basics" to understand this pattern matching
1765 method.
1766
1767
1768.. js:function:: Map.new(file, method)
1769
1770 Creates and load a map.
1771
1772 :param string file: Is the file containing the map.
1773 :param integer method: Is the map pattern matching method. See the attributes
1774 of the Map class.
1775 :returns: a class Map object.
1776 :see: The Map attributes.
1777
1778.. js:function:: Map.lookup(map, str)
1779
1780 Perform a lookup in a map.
1781
1782 :param class_map map: Is the class Map object.
1783 :param string str: Is the string used as key.
1784 :returns: a string containing the result or nil if no match.
1785
1786.. js:function:: Map.slookup(map, str)
1787
1788 Perform a lookup in a map.
1789
1790 :param class_map map: Is the class Map object.
1791 :param string str: Is the string used as key.
1792 :returns: a string containing the result or empty string if no match.
1793
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001794.. _applethttp_class:
1795
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02001796AppletHTTP class
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001797================
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02001798
1799.. js:class:: AppletHTTP
1800
1801 This class is used with applets that requires the 'http' mode. The http applet
1802 can be registered with the *core.register_service()* function. They are used
1803 for processing an http request like a server in back of HAProxy.
1804
1805 This is an hello world sample code:
1806
1807.. code-block:: lua
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001808
Pieter Baauw4d7f7662015-11-08 16:38:08 +01001809 core.register_service("hello-world", "http", function(applet)
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02001810 local response = "Hello World !"
1811 applet:set_status(200)
Pieter Baauw2dcb9bc2015-10-01 22:47:12 +02001812 applet:add_header("content-length", string.len(response))
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02001813 applet:add_header("content-type", "text/plain")
Pieter Baauw2dcb9bc2015-10-01 22:47:12 +02001814 applet:start_response()
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02001815 applet:send(response)
1816 end)
1817
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001818.. js:attribute:: AppletHTTP.c
1819
1820 :returns: A :ref:`converters_class`
1821
1822 This attribute contains a Converters class object.
1823
1824.. js:attribute:: AppletHTTP.sc
1825
1826 :returns: A :ref:`converters_class`
1827
1828 This attribute contains a Converters class object. The
1829 functions of this object returns always a string.
1830
1831.. js:attribute:: AppletHTTP.f
1832
1833 :returns: A :ref:`fetches_class`
1834
1835 This attribute contains a Fetches class object. Note that the
1836 applet execution place cannot access to a valid HAProxy core HTTP
1837 transaction, so some sample fecthes related to the HTTP dependant
1838 values (hdr, path, ...) are not available.
1839
1840.. js:attribute:: AppletHTTP.sf
1841
1842 :returns: A :ref:`fetches_class`
1843
1844 This attribute contains a Fetches class object. The functions of
1845 this object returns always a string. Note that the applet
1846 execution place cannot access to a valid HAProxy core HTTP
1847 transaction, so some sample fecthes related to the HTTP dependant
1848 values (hdr, path, ...) are not available.
1849
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01001850.. js:attribute:: AppletHTTP.method
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001851
1852 :returns: string
1853
1854 The attribute method returns a string containing the HTTP
1855 method.
1856
1857.. js:attribute:: AppletHTTP.version
1858
1859 :returns: string
1860
1861 The attribute version, returns a string containing the HTTP
1862 request version.
1863
1864.. js:attribute:: AppletHTTP.path
1865
1866 :returns: string
1867
1868 The attribute path returns a string containing the HTTP
1869 request path.
1870
1871.. js:attribute:: AppletHTTP.qs
1872
1873 :returns: string
1874
1875 The attribute qs returns a string containing the HTTP
1876 request query string.
1877
1878.. js:attribute:: AppletHTTP.length
1879
1880 :returns: integer
1881
1882 The attribute length returns an integer containing the HTTP
1883 body length.
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02001884
Thierry FOURNIER841475e2015-12-11 17:10:09 +01001885.. js:attribute:: AppletHTTP.headers
1886
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001887 :returns: array
1888
1889 The attribute headers returns an array containing the HTTP
1890 headers. The header names are always in lower case. As the header name can be
1891 encountered more than once in each request, the value is indexed with 0 as
1892 first index value. The array have this form:
1893
1894.. code-block:: lua
1895
1896 AppletHTTP.headers['<header-name>'][<header-index>] = "<header-value>"
1897
1898 AppletHTTP.headers["host"][0] = "www.test.com"
1899 AppletHTTP.headers["accept"][0] = "audio/basic q=1"
1900 AppletHTTP.headers["accept"][1] = "audio/*, q=0.2"
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01001901 AppletHTTP.headers["accept"][2] = "*/*, q=0.1"
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001902..
1903
1904.. js:attribute:: AppletHTTP.headers
1905
Thierry FOURNIER841475e2015-12-11 17:10:09 +01001906 Contains an array containing all the request headers.
1907
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01001908.. js:function:: AppletHTTP.set_status(applet, code)
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02001909
1910 This function sets the HTTP status code for the response. The allowed code are
1911 from 100 to 599.
1912
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01001913 :param class_AppletHTTP applet: An :ref:`applethttp_class`
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02001914 :param integer code: the status code returned to the client.
1915
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01001916.. js:function:: AppletHTTP.add_header(applet, name, value)
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02001917
1918 This function add an header in the response. Duplicated headers are not
1919 collapsed. The special header *content-length* is used to determinate the
1920 response length. If it not exists, a *transfer-encoding: chunked* is set, and
1921 all the write from the funcion *AppletHTTP:send()* become a chunk.
1922
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01001923 :param class_AppletHTTP applet: An :ref:`applethttp_class`
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02001924 :param string name: the header name
1925 :param string value: the header value
1926
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01001927.. js:function:: AppletHTTP.start_response(applet)
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02001928
1929 This function indicates to the HTTP engine that it can process and send the
1930 response headers. After this called we cannot add headers to the response; We
1931 cannot use the *AppletHTTP:send()* function if the
1932 *AppletHTTP:start_response()* is not called.
1933
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01001934 :param class_AppletHTTP applet: An :ref:`applethttp_class`
1935
1936.. js:function:: AppletHTTP.getline(applet)
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02001937
1938 This function returns a string containing one line from the http body. If the
1939 data returned doesn't contains a final '\\n' its assumed than its the last
1940 available data before the end of stream.
1941
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01001942 :param class_AppletHTTP applet: An :ref:`applethttp_class`
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02001943 :returns: a string. The string can be empty if we reach the end of the stream.
1944
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01001945.. js:function:: AppletHTTP.receive(applet, [size])
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02001946
1947 Reads data from the HTTP body, according to the specified read *size*. If the
1948 *size* is missing, the function tries to read all the content of the stream
1949 until the end. If the *size* is bigger than the http body, it returns the
1950 amount of data avalaible.
1951
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01001952 :param class_AppletHTTP applet: An :ref:`applethttp_class`
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02001953 :param integer size: the required read size.
1954 :returns: always return a string,the string can be empty is the connexion is
1955 closed.
1956
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01001957.. js:function:: AppletHTTP.send(applet, msg)
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02001958
1959 Send the message *msg* on the http request body.
1960
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01001961 :param class_AppletHTTP applet: An :ref:`applethttp_class`
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02001962 :param string msg: the message to send.
1963
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01001964.. js:function:: AppletHTTP.get_priv(applet)
1965
1966 Return Lua data stored in the current transaction (with the
1967 `AppletHTTP.set_priv()`) function. If no data are stored, it returns a nil
1968 value.
1969
1970 :param class_AppletHTTP applet: An :ref:`applethttp_class`
1971 :returns: the opaque data previsously stored, or nil if nothing is
1972 avalaible.
1973
1974.. js:function:: AppletHTTP.set_priv(applet, data)
1975
1976 Store any data in the current HAProxy transaction. This action replace the
1977 old stored data.
1978
1979 :param class_AppletHTTP applet: An :ref:`applethttp_class`
1980 :param opaque data: The data which is stored in the transaction.
1981
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001982.. _applettcp_class:
1983
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02001984AppletTCP class
1985===============
1986
1987.. js:class:: AppletTCP
1988
1989 This class is used with applets that requires the 'tcp' mode. The tcp applet
1990 can be registered with the *core.register_service()* function. They are used
1991 for processing a tcp stream like a server in back of HAProxy.
1992
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001993.. js:attribute:: AppletTCP.c
1994
1995 :returns: A :ref:`converters_class`
1996
1997 This attribute contains a Converters class object.
1998
1999.. js:attribute:: AppletTCP.sc
2000
2001 :returns: A :ref:`converters_class`
2002
2003 This attribute contains a Converters class object. The
2004 functions of this object returns always a string.
2005
2006.. js:attribute:: AppletTCP.f
2007
2008 :returns: A :ref:`fetches_class`
2009
2010 This attribute contains a Fetches class object.
2011
2012.. js:attribute:: AppletTCP.sf
2013
2014 :returns: A :ref:`fetches_class`
2015
2016 This attribute contains a Fetches class object.
2017
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01002018.. js:function:: AppletTCP.getline(applet)
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002019
2020 This function returns a string containing one line from the stream. If the
2021 data returned doesn't contains a final '\\n' its assumed than its the last
2022 available data before the end of stream.
2023
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01002024 :param class_AppletTCP applet: An :ref:`applettcp_class`
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002025 :returns: a string. The string can be empty if we reach the end of the stream.
2026
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01002027.. js:function:: AppletTCP.receive(applet, [size])
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002028
2029 Reads data from the TCP stream, according to the specified read *size*. If the
2030 *size* is missing, the function tries to read all the content of the stream
2031 until the end.
2032
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01002033 :param class_AppletTCP applet: An :ref:`applettcp_class`
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002034 :param integer size: the required read size.
2035 :returns: always return a string,the string can be empty is the connexion is
2036 closed.
2037
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01002038.. js:function:: AppletTCP.send(appletmsg)
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002039
2040 Send the message on the stream.
2041
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01002042 :param class_AppletTCP applet: An :ref:`applettcp_class`
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002043 :param string msg: the message to send.
2044
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01002045.. js:function:: AppletTCP.get_priv(applet)
2046
2047 Return Lua data stored in the current transaction (with the
2048 `AppletTCP.set_priv()`) function. If no data are stored, it returns a nil
2049 value.
2050
2051 :param class_AppletTCP applet: An :ref:`applettcp_class`
2052 :returns: the opaque data previsously stored, or nil if nothing is
2053 avalaible.
2054
2055.. js:function:: AppletTCP.set_priv(applet, data)
2056
2057 Store any data in the current HAProxy transaction. This action replace the
2058 old stored data.
2059
2060 :param class_AppletTCP applet: An :ref:`applettcp_class`
2061 :param opaque data: The data which is stored in the transaction.
2062
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002063External Lua libraries
2064======================
2065
2066A lot of useful lua libraries can be found here:
2067
2068* `https://lua-toolbox.com/ <https://lua-toolbox.com/>`_
2069
2070Redis acces:
2071
2072* `https://github.com/nrk/redis-lua <https://github.com/nrk/redis-lua>`_
2073
2074This is an example about the usage of the Redis library with HAProxy. Note that
2075each call of any function of this library can throw an error if the socket
2076connection fails.
2077
2078.. code-block:: lua
2079
2080 -- load the redis library
2081 local redis = require("redis");
2082
2083 function do_something(txn)
2084
2085 -- create and connect new tcp socket
2086 local tcp = core.tcp();
2087 tcp:settimeout(1);
2088 tcp:connect("127.0.0.1", 6379);
2089
2090 -- use the redis library with this new socket
2091 local client = redis.connect({socket=tcp});
2092 client:ping();
2093
2094 end
2095
2096OpenSSL:
2097
2098* `http://mkottman.github.io/luacrypto/index.html
2099 <http://mkottman.github.io/luacrypto/index.html>`_
2100
2101* `https://github.com/brunoos/luasec/wiki
2102 <https://github.com/brunoos/luasec/wiki>`_
Thierry FOURNIER2e4893c2015-03-18 13:37:27 +01002103