blob: 55d140a5a93ee2efd73cdef851daae058c30c139 [file] [log] [blame]
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001.. toctree::
2 :maxdepth: 2
3
4
5How Lua runs in HAProxy
6=======================
7
8HAProxy Lua running contexts
9----------------------------
10
11The Lua code executed in HAProxy can be processed in 2 main modes. The first one
12is the **initialisation mode**, and the second is the **runtime mode**.
13
14* In the **initialisation mode**, we can perform DNS solves, but we cannot
15 perform socket I/O. In this initialisation mode, HAProxy still blocked during
16 the execution of the Lua program.
17
18* In the **runtime mode**, we cannot perform DNS solves, but we can use sockets.
19 The execution of the Lua code is multiplexed with the requests processing, so
20 the Lua code seems to be run in blocking, but it is not the case.
21
22The Lua code is loaded in one or more files. These files contains main code and
23functions. Lua have 6 execution context.
24
251. The Lua file **body context**. It is executed during the load of the Lua file
26 in the HAProxy `[global]` section with the directive `lua-load`. It is
27 executed in initialisation mode. This section is use for configuring Lua
28 bindings in HAProxy.
29
David Carlier61fdf8b2015-10-02 11:59:38 +0100302. The Lua **init context**. It is a Lua function executed just after the
Thierry FOURNIER17bd1522015-03-11 20:31:00 +010031 HAProxy configuration parsing. The execution is in initialisation mode. In
32 this context the HAProxy environment are already initialized. It is useful to
33 check configuration, or initializing socket connections or tasks. These
34 functions are declared in the body context with the Lua function
35 `core.register_init()`. The prototype of the function is a simple function
36 without return value and without parameters, like this: `function fcn()`.
37
David Carlier61fdf8b2015-10-02 11:59:38 +0100383. The Lua **task context**. It is a Lua function executed after the start
Thierry FOURNIER17bd1522015-03-11 20:31:00 +010039 of the HAProxy scheduler, and just after the declaration of the task with the
40 Lua function `core.register_task()`. This context can be concurrent with the
41 traffic processing. It is executed in runtime mode. The prototype of the
42 function is a simple function without return value and without parameters,
43 like this: `function fcn()`.
44
David Carlier61fdf8b2015-10-02 11:59:38 +0100454. The **action context**. It is a Lua function conditionally executed. These
Thierry FOURNIERa2d02252015-10-01 15:00:42 +020046 actions are registered by the Lua directives "`core.register_action()`". The
47 prototype of the Lua called function is a function with doesn't returns
48 anything and that take an object of class TXN as entry. `function fcn(txn)`.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +010049
505. The **sample-fetch context**. This function takes a TXN object as entry
51 argument and returns a string. These types of function cannot execute any
52 blocking function. They are useful to aggregate some of original HAProxy
53 sample-fetches and return the result. The prototype of the function is
54 `function string fcn(txn)`. These functions can be registered with the Lua
55 function `core.register_fetches()`. Each declared sample-fetch is prefixed by
56 the string "lua.".
57
58 **NOTE**: It is possible that this function cannot found the required data
59 in the original HAProxy sample-fetches, in this case, it cannot return the
60 result. This case is not yet supported
61
David Carlier61fdf8b2015-10-02 11:59:38 +0100626. The **converter context**. It is a Lua function that takes a string as input
Thierry FOURNIER17bd1522015-03-11 20:31:00 +010063 and returns another string as output. These types of function are stateless,
64 it cannot access to any context. They don't execute any blocking function.
65 The call prototype is `function string fcn(string)`. This function can be
66 registered with the Lua function `core.register_converters()`. Each declared
67 converter is prefixed by the string "lua.".
68
69HAProxy Lua Hello world
70-----------------------
71
72HAProxy configuration file (`hello_world.conf`):
73
74::
75
76 global
77 lua-load hello_world.lua
78
79 listen proxy
80 bind 127.0.0.1:10001
Thierry FOURNIERa2d02252015-10-01 15:00:42 +020081 tcp-request inspect-delay 1s
82 tcp-request content use-service lua.hello_world
Thierry FOURNIER17bd1522015-03-11 20:31:00 +010083
84HAProxy Lua file (`hello_world.lua`):
85
86.. code-block:: lua
87
Thierry FOURNIERa2d02252015-10-01 15:00:42 +020088 core.register_service("hello_world", "tcp", function(applet)
89 applet:send("hello world\n")
90 end)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +010091
92How to start HAProxy for testing this configuration:
93
94::
95
96 ./haproxy -f hello_world.conf
97
98On other terminal, you can test with telnet:
99
100::
101
102 #:~ telnet 127.0.0.1 10001
103 hello world
104
105Core class
106==========
107
108.. js:class:: core
109
110 The "core" class contains all the HAProxy core functions. These function are
111 useful for the controlling the execution flow, registering hooks, manipulating
112 global maps or ACL, ...
113
114 "core" class is basically provided with HAProxy. No `require` line is
115 required to uses these function.
116
David Carlier61fdf8b2015-10-02 11:59:38 +0100117 The "core" class is static, it is not possible to create a new object of this
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100118 type.
119
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100120.. js:attribute:: core.emerg
121
Thierry FOURNIERdc595002015-12-21 11:13:52 +0100122 :returns: integer
123
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100124 This attribute is an integer, it contains the value of the loglevel "emergency" (0).
125
126.. js:attribute:: core.alert
127
Thierry FOURNIERdc595002015-12-21 11:13:52 +0100128 :returns: integer
129
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100130 This attribute is an integer, it contains the value of the loglevel "alert" (1).
131
132.. js:attribute:: core.crit
133
Thierry FOURNIERdc595002015-12-21 11:13:52 +0100134 :returns: integer
135
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100136 This attribute is an integer, it contains the value of the loglevel "critical" (2).
137
138.. js:attribute:: core.err
139
Thierry FOURNIERdc595002015-12-21 11:13:52 +0100140 :returns: integer
141
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100142 This attribute is an integer, it contains the value of the loglevel "error" (3).
143
144.. js:attribute:: core.warning
145
Thierry FOURNIERdc595002015-12-21 11:13:52 +0100146 :returns: integer
147
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100148 This attribute is an integer, it contains the value of the loglevel "warning" (4).
149
150.. js:attribute:: core.notice
151
Thierry FOURNIERdc595002015-12-21 11:13:52 +0100152 :returns: integer
153
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100154 This attribute is an integer, it contains the value of the loglevel "notice" (5).
155
156.. js:attribute:: core.info
157
Thierry FOURNIERdc595002015-12-21 11:13:52 +0100158 :returns: integer
159
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100160 This attribute is an integer, it contains the value of the loglevel "info" (6).
161
162.. js:attribute:: core.debug
163
Thierry FOURNIERdc595002015-12-21 11:13:52 +0100164 :returns: integer
165
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100166 This attribute is an integer, it contains the value of the loglevel "debug" (7).
167
168.. js:function:: core.log(loglevel, msg)
169
170 **context**: body, init, task, action, sample-fetch, converter
171
David Carlier61fdf8b2015-10-02 11:59:38 +0100172 This function sends a log. The log is sent, according with the HAProxy
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100173 configuration file, on the default syslog server if it is configured and on
174 the stderr if it is allowed.
175
176 :param integer loglevel: Is the log level asociated with the message. It is a
177 number between 0 and 7.
178 :param string msg: The log content.
179 :see: core.emerg, core.alert, core.crit, core.err, core.warning, core.notice,
180 core.info, core.debug (log level definitions)
181 :see: code.Debug
182 :see: core.Info
183 :see: core.Warning
184 :see: core.Alert
185
186.. js:function:: core.Debug(msg)
187
188 **context**: body, init, task, action, sample-fetch, converter
189
190 :param string msg: The log content.
191 :see: log
192
193 Does the same job than:
194
195.. code-block:: lua
196
Thierry FOURNIERdc595002015-12-21 11:13:52 +0100197 function Debug(msg)
198 core.log(core.debug, msg)
199 end
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100200..
201
202.. js:function:: core.Info(msg)
203
204 **context**: body, init, task, action, sample-fetch, converter
205
206 :param string msg: The log content.
207 :see: log
208
209.. code-block:: lua
210
Thierry FOURNIERdc595002015-12-21 11:13:52 +0100211 function Info(msg)
212 core.log(core.info, msg)
213 end
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100214..
215
216.. js:function:: core.Warning(msg)
217
218 **context**: body, init, task, action, sample-fetch, converter
219
220 :param string msg: The log content.
221 :see: log
222
223.. code-block:: lua
224
Thierry FOURNIERdc595002015-12-21 11:13:52 +0100225 function Warning(msg)
226 core.log(core.warning, msg)
227 end
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100228..
229
230.. js:function:: core.Alert(msg)
231
232 **context**: body, init, task, action, sample-fetch, converter
233
234 :param string msg: The log content.
235 :see: log
236
237.. code-block:: lua
238
Thierry FOURNIERdc595002015-12-21 11:13:52 +0100239 function Alert(msg)
240 core.log(core.alert, msg)
241 end
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100242..
243
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100244.. js:function:: core.add_acl(filename, key)
245
246 **context**: init, task, action, sample-fetch, converter
247
248 Add the ACL *key* in the ACLs list referenced by the file *filename*.
249
250 :param string filename: the filename that reference the ACL entries.
251 :param string key: the key which will be added.
252
253.. js:function:: core.del_acl(filename, key)
254
255 **context**: init, task, action, sample-fetch, converter
256
257 Delete the ACL entry referenced by the key *key* in the list of ACLs
258 referenced by *filename*.
259
260 :param string filename: the filename that reference the ACL entries.
261 :param string key: the key which will be deleted.
262
263.. js:function:: core.del_map(filename, key)
264
265 **context**: init, task, action, sample-fetch, converter
266
267 Delete the map entry indexed with the specified key in the list of maps
268 referenced by his filename.
269
270 :param string filename: the filename that reference the map entries.
271 :param string key: the key which will be deleted.
272
Thierry Fourniereea77c02016-03-18 08:47:13 +0100273.. js:function:: core.get_info()
274
275 **context**: body, init, task, action, sample-fetch, converter
276
277 Returns HAProxy core informations. We can found information like the uptime,
278 the pid, memory pool usage, tasks number, ...
279
280 These information are also returned by the management sockat via the command
281 "show info". See the management socket documentation fpor more information
282 about the content of these variables.
283
284 :returns: an array of values.
285
Thierry Fournierb1f46562016-01-21 09:46:15 +0100286.. js:function:: core.now()
287
288 **context**: body, init, task, action
289
290 This function returns the current time. The time returned is fixed by the
291 HAProxy core and assures than the hour will be monotnic and that the system
292 call 'gettimeofday' will not be called too. The time is refreshed between each
293 Lua execution or resume, so two consecutive call to the function "now" will
294 probably returns the same result.
295
296 :returns: an array which contains two entries "sec" and "usec". "sec"
297 contains the current at the epoch format, and "usec" contains the
298 current microseconds.
299
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100300.. js:function:: core.msleep(milliseconds)
301
302 **context**: body, init, task, action
303
304 The `core.msleep()` stops the Lua execution between specified milliseconds.
305
306 :param integer milliseconds: the required milliseconds.
307
Thierry Fournierf61aa632016-02-19 20:56:00 +0100308.. js:attribute:: core.proxies
309
310 **context**: body, init, task, action, sample-fetch, converter
311
312 proxies is an array containing the list of all proxies declared in the
313 configuration file. Each entry of the proxies array is an object of type
314 :ref:`proxy_class`
315
Thierry FOURNIER8255a752015-09-23 21:03:35 +0200316.. js:function:: core.register_action(name, actions, func)
317
318 **context**: body
319
David Carlier61fdf8b2015-10-02 11:59:38 +0100320 Register a Lua function executed as action. All the registered action can be
Thierry FOURNIER8255a752015-09-23 21:03:35 +0200321 used in HAProxy with the prefix "lua.". An action gets a TXN object class as
322 input.
323
324 :param string name: is the name of the converter.
Willy Tarreau61add3c2015-09-28 15:39:10 +0200325 :param table actions: is a table of string describing the HAProxy actions who
Thierry FOURNIER8255a752015-09-23 21:03:35 +0200326 want to register to. The expected actions are 'tcp-req',
327 'tcp-res', 'http-req' or 'http-res'.
328 :param function func: is the Lua function called to work as converter.
329
330 The prototype of the Lua function used as argument is:
331
332.. code-block:: lua
333
334 function(txn)
335..
336
Thierry FOURNIERdc595002015-12-21 11:13:52 +0100337 * **txn** (:ref:`txn_class`): this is a TXN object used for manipulating the
Thierry FOURNIER8255a752015-09-23 21:03:35 +0200338 current request or TCP stream.
339
Willy Tarreau61add3c2015-09-28 15:39:10 +0200340 Here, an exemple of action registration. the action juste send an 'Hello world'
Thierry FOURNIER8255a752015-09-23 21:03:35 +0200341 in the logs.
342
343.. code-block:: lua
344
345 core.register_action("hello-world", { "tcp-req", "http-req" }, function(txn)
346 txn:Info("Hello world")
347 end)
348..
349
350 This example code is used in HAproxy configuration like this:
351
352::
353
354 frontend tcp_frt
355 mode tcp
356 tcp-request content lua.hello-world
357
358 frontend http_frt
359 mode http
360 http-request lua.hello-world
361
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100362.. js:function:: core.register_converters(name, func)
363
364 **context**: body
365
David Carlier61fdf8b2015-10-02 11:59:38 +0100366 Register a Lua function executed as converter. All the registered converters
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100367 can be used in HAProxy with the prefix "lua.". An converter get a string as
368 input and return a string as output. The registered function can take up to 9
369 values as parameter. All the value are strings.
370
371 :param string name: is the name of the converter.
372 :param function func: is the Lua function called to work as converter.
373
374 The prototype of the Lua function used as argument is:
375
376.. code-block:: lua
377
378 function(str, [p1 [, p2 [, ... [, p5]]]])
379..
380
381 * **str** (*string*): this is the input value automatically converted in
382 string.
383 * **p1** .. **p5** (*string*): this is a list of string arguments declared in
384 the haroxy configuration file. The number of arguments doesn't exceed 5.
385 The order and the nature of these is conventionally choose by the
386 developper.
387
388.. js:function:: core.register_fetches(name, func)
389
390 **context**: body
391
David Carlier61fdf8b2015-10-02 11:59:38 +0100392 Register a Lua function executed as sample fetch. All the registered sample
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100393 fetchs can be used in HAProxy with the prefix "lua.". A Lua sample fetch
394 return a string as output. The registered function can take up to 9 values as
395 parameter. All the value are strings.
396
397 :param string name: is the name of the converter.
398 :param function func: is the Lua function called to work as sample fetch.
399
400 The prototype of the Lua function used as argument is:
401
402.. code-block:: lua
403
404 string function(txn, [p1 [, p2 [, ... [, p5]]]])
405..
406
Thierry FOURNIERdc595002015-12-21 11:13:52 +0100407 * **txn** (:ref:`txn_class`): this is the txn object associated with the current
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100408 request.
409 * **p1** .. **p5** (*string*): this is a list of string arguments declared in
410 the haroxy configuration file. The number of arguments doesn't exceed 5.
411 The order and the nature of these is conventionally choose by the
412 developper.
413 * **Returns**: A string containing some data, ot nil if the value cannot be
414 returned now.
415
416 lua example code:
417
418.. code-block:: lua
419
420 core.register_fetches("hello", function(txn)
421 return "hello"
422 end)
423..
424
425 HAProxy example configuration:
426
427::
428
429 frontend example
430 http-request redirect location /%[lua.hello]
431
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +0200432.. js:function:: core.register_service(name, mode, func)
433
434 **context**: body
435
David Carlier61fdf8b2015-10-02 11:59:38 +0100436 Register a Lua function executed as a service. All the registered service can
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +0200437 be used in HAProxy with the prefix "lua.". A service gets an object class as
438 input according with the required mode.
439
440 :param string name: is the name of the converter.
Willy Tarreau61add3c2015-09-28 15:39:10 +0200441 :param string mode: is string describing the required mode. Only 'tcp' or
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +0200442 'http' are allowed.
443 :param function func: is the Lua function called to work as converter.
444
445 The prototype of the Lua function used as argument is:
446
447.. code-block:: lua
448
449 function(applet)
450..
451
Thierry FOURNIERdc595002015-12-21 11:13:52 +0100452 * **applet** *applet* will be a :ref:`applettcp_class` or a
453 :ref:`applethttp_class`. It depends the type of registered applet. An applet
454 registered with the 'http' value for the *mode* parameter will gets a
455 :ref:`applethttp_class`. If the *mode* value is 'tcp', the applet will gets
456 a :ref:`applettcp_class`.
457
458 **warning**: Applets of type 'http' cannot be called from 'tcp-*'
459 rulesets. Only the 'http-*' rulesets are authorized, this means
460 that is not possible to call an HTTP applet from a proxy in tcp
461 mode. Applets of type 'tcp' can be called from anywhre.
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +0200462
Willy Tarreau61add3c2015-09-28 15:39:10 +0200463 Here, an exemple of service registration. the service just send an 'Hello world'
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +0200464 as an http response.
465
466.. code-block:: lua
467
Pieter Baauw4d7f7662015-11-08 16:38:08 +0100468 core.register_service("hello-world", "http", function(applet)
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +0200469 local response = "Hello World !"
470 applet:set_status(200)
Pieter Baauw2dcb9bc2015-10-01 22:47:12 +0200471 applet:add_header("content-length", string.len(response))
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +0200472 applet:add_header("content-type", "text/plain")
Pieter Baauw2dcb9bc2015-10-01 22:47:12 +0200473 applet:start_response()
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +0200474 applet:send(response)
475 end)
476..
477
478 This example code is used in HAproxy configuration like this:
479
480::
481
482 frontend example
483 http-request use-service lua.hello-world
484
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100485.. js:function:: core.register_init(func)
486
487 **context**: body
488
489 Register a function executed after the configuration parsing. This is useful
490 to check any parameters.
491
Pieter Baauw4d7f7662015-11-08 16:38:08 +0100492 :param function func: is the Lua function called to work as initializer.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100493
494 The prototype of the Lua function used as argument is:
495
496.. code-block:: lua
497
498 function()
499..
500
501 It takes no input, and no output is expected.
502
503.. js:function:: core.register_task(func)
504
505 **context**: body, init, task, action, sample-fetch, converter
506
507 Register and start independent task. The task is started when the HAProxy
508 main scheduler starts. For example this type of tasks can be executed to
Thierry FOURNIER2e4893c2015-03-18 13:37:27 +0100509 perform complex health checks.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100510
Pieter Baauw4d7f7662015-11-08 16:38:08 +0100511 :param function func: is the Lua function called to work as initializer.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100512
513 The prototype of the Lua function used as argument is:
514
515.. code-block:: lua
516
517 function()
518..
519
520 It takes no input, and no output is expected.
521
522.. js:function:: core.set_nice(nice)
523
524 **context**: task, action, sample-fetch, converter
525
526 Change the nice of the current task or current session.
Thierry FOURNIER2e4893c2015-03-18 13:37:27 +0100527
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100528 :param integer nice: the nice value, it must be between -1024 and 1024.
529
530.. js:function:: core.set_map(filename, key, value)
531
532 **context**: init, task, action, sample-fetch, converter
533
534 set the value *value* associated to the key *key* in the map referenced by
535 *filename*.
536
Thierry FOURNIERdc595002015-12-21 11:13:52 +0100537 :param string filename: the Map reference
538 :param string key: the key to set or replace
539 :param string value: the associated value
540
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100541.. js:function:: core.sleep(int seconds)
542
543 **context**: body, init, task, action
544
545 The `core.sleep()` functions stop the Lua execution between specified seconds.
546
547 :param integer seconds: the required seconds.
548
549.. js:function:: core.tcp()
550
551 **context**: init, task, action
552
553 This function returns a new object of a *socket* class.
554
Thierry FOURNIERdc595002015-12-21 11:13:52 +0100555 :returns: A :ref:`socket_class` object.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100556
Thierry Fournier1de16592016-01-27 09:49:07 +0100557.. js:function:: core.concat()
558
559 **context**: body, init, task, action, sample-fetch, converter
560
561 This function retruns a new concat object.
562
563 :returns: A :ref:`concat_class` object.
564
Thierry FOURNIER0a99b892015-08-26 00:14:17 +0200565.. js:function:: core.done(data)
566
567 **context**: body, init, task, action, sample-fetch, converter
568
569 :param any data: Return some data for the caller. It is useful with
570 sample-fetches and sample-converters.
571
572 Immediately stops the current Lua execution and returns to the caller which
573 may be a sample fetch, a converter or an action and returns the specified
574 value (ignored for actions). It is used when the LUA process finishes its
575 work and wants to give back the control to HAProxy without executing the
576 remaining code. It can be seen as a multi-level "return".
577
Thierry FOURNIER486f5a02015-03-16 15:13:03 +0100578.. js:function:: core.yield()
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100579
580 **context**: task, action, sample-fetch, converter
581
582 Give back the hand at the HAProxy scheduler. It is used when the LUA
583 processing consumes a lot of processing time.
584
Thierry Fournierf61aa632016-02-19 20:56:00 +0100585.. _proxy_class:
586
587Proxy class
588============
589
590.. js:class:: Proxy
591
592 This class provides a way for manipulating proxy and retrieving information
593 like statistics.
594
595.. js:function:: Proxy.pause(px)
596
597 Pause the proxy. See the management socket documentation for more information.
598
599 :param class_proxy px: A :ref:`proxy_class` which indicates the manipulated
600 proxy.
601
602.. js:function:: Proxy.resume(px)
603
604 Resume the proxy. See the management socket documentation for more
605 information.
606
607 :param class_proxy px: A :ref:`proxy_class` which indicates the manipulated
608 proxy.
609
610.. js:function:: Proxy.stop(px)
611
612 Stop the proxy. See the management socket documentation for more information.
613
614 :param class_proxy px: A :ref:`proxy_class` which indicates the manipulated
615 proxy.
616
617.. js:function:: Proxy.shut_bcksess(px)
618
619 Kill the session attached to a backup server. See the management socket
620 documentation for more information.
621
622 :param class_proxy px: A :ref:`proxy_class` which indicates the manipulated
623 proxy.
624
625.. js:function:: Proxy.get_cap(px)
626
627 Returns a string describing the capabilities of the proxy.
628
629 :param class_proxy px: A :ref:`proxy_class` which indicates the manipulated
630 proxy.
631 :returns: a string "frontend", "backend", "proxy" or "ruleset".
632
633.. js:function:: Proxy.get_mode(px)
634
635 Returns a string describing the mode of the current proxy.
636
637 :param class_proxy px: A :ref:`proxy_class` which indicates the manipulated
638 proxy.
639 :returns: a string "tcp", "http", "health" or "unknown"
640
641.. js:function:: Proxy.get_stats(px)
642
643 Returns an array containg the proxy statistics. The statistics returned are
644 not the same if the proxy is frontend or a backend.
645
646 :param class_proxy px: A :ref:`proxy_class` which indicates the manipulated
647 proxy.
648 :returns: a key/value array containing stats
649
Thierry Fournier1de16592016-01-27 09:49:07 +0100650.. _concat_class:
651
652Concat class
653============
654
655.. js:class:: Concat
656
657 This class provides a fast way for string concatenation. The way using native
658 Lua concatenation like the code below is slow for some reasons.
659
660.. code-block:: lua
661
662 str = "string1"
663 str = str .. ", string2"
664 str = str .. ", string3"
665..
666
667 For each concatenation, Lua:
668 * allocate memory for the result,
669 * catenate the two string copying the strings in the new memory bloc,
670 * free the old memory block containing the string whoch is no longer used.
671 This process does many memory move, allocation and free. In addition, the
672 memory is not really freed, it is just mark mark as unsused and wait for the
673 garbage collector.
674
675 The Concat class provide an alternative way for catenating strings. It uses
676 the internal Lua mechanism (it does not allocate memory), but it doesn't copy
677 the data more than once.
678
679 On my computer, the following loops spends 0.2s for the Concat method and
680 18.5s for the pure Lua implementation. So, the Concat class is about 1000x
681 faster than the embedded solution.
682
683.. code-block:: lua
684
685 for j = 1, 100 do
686 c = core.concat()
687 for i = 1, 20000 do
688 c:add("#####")
689 end
690 end
691..
692
693.. code-block:: lua
694
695 for j = 1, 100 do
696 c = ""
697 for i = 1, 20000 do
698 c = c .. "#####"
699 end
700 end
701..
702
703.. js:function:: Concat.add(concat, string)
704
705 This function adds a string to the current concatenated string.
706
707 :param class_concat concat: A :ref:`concat_class` which contains the currently
708 builded string.
709 :param string string: A new string to concatenate to the current builded
710 string.
711
712.. js:function:: Concat.dump(concat)
713
714 This function returns the concanated string.
715
716 :param class_concat concat: A :ref:`concat_class` which contains the currently
717 builded string.
718 :returns: the concatenated string
719
Thierry FOURNIERdc595002015-12-21 11:13:52 +0100720.. _fetches_class:
721
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100722Fetches class
723=============
724
725.. js:class:: Fetches
726
727 This class contains a lot of internal HAProxy sample fetches. See the
Thierry FOURNIER2e4893c2015-03-18 13:37:27 +0100728 HAProxy "configuration.txt" documentation for more information about her
729 usage. they are the chapters 7.3.2 to 7.3.6.
730
Thierry FOURNIERdc595002015-12-21 11:13:52 +0100731 **warning** some sample fetches are not available in some context. These
732 limitations are specified in this documentation when theire useful.
733
Thierry FOURNIER2e4893c2015-03-18 13:37:27 +0100734 :see: TXN.f
735 :see: TXN.sf
736
737 Fetches are useful for:
738
739 * get system time,
740 * get environment variable,
741 * get random numbers,
742 * known backend status like the number of users in queue or the number of
743 connections established,
744 * client information like ip source or destination,
745 * deal with stick tables,
746 * Established SSL informations,
747 * HTTP information like headers or method.
748
749.. code-block:: lua
750
Thierry FOURNIERdc595002015-12-21 11:13:52 +0100751 function action(txn)
752 -- Get source IP
753 local clientip = txn.f:src()
754 end
Thierry FOURNIER2e4893c2015-03-18 13:37:27 +0100755..
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100756
Thierry FOURNIERdc595002015-12-21 11:13:52 +0100757.. _converters_class:
758
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100759Converters class
760================
761
762.. js:class:: Converters
763
764 This class contains a lot of internal HAProxy sample converters. See the
Thierry FOURNIER2e4893c2015-03-18 13:37:27 +0100765 HAProxy documentation "configuration.txt" for more information about her
766 usage. Its the chapter 7.3.1.
767
768 :see: TXN.c
769 :see: TXN.sc
770
771 Converters provides statefull transformation. They are useful for:
772
773 * converting input to base64,
774 * applying hash on input string (djb2, crc32, sdbm, wt6),
775 * format date,
776 * json escape,
Pieter Baauw4d7f7662015-11-08 16:38:08 +0100777 * extracting preferred language comparing two lists,
Thierry FOURNIER2e4893c2015-03-18 13:37:27 +0100778 * turn to lower or upper chars,
779 * deal with stick tables.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100780
Thierry FOURNIERdc595002015-12-21 11:13:52 +0100781.. _channel_class:
782
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100783Channel class
784=============
785
786.. js:class:: Channel
787
788 HAProxy uses two buffers for the processing of the requests. The first one is
789 used with the request data (from the client to the server) and the second is
790 used for the response data (from the server to the client).
791
792 Each buffer contains two types of data. The first type is the incoming data
793 waiting for a processing. The second part is the outgoing data already
794 processed. Usually, the incoming data is processed, after it is tagged as
795 outgoing data, and finally it is sent. The following functions provides tools
796 for manipulating these data in a buffer.
797
798 The following diagram shows where the channel class function are applied.
799
800 **Warning**: It is not possible to read from the response in request action,
801 and it is not possible to read for the request channel in response action.
802
Thierry FOURNIER2e4893c2015-03-18 13:37:27 +0100803.. image:: _static/channel.png
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100804
Thierry FOURNIER486f5a02015-03-16 15:13:03 +0100805.. js:function:: Channel.dup(channel)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100806
807 This function returns a string that contain the entire buffer. The data is
808 not remove from the buffer and can be reprocessed later.
809
810 If the buffer cant receive more data, a 'nil' value is returned.
811
Thierry FOURNIER486f5a02015-03-16 15:13:03 +0100812 :param class_channel channel: The manipulated Channel.
Pieter Baauw4d7f7662015-11-08 16:38:08 +0100813 :returns: a string containing all the available data or nil.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100814
Thierry FOURNIER486f5a02015-03-16 15:13:03 +0100815.. js:function:: Channel.get(channel)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100816
817 This function returns a string that contain the entire buffer. The data is
818 consumed from the buffer.
819
820 If the buffer cant receive more data, a 'nil' value is returned.
821
Thierry FOURNIER486f5a02015-03-16 15:13:03 +0100822 :param class_channel channel: The manipulated Channel.
Pieter Baauw4d7f7662015-11-08 16:38:08 +0100823 :returns: a string containing all the available data or nil.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100824
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +0200825.. js:function:: Channel.getline(channel)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100826
827 This function returns a string that contain the first line of the buffer. The
828 data is consumed. If the data returned doesn't contains a final '\n' its
829 assumed than its the last available data in the buffer.
830
831 If the buffer cant receive more data, a 'nil' value is returned.
832
Thierry FOURNIER486f5a02015-03-16 15:13:03 +0100833 :param class_channel channel: The manipulated Channel.
Pieter Baauw386a1272015-08-16 15:26:24 +0200834 :returns: a string containing the available line or nil.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100835
Thierry FOURNIER486f5a02015-03-16 15:13:03 +0100836.. js:function:: Channel.set(channel, string)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100837
838 This function replace the content of the buffer by the string. The function
839 returns the copied length, otherwise, it returns -1.
840
841 The data set with this function are not send. They wait for the end of
842 HAProxy processing, so the buffer can be full.
843
Thierry FOURNIER486f5a02015-03-16 15:13:03 +0100844 :param class_channel channel: The manipulated Channel.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100845 :param string string: The data which will sent.
Pieter Baauw4d7f7662015-11-08 16:38:08 +0100846 :returns: an integer containing the amount of bytes copied or -1.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100847
Thierry FOURNIER486f5a02015-03-16 15:13:03 +0100848.. js:function:: Channel.append(channel, string)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100849
850 This function append the string argument to the content of the buffer. The
851 function returns the copied length, otherwise, it returns -1.
852
853 The data set with this function are not send. They wait for the end of
854 HAProxy processing, so the buffer can be full.
855
Thierry FOURNIER486f5a02015-03-16 15:13:03 +0100856 :param class_channel channel: The manipulated Channel.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100857 :param string string: The data which will sent.
Pieter Baauw4d7f7662015-11-08 16:38:08 +0100858 :returns: an integer containing the amount of bytes copied or -1.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100859
Thierry FOURNIER486f5a02015-03-16 15:13:03 +0100860.. js:function:: Channel.send(channel, string)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100861
862 This function required immediate send of the data. Unless if the connection
863 is close, the buffer is regularly flushed and all the string can be sent.
864
Thierry FOURNIER486f5a02015-03-16 15:13:03 +0100865 :param class_channel channel: The manipulated Channel.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100866 :param string string: The data which will sent.
Pieter Baauw4d7f7662015-11-08 16:38:08 +0100867 :returns: an integer containing the amount of bytes copied or -1.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100868
Thierry FOURNIER486f5a02015-03-16 15:13:03 +0100869.. js:function:: Channel.get_in_length(channel)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100870
871 This function returns the length of the input part of the buffer.
872
Thierry FOURNIER486f5a02015-03-16 15:13:03 +0100873 :param class_channel channel: The manipulated Channel.
Pieter Baauw4d7f7662015-11-08 16:38:08 +0100874 :returns: an integer containing the amount of available bytes.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100875
Thierry FOURNIER486f5a02015-03-16 15:13:03 +0100876.. js:function:: Channel.get_out_length(channel)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100877
878 This function returns the length of the output part of the buffer.
879
Thierry FOURNIER486f5a02015-03-16 15:13:03 +0100880 :param class_channel channel: The manipulated Channel.
Pieter Baauw4d7f7662015-11-08 16:38:08 +0100881 :returns: an integer containing the amount of available bytes.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100882
Thierry FOURNIER486f5a02015-03-16 15:13:03 +0100883.. js:function:: Channel.forward(channel, int)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100884
885 This function transfer bytes from the input part of the buffer to the output
886 part.
887
Thierry FOURNIER486f5a02015-03-16 15:13:03 +0100888 :param class_channel channel: The manipulated Channel.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100889 :param integer int: The amount of data which will be forwarded.
890
Thierry FOURNIERdc595002015-12-21 11:13:52 +0100891
892.. _http_class:
Thierry FOURNIER08504f42015-03-16 14:17:08 +0100893
894HTTP class
895==========
896
897.. js:class:: HTTP
898
899 This class contain all the HTTP manipulation functions.
900
Pieter Baauw386a1272015-08-16 15:26:24 +0200901.. js:function:: HTTP.req_get_headers(http)
Thierry FOURNIER08504f42015-03-16 14:17:08 +0100902
903 Returns an array containing all the request headers.
904
905 :param class_http http: The related http object.
906 :returns: array of headers.
Pieter Baauw386a1272015-08-16 15:26:24 +0200907 :see: HTTP.res_get_headers()
Thierry FOURNIER08504f42015-03-16 14:17:08 +0100908
Thierry FOURNIERdc595002015-12-21 11:13:52 +0100909 This is the form of the returned array:
910
911.. code-block:: lua
912
913 HTTP:req_get_headers()['<header-name>'][<header-index>] = "<header-value>"
914
915 local hdr = HTTP:req_get_headers()
916 hdr["host"][0] = "www.test.com"
917 hdr["accept"][0] = "audio/basic q=1"
918 hdr["accept"][1] = "audio/*, q=0.2"
919 hdr["accept"][2] = "*/*, q=0.1"
920..
921
Pieter Baauw386a1272015-08-16 15:26:24 +0200922.. js:function:: HTTP.res_get_headers(http)
Thierry FOURNIER08504f42015-03-16 14:17:08 +0100923
924 Returns an array containing all the response headers.
925
926 :param class_http http: The related http object.
927 :returns: array of headers.
Pieter Baauw386a1272015-08-16 15:26:24 +0200928 :see: HTTP.req_get_headers()
Thierry FOURNIER08504f42015-03-16 14:17:08 +0100929
Thierry FOURNIERdc595002015-12-21 11:13:52 +0100930 This is the form of the returned array:
931
932.. code-block:: lua
933
934 HTTP:res_get_headers()['<header-name>'][<header-index>] = "<header-value>"
935
936 local hdr = HTTP:req_get_headers()
937 hdr["host"][0] = "www.test.com"
938 hdr["accept"][0] = "audio/basic q=1"
939 hdr["accept"][1] = "audio/*, q=0.2"
940 hdr["accept"][2] = "*.*, q=0.1"
941..
942
Thierry FOURNIER486f5a02015-03-16 15:13:03 +0100943.. js:function:: HTTP.req_add_header(http, name, value)
Thierry FOURNIER08504f42015-03-16 14:17:08 +0100944
945 Appends an HTTP header field in the request whose name is
946 specified in "name" and whose value is defined in "value".
947
948 :param class_http http: The related http object.
949 :param string name: The header name.
950 :param string value: The header value.
Thierry FOURNIER486f5a02015-03-16 15:13:03 +0100951 :see: HTTP.res_add_header()
Thierry FOURNIER08504f42015-03-16 14:17:08 +0100952
Thierry FOURNIER486f5a02015-03-16 15:13:03 +0100953.. js:function:: HTTP.res_add_header(http, name, value)
Thierry FOURNIER08504f42015-03-16 14:17:08 +0100954
955 appends an HTTP header field in the response whose name is
956 specified in "name" and whose value is defined in "value".
957
958 :param class_http http: The related http object.
959 :param string name: The header name.
960 :param string value: The header value.
Thierry FOURNIER486f5a02015-03-16 15:13:03 +0100961 :see: HTTP.req_add_header()
Thierry FOURNIER08504f42015-03-16 14:17:08 +0100962
Thierry FOURNIER486f5a02015-03-16 15:13:03 +0100963.. js:function:: HTTP.req_del_header(http, name)
Thierry FOURNIER08504f42015-03-16 14:17:08 +0100964
965 Removes all HTTP header fields in the request whose name is
966 specified in "name".
967
968 :param class_http http: The related http object.
969 :param string name: The header name.
Thierry FOURNIER486f5a02015-03-16 15:13:03 +0100970 :see: HTTP.res_del_header()
Thierry FOURNIER08504f42015-03-16 14:17:08 +0100971
Thierry FOURNIER486f5a02015-03-16 15:13:03 +0100972.. js:function:: HTTP.res_del_header(http, name)
Thierry FOURNIER08504f42015-03-16 14:17:08 +0100973
974 Removes all HTTP header fields in the response whose name is
975 specified in "name".
976
977 :param class_http http: The related http object.
978 :param string name: The header name.
Thierry FOURNIER486f5a02015-03-16 15:13:03 +0100979 :see: HTTP.req_del_header()
Thierry FOURNIER08504f42015-03-16 14:17:08 +0100980
Thierry FOURNIER486f5a02015-03-16 15:13:03 +0100981.. js:function:: HTTP.req_set_header(http, name, value)
Thierry FOURNIER08504f42015-03-16 14:17:08 +0100982
983 This variable replace all occurence of all header "name", by only
984 one containing the "value".
985
986 :param class_http http: The related http object.
987 :param string name: The header name.
988 :param string value: The header value.
Thierry FOURNIER486f5a02015-03-16 15:13:03 +0100989 :see: HTTP.res_set_header()
Thierry FOURNIER08504f42015-03-16 14:17:08 +0100990
991 This function does the same work as the folowwing code:
992
993.. code-block:: lua
994
995 function fcn(txn)
Thierry FOURNIER486f5a02015-03-16 15:13:03 +0100996 TXN.http:req_del_header("header")
997 TXN.http:req_add_header("header", "value")
Thierry FOURNIER08504f42015-03-16 14:17:08 +0100998 end
999..
1000
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001001.. js:function:: HTTP.res_set_header(http, name, value)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001002
1003 This variable replace all occurence of all header "name", by only
1004 one containing the "value".
1005
1006 :param class_http http: The related http object.
1007 :param string name: The header name.
1008 :param string value: The header value.
Pieter Baauw386a1272015-08-16 15:26:24 +02001009 :see: HTTP.req_rep_header()
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001010
Pieter Baauw386a1272015-08-16 15:26:24 +02001011.. js:function:: HTTP.req_rep_header(http, name, regex, replace)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001012
1013 Matches the regular expression in all occurrences of header field "name"
1014 according to "regex", and replaces them with the "replace" argument. The
1015 replacement value can contain back references like \1, \2, ... This
1016 function works with the request.
1017
1018 :param class_http http: The related http object.
1019 :param string name: The header name.
1020 :param string regex: The match regular expression.
1021 :param string replace: The replacement value.
Pieter Baauw386a1272015-08-16 15:26:24 +02001022 :see: HTTP.res_rep_header()
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001023
Pieter Baauw386a1272015-08-16 15:26:24 +02001024.. js:function:: HTTP.res_rep_header(http, name, regex, string)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001025
1026 Matches the regular expression in all occurrences of header field "name"
1027 according to "regex", and replaces them with the "replace" argument. The
1028 replacement value can contain back references like \1, \2, ... This
1029 function works with the request.
1030
1031 :param class_http http: The related http object.
1032 :param string name: The header name.
1033 :param string regex: The match regular expression.
1034 :param string replace: The replacement value.
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001035 :see: HTTP.req_replace_header()
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001036
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001037.. js:function:: HTTP.req_replace_value(http, name, regex, replace)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001038
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001039 Works like "HTTP.req_replace_header()" except that it matches the regex
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001040 against every comma-delimited value of the header field "name" instead of the
1041 entire header.
1042
1043 :param class_http http: The related http object.
1044 :param string name: The header name.
1045 :param string regex: The match regular expression.
1046 :param string replace: The replacement value.
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001047 :see: HTTP.req_replace_header()
1048 :see: HTTP.res_replace_value()
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001049
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001050.. js:function:: HTTP.res_replace_value(http, name, regex, replace)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001051
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001052 Works like "HTTP.res_replace_header()" except that it matches the regex
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001053 against every comma-delimited value of the header field "name" instead of the
1054 entire header.
1055
1056 :param class_http http: The related http object.
1057 :param string name: The header name.
1058 :param string regex: The match regular expression.
1059 :param string replace: The replacement value.
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001060 :see: HTTP.res_replace_header()
1061 :see: HTTP.req_replace_value()
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001062
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001063.. js:function:: HTTP.req_set_method(http, method)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001064
1065 Rewrites the request method with the parameter "method".
1066
1067 :param class_http http: The related http object.
1068 :param string method: The new method.
1069
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001070.. js:function:: HTTP.req_set_path(http, path)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001071
1072 Rewrites the request path with the "path" parameter.
1073
1074 :param class_http http: The related http object.
1075 :param string path: The new path.
1076
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001077.. js:function:: HTTP.req_set_query(http, query)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001078
1079 Rewrites the request's query string which appears after the first question
1080 mark ("?") with the parameter "query".
1081
1082 :param class_http http: The related http object.
1083 :param string query: The new query.
1084
Thierry FOURNIER0d79cf62015-08-26 14:20:58 +02001085.. js:function:: HTTP.req_set_uri(http, uri)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001086
1087 Rewrites the request URI with the parameter "uri".
1088
1089 :param class_http http: The related http object.
1090 :param string uri: The new uri.
1091
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +02001092.. js:function:: HTTP.res_set_status(http, status)
1093
1094 Rewrites the response status code with the parameter "code". Note that the
1095 reason is automatically adapted to the new code.
1096
1097 :param class_http http: The related http object.
1098 :param integer status: The new response status code.
1099
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001100.. _txn_class:
1101
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001102TXN class
1103=========
1104
1105.. js:class:: TXN
1106
1107 The txn class contain all the functions relative to the http or tcp
1108 transaction (Note than a tcp stream is the same than a tcp transaction, but
1109 an HTTP transaction is not the same than a tcp stream).
1110
1111 The usage of this class permits to retrieve data from the requests, alter it
1112 and forward it.
1113
1114 All the functions provided by this class are available in the context
1115 **sample-fetches** and **actions**.
1116
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001117.. js:attribute:: TXN.c
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001118
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001119 :returns: An :ref:`converters_class`.
1120
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001121 This attribute contains a Converters class object.
1122
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001123.. js:attribute:: TXN.sc
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001124
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001125 :returns: An :ref:`converters_class`.
1126
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001127 This attribute contains a Converters class object. The functions of
1128 this object returns always a string.
1129
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001130.. js:attribute:: TXN.f
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001131
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001132 :returns: An :ref:`fetches_class`.
1133
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001134 This attribute contains a Fetches class object.
1135
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001136.. js:attribute:: TXN.sf
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001137
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001138 :returns: An :ref:`fetches_class`.
1139
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001140 This attribute contains a Fetches class object. The functions of
1141 this object returns always a string.
1142
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001143.. js:attribute:: TXN.req
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001144
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001145 :returns: An :ref:`channel_class`.
1146
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001147 This attribute contains a channel class object for the request buffer.
1148
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001149.. js:attribute:: TXN.res
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001150
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001151 :returns: An :ref:`channel_class`.
1152
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001153 This attribute contains a channel class object for the response buffer.
1154
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001155.. js:attribute:: TXN.http
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001156
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001157 :returns: An :ref:`http_class`.
1158
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001159 This attribute contains an HTTP class object. It is avalaible only if the
1160 proxy has the "mode http" enabled.
1161
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01001162.. js:function:: TXN.log(TXN, loglevel, msg)
1163
1164 This function sends a log. The log is sent, according with the HAProxy
1165 configuration file, on the default syslog server if it is configured and on
1166 the stderr if it is allowed.
1167
1168 :param class_txn txn: The class txn object containing the data.
1169 :param integer loglevel: Is the log level asociated with the message. It is a
1170 number between 0 and 7.
1171 :param string msg: The log content.
1172 :see: core.emerg, core.alert, core.crit, core.err, core.warning, core.notice,
1173 core.info, core.debug (log level definitions)
1174 :see: TXN.deflog
1175 :see: TXN.Debug
1176 :see: TXN.Info
1177 :see: TXN.Warning
1178 :see: TXN.Alert
1179
1180.. js:function:: TXN.deflog(TXN, msg)
1181
1182 Sends a log line with the default loglevel for the proxy ssociated with the
1183 transaction.
1184
1185 :param class_txn txn: The class txn object containing the data.
1186 :param string msg: The log content.
1187 :see: TXN.log
1188
1189.. js:function:: TXN.Debug(txn, msg)
1190
1191 :param class_txn txn: The class txn object containing the data.
1192 :param string msg: The log content.
1193 :see: TXN.log
1194
1195 Does the same job than:
1196
1197.. code-block:: lua
1198
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001199 function Debug(txn, msg)
1200 TXN.log(txn, core.debug, msg)
1201 end
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01001202..
1203
1204.. js:function:: TXN.Info(txn, msg)
1205
1206 :param class_txn txn: The class txn object containing the data.
1207 :param string msg: The log content.
1208 :see: TXN.log
1209
1210.. code-block:: lua
1211
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001212 function Debug(txn, msg)
1213 TXN.log(txn, core.info, msg)
1214 end
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01001215..
1216
1217.. js:function:: TXN.Warning(txn, msg)
1218
1219 :param class_txn txn: The class txn object containing the data.
1220 :param string msg: The log content.
1221 :see: TXN.log
1222
1223.. code-block:: lua
1224
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001225 function Debug(txn, msg)
1226 TXN.log(txn, core.warning, msg)
1227 end
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01001228..
1229
1230.. js:function:: TXN.Alert(txn, msg)
1231
1232 :param class_txn txn: The class txn object containing the data.
1233 :param string msg: The log content.
1234 :see: TXN.log
1235
1236.. code-block:: lua
1237
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001238 function Debug(txn, msg)
1239 TXN.log(txn, core.alert, msg)
1240 end
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01001241..
1242
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001243.. js:function:: TXN.get_priv(txn)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001244
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001245 Return Lua data stored in the current transaction (with the `TXN.set_priv()`)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001246 function. If no data are stored, it returns a nil value.
1247
1248 :param class_txn txn: The class txn object containing the data.
1249 :returns: the opaque data previsously stored, or nil if nothing is
1250 avalaible.
1251
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001252.. js:function:: TXN.set_priv(txn, data)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001253
1254 Store any data in the current HAProxy transaction. This action replace the
1255 old stored data.
1256
1257 :param class_txn txn: The class txn object containing the data.
1258 :param opaque data: The data which is stored in the transaction.
1259
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +02001260.. js:function:: TXN.set_var(TXN, var, value)
1261
David Carlier61fdf8b2015-10-02 11:59:38 +01001262 Converts a Lua type in a HAProxy type and store it in a variable <var>.
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +02001263
1264 :param class_txn txn: The class txn object containing the data.
1265 :param string var: The variable name according with the HAProxy variable syntax.
1266 :param opaque value: The data which is stored in the variable.
1267
1268.. js:function:: TXN.get_var(TXN, var)
1269
1270 Returns data stored in the variable <var> converter in Lua type.
1271
1272 :param class_txn txn: The class txn object containing the data.
1273 :param string var: The variable name according with the HAProxy variable syntax.
1274
Willy Tarreaubc183a62015-08-28 10:39:11 +02001275.. js:function:: TXN.done(txn)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001276
Willy Tarreaubc183a62015-08-28 10:39:11 +02001277 This function terminates processing of the transaction and the associated
1278 session. It can be used when a critical error is detected or to terminate
1279 processing after some data have been returned to the client (eg: a redirect).
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001280
1281 :param class_txn txn: The class txn object containing the data.
1282
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001283.. js:function:: TXN.set_loglevel(txn, loglevel)
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01001284
1285 Is used to change the log level of the current request. The "loglevel" must
1286 be an integer between 0 and 7.
1287
1288 :param class_txn txn: The class txn object containing the data.
1289 :param integer loglevel: The required log level. This variable can be one of
1290 :see: core.<loglevel>
1291
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001292.. js:function:: TXN.set_tos(txn, tos)
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01001293
1294 Is used to set the TOS or DSCP field value of packets sent to the client to
1295 the value passed in "tos" on platforms which support this.
1296
1297 :param class_txn txn: The class txn object containing the data.
1298 :param integer tos: The new TOS os DSCP.
1299
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001300.. js:function:: TXN.set_mark(txn, mark)
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01001301
1302 Is used to set the Netfilter MARK on all packets sent to the client to the
1303 value passed in "mark" on platforms which support it.
1304
1305 :param class_txn txn: The class txn object containing the data.
1306 :param integer mark: The mark value.
1307
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001308.. _socket_class:
1309
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001310Socket class
1311============
1312
1313.. js:class:: Socket
1314
1315 This class must be compatible with the Lua Socket class. Only the 'client'
1316 functions are available. See the Lua Socket documentation:
1317
1318 `http://w3.impa.br/~diego/software/luasocket/tcp.html
1319 <http://w3.impa.br/~diego/software/luasocket/tcp.html>`_
1320
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001321.. js:function:: Socket.close(socket)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001322
1323 Closes a TCP object. The internal socket used by the object is closed and the
1324 local address to which the object was bound is made available to other
1325 applications. No further operations (except for further calls to the close
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001326 method) are allowed on a closed Socket.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001327
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001328 :param class_socket socket: Is the manipulated Socket.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001329
1330 Note: It is important to close all used sockets once they are not needed,
1331 since, in many systems, each socket uses a file descriptor, which are limited
1332 system resources. Garbage-collected objects are automatically closed before
1333 destruction, though.
1334
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02001335.. js:function:: Socket.connect(socket, address[, port])
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001336
1337 Attempts to connect a socket object to a remote host.
1338
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001339
1340 In case of error, the method returns nil followed by a string describing the
1341 error. In case of success, the method returns 1.
1342
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001343 :param class_socket socket: Is the manipulated Socket.
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02001344 :param string address: can be an IP address or a host name. See below for more
1345 information.
1346 :param integer port: must be an integer number in the range [1..64K].
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001347 :returns: 1 or nil.
1348
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02001349 an address field extension permits to use the connect() function to connect to
1350 other stream than TCP. The syntax containing a simpleipv4 or ipv6 address is
1351 the basically expected format. This format requires the port.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001352
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02001353 Other format accepted are a socket path like "/socket/path", it permits to
1354 connect to a socket. abstract namespaces are supported with the prefix
1355 "abns@", and finaly a filedescriotr can be passed with the prefix "fd@".
1356 The prefix "ipv4@", "ipv6@" and "unix@" are also supported. The port can be
1357 passed int the string. The syntax "127.0.0.1:1234" is valid. in this case, the
1358 parameter *port* is ignored.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001359
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001360.. js:function:: Socket.connect_ssl(socket, address, port)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001361
1362 Same behavior than the function socket:connect, but uses SSL.
1363
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001364 :param class_socket socket: Is the manipulated Socket.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001365 :returns: 1 or nil.
1366
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001367.. js:function:: Socket.getpeername(socket)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001368
1369 Returns information about the remote side of a connected client object.
1370
1371 Returns a string with the IP address of the peer, followed by the port number
1372 that peer is using for the connection. In case of error, the method returns
1373 nil.
1374
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001375 :param class_socket socket: Is the manipulated Socket.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001376 :returns: a string containing the server information.
1377
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001378.. js:function:: Socket.getsockname(socket)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001379
1380 Returns the local address information associated to the object.
1381
1382 The method returns a string with local IP address and a number with the port.
1383 In case of error, the method returns nil.
1384
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001385 :param class_socket socket: Is the manipulated Socket.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001386 :returns: a string containing the client information.
1387
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001388.. js:function:: Socket.receive(socket, [pattern [, prefix]])
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001389
1390 Reads data from a client object, according to the specified read pattern.
1391 Patterns follow the Lua file I/O format, and the difference in performance
1392 between all patterns is negligible.
1393
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001394 :param class_socket socket: Is the manipulated Socket.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001395 :param string|integer pattern: Describe what is required (see below).
1396 :param string prefix: A string which will be prefix the returned data.
1397 :returns: a string containing the required data or nil.
1398
1399 Pattern can be any of the following:
1400
1401 * **`*a`**: reads from the socket until the connection is closed. No
1402 end-of-line translation is performed;
1403
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001404 * **`*l`**: reads a line of text from the Socket. The line is terminated by a
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001405 LF character (ASCII 10), optionally preceded by a CR character
1406 (ASCII 13). The CR and LF characters are not included in the
1407 returned line. In fact, all CR characters are ignored by the
1408 pattern. This is the default pattern.
1409
1410 * **number**: causes the method to read a specified number of bytes from the
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001411 Socket. Prefix is an optional string to be concatenated to the
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001412 beginning of any received data before return.
1413
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02001414 * **empty**: If the pattern is left empty, the default option is `*l`.
1415
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001416 If successful, the method returns the received pattern. In case of error, the
1417 method returns nil followed by an error message which can be the string
1418 'closed' in case the connection was closed before the transmission was
1419 completed or the string 'timeout' in case there was a timeout during the
1420 operation. Also, after the error message, the function returns the partial
1421 result of the transmission.
1422
1423 Important note: This function was changed severely. It used to support
1424 multiple patterns (but I have never seen this feature used) and now it
1425 doesn't anymore. Partial results used to be returned in the same way as
1426 successful results. This last feature violated the idea that all functions
1427 should return nil on error. Thus it was changed too.
1428
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001429.. js:function:: Socket.send(socket, data [, start [, end ]])
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001430
1431 Sends data through client object.
1432
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001433 :param class_socket socket: Is the manipulated Socket.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001434 :param string data: The data that will be sent.
1435 :param integer start: The start position in the buffer of the data which will
1436 be sent.
1437 :param integer end: The end position in the buffer of the data which will
1438 be sent.
1439 :returns: see below.
1440
1441 Data is the string to be sent. The optional arguments i and j work exactly
1442 like the standard string.sub Lua function to allow the selection of a
1443 substring to be sent.
1444
1445 If successful, the method returns the index of the last byte within [start,
1446 end] that has been sent. Notice that, if start is 1 or absent, this is
1447 effectively the total number of bytes sent. In case of error, the method
1448 returns nil, followed by an error message, followed by the index of the last
1449 byte within [start, end] that has been sent. You might want to try again from
1450 the byte following that. The error message can be 'closed' in case the
1451 connection was closed before the transmission was completed or the string
1452 'timeout' in case there was a timeout during the operation.
1453
1454 Note: Output is not buffered. For small strings, it is always better to
1455 concatenate them in Lua (with the '..' operator) and send the result in one
1456 call instead of calling the method several times.
1457
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001458.. js:function:: Socket.setoption(socket, option [, value])
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001459
1460 Just implemented for compatibility, this cal does nothing.
1461
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001462.. js:function:: Socket.settimeout(socket, value [, mode])
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001463
1464 Changes the timeout values for the object. All I/O operations are blocking.
1465 That is, any call to the methods send, receive, and accept will block
1466 indefinitely, until the operation completes. The settimeout method defines a
1467 limit on the amount of time the I/O methods can block. When a timeout time
1468 has elapsed, the affected methods give up and fail with an error code.
1469
1470 The amount of time to wait is specified as the value parameter, in seconds.
1471
1472 The timeout modes are bot implemented, the only settable timeout is the
1473 inactivity time waiting for complete the internal buffer send or waiting for
1474 receive data.
1475
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001476 :param class_socket socket: Is the manipulated Socket.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001477 :param integer value: The timeout value.
1478
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001479.. _map_class:
1480
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001481Map class
1482=========
1483
1484.. js:class:: Map
1485
1486 This class permits to do some lookup in HAProxy maps. The declared maps can
1487 be modified during the runtime throught the HAProxy management socket.
1488
1489.. code-block:: lua
1490
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001491 default = "usa"
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001492
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001493 -- Create and load map
1494 geo = Map.new("geo.map", Map.ip);
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001495
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001496 -- Create new fetch that returns the user country
1497 core.register_fetches("country", function(txn)
1498 local src;
1499 local loc;
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001500
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001501 src = txn.f:fhdr("x-forwarded-for");
1502 if (src == nil) then
1503 src = txn.f:src()
1504 if (src == nil) then
1505 return default;
1506 end
1507 end
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001508
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001509 -- Perform lookup
1510 loc = geo:lookup(src);
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001511
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001512 if (loc == nil) then
1513 return default;
1514 end
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001515
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001516 return loc;
1517 end);
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001518
1519.. js:attribute:: Map.int
1520
1521 See the HAProxy configuration.txt file, chapter "Using ACLs and fetching
1522 samples" ans subchapter "ACL basics" to understand this pattern matching
1523 method.
1524
1525.. js:attribute:: Map.ip
1526
1527 See the HAProxy configuration.txt file, chapter "Using ACLs and fetching
1528 samples" ans subchapter "ACL basics" to understand this pattern matching
1529 method.
1530
1531.. js:attribute:: Map.str
1532
1533 See the HAProxy configuration.txt file, chapter "Using ACLs and fetching
1534 samples" ans subchapter "ACL basics" to understand this pattern matching
1535 method.
1536
1537.. js:attribute:: Map.beg
1538
1539 See the HAProxy configuration.txt file, chapter "Using ACLs and fetching
1540 samples" ans subchapter "ACL basics" to understand this pattern matching
1541 method.
1542
1543.. js:attribute:: Map.sub
1544
1545 See the HAProxy configuration.txt file, chapter "Using ACLs and fetching
1546 samples" ans subchapter "ACL basics" to understand this pattern matching
1547 method.
1548
1549.. js:attribute:: Map.dir
1550
1551 See the HAProxy configuration.txt file, chapter "Using ACLs and fetching
1552 samples" ans subchapter "ACL basics" to understand this pattern matching
1553 method.
1554
1555.. js:attribute:: Map.dom
1556
1557 See the HAProxy configuration.txt file, chapter "Using ACLs and fetching
1558 samples" ans subchapter "ACL basics" to understand this pattern matching
1559 method.
1560
1561.. js:attribute:: Map.end
1562
1563 See the HAProxy configuration.txt file, chapter "Using ACLs and fetching
1564 samples" ans subchapter "ACL basics" to understand this pattern matching
1565 method.
1566
1567.. js:attribute:: Map.reg
1568
1569 See the HAProxy configuration.txt file, chapter "Using ACLs and fetching
1570 samples" ans subchapter "ACL basics" to understand this pattern matching
1571 method.
1572
1573
1574.. js:function:: Map.new(file, method)
1575
1576 Creates and load a map.
1577
1578 :param string file: Is the file containing the map.
1579 :param integer method: Is the map pattern matching method. See the attributes
1580 of the Map class.
1581 :returns: a class Map object.
1582 :see: The Map attributes.
1583
1584.. js:function:: Map.lookup(map, str)
1585
1586 Perform a lookup in a map.
1587
1588 :param class_map map: Is the class Map object.
1589 :param string str: Is the string used as key.
1590 :returns: a string containing the result or nil if no match.
1591
1592.. js:function:: Map.slookup(map, str)
1593
1594 Perform a lookup in a map.
1595
1596 :param class_map map: Is the class Map object.
1597 :param string str: Is the string used as key.
1598 :returns: a string containing the result or empty string if no match.
1599
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001600.. _applethttp_class:
1601
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02001602AppletHTTP class
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001603================
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02001604
1605.. js:class:: AppletHTTP
1606
1607 This class is used with applets that requires the 'http' mode. The http applet
1608 can be registered with the *core.register_service()* function. They are used
1609 for processing an http request like a server in back of HAProxy.
1610
1611 This is an hello world sample code:
1612
1613.. code-block:: lua
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001614
Pieter Baauw4d7f7662015-11-08 16:38:08 +01001615 core.register_service("hello-world", "http", function(applet)
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02001616 local response = "Hello World !"
1617 applet:set_status(200)
Pieter Baauw2dcb9bc2015-10-01 22:47:12 +02001618 applet:add_header("content-length", string.len(response))
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02001619 applet:add_header("content-type", "text/plain")
Pieter Baauw2dcb9bc2015-10-01 22:47:12 +02001620 applet:start_response()
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02001621 applet:send(response)
1622 end)
1623
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001624.. js:attribute:: AppletHTTP.c
1625
1626 :returns: A :ref:`converters_class`
1627
1628 This attribute contains a Converters class object.
1629
1630.. js:attribute:: AppletHTTP.sc
1631
1632 :returns: A :ref:`converters_class`
1633
1634 This attribute contains a Converters class object. The
1635 functions of this object returns always a string.
1636
1637.. js:attribute:: AppletHTTP.f
1638
1639 :returns: A :ref:`fetches_class`
1640
1641 This attribute contains a Fetches class object. Note that the
1642 applet execution place cannot access to a valid HAProxy core HTTP
1643 transaction, so some sample fecthes related to the HTTP dependant
1644 values (hdr, path, ...) are not available.
1645
1646.. js:attribute:: AppletHTTP.sf
1647
1648 :returns: A :ref:`fetches_class`
1649
1650 This attribute contains a Fetches class object. The functions of
1651 this object returns always a string. Note that the applet
1652 execution place cannot access to a valid HAProxy core HTTP
1653 transaction, so some sample fecthes related to the HTTP dependant
1654 values (hdr, path, ...) are not available.
1655
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01001656.. js:attribute:: AppletHTTP.method
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001657
1658 :returns: string
1659
1660 The attribute method returns a string containing the HTTP
1661 method.
1662
1663.. js:attribute:: AppletHTTP.version
1664
1665 :returns: string
1666
1667 The attribute version, returns a string containing the HTTP
1668 request version.
1669
1670.. js:attribute:: AppletHTTP.path
1671
1672 :returns: string
1673
1674 The attribute path returns a string containing the HTTP
1675 request path.
1676
1677.. js:attribute:: AppletHTTP.qs
1678
1679 :returns: string
1680
1681 The attribute qs returns a string containing the HTTP
1682 request query string.
1683
1684.. js:attribute:: AppletHTTP.length
1685
1686 :returns: integer
1687
1688 The attribute length returns an integer containing the HTTP
1689 body length.
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02001690
Thierry FOURNIER841475e2015-12-11 17:10:09 +01001691.. js:attribute:: AppletHTTP.headers
1692
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001693 :returns: array
1694
1695 The attribute headers returns an array containing the HTTP
1696 headers. The header names are always in lower case. As the header name can be
1697 encountered more than once in each request, the value is indexed with 0 as
1698 first index value. The array have this form:
1699
1700.. code-block:: lua
1701
1702 AppletHTTP.headers['<header-name>'][<header-index>] = "<header-value>"
1703
1704 AppletHTTP.headers["host"][0] = "www.test.com"
1705 AppletHTTP.headers["accept"][0] = "audio/basic q=1"
1706 AppletHTTP.headers["accept"][1] = "audio/*, q=0.2"
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01001707 AppletHTTP.headers["accept"][2] = "*/*, q=0.1"
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001708..
1709
1710.. js:attribute:: AppletHTTP.headers
1711
Thierry FOURNIER841475e2015-12-11 17:10:09 +01001712 Contains an array containing all the request headers.
1713
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01001714.. js:function:: AppletHTTP.set_status(applet, code)
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02001715
1716 This function sets the HTTP status code for the response. The allowed code are
1717 from 100 to 599.
1718
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01001719 :param class_AppletHTTP applet: An :ref:`applethttp_class`
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02001720 :param integer code: the status code returned to the client.
1721
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01001722.. js:function:: AppletHTTP.add_header(applet, name, value)
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02001723
1724 This function add an header in the response. Duplicated headers are not
1725 collapsed. The special header *content-length* is used to determinate the
1726 response length. If it not exists, a *transfer-encoding: chunked* is set, and
1727 all the write from the funcion *AppletHTTP:send()* become a chunk.
1728
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01001729 :param class_AppletHTTP applet: An :ref:`applethttp_class`
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02001730 :param string name: the header name
1731 :param string value: the header value
1732
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01001733.. js:function:: AppletHTTP.start_response(applet)
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02001734
1735 This function indicates to the HTTP engine that it can process and send the
1736 response headers. After this called we cannot add headers to the response; We
1737 cannot use the *AppletHTTP:send()* function if the
1738 *AppletHTTP:start_response()* is not called.
1739
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01001740 :param class_AppletHTTP applet: An :ref:`applethttp_class`
1741
1742.. js:function:: AppletHTTP.getline(applet)
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02001743
1744 This function returns a string containing one line from the http body. If the
1745 data returned doesn't contains a final '\\n' its assumed than its the last
1746 available data before the end of stream.
1747
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01001748 :param class_AppletHTTP applet: An :ref:`applethttp_class`
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02001749 :returns: a string. The string can be empty if we reach the end of the stream.
1750
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01001751.. js:function:: AppletHTTP.receive(applet, [size])
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02001752
1753 Reads data from the HTTP body, according to the specified read *size*. If the
1754 *size* is missing, the function tries to read all the content of the stream
1755 until the end. If the *size* is bigger than the http body, it returns the
1756 amount of data avalaible.
1757
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01001758 :param class_AppletHTTP applet: An :ref:`applethttp_class`
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02001759 :param integer size: the required read size.
1760 :returns: always return a string,the string can be empty is the connexion is
1761 closed.
1762
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01001763.. js:function:: AppletHTTP.send(applet, msg)
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02001764
1765 Send the message *msg* on the http request body.
1766
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01001767 :param class_AppletHTTP applet: An :ref:`applethttp_class`
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02001768 :param string msg: the message to send.
1769
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01001770.. js:function:: AppletHTTP.get_priv(applet)
1771
1772 Return Lua data stored in the current transaction (with the
1773 `AppletHTTP.set_priv()`) function. If no data are stored, it returns a nil
1774 value.
1775
1776 :param class_AppletHTTP applet: An :ref:`applethttp_class`
1777 :returns: the opaque data previsously stored, or nil if nothing is
1778 avalaible.
1779
1780.. js:function:: AppletHTTP.set_priv(applet, data)
1781
1782 Store any data in the current HAProxy transaction. This action replace the
1783 old stored data.
1784
1785 :param class_AppletHTTP applet: An :ref:`applethttp_class`
1786 :param opaque data: The data which is stored in the transaction.
1787
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001788.. _applettcp_class:
1789
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02001790AppletTCP class
1791===============
1792
1793.. js:class:: AppletTCP
1794
1795 This class is used with applets that requires the 'tcp' mode. The tcp applet
1796 can be registered with the *core.register_service()* function. They are used
1797 for processing a tcp stream like a server in back of HAProxy.
1798
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001799.. js:attribute:: AppletTCP.c
1800
1801 :returns: A :ref:`converters_class`
1802
1803 This attribute contains a Converters class object.
1804
1805.. js:attribute:: AppletTCP.sc
1806
1807 :returns: A :ref:`converters_class`
1808
1809 This attribute contains a Converters class object. The
1810 functions of this object returns always a string.
1811
1812.. js:attribute:: AppletTCP.f
1813
1814 :returns: A :ref:`fetches_class`
1815
1816 This attribute contains a Fetches class object.
1817
1818.. js:attribute:: AppletTCP.sf
1819
1820 :returns: A :ref:`fetches_class`
1821
1822 This attribute contains a Fetches class object.
1823
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01001824.. js:function:: AppletTCP.getline(applet)
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02001825
1826 This function returns a string containing one line from the stream. If the
1827 data returned doesn't contains a final '\\n' its assumed than its the last
1828 available data before the end of stream.
1829
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01001830 :param class_AppletTCP applet: An :ref:`applettcp_class`
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02001831 :returns: a string. The string can be empty if we reach the end of the stream.
1832
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01001833.. js:function:: AppletTCP.receive(applet, [size])
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02001834
1835 Reads data from the TCP stream, according to the specified read *size*. If the
1836 *size* is missing, the function tries to read all the content of the stream
1837 until the end.
1838
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01001839 :param class_AppletTCP applet: An :ref:`applettcp_class`
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02001840 :param integer size: the required read size.
1841 :returns: always return a string,the string can be empty is the connexion is
1842 closed.
1843
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01001844.. js:function:: AppletTCP.send(appletmsg)
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02001845
1846 Send the message on the stream.
1847
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01001848 :param class_AppletTCP applet: An :ref:`applettcp_class`
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02001849 :param string msg: the message to send.
1850
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01001851.. js:function:: AppletTCP.get_priv(applet)
1852
1853 Return Lua data stored in the current transaction (with the
1854 `AppletTCP.set_priv()`) function. If no data are stored, it returns a nil
1855 value.
1856
1857 :param class_AppletTCP applet: An :ref:`applettcp_class`
1858 :returns: the opaque data previsously stored, or nil if nothing is
1859 avalaible.
1860
1861.. js:function:: AppletTCP.set_priv(applet, data)
1862
1863 Store any data in the current HAProxy transaction. This action replace the
1864 old stored data.
1865
1866 :param class_AppletTCP applet: An :ref:`applettcp_class`
1867 :param opaque data: The data which is stored in the transaction.
1868
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001869External Lua libraries
1870======================
1871
1872A lot of useful lua libraries can be found here:
1873
1874* `https://lua-toolbox.com/ <https://lua-toolbox.com/>`_
1875
1876Redis acces:
1877
1878* `https://github.com/nrk/redis-lua <https://github.com/nrk/redis-lua>`_
1879
1880This is an example about the usage of the Redis library with HAProxy. Note that
1881each call of any function of this library can throw an error if the socket
1882connection fails.
1883
1884.. code-block:: lua
1885
1886 -- load the redis library
1887 local redis = require("redis");
1888
1889 function do_something(txn)
1890
1891 -- create and connect new tcp socket
1892 local tcp = core.tcp();
1893 tcp:settimeout(1);
1894 tcp:connect("127.0.0.1", 6379);
1895
1896 -- use the redis library with this new socket
1897 local client = redis.connect({socket=tcp});
1898 client:ping();
1899
1900 end
1901
1902OpenSSL:
1903
1904* `http://mkottman.github.io/luacrypto/index.html
1905 <http://mkottman.github.io/luacrypto/index.html>`_
1906
1907* `https://github.com/brunoos/luasec/wiki
1908 <https://github.com/brunoos/luasec/wiki>`_
Thierry FOURNIER2e4893c2015-03-18 13:37:27 +01001909