blob: fa16519b62d3e8070fcd08a7c7a8bf36634bd623 [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 FOURNIER8255a752015-09-23 21:03:35 +0200308.. js:function:: core.register_action(name, actions, func)
309
310 **context**: body
311
David Carlier61fdf8b2015-10-02 11:59:38 +0100312 Register a Lua function executed as action. All the registered action can be
Thierry FOURNIER8255a752015-09-23 21:03:35 +0200313 used in HAProxy with the prefix "lua.". An action gets a TXN object class as
314 input.
315
316 :param string name: is the name of the converter.
Willy Tarreau61add3c2015-09-28 15:39:10 +0200317 :param table actions: is a table of string describing the HAProxy actions who
Thierry FOURNIER8255a752015-09-23 21:03:35 +0200318 want to register to. The expected actions are 'tcp-req',
319 'tcp-res', 'http-req' or 'http-res'.
320 :param function func: is the Lua function called to work as converter.
321
322 The prototype of the Lua function used as argument is:
323
324.. code-block:: lua
325
326 function(txn)
327..
328
Thierry FOURNIERdc595002015-12-21 11:13:52 +0100329 * **txn** (:ref:`txn_class`): this is a TXN object used for manipulating the
Thierry FOURNIER8255a752015-09-23 21:03:35 +0200330 current request or TCP stream.
331
Willy Tarreau61add3c2015-09-28 15:39:10 +0200332 Here, an exemple of action registration. the action juste send an 'Hello world'
Thierry FOURNIER8255a752015-09-23 21:03:35 +0200333 in the logs.
334
335.. code-block:: lua
336
337 core.register_action("hello-world", { "tcp-req", "http-req" }, function(txn)
338 txn:Info("Hello world")
339 end)
340..
341
342 This example code is used in HAproxy configuration like this:
343
344::
345
346 frontend tcp_frt
347 mode tcp
348 tcp-request content lua.hello-world
349
350 frontend http_frt
351 mode http
352 http-request lua.hello-world
353
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100354.. js:function:: core.register_converters(name, func)
355
356 **context**: body
357
David Carlier61fdf8b2015-10-02 11:59:38 +0100358 Register a Lua function executed as converter. All the registered converters
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100359 can be used in HAProxy with the prefix "lua.". An converter get a string as
360 input and return a string as output. The registered function can take up to 9
361 values as parameter. All the value are strings.
362
363 :param string name: is the name of the converter.
364 :param function func: is the Lua function called to work as converter.
365
366 The prototype of the Lua function used as argument is:
367
368.. code-block:: lua
369
370 function(str, [p1 [, p2 [, ... [, p5]]]])
371..
372
373 * **str** (*string*): this is the input value automatically converted in
374 string.
375 * **p1** .. **p5** (*string*): this is a list of string arguments declared in
376 the haroxy configuration file. The number of arguments doesn't exceed 5.
377 The order and the nature of these is conventionally choose by the
378 developper.
379
380.. js:function:: core.register_fetches(name, func)
381
382 **context**: body
383
David Carlier61fdf8b2015-10-02 11:59:38 +0100384 Register a Lua function executed as sample fetch. All the registered sample
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100385 fetchs can be used in HAProxy with the prefix "lua.". A Lua sample fetch
386 return a string as output. The registered function can take up to 9 values as
387 parameter. All the value are strings.
388
389 :param string name: is the name of the converter.
390 :param function func: is the Lua function called to work as sample fetch.
391
392 The prototype of the Lua function used as argument is:
393
394.. code-block:: lua
395
396 string function(txn, [p1 [, p2 [, ... [, p5]]]])
397..
398
Thierry FOURNIERdc595002015-12-21 11:13:52 +0100399 * **txn** (:ref:`txn_class`): this is the txn object associated with the current
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100400 request.
401 * **p1** .. **p5** (*string*): this is a list of string arguments declared in
402 the haroxy configuration file. The number of arguments doesn't exceed 5.
403 The order and the nature of these is conventionally choose by the
404 developper.
405 * **Returns**: A string containing some data, ot nil if the value cannot be
406 returned now.
407
408 lua example code:
409
410.. code-block:: lua
411
412 core.register_fetches("hello", function(txn)
413 return "hello"
414 end)
415..
416
417 HAProxy example configuration:
418
419::
420
421 frontend example
422 http-request redirect location /%[lua.hello]
423
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +0200424.. js:function:: core.register_service(name, mode, func)
425
426 **context**: body
427
David Carlier61fdf8b2015-10-02 11:59:38 +0100428 Register a Lua function executed as a service. All the registered service can
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +0200429 be used in HAProxy with the prefix "lua.". A service gets an object class as
430 input according with the required mode.
431
432 :param string name: is the name of the converter.
Willy Tarreau61add3c2015-09-28 15:39:10 +0200433 :param string mode: is string describing the required mode. Only 'tcp' or
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +0200434 'http' are allowed.
435 :param function func: is the Lua function called to work as converter.
436
437 The prototype of the Lua function used as argument is:
438
439.. code-block:: lua
440
441 function(applet)
442..
443
Thierry FOURNIERdc595002015-12-21 11:13:52 +0100444 * **applet** *applet* will be a :ref:`applettcp_class` or a
445 :ref:`applethttp_class`. It depends the type of registered applet. An applet
446 registered with the 'http' value for the *mode* parameter will gets a
447 :ref:`applethttp_class`. If the *mode* value is 'tcp', the applet will gets
448 a :ref:`applettcp_class`.
449
450 **warning**: Applets of type 'http' cannot be called from 'tcp-*'
451 rulesets. Only the 'http-*' rulesets are authorized, this means
452 that is not possible to call an HTTP applet from a proxy in tcp
453 mode. Applets of type 'tcp' can be called from anywhre.
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +0200454
Willy Tarreau61add3c2015-09-28 15:39:10 +0200455 Here, an exemple of service registration. the service just send an 'Hello world'
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +0200456 as an http response.
457
458.. code-block:: lua
459
Pieter Baauw4d7f7662015-11-08 16:38:08 +0100460 core.register_service("hello-world", "http", function(applet)
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +0200461 local response = "Hello World !"
462 applet:set_status(200)
Pieter Baauw2dcb9bc2015-10-01 22:47:12 +0200463 applet:add_header("content-length", string.len(response))
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +0200464 applet:add_header("content-type", "text/plain")
Pieter Baauw2dcb9bc2015-10-01 22:47:12 +0200465 applet:start_response()
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +0200466 applet:send(response)
467 end)
468..
469
470 This example code is used in HAproxy configuration like this:
471
472::
473
474 frontend example
475 http-request use-service lua.hello-world
476
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100477.. js:function:: core.register_init(func)
478
479 **context**: body
480
481 Register a function executed after the configuration parsing. This is useful
482 to check any parameters.
483
Pieter Baauw4d7f7662015-11-08 16:38:08 +0100484 :param function func: is the Lua function called to work as initializer.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100485
486 The prototype of the Lua function used as argument is:
487
488.. code-block:: lua
489
490 function()
491..
492
493 It takes no input, and no output is expected.
494
495.. js:function:: core.register_task(func)
496
497 **context**: body, init, task, action, sample-fetch, converter
498
499 Register and start independent task. The task is started when the HAProxy
500 main scheduler starts. For example this type of tasks can be executed to
Thierry FOURNIER2e4893c2015-03-18 13:37:27 +0100501 perform complex health checks.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100502
Pieter Baauw4d7f7662015-11-08 16:38:08 +0100503 :param function func: is the Lua function called to work as initializer.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100504
505 The prototype of the Lua function used as argument is:
506
507.. code-block:: lua
508
509 function()
510..
511
512 It takes no input, and no output is expected.
513
514.. js:function:: core.set_nice(nice)
515
516 **context**: task, action, sample-fetch, converter
517
518 Change the nice of the current task or current session.
Thierry FOURNIER2e4893c2015-03-18 13:37:27 +0100519
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100520 :param integer nice: the nice value, it must be between -1024 and 1024.
521
522.. js:function:: core.set_map(filename, key, value)
523
524 **context**: init, task, action, sample-fetch, converter
525
526 set the value *value* associated to the key *key* in the map referenced by
527 *filename*.
528
Thierry FOURNIERdc595002015-12-21 11:13:52 +0100529 :param string filename: the Map reference
530 :param string key: the key to set or replace
531 :param string value: the associated value
532
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100533.. js:function:: core.sleep(int seconds)
534
535 **context**: body, init, task, action
536
537 The `core.sleep()` functions stop the Lua execution between specified seconds.
538
539 :param integer seconds: the required seconds.
540
541.. js:function:: core.tcp()
542
543 **context**: init, task, action
544
545 This function returns a new object of a *socket* class.
546
Thierry FOURNIERdc595002015-12-21 11:13:52 +0100547 :returns: A :ref:`socket_class` object.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100548
Thierry Fournier1de16592016-01-27 09:49:07 +0100549.. js:function:: core.concat()
550
551 **context**: body, init, task, action, sample-fetch, converter
552
553 This function retruns a new concat object.
554
555 :returns: A :ref:`concat_class` object.
556
Thierry FOURNIER0a99b892015-08-26 00:14:17 +0200557.. js:function:: core.done(data)
558
559 **context**: body, init, task, action, sample-fetch, converter
560
561 :param any data: Return some data for the caller. It is useful with
562 sample-fetches and sample-converters.
563
564 Immediately stops the current Lua execution and returns to the caller which
565 may be a sample fetch, a converter or an action and returns the specified
566 value (ignored for actions). It is used when the LUA process finishes its
567 work and wants to give back the control to HAProxy without executing the
568 remaining code. It can be seen as a multi-level "return".
569
Thierry FOURNIER486f5a02015-03-16 15:13:03 +0100570.. js:function:: core.yield()
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100571
572 **context**: task, action, sample-fetch, converter
573
574 Give back the hand at the HAProxy scheduler. It is used when the LUA
575 processing consumes a lot of processing time.
576
Thierry Fournier1de16592016-01-27 09:49:07 +0100577.. _concat_class:
578
579Concat class
580============
581
582.. js:class:: Concat
583
584 This class provides a fast way for string concatenation. The way using native
585 Lua concatenation like the code below is slow for some reasons.
586
587.. code-block:: lua
588
589 str = "string1"
590 str = str .. ", string2"
591 str = str .. ", string3"
592..
593
594 For each concatenation, Lua:
595 * allocate memory for the result,
596 * catenate the two string copying the strings in the new memory bloc,
597 * free the old memory block containing the string whoch is no longer used.
598 This process does many memory move, allocation and free. In addition, the
599 memory is not really freed, it is just mark mark as unsused and wait for the
600 garbage collector.
601
602 The Concat class provide an alternative way for catenating strings. It uses
603 the internal Lua mechanism (it does not allocate memory), but it doesn't copy
604 the data more than once.
605
606 On my computer, the following loops spends 0.2s for the Concat method and
607 18.5s for the pure Lua implementation. So, the Concat class is about 1000x
608 faster than the embedded solution.
609
610.. code-block:: lua
611
612 for j = 1, 100 do
613 c = core.concat()
614 for i = 1, 20000 do
615 c:add("#####")
616 end
617 end
618..
619
620.. code-block:: lua
621
622 for j = 1, 100 do
623 c = ""
624 for i = 1, 20000 do
625 c = c .. "#####"
626 end
627 end
628..
629
630.. js:function:: Concat.add(concat, string)
631
632 This function adds a string to the current concatenated string.
633
634 :param class_concat concat: A :ref:`concat_class` which contains the currently
635 builded string.
636 :param string string: A new string to concatenate to the current builded
637 string.
638
639.. js:function:: Concat.dump(concat)
640
641 This function returns the concanated string.
642
643 :param class_concat concat: A :ref:`concat_class` which contains the currently
644 builded string.
645 :returns: the concatenated string
646
Thierry FOURNIERdc595002015-12-21 11:13:52 +0100647.. _fetches_class:
648
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100649Fetches class
650=============
651
652.. js:class:: Fetches
653
654 This class contains a lot of internal HAProxy sample fetches. See the
Thierry FOURNIER2e4893c2015-03-18 13:37:27 +0100655 HAProxy "configuration.txt" documentation for more information about her
656 usage. they are the chapters 7.3.2 to 7.3.6.
657
Thierry FOURNIERdc595002015-12-21 11:13:52 +0100658 **warning** some sample fetches are not available in some context. These
659 limitations are specified in this documentation when theire useful.
660
Thierry FOURNIER2e4893c2015-03-18 13:37:27 +0100661 :see: TXN.f
662 :see: TXN.sf
663
664 Fetches are useful for:
665
666 * get system time,
667 * get environment variable,
668 * get random numbers,
669 * known backend status like the number of users in queue or the number of
670 connections established,
671 * client information like ip source or destination,
672 * deal with stick tables,
673 * Established SSL informations,
674 * HTTP information like headers or method.
675
676.. code-block:: lua
677
Thierry FOURNIERdc595002015-12-21 11:13:52 +0100678 function action(txn)
679 -- Get source IP
680 local clientip = txn.f:src()
681 end
Thierry FOURNIER2e4893c2015-03-18 13:37:27 +0100682..
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100683
Thierry FOURNIERdc595002015-12-21 11:13:52 +0100684.. _converters_class:
685
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100686Converters class
687================
688
689.. js:class:: Converters
690
691 This class contains a lot of internal HAProxy sample converters. See the
Thierry FOURNIER2e4893c2015-03-18 13:37:27 +0100692 HAProxy documentation "configuration.txt" for more information about her
693 usage. Its the chapter 7.3.1.
694
695 :see: TXN.c
696 :see: TXN.sc
697
698 Converters provides statefull transformation. They are useful for:
699
700 * converting input to base64,
701 * applying hash on input string (djb2, crc32, sdbm, wt6),
702 * format date,
703 * json escape,
Pieter Baauw4d7f7662015-11-08 16:38:08 +0100704 * extracting preferred language comparing two lists,
Thierry FOURNIER2e4893c2015-03-18 13:37:27 +0100705 * turn to lower or upper chars,
706 * deal with stick tables.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100707
Thierry FOURNIERdc595002015-12-21 11:13:52 +0100708.. _channel_class:
709
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100710Channel class
711=============
712
713.. js:class:: Channel
714
715 HAProxy uses two buffers for the processing of the requests. The first one is
716 used with the request data (from the client to the server) and the second is
717 used for the response data (from the server to the client).
718
719 Each buffer contains two types of data. The first type is the incoming data
720 waiting for a processing. The second part is the outgoing data already
721 processed. Usually, the incoming data is processed, after it is tagged as
722 outgoing data, and finally it is sent. The following functions provides tools
723 for manipulating these data in a buffer.
724
725 The following diagram shows where the channel class function are applied.
726
727 **Warning**: It is not possible to read from the response in request action,
728 and it is not possible to read for the request channel in response action.
729
Thierry FOURNIER2e4893c2015-03-18 13:37:27 +0100730.. image:: _static/channel.png
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100731
Thierry FOURNIER486f5a02015-03-16 15:13:03 +0100732.. js:function:: Channel.dup(channel)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100733
734 This function returns a string that contain the entire buffer. The data is
735 not remove from the buffer and can be reprocessed later.
736
737 If the buffer cant receive more data, a 'nil' value is returned.
738
Thierry FOURNIER486f5a02015-03-16 15:13:03 +0100739 :param class_channel channel: The manipulated Channel.
Pieter Baauw4d7f7662015-11-08 16:38:08 +0100740 :returns: a string containing all the available data or nil.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100741
Thierry FOURNIER486f5a02015-03-16 15:13:03 +0100742.. js:function:: Channel.get(channel)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100743
744 This function returns a string that contain the entire buffer. The data is
745 consumed from the buffer.
746
747 If the buffer cant receive more data, a 'nil' value is returned.
748
Thierry FOURNIER486f5a02015-03-16 15:13:03 +0100749 :param class_channel channel: The manipulated Channel.
Pieter Baauw4d7f7662015-11-08 16:38:08 +0100750 :returns: a string containing all the available data or nil.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100751
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +0200752.. js:function:: Channel.getline(channel)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100753
754 This function returns a string that contain the first line of the buffer. The
755 data is consumed. If the data returned doesn't contains a final '\n' its
756 assumed than its the last available data in the buffer.
757
758 If the buffer cant receive more data, a 'nil' value is returned.
759
Thierry FOURNIER486f5a02015-03-16 15:13:03 +0100760 :param class_channel channel: The manipulated Channel.
Pieter Baauw386a1272015-08-16 15:26:24 +0200761 :returns: a string containing the available line or nil.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100762
Thierry FOURNIER486f5a02015-03-16 15:13:03 +0100763.. js:function:: Channel.set(channel, string)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100764
765 This function replace the content of the buffer by the string. The function
766 returns the copied length, otherwise, it returns -1.
767
768 The data set with this function are not send. They wait for the end of
769 HAProxy processing, so the buffer can be full.
770
Thierry FOURNIER486f5a02015-03-16 15:13:03 +0100771 :param class_channel channel: The manipulated Channel.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100772 :param string string: The data which will sent.
Pieter Baauw4d7f7662015-11-08 16:38:08 +0100773 :returns: an integer containing the amount of bytes copied or -1.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100774
Thierry FOURNIER486f5a02015-03-16 15:13:03 +0100775.. js:function:: Channel.append(channel, string)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100776
777 This function append the string argument to the content of the buffer. The
778 function returns the copied length, otherwise, it returns -1.
779
780 The data set with this function are not send. They wait for the end of
781 HAProxy processing, so the buffer can be full.
782
Thierry FOURNIER486f5a02015-03-16 15:13:03 +0100783 :param class_channel channel: The manipulated Channel.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100784 :param string string: The data which will sent.
Pieter Baauw4d7f7662015-11-08 16:38:08 +0100785 :returns: an integer containing the amount of bytes copied or -1.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100786
Thierry FOURNIER486f5a02015-03-16 15:13:03 +0100787.. js:function:: Channel.send(channel, string)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100788
789 This function required immediate send of the data. Unless if the connection
790 is close, the buffer is regularly flushed and all the string can be sent.
791
Thierry FOURNIER486f5a02015-03-16 15:13:03 +0100792 :param class_channel channel: The manipulated Channel.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100793 :param string string: The data which will sent.
Pieter Baauw4d7f7662015-11-08 16:38:08 +0100794 :returns: an integer containing the amount of bytes copied or -1.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100795
Thierry FOURNIER486f5a02015-03-16 15:13:03 +0100796.. js:function:: Channel.get_in_length(channel)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100797
798 This function returns the length of the input part of the buffer.
799
Thierry FOURNIER486f5a02015-03-16 15:13:03 +0100800 :param class_channel channel: The manipulated Channel.
Pieter Baauw4d7f7662015-11-08 16:38:08 +0100801 :returns: an integer containing the amount of available bytes.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100802
Thierry FOURNIER486f5a02015-03-16 15:13:03 +0100803.. js:function:: Channel.get_out_length(channel)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100804
805 This function returns the length of the output part of the buffer.
806
Thierry FOURNIER486f5a02015-03-16 15:13:03 +0100807 :param class_channel channel: The manipulated Channel.
Pieter Baauw4d7f7662015-11-08 16:38:08 +0100808 :returns: an integer containing the amount of available bytes.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100809
Thierry FOURNIER486f5a02015-03-16 15:13:03 +0100810.. js:function:: Channel.forward(channel, int)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100811
812 This function transfer bytes from the input part of the buffer to the output
813 part.
814
Thierry FOURNIER486f5a02015-03-16 15:13:03 +0100815 :param class_channel channel: The manipulated Channel.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100816 :param integer int: The amount of data which will be forwarded.
817
Thierry FOURNIERdc595002015-12-21 11:13:52 +0100818
819.. _http_class:
Thierry FOURNIER08504f42015-03-16 14:17:08 +0100820
821HTTP class
822==========
823
824.. js:class:: HTTP
825
826 This class contain all the HTTP manipulation functions.
827
Pieter Baauw386a1272015-08-16 15:26:24 +0200828.. js:function:: HTTP.req_get_headers(http)
Thierry FOURNIER08504f42015-03-16 14:17:08 +0100829
830 Returns an array containing all the request headers.
831
832 :param class_http http: The related http object.
833 :returns: array of headers.
Pieter Baauw386a1272015-08-16 15:26:24 +0200834 :see: HTTP.res_get_headers()
Thierry FOURNIER08504f42015-03-16 14:17:08 +0100835
Thierry FOURNIERdc595002015-12-21 11:13:52 +0100836 This is the form of the returned array:
837
838.. code-block:: lua
839
840 HTTP:req_get_headers()['<header-name>'][<header-index>] = "<header-value>"
841
842 local hdr = HTTP:req_get_headers()
843 hdr["host"][0] = "www.test.com"
844 hdr["accept"][0] = "audio/basic q=1"
845 hdr["accept"][1] = "audio/*, q=0.2"
846 hdr["accept"][2] = "*/*, q=0.1"
847..
848
Pieter Baauw386a1272015-08-16 15:26:24 +0200849.. js:function:: HTTP.res_get_headers(http)
Thierry FOURNIER08504f42015-03-16 14:17:08 +0100850
851 Returns an array containing all the response headers.
852
853 :param class_http http: The related http object.
854 :returns: array of headers.
Pieter Baauw386a1272015-08-16 15:26:24 +0200855 :see: HTTP.req_get_headers()
Thierry FOURNIER08504f42015-03-16 14:17:08 +0100856
Thierry FOURNIERdc595002015-12-21 11:13:52 +0100857 This is the form of the returned array:
858
859.. code-block:: lua
860
861 HTTP:res_get_headers()['<header-name>'][<header-index>] = "<header-value>"
862
863 local hdr = HTTP:req_get_headers()
864 hdr["host"][0] = "www.test.com"
865 hdr["accept"][0] = "audio/basic q=1"
866 hdr["accept"][1] = "audio/*, q=0.2"
867 hdr["accept"][2] = "*.*, q=0.1"
868..
869
Thierry FOURNIER486f5a02015-03-16 15:13:03 +0100870.. js:function:: HTTP.req_add_header(http, name, value)
Thierry FOURNIER08504f42015-03-16 14:17:08 +0100871
872 Appends an HTTP header field in the request whose name is
873 specified in "name" and whose value is defined in "value".
874
875 :param class_http http: The related http object.
876 :param string name: The header name.
877 :param string value: The header value.
Thierry FOURNIER486f5a02015-03-16 15:13:03 +0100878 :see: HTTP.res_add_header()
Thierry FOURNIER08504f42015-03-16 14:17:08 +0100879
Thierry FOURNIER486f5a02015-03-16 15:13:03 +0100880.. js:function:: HTTP.res_add_header(http, name, value)
Thierry FOURNIER08504f42015-03-16 14:17:08 +0100881
882 appends an HTTP header field in the response whose name is
883 specified in "name" and whose value is defined in "value".
884
885 :param class_http http: The related http object.
886 :param string name: The header name.
887 :param string value: The header value.
Thierry FOURNIER486f5a02015-03-16 15:13:03 +0100888 :see: HTTP.req_add_header()
Thierry FOURNIER08504f42015-03-16 14:17:08 +0100889
Thierry FOURNIER486f5a02015-03-16 15:13:03 +0100890.. js:function:: HTTP.req_del_header(http, name)
Thierry FOURNIER08504f42015-03-16 14:17:08 +0100891
892 Removes all HTTP header fields in the request whose name is
893 specified in "name".
894
895 :param class_http http: The related http object.
896 :param string name: The header name.
Thierry FOURNIER486f5a02015-03-16 15:13:03 +0100897 :see: HTTP.res_del_header()
Thierry FOURNIER08504f42015-03-16 14:17:08 +0100898
Thierry FOURNIER486f5a02015-03-16 15:13:03 +0100899.. js:function:: HTTP.res_del_header(http, name)
Thierry FOURNIER08504f42015-03-16 14:17:08 +0100900
901 Removes all HTTP header fields in the response whose name is
902 specified in "name".
903
904 :param class_http http: The related http object.
905 :param string name: The header name.
Thierry FOURNIER486f5a02015-03-16 15:13:03 +0100906 :see: HTTP.req_del_header()
Thierry FOURNIER08504f42015-03-16 14:17:08 +0100907
Thierry FOURNIER486f5a02015-03-16 15:13:03 +0100908.. js:function:: HTTP.req_set_header(http, name, value)
Thierry FOURNIER08504f42015-03-16 14:17:08 +0100909
910 This variable replace all occurence of all header "name", by only
911 one containing the "value".
912
913 :param class_http http: The related http object.
914 :param string name: The header name.
915 :param string value: The header value.
Thierry FOURNIER486f5a02015-03-16 15:13:03 +0100916 :see: HTTP.res_set_header()
Thierry FOURNIER08504f42015-03-16 14:17:08 +0100917
918 This function does the same work as the folowwing code:
919
920.. code-block:: lua
921
922 function fcn(txn)
Thierry FOURNIER486f5a02015-03-16 15:13:03 +0100923 TXN.http:req_del_header("header")
924 TXN.http:req_add_header("header", "value")
Thierry FOURNIER08504f42015-03-16 14:17:08 +0100925 end
926..
927
Thierry FOURNIER486f5a02015-03-16 15:13:03 +0100928.. js:function:: HTTP.res_set_header(http, name, value)
Thierry FOURNIER08504f42015-03-16 14:17:08 +0100929
930 This variable replace all occurence of all header "name", by only
931 one containing the "value".
932
933 :param class_http http: The related http object.
934 :param string name: The header name.
935 :param string value: The header value.
Pieter Baauw386a1272015-08-16 15:26:24 +0200936 :see: HTTP.req_rep_header()
Thierry FOURNIER08504f42015-03-16 14:17:08 +0100937
Pieter Baauw386a1272015-08-16 15:26:24 +0200938.. js:function:: HTTP.req_rep_header(http, name, regex, replace)
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.
Pieter Baauw386a1272015-08-16 15:26:24 +0200949 :see: HTTP.res_rep_header()
Thierry FOURNIER08504f42015-03-16 14:17:08 +0100950
Pieter Baauw386a1272015-08-16 15:26:24 +0200951.. js:function:: HTTP.res_rep_header(http, name, regex, string)
Thierry FOURNIER08504f42015-03-16 14:17:08 +0100952
953 Matches the regular expression in all occurrences of header field "name"
954 according to "regex", and replaces them with the "replace" argument. The
955 replacement value can contain back references like \1, \2, ... This
956 function works with the request.
957
958 :param class_http http: The related http object.
959 :param string name: The header name.
960 :param string regex: The match regular expression.
961 :param string replace: The replacement value.
Thierry FOURNIER486f5a02015-03-16 15:13:03 +0100962 :see: HTTP.req_replace_header()
Thierry FOURNIER08504f42015-03-16 14:17:08 +0100963
Thierry FOURNIER486f5a02015-03-16 15:13:03 +0100964.. js:function:: HTTP.req_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.req_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.req_replace_header()
975 :see: HTTP.res_replace_value()
Thierry FOURNIER08504f42015-03-16 14:17:08 +0100976
Thierry FOURNIER486f5a02015-03-16 15:13:03 +0100977.. js:function:: HTTP.res_replace_value(http, name, regex, replace)
Thierry FOURNIER08504f42015-03-16 14:17:08 +0100978
Thierry FOURNIER486f5a02015-03-16 15:13:03 +0100979 Works like "HTTP.res_replace_header()" except that it matches the regex
Thierry FOURNIER08504f42015-03-16 14:17:08 +0100980 against every comma-delimited value of the header field "name" instead of the
981 entire header.
982
983 :param class_http http: The related http object.
984 :param string name: The header name.
985 :param string regex: The match regular expression.
986 :param string replace: The replacement value.
Thierry FOURNIER486f5a02015-03-16 15:13:03 +0100987 :see: HTTP.res_replace_header()
988 :see: HTTP.req_replace_value()
Thierry FOURNIER08504f42015-03-16 14:17:08 +0100989
Thierry FOURNIER486f5a02015-03-16 15:13:03 +0100990.. js:function:: HTTP.req_set_method(http, method)
Thierry FOURNIER08504f42015-03-16 14:17:08 +0100991
992 Rewrites the request method with the parameter "method".
993
994 :param class_http http: The related http object.
995 :param string method: The new method.
996
Thierry FOURNIER486f5a02015-03-16 15:13:03 +0100997.. js:function:: HTTP.req_set_path(http, path)
Thierry FOURNIER08504f42015-03-16 14:17:08 +0100998
999 Rewrites the request path with the "path" parameter.
1000
1001 :param class_http http: The related http object.
1002 :param string path: The new path.
1003
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001004.. js:function:: HTTP.req_set_query(http, query)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001005
1006 Rewrites the request's query string which appears after the first question
1007 mark ("?") with the parameter "query".
1008
1009 :param class_http http: The related http object.
1010 :param string query: The new query.
1011
Thierry FOURNIER0d79cf62015-08-26 14:20:58 +02001012.. js:function:: HTTP.req_set_uri(http, uri)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001013
1014 Rewrites the request URI with the parameter "uri".
1015
1016 :param class_http http: The related http object.
1017 :param string uri: The new uri.
1018
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +02001019.. js:function:: HTTP.res_set_status(http, status)
1020
1021 Rewrites the response status code with the parameter "code". Note that the
1022 reason is automatically adapted to the new code.
1023
1024 :param class_http http: The related http object.
1025 :param integer status: The new response status code.
1026
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001027.. _txn_class:
1028
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001029TXN class
1030=========
1031
1032.. js:class:: TXN
1033
1034 The txn class contain all the functions relative to the http or tcp
1035 transaction (Note than a tcp stream is the same than a tcp transaction, but
1036 an HTTP transaction is not the same than a tcp stream).
1037
1038 The usage of this class permits to retrieve data from the requests, alter it
1039 and forward it.
1040
1041 All the functions provided by this class are available in the context
1042 **sample-fetches** and **actions**.
1043
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001044.. js:attribute:: TXN.c
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001045
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001046 :returns: An :ref:`converters_class`.
1047
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001048 This attribute contains a Converters class object.
1049
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001050.. js:attribute:: TXN.sc
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001051
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001052 :returns: An :ref:`converters_class`.
1053
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001054 This attribute contains a Converters class object. The functions of
1055 this object returns always a string.
1056
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001057.. js:attribute:: TXN.f
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001058
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001059 :returns: An :ref:`fetches_class`.
1060
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001061 This attribute contains a Fetches class object.
1062
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001063.. js:attribute:: TXN.sf
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001064
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001065 :returns: An :ref:`fetches_class`.
1066
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001067 This attribute contains a Fetches class object. The functions of
1068 this object returns always a string.
1069
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001070.. js:attribute:: TXN.req
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001071
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001072 :returns: An :ref:`channel_class`.
1073
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001074 This attribute contains a channel class object for the request buffer.
1075
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001076.. js:attribute:: TXN.res
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001077
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001078 :returns: An :ref:`channel_class`.
1079
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001080 This attribute contains a channel class object for the response buffer.
1081
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001082.. js:attribute:: TXN.http
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001083
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001084 :returns: An :ref:`http_class`.
1085
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001086 This attribute contains an HTTP class object. It is avalaible only if the
1087 proxy has the "mode http" enabled.
1088
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01001089.. js:function:: TXN.log(TXN, loglevel, msg)
1090
1091 This function sends a log. The log is sent, according with the HAProxy
1092 configuration file, on the default syslog server if it is configured and on
1093 the stderr if it is allowed.
1094
1095 :param class_txn txn: The class txn object containing the data.
1096 :param integer loglevel: Is the log level asociated with the message. It is a
1097 number between 0 and 7.
1098 :param string msg: The log content.
1099 :see: core.emerg, core.alert, core.crit, core.err, core.warning, core.notice,
1100 core.info, core.debug (log level definitions)
1101 :see: TXN.deflog
1102 :see: TXN.Debug
1103 :see: TXN.Info
1104 :see: TXN.Warning
1105 :see: TXN.Alert
1106
1107.. js:function:: TXN.deflog(TXN, msg)
1108
1109 Sends a log line with the default loglevel for the proxy ssociated with the
1110 transaction.
1111
1112 :param class_txn txn: The class txn object containing the data.
1113 :param string msg: The log content.
1114 :see: TXN.log
1115
1116.. js:function:: TXN.Debug(txn, msg)
1117
1118 :param class_txn txn: The class txn object containing the data.
1119 :param string msg: The log content.
1120 :see: TXN.log
1121
1122 Does the same job than:
1123
1124.. code-block:: lua
1125
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001126 function Debug(txn, msg)
1127 TXN.log(txn, core.debug, msg)
1128 end
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01001129..
1130
1131.. js:function:: TXN.Info(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.info, msg)
1141 end
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01001142..
1143
1144.. js:function:: TXN.Warning(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.warning, msg)
1154 end
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01001155..
1156
1157.. js:function:: TXN.Alert(txn, msg)
1158
1159 :param class_txn txn: The class txn object containing the data.
1160 :param string msg: The log content.
1161 :see: TXN.log
1162
1163.. code-block:: lua
1164
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001165 function Debug(txn, msg)
1166 TXN.log(txn, core.alert, msg)
1167 end
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01001168..
1169
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001170.. js:function:: TXN.get_priv(txn)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001171
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001172 Return Lua data stored in the current transaction (with the `TXN.set_priv()`)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001173 function. If no data are stored, it returns a nil value.
1174
1175 :param class_txn txn: The class txn object containing the data.
1176 :returns: the opaque data previsously stored, or nil if nothing is
1177 avalaible.
1178
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001179.. js:function:: TXN.set_priv(txn, data)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001180
1181 Store any data in the current HAProxy transaction. This action replace the
1182 old stored data.
1183
1184 :param class_txn txn: The class txn object containing the data.
1185 :param opaque data: The data which is stored in the transaction.
1186
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +02001187.. js:function:: TXN.set_var(TXN, var, value)
1188
David Carlier61fdf8b2015-10-02 11:59:38 +01001189 Converts a Lua type in a HAProxy type and store it in a variable <var>.
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +02001190
1191 :param class_txn txn: The class txn object containing the data.
1192 :param string var: The variable name according with the HAProxy variable syntax.
1193 :param opaque value: The data which is stored in the variable.
1194
1195.. js:function:: TXN.get_var(TXN, var)
1196
1197 Returns data stored in the variable <var> converter in Lua type.
1198
1199 :param class_txn txn: The class txn object containing the data.
1200 :param string var: The variable name according with the HAProxy variable syntax.
1201
Willy Tarreaubc183a62015-08-28 10:39:11 +02001202.. js:function:: TXN.done(txn)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001203
Willy Tarreaubc183a62015-08-28 10:39:11 +02001204 This function terminates processing of the transaction and the associated
1205 session. It can be used when a critical error is detected or to terminate
1206 processing after some data have been returned to the client (eg: a redirect).
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001207
1208 :param class_txn txn: The class txn object containing the data.
1209
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001210.. js:function:: TXN.set_loglevel(txn, loglevel)
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01001211
1212 Is used to change the log level of the current request. The "loglevel" must
1213 be an integer between 0 and 7.
1214
1215 :param class_txn txn: The class txn object containing the data.
1216 :param integer loglevel: The required log level. This variable can be one of
1217 :see: core.<loglevel>
1218
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001219.. js:function:: TXN.set_tos(txn, tos)
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01001220
1221 Is used to set the TOS or DSCP field value of packets sent to the client to
1222 the value passed in "tos" on platforms which support this.
1223
1224 :param class_txn txn: The class txn object containing the data.
1225 :param integer tos: The new TOS os DSCP.
1226
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001227.. js:function:: TXN.set_mark(txn, mark)
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01001228
1229 Is used to set the Netfilter MARK on all packets sent to the client to the
1230 value passed in "mark" on platforms which support it.
1231
1232 :param class_txn txn: The class txn object containing the data.
1233 :param integer mark: The mark value.
1234
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001235.. _socket_class:
1236
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001237Socket class
1238============
1239
1240.. js:class:: Socket
1241
1242 This class must be compatible with the Lua Socket class. Only the 'client'
1243 functions are available. See the Lua Socket documentation:
1244
1245 `http://w3.impa.br/~diego/software/luasocket/tcp.html
1246 <http://w3.impa.br/~diego/software/luasocket/tcp.html>`_
1247
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001248.. js:function:: Socket.close(socket)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001249
1250 Closes a TCP object. The internal socket used by the object is closed and the
1251 local address to which the object was bound is made available to other
1252 applications. No further operations (except for further calls to the close
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001253 method) are allowed on a closed Socket.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001254
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001255 :param class_socket socket: Is the manipulated Socket.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001256
1257 Note: It is important to close all used sockets once they are not needed,
1258 since, in many systems, each socket uses a file descriptor, which are limited
1259 system resources. Garbage-collected objects are automatically closed before
1260 destruction, though.
1261
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02001262.. js:function:: Socket.connect(socket, address[, port])
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001263
1264 Attempts to connect a socket object to a remote host.
1265
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001266
1267 In case of error, the method returns nil followed by a string describing the
1268 error. In case of success, the method returns 1.
1269
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001270 :param class_socket socket: Is the manipulated Socket.
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02001271 :param string address: can be an IP address or a host name. See below for more
1272 information.
1273 :param integer port: must be an integer number in the range [1..64K].
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001274 :returns: 1 or nil.
1275
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02001276 an address field extension permits to use the connect() function to connect to
1277 other stream than TCP. The syntax containing a simpleipv4 or ipv6 address is
1278 the basically expected format. This format requires the port.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001279
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02001280 Other format accepted are a socket path like "/socket/path", it permits to
1281 connect to a socket. abstract namespaces are supported with the prefix
1282 "abns@", and finaly a filedescriotr can be passed with the prefix "fd@".
1283 The prefix "ipv4@", "ipv6@" and "unix@" are also supported. The port can be
1284 passed int the string. The syntax "127.0.0.1:1234" is valid. in this case, the
1285 parameter *port* is ignored.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001286
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001287.. js:function:: Socket.connect_ssl(socket, address, port)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001288
1289 Same behavior than the function socket:connect, but uses SSL.
1290
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001291 :param class_socket socket: Is the manipulated Socket.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001292 :returns: 1 or nil.
1293
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001294.. js:function:: Socket.getpeername(socket)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001295
1296 Returns information about the remote side of a connected client object.
1297
1298 Returns a string with the IP address of the peer, followed by the port number
1299 that peer is using for the connection. In case of error, the method returns
1300 nil.
1301
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001302 :param class_socket socket: Is the manipulated Socket.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001303 :returns: a string containing the server information.
1304
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001305.. js:function:: Socket.getsockname(socket)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001306
1307 Returns the local address information associated to the object.
1308
1309 The method returns a string with local IP address and a number with the port.
1310 In case of error, the method returns nil.
1311
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001312 :param class_socket socket: Is the manipulated Socket.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001313 :returns: a string containing the client information.
1314
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001315.. js:function:: Socket.receive(socket, [pattern [, prefix]])
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001316
1317 Reads data from a client object, according to the specified read pattern.
1318 Patterns follow the Lua file I/O format, and the difference in performance
1319 between all patterns is negligible.
1320
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001321 :param class_socket socket: Is the manipulated Socket.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001322 :param string|integer pattern: Describe what is required (see below).
1323 :param string prefix: A string which will be prefix the returned data.
1324 :returns: a string containing the required data or nil.
1325
1326 Pattern can be any of the following:
1327
1328 * **`*a`**: reads from the socket until the connection is closed. No
1329 end-of-line translation is performed;
1330
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001331 * **`*l`**: reads a line of text from the Socket. The line is terminated by a
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001332 LF character (ASCII 10), optionally preceded by a CR character
1333 (ASCII 13). The CR and LF characters are not included in the
1334 returned line. In fact, all CR characters are ignored by the
1335 pattern. This is the default pattern.
1336
1337 * **number**: causes the method to read a specified number of bytes from the
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001338 Socket. Prefix is an optional string to be concatenated to the
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001339 beginning of any received data before return.
1340
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02001341 * **empty**: If the pattern is left empty, the default option is `*l`.
1342
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001343 If successful, the method returns the received pattern. In case of error, the
1344 method returns nil followed by an error message which can be the string
1345 'closed' in case the connection was closed before the transmission was
1346 completed or the string 'timeout' in case there was a timeout during the
1347 operation. Also, after the error message, the function returns the partial
1348 result of the transmission.
1349
1350 Important note: This function was changed severely. It used to support
1351 multiple patterns (but I have never seen this feature used) and now it
1352 doesn't anymore. Partial results used to be returned in the same way as
1353 successful results. This last feature violated the idea that all functions
1354 should return nil on error. Thus it was changed too.
1355
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001356.. js:function:: Socket.send(socket, data [, start [, end ]])
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001357
1358 Sends data through client object.
1359
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001360 :param class_socket socket: Is the manipulated Socket.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001361 :param string data: The data that will be sent.
1362 :param integer start: The start position in the buffer of the data which will
1363 be sent.
1364 :param integer end: The end position in the buffer of the data which will
1365 be sent.
1366 :returns: see below.
1367
1368 Data is the string to be sent. The optional arguments i and j work exactly
1369 like the standard string.sub Lua function to allow the selection of a
1370 substring to be sent.
1371
1372 If successful, the method returns the index of the last byte within [start,
1373 end] that has been sent. Notice that, if start is 1 or absent, this is
1374 effectively the total number of bytes sent. In case of error, the method
1375 returns nil, followed by an error message, followed by the index of the last
1376 byte within [start, end] that has been sent. You might want to try again from
1377 the byte following that. The error message can be 'closed' in case the
1378 connection was closed before the transmission was completed or the string
1379 'timeout' in case there was a timeout during the operation.
1380
1381 Note: Output is not buffered. For small strings, it is always better to
1382 concatenate them in Lua (with the '..' operator) and send the result in one
1383 call instead of calling the method several times.
1384
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001385.. js:function:: Socket.setoption(socket, option [, value])
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001386
1387 Just implemented for compatibility, this cal does nothing.
1388
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001389.. js:function:: Socket.settimeout(socket, value [, mode])
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001390
1391 Changes the timeout values for the object. All I/O operations are blocking.
1392 That is, any call to the methods send, receive, and accept will block
1393 indefinitely, until the operation completes. The settimeout method defines a
1394 limit on the amount of time the I/O methods can block. When a timeout time
1395 has elapsed, the affected methods give up and fail with an error code.
1396
1397 The amount of time to wait is specified as the value parameter, in seconds.
1398
1399 The timeout modes are bot implemented, the only settable timeout is the
1400 inactivity time waiting for complete the internal buffer send or waiting for
1401 receive data.
1402
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001403 :param class_socket socket: Is the manipulated Socket.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001404 :param integer value: The timeout value.
1405
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001406.. _map_class:
1407
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001408Map class
1409=========
1410
1411.. js:class:: Map
1412
1413 This class permits to do some lookup in HAProxy maps. The declared maps can
1414 be modified during the runtime throught the HAProxy management socket.
1415
1416.. code-block:: lua
1417
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001418 default = "usa"
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001419
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001420 -- Create and load map
1421 geo = Map.new("geo.map", Map.ip);
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001422
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001423 -- Create new fetch that returns the user country
1424 core.register_fetches("country", function(txn)
1425 local src;
1426 local loc;
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001427
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001428 src = txn.f:fhdr("x-forwarded-for");
1429 if (src == nil) then
1430 src = txn.f:src()
1431 if (src == nil) then
1432 return default;
1433 end
1434 end
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001435
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001436 -- Perform lookup
1437 loc = geo:lookup(src);
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001438
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001439 if (loc == nil) then
1440 return default;
1441 end
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001442
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001443 return loc;
1444 end);
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001445
1446.. js:attribute:: Map.int
1447
1448 See the HAProxy configuration.txt file, chapter "Using ACLs and fetching
1449 samples" ans subchapter "ACL basics" to understand this pattern matching
1450 method.
1451
1452.. js:attribute:: Map.ip
1453
1454 See the HAProxy configuration.txt file, chapter "Using ACLs and fetching
1455 samples" ans subchapter "ACL basics" to understand this pattern matching
1456 method.
1457
1458.. js:attribute:: Map.str
1459
1460 See the HAProxy configuration.txt file, chapter "Using ACLs and fetching
1461 samples" ans subchapter "ACL basics" to understand this pattern matching
1462 method.
1463
1464.. js:attribute:: Map.beg
1465
1466 See the HAProxy configuration.txt file, chapter "Using ACLs and fetching
1467 samples" ans subchapter "ACL basics" to understand this pattern matching
1468 method.
1469
1470.. js:attribute:: Map.sub
1471
1472 See the HAProxy configuration.txt file, chapter "Using ACLs and fetching
1473 samples" ans subchapter "ACL basics" to understand this pattern matching
1474 method.
1475
1476.. js:attribute:: Map.dir
1477
1478 See the HAProxy configuration.txt file, chapter "Using ACLs and fetching
1479 samples" ans subchapter "ACL basics" to understand this pattern matching
1480 method.
1481
1482.. js:attribute:: Map.dom
1483
1484 See the HAProxy configuration.txt file, chapter "Using ACLs and fetching
1485 samples" ans subchapter "ACL basics" to understand this pattern matching
1486 method.
1487
1488.. js:attribute:: Map.end
1489
1490 See the HAProxy configuration.txt file, chapter "Using ACLs and fetching
1491 samples" ans subchapter "ACL basics" to understand this pattern matching
1492 method.
1493
1494.. js:attribute:: Map.reg
1495
1496 See the HAProxy configuration.txt file, chapter "Using ACLs and fetching
1497 samples" ans subchapter "ACL basics" to understand this pattern matching
1498 method.
1499
1500
1501.. js:function:: Map.new(file, method)
1502
1503 Creates and load a map.
1504
1505 :param string file: Is the file containing the map.
1506 :param integer method: Is the map pattern matching method. See the attributes
1507 of the Map class.
1508 :returns: a class Map object.
1509 :see: The Map attributes.
1510
1511.. js:function:: Map.lookup(map, str)
1512
1513 Perform a lookup in a map.
1514
1515 :param class_map map: Is the class Map object.
1516 :param string str: Is the string used as key.
1517 :returns: a string containing the result or nil if no match.
1518
1519.. js:function:: Map.slookup(map, str)
1520
1521 Perform a lookup in a map.
1522
1523 :param class_map map: Is the class Map object.
1524 :param string str: Is the string used as key.
1525 :returns: a string containing the result or empty string if no match.
1526
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001527.. _applethttp_class:
1528
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02001529AppletHTTP class
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001530================
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02001531
1532.. js:class:: AppletHTTP
1533
1534 This class is used with applets that requires the 'http' mode. The http applet
1535 can be registered with the *core.register_service()* function. They are used
1536 for processing an http request like a server in back of HAProxy.
1537
1538 This is an hello world sample code:
1539
1540.. code-block:: lua
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001541
Pieter Baauw4d7f7662015-11-08 16:38:08 +01001542 core.register_service("hello-world", "http", function(applet)
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02001543 local response = "Hello World !"
1544 applet:set_status(200)
Pieter Baauw2dcb9bc2015-10-01 22:47:12 +02001545 applet:add_header("content-length", string.len(response))
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02001546 applet:add_header("content-type", "text/plain")
Pieter Baauw2dcb9bc2015-10-01 22:47:12 +02001547 applet:start_response()
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02001548 applet:send(response)
1549 end)
1550
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001551.. js:attribute:: AppletHTTP.c
1552
1553 :returns: A :ref:`converters_class`
1554
1555 This attribute contains a Converters class object.
1556
1557.. js:attribute:: AppletHTTP.sc
1558
1559 :returns: A :ref:`converters_class`
1560
1561 This attribute contains a Converters class object. The
1562 functions of this object returns always a string.
1563
1564.. js:attribute:: AppletHTTP.f
1565
1566 :returns: A :ref:`fetches_class`
1567
1568 This attribute contains a Fetches class object. Note that the
1569 applet execution place cannot access to a valid HAProxy core HTTP
1570 transaction, so some sample fecthes related to the HTTP dependant
1571 values (hdr, path, ...) are not available.
1572
1573.. js:attribute:: AppletHTTP.sf
1574
1575 :returns: A :ref:`fetches_class`
1576
1577 This attribute contains a Fetches class object. The functions of
1578 this object returns always a string. Note that the applet
1579 execution place cannot access to a valid HAProxy core HTTP
1580 transaction, so some sample fecthes related to the HTTP dependant
1581 values (hdr, path, ...) are not available.
1582
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01001583.. js:attribute:: AppletHTTP.method
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001584
1585 :returns: string
1586
1587 The attribute method returns a string containing the HTTP
1588 method.
1589
1590.. js:attribute:: AppletHTTP.version
1591
1592 :returns: string
1593
1594 The attribute version, returns a string containing the HTTP
1595 request version.
1596
1597.. js:attribute:: AppletHTTP.path
1598
1599 :returns: string
1600
1601 The attribute path returns a string containing the HTTP
1602 request path.
1603
1604.. js:attribute:: AppletHTTP.qs
1605
1606 :returns: string
1607
1608 The attribute qs returns a string containing the HTTP
1609 request query string.
1610
1611.. js:attribute:: AppletHTTP.length
1612
1613 :returns: integer
1614
1615 The attribute length returns an integer containing the HTTP
1616 body length.
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02001617
Thierry FOURNIER841475e2015-12-11 17:10:09 +01001618.. js:attribute:: AppletHTTP.headers
1619
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001620 :returns: array
1621
1622 The attribute headers returns an array containing the HTTP
1623 headers. The header names are always in lower case. As the header name can be
1624 encountered more than once in each request, the value is indexed with 0 as
1625 first index value. The array have this form:
1626
1627.. code-block:: lua
1628
1629 AppletHTTP.headers['<header-name>'][<header-index>] = "<header-value>"
1630
1631 AppletHTTP.headers["host"][0] = "www.test.com"
1632 AppletHTTP.headers["accept"][0] = "audio/basic q=1"
1633 AppletHTTP.headers["accept"][1] = "audio/*, q=0.2"
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01001634 AppletHTTP.headers["accept"][2] = "*/*, q=0.1"
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001635..
1636
1637.. js:attribute:: AppletHTTP.headers
1638
Thierry FOURNIER841475e2015-12-11 17:10:09 +01001639 Contains an array containing all the request headers.
1640
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01001641.. js:function:: AppletHTTP.set_status(applet, code)
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02001642
1643 This function sets the HTTP status code for the response. The allowed code are
1644 from 100 to 599.
1645
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01001646 :param class_AppletHTTP applet: An :ref:`applethttp_class`
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02001647 :param integer code: the status code returned to the client.
1648
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01001649.. js:function:: AppletHTTP.add_header(applet, name, value)
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02001650
1651 This function add an header in the response. Duplicated headers are not
1652 collapsed. The special header *content-length* is used to determinate the
1653 response length. If it not exists, a *transfer-encoding: chunked* is set, and
1654 all the write from the funcion *AppletHTTP:send()* become a chunk.
1655
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01001656 :param class_AppletHTTP applet: An :ref:`applethttp_class`
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02001657 :param string name: the header name
1658 :param string value: the header value
1659
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01001660.. js:function:: AppletHTTP.start_response(applet)
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02001661
1662 This function indicates to the HTTP engine that it can process and send the
1663 response headers. After this called we cannot add headers to the response; We
1664 cannot use the *AppletHTTP:send()* function if the
1665 *AppletHTTP:start_response()* is not called.
1666
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01001667 :param class_AppletHTTP applet: An :ref:`applethttp_class`
1668
1669.. js:function:: AppletHTTP.getline(applet)
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02001670
1671 This function returns a string containing one line from the http body. If the
1672 data returned doesn't contains a final '\\n' its assumed than its the last
1673 available data before the end of stream.
1674
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01001675 :param class_AppletHTTP applet: An :ref:`applethttp_class`
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02001676 :returns: a string. The string can be empty if we reach the end of the stream.
1677
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01001678.. js:function:: AppletHTTP.receive(applet, [size])
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02001679
1680 Reads data from the HTTP body, according to the specified read *size*. If the
1681 *size* is missing, the function tries to read all the content of the stream
1682 until the end. If the *size* is bigger than the http body, it returns the
1683 amount of data avalaible.
1684
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01001685 :param class_AppletHTTP applet: An :ref:`applethttp_class`
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02001686 :param integer size: the required read size.
1687 :returns: always return a string,the string can be empty is the connexion is
1688 closed.
1689
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01001690.. js:function:: AppletHTTP.send(applet, msg)
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02001691
1692 Send the message *msg* on the http request body.
1693
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01001694 :param class_AppletHTTP applet: An :ref:`applethttp_class`
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02001695 :param string msg: the message to send.
1696
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01001697.. js:function:: AppletHTTP.get_priv(applet)
1698
1699 Return Lua data stored in the current transaction (with the
1700 `AppletHTTP.set_priv()`) function. If no data are stored, it returns a nil
1701 value.
1702
1703 :param class_AppletHTTP applet: An :ref:`applethttp_class`
1704 :returns: the opaque data previsously stored, or nil if nothing is
1705 avalaible.
1706
1707.. js:function:: AppletHTTP.set_priv(applet, data)
1708
1709 Store any data in the current HAProxy transaction. This action replace the
1710 old stored data.
1711
1712 :param class_AppletHTTP applet: An :ref:`applethttp_class`
1713 :param opaque data: The data which is stored in the transaction.
1714
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001715.. _applettcp_class:
1716
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02001717AppletTCP class
1718===============
1719
1720.. js:class:: AppletTCP
1721
1722 This class is used with applets that requires the 'tcp' mode. The tcp applet
1723 can be registered with the *core.register_service()* function. They are used
1724 for processing a tcp stream like a server in back of HAProxy.
1725
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001726.. js:attribute:: AppletTCP.c
1727
1728 :returns: A :ref:`converters_class`
1729
1730 This attribute contains a Converters class object.
1731
1732.. js:attribute:: AppletTCP.sc
1733
1734 :returns: A :ref:`converters_class`
1735
1736 This attribute contains a Converters class object. The
1737 functions of this object returns always a string.
1738
1739.. js:attribute:: AppletTCP.f
1740
1741 :returns: A :ref:`fetches_class`
1742
1743 This attribute contains a Fetches class object.
1744
1745.. js:attribute:: AppletTCP.sf
1746
1747 :returns: A :ref:`fetches_class`
1748
1749 This attribute contains a Fetches class object.
1750
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01001751.. js:function:: AppletTCP.getline(applet)
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02001752
1753 This function returns a string containing one line from the stream. If the
1754 data returned doesn't contains a final '\\n' its assumed than its the last
1755 available data before the end of stream.
1756
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01001757 :param class_AppletTCP applet: An :ref:`applettcp_class`
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02001758 :returns: a string. The string can be empty if we reach the end of the stream.
1759
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01001760.. js:function:: AppletTCP.receive(applet, [size])
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02001761
1762 Reads data from the TCP stream, according to the specified read *size*. If the
1763 *size* is missing, the function tries to read all the content of the stream
1764 until the end.
1765
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01001766 :param class_AppletTCP applet: An :ref:`applettcp_class`
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02001767 :param integer size: the required read size.
1768 :returns: always return a string,the string can be empty is the connexion is
1769 closed.
1770
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01001771.. js:function:: AppletTCP.send(appletmsg)
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02001772
1773 Send the message on the stream.
1774
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01001775 :param class_AppletTCP applet: An :ref:`applettcp_class`
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02001776 :param string msg: the message to send.
1777
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01001778.. js:function:: AppletTCP.get_priv(applet)
1779
1780 Return Lua data stored in the current transaction (with the
1781 `AppletTCP.set_priv()`) function. If no data are stored, it returns a nil
1782 value.
1783
1784 :param class_AppletTCP applet: An :ref:`applettcp_class`
1785 :returns: the opaque data previsously stored, or nil if nothing is
1786 avalaible.
1787
1788.. js:function:: AppletTCP.set_priv(applet, data)
1789
1790 Store any data in the current HAProxy transaction. This action replace the
1791 old stored data.
1792
1793 :param class_AppletTCP applet: An :ref:`applettcp_class`
1794 :param opaque data: The data which is stored in the transaction.
1795
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001796External Lua libraries
1797======================
1798
1799A lot of useful lua libraries can be found here:
1800
1801* `https://lua-toolbox.com/ <https://lua-toolbox.com/>`_
1802
1803Redis acces:
1804
1805* `https://github.com/nrk/redis-lua <https://github.com/nrk/redis-lua>`_
1806
1807This is an example about the usage of the Redis library with HAProxy. Note that
1808each call of any function of this library can throw an error if the socket
1809connection fails.
1810
1811.. code-block:: lua
1812
1813 -- load the redis library
1814 local redis = require("redis");
1815
1816 function do_something(txn)
1817
1818 -- create and connect new tcp socket
1819 local tcp = core.tcp();
1820 tcp:settimeout(1);
1821 tcp:connect("127.0.0.1", 6379);
1822
1823 -- use the redis library with this new socket
1824 local client = redis.connect({socket=tcp});
1825 client:ping();
1826
1827 end
1828
1829OpenSSL:
1830
1831* `http://mkottman.github.io/luacrypto/index.html
1832 <http://mkottman.github.io/luacrypto/index.html>`_
1833
1834* `https://github.com/brunoos/luasec/wiki
1835 <https://github.com/brunoos/luasec/wiki>`_
Thierry FOURNIER2e4893c2015-03-18 13:37:27 +01001836