blob: cbbab5f7ed6ba52610d5ffa3286257f25705e056 [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 Fournierb1f46562016-01-21 09:46:15 +0100273.. js:function:: core.now()
274
275 **context**: body, init, task, action
276
277 This function returns the current time. The time returned is fixed by the
278 HAProxy core and assures than the hour will be monotnic and that the system
279 call 'gettimeofday' will not be called too. The time is refreshed between each
280 Lua execution or resume, so two consecutive call to the function "now" will
281 probably returns the same result.
282
283 :returns: an array which contains two entries "sec" and "usec". "sec"
284 contains the current at the epoch format, and "usec" contains the
285 current microseconds.
286
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100287.. js:function:: core.msleep(milliseconds)
288
289 **context**: body, init, task, action
290
291 The `core.msleep()` stops the Lua execution between specified milliseconds.
292
293 :param integer milliseconds: the required milliseconds.
294
Thierry FOURNIER8255a752015-09-23 21:03:35 +0200295.. js:function:: core.register_action(name, actions, func)
296
297 **context**: body
298
David Carlier61fdf8b2015-10-02 11:59:38 +0100299 Register a Lua function executed as action. All the registered action can be
Thierry FOURNIER8255a752015-09-23 21:03:35 +0200300 used in HAProxy with the prefix "lua.". An action gets a TXN object class as
301 input.
302
303 :param string name: is the name of the converter.
Willy Tarreau61add3c2015-09-28 15:39:10 +0200304 :param table actions: is a table of string describing the HAProxy actions who
Thierry FOURNIER8255a752015-09-23 21:03:35 +0200305 want to register to. The expected actions are 'tcp-req',
306 'tcp-res', 'http-req' or 'http-res'.
307 :param function func: is the Lua function called to work as converter.
308
309 The prototype of the Lua function used as argument is:
310
311.. code-block:: lua
312
313 function(txn)
314..
315
Thierry FOURNIERdc595002015-12-21 11:13:52 +0100316 * **txn** (:ref:`txn_class`): this is a TXN object used for manipulating the
Thierry FOURNIER8255a752015-09-23 21:03:35 +0200317 current request or TCP stream.
318
Willy Tarreau61add3c2015-09-28 15:39:10 +0200319 Here, an exemple of action registration. the action juste send an 'Hello world'
Thierry FOURNIER8255a752015-09-23 21:03:35 +0200320 in the logs.
321
322.. code-block:: lua
323
324 core.register_action("hello-world", { "tcp-req", "http-req" }, function(txn)
325 txn:Info("Hello world")
326 end)
327..
328
329 This example code is used in HAproxy configuration like this:
330
331::
332
333 frontend tcp_frt
334 mode tcp
335 tcp-request content lua.hello-world
336
337 frontend http_frt
338 mode http
339 http-request lua.hello-world
340
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100341.. js:function:: core.register_converters(name, func)
342
343 **context**: body
344
David Carlier61fdf8b2015-10-02 11:59:38 +0100345 Register a Lua function executed as converter. All the registered converters
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100346 can be used in HAProxy with the prefix "lua.". An converter get a string as
347 input and return a string as output. The registered function can take up to 9
348 values as parameter. All the value are strings.
349
350 :param string name: is the name of the converter.
351 :param function func: is the Lua function called to work as converter.
352
353 The prototype of the Lua function used as argument is:
354
355.. code-block:: lua
356
357 function(str, [p1 [, p2 [, ... [, p5]]]])
358..
359
360 * **str** (*string*): this is the input value automatically converted in
361 string.
362 * **p1** .. **p5** (*string*): this is a list of string arguments declared in
363 the haroxy configuration file. The number of arguments doesn't exceed 5.
364 The order and the nature of these is conventionally choose by the
365 developper.
366
367.. js:function:: core.register_fetches(name, func)
368
369 **context**: body
370
David Carlier61fdf8b2015-10-02 11:59:38 +0100371 Register a Lua function executed as sample fetch. All the registered sample
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100372 fetchs can be used in HAProxy with the prefix "lua.". A Lua sample fetch
373 return a string as output. The registered function can take up to 9 values as
374 parameter. All the value are strings.
375
376 :param string name: is the name of the converter.
377 :param function func: is the Lua function called to work as sample fetch.
378
379 The prototype of the Lua function used as argument is:
380
381.. code-block:: lua
382
383 string function(txn, [p1 [, p2 [, ... [, p5]]]])
384..
385
Thierry FOURNIERdc595002015-12-21 11:13:52 +0100386 * **txn** (:ref:`txn_class`): this is the txn object associated with the current
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100387 request.
388 * **p1** .. **p5** (*string*): this is a list of string arguments declared in
389 the haroxy configuration file. The number of arguments doesn't exceed 5.
390 The order and the nature of these is conventionally choose by the
391 developper.
392 * **Returns**: A string containing some data, ot nil if the value cannot be
393 returned now.
394
395 lua example code:
396
397.. code-block:: lua
398
399 core.register_fetches("hello", function(txn)
400 return "hello"
401 end)
402..
403
404 HAProxy example configuration:
405
406::
407
408 frontend example
409 http-request redirect location /%[lua.hello]
410
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +0200411.. js:function:: core.register_service(name, mode, func)
412
413 **context**: body
414
David Carlier61fdf8b2015-10-02 11:59:38 +0100415 Register a Lua function executed as a service. All the registered service can
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +0200416 be used in HAProxy with the prefix "lua.". A service gets an object class as
417 input according with the required mode.
418
419 :param string name: is the name of the converter.
Willy Tarreau61add3c2015-09-28 15:39:10 +0200420 :param string mode: is string describing the required mode. Only 'tcp' or
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +0200421 'http' are allowed.
422 :param function func: is the Lua function called to work as converter.
423
424 The prototype of the Lua function used as argument is:
425
426.. code-block:: lua
427
428 function(applet)
429..
430
Thierry FOURNIERdc595002015-12-21 11:13:52 +0100431 * **applet** *applet* will be a :ref:`applettcp_class` or a
432 :ref:`applethttp_class`. It depends the type of registered applet. An applet
433 registered with the 'http' value for the *mode* parameter will gets a
434 :ref:`applethttp_class`. If the *mode* value is 'tcp', the applet will gets
435 a :ref:`applettcp_class`.
436
437 **warning**: Applets of type 'http' cannot be called from 'tcp-*'
438 rulesets. Only the 'http-*' rulesets are authorized, this means
439 that is not possible to call an HTTP applet from a proxy in tcp
440 mode. Applets of type 'tcp' can be called from anywhre.
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +0200441
Willy Tarreau61add3c2015-09-28 15:39:10 +0200442 Here, an exemple of service registration. the service just send an 'Hello world'
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +0200443 as an http response.
444
445.. code-block:: lua
446
Pieter Baauw4d7f7662015-11-08 16:38:08 +0100447 core.register_service("hello-world", "http", function(applet)
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +0200448 local response = "Hello World !"
449 applet:set_status(200)
Pieter Baauw2dcb9bc2015-10-01 22:47:12 +0200450 applet:add_header("content-length", string.len(response))
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +0200451 applet:add_header("content-type", "text/plain")
Pieter Baauw2dcb9bc2015-10-01 22:47:12 +0200452 applet:start_response()
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +0200453 applet:send(response)
454 end)
455..
456
457 This example code is used in HAproxy configuration like this:
458
459::
460
461 frontend example
462 http-request use-service lua.hello-world
463
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100464.. js:function:: core.register_init(func)
465
466 **context**: body
467
468 Register a function executed after the configuration parsing. This is useful
469 to check any parameters.
470
Pieter Baauw4d7f7662015-11-08 16:38:08 +0100471 :param function func: is the Lua function called to work as initializer.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100472
473 The prototype of the Lua function used as argument is:
474
475.. code-block:: lua
476
477 function()
478..
479
480 It takes no input, and no output is expected.
481
482.. js:function:: core.register_task(func)
483
484 **context**: body, init, task, action, sample-fetch, converter
485
486 Register and start independent task. The task is started when the HAProxy
487 main scheduler starts. For example this type of tasks can be executed to
Thierry FOURNIER2e4893c2015-03-18 13:37:27 +0100488 perform complex health checks.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100489
Pieter Baauw4d7f7662015-11-08 16:38:08 +0100490 :param function func: is the Lua function called to work as initializer.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100491
492 The prototype of the Lua function used as argument is:
493
494.. code-block:: lua
495
496 function()
497..
498
499 It takes no input, and no output is expected.
500
501.. js:function:: core.set_nice(nice)
502
503 **context**: task, action, sample-fetch, converter
504
505 Change the nice of the current task or current session.
Thierry FOURNIER2e4893c2015-03-18 13:37:27 +0100506
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100507 :param integer nice: the nice value, it must be between -1024 and 1024.
508
509.. js:function:: core.set_map(filename, key, value)
510
511 **context**: init, task, action, sample-fetch, converter
512
513 set the value *value* associated to the key *key* in the map referenced by
514 *filename*.
515
Thierry FOURNIERdc595002015-12-21 11:13:52 +0100516 :param string filename: the Map reference
517 :param string key: the key to set or replace
518 :param string value: the associated value
519
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100520.. js:function:: core.sleep(int seconds)
521
522 **context**: body, init, task, action
523
524 The `core.sleep()` functions stop the Lua execution between specified seconds.
525
526 :param integer seconds: the required seconds.
527
528.. js:function:: core.tcp()
529
530 **context**: init, task, action
531
532 This function returns a new object of a *socket* class.
533
Thierry FOURNIERdc595002015-12-21 11:13:52 +0100534 :returns: A :ref:`socket_class` object.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100535
Thierry Fournier1de16592016-01-27 09:49:07 +0100536.. js:function:: core.concat()
537
538 **context**: body, init, task, action, sample-fetch, converter
539
540 This function retruns a new concat object.
541
542 :returns: A :ref:`concat_class` object.
543
Thierry FOURNIER0a99b892015-08-26 00:14:17 +0200544.. js:function:: core.done(data)
545
546 **context**: body, init, task, action, sample-fetch, converter
547
548 :param any data: Return some data for the caller. It is useful with
549 sample-fetches and sample-converters.
550
551 Immediately stops the current Lua execution and returns to the caller which
552 may be a sample fetch, a converter or an action and returns the specified
553 value (ignored for actions). It is used when the LUA process finishes its
554 work and wants to give back the control to HAProxy without executing the
555 remaining code. It can be seen as a multi-level "return".
556
Thierry FOURNIER486f5a02015-03-16 15:13:03 +0100557.. js:function:: core.yield()
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100558
559 **context**: task, action, sample-fetch, converter
560
561 Give back the hand at the HAProxy scheduler. It is used when the LUA
562 processing consumes a lot of processing time.
563
Thierry Fournier1de16592016-01-27 09:49:07 +0100564.. _concat_class:
565
566Concat class
567============
568
569.. js:class:: Concat
570
571 This class provides a fast way for string concatenation. The way using native
572 Lua concatenation like the code below is slow for some reasons.
573
574.. code-block:: lua
575
576 str = "string1"
577 str = str .. ", string2"
578 str = str .. ", string3"
579..
580
581 For each concatenation, Lua:
582 * allocate memory for the result,
583 * catenate the two string copying the strings in the new memory bloc,
584 * free the old memory block containing the string whoch is no longer used.
585 This process does many memory move, allocation and free. In addition, the
586 memory is not really freed, it is just mark mark as unsused and wait for the
587 garbage collector.
588
589 The Concat class provide an alternative way for catenating strings. It uses
590 the internal Lua mechanism (it does not allocate memory), but it doesn't copy
591 the data more than once.
592
593 On my computer, the following loops spends 0.2s for the Concat method and
594 18.5s for the pure Lua implementation. So, the Concat class is about 1000x
595 faster than the embedded solution.
596
597.. code-block:: lua
598
599 for j = 1, 100 do
600 c = core.concat()
601 for i = 1, 20000 do
602 c:add("#####")
603 end
604 end
605..
606
607.. code-block:: lua
608
609 for j = 1, 100 do
610 c = ""
611 for i = 1, 20000 do
612 c = c .. "#####"
613 end
614 end
615..
616
617.. js:function:: Concat.add(concat, string)
618
619 This function adds a string to the current concatenated string.
620
621 :param class_concat concat: A :ref:`concat_class` which contains the currently
622 builded string.
623 :param string string: A new string to concatenate to the current builded
624 string.
625
626.. js:function:: Concat.dump(concat)
627
628 This function returns the concanated string.
629
630 :param class_concat concat: A :ref:`concat_class` which contains the currently
631 builded string.
632 :returns: the concatenated string
633
Thierry FOURNIERdc595002015-12-21 11:13:52 +0100634.. _fetches_class:
635
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100636Fetches class
637=============
638
639.. js:class:: Fetches
640
641 This class contains a lot of internal HAProxy sample fetches. See the
Thierry FOURNIER2e4893c2015-03-18 13:37:27 +0100642 HAProxy "configuration.txt" documentation for more information about her
643 usage. they are the chapters 7.3.2 to 7.3.6.
644
Thierry FOURNIERdc595002015-12-21 11:13:52 +0100645 **warning** some sample fetches are not available in some context. These
646 limitations are specified in this documentation when theire useful.
647
Thierry FOURNIER2e4893c2015-03-18 13:37:27 +0100648 :see: TXN.f
649 :see: TXN.sf
650
651 Fetches are useful for:
652
653 * get system time,
654 * get environment variable,
655 * get random numbers,
656 * known backend status like the number of users in queue or the number of
657 connections established,
658 * client information like ip source or destination,
659 * deal with stick tables,
660 * Established SSL informations,
661 * HTTP information like headers or method.
662
663.. code-block:: lua
664
Thierry FOURNIERdc595002015-12-21 11:13:52 +0100665 function action(txn)
666 -- Get source IP
667 local clientip = txn.f:src()
668 end
Thierry FOURNIER2e4893c2015-03-18 13:37:27 +0100669..
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100670
Thierry FOURNIERdc595002015-12-21 11:13:52 +0100671.. _converters_class:
672
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100673Converters class
674================
675
676.. js:class:: Converters
677
678 This class contains a lot of internal HAProxy sample converters. See the
Thierry FOURNIER2e4893c2015-03-18 13:37:27 +0100679 HAProxy documentation "configuration.txt" for more information about her
680 usage. Its the chapter 7.3.1.
681
682 :see: TXN.c
683 :see: TXN.sc
684
685 Converters provides statefull transformation. They are useful for:
686
687 * converting input to base64,
688 * applying hash on input string (djb2, crc32, sdbm, wt6),
689 * format date,
690 * json escape,
Pieter Baauw4d7f7662015-11-08 16:38:08 +0100691 * extracting preferred language comparing two lists,
Thierry FOURNIER2e4893c2015-03-18 13:37:27 +0100692 * turn to lower or upper chars,
693 * deal with stick tables.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100694
Thierry FOURNIERdc595002015-12-21 11:13:52 +0100695.. _channel_class:
696
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100697Channel class
698=============
699
700.. js:class:: Channel
701
702 HAProxy uses two buffers for the processing of the requests. The first one is
703 used with the request data (from the client to the server) and the second is
704 used for the response data (from the server to the client).
705
706 Each buffer contains two types of data. The first type is the incoming data
707 waiting for a processing. The second part is the outgoing data already
708 processed. Usually, the incoming data is processed, after it is tagged as
709 outgoing data, and finally it is sent. The following functions provides tools
710 for manipulating these data in a buffer.
711
712 The following diagram shows where the channel class function are applied.
713
714 **Warning**: It is not possible to read from the response in request action,
715 and it is not possible to read for the request channel in response action.
716
Thierry FOURNIER2e4893c2015-03-18 13:37:27 +0100717.. image:: _static/channel.png
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100718
Thierry FOURNIER486f5a02015-03-16 15:13:03 +0100719.. js:function:: Channel.dup(channel)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100720
721 This function returns a string that contain the entire buffer. The data is
722 not remove from the buffer and can be reprocessed later.
723
724 If the buffer cant receive more data, a 'nil' value is returned.
725
Thierry FOURNIER486f5a02015-03-16 15:13:03 +0100726 :param class_channel channel: The manipulated Channel.
Pieter Baauw4d7f7662015-11-08 16:38:08 +0100727 :returns: a string containing all the available data or nil.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100728
Thierry FOURNIER486f5a02015-03-16 15:13:03 +0100729.. js:function:: Channel.get(channel)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100730
731 This function returns a string that contain the entire buffer. The data is
732 consumed from the buffer.
733
734 If the buffer cant receive more data, a 'nil' value is returned.
735
Thierry FOURNIER486f5a02015-03-16 15:13:03 +0100736 :param class_channel channel: The manipulated Channel.
Pieter Baauw4d7f7662015-11-08 16:38:08 +0100737 :returns: a string containing all the available data or nil.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100738
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +0200739.. js:function:: Channel.getline(channel)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100740
741 This function returns a string that contain the first line of the buffer. The
742 data is consumed. If the data returned doesn't contains a final '\n' its
743 assumed than its the last available data in the buffer.
744
745 If the buffer cant receive more data, a 'nil' value is returned.
746
Thierry FOURNIER486f5a02015-03-16 15:13:03 +0100747 :param class_channel channel: The manipulated Channel.
Pieter Baauw386a1272015-08-16 15:26:24 +0200748 :returns: a string containing the available line or nil.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100749
Thierry FOURNIER486f5a02015-03-16 15:13:03 +0100750.. js:function:: Channel.set(channel, string)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100751
752 This function replace the content of the buffer by the string. The function
753 returns the copied length, otherwise, it returns -1.
754
755 The data set with this function are not send. They wait for the end of
756 HAProxy processing, so the buffer can be full.
757
Thierry FOURNIER486f5a02015-03-16 15:13:03 +0100758 :param class_channel channel: The manipulated Channel.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100759 :param string string: The data which will sent.
Pieter Baauw4d7f7662015-11-08 16:38:08 +0100760 :returns: an integer containing the amount of bytes copied or -1.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100761
Thierry FOURNIER486f5a02015-03-16 15:13:03 +0100762.. js:function:: Channel.append(channel, string)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100763
764 This function append the string argument to the content of the buffer. The
765 function returns the copied length, otherwise, it returns -1.
766
767 The data set with this function are not send. They wait for the end of
768 HAProxy processing, so the buffer can be full.
769
Thierry FOURNIER486f5a02015-03-16 15:13:03 +0100770 :param class_channel channel: The manipulated Channel.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100771 :param string string: The data which will sent.
Pieter Baauw4d7f7662015-11-08 16:38:08 +0100772 :returns: an integer containing the amount of bytes copied or -1.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100773
Thierry FOURNIER486f5a02015-03-16 15:13:03 +0100774.. js:function:: Channel.send(channel, string)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100775
776 This function required immediate send of the data. Unless if the connection
777 is close, the buffer is regularly flushed and all the string can be sent.
778
Thierry FOURNIER486f5a02015-03-16 15:13:03 +0100779 :param class_channel channel: The manipulated Channel.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100780 :param string string: The data which will sent.
Pieter Baauw4d7f7662015-11-08 16:38:08 +0100781 :returns: an integer containing the amount of bytes copied or -1.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100782
Thierry FOURNIER486f5a02015-03-16 15:13:03 +0100783.. js:function:: Channel.get_in_length(channel)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100784
785 This function returns the length of the input part of the buffer.
786
Thierry FOURNIER486f5a02015-03-16 15:13:03 +0100787 :param class_channel channel: The manipulated Channel.
Pieter Baauw4d7f7662015-11-08 16:38:08 +0100788 :returns: an integer containing the amount of available bytes.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100789
Thierry FOURNIER486f5a02015-03-16 15:13:03 +0100790.. js:function:: Channel.get_out_length(channel)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100791
792 This function returns the length of the output part of the buffer.
793
Thierry FOURNIER486f5a02015-03-16 15:13:03 +0100794 :param class_channel channel: The manipulated Channel.
Pieter Baauw4d7f7662015-11-08 16:38:08 +0100795 :returns: an integer containing the amount of available bytes.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100796
Thierry FOURNIER486f5a02015-03-16 15:13:03 +0100797.. js:function:: Channel.forward(channel, int)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100798
799 This function transfer bytes from the input part of the buffer to the output
800 part.
801
Thierry FOURNIER486f5a02015-03-16 15:13:03 +0100802 :param class_channel channel: The manipulated Channel.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100803 :param integer int: The amount of data which will be forwarded.
804
Thierry FOURNIERdc595002015-12-21 11:13:52 +0100805
806.. _http_class:
Thierry FOURNIER08504f42015-03-16 14:17:08 +0100807
808HTTP class
809==========
810
811.. js:class:: HTTP
812
813 This class contain all the HTTP manipulation functions.
814
Pieter Baauw386a1272015-08-16 15:26:24 +0200815.. js:function:: HTTP.req_get_headers(http)
Thierry FOURNIER08504f42015-03-16 14:17:08 +0100816
817 Returns an array containing all the request headers.
818
819 :param class_http http: The related http object.
820 :returns: array of headers.
Pieter Baauw386a1272015-08-16 15:26:24 +0200821 :see: HTTP.res_get_headers()
Thierry FOURNIER08504f42015-03-16 14:17:08 +0100822
Thierry FOURNIERdc595002015-12-21 11:13:52 +0100823 This is the form of the returned array:
824
825.. code-block:: lua
826
827 HTTP:req_get_headers()['<header-name>'][<header-index>] = "<header-value>"
828
829 local hdr = HTTP:req_get_headers()
830 hdr["host"][0] = "www.test.com"
831 hdr["accept"][0] = "audio/basic q=1"
832 hdr["accept"][1] = "audio/*, q=0.2"
833 hdr["accept"][2] = "*/*, q=0.1"
834..
835
Pieter Baauw386a1272015-08-16 15:26:24 +0200836.. js:function:: HTTP.res_get_headers(http)
Thierry FOURNIER08504f42015-03-16 14:17:08 +0100837
838 Returns an array containing all the response headers.
839
840 :param class_http http: The related http object.
841 :returns: array of headers.
Pieter Baauw386a1272015-08-16 15:26:24 +0200842 :see: HTTP.req_get_headers()
Thierry FOURNIER08504f42015-03-16 14:17:08 +0100843
Thierry FOURNIERdc595002015-12-21 11:13:52 +0100844 This is the form of the returned array:
845
846.. code-block:: lua
847
848 HTTP:res_get_headers()['<header-name>'][<header-index>] = "<header-value>"
849
850 local hdr = HTTP:req_get_headers()
851 hdr["host"][0] = "www.test.com"
852 hdr["accept"][0] = "audio/basic q=1"
853 hdr["accept"][1] = "audio/*, q=0.2"
854 hdr["accept"][2] = "*.*, q=0.1"
855..
856
Thierry FOURNIER486f5a02015-03-16 15:13:03 +0100857.. js:function:: HTTP.req_add_header(http, name, value)
Thierry FOURNIER08504f42015-03-16 14:17:08 +0100858
859 Appends an HTTP header field in the request whose name is
860 specified in "name" and whose value is defined in "value".
861
862 :param class_http http: The related http object.
863 :param string name: The header name.
864 :param string value: The header value.
Thierry FOURNIER486f5a02015-03-16 15:13:03 +0100865 :see: HTTP.res_add_header()
Thierry FOURNIER08504f42015-03-16 14:17:08 +0100866
Thierry FOURNIER486f5a02015-03-16 15:13:03 +0100867.. js:function:: HTTP.res_add_header(http, name, value)
Thierry FOURNIER08504f42015-03-16 14:17:08 +0100868
869 appends an HTTP header field in the response whose name is
870 specified in "name" and whose value is defined in "value".
871
872 :param class_http http: The related http object.
873 :param string name: The header name.
874 :param string value: The header value.
Thierry FOURNIER486f5a02015-03-16 15:13:03 +0100875 :see: HTTP.req_add_header()
Thierry FOURNIER08504f42015-03-16 14:17:08 +0100876
Thierry FOURNIER486f5a02015-03-16 15:13:03 +0100877.. js:function:: HTTP.req_del_header(http, name)
Thierry FOURNIER08504f42015-03-16 14:17:08 +0100878
879 Removes all HTTP header fields in the request whose name is
880 specified in "name".
881
882 :param class_http http: The related http object.
883 :param string name: The header name.
Thierry FOURNIER486f5a02015-03-16 15:13:03 +0100884 :see: HTTP.res_del_header()
Thierry FOURNIER08504f42015-03-16 14:17:08 +0100885
Thierry FOURNIER486f5a02015-03-16 15:13:03 +0100886.. js:function:: HTTP.res_del_header(http, name)
Thierry FOURNIER08504f42015-03-16 14:17:08 +0100887
888 Removes all HTTP header fields in the response whose name is
889 specified in "name".
890
891 :param class_http http: The related http object.
892 :param string name: The header name.
Thierry FOURNIER486f5a02015-03-16 15:13:03 +0100893 :see: HTTP.req_del_header()
Thierry FOURNIER08504f42015-03-16 14:17:08 +0100894
Thierry FOURNIER486f5a02015-03-16 15:13:03 +0100895.. js:function:: HTTP.req_set_header(http, name, value)
Thierry FOURNIER08504f42015-03-16 14:17:08 +0100896
897 This variable replace all occurence of all header "name", by only
898 one containing the "value".
899
900 :param class_http http: The related http object.
901 :param string name: The header name.
902 :param string value: The header value.
Thierry FOURNIER486f5a02015-03-16 15:13:03 +0100903 :see: HTTP.res_set_header()
Thierry FOURNIER08504f42015-03-16 14:17:08 +0100904
905 This function does the same work as the folowwing code:
906
907.. code-block:: lua
908
909 function fcn(txn)
Thierry FOURNIER486f5a02015-03-16 15:13:03 +0100910 TXN.http:req_del_header("header")
911 TXN.http:req_add_header("header", "value")
Thierry FOURNIER08504f42015-03-16 14:17:08 +0100912 end
913..
914
Thierry FOURNIER486f5a02015-03-16 15:13:03 +0100915.. js:function:: HTTP.res_set_header(http, name, value)
Thierry FOURNIER08504f42015-03-16 14:17:08 +0100916
917 This variable replace all occurence of all header "name", by only
918 one containing the "value".
919
920 :param class_http http: The related http object.
921 :param string name: The header name.
922 :param string value: The header value.
Pieter Baauw386a1272015-08-16 15:26:24 +0200923 :see: HTTP.req_rep_header()
Thierry FOURNIER08504f42015-03-16 14:17:08 +0100924
Pieter Baauw386a1272015-08-16 15:26:24 +0200925.. js:function:: HTTP.req_rep_header(http, name, regex, replace)
Thierry FOURNIER08504f42015-03-16 14:17:08 +0100926
927 Matches the regular expression in all occurrences of header field "name"
928 according to "regex", and replaces them with the "replace" argument. The
929 replacement value can contain back references like \1, \2, ... This
930 function works with the request.
931
932 :param class_http http: The related http object.
933 :param string name: The header name.
934 :param string regex: The match regular expression.
935 :param string replace: The replacement value.
Pieter Baauw386a1272015-08-16 15:26:24 +0200936 :see: HTTP.res_rep_header()
Thierry FOURNIER08504f42015-03-16 14:17:08 +0100937
Pieter Baauw386a1272015-08-16 15:26:24 +0200938.. js:function:: HTTP.res_rep_header(http, name, regex, string)
Thierry FOURNIER08504f42015-03-16 14:17:08 +0100939
940 Matches the regular expression in all occurrences of header field "name"
941 according to "regex", and replaces them with the "replace" argument. The
942 replacement value can contain back references like \1, \2, ... This
943 function works with the request.
944
945 :param class_http http: The related http object.
946 :param string name: The header name.
947 :param string regex: The match regular expression.
948 :param string replace: The replacement value.
Thierry FOURNIER486f5a02015-03-16 15:13:03 +0100949 :see: HTTP.req_replace_header()
Thierry FOURNIER08504f42015-03-16 14:17:08 +0100950
Thierry FOURNIER486f5a02015-03-16 15:13:03 +0100951.. js:function:: HTTP.req_replace_value(http, name, regex, replace)
Thierry FOURNIER08504f42015-03-16 14:17:08 +0100952
Thierry FOURNIER486f5a02015-03-16 15:13:03 +0100953 Works like "HTTP.req_replace_header()" except that it matches the regex
Thierry FOURNIER08504f42015-03-16 14:17:08 +0100954 against every comma-delimited value of the header field "name" instead of the
955 entire header.
956
957 :param class_http http: The related http object.
958 :param string name: The header name.
959 :param string regex: The match regular expression.
960 :param string replace: The replacement value.
Thierry FOURNIER486f5a02015-03-16 15:13:03 +0100961 :see: HTTP.req_replace_header()
962 :see: HTTP.res_replace_value()
Thierry FOURNIER08504f42015-03-16 14:17:08 +0100963
Thierry FOURNIER486f5a02015-03-16 15:13:03 +0100964.. js:function:: HTTP.res_replace_value(http, name, regex, replace)
Thierry FOURNIER08504f42015-03-16 14:17:08 +0100965
Thierry FOURNIER486f5a02015-03-16 15:13:03 +0100966 Works like "HTTP.res_replace_header()" except that it matches the regex
Thierry FOURNIER08504f42015-03-16 14:17:08 +0100967 against every comma-delimited value of the header field "name" instead of the
968 entire header.
969
970 :param class_http http: The related http object.
971 :param string name: The header name.
972 :param string regex: The match regular expression.
973 :param string replace: The replacement value.
Thierry FOURNIER486f5a02015-03-16 15:13:03 +0100974 :see: HTTP.res_replace_header()
975 :see: HTTP.req_replace_value()
Thierry FOURNIER08504f42015-03-16 14:17:08 +0100976
Thierry FOURNIER486f5a02015-03-16 15:13:03 +0100977.. js:function:: HTTP.req_set_method(http, method)
Thierry FOURNIER08504f42015-03-16 14:17:08 +0100978
979 Rewrites the request method with the parameter "method".
980
981 :param class_http http: The related http object.
982 :param string method: The new method.
983
Thierry FOURNIER486f5a02015-03-16 15:13:03 +0100984.. js:function:: HTTP.req_set_path(http, path)
Thierry FOURNIER08504f42015-03-16 14:17:08 +0100985
986 Rewrites the request path with the "path" parameter.
987
988 :param class_http http: The related http object.
989 :param string path: The new path.
990
Thierry FOURNIER486f5a02015-03-16 15:13:03 +0100991.. js:function:: HTTP.req_set_query(http, query)
Thierry FOURNIER08504f42015-03-16 14:17:08 +0100992
993 Rewrites the request's query string which appears after the first question
994 mark ("?") with the parameter "query".
995
996 :param class_http http: The related http object.
997 :param string query: The new query.
998
Thierry FOURNIER0d79cf62015-08-26 14:20:58 +0200999.. js:function:: HTTP.req_set_uri(http, uri)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001000
1001 Rewrites the request URI with the parameter "uri".
1002
1003 :param class_http http: The related http object.
1004 :param string uri: The new uri.
1005
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +02001006.. js:function:: HTTP.res_set_status(http, status)
1007
1008 Rewrites the response status code with the parameter "code". Note that the
1009 reason is automatically adapted to the new code.
1010
1011 :param class_http http: The related http object.
1012 :param integer status: The new response status code.
1013
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001014.. _txn_class:
1015
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001016TXN class
1017=========
1018
1019.. js:class:: TXN
1020
1021 The txn class contain all the functions relative to the http or tcp
1022 transaction (Note than a tcp stream is the same than a tcp transaction, but
1023 an HTTP transaction is not the same than a tcp stream).
1024
1025 The usage of this class permits to retrieve data from the requests, alter it
1026 and forward it.
1027
1028 All the functions provided by this class are available in the context
1029 **sample-fetches** and **actions**.
1030
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001031.. js:attribute:: TXN.c
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001032
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001033 :returns: An :ref:`converters_class`.
1034
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001035 This attribute contains a Converters class object.
1036
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001037.. js:attribute:: TXN.sc
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001038
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001039 :returns: An :ref:`converters_class`.
1040
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001041 This attribute contains a Converters class object. The functions of
1042 this object returns always a string.
1043
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001044.. js:attribute:: TXN.f
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001045
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001046 :returns: An :ref:`fetches_class`.
1047
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001048 This attribute contains a Fetches class object.
1049
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001050.. js:attribute:: TXN.sf
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001051
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001052 :returns: An :ref:`fetches_class`.
1053
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001054 This attribute contains a Fetches class object. The functions of
1055 this object returns always a string.
1056
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001057.. js:attribute:: TXN.req
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001058
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001059 :returns: An :ref:`channel_class`.
1060
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001061 This attribute contains a channel class object for the request buffer.
1062
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001063.. js:attribute:: TXN.res
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001064
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001065 :returns: An :ref:`channel_class`.
1066
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001067 This attribute contains a channel class object for the response buffer.
1068
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001069.. js:attribute:: TXN.http
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001070
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001071 :returns: An :ref:`http_class`.
1072
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001073 This attribute contains an HTTP class object. It is avalaible only if the
1074 proxy has the "mode http" enabled.
1075
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01001076.. js:function:: TXN.log(TXN, loglevel, msg)
1077
1078 This function sends a log. The log is sent, according with the HAProxy
1079 configuration file, on the default syslog server if it is configured and on
1080 the stderr if it is allowed.
1081
1082 :param class_txn txn: The class txn object containing the data.
1083 :param integer loglevel: Is the log level asociated with the message. It is a
1084 number between 0 and 7.
1085 :param string msg: The log content.
1086 :see: core.emerg, core.alert, core.crit, core.err, core.warning, core.notice,
1087 core.info, core.debug (log level definitions)
1088 :see: TXN.deflog
1089 :see: TXN.Debug
1090 :see: TXN.Info
1091 :see: TXN.Warning
1092 :see: TXN.Alert
1093
1094.. js:function:: TXN.deflog(TXN, msg)
1095
1096 Sends a log line with the default loglevel for the proxy ssociated with the
1097 transaction.
1098
1099 :param class_txn txn: The class txn object containing the data.
1100 :param string msg: The log content.
1101 :see: TXN.log
1102
1103.. js:function:: TXN.Debug(txn, msg)
1104
1105 :param class_txn txn: The class txn object containing the data.
1106 :param string msg: The log content.
1107 :see: TXN.log
1108
1109 Does the same job than:
1110
1111.. code-block:: lua
1112
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001113 function Debug(txn, msg)
1114 TXN.log(txn, core.debug, msg)
1115 end
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01001116..
1117
1118.. js:function:: TXN.Info(txn, msg)
1119
1120 :param class_txn txn: The class txn object containing the data.
1121 :param string msg: The log content.
1122 :see: TXN.log
1123
1124.. code-block:: lua
1125
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001126 function Debug(txn, msg)
1127 TXN.log(txn, core.info, msg)
1128 end
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01001129..
1130
1131.. js:function:: TXN.Warning(txn, msg)
1132
1133 :param class_txn txn: The class txn object containing the data.
1134 :param string msg: The log content.
1135 :see: TXN.log
1136
1137.. code-block:: lua
1138
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001139 function Debug(txn, msg)
1140 TXN.log(txn, core.warning, msg)
1141 end
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01001142..
1143
1144.. js:function:: TXN.Alert(txn, msg)
1145
1146 :param class_txn txn: The class txn object containing the data.
1147 :param string msg: The log content.
1148 :see: TXN.log
1149
1150.. code-block:: lua
1151
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001152 function Debug(txn, msg)
1153 TXN.log(txn, core.alert, msg)
1154 end
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01001155..
1156
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001157.. js:function:: TXN.get_priv(txn)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001158
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001159 Return Lua data stored in the current transaction (with the `TXN.set_priv()`)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001160 function. If no data are stored, it returns a nil value.
1161
1162 :param class_txn txn: The class txn object containing the data.
1163 :returns: the opaque data previsously stored, or nil if nothing is
1164 avalaible.
1165
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001166.. js:function:: TXN.set_priv(txn, data)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001167
1168 Store any data in the current HAProxy transaction. This action replace the
1169 old stored data.
1170
1171 :param class_txn txn: The class txn object containing the data.
1172 :param opaque data: The data which is stored in the transaction.
1173
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +02001174.. js:function:: TXN.set_var(TXN, var, value)
1175
David Carlier61fdf8b2015-10-02 11:59:38 +01001176 Converts a Lua type in a HAProxy type and store it in a variable <var>.
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +02001177
1178 :param class_txn txn: The class txn object containing the data.
1179 :param string var: The variable name according with the HAProxy variable syntax.
1180 :param opaque value: The data which is stored in the variable.
1181
1182.. js:function:: TXN.get_var(TXN, var)
1183
1184 Returns data stored in the variable <var> converter in Lua type.
1185
1186 :param class_txn txn: The class txn object containing the data.
1187 :param string var: The variable name according with the HAProxy variable syntax.
1188
Willy Tarreaubc183a62015-08-28 10:39:11 +02001189.. js:function:: TXN.done(txn)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001190
Willy Tarreaubc183a62015-08-28 10:39:11 +02001191 This function terminates processing of the transaction and the associated
1192 session. It can be used when a critical error is detected or to terminate
1193 processing after some data have been returned to the client (eg: a redirect).
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001194
1195 :param class_txn txn: The class txn object containing the data.
1196
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001197.. js:function:: TXN.set_loglevel(txn, loglevel)
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01001198
1199 Is used to change the log level of the current request. The "loglevel" must
1200 be an integer between 0 and 7.
1201
1202 :param class_txn txn: The class txn object containing the data.
1203 :param integer loglevel: The required log level. This variable can be one of
1204 :see: core.<loglevel>
1205
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001206.. js:function:: TXN.set_tos(txn, tos)
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01001207
1208 Is used to set the TOS or DSCP field value of packets sent to the client to
1209 the value passed in "tos" on platforms which support this.
1210
1211 :param class_txn txn: The class txn object containing the data.
1212 :param integer tos: The new TOS os DSCP.
1213
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001214.. js:function:: TXN.set_mark(txn, mark)
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01001215
1216 Is used to set the Netfilter MARK on all packets sent to the client to the
1217 value passed in "mark" on platforms which support it.
1218
1219 :param class_txn txn: The class txn object containing the data.
1220 :param integer mark: The mark value.
1221
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001222.. _socket_class:
1223
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001224Socket class
1225============
1226
1227.. js:class:: Socket
1228
1229 This class must be compatible with the Lua Socket class. Only the 'client'
1230 functions are available. See the Lua Socket documentation:
1231
1232 `http://w3.impa.br/~diego/software/luasocket/tcp.html
1233 <http://w3.impa.br/~diego/software/luasocket/tcp.html>`_
1234
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001235.. js:function:: Socket.close(socket)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001236
1237 Closes a TCP object. The internal socket used by the object is closed and the
1238 local address to which the object was bound is made available to other
1239 applications. No further operations (except for further calls to the close
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001240 method) are allowed on a closed Socket.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001241
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001242 :param class_socket socket: Is the manipulated Socket.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001243
1244 Note: It is important to close all used sockets once they are not needed,
1245 since, in many systems, each socket uses a file descriptor, which are limited
1246 system resources. Garbage-collected objects are automatically closed before
1247 destruction, though.
1248
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02001249.. js:function:: Socket.connect(socket, address[, port])
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001250
1251 Attempts to connect a socket object to a remote host.
1252
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001253
1254 In case of error, the method returns nil followed by a string describing the
1255 error. In case of success, the method returns 1.
1256
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001257 :param class_socket socket: Is the manipulated Socket.
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02001258 :param string address: can be an IP address or a host name. See below for more
1259 information.
1260 :param integer port: must be an integer number in the range [1..64K].
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001261 :returns: 1 or nil.
1262
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02001263 an address field extension permits to use the connect() function to connect to
1264 other stream than TCP. The syntax containing a simpleipv4 or ipv6 address is
1265 the basically expected format. This format requires the port.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001266
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02001267 Other format accepted are a socket path like "/socket/path", it permits to
1268 connect to a socket. abstract namespaces are supported with the prefix
1269 "abns@", and finaly a filedescriotr can be passed with the prefix "fd@".
1270 The prefix "ipv4@", "ipv6@" and "unix@" are also supported. The port can be
1271 passed int the string. The syntax "127.0.0.1:1234" is valid. in this case, the
1272 parameter *port* is ignored.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001273
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001274.. js:function:: Socket.connect_ssl(socket, address, port)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001275
1276 Same behavior than the function socket:connect, but uses SSL.
1277
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001278 :param class_socket socket: Is the manipulated Socket.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001279 :returns: 1 or nil.
1280
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001281.. js:function:: Socket.getpeername(socket)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001282
1283 Returns information about the remote side of a connected client object.
1284
1285 Returns a string with the IP address of the peer, followed by the port number
1286 that peer is using for the connection. In case of error, the method returns
1287 nil.
1288
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001289 :param class_socket socket: Is the manipulated Socket.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001290 :returns: a string containing the server information.
1291
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001292.. js:function:: Socket.getsockname(socket)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001293
1294 Returns the local address information associated to the object.
1295
1296 The method returns a string with local IP address and a number with the port.
1297 In case of error, the method returns nil.
1298
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001299 :param class_socket socket: Is the manipulated Socket.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001300 :returns: a string containing the client information.
1301
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001302.. js:function:: Socket.receive(socket, [pattern [, prefix]])
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001303
1304 Reads data from a client object, according to the specified read pattern.
1305 Patterns follow the Lua file I/O format, and the difference in performance
1306 between all patterns is negligible.
1307
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001308 :param class_socket socket: Is the manipulated Socket.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001309 :param string|integer pattern: Describe what is required (see below).
1310 :param string prefix: A string which will be prefix the returned data.
1311 :returns: a string containing the required data or nil.
1312
1313 Pattern can be any of the following:
1314
1315 * **`*a`**: reads from the socket until the connection is closed. No
1316 end-of-line translation is performed;
1317
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001318 * **`*l`**: reads a line of text from the Socket. The line is terminated by a
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001319 LF character (ASCII 10), optionally preceded by a CR character
1320 (ASCII 13). The CR and LF characters are not included in the
1321 returned line. In fact, all CR characters are ignored by the
1322 pattern. This is the default pattern.
1323
1324 * **number**: causes the method to read a specified number of bytes from the
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001325 Socket. Prefix is an optional string to be concatenated to the
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001326 beginning of any received data before return.
1327
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02001328 * **empty**: If the pattern is left empty, the default option is `*l`.
1329
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001330 If successful, the method returns the received pattern. In case of error, the
1331 method returns nil followed by an error message which can be the string
1332 'closed' in case the connection was closed before the transmission was
1333 completed or the string 'timeout' in case there was a timeout during the
1334 operation. Also, after the error message, the function returns the partial
1335 result of the transmission.
1336
1337 Important note: This function was changed severely. It used to support
1338 multiple patterns (but I have never seen this feature used) and now it
1339 doesn't anymore. Partial results used to be returned in the same way as
1340 successful results. This last feature violated the idea that all functions
1341 should return nil on error. Thus it was changed too.
1342
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001343.. js:function:: Socket.send(socket, data [, start [, end ]])
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001344
1345 Sends data through client object.
1346
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001347 :param class_socket socket: Is the manipulated Socket.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001348 :param string data: The data that will be sent.
1349 :param integer start: The start position in the buffer of the data which will
1350 be sent.
1351 :param integer end: The end position in the buffer of the data which will
1352 be sent.
1353 :returns: see below.
1354
1355 Data is the string to be sent. The optional arguments i and j work exactly
1356 like the standard string.sub Lua function to allow the selection of a
1357 substring to be sent.
1358
1359 If successful, the method returns the index of the last byte within [start,
1360 end] that has been sent. Notice that, if start is 1 or absent, this is
1361 effectively the total number of bytes sent. In case of error, the method
1362 returns nil, followed by an error message, followed by the index of the last
1363 byte within [start, end] that has been sent. You might want to try again from
1364 the byte following that. The error message can be 'closed' in case the
1365 connection was closed before the transmission was completed or the string
1366 'timeout' in case there was a timeout during the operation.
1367
1368 Note: Output is not buffered. For small strings, it is always better to
1369 concatenate them in Lua (with the '..' operator) and send the result in one
1370 call instead of calling the method several times.
1371
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001372.. js:function:: Socket.setoption(socket, option [, value])
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001373
1374 Just implemented for compatibility, this cal does nothing.
1375
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001376.. js:function:: Socket.settimeout(socket, value [, mode])
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001377
1378 Changes the timeout values for the object. All I/O operations are blocking.
1379 That is, any call to the methods send, receive, and accept will block
1380 indefinitely, until the operation completes. The settimeout method defines a
1381 limit on the amount of time the I/O methods can block. When a timeout time
1382 has elapsed, the affected methods give up and fail with an error code.
1383
1384 The amount of time to wait is specified as the value parameter, in seconds.
1385
1386 The timeout modes are bot implemented, the only settable timeout is the
1387 inactivity time waiting for complete the internal buffer send or waiting for
1388 receive data.
1389
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001390 :param class_socket socket: Is the manipulated Socket.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001391 :param integer value: The timeout value.
1392
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001393.. _map_class:
1394
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001395Map class
1396=========
1397
1398.. js:class:: Map
1399
1400 This class permits to do some lookup in HAProxy maps. The declared maps can
1401 be modified during the runtime throught the HAProxy management socket.
1402
1403.. code-block:: lua
1404
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001405 default = "usa"
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001406
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001407 -- Create and load map
1408 geo = Map.new("geo.map", Map.ip);
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001409
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001410 -- Create new fetch that returns the user country
1411 core.register_fetches("country", function(txn)
1412 local src;
1413 local loc;
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001414
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001415 src = txn.f:fhdr("x-forwarded-for");
1416 if (src == nil) then
1417 src = txn.f:src()
1418 if (src == nil) then
1419 return default;
1420 end
1421 end
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001422
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001423 -- Perform lookup
1424 loc = geo:lookup(src);
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001425
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001426 if (loc == nil) then
1427 return default;
1428 end
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001429
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001430 return loc;
1431 end);
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001432
1433.. js:attribute:: Map.int
1434
1435 See the HAProxy configuration.txt file, chapter "Using ACLs and fetching
1436 samples" ans subchapter "ACL basics" to understand this pattern matching
1437 method.
1438
1439.. js:attribute:: Map.ip
1440
1441 See the HAProxy configuration.txt file, chapter "Using ACLs and fetching
1442 samples" ans subchapter "ACL basics" to understand this pattern matching
1443 method.
1444
1445.. js:attribute:: Map.str
1446
1447 See the HAProxy configuration.txt file, chapter "Using ACLs and fetching
1448 samples" ans subchapter "ACL basics" to understand this pattern matching
1449 method.
1450
1451.. js:attribute:: Map.beg
1452
1453 See the HAProxy configuration.txt file, chapter "Using ACLs and fetching
1454 samples" ans subchapter "ACL basics" to understand this pattern matching
1455 method.
1456
1457.. js:attribute:: Map.sub
1458
1459 See the HAProxy configuration.txt file, chapter "Using ACLs and fetching
1460 samples" ans subchapter "ACL basics" to understand this pattern matching
1461 method.
1462
1463.. js:attribute:: Map.dir
1464
1465 See the HAProxy configuration.txt file, chapter "Using ACLs and fetching
1466 samples" ans subchapter "ACL basics" to understand this pattern matching
1467 method.
1468
1469.. js:attribute:: Map.dom
1470
1471 See the HAProxy configuration.txt file, chapter "Using ACLs and fetching
1472 samples" ans subchapter "ACL basics" to understand this pattern matching
1473 method.
1474
1475.. js:attribute:: Map.end
1476
1477 See the HAProxy configuration.txt file, chapter "Using ACLs and fetching
1478 samples" ans subchapter "ACL basics" to understand this pattern matching
1479 method.
1480
1481.. js:attribute:: Map.reg
1482
1483 See the HAProxy configuration.txt file, chapter "Using ACLs and fetching
1484 samples" ans subchapter "ACL basics" to understand this pattern matching
1485 method.
1486
1487
1488.. js:function:: Map.new(file, method)
1489
1490 Creates and load a map.
1491
1492 :param string file: Is the file containing the map.
1493 :param integer method: Is the map pattern matching method. See the attributes
1494 of the Map class.
1495 :returns: a class Map object.
1496 :see: The Map attributes.
1497
1498.. js:function:: Map.lookup(map, str)
1499
1500 Perform a lookup in a map.
1501
1502 :param class_map map: Is the class Map object.
1503 :param string str: Is the string used as key.
1504 :returns: a string containing the result or nil if no match.
1505
1506.. js:function:: Map.slookup(map, str)
1507
1508 Perform a lookup in a map.
1509
1510 :param class_map map: Is the class Map object.
1511 :param string str: Is the string used as key.
1512 :returns: a string containing the result or empty string if no match.
1513
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001514.. _applethttp_class:
1515
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02001516AppletHTTP class
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001517================
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02001518
1519.. js:class:: AppletHTTP
1520
1521 This class is used with applets that requires the 'http' mode. The http applet
1522 can be registered with the *core.register_service()* function. They are used
1523 for processing an http request like a server in back of HAProxy.
1524
1525 This is an hello world sample code:
1526
1527.. code-block:: lua
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001528
Pieter Baauw4d7f7662015-11-08 16:38:08 +01001529 core.register_service("hello-world", "http", function(applet)
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02001530 local response = "Hello World !"
1531 applet:set_status(200)
Pieter Baauw2dcb9bc2015-10-01 22:47:12 +02001532 applet:add_header("content-length", string.len(response))
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02001533 applet:add_header("content-type", "text/plain")
Pieter Baauw2dcb9bc2015-10-01 22:47:12 +02001534 applet:start_response()
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02001535 applet:send(response)
1536 end)
1537
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001538.. js:attribute:: AppletHTTP.c
1539
1540 :returns: A :ref:`converters_class`
1541
1542 This attribute contains a Converters class object.
1543
1544.. js:attribute:: AppletHTTP.sc
1545
1546 :returns: A :ref:`converters_class`
1547
1548 This attribute contains a Converters class object. The
1549 functions of this object returns always a string.
1550
1551.. js:attribute:: AppletHTTP.f
1552
1553 :returns: A :ref:`fetches_class`
1554
1555 This attribute contains a Fetches class object. Note that the
1556 applet execution place cannot access to a valid HAProxy core HTTP
1557 transaction, so some sample fecthes related to the HTTP dependant
1558 values (hdr, path, ...) are not available.
1559
1560.. js:attribute:: AppletHTTP.sf
1561
1562 :returns: A :ref:`fetches_class`
1563
1564 This attribute contains a Fetches class object. The functions of
1565 this object returns always a string. Note that the applet
1566 execution place cannot access to a valid HAProxy core HTTP
1567 transaction, so some sample fecthes related to the HTTP dependant
1568 values (hdr, path, ...) are not available.
1569
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01001570.. js:attribute:: AppletHTTP.method
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001571
1572 :returns: string
1573
1574 The attribute method returns a string containing the HTTP
1575 method.
1576
1577.. js:attribute:: AppletHTTP.version
1578
1579 :returns: string
1580
1581 The attribute version, returns a string containing the HTTP
1582 request version.
1583
1584.. js:attribute:: AppletHTTP.path
1585
1586 :returns: string
1587
1588 The attribute path returns a string containing the HTTP
1589 request path.
1590
1591.. js:attribute:: AppletHTTP.qs
1592
1593 :returns: string
1594
1595 The attribute qs returns a string containing the HTTP
1596 request query string.
1597
1598.. js:attribute:: AppletHTTP.length
1599
1600 :returns: integer
1601
1602 The attribute length returns an integer containing the HTTP
1603 body length.
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02001604
Thierry FOURNIER841475e2015-12-11 17:10:09 +01001605.. js:attribute:: AppletHTTP.headers
1606
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001607 :returns: array
1608
1609 The attribute headers returns an array containing the HTTP
1610 headers. The header names are always in lower case. As the header name can be
1611 encountered more than once in each request, the value is indexed with 0 as
1612 first index value. The array have this form:
1613
1614.. code-block:: lua
1615
1616 AppletHTTP.headers['<header-name>'][<header-index>] = "<header-value>"
1617
1618 AppletHTTP.headers["host"][0] = "www.test.com"
1619 AppletHTTP.headers["accept"][0] = "audio/basic q=1"
1620 AppletHTTP.headers["accept"][1] = "audio/*, q=0.2"
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01001621 AppletHTTP.headers["accept"][2] = "*/*, q=0.1"
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001622..
1623
1624.. js:attribute:: AppletHTTP.headers
1625
Thierry FOURNIER841475e2015-12-11 17:10:09 +01001626 Contains an array containing all the request headers.
1627
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01001628.. js:function:: AppletHTTP.set_status(applet, code)
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02001629
1630 This function sets the HTTP status code for the response. The allowed code are
1631 from 100 to 599.
1632
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01001633 :param class_AppletHTTP applet: An :ref:`applethttp_class`
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02001634 :param integer code: the status code returned to the client.
1635
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01001636.. js:function:: AppletHTTP.add_header(applet, name, value)
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02001637
1638 This function add an header in the response. Duplicated headers are not
1639 collapsed. The special header *content-length* is used to determinate the
1640 response length. If it not exists, a *transfer-encoding: chunked* is set, and
1641 all the write from the funcion *AppletHTTP:send()* become a chunk.
1642
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01001643 :param class_AppletHTTP applet: An :ref:`applethttp_class`
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02001644 :param string name: the header name
1645 :param string value: the header value
1646
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01001647.. js:function:: AppletHTTP.start_response(applet)
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02001648
1649 This function indicates to the HTTP engine that it can process and send the
1650 response headers. After this called we cannot add headers to the response; We
1651 cannot use the *AppletHTTP:send()* function if the
1652 *AppletHTTP:start_response()* is not called.
1653
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01001654 :param class_AppletHTTP applet: An :ref:`applethttp_class`
1655
1656.. js:function:: AppletHTTP.getline(applet)
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02001657
1658 This function returns a string containing one line from the http body. If the
1659 data returned doesn't contains a final '\\n' its assumed than its the last
1660 available data before the end of stream.
1661
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01001662 :param class_AppletHTTP applet: An :ref:`applethttp_class`
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02001663 :returns: a string. The string can be empty if we reach the end of the stream.
1664
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01001665.. js:function:: AppletHTTP.receive(applet, [size])
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02001666
1667 Reads data from the HTTP body, according to the specified read *size*. If the
1668 *size* is missing, the function tries to read all the content of the stream
1669 until the end. If the *size* is bigger than the http body, it returns the
1670 amount of data avalaible.
1671
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01001672 :param class_AppletHTTP applet: An :ref:`applethttp_class`
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02001673 :param integer size: the required read size.
1674 :returns: always return a string,the string can be empty is the connexion is
1675 closed.
1676
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01001677.. js:function:: AppletHTTP.send(applet, msg)
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02001678
1679 Send the message *msg* on the http request body.
1680
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01001681 :param class_AppletHTTP applet: An :ref:`applethttp_class`
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02001682 :param string msg: the message to send.
1683
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01001684.. js:function:: AppletHTTP.get_priv(applet)
1685
1686 Return Lua data stored in the current transaction (with the
1687 `AppletHTTP.set_priv()`) function. If no data are stored, it returns a nil
1688 value.
1689
1690 :param class_AppletHTTP applet: An :ref:`applethttp_class`
1691 :returns: the opaque data previsously stored, or nil if nothing is
1692 avalaible.
1693
1694.. js:function:: AppletHTTP.set_priv(applet, data)
1695
1696 Store any data in the current HAProxy transaction. This action replace the
1697 old stored data.
1698
1699 :param class_AppletHTTP applet: An :ref:`applethttp_class`
1700 :param opaque data: The data which is stored in the transaction.
1701
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001702.. _applettcp_class:
1703
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02001704AppletTCP class
1705===============
1706
1707.. js:class:: AppletTCP
1708
1709 This class is used with applets that requires the 'tcp' mode. The tcp applet
1710 can be registered with the *core.register_service()* function. They are used
1711 for processing a tcp stream like a server in back of HAProxy.
1712
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001713.. js:attribute:: AppletTCP.c
1714
1715 :returns: A :ref:`converters_class`
1716
1717 This attribute contains a Converters class object.
1718
1719.. js:attribute:: AppletTCP.sc
1720
1721 :returns: A :ref:`converters_class`
1722
1723 This attribute contains a Converters class object. The
1724 functions of this object returns always a string.
1725
1726.. js:attribute:: AppletTCP.f
1727
1728 :returns: A :ref:`fetches_class`
1729
1730 This attribute contains a Fetches class object.
1731
1732.. js:attribute:: AppletTCP.sf
1733
1734 :returns: A :ref:`fetches_class`
1735
1736 This attribute contains a Fetches class object.
1737
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01001738.. js:function:: AppletTCP.getline(applet)
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02001739
1740 This function returns a string containing one line from the stream. If the
1741 data returned doesn't contains a final '\\n' its assumed than its the last
1742 available data before the end of stream.
1743
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01001744 :param class_AppletTCP applet: An :ref:`applettcp_class`
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02001745 :returns: a string. The string can be empty if we reach the end of the stream.
1746
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01001747.. js:function:: AppletTCP.receive(applet, [size])
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02001748
1749 Reads data from the TCP stream, according to the specified read *size*. If the
1750 *size* is missing, the function tries to read all the content of the stream
1751 until the end.
1752
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01001753 :param class_AppletTCP applet: An :ref:`applettcp_class`
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02001754 :param integer size: the required read size.
1755 :returns: always return a string,the string can be empty is the connexion is
1756 closed.
1757
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01001758.. js:function:: AppletTCP.send(appletmsg)
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02001759
1760 Send the message on the stream.
1761
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01001762 :param class_AppletTCP applet: An :ref:`applettcp_class`
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02001763 :param string msg: the message to send.
1764
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01001765.. js:function:: AppletTCP.get_priv(applet)
1766
1767 Return Lua data stored in the current transaction (with the
1768 `AppletTCP.set_priv()`) function. If no data are stored, it returns a nil
1769 value.
1770
1771 :param class_AppletTCP applet: An :ref:`applettcp_class`
1772 :returns: the opaque data previsously stored, or nil if nothing is
1773 avalaible.
1774
1775.. js:function:: AppletTCP.set_priv(applet, data)
1776
1777 Store any data in the current HAProxy transaction. This action replace the
1778 old stored data.
1779
1780 :param class_AppletTCP applet: An :ref:`applettcp_class`
1781 :param opaque data: The data which is stored in the transaction.
1782
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001783External Lua libraries
1784======================
1785
1786A lot of useful lua libraries can be found here:
1787
1788* `https://lua-toolbox.com/ <https://lua-toolbox.com/>`_
1789
1790Redis acces:
1791
1792* `https://github.com/nrk/redis-lua <https://github.com/nrk/redis-lua>`_
1793
1794This is an example about the usage of the Redis library with HAProxy. Note that
1795each call of any function of this library can throw an error if the socket
1796connection fails.
1797
1798.. code-block:: lua
1799
1800 -- load the redis library
1801 local redis = require("redis");
1802
1803 function do_something(txn)
1804
1805 -- create and connect new tcp socket
1806 local tcp = core.tcp();
1807 tcp:settimeout(1);
1808 tcp:connect("127.0.0.1", 6379);
1809
1810 -- use the redis library with this new socket
1811 local client = redis.connect({socket=tcp});
1812 client:ping();
1813
1814 end
1815
1816OpenSSL:
1817
1818* `http://mkottman.github.io/luacrypto/index.html
1819 <http://mkottman.github.io/luacrypto/index.html>`_
1820
1821* `https://github.com/brunoos/luasec/wiki
1822 <https://github.com/brunoos/luasec/wiki>`_
Thierry FOURNIER2e4893c2015-03-18 13:37:27 +01001823