blob: d41451f53d26f7887d2140f700a75f3decf8d48c [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 FOURNIER0a99b892015-08-26 00:14:17 +0200536.. js:function:: core.done(data)
537
538 **context**: body, init, task, action, sample-fetch, converter
539
540 :param any data: Return some data for the caller. It is useful with
541 sample-fetches and sample-converters.
542
543 Immediately stops the current Lua execution and returns to the caller which
544 may be a sample fetch, a converter or an action and returns the specified
545 value (ignored for actions). It is used when the LUA process finishes its
546 work and wants to give back the control to HAProxy without executing the
547 remaining code. It can be seen as a multi-level "return".
548
Thierry FOURNIER486f5a02015-03-16 15:13:03 +0100549.. js:function:: core.yield()
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100550
551 **context**: task, action, sample-fetch, converter
552
553 Give back the hand at the HAProxy scheduler. It is used when the LUA
554 processing consumes a lot of processing time.
555
Thierry FOURNIERdc595002015-12-21 11:13:52 +0100556.. _fetches_class:
557
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100558Fetches class
559=============
560
561.. js:class:: Fetches
562
563 This class contains a lot of internal HAProxy sample fetches. See the
Thierry FOURNIER2e4893c2015-03-18 13:37:27 +0100564 HAProxy "configuration.txt" documentation for more information about her
565 usage. they are the chapters 7.3.2 to 7.3.6.
566
Thierry FOURNIERdc595002015-12-21 11:13:52 +0100567 **warning** some sample fetches are not available in some context. These
568 limitations are specified in this documentation when theire useful.
569
Thierry FOURNIER2e4893c2015-03-18 13:37:27 +0100570 :see: TXN.f
571 :see: TXN.sf
572
573 Fetches are useful for:
574
575 * get system time,
576 * get environment variable,
577 * get random numbers,
578 * known backend status like the number of users in queue or the number of
579 connections established,
580 * client information like ip source or destination,
581 * deal with stick tables,
582 * Established SSL informations,
583 * HTTP information like headers or method.
584
585.. code-block:: lua
586
Thierry FOURNIERdc595002015-12-21 11:13:52 +0100587 function action(txn)
588 -- Get source IP
589 local clientip = txn.f:src()
590 end
Thierry FOURNIER2e4893c2015-03-18 13:37:27 +0100591..
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100592
Thierry FOURNIERdc595002015-12-21 11:13:52 +0100593.. _converters_class:
594
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100595Converters class
596================
597
598.. js:class:: Converters
599
600 This class contains a lot of internal HAProxy sample converters. See the
Thierry FOURNIER2e4893c2015-03-18 13:37:27 +0100601 HAProxy documentation "configuration.txt" for more information about her
602 usage. Its the chapter 7.3.1.
603
604 :see: TXN.c
605 :see: TXN.sc
606
607 Converters provides statefull transformation. They are useful for:
608
609 * converting input to base64,
610 * applying hash on input string (djb2, crc32, sdbm, wt6),
611 * format date,
612 * json escape,
Pieter Baauw4d7f7662015-11-08 16:38:08 +0100613 * extracting preferred language comparing two lists,
Thierry FOURNIER2e4893c2015-03-18 13:37:27 +0100614 * turn to lower or upper chars,
615 * deal with stick tables.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100616
Thierry FOURNIERdc595002015-12-21 11:13:52 +0100617.. _channel_class:
618
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100619Channel class
620=============
621
622.. js:class:: Channel
623
624 HAProxy uses two buffers for the processing of the requests. The first one is
625 used with the request data (from the client to the server) and the second is
626 used for the response data (from the server to the client).
627
628 Each buffer contains two types of data. The first type is the incoming data
629 waiting for a processing. The second part is the outgoing data already
630 processed. Usually, the incoming data is processed, after it is tagged as
631 outgoing data, and finally it is sent. The following functions provides tools
632 for manipulating these data in a buffer.
633
634 The following diagram shows where the channel class function are applied.
635
636 **Warning**: It is not possible to read from the response in request action,
637 and it is not possible to read for the request channel in response action.
638
Thierry FOURNIER2e4893c2015-03-18 13:37:27 +0100639.. image:: _static/channel.png
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100640
Thierry FOURNIER486f5a02015-03-16 15:13:03 +0100641.. js:function:: Channel.dup(channel)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100642
643 This function returns a string that contain the entire buffer. The data is
644 not remove from the buffer and can be reprocessed later.
645
646 If the buffer cant receive more data, a 'nil' value is returned.
647
Thierry FOURNIER486f5a02015-03-16 15:13:03 +0100648 :param class_channel channel: The manipulated Channel.
Pieter Baauw4d7f7662015-11-08 16:38:08 +0100649 :returns: a string containing all the available data or nil.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100650
Thierry FOURNIER486f5a02015-03-16 15:13:03 +0100651.. js:function:: Channel.get(channel)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100652
653 This function returns a string that contain the entire buffer. The data is
654 consumed from the buffer.
655
656 If the buffer cant receive more data, a 'nil' value is returned.
657
Thierry FOURNIER486f5a02015-03-16 15:13:03 +0100658 :param class_channel channel: The manipulated Channel.
Pieter Baauw4d7f7662015-11-08 16:38:08 +0100659 :returns: a string containing all the available data or nil.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100660
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +0200661.. js:function:: Channel.getline(channel)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100662
663 This function returns a string that contain the first line of the buffer. The
664 data is consumed. If the data returned doesn't contains a final '\n' its
665 assumed than its the last available data in the buffer.
666
667 If the buffer cant receive more data, a 'nil' value is returned.
668
Thierry FOURNIER486f5a02015-03-16 15:13:03 +0100669 :param class_channel channel: The manipulated Channel.
Pieter Baauw386a1272015-08-16 15:26:24 +0200670 :returns: a string containing the available line or nil.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100671
Thierry FOURNIER486f5a02015-03-16 15:13:03 +0100672.. js:function:: Channel.set(channel, string)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100673
674 This function replace the content of the buffer by the string. The function
675 returns the copied length, otherwise, it returns -1.
676
677 The data set with this function are not send. They wait for the end of
678 HAProxy processing, so the buffer can be full.
679
Thierry FOURNIER486f5a02015-03-16 15:13:03 +0100680 :param class_channel channel: The manipulated Channel.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100681 :param string string: The data which will sent.
Pieter Baauw4d7f7662015-11-08 16:38:08 +0100682 :returns: an integer containing the amount of bytes copied or -1.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100683
Thierry FOURNIER486f5a02015-03-16 15:13:03 +0100684.. js:function:: Channel.append(channel, string)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100685
686 This function append the string argument to the content of the buffer. The
687 function returns the copied length, otherwise, it returns -1.
688
689 The data set with this function are not send. They wait for the end of
690 HAProxy processing, so the buffer can be full.
691
Thierry FOURNIER486f5a02015-03-16 15:13:03 +0100692 :param class_channel channel: The manipulated Channel.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100693 :param string string: The data which will sent.
Pieter Baauw4d7f7662015-11-08 16:38:08 +0100694 :returns: an integer containing the amount of bytes copied or -1.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100695
Thierry FOURNIER486f5a02015-03-16 15:13:03 +0100696.. js:function:: Channel.send(channel, string)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100697
698 This function required immediate send of the data. Unless if the connection
699 is close, the buffer is regularly flushed and all the string can be sent.
700
Thierry FOURNIER486f5a02015-03-16 15:13:03 +0100701 :param class_channel channel: The manipulated Channel.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100702 :param string string: The data which will sent.
Pieter Baauw4d7f7662015-11-08 16:38:08 +0100703 :returns: an integer containing the amount of bytes copied or -1.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100704
Thierry FOURNIER486f5a02015-03-16 15:13:03 +0100705.. js:function:: Channel.get_in_length(channel)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100706
707 This function returns the length of the input part of the buffer.
708
Thierry FOURNIER486f5a02015-03-16 15:13:03 +0100709 :param class_channel channel: The manipulated Channel.
Pieter Baauw4d7f7662015-11-08 16:38:08 +0100710 :returns: an integer containing the amount of available bytes.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100711
Thierry FOURNIER486f5a02015-03-16 15:13:03 +0100712.. js:function:: Channel.get_out_length(channel)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100713
714 This function returns the length of the output part of the buffer.
715
Thierry FOURNIER486f5a02015-03-16 15:13:03 +0100716 :param class_channel channel: The manipulated Channel.
Pieter Baauw4d7f7662015-11-08 16:38:08 +0100717 :returns: an integer containing the amount of available bytes.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100718
Thierry FOURNIER486f5a02015-03-16 15:13:03 +0100719.. js:function:: Channel.forward(channel, int)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100720
721 This function transfer bytes from the input part of the buffer to the output
722 part.
723
Thierry FOURNIER486f5a02015-03-16 15:13:03 +0100724 :param class_channel channel: The manipulated Channel.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100725 :param integer int: The amount of data which will be forwarded.
726
Thierry FOURNIERdc595002015-12-21 11:13:52 +0100727
728.. _http_class:
Thierry FOURNIER08504f42015-03-16 14:17:08 +0100729
730HTTP class
731==========
732
733.. js:class:: HTTP
734
735 This class contain all the HTTP manipulation functions.
736
Pieter Baauw386a1272015-08-16 15:26:24 +0200737.. js:function:: HTTP.req_get_headers(http)
Thierry FOURNIER08504f42015-03-16 14:17:08 +0100738
739 Returns an array containing all the request headers.
740
741 :param class_http http: The related http object.
742 :returns: array of headers.
Pieter Baauw386a1272015-08-16 15:26:24 +0200743 :see: HTTP.res_get_headers()
Thierry FOURNIER08504f42015-03-16 14:17:08 +0100744
Thierry FOURNIERdc595002015-12-21 11:13:52 +0100745 This is the form of the returned array:
746
747.. code-block:: lua
748
749 HTTP:req_get_headers()['<header-name>'][<header-index>] = "<header-value>"
750
751 local hdr = HTTP:req_get_headers()
752 hdr["host"][0] = "www.test.com"
753 hdr["accept"][0] = "audio/basic q=1"
754 hdr["accept"][1] = "audio/*, q=0.2"
755 hdr["accept"][2] = "*/*, q=0.1"
756..
757
Pieter Baauw386a1272015-08-16 15:26:24 +0200758.. js:function:: HTTP.res_get_headers(http)
Thierry FOURNIER08504f42015-03-16 14:17:08 +0100759
760 Returns an array containing all the response headers.
761
762 :param class_http http: The related http object.
763 :returns: array of headers.
Pieter Baauw386a1272015-08-16 15:26:24 +0200764 :see: HTTP.req_get_headers()
Thierry FOURNIER08504f42015-03-16 14:17:08 +0100765
Thierry FOURNIERdc595002015-12-21 11:13:52 +0100766 This is the form of the returned array:
767
768.. code-block:: lua
769
770 HTTP:res_get_headers()['<header-name>'][<header-index>] = "<header-value>"
771
772 local hdr = HTTP:req_get_headers()
773 hdr["host"][0] = "www.test.com"
774 hdr["accept"][0] = "audio/basic q=1"
775 hdr["accept"][1] = "audio/*, q=0.2"
776 hdr["accept"][2] = "*.*, q=0.1"
777..
778
Thierry FOURNIER486f5a02015-03-16 15:13:03 +0100779.. js:function:: HTTP.req_add_header(http, name, value)
Thierry FOURNIER08504f42015-03-16 14:17:08 +0100780
781 Appends an HTTP header field in the request whose name is
782 specified in "name" and whose value is defined in "value".
783
784 :param class_http http: The related http object.
785 :param string name: The header name.
786 :param string value: The header value.
Thierry FOURNIER486f5a02015-03-16 15:13:03 +0100787 :see: HTTP.res_add_header()
Thierry FOURNIER08504f42015-03-16 14:17:08 +0100788
Thierry FOURNIER486f5a02015-03-16 15:13:03 +0100789.. js:function:: HTTP.res_add_header(http, name, value)
Thierry FOURNIER08504f42015-03-16 14:17:08 +0100790
791 appends an HTTP header field in the response whose name is
792 specified in "name" and whose value is defined in "value".
793
794 :param class_http http: The related http object.
795 :param string name: The header name.
796 :param string value: The header value.
Thierry FOURNIER486f5a02015-03-16 15:13:03 +0100797 :see: HTTP.req_add_header()
Thierry FOURNIER08504f42015-03-16 14:17:08 +0100798
Thierry FOURNIER486f5a02015-03-16 15:13:03 +0100799.. js:function:: HTTP.req_del_header(http, name)
Thierry FOURNIER08504f42015-03-16 14:17:08 +0100800
801 Removes all HTTP header fields in the request whose name is
802 specified in "name".
803
804 :param class_http http: The related http object.
805 :param string name: The header name.
Thierry FOURNIER486f5a02015-03-16 15:13:03 +0100806 :see: HTTP.res_del_header()
Thierry FOURNIER08504f42015-03-16 14:17:08 +0100807
Thierry FOURNIER486f5a02015-03-16 15:13:03 +0100808.. js:function:: HTTP.res_del_header(http, name)
Thierry FOURNIER08504f42015-03-16 14:17:08 +0100809
810 Removes all HTTP header fields in the response whose name is
811 specified in "name".
812
813 :param class_http http: The related http object.
814 :param string name: The header name.
Thierry FOURNIER486f5a02015-03-16 15:13:03 +0100815 :see: HTTP.req_del_header()
Thierry FOURNIER08504f42015-03-16 14:17:08 +0100816
Thierry FOURNIER486f5a02015-03-16 15:13:03 +0100817.. js:function:: HTTP.req_set_header(http, name, value)
Thierry FOURNIER08504f42015-03-16 14:17:08 +0100818
819 This variable replace all occurence of all header "name", by only
820 one containing the "value".
821
822 :param class_http http: The related http object.
823 :param string name: The header name.
824 :param string value: The header value.
Thierry FOURNIER486f5a02015-03-16 15:13:03 +0100825 :see: HTTP.res_set_header()
Thierry FOURNIER08504f42015-03-16 14:17:08 +0100826
827 This function does the same work as the folowwing code:
828
829.. code-block:: lua
830
831 function fcn(txn)
Thierry FOURNIER486f5a02015-03-16 15:13:03 +0100832 TXN.http:req_del_header("header")
833 TXN.http:req_add_header("header", "value")
Thierry FOURNIER08504f42015-03-16 14:17:08 +0100834 end
835..
836
Thierry FOURNIER486f5a02015-03-16 15:13:03 +0100837.. js:function:: HTTP.res_set_header(http, name, value)
Thierry FOURNIER08504f42015-03-16 14:17:08 +0100838
839 This variable replace all occurence of all header "name", by only
840 one containing the "value".
841
842 :param class_http http: The related http object.
843 :param string name: The header name.
844 :param string value: The header value.
Pieter Baauw386a1272015-08-16 15:26:24 +0200845 :see: HTTP.req_rep_header()
Thierry FOURNIER08504f42015-03-16 14:17:08 +0100846
Pieter Baauw386a1272015-08-16 15:26:24 +0200847.. js:function:: HTTP.req_rep_header(http, name, regex, replace)
Thierry FOURNIER08504f42015-03-16 14:17:08 +0100848
849 Matches the regular expression in all occurrences of header field "name"
850 according to "regex", and replaces them with the "replace" argument. The
851 replacement value can contain back references like \1, \2, ... This
852 function works with the request.
853
854 :param class_http http: The related http object.
855 :param string name: The header name.
856 :param string regex: The match regular expression.
857 :param string replace: The replacement value.
Pieter Baauw386a1272015-08-16 15:26:24 +0200858 :see: HTTP.res_rep_header()
Thierry FOURNIER08504f42015-03-16 14:17:08 +0100859
Pieter Baauw386a1272015-08-16 15:26:24 +0200860.. js:function:: HTTP.res_rep_header(http, name, regex, string)
Thierry FOURNIER08504f42015-03-16 14:17:08 +0100861
862 Matches the regular expression in all occurrences of header field "name"
863 according to "regex", and replaces them with the "replace" argument. The
864 replacement value can contain back references like \1, \2, ... This
865 function works with the request.
866
867 :param class_http http: The related http object.
868 :param string name: The header name.
869 :param string regex: The match regular expression.
870 :param string replace: The replacement value.
Thierry FOURNIER486f5a02015-03-16 15:13:03 +0100871 :see: HTTP.req_replace_header()
Thierry FOURNIER08504f42015-03-16 14:17:08 +0100872
Thierry FOURNIER486f5a02015-03-16 15:13:03 +0100873.. js:function:: HTTP.req_replace_value(http, name, regex, replace)
Thierry FOURNIER08504f42015-03-16 14:17:08 +0100874
Thierry FOURNIER486f5a02015-03-16 15:13:03 +0100875 Works like "HTTP.req_replace_header()" except that it matches the regex
Thierry FOURNIER08504f42015-03-16 14:17:08 +0100876 against every comma-delimited value of the header field "name" instead of the
877 entire header.
878
879 :param class_http http: The related http object.
880 :param string name: The header name.
881 :param string regex: The match regular expression.
882 :param string replace: The replacement value.
Thierry FOURNIER486f5a02015-03-16 15:13:03 +0100883 :see: HTTP.req_replace_header()
884 :see: HTTP.res_replace_value()
Thierry FOURNIER08504f42015-03-16 14:17:08 +0100885
Thierry FOURNIER486f5a02015-03-16 15:13:03 +0100886.. js:function:: HTTP.res_replace_value(http, name, regex, replace)
Thierry FOURNIER08504f42015-03-16 14:17:08 +0100887
Thierry FOURNIER486f5a02015-03-16 15:13:03 +0100888 Works like "HTTP.res_replace_header()" except that it matches the regex
Thierry FOURNIER08504f42015-03-16 14:17:08 +0100889 against every comma-delimited value of the header field "name" instead of the
890 entire header.
891
892 :param class_http http: The related http object.
893 :param string name: The header name.
894 :param string regex: The match regular expression.
895 :param string replace: The replacement value.
Thierry FOURNIER486f5a02015-03-16 15:13:03 +0100896 :see: HTTP.res_replace_header()
897 :see: HTTP.req_replace_value()
Thierry FOURNIER08504f42015-03-16 14:17:08 +0100898
Thierry FOURNIER486f5a02015-03-16 15:13:03 +0100899.. js:function:: HTTP.req_set_method(http, method)
Thierry FOURNIER08504f42015-03-16 14:17:08 +0100900
901 Rewrites the request method with the parameter "method".
902
903 :param class_http http: The related http object.
904 :param string method: The new method.
905
Thierry FOURNIER486f5a02015-03-16 15:13:03 +0100906.. js:function:: HTTP.req_set_path(http, path)
Thierry FOURNIER08504f42015-03-16 14:17:08 +0100907
908 Rewrites the request path with the "path" parameter.
909
910 :param class_http http: The related http object.
911 :param string path: The new path.
912
Thierry FOURNIER486f5a02015-03-16 15:13:03 +0100913.. js:function:: HTTP.req_set_query(http, query)
Thierry FOURNIER08504f42015-03-16 14:17:08 +0100914
915 Rewrites the request's query string which appears after the first question
916 mark ("?") with the parameter "query".
917
918 :param class_http http: The related http object.
919 :param string query: The new query.
920
Thierry FOURNIER0d79cf62015-08-26 14:20:58 +0200921.. js:function:: HTTP.req_set_uri(http, uri)
Thierry FOURNIER08504f42015-03-16 14:17:08 +0100922
923 Rewrites the request URI with the parameter "uri".
924
925 :param class_http http: The related http object.
926 :param string uri: The new uri.
927
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +0200928.. js:function:: HTTP.res_set_status(http, status)
929
930 Rewrites the response status code with the parameter "code". Note that the
931 reason is automatically adapted to the new code.
932
933 :param class_http http: The related http object.
934 :param integer status: The new response status code.
935
Thierry FOURNIERdc595002015-12-21 11:13:52 +0100936.. _txn_class:
937
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100938TXN class
939=========
940
941.. js:class:: TXN
942
943 The txn class contain all the functions relative to the http or tcp
944 transaction (Note than a tcp stream is the same than a tcp transaction, but
945 an HTTP transaction is not the same than a tcp stream).
946
947 The usage of this class permits to retrieve data from the requests, alter it
948 and forward it.
949
950 All the functions provided by this class are available in the context
951 **sample-fetches** and **actions**.
952
Thierry FOURNIER486f5a02015-03-16 15:13:03 +0100953.. js:attribute:: TXN.c
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100954
Thierry FOURNIERdc595002015-12-21 11:13:52 +0100955 :returns: An :ref:`converters_class`.
956
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100957 This attribute contains a Converters class object.
958
Thierry FOURNIER486f5a02015-03-16 15:13:03 +0100959.. js:attribute:: TXN.sc
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100960
Thierry FOURNIERdc595002015-12-21 11:13:52 +0100961 :returns: An :ref:`converters_class`.
962
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100963 This attribute contains a Converters class object. The functions of
964 this object returns always a string.
965
Thierry FOURNIER486f5a02015-03-16 15:13:03 +0100966.. js:attribute:: TXN.f
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100967
Thierry FOURNIERdc595002015-12-21 11:13:52 +0100968 :returns: An :ref:`fetches_class`.
969
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100970 This attribute contains a Fetches class object.
971
Thierry FOURNIER486f5a02015-03-16 15:13:03 +0100972.. js:attribute:: TXN.sf
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100973
Thierry FOURNIERdc595002015-12-21 11:13:52 +0100974 :returns: An :ref:`fetches_class`.
975
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100976 This attribute contains a Fetches class object. The functions of
977 this object returns always a string.
978
Thierry FOURNIER486f5a02015-03-16 15:13:03 +0100979.. js:attribute:: TXN.req
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100980
Thierry FOURNIERdc595002015-12-21 11:13:52 +0100981 :returns: An :ref:`channel_class`.
982
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100983 This attribute contains a channel class object for the request buffer.
984
Thierry FOURNIER486f5a02015-03-16 15:13:03 +0100985.. js:attribute:: TXN.res
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100986
Thierry FOURNIERdc595002015-12-21 11:13:52 +0100987 :returns: An :ref:`channel_class`.
988
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100989 This attribute contains a channel class object for the response buffer.
990
Thierry FOURNIER486f5a02015-03-16 15:13:03 +0100991.. js:attribute:: TXN.http
Thierry FOURNIER08504f42015-03-16 14:17:08 +0100992
Thierry FOURNIERdc595002015-12-21 11:13:52 +0100993 :returns: An :ref:`http_class`.
994
Thierry FOURNIER08504f42015-03-16 14:17:08 +0100995 This attribute contains an HTTP class object. It is avalaible only if the
996 proxy has the "mode http" enabled.
997
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100998.. js:function:: TXN.log(TXN, loglevel, msg)
999
1000 This function sends a log. The log is sent, according with the HAProxy
1001 configuration file, on the default syslog server if it is configured and on
1002 the stderr if it is allowed.
1003
1004 :param class_txn txn: The class txn object containing the data.
1005 :param integer loglevel: Is the log level asociated with the message. It is a
1006 number between 0 and 7.
1007 :param string msg: The log content.
1008 :see: core.emerg, core.alert, core.crit, core.err, core.warning, core.notice,
1009 core.info, core.debug (log level definitions)
1010 :see: TXN.deflog
1011 :see: TXN.Debug
1012 :see: TXN.Info
1013 :see: TXN.Warning
1014 :see: TXN.Alert
1015
1016.. js:function:: TXN.deflog(TXN, msg)
1017
1018 Sends a log line with the default loglevel for the proxy ssociated with the
1019 transaction.
1020
1021 :param class_txn txn: The class txn object containing the data.
1022 :param string msg: The log content.
1023 :see: TXN.log
1024
1025.. js:function:: TXN.Debug(txn, msg)
1026
1027 :param class_txn txn: The class txn object containing the data.
1028 :param string msg: The log content.
1029 :see: TXN.log
1030
1031 Does the same job than:
1032
1033.. code-block:: lua
1034
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001035 function Debug(txn, msg)
1036 TXN.log(txn, core.debug, msg)
1037 end
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01001038..
1039
1040.. js:function:: TXN.Info(txn, msg)
1041
1042 :param class_txn txn: The class txn object containing the data.
1043 :param string msg: The log content.
1044 :see: TXN.log
1045
1046.. code-block:: lua
1047
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001048 function Debug(txn, msg)
1049 TXN.log(txn, core.info, msg)
1050 end
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01001051..
1052
1053.. js:function:: TXN.Warning(txn, msg)
1054
1055 :param class_txn txn: The class txn object containing the data.
1056 :param string msg: The log content.
1057 :see: TXN.log
1058
1059.. code-block:: lua
1060
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001061 function Debug(txn, msg)
1062 TXN.log(txn, core.warning, msg)
1063 end
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01001064..
1065
1066.. js:function:: TXN.Alert(txn, msg)
1067
1068 :param class_txn txn: The class txn object containing the data.
1069 :param string msg: The log content.
1070 :see: TXN.log
1071
1072.. code-block:: lua
1073
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001074 function Debug(txn, msg)
1075 TXN.log(txn, core.alert, msg)
1076 end
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01001077..
1078
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001079.. js:function:: TXN.get_priv(txn)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001080
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001081 Return Lua data stored in the current transaction (with the `TXN.set_priv()`)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001082 function. If no data are stored, it returns a nil value.
1083
1084 :param class_txn txn: The class txn object containing the data.
1085 :returns: the opaque data previsously stored, or nil if nothing is
1086 avalaible.
1087
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001088.. js:function:: TXN.set_priv(txn, data)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001089
1090 Store any data in the current HAProxy transaction. This action replace the
1091 old stored data.
1092
1093 :param class_txn txn: The class txn object containing the data.
1094 :param opaque data: The data which is stored in the transaction.
1095
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +02001096.. js:function:: TXN.set_var(TXN, var, value)
1097
David Carlier61fdf8b2015-10-02 11:59:38 +01001098 Converts a Lua type in a HAProxy type and store it in a variable <var>.
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +02001099
1100 :param class_txn txn: The class txn object containing the data.
1101 :param string var: The variable name according with the HAProxy variable syntax.
1102 :param opaque value: The data which is stored in the variable.
1103
1104.. js:function:: TXN.get_var(TXN, var)
1105
1106 Returns data stored in the variable <var> converter in Lua type.
1107
1108 :param class_txn txn: The class txn object containing the data.
1109 :param string var: The variable name according with the HAProxy variable syntax.
1110
Willy Tarreaubc183a62015-08-28 10:39:11 +02001111.. js:function:: TXN.done(txn)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001112
Willy Tarreaubc183a62015-08-28 10:39:11 +02001113 This function terminates processing of the transaction and the associated
1114 session. It can be used when a critical error is detected or to terminate
1115 processing after some data have been returned to the client (eg: a redirect).
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001116
1117 :param class_txn txn: The class txn object containing the data.
1118
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001119.. js:function:: TXN.set_loglevel(txn, loglevel)
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01001120
1121 Is used to change the log level of the current request. The "loglevel" must
1122 be an integer between 0 and 7.
1123
1124 :param class_txn txn: The class txn object containing the data.
1125 :param integer loglevel: The required log level. This variable can be one of
1126 :see: core.<loglevel>
1127
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001128.. js:function:: TXN.set_tos(txn, tos)
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01001129
1130 Is used to set the TOS or DSCP field value of packets sent to the client to
1131 the value passed in "tos" on platforms which support this.
1132
1133 :param class_txn txn: The class txn object containing the data.
1134 :param integer tos: The new TOS os DSCP.
1135
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001136.. js:function:: TXN.set_mark(txn, mark)
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01001137
1138 Is used to set the Netfilter MARK on all packets sent to the client to the
1139 value passed in "mark" on platforms which support it.
1140
1141 :param class_txn txn: The class txn object containing the data.
1142 :param integer mark: The mark value.
1143
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001144.. _socket_class:
1145
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001146Socket class
1147============
1148
1149.. js:class:: Socket
1150
1151 This class must be compatible with the Lua Socket class. Only the 'client'
1152 functions are available. See the Lua Socket documentation:
1153
1154 `http://w3.impa.br/~diego/software/luasocket/tcp.html
1155 <http://w3.impa.br/~diego/software/luasocket/tcp.html>`_
1156
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001157.. js:function:: Socket.close(socket)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001158
1159 Closes a TCP object. The internal socket used by the object is closed and the
1160 local address to which the object was bound is made available to other
1161 applications. No further operations (except for further calls to the close
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001162 method) are allowed on a closed Socket.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001163
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001164 :param class_socket socket: Is the manipulated Socket.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001165
1166 Note: It is important to close all used sockets once they are not needed,
1167 since, in many systems, each socket uses a file descriptor, which are limited
1168 system resources. Garbage-collected objects are automatically closed before
1169 destruction, though.
1170
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02001171.. js:function:: Socket.connect(socket, address[, port])
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001172
1173 Attempts to connect a socket object to a remote host.
1174
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001175
1176 In case of error, the method returns nil followed by a string describing the
1177 error. In case of success, the method returns 1.
1178
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001179 :param class_socket socket: Is the manipulated Socket.
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02001180 :param string address: can be an IP address or a host name. See below for more
1181 information.
1182 :param integer port: must be an integer number in the range [1..64K].
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001183 :returns: 1 or nil.
1184
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02001185 an address field extension permits to use the connect() function to connect to
1186 other stream than TCP. The syntax containing a simpleipv4 or ipv6 address is
1187 the basically expected format. This format requires the port.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001188
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02001189 Other format accepted are a socket path like "/socket/path", it permits to
1190 connect to a socket. abstract namespaces are supported with the prefix
1191 "abns@", and finaly a filedescriotr can be passed with the prefix "fd@".
1192 The prefix "ipv4@", "ipv6@" and "unix@" are also supported. The port can be
1193 passed int the string. The syntax "127.0.0.1:1234" is valid. in this case, the
1194 parameter *port* is ignored.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001195
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001196.. js:function:: Socket.connect_ssl(socket, address, port)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001197
1198 Same behavior than the function socket:connect, but uses SSL.
1199
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001200 :param class_socket socket: Is the manipulated Socket.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001201 :returns: 1 or nil.
1202
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001203.. js:function:: Socket.getpeername(socket)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001204
1205 Returns information about the remote side of a connected client object.
1206
1207 Returns a string with the IP address of the peer, followed by the port number
1208 that peer is using for the connection. In case of error, the method returns
1209 nil.
1210
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001211 :param class_socket socket: Is the manipulated Socket.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001212 :returns: a string containing the server information.
1213
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001214.. js:function:: Socket.getsockname(socket)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001215
1216 Returns the local address information associated to the object.
1217
1218 The method returns a string with local IP address and a number with the port.
1219 In case of error, the method returns nil.
1220
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001221 :param class_socket socket: Is the manipulated Socket.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001222 :returns: a string containing the client information.
1223
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001224.. js:function:: Socket.receive(socket, [pattern [, prefix]])
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001225
1226 Reads data from a client object, according to the specified read pattern.
1227 Patterns follow the Lua file I/O format, and the difference in performance
1228 between all patterns is negligible.
1229
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001230 :param class_socket socket: Is the manipulated Socket.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001231 :param string|integer pattern: Describe what is required (see below).
1232 :param string prefix: A string which will be prefix the returned data.
1233 :returns: a string containing the required data or nil.
1234
1235 Pattern can be any of the following:
1236
1237 * **`*a`**: reads from the socket until the connection is closed. No
1238 end-of-line translation is performed;
1239
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001240 * **`*l`**: reads a line of text from the Socket. The line is terminated by a
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001241 LF character (ASCII 10), optionally preceded by a CR character
1242 (ASCII 13). The CR and LF characters are not included in the
1243 returned line. In fact, all CR characters are ignored by the
1244 pattern. This is the default pattern.
1245
1246 * **number**: causes the method to read a specified number of bytes from the
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001247 Socket. Prefix is an optional string to be concatenated to the
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001248 beginning of any received data before return.
1249
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02001250 * **empty**: If the pattern is left empty, the default option is `*l`.
1251
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001252 If successful, the method returns the received pattern. In case of error, the
1253 method returns nil followed by an error message which can be the string
1254 'closed' in case the connection was closed before the transmission was
1255 completed or the string 'timeout' in case there was a timeout during the
1256 operation. Also, after the error message, the function returns the partial
1257 result of the transmission.
1258
1259 Important note: This function was changed severely. It used to support
1260 multiple patterns (but I have never seen this feature used) and now it
1261 doesn't anymore. Partial results used to be returned in the same way as
1262 successful results. This last feature violated the idea that all functions
1263 should return nil on error. Thus it was changed too.
1264
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001265.. js:function:: Socket.send(socket, data [, start [, end ]])
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001266
1267 Sends data through client object.
1268
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001269 :param class_socket socket: Is the manipulated Socket.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001270 :param string data: The data that will be sent.
1271 :param integer start: The start position in the buffer of the data which will
1272 be sent.
1273 :param integer end: The end position in the buffer of the data which will
1274 be sent.
1275 :returns: see below.
1276
1277 Data is the string to be sent. The optional arguments i and j work exactly
1278 like the standard string.sub Lua function to allow the selection of a
1279 substring to be sent.
1280
1281 If successful, the method returns the index of the last byte within [start,
1282 end] that has been sent. Notice that, if start is 1 or absent, this is
1283 effectively the total number of bytes sent. In case of error, the method
1284 returns nil, followed by an error message, followed by the index of the last
1285 byte within [start, end] that has been sent. You might want to try again from
1286 the byte following that. The error message can be 'closed' in case the
1287 connection was closed before the transmission was completed or the string
1288 'timeout' in case there was a timeout during the operation.
1289
1290 Note: Output is not buffered. For small strings, it is always better to
1291 concatenate them in Lua (with the '..' operator) and send the result in one
1292 call instead of calling the method several times.
1293
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001294.. js:function:: Socket.setoption(socket, option [, value])
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001295
1296 Just implemented for compatibility, this cal does nothing.
1297
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001298.. js:function:: Socket.settimeout(socket, value [, mode])
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001299
1300 Changes the timeout values for the object. All I/O operations are blocking.
1301 That is, any call to the methods send, receive, and accept will block
1302 indefinitely, until the operation completes. The settimeout method defines a
1303 limit on the amount of time the I/O methods can block. When a timeout time
1304 has elapsed, the affected methods give up and fail with an error code.
1305
1306 The amount of time to wait is specified as the value parameter, in seconds.
1307
1308 The timeout modes are bot implemented, the only settable timeout is the
1309 inactivity time waiting for complete the internal buffer send or waiting for
1310 receive data.
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 :param integer value: The timeout value.
1314
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001315.. _map_class:
1316
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001317Map class
1318=========
1319
1320.. js:class:: Map
1321
1322 This class permits to do some lookup in HAProxy maps. The declared maps can
1323 be modified during the runtime throught the HAProxy management socket.
1324
1325.. code-block:: lua
1326
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001327 default = "usa"
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001328
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001329 -- Create and load map
1330 geo = Map.new("geo.map", Map.ip);
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001331
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001332 -- Create new fetch that returns the user country
1333 core.register_fetches("country", function(txn)
1334 local src;
1335 local loc;
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001336
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001337 src = txn.f:fhdr("x-forwarded-for");
1338 if (src == nil) then
1339 src = txn.f:src()
1340 if (src == nil) then
1341 return default;
1342 end
1343 end
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001344
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001345 -- Perform lookup
1346 loc = geo:lookup(src);
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001347
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001348 if (loc == nil) then
1349 return default;
1350 end
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001351
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001352 return loc;
1353 end);
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001354
1355.. js:attribute:: Map.int
1356
1357 See the HAProxy configuration.txt file, chapter "Using ACLs and fetching
1358 samples" ans subchapter "ACL basics" to understand this pattern matching
1359 method.
1360
1361.. js:attribute:: Map.ip
1362
1363 See the HAProxy configuration.txt file, chapter "Using ACLs and fetching
1364 samples" ans subchapter "ACL basics" to understand this pattern matching
1365 method.
1366
1367.. js:attribute:: Map.str
1368
1369 See the HAProxy configuration.txt file, chapter "Using ACLs and fetching
1370 samples" ans subchapter "ACL basics" to understand this pattern matching
1371 method.
1372
1373.. js:attribute:: Map.beg
1374
1375 See the HAProxy configuration.txt file, chapter "Using ACLs and fetching
1376 samples" ans subchapter "ACL basics" to understand this pattern matching
1377 method.
1378
1379.. js:attribute:: Map.sub
1380
1381 See the HAProxy configuration.txt file, chapter "Using ACLs and fetching
1382 samples" ans subchapter "ACL basics" to understand this pattern matching
1383 method.
1384
1385.. js:attribute:: Map.dir
1386
1387 See the HAProxy configuration.txt file, chapter "Using ACLs and fetching
1388 samples" ans subchapter "ACL basics" to understand this pattern matching
1389 method.
1390
1391.. js:attribute:: Map.dom
1392
1393 See the HAProxy configuration.txt file, chapter "Using ACLs and fetching
1394 samples" ans subchapter "ACL basics" to understand this pattern matching
1395 method.
1396
1397.. js:attribute:: Map.end
1398
1399 See the HAProxy configuration.txt file, chapter "Using ACLs and fetching
1400 samples" ans subchapter "ACL basics" to understand this pattern matching
1401 method.
1402
1403.. js:attribute:: Map.reg
1404
1405 See the HAProxy configuration.txt file, chapter "Using ACLs and fetching
1406 samples" ans subchapter "ACL basics" to understand this pattern matching
1407 method.
1408
1409
1410.. js:function:: Map.new(file, method)
1411
1412 Creates and load a map.
1413
1414 :param string file: Is the file containing the map.
1415 :param integer method: Is the map pattern matching method. See the attributes
1416 of the Map class.
1417 :returns: a class Map object.
1418 :see: The Map attributes.
1419
1420.. js:function:: Map.lookup(map, str)
1421
1422 Perform a lookup in a map.
1423
1424 :param class_map map: Is the class Map object.
1425 :param string str: Is the string used as key.
1426 :returns: a string containing the result or nil if no match.
1427
1428.. js:function:: Map.slookup(map, str)
1429
1430 Perform a lookup in a map.
1431
1432 :param class_map map: Is the class Map object.
1433 :param string str: Is the string used as key.
1434 :returns: a string containing the result or empty string if no match.
1435
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001436.. _applethttp_class:
1437
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02001438AppletHTTP class
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001439================
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02001440
1441.. js:class:: AppletHTTP
1442
1443 This class is used with applets that requires the 'http' mode. The http applet
1444 can be registered with the *core.register_service()* function. They are used
1445 for processing an http request like a server in back of HAProxy.
1446
1447 This is an hello world sample code:
1448
1449.. code-block:: lua
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001450
Pieter Baauw4d7f7662015-11-08 16:38:08 +01001451 core.register_service("hello-world", "http", function(applet)
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02001452 local response = "Hello World !"
1453 applet:set_status(200)
Pieter Baauw2dcb9bc2015-10-01 22:47:12 +02001454 applet:add_header("content-length", string.len(response))
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02001455 applet:add_header("content-type", "text/plain")
Pieter Baauw2dcb9bc2015-10-01 22:47:12 +02001456 applet:start_response()
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02001457 applet:send(response)
1458 end)
1459
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001460.. js:attribute:: AppletHTTP.c
1461
1462 :returns: A :ref:`converters_class`
1463
1464 This attribute contains a Converters class object.
1465
1466.. js:attribute:: AppletHTTP.sc
1467
1468 :returns: A :ref:`converters_class`
1469
1470 This attribute contains a Converters class object. The
1471 functions of this object returns always a string.
1472
1473.. js:attribute:: AppletHTTP.f
1474
1475 :returns: A :ref:`fetches_class`
1476
1477 This attribute contains a Fetches class object. Note that the
1478 applet execution place cannot access to a valid HAProxy core HTTP
1479 transaction, so some sample fecthes related to the HTTP dependant
1480 values (hdr, path, ...) are not available.
1481
1482.. js:attribute:: AppletHTTP.sf
1483
1484 :returns: A :ref:`fetches_class`
1485
1486 This attribute contains a Fetches class object. The functions of
1487 this object returns always a string. Note that the applet
1488 execution place cannot access to a valid HAProxy core HTTP
1489 transaction, so some sample fecthes related to the HTTP dependant
1490 values (hdr, path, ...) are not available.
1491
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01001492.. js:attribute:: AppletHTTP.method
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001493
1494 :returns: string
1495
1496 The attribute method returns a string containing the HTTP
1497 method.
1498
1499.. js:attribute:: AppletHTTP.version
1500
1501 :returns: string
1502
1503 The attribute version, returns a string containing the HTTP
1504 request version.
1505
1506.. js:attribute:: AppletHTTP.path
1507
1508 :returns: string
1509
1510 The attribute path returns a string containing the HTTP
1511 request path.
1512
1513.. js:attribute:: AppletHTTP.qs
1514
1515 :returns: string
1516
1517 The attribute qs returns a string containing the HTTP
1518 request query string.
1519
1520.. js:attribute:: AppletHTTP.length
1521
1522 :returns: integer
1523
1524 The attribute length returns an integer containing the HTTP
1525 body length.
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02001526
Thierry FOURNIER841475e2015-12-11 17:10:09 +01001527.. js:attribute:: AppletHTTP.headers
1528
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001529 :returns: array
1530
1531 The attribute headers returns an array containing the HTTP
1532 headers. The header names are always in lower case. As the header name can be
1533 encountered more than once in each request, the value is indexed with 0 as
1534 first index value. The array have this form:
1535
1536.. code-block:: lua
1537
1538 AppletHTTP.headers['<header-name>'][<header-index>] = "<header-value>"
1539
1540 AppletHTTP.headers["host"][0] = "www.test.com"
1541 AppletHTTP.headers["accept"][0] = "audio/basic q=1"
1542 AppletHTTP.headers["accept"][1] = "audio/*, q=0.2"
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01001543 AppletHTTP.headers["accept"][2] = "*/*, q=0.1"
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001544..
1545
1546.. js:attribute:: AppletHTTP.headers
1547
Thierry FOURNIER841475e2015-12-11 17:10:09 +01001548 Contains an array containing all the request headers.
1549
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01001550.. js:function:: AppletHTTP.set_status(applet, code)
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02001551
1552 This function sets the HTTP status code for the response. The allowed code are
1553 from 100 to 599.
1554
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01001555 :param class_AppletHTTP applet: An :ref:`applethttp_class`
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02001556 :param integer code: the status code returned to the client.
1557
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01001558.. js:function:: AppletHTTP.add_header(applet, name, value)
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02001559
1560 This function add an header in the response. Duplicated headers are not
1561 collapsed. The special header *content-length* is used to determinate the
1562 response length. If it not exists, a *transfer-encoding: chunked* is set, and
1563 all the write from the funcion *AppletHTTP:send()* become a chunk.
1564
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01001565 :param class_AppletHTTP applet: An :ref:`applethttp_class`
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02001566 :param string name: the header name
1567 :param string value: the header value
1568
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01001569.. js:function:: AppletHTTP.start_response(applet)
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02001570
1571 This function indicates to the HTTP engine that it can process and send the
1572 response headers. After this called we cannot add headers to the response; We
1573 cannot use the *AppletHTTP:send()* function if the
1574 *AppletHTTP:start_response()* is not called.
1575
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01001576 :param class_AppletHTTP applet: An :ref:`applethttp_class`
1577
1578.. js:function:: AppletHTTP.getline(applet)
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02001579
1580 This function returns a string containing one line from the http body. If the
1581 data returned doesn't contains a final '\\n' its assumed than its the last
1582 available data before the end of stream.
1583
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01001584 :param class_AppletHTTP applet: An :ref:`applethttp_class`
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02001585 :returns: a string. The string can be empty if we reach the end of the stream.
1586
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01001587.. js:function:: AppletHTTP.receive(applet, [size])
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02001588
1589 Reads data from the HTTP body, according to the specified read *size*. If the
1590 *size* is missing, the function tries to read all the content of the stream
1591 until the end. If the *size* is bigger than the http body, it returns the
1592 amount of data avalaible.
1593
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01001594 :param class_AppletHTTP applet: An :ref:`applethttp_class`
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02001595 :param integer size: the required read size.
1596 :returns: always return a string,the string can be empty is the connexion is
1597 closed.
1598
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01001599.. js:function:: AppletHTTP.send(applet, msg)
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02001600
1601 Send the message *msg* on the http request body.
1602
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01001603 :param class_AppletHTTP applet: An :ref:`applethttp_class`
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02001604 :param string msg: the message to send.
1605
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01001606.. js:function:: AppletHTTP.get_priv(applet)
1607
1608 Return Lua data stored in the current transaction (with the
1609 `AppletHTTP.set_priv()`) function. If no data are stored, it returns a nil
1610 value.
1611
1612 :param class_AppletHTTP applet: An :ref:`applethttp_class`
1613 :returns: the opaque data previsously stored, or nil if nothing is
1614 avalaible.
1615
1616.. js:function:: AppletHTTP.set_priv(applet, data)
1617
1618 Store any data in the current HAProxy transaction. This action replace the
1619 old stored data.
1620
1621 :param class_AppletHTTP applet: An :ref:`applethttp_class`
1622 :param opaque data: The data which is stored in the transaction.
1623
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001624.. _applettcp_class:
1625
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02001626AppletTCP class
1627===============
1628
1629.. js:class:: AppletTCP
1630
1631 This class is used with applets that requires the 'tcp' mode. The tcp applet
1632 can be registered with the *core.register_service()* function. They are used
1633 for processing a tcp stream like a server in back of HAProxy.
1634
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001635.. js:attribute:: AppletTCP.c
1636
1637 :returns: A :ref:`converters_class`
1638
1639 This attribute contains a Converters class object.
1640
1641.. js:attribute:: AppletTCP.sc
1642
1643 :returns: A :ref:`converters_class`
1644
1645 This attribute contains a Converters class object. The
1646 functions of this object returns always a string.
1647
1648.. js:attribute:: AppletTCP.f
1649
1650 :returns: A :ref:`fetches_class`
1651
1652 This attribute contains a Fetches class object.
1653
1654.. js:attribute:: AppletTCP.sf
1655
1656 :returns: A :ref:`fetches_class`
1657
1658 This attribute contains a Fetches class object.
1659
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01001660.. js:function:: AppletTCP.getline(applet)
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02001661
1662 This function returns a string containing one line from the stream. If the
1663 data returned doesn't contains a final '\\n' its assumed than its the last
1664 available data before the end of stream.
1665
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01001666 :param class_AppletTCP applet: An :ref:`applettcp_class`
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02001667 :returns: a string. The string can be empty if we reach the end of the stream.
1668
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01001669.. js:function:: AppletTCP.receive(applet, [size])
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02001670
1671 Reads data from the TCP stream, according to the specified read *size*. If the
1672 *size* is missing, the function tries to read all the content of the stream
1673 until the end.
1674
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01001675 :param class_AppletTCP applet: An :ref:`applettcp_class`
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02001676 :param integer size: the required read size.
1677 :returns: always return a string,the string can be empty is the connexion is
1678 closed.
1679
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01001680.. js:function:: AppletTCP.send(appletmsg)
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02001681
1682 Send the message on the stream.
1683
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01001684 :param class_AppletTCP applet: An :ref:`applettcp_class`
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02001685 :param string msg: the message to send.
1686
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01001687.. js:function:: AppletTCP.get_priv(applet)
1688
1689 Return Lua data stored in the current transaction (with the
1690 `AppletTCP.set_priv()`) function. If no data are stored, it returns a nil
1691 value.
1692
1693 :param class_AppletTCP applet: An :ref:`applettcp_class`
1694 :returns: the opaque data previsously stored, or nil if nothing is
1695 avalaible.
1696
1697.. js:function:: AppletTCP.set_priv(applet, data)
1698
1699 Store any data in the current HAProxy transaction. This action replace the
1700 old stored data.
1701
1702 :param class_AppletTCP applet: An :ref:`applettcp_class`
1703 :param opaque data: The data which is stored in the transaction.
1704
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001705External Lua libraries
1706======================
1707
1708A lot of useful lua libraries can be found here:
1709
1710* `https://lua-toolbox.com/ <https://lua-toolbox.com/>`_
1711
1712Redis acces:
1713
1714* `https://github.com/nrk/redis-lua <https://github.com/nrk/redis-lua>`_
1715
1716This is an example about the usage of the Redis library with HAProxy. Note that
1717each call of any function of this library can throw an error if the socket
1718connection fails.
1719
1720.. code-block:: lua
1721
1722 -- load the redis library
1723 local redis = require("redis");
1724
1725 function do_something(txn)
1726
1727 -- create and connect new tcp socket
1728 local tcp = core.tcp();
1729 tcp:settimeout(1);
1730 tcp:connect("127.0.0.1", 6379);
1731
1732 -- use the redis library with this new socket
1733 local client = redis.connect({socket=tcp});
1734 client:ping();
1735
1736 end
1737
1738OpenSSL:
1739
1740* `http://mkottman.github.io/luacrypto/index.html
1741 <http://mkottman.github.io/luacrypto/index.html>`_
1742
1743* `https://github.com/brunoos/luasec/wiki
1744 <https://github.com/brunoos/luasec/wiki>`_
Thierry FOURNIER2e4893c2015-03-18 13:37:27 +01001745