blob: 96367f601b22ce643689f47c87e0bc15e79dd909 [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
Thierry FOURNIER12a865d2016-12-14 19:40:37 +0100168.. js:attribute:: core.proxies
169
170 **context**: task, action, sample-fetch, converter
171
172 This attribute is an array of declared proxies (frontend and backends). Each
173 proxy give an access to his list of listeners and servers. Each entry is of
174 type :ref:`proxy_class`
175
Thierry FOURNIER9b82a582017-07-24 13:30:43 +0200176 Warning, if you are declared frontend and backend with the same name, only one
177 of these are listed.
178
179 :see: :js:attr:`core.backends`
180 :see: :js:attr:`core.frontends`
181
182.. js:attribute:: core.backends
183
184 **context**: task, action, sample-fetch, converter
185
186 This attribute is an array of declared proxies with backend capability. Each
187 proxy give an access to his list of listeners and servers. Each entry is of
188 type :ref:`proxy_class`
189
190 Warning, if you are declared frontend and backend with the same name, only one
191 of these are listed.
192
193 :see: :js:attr:`core.proxies`
194 :see: :js:attr:`core.frontends`
195
196.. js:attribute:: core.frontends
197
198 **context**: task, action, sample-fetch, converter
199
200 This attribute is an array of declared proxies with frontend capability. Each
201 proxy give an access to his list of listeners and servers. Each entry is of
202 type :ref:`proxy_class`
203
204 Warning, if you are declared frontend and backend with the same name, only one
205 of these are listed.
206
207 :see: :js:attr:`core.proxies`
208 :see: :js:attr:`core.backends`
209
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100210.. js:function:: core.log(loglevel, msg)
211
212 **context**: body, init, task, action, sample-fetch, converter
213
David Carlier61fdf8b2015-10-02 11:59:38 +0100214 This function sends a log. The log is sent, according with the HAProxy
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100215 configuration file, on the default syslog server if it is configured and on
216 the stderr if it is allowed.
217
218 :param integer loglevel: Is the log level asociated with the message. It is a
219 number between 0 and 7.
220 :param string msg: The log content.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +0100221 :see: :js:attr:`core.emerg`, :js:attr:`core.alert`, :js:attr:`core.crit`,
222 :js:attr:`core.err`, :js:attr:`core.warning`, :js:attr:`core.notice`,
223 :js:attr:`core.info`, :js:attr:`core.debug` (log level definitions)
224 :see: :js:func:`core.Debug`
225 :see: :js:func:`core.Info`
226 :see: :js:func:`core.Warning`
227 :see: :js:func:`core.Alert`
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100228
229.. js:function:: core.Debug(msg)
230
231 **context**: body, init, task, action, sample-fetch, converter
232
233 :param string msg: The log content.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +0100234 :see: :js:func:`core.log`
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100235
236 Does the same job than:
237
238.. code-block:: lua
239
Thierry FOURNIERdc595002015-12-21 11:13:52 +0100240 function Debug(msg)
241 core.log(core.debug, msg)
242 end
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100243..
244
245.. js:function:: core.Info(msg)
246
247 **context**: body, init, task, action, sample-fetch, converter
248
249 :param string msg: The log content.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +0100250 :see: :js:func:`core.log`
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100251
252.. code-block:: lua
253
Thierry FOURNIERdc595002015-12-21 11:13:52 +0100254 function Info(msg)
255 core.log(core.info, msg)
256 end
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100257..
258
259.. js:function:: core.Warning(msg)
260
261 **context**: body, init, task, action, sample-fetch, converter
262
263 :param string msg: The log content.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +0100264 :see: :js:func:`core.log`
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100265
266.. code-block:: lua
267
Thierry FOURNIERdc595002015-12-21 11:13:52 +0100268 function Warning(msg)
269 core.log(core.warning, msg)
270 end
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100271..
272
273.. js:function:: core.Alert(msg)
274
275 **context**: body, init, task, action, sample-fetch, converter
276
277 :param string msg: The log content.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +0100278 :see: :js:func:`core.log`
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100279
280.. code-block:: lua
281
Thierry FOURNIERdc595002015-12-21 11:13:52 +0100282 function Alert(msg)
283 core.log(core.alert, msg)
284 end
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100285..
286
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100287.. js:function:: core.add_acl(filename, key)
288
289 **context**: init, task, action, sample-fetch, converter
290
291 Add the ACL *key* in the ACLs list referenced by the file *filename*.
292
293 :param string filename: the filename that reference the ACL entries.
294 :param string key: the key which will be added.
295
296.. js:function:: core.del_acl(filename, key)
297
298 **context**: init, task, action, sample-fetch, converter
299
300 Delete the ACL entry referenced by the key *key* in the list of ACLs
301 referenced by *filename*.
302
303 :param string filename: the filename that reference the ACL entries.
304 :param string key: the key which will be deleted.
305
306.. js:function:: core.del_map(filename, key)
307
308 **context**: init, task, action, sample-fetch, converter
309
310 Delete the map entry indexed with the specified key in the list of maps
311 referenced by his filename.
312
313 :param string filename: the filename that reference the map entries.
314 :param string key: the key which will be deleted.
315
Thierry Fourniereea77c02016-03-18 08:47:13 +0100316.. js:function:: core.get_info()
317
318 **context**: body, init, task, action, sample-fetch, converter
319
320 Returns HAProxy core informations. We can found information like the uptime,
321 the pid, memory pool usage, tasks number, ...
322
323 These information are also returned by the management sockat via the command
324 "show info". See the management socket documentation fpor more information
325 about the content of these variables.
326
327 :returns: an array of values.
328
Thierry Fournierb1f46562016-01-21 09:46:15 +0100329.. js:function:: core.now()
330
331 **context**: body, init, task, action
332
333 This function returns the current time. The time returned is fixed by the
334 HAProxy core and assures than the hour will be monotnic and that the system
335 call 'gettimeofday' will not be called too. The time is refreshed between each
336 Lua execution or resume, so two consecutive call to the function "now" will
337 probably returns the same result.
338
339 :returns: an array which contains two entries "sec" and "usec". "sec"
340 contains the current at the epoch format, and "usec" contains the
341 current microseconds.
342
Thierry FOURNIERa78f0372016-12-14 19:04:41 +0100343.. js:function:: core.http_date(date)
344
345 **context**: body, init, task, action
346
347 This function take a string repsenting http date, and returns an integer
348 containing the corresponding date with a epoch format. A valid http date
349 me respect the format IMF, RFC850 or ASCTIME.
350
351 :param string date: a date http-date formatted
352 :returns: integer containing epoch date
353 :see: :js:func:`core.imf_date`.
354 :see: :js:func:`core.rfc850_date`.
355 :see: :js:func:`core.asctime_date`.
356 :see: https://tools.ietf.org/html/rfc7231#section-7.1.1.1
357
358.. js:function:: core.imf_date(date)
359
360 **context**: body, init, task, action
361
362 This function take a string repsenting IMF date, and returns an integer
363 containing the corresponding date with a epoch format.
364
365 :param string date: a date IMF formatted
366 :returns: integer containing epoch date
367 :see: https://tools.ietf.org/html/rfc7231#section-7.1.1.1
368
369 The IMF format is like this:
370
371.. code-block:: text
372
373 Sun, 06 Nov 1994 08:49:37 GMT
374..
375
376.. js:function:: core.rfc850_date(date)
377
378 **context**: body, init, task, action
379
380 This function take a string repsenting RFC850 date, and returns an integer
381 containing the corresponding date with a epoch format.
382
383 :param string date: a date RFC859 formatted
384 :returns: integer containing epoch date
385 :see: https://tools.ietf.org/html/rfc7231#section-7.1.1.1
386
387 The RFC850 format is like this:
388
389.. code-block:: text
390
391 Sunday, 06-Nov-94 08:49:37 GMT
392..
393
394.. js:function:: core.asctime_date(date)
395
396 **context**: body, init, task, action
397
398 This function take a string repsenting ASCTIME date, and returns an integer
399 containing the corresponding date with a epoch format.
400
401 :param string date: a date ASCTIME formatted
402 :returns: integer containing epoch date
403 :see: https://tools.ietf.org/html/rfc7231#section-7.1.1.1
404
405 The ASCTIME format is like this:
406
407.. code-block:: text
408
409 Sun Nov 6 08:49:37 1994
410..
411
412.. js:function:: core.rfc850_date(date)
413
414 **context**: body, init, task, action
415
416 This function take a string repsenting http date, and returns an integer
417 containing the corresponding date with a epoch format.
418
419 :param string date: a date http-date formatted
420
421.. js:function:: core.asctime_date(date)
422
423 **context**: body, init, task, action
424
425 This function take a string repsenting http date, and returns an integer
426 containing the corresponding date with a epoch format.
427
428 :param string date: a date http-date formatted
429
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100430.. js:function:: core.msleep(milliseconds)
431
432 **context**: body, init, task, action
433
434 The `core.msleep()` stops the Lua execution between specified milliseconds.
435
436 :param integer milliseconds: the required milliseconds.
437
Thierry Fournierf61aa632016-02-19 20:56:00 +0100438.. js:attribute:: core.proxies
439
440 **context**: body, init, task, action, sample-fetch, converter
441
442 proxies is an array containing the list of all proxies declared in the
443 configuration file. Each entry of the proxies array is an object of type
444 :ref:`proxy_class`
445
Thierry FOURNIER8255a752015-09-23 21:03:35 +0200446.. js:function:: core.register_action(name, actions, func)
447
448 **context**: body
449
David Carlier61fdf8b2015-10-02 11:59:38 +0100450 Register a Lua function executed as action. All the registered action can be
Thierry FOURNIER8255a752015-09-23 21:03:35 +0200451 used in HAProxy with the prefix "lua.". An action gets a TXN object class as
452 input.
453
454 :param string name: is the name of the converter.
Willy Tarreau61add3c2015-09-28 15:39:10 +0200455 :param table actions: is a table of string describing the HAProxy actions who
Thierry FOURNIER8255a752015-09-23 21:03:35 +0200456 want to register to. The expected actions are 'tcp-req',
457 'tcp-res', 'http-req' or 'http-res'.
458 :param function func: is the Lua function called to work as converter.
459
460 The prototype of the Lua function used as argument is:
461
462.. code-block:: lua
463
464 function(txn)
465..
466
Thierry FOURNIERdc595002015-12-21 11:13:52 +0100467 * **txn** (:ref:`txn_class`): this is a TXN object used for manipulating the
Thierry FOURNIER8255a752015-09-23 21:03:35 +0200468 current request or TCP stream.
469
Willy Tarreau61add3c2015-09-28 15:39:10 +0200470 Here, an exemple of action registration. the action juste send an 'Hello world'
Thierry FOURNIER8255a752015-09-23 21:03:35 +0200471 in the logs.
472
473.. code-block:: lua
474
475 core.register_action("hello-world", { "tcp-req", "http-req" }, function(txn)
476 txn:Info("Hello world")
477 end)
478..
479
480 This example code is used in HAproxy configuration like this:
481
482::
483
484 frontend tcp_frt
485 mode tcp
486 tcp-request content lua.hello-world
487
488 frontend http_frt
489 mode http
490 http-request lua.hello-world
491
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100492.. js:function:: core.register_converters(name, func)
493
494 **context**: body
495
David Carlier61fdf8b2015-10-02 11:59:38 +0100496 Register a Lua function executed as converter. All the registered converters
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100497 can be used in HAProxy with the prefix "lua.". An converter get a string as
498 input and return a string as output. The registered function can take up to 9
499 values as parameter. All the value are strings.
500
501 :param string name: is the name of the converter.
502 :param function func: is the Lua function called to work as converter.
503
504 The prototype of the Lua function used as argument is:
505
506.. code-block:: lua
507
508 function(str, [p1 [, p2 [, ... [, p5]]]])
509..
510
511 * **str** (*string*): this is the input value automatically converted in
512 string.
513 * **p1** .. **p5** (*string*): this is a list of string arguments declared in
514 the haroxy configuration file. The number of arguments doesn't exceed 5.
515 The order and the nature of these is conventionally choose by the
516 developper.
517
518.. js:function:: core.register_fetches(name, func)
519
520 **context**: body
521
David Carlier61fdf8b2015-10-02 11:59:38 +0100522 Register a Lua function executed as sample fetch. All the registered sample
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100523 fetchs can be used in HAProxy with the prefix "lua.". A Lua sample fetch
524 return a string as output. The registered function can take up to 9 values as
525 parameter. All the value are strings.
526
527 :param string name: is the name of the converter.
528 :param function func: is the Lua function called to work as sample fetch.
529
530 The prototype of the Lua function used as argument is:
531
532.. code-block:: lua
533
534 string function(txn, [p1 [, p2 [, ... [, p5]]]])
535..
536
Thierry FOURNIERdc595002015-12-21 11:13:52 +0100537 * **txn** (:ref:`txn_class`): this is the txn object associated with the current
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100538 request.
539 * **p1** .. **p5** (*string*): this is a list of string arguments declared in
540 the haroxy configuration file. The number of arguments doesn't exceed 5.
541 The order and the nature of these is conventionally choose by the
542 developper.
543 * **Returns**: A string containing some data, ot nil if the value cannot be
544 returned now.
545
546 lua example code:
547
548.. code-block:: lua
549
550 core.register_fetches("hello", function(txn)
551 return "hello"
552 end)
553..
554
555 HAProxy example configuration:
556
557::
558
559 frontend example
560 http-request redirect location /%[lua.hello]
561
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +0200562.. js:function:: core.register_service(name, mode, func)
563
564 **context**: body
565
David Carlier61fdf8b2015-10-02 11:59:38 +0100566 Register a Lua function executed as a service. All the registered service can
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +0200567 be used in HAProxy with the prefix "lua.". A service gets an object class as
568 input according with the required mode.
569
570 :param string name: is the name of the converter.
Willy Tarreau61add3c2015-09-28 15:39:10 +0200571 :param string mode: is string describing the required mode. Only 'tcp' or
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +0200572 'http' are allowed.
573 :param function func: is the Lua function called to work as converter.
574
575 The prototype of the Lua function used as argument is:
576
577.. code-block:: lua
578
579 function(applet)
580..
581
Thierry FOURNIERdc595002015-12-21 11:13:52 +0100582 * **applet** *applet* will be a :ref:`applettcp_class` or a
583 :ref:`applethttp_class`. It depends the type of registered applet. An applet
584 registered with the 'http' value for the *mode* parameter will gets a
585 :ref:`applethttp_class`. If the *mode* value is 'tcp', the applet will gets
586 a :ref:`applettcp_class`.
587
588 **warning**: Applets of type 'http' cannot be called from 'tcp-*'
589 rulesets. Only the 'http-*' rulesets are authorized, this means
590 that is not possible to call an HTTP applet from a proxy in tcp
591 mode. Applets of type 'tcp' can be called from anywhre.
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +0200592
Willy Tarreau61add3c2015-09-28 15:39:10 +0200593 Here, an exemple of service registration. the service just send an 'Hello world'
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +0200594 as an http response.
595
596.. code-block:: lua
597
Pieter Baauw4d7f7662015-11-08 16:38:08 +0100598 core.register_service("hello-world", "http", function(applet)
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +0200599 local response = "Hello World !"
600 applet:set_status(200)
Pieter Baauw2dcb9bc2015-10-01 22:47:12 +0200601 applet:add_header("content-length", string.len(response))
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +0200602 applet:add_header("content-type", "text/plain")
Pieter Baauw2dcb9bc2015-10-01 22:47:12 +0200603 applet:start_response()
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +0200604 applet:send(response)
605 end)
606..
607
608 This example code is used in HAproxy configuration like this:
609
610::
611
612 frontend example
613 http-request use-service lua.hello-world
614
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100615.. js:function:: core.register_init(func)
616
617 **context**: body
618
619 Register a function executed after the configuration parsing. This is useful
620 to check any parameters.
621
Pieter Baauw4d7f7662015-11-08 16:38:08 +0100622 :param function func: is the Lua function called to work as initializer.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100623
624 The prototype of the Lua function used as argument is:
625
626.. code-block:: lua
627
628 function()
629..
630
631 It takes no input, and no output is expected.
632
633.. js:function:: core.register_task(func)
634
635 **context**: body, init, task, action, sample-fetch, converter
636
637 Register and start independent task. The task is started when the HAProxy
638 main scheduler starts. For example this type of tasks can be executed to
Thierry FOURNIER2e4893c2015-03-18 13:37:27 +0100639 perform complex health checks.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100640
Pieter Baauw4d7f7662015-11-08 16:38:08 +0100641 :param function func: is the Lua function called to work as initializer.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100642
643 The prototype of the Lua function used as argument is:
644
645.. code-block:: lua
646
647 function()
648..
649
650 It takes no input, and no output is expected.
651
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +0100652.. js:function:: core.register_cli([path], usage, func)
653
654 **context**: body
655
656 Register and start independent task. The task is started when the HAProxy
657 main scheduler starts. For example this type of tasks can be executed to
658 perform complex health checks.
659
660 :param array path: is the sequence of word for which the cli execute the Lua
661 binding.
662 :param string usage: is the usage message displayed in the help.
663 :param function func: is the Lua function called to handle the CLI commands.
664
665 The prototype of the Lua function used as argument is:
666
667.. code-block:: lua
668
669 function(AppletTCP, [arg1, [arg2, [...]]])
670..
671
672 I/O are managed with the :ref:`applettcp_class` object. Args are given as
673 paramter. The args embbed the registred path. If the path is declared like
674 this:
675
676.. code-block:: lua
677
678 core.register_cli({"show", "ssl", "stats"}, "Display SSL stats..", function(applet, arg1, arg2, arg3, arg4, arg5)
679 end)
680..
681
682 And we execute this in the prompt:
683
684.. code-block:: text
685
686 > prompt
687 > show ssl stats all
688..
689
690 Then, arg1, arg2 and arg3 will contains respectivey "show", "ssl" and "stats".
691 arg4 will contain "all". arg5 contains nil.
692
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100693.. js:function:: core.set_nice(nice)
694
695 **context**: task, action, sample-fetch, converter
696
697 Change the nice of the current task or current session.
Thierry FOURNIER2e4893c2015-03-18 13:37:27 +0100698
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100699 :param integer nice: the nice value, it must be between -1024 and 1024.
700
701.. js:function:: core.set_map(filename, key, value)
702
703 **context**: init, task, action, sample-fetch, converter
704
705 set the value *value* associated to the key *key* in the map referenced by
706 *filename*.
707
Thierry FOURNIERdc595002015-12-21 11:13:52 +0100708 :param string filename: the Map reference
709 :param string key: the key to set or replace
710 :param string value: the associated value
711
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100712.. js:function:: core.sleep(int seconds)
713
714 **context**: body, init, task, action
715
716 The `core.sleep()` functions stop the Lua execution between specified seconds.
717
718 :param integer seconds: the required seconds.
719
720.. js:function:: core.tcp()
721
722 **context**: init, task, action
723
724 This function returns a new object of a *socket* class.
725
Thierry FOURNIERdc595002015-12-21 11:13:52 +0100726 :returns: A :ref:`socket_class` object.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100727
Thierry Fournier1de16592016-01-27 09:49:07 +0100728.. js:function:: core.concat()
729
730 **context**: body, init, task, action, sample-fetch, converter
731
732 This function retruns a new concat object.
733
734 :returns: A :ref:`concat_class` object.
735
Thierry FOURNIER0a99b892015-08-26 00:14:17 +0200736.. js:function:: core.done(data)
737
738 **context**: body, init, task, action, sample-fetch, converter
739
740 :param any data: Return some data for the caller. It is useful with
741 sample-fetches and sample-converters.
742
743 Immediately stops the current Lua execution and returns to the caller which
744 may be a sample fetch, a converter or an action and returns the specified
745 value (ignored for actions). It is used when the LUA process finishes its
746 work and wants to give back the control to HAProxy without executing the
747 remaining code. It can be seen as a multi-level "return".
748
Thierry FOURNIER486f5a02015-03-16 15:13:03 +0100749.. js:function:: core.yield()
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100750
751 **context**: task, action, sample-fetch, converter
752
753 Give back the hand at the HAProxy scheduler. It is used when the LUA
754 processing consumes a lot of processing time.
755
Thierry FOURNIER / OZON.IO62fec752016-11-10 20:38:11 +0100756.. js:function:: core.parse_addr(address)
757
758 **context**: body, init, task, action, sample-fetch, converter
759
760 :param network: is a string describing an ipv4 or ipv6 address and optionally
761 its network length, like this: "127.0.0.1/8" or "aaaa::1234/32".
762 :returns: a userdata containing network or nil if an error occurs.
763
764 Parse ipv4 or ipv6 adresses and its facultative associated network.
765
766.. js:function:: core.match_addr(addr1, addr2)
767
768 **context**: body, init, task, action, sample-fetch, converter
769
770 :param addr1: is an address created with "core.parse_addr".
771 :param addr2: is an address created with "core.parse_addr".
772 :returns: boolean, true if the network of the addresses matche, else returns
773 false.
774
775 Match two networks. For example "127.0.0.1/32" matchs "127.0.0.0/8". The order
776 of network is not important.
777
Thierry FOURNIER / OZON.IO8a1027a2016-11-24 20:48:38 +0100778.. js:function:: core.tokenize(str, separators [, noblank])
779
780 **context**: body, init, task, action, sample-fetch, converter
781
782 This function is useful for tokenizing an entry, or splitting some messages.
783 :param string str: The string which will be split.
784 :param string separators: A string containing a list of separators.
785 :param boolean noblank: Ignore empty entries.
786 :returns: an array of string.
787
788 For example:
789
790.. code-block:: lua
791
792 local array = core.tokenize("This function is useful, for tokenizing an entry.", "., ", true)
793 print_r(array)
794..
795
796 Returns this array:
797
798.. code-block:: text
799
800 (table) table: 0x21c01e0 [
801 1: (string) "This"
802 2: (string) "function"
803 3: (string) "is"
804 4: (string) "useful"
805 5: (string) "for"
806 6: (string) "tokenizing"
807 7: (string) "an"
808 8: (string) "entry"
809 ]
810..
811
Thierry Fournierf61aa632016-02-19 20:56:00 +0100812.. _proxy_class:
813
814Proxy class
815============
816
817.. js:class:: Proxy
818
819 This class provides a way for manipulating proxy and retrieving information
820 like statistics.
821
Thierry FOURNIER817e7592017-07-24 14:35:04 +0200822.. js:attribute:: Proxy.name
823
824 Contain the name of the proxy.
825
Baptiste Assmann46c72552017-10-26 21:51:58 +0200826.. js:attribute:: Proxy.uuid
827
828 Contain the unique identifier of the proxy.
829
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +0100830.. js:attribute:: Proxy.servers
831
832 Contain an array with the attached servers. Each server entry is an object of
833 type :ref:`server_class`.
834
Thierry Fournierff480422016-02-25 08:36:46 +0100835.. js:attribute:: Proxy.listeners
836
837 Contain an array with the attached listeners. Each listeners entry is an
838 object of type :ref:`listener_class`.
839
Thierry Fournierf61aa632016-02-19 20:56:00 +0100840.. js:function:: Proxy.pause(px)
841
842 Pause the proxy. See the management socket documentation for more information.
843
844 :param class_proxy px: A :ref:`proxy_class` which indicates the manipulated
845 proxy.
846
847.. js:function:: Proxy.resume(px)
848
849 Resume the proxy. See the management socket documentation for more
850 information.
851
852 :param class_proxy px: A :ref:`proxy_class` which indicates the manipulated
853 proxy.
854
855.. js:function:: Proxy.stop(px)
856
857 Stop the proxy. See the management socket documentation for more information.
858
859 :param class_proxy px: A :ref:`proxy_class` which indicates the manipulated
860 proxy.
861
862.. js:function:: Proxy.shut_bcksess(px)
863
864 Kill the session attached to a backup server. See the management socket
865 documentation for more information.
866
867 :param class_proxy px: A :ref:`proxy_class` which indicates the manipulated
868 proxy.
869
870.. js:function:: Proxy.get_cap(px)
871
872 Returns a string describing the capabilities of the proxy.
873
874 :param class_proxy px: A :ref:`proxy_class` which indicates the manipulated
875 proxy.
876 :returns: a string "frontend", "backend", "proxy" or "ruleset".
877
878.. js:function:: Proxy.get_mode(px)
879
880 Returns a string describing the mode of the current proxy.
881
882 :param class_proxy px: A :ref:`proxy_class` which indicates the manipulated
883 proxy.
884 :returns: a string "tcp", "http", "health" or "unknown"
885
886.. js:function:: Proxy.get_stats(px)
887
888 Returns an array containg the proxy statistics. The statistics returned are
889 not the same if the proxy is frontend or a backend.
890
891 :param class_proxy px: A :ref:`proxy_class` which indicates the manipulated
892 proxy.
893 :returns: a key/value array containing stats
894
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +0100895.. _server_class:
896
897Server class
898============
899
900.. js:function:: Server.is_draining(sv)
901
902 Return true if the server is currently draining stiky connections.
903
904 :param class_server sv: A :ref:`server_class` which indicates the manipulated
905 server.
906 :returns: a boolean
907
908.. js:function:: Server.set_weight(sv, weight)
909
910 Dynamically change the weight of the serveur. See the management socket
911 documentation for more information about the format of the string.
912
913 :param class_server sv: A :ref:`server_class` which indicates the manipulated
914 server.
915 :param string weight: A string describing the server weight.
916
917.. js:function:: Server.get_weight(sv)
918
919 This function returns an integer representing the serveur weight.
920
921 :param class_server sv: A :ref:`server_class` which indicates the manipulated
922 server.
923 :returns: an integer.
924
925.. js:function:: Server.set_addr(sv, addr)
926
927 Dynamically change the address of the serveur. See the management socket
928 documentation for more information about the format of the string.
929
930 :param class_server sv: A :ref:`server_class` which indicates the manipulated
931 server.
932 :param string weight: A string describing the server address.
933
934.. js:function:: Server.get_addr(sv)
935
936 Returns a string describing the address of the serveur.
937
938 :param class_server sv: A :ref:`server_class` which indicates the manipulated
939 server.
940 :returns: A string
941
942.. js:function:: Server.get_stats(sv)
943
944 Returns server statistics.
945
946 :param class_server sv: A :ref:`server_class` which indicates the manipulated
947 server.
948 :returns: a key/value array containing stats
949
950.. js:function:: Server.shut_sess(sv)
951
952 Shutdown all the sessions attached to the server. See the management socket
953 documentation for more information about this function.
954
955 :param class_server sv: A :ref:`server_class` which indicates the manipulated
956 server.
957
958.. js:function:: Server.set_drain(sv)
959
960 Drain sticky sessions. See the management socket documentation for more
961 information about this function.
962
963 :param class_server sv: A :ref:`server_class` which indicates the manipulated
964 server.
965
966.. js:function:: Server.set_maint(sv)
967
968 Set maintenance mode. See the management socket documentation for more
969 information about this function.
970
971 :param class_server sv: A :ref:`server_class` which indicates the manipulated
972 server.
973
974.. js:function:: Server.set_ready(sv)
975
976 Set normal mode. See the management socket documentation for more information
977 about this function.
978
979 :param class_server sv: A :ref:`server_class` which indicates the manipulated
980 server.
981
982.. js:function:: Server.check_enable(sv)
983
984 Enable health checks. See the management socket documentation for more
985 information about this function.
986
987 :param class_server sv: A :ref:`server_class` which indicates the manipulated
988 server.
989
990.. js:function:: Server.check_disable(sv)
991
992 Disable health checks. See the management socket documentation for more
993 information about this function.
994
995 :param class_server sv: A :ref:`server_class` which indicates the manipulated
996 server.
997
998.. js:function:: Server.check_force_up(sv)
999
1000 Force health-check up. See the management socket documentation for more
1001 information about this function.
1002
1003 :param class_server sv: A :ref:`server_class` which indicates the manipulated
1004 server.
1005
1006.. js:function:: Server.check_force_nolb(sv)
1007
1008 Force health-check nolb mode. See the management socket documentation for more
1009 information about this function.
1010
1011 :param class_server sv: A :ref:`server_class` which indicates the manipulated
1012 server.
1013
1014.. js:function:: Server.check_force_down(sv)
1015
1016 Force health-check down. See the management socket documentation for more
1017 information about this function.
1018
1019 :param class_server sv: A :ref:`server_class` which indicates the manipulated
1020 server.
1021
1022.. js:function:: Server.agent_enable(sv)
1023
1024 Enable agent check. See the management socket documentation for more
1025 information about this function.
1026
1027 :param class_server sv: A :ref:`server_class` which indicates the manipulated
1028 server.
1029
1030.. js:function:: Server.agent_disable(sv)
1031
1032 Disable agent check. See the management socket documentation for more
1033 information about this function.
1034
1035 :param class_server sv: A :ref:`server_class` which indicates the manipulated
1036 server.
1037
1038.. js:function:: Server.agent_force_up(sv)
1039
1040 Force agent check up. See the management socket documentation for more
1041 information about this function.
1042
1043 :param class_server sv: A :ref:`server_class` which indicates the manipulated
1044 server.
1045
1046.. js:function:: Server.agent_force_down(sv)
1047
1048 Force agent check down. See the management socket documentation for more
1049 information about this function.
1050
1051 :param class_server sv: A :ref:`server_class` which indicates the manipulated
1052 server.
1053
Thierry Fournierff480422016-02-25 08:36:46 +01001054.. _listener_class:
1055
1056Listener class
1057==============
1058
1059.. js:function:: Listener.get_stats(ls)
1060
1061 Returns server statistics.
1062
1063 :param class_listener ls: A :ref:`listener_class` which indicates the
1064 manipulated listener.
1065 :returns: a key/value array containing stats
1066
Thierry Fournier1de16592016-01-27 09:49:07 +01001067.. _concat_class:
1068
1069Concat class
1070============
1071
1072.. js:class:: Concat
1073
1074 This class provides a fast way for string concatenation. The way using native
1075 Lua concatenation like the code below is slow for some reasons.
1076
1077.. code-block:: lua
1078
1079 str = "string1"
1080 str = str .. ", string2"
1081 str = str .. ", string3"
1082..
1083
1084 For each concatenation, Lua:
1085 * allocate memory for the result,
1086 * catenate the two string copying the strings in the new memory bloc,
1087 * free the old memory block containing the string whoch is no longer used.
1088 This process does many memory move, allocation and free. In addition, the
1089 memory is not really freed, it is just mark mark as unsused and wait for the
1090 garbage collector.
1091
1092 The Concat class provide an alternative way for catenating strings. It uses
1093 the internal Lua mechanism (it does not allocate memory), but it doesn't copy
1094 the data more than once.
1095
1096 On my computer, the following loops spends 0.2s for the Concat method and
1097 18.5s for the pure Lua implementation. So, the Concat class is about 1000x
1098 faster than the embedded solution.
1099
1100.. code-block:: lua
1101
1102 for j = 1, 100 do
1103 c = core.concat()
1104 for i = 1, 20000 do
1105 c:add("#####")
1106 end
1107 end
1108..
1109
1110.. code-block:: lua
1111
1112 for j = 1, 100 do
1113 c = ""
1114 for i = 1, 20000 do
1115 c = c .. "#####"
1116 end
1117 end
1118..
1119
1120.. js:function:: Concat.add(concat, string)
1121
1122 This function adds a string to the current concatenated string.
1123
1124 :param class_concat concat: A :ref:`concat_class` which contains the currently
1125 builded string.
1126 :param string string: A new string to concatenate to the current builded
1127 string.
1128
1129.. js:function:: Concat.dump(concat)
1130
1131 This function returns the concanated string.
1132
1133 :param class_concat concat: A :ref:`concat_class` which contains the currently
1134 builded string.
1135 :returns: the concatenated string
1136
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001137.. _fetches_class:
1138
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001139Fetches class
1140=============
1141
1142.. js:class:: Fetches
1143
1144 This class contains a lot of internal HAProxy sample fetches. See the
Thierry FOURNIER2e4893c2015-03-18 13:37:27 +01001145 HAProxy "configuration.txt" documentation for more information about her
1146 usage. they are the chapters 7.3.2 to 7.3.6.
1147
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001148 **warning** some sample fetches are not available in some context. These
1149 limitations are specified in this documentation when theire useful.
1150
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01001151 :see: :js:attr:`TXN.f`
1152 :see: :js:attr:`TXN.sf`
Thierry FOURNIER2e4893c2015-03-18 13:37:27 +01001153
1154 Fetches are useful for:
1155
1156 * get system time,
1157 * get environment variable,
1158 * get random numbers,
1159 * known backend status like the number of users in queue or the number of
1160 connections established,
1161 * client information like ip source or destination,
1162 * deal with stick tables,
1163 * Established SSL informations,
1164 * HTTP information like headers or method.
1165
1166.. code-block:: lua
1167
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001168 function action(txn)
1169 -- Get source IP
1170 local clientip = txn.f:src()
1171 end
Thierry FOURNIER2e4893c2015-03-18 13:37:27 +01001172..
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001173
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001174.. _converters_class:
1175
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001176Converters class
1177================
1178
1179.. js:class:: Converters
1180
1181 This class contains a lot of internal HAProxy sample converters. See the
Thierry FOURNIER2e4893c2015-03-18 13:37:27 +01001182 HAProxy documentation "configuration.txt" for more information about her
1183 usage. Its the chapter 7.3.1.
1184
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01001185 :see: :js:attr:`TXN.c`
1186 :see: :js:attr:`TXN.sc`
Thierry FOURNIER2e4893c2015-03-18 13:37:27 +01001187
1188 Converters provides statefull transformation. They are useful for:
1189
1190 * converting input to base64,
1191 * applying hash on input string (djb2, crc32, sdbm, wt6),
1192 * format date,
1193 * json escape,
Pieter Baauw4d7f7662015-11-08 16:38:08 +01001194 * extracting preferred language comparing two lists,
Thierry FOURNIER2e4893c2015-03-18 13:37:27 +01001195 * turn to lower or upper chars,
1196 * deal with stick tables.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001197
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001198.. _channel_class:
1199
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001200Channel class
1201=============
1202
1203.. js:class:: Channel
1204
1205 HAProxy uses two buffers for the processing of the requests. The first one is
1206 used with the request data (from the client to the server) and the second is
1207 used for the response data (from the server to the client).
1208
1209 Each buffer contains two types of data. The first type is the incoming data
1210 waiting for a processing. The second part is the outgoing data already
1211 processed. Usually, the incoming data is processed, after it is tagged as
1212 outgoing data, and finally it is sent. The following functions provides tools
1213 for manipulating these data in a buffer.
1214
1215 The following diagram shows where the channel class function are applied.
1216
1217 **Warning**: It is not possible to read from the response in request action,
1218 and it is not possible to read for the request channel in response action.
1219
Thierry FOURNIER2e4893c2015-03-18 13:37:27 +01001220.. image:: _static/channel.png
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001221
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001222.. js:function:: Channel.dup(channel)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001223
1224 This function returns a string that contain the entire buffer. The data is
1225 not remove from the buffer and can be reprocessed later.
1226
1227 If the buffer cant receive more data, a 'nil' value is returned.
1228
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001229 :param class_channel channel: The manipulated Channel.
Pieter Baauw4d7f7662015-11-08 16:38:08 +01001230 :returns: a string containing all the available data or nil.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001231
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001232.. js:function:: Channel.get(channel)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001233
1234 This function returns a string that contain the entire buffer. The data is
1235 consumed from the buffer.
1236
1237 If the buffer cant receive more data, a 'nil' value is returned.
1238
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001239 :param class_channel channel: The manipulated Channel.
Pieter Baauw4d7f7662015-11-08 16:38:08 +01001240 :returns: a string containing all the available data or nil.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001241
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02001242.. js:function:: Channel.getline(channel)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001243
1244 This function returns a string that contain the first line of the buffer. The
1245 data is consumed. If the data returned doesn't contains a final '\n' its
1246 assumed than its the last available data in the buffer.
1247
1248 If the buffer cant receive more data, a 'nil' value is returned.
1249
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001250 :param class_channel channel: The manipulated Channel.
Pieter Baauw386a1272015-08-16 15:26:24 +02001251 :returns: a string containing the available line or nil.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001252
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001253.. js:function:: Channel.set(channel, string)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001254
1255 This function replace the content of the buffer by the string. The function
1256 returns the copied length, otherwise, it returns -1.
1257
1258 The data set with this function are not send. They wait for the end of
1259 HAProxy processing, so the buffer can be full.
1260
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001261 :param class_channel channel: The manipulated Channel.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001262 :param string string: The data which will sent.
Pieter Baauw4d7f7662015-11-08 16:38:08 +01001263 :returns: an integer containing the amount of bytes copied or -1.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001264
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001265.. js:function:: Channel.append(channel, string)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001266
1267 This function append the string argument to the content of the buffer. The
1268 function returns the copied length, otherwise, it returns -1.
1269
1270 The data set with this function are not send. They wait for the end of
1271 HAProxy processing, so the buffer can be full.
1272
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001273 :param class_channel channel: The manipulated Channel.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001274 :param string string: The data which will sent.
Pieter Baauw4d7f7662015-11-08 16:38:08 +01001275 :returns: an integer containing the amount of bytes copied or -1.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001276
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001277.. js:function:: Channel.send(channel, string)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001278
1279 This function required immediate send of the data. Unless if the connection
1280 is close, the buffer is regularly flushed and all the string can be sent.
1281
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001282 :param class_channel channel: The manipulated Channel.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001283 :param string string: The data which will sent.
Pieter Baauw4d7f7662015-11-08 16:38:08 +01001284 :returns: an integer containing the amount of bytes copied or -1.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001285
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001286.. js:function:: Channel.get_in_length(channel)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001287
1288 This function returns the length of the input part of the buffer.
1289
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001290 :param class_channel channel: The manipulated Channel.
Pieter Baauw4d7f7662015-11-08 16:38:08 +01001291 :returns: an integer containing the amount of available bytes.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001292
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001293.. js:function:: Channel.get_out_length(channel)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001294
1295 This function returns the length of the output part of the buffer.
1296
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001297 :param class_channel channel: The manipulated Channel.
Pieter Baauw4d7f7662015-11-08 16:38:08 +01001298 :returns: an integer containing the amount of available bytes.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001299
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001300.. js:function:: Channel.forward(channel, int)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001301
1302 This function transfer bytes from the input part of the buffer to the output
1303 part.
1304
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001305 :param class_channel channel: The manipulated Channel.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001306 :param integer int: The amount of data which will be forwarded.
1307
Thierry FOURNIER / OZON.IO65192f32016-11-07 15:28:40 +01001308.. js:function:: Channel.is_full(channel)
1309
1310 This function returns true if the buffer channel is full.
1311
1312 :returns: a boolean
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001313
1314.. _http_class:
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001315
1316HTTP class
1317==========
1318
1319.. js:class:: HTTP
1320
1321 This class contain all the HTTP manipulation functions.
1322
Pieter Baauw386a1272015-08-16 15:26:24 +02001323.. js:function:: HTTP.req_get_headers(http)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001324
1325 Returns an array containing all the request headers.
1326
1327 :param class_http http: The related http object.
1328 :returns: array of headers.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01001329 :see: :js:func:`HTTP.res_get_headers`
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001330
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001331 This is the form of the returned array:
1332
1333.. code-block:: lua
1334
1335 HTTP:req_get_headers()['<header-name>'][<header-index>] = "<header-value>"
1336
1337 local hdr = HTTP:req_get_headers()
1338 hdr["host"][0] = "www.test.com"
1339 hdr["accept"][0] = "audio/basic q=1"
1340 hdr["accept"][1] = "audio/*, q=0.2"
1341 hdr["accept"][2] = "*/*, q=0.1"
1342..
1343
Pieter Baauw386a1272015-08-16 15:26:24 +02001344.. js:function:: HTTP.res_get_headers(http)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001345
1346 Returns an array containing all the response headers.
1347
1348 :param class_http http: The related http object.
1349 :returns: array of headers.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01001350 :see: :js:func:`HTTP.req_get_headers`
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001351
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001352 This is the form of the returned array:
1353
1354.. code-block:: lua
1355
1356 HTTP:res_get_headers()['<header-name>'][<header-index>] = "<header-value>"
1357
1358 local hdr = HTTP:req_get_headers()
1359 hdr["host"][0] = "www.test.com"
1360 hdr["accept"][0] = "audio/basic q=1"
1361 hdr["accept"][1] = "audio/*, q=0.2"
1362 hdr["accept"][2] = "*.*, q=0.1"
1363..
1364
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001365.. js:function:: HTTP.req_add_header(http, name, value)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001366
1367 Appends an HTTP header field in the request whose name is
1368 specified in "name" and whose value is defined in "value".
1369
1370 :param class_http http: The related http object.
1371 :param string name: The header name.
1372 :param string value: The header value.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01001373 :see: :js:func:`HTTP.res_add_header`
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001374
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001375.. js:function:: HTTP.res_add_header(http, name, value)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001376
1377 appends an HTTP header field in the response whose name is
1378 specified in "name" and whose value is defined in "value".
1379
1380 :param class_http http: The related http object.
1381 :param string name: The header name.
1382 :param string value: The header value.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01001383 :see: :js:func:`HTTP.req_add_header`
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001384
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001385.. js:function:: HTTP.req_del_header(http, name)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001386
1387 Removes all HTTP header fields in the request whose name is
1388 specified in "name".
1389
1390 :param class_http http: The related http object.
1391 :param string name: The header name.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01001392 :see: :js:func:`HTTP.res_del_header`
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001393
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001394.. js:function:: HTTP.res_del_header(http, name)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001395
1396 Removes all HTTP header fields in the response whose name is
1397 specified in "name".
1398
1399 :param class_http http: The related http object.
1400 :param string name: The header name.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01001401 :see: :js:func:`HTTP.req_del_header`
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001402
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001403.. js:function:: HTTP.req_set_header(http, name, value)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001404
1405 This variable replace all occurence of all header "name", by only
1406 one containing the "value".
1407
1408 :param class_http http: The related http object.
1409 :param string name: The header name.
1410 :param string value: The header value.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01001411 :see: :js:func:`HTTP.res_set_header`
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001412
1413 This function does the same work as the folowwing code:
1414
1415.. code-block:: lua
1416
1417 function fcn(txn)
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001418 TXN.http:req_del_header("header")
1419 TXN.http:req_add_header("header", "value")
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001420 end
1421..
1422
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001423.. js:function:: HTTP.res_set_header(http, name, value)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001424
1425 This variable replace all occurence of all header "name", by only
1426 one containing the "value".
1427
1428 :param class_http http: The related http object.
1429 :param string name: The header name.
1430 :param string value: The header value.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01001431 :see: :js:func:`HTTP.req_rep_header()`
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001432
Pieter Baauw386a1272015-08-16 15:26:24 +02001433.. js:function:: HTTP.req_rep_header(http, name, regex, replace)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001434
1435 Matches the regular expression in all occurrences of header field "name"
1436 according to "regex", and replaces them with the "replace" argument. The
1437 replacement value can contain back references like \1, \2, ... This
1438 function works with the request.
1439
1440 :param class_http http: The related http object.
1441 :param string name: The header name.
1442 :param string regex: The match regular expression.
1443 :param string replace: The replacement value.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01001444 :see: :js:func:`HTTP.res_rep_header()`
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001445
Pieter Baauw386a1272015-08-16 15:26:24 +02001446.. js:function:: HTTP.res_rep_header(http, name, regex, string)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001447
1448 Matches the regular expression in all occurrences of header field "name"
1449 according to "regex", and replaces them with the "replace" argument. The
1450 replacement value can contain back references like \1, \2, ... This
1451 function works with the request.
1452
1453 :param class_http http: The related http object.
1454 :param string name: The header name.
1455 :param string regex: The match regular expression.
1456 :param string replace: The replacement value.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01001457 :see: :js:func:`HTTP.req_rep_header()`
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001458
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001459.. js:function:: HTTP.req_set_method(http, method)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001460
1461 Rewrites the request method with the parameter "method".
1462
1463 :param class_http http: The related http object.
1464 :param string method: The new method.
1465
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001466.. js:function:: HTTP.req_set_path(http, path)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001467
1468 Rewrites the request path with the "path" parameter.
1469
1470 :param class_http http: The related http object.
1471 :param string path: The new path.
1472
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001473.. js:function:: HTTP.req_set_query(http, query)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001474
1475 Rewrites the request's query string which appears after the first question
1476 mark ("?") with the parameter "query".
1477
1478 :param class_http http: The related http object.
1479 :param string query: The new query.
1480
Thierry FOURNIER0d79cf62015-08-26 14:20:58 +02001481.. js:function:: HTTP.req_set_uri(http, uri)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001482
1483 Rewrites the request URI with the parameter "uri".
1484
1485 :param class_http http: The related http object.
1486 :param string uri: The new uri.
1487
Robin H. Johnson52f5db22017-01-01 13:10:52 -08001488.. js:function:: HTTP.res_set_status(http, status [, reason])
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +02001489
Robin H. Johnson52f5db22017-01-01 13:10:52 -08001490 Rewrites the response status code with the parameter "code".
1491
1492 If no custom reason is provided, it will be generated from the status.
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +02001493
1494 :param class_http http: The related http object.
1495 :param integer status: The new response status code.
Robin H. Johnson52f5db22017-01-01 13:10:52 -08001496 :param string reason: The new response reason (optional).
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +02001497
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001498.. _txn_class:
1499
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001500TXN class
1501=========
1502
1503.. js:class:: TXN
1504
1505 The txn class contain all the functions relative to the http or tcp
1506 transaction (Note than a tcp stream is the same than a tcp transaction, but
1507 an HTTP transaction is not the same than a tcp stream).
1508
1509 The usage of this class permits to retrieve data from the requests, alter it
1510 and forward it.
1511
1512 All the functions provided by this class are available in the context
1513 **sample-fetches** and **actions**.
1514
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001515.. js:attribute:: TXN.c
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001516
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001517 :returns: An :ref:`converters_class`.
1518
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001519 This attribute contains a Converters class object.
1520
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001521.. js:attribute:: TXN.sc
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001522
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001523 :returns: An :ref:`converters_class`.
1524
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001525 This attribute contains a Converters class object. The functions of
1526 this object returns always a string.
1527
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001528.. js:attribute:: TXN.f
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001529
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001530 :returns: An :ref:`fetches_class`.
1531
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001532 This attribute contains a Fetches class object.
1533
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001534.. js:attribute:: TXN.sf
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001535
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001536 :returns: An :ref:`fetches_class`.
1537
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001538 This attribute contains a Fetches class object. The functions of
1539 this object returns always a string.
1540
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001541.. js:attribute:: TXN.req
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001542
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001543 :returns: An :ref:`channel_class`.
1544
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001545 This attribute contains a channel class object for the request buffer.
1546
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001547.. js:attribute:: TXN.res
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001548
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001549 :returns: An :ref:`channel_class`.
1550
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001551 This attribute contains a channel class object for the response buffer.
1552
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001553.. js:attribute:: TXN.http
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001554
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001555 :returns: An :ref:`http_class`.
1556
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001557 This attribute contains an HTTP class object. It is avalaible only if the
1558 proxy has the "mode http" enabled.
1559
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01001560.. js:function:: TXN.log(TXN, loglevel, msg)
1561
1562 This function sends a log. The log is sent, according with the HAProxy
1563 configuration file, on the default syslog server if it is configured and on
1564 the stderr if it is allowed.
1565
1566 :param class_txn txn: The class txn object containing the data.
1567 :param integer loglevel: Is the log level asociated with the message. It is a
1568 number between 0 and 7.
1569 :param string msg: The log content.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01001570 :see: :js:attr:`core.emerg`, :js:attr:`core.alert`, :js:attr:`core.crit`,
1571 :js:attr:`core.err`, :js:attr:`core.warning`, :js:attr:`core.notice`,
1572 :js:attr:`core.info`, :js:attr:`core.debug` (log level definitions)
1573 :see: :js:func:`TXN.deflog`
1574 :see: :js:func:`TXN.Debug`
1575 :see: :js:func:`TXN.Info`
1576 :see: :js:func:`TXN.Warning`
1577 :see: :js:func:`TXN.Alert`
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01001578
1579.. js:function:: TXN.deflog(TXN, msg)
1580
1581 Sends a log line with the default loglevel for the proxy ssociated with the
1582 transaction.
1583
1584 :param class_txn txn: The class txn object containing the data.
1585 :param string msg: The log content.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01001586 :see: :js:func:`TXN.log
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01001587
1588.. js:function:: TXN.Debug(txn, msg)
1589
1590 :param class_txn txn: The class txn object containing the data.
1591 :param string msg: The log content.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01001592 :see: :js:func:`TXN.log`
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01001593
1594 Does the same job than:
1595
1596.. code-block:: lua
1597
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001598 function Debug(txn, msg)
1599 TXN.log(txn, core.debug, msg)
1600 end
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01001601..
1602
1603.. js:function:: TXN.Info(txn, msg)
1604
1605 :param class_txn txn: The class txn object containing the data.
1606 :param string msg: The log content.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01001607 :see: :js:func:`TXN.log`
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01001608
1609.. code-block:: lua
1610
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001611 function Debug(txn, msg)
1612 TXN.log(txn, core.info, msg)
1613 end
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01001614..
1615
1616.. js:function:: TXN.Warning(txn, msg)
1617
1618 :param class_txn txn: The class txn object containing the data.
1619 :param string msg: The log content.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01001620 :see: :js:func:`TXN.log`
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01001621
1622.. code-block:: lua
1623
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001624 function Debug(txn, msg)
1625 TXN.log(txn, core.warning, msg)
1626 end
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01001627..
1628
1629.. js:function:: TXN.Alert(txn, msg)
1630
1631 :param class_txn txn: The class txn object containing the data.
1632 :param string msg: The log content.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01001633 :see: :js:func:`TXN.log`
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01001634
1635.. code-block:: lua
1636
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001637 function Debug(txn, msg)
1638 TXN.log(txn, core.alert, msg)
1639 end
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01001640..
1641
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001642.. js:function:: TXN.get_priv(txn)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001643
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001644 Return Lua data stored in the current transaction (with the `TXN.set_priv()`)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001645 function. If no data are stored, it returns a nil value.
1646
1647 :param class_txn txn: The class txn object containing the data.
1648 :returns: the opaque data previsously stored, or nil if nothing is
1649 avalaible.
1650
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001651.. js:function:: TXN.set_priv(txn, data)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001652
1653 Store any data in the current HAProxy transaction. This action replace the
1654 old stored data.
1655
1656 :param class_txn txn: The class txn object containing the data.
1657 :param opaque data: The data which is stored in the transaction.
1658
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +02001659.. js:function:: TXN.set_var(TXN, var, value)
1660
David Carlier61fdf8b2015-10-02 11:59:38 +01001661 Converts a Lua type in a HAProxy type and store it in a variable <var>.
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +02001662
1663 :param class_txn txn: The class txn object containing the data.
1664 :param string var: The variable name according with the HAProxy variable syntax.
Thierry FOURNIER / OZON.IOb210bcc2016-12-12 16:24:16 +01001665 :param type value: The value associated to the variable. The type can be string or
1666 integer.
Christopher Faulet85d79c92016-11-09 16:54:56 +01001667
1668.. js:function:: TXN.unset_var(TXN, var)
1669
1670 Unset the variable <var>.
1671
1672 :param class_txn txn: The class txn object containing the data.
1673 :param string var: The variable name according with the HAProxy variable syntax.
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +02001674
1675.. js:function:: TXN.get_var(TXN, var)
1676
1677 Returns data stored in the variable <var> converter in Lua type.
1678
1679 :param class_txn txn: The class txn object containing the data.
1680 :param string var: The variable name according with the HAProxy variable syntax.
1681
Willy Tarreaubc183a62015-08-28 10:39:11 +02001682.. js:function:: TXN.done(txn)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001683
Willy Tarreaubc183a62015-08-28 10:39:11 +02001684 This function terminates processing of the transaction and the associated
1685 session. It can be used when a critical error is detected or to terminate
1686 processing after some data have been returned to the client (eg: a redirect).
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001687
Thierry FOURNIERab00df62016-07-14 11:42:37 +02001688 *Warning*: It not make sense to call this function from sample-fetches. In
1689 this case the behaviour of this one is the same than core.done(): it quit
1690 the Lua execution. The transaction is really aborted only from an action
1691 registered function.
1692
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001693 :param class_txn txn: The class txn object containing the data.
1694
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001695.. js:function:: TXN.set_loglevel(txn, loglevel)
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01001696
1697 Is used to change the log level of the current request. The "loglevel" must
1698 be an integer between 0 and 7.
1699
1700 :param class_txn txn: The class txn object containing the data.
1701 :param integer loglevel: The required log level. This variable can be one of
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01001702 :see: :js:attr:`core.emerg`, :js:attr:`core.alert`, :js:attr:`core.crit`,
1703 :js:attr:`core.err`, :js:attr:`core.warning`, :js:attr:`core.notice`,
1704 :js:attr:`core.info`, :js:attr:`core.debug` (log level definitions)
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01001705
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001706.. js:function:: TXN.set_tos(txn, tos)
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01001707
1708 Is used to set the TOS or DSCP field value of packets sent to the client to
1709 the value passed in "tos" on platforms which support this.
1710
1711 :param class_txn txn: The class txn object containing the data.
1712 :param integer tos: The new TOS os DSCP.
1713
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001714.. js:function:: TXN.set_mark(txn, mark)
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01001715
1716 Is used to set the Netfilter MARK on all packets sent to the client to the
1717 value passed in "mark" on platforms which support it.
1718
1719 :param class_txn txn: The class txn object containing the data.
1720 :param integer mark: The mark value.
1721
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001722.. _socket_class:
1723
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001724Socket class
1725============
1726
1727.. js:class:: Socket
1728
1729 This class must be compatible with the Lua Socket class. Only the 'client'
1730 functions are available. See the Lua Socket documentation:
1731
1732 `http://w3.impa.br/~diego/software/luasocket/tcp.html
1733 <http://w3.impa.br/~diego/software/luasocket/tcp.html>`_
1734
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001735.. js:function:: Socket.close(socket)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001736
1737 Closes a TCP object. The internal socket used by the object is closed and the
1738 local address to which the object was bound is made available to other
1739 applications. No further operations (except for further calls to the close
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001740 method) are allowed on a closed Socket.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001741
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001742 :param class_socket socket: Is the manipulated Socket.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001743
1744 Note: It is important to close all used sockets once they are not needed,
1745 since, in many systems, each socket uses a file descriptor, which are limited
1746 system resources. Garbage-collected objects are automatically closed before
1747 destruction, though.
1748
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02001749.. js:function:: Socket.connect(socket, address[, port])
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001750
1751 Attempts to connect a socket object to a remote host.
1752
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001753
1754 In case of error, the method returns nil followed by a string describing the
1755 error. In case of success, the method returns 1.
1756
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001757 :param class_socket socket: Is the manipulated Socket.
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02001758 :param string address: can be an IP address or a host name. See below for more
1759 information.
1760 :param integer port: must be an integer number in the range [1..64K].
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001761 :returns: 1 or nil.
1762
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02001763 an address field extension permits to use the connect() function to connect to
1764 other stream than TCP. The syntax containing a simpleipv4 or ipv6 address is
1765 the basically expected format. This format requires the port.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001766
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02001767 Other format accepted are a socket path like "/socket/path", it permits to
1768 connect to a socket. abstract namespaces are supported with the prefix
1769 "abns@", and finaly a filedescriotr can be passed with the prefix "fd@".
1770 The prefix "ipv4@", "ipv6@" and "unix@" are also supported. The port can be
1771 passed int the string. The syntax "127.0.0.1:1234" is valid. in this case, the
1772 parameter *port* is ignored.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001773
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001774.. js:function:: Socket.connect_ssl(socket, address, port)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001775
1776 Same behavior than the function socket:connect, but uses SSL.
1777
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001778 :param class_socket socket: Is the manipulated Socket.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001779 :returns: 1 or nil.
1780
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001781.. js:function:: Socket.getpeername(socket)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001782
1783 Returns information about the remote side of a connected client object.
1784
1785 Returns a string with the IP address of the peer, followed by the port number
1786 that peer is using for the connection. In case of error, the method returns
1787 nil.
1788
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001789 :param class_socket socket: Is the manipulated Socket.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001790 :returns: a string containing the server information.
1791
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001792.. js:function:: Socket.getsockname(socket)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001793
1794 Returns the local address information associated to the object.
1795
1796 The method returns a string with local IP address and a number with the port.
1797 In case of error, the method returns nil.
1798
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001799 :param class_socket socket: Is the manipulated Socket.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001800 :returns: a string containing the client information.
1801
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001802.. js:function:: Socket.receive(socket, [pattern [, prefix]])
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001803
1804 Reads data from a client object, according to the specified read pattern.
1805 Patterns follow the Lua file I/O format, and the difference in performance
1806 between all patterns is negligible.
1807
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001808 :param class_socket socket: Is the manipulated Socket.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001809 :param string|integer pattern: Describe what is required (see below).
1810 :param string prefix: A string which will be prefix the returned data.
1811 :returns: a string containing the required data or nil.
1812
1813 Pattern can be any of the following:
1814
1815 * **`*a`**: reads from the socket until the connection is closed. No
1816 end-of-line translation is performed;
1817
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001818 * **`*l`**: reads a line of text from the Socket. The line is terminated by a
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001819 LF character (ASCII 10), optionally preceded by a CR character
1820 (ASCII 13). The CR and LF characters are not included in the
1821 returned line. In fact, all CR characters are ignored by the
1822 pattern. This is the default pattern.
1823
1824 * **number**: causes the method to read a specified number of bytes from the
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001825 Socket. Prefix is an optional string to be concatenated to the
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001826 beginning of any received data before return.
1827
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02001828 * **empty**: If the pattern is left empty, the default option is `*l`.
1829
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001830 If successful, the method returns the received pattern. In case of error, the
1831 method returns nil followed by an error message which can be the string
1832 'closed' in case the connection was closed before the transmission was
1833 completed or the string 'timeout' in case there was a timeout during the
1834 operation. Also, after the error message, the function returns the partial
1835 result of the transmission.
1836
1837 Important note: This function was changed severely. It used to support
1838 multiple patterns (but I have never seen this feature used) and now it
1839 doesn't anymore. Partial results used to be returned in the same way as
1840 successful results. This last feature violated the idea that all functions
1841 should return nil on error. Thus it was changed too.
1842
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001843.. js:function:: Socket.send(socket, data [, start [, end ]])
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001844
1845 Sends data through client object.
1846
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001847 :param class_socket socket: Is the manipulated Socket.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001848 :param string data: The data that will be sent.
1849 :param integer start: The start position in the buffer of the data which will
1850 be sent.
1851 :param integer end: The end position in the buffer of the data which will
1852 be sent.
1853 :returns: see below.
1854
1855 Data is the string to be sent. The optional arguments i and j work exactly
1856 like the standard string.sub Lua function to allow the selection of a
1857 substring to be sent.
1858
1859 If successful, the method returns the index of the last byte within [start,
1860 end] that has been sent. Notice that, if start is 1 or absent, this is
1861 effectively the total number of bytes sent. In case of error, the method
1862 returns nil, followed by an error message, followed by the index of the last
1863 byte within [start, end] that has been sent. You might want to try again from
1864 the byte following that. The error message can be 'closed' in case the
1865 connection was closed before the transmission was completed or the string
1866 'timeout' in case there was a timeout during the operation.
1867
1868 Note: Output is not buffered. For small strings, it is always better to
1869 concatenate them in Lua (with the '..' operator) and send the result in one
1870 call instead of calling the method several times.
1871
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001872.. js:function:: Socket.setoption(socket, option [, value])
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001873
1874 Just implemented for compatibility, this cal does nothing.
1875
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001876.. js:function:: Socket.settimeout(socket, value [, mode])
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001877
1878 Changes the timeout values for the object. All I/O operations are blocking.
1879 That is, any call to the methods send, receive, and accept will block
1880 indefinitely, until the operation completes. The settimeout method defines a
1881 limit on the amount of time the I/O methods can block. When a timeout time
1882 has elapsed, the affected methods give up and fail with an error code.
1883
1884 The amount of time to wait is specified as the value parameter, in seconds.
1885
1886 The timeout modes are bot implemented, the only settable timeout is the
1887 inactivity time waiting for complete the internal buffer send or waiting for
1888 receive data.
1889
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001890 :param class_socket socket: Is the manipulated Socket.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001891 :param integer value: The timeout value.
1892
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001893.. _map_class:
1894
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001895Map class
1896=========
1897
1898.. js:class:: Map
1899
1900 This class permits to do some lookup in HAProxy maps. The declared maps can
1901 be modified during the runtime throught the HAProxy management socket.
1902
1903.. code-block:: lua
1904
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001905 default = "usa"
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001906
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001907 -- Create and load map
Thierry FOURNIER4dc71972017-01-28 08:33:08 +01001908 geo = Map.new("geo.map", Map._ip);
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001909
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001910 -- Create new fetch that returns the user country
1911 core.register_fetches("country", function(txn)
1912 local src;
1913 local loc;
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001914
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001915 src = txn.f:fhdr("x-forwarded-for");
1916 if (src == nil) then
1917 src = txn.f:src()
1918 if (src == nil) then
1919 return default;
1920 end
1921 end
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001922
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001923 -- Perform lookup
1924 loc = geo:lookup(src);
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001925
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001926 if (loc == nil) then
1927 return default;
1928 end
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001929
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001930 return loc;
1931 end);
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001932
Thierry FOURNIER4dc71972017-01-28 08:33:08 +01001933.. js:attribute:: Map._int
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001934
1935 See the HAProxy configuration.txt file, chapter "Using ACLs and fetching
1936 samples" ans subchapter "ACL basics" to understand this pattern matching
1937 method.
1938
Thierry FOURNIER4dc71972017-01-28 08:33:08 +01001939 Note that :js:attr:`Map.int` is also available for compatibility.
1940
1941.. js:attribute:: Map._ip
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001942
1943 See the HAProxy configuration.txt file, chapter "Using ACLs and fetching
1944 samples" ans subchapter "ACL basics" to understand this pattern matching
1945 method.
1946
Thierry FOURNIER4dc71972017-01-28 08:33:08 +01001947 Note that :js:attr:`Map.ip` is also available for compatibility.
1948
1949.. js:attribute:: Map._str
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001950
1951 See the HAProxy configuration.txt file, chapter "Using ACLs and fetching
1952 samples" ans subchapter "ACL basics" to understand this pattern matching
1953 method.
1954
Thierry FOURNIER4dc71972017-01-28 08:33:08 +01001955 Note that :js:attr:`Map.str` is also available for compatibility.
1956
1957.. js:attribute:: Map._beg
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001958
1959 See the HAProxy configuration.txt file, chapter "Using ACLs and fetching
1960 samples" ans subchapter "ACL basics" to understand this pattern matching
1961 method.
1962
Thierry FOURNIER4dc71972017-01-28 08:33:08 +01001963 Note that :js:attr:`Map.beg` is also available for compatibility.
1964
1965.. js:attribute:: Map._sub
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001966
1967 See the HAProxy configuration.txt file, chapter "Using ACLs and fetching
1968 samples" ans subchapter "ACL basics" to understand this pattern matching
1969 method.
1970
Thierry FOURNIER4dc71972017-01-28 08:33:08 +01001971 Note that :js:attr:`Map.sub` is also available for compatibility.
1972
1973.. js:attribute:: Map._dir
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001974
1975 See the HAProxy configuration.txt file, chapter "Using ACLs and fetching
1976 samples" ans subchapter "ACL basics" to understand this pattern matching
1977 method.
1978
Thierry FOURNIER4dc71972017-01-28 08:33:08 +01001979 Note that :js:attr:`Map.dir` is also available for compatibility.
1980
1981.. js:attribute:: Map._dom
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001982
1983 See the HAProxy configuration.txt file, chapter "Using ACLs and fetching
1984 samples" ans subchapter "ACL basics" to understand this pattern matching
1985 method.
1986
Thierry FOURNIER4dc71972017-01-28 08:33:08 +01001987 Note that :js:attr:`Map.dom` is also available for compatibility.
1988
1989.. js:attribute:: Map._end
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001990
1991 See the HAProxy configuration.txt file, chapter "Using ACLs and fetching
1992 samples" ans subchapter "ACL basics" to understand this pattern matching
1993 method.
1994
Thierry FOURNIER4dc71972017-01-28 08:33:08 +01001995.. js:attribute:: Map._reg
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001996
1997 See the HAProxy configuration.txt file, chapter "Using ACLs and fetching
1998 samples" ans subchapter "ACL basics" to understand this pattern matching
1999 method.
2000
Thierry FOURNIER4dc71972017-01-28 08:33:08 +01002001 Note that :js:attr:`Map.reg` is also available for compatibility.
2002
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002003
2004.. js:function:: Map.new(file, method)
2005
2006 Creates and load a map.
2007
2008 :param string file: Is the file containing the map.
2009 :param integer method: Is the map pattern matching method. See the attributes
2010 of the Map class.
2011 :returns: a class Map object.
Thierry FOURNIER4dc71972017-01-28 08:33:08 +01002012 :see: The Map attributes: :js:attr:`Map._int`, :js:attr:`Map._ip`,
2013 :js:attr:`Map._str`, :js:attr:`Map._beg`, :js:attr:`Map._sub`,
2014 :js:attr:`Map._dir`, :js:attr:`Map._dom`, :js:attr:`Map._end` and
2015 :js:attr:`Map._reg`.
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002016
2017.. js:function:: Map.lookup(map, str)
2018
2019 Perform a lookup in a map.
2020
2021 :param class_map map: Is the class Map object.
2022 :param string str: Is the string used as key.
2023 :returns: a string containing the result or nil if no match.
2024
2025.. js:function:: Map.slookup(map, str)
2026
2027 Perform a lookup in a map.
2028
2029 :param class_map map: Is the class Map object.
2030 :param string str: Is the string used as key.
2031 :returns: a string containing the result or empty string if no match.
2032
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002033.. _applethttp_class:
2034
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002035AppletHTTP class
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002036================
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002037
2038.. js:class:: AppletHTTP
2039
2040 This class is used with applets that requires the 'http' mode. The http applet
2041 can be registered with the *core.register_service()* function. They are used
2042 for processing an http request like a server in back of HAProxy.
2043
2044 This is an hello world sample code:
2045
2046.. code-block:: lua
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002047
Pieter Baauw4d7f7662015-11-08 16:38:08 +01002048 core.register_service("hello-world", "http", function(applet)
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002049 local response = "Hello World !"
2050 applet:set_status(200)
Pieter Baauw2dcb9bc2015-10-01 22:47:12 +02002051 applet:add_header("content-length", string.len(response))
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002052 applet:add_header("content-type", "text/plain")
Pieter Baauw2dcb9bc2015-10-01 22:47:12 +02002053 applet:start_response()
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002054 applet:send(response)
2055 end)
2056
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002057.. js:attribute:: AppletHTTP.c
2058
2059 :returns: A :ref:`converters_class`
2060
2061 This attribute contains a Converters class object.
2062
2063.. js:attribute:: AppletHTTP.sc
2064
2065 :returns: A :ref:`converters_class`
2066
2067 This attribute contains a Converters class object. The
2068 functions of this object returns always a string.
2069
2070.. js:attribute:: AppletHTTP.f
2071
2072 :returns: A :ref:`fetches_class`
2073
2074 This attribute contains a Fetches class object. Note that the
2075 applet execution place cannot access to a valid HAProxy core HTTP
2076 transaction, so some sample fecthes related to the HTTP dependant
2077 values (hdr, path, ...) are not available.
2078
2079.. js:attribute:: AppletHTTP.sf
2080
2081 :returns: A :ref:`fetches_class`
2082
2083 This attribute contains a Fetches class object. The functions of
2084 this object returns always a string. Note that the applet
2085 execution place cannot access to a valid HAProxy core HTTP
2086 transaction, so some sample fecthes related to the HTTP dependant
2087 values (hdr, path, ...) are not available.
2088
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01002089.. js:attribute:: AppletHTTP.method
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002090
2091 :returns: string
2092
2093 The attribute method returns a string containing the HTTP
2094 method.
2095
2096.. js:attribute:: AppletHTTP.version
2097
2098 :returns: string
2099
2100 The attribute version, returns a string containing the HTTP
2101 request version.
2102
2103.. js:attribute:: AppletHTTP.path
2104
2105 :returns: string
2106
2107 The attribute path returns a string containing the HTTP
2108 request path.
2109
2110.. js:attribute:: AppletHTTP.qs
2111
2112 :returns: string
2113
2114 The attribute qs returns a string containing the HTTP
2115 request query string.
2116
2117.. js:attribute:: AppletHTTP.length
2118
2119 :returns: integer
2120
2121 The attribute length returns an integer containing the HTTP
2122 body length.
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002123
Thierry FOURNIER841475e2015-12-11 17:10:09 +01002124.. js:attribute:: AppletHTTP.headers
2125
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002126 :returns: array
2127
2128 The attribute headers returns an array containing the HTTP
2129 headers. The header names are always in lower case. As the header name can be
2130 encountered more than once in each request, the value is indexed with 0 as
2131 first index value. The array have this form:
2132
2133.. code-block:: lua
2134
2135 AppletHTTP.headers['<header-name>'][<header-index>] = "<header-value>"
2136
2137 AppletHTTP.headers["host"][0] = "www.test.com"
2138 AppletHTTP.headers["accept"][0] = "audio/basic q=1"
2139 AppletHTTP.headers["accept"][1] = "audio/*, q=0.2"
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01002140 AppletHTTP.headers["accept"][2] = "*/*, q=0.1"
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002141..
2142
Robin H. Johnson52f5db22017-01-01 13:10:52 -08002143.. js:function:: AppletHTTP.set_status(applet, code [, reason])
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002144
2145 This function sets the HTTP status code for the response. The allowed code are
2146 from 100 to 599.
2147
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01002148 :param class_AppletHTTP applet: An :ref:`applethttp_class`
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002149 :param integer code: the status code returned to the client.
Robin H. Johnson52f5db22017-01-01 13:10:52 -08002150 :param string reason: the status reason returned to the client (optional).
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002151
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01002152.. js:function:: AppletHTTP.add_header(applet, name, value)
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002153
2154 This function add an header in the response. Duplicated headers are not
2155 collapsed. The special header *content-length* is used to determinate the
2156 response length. If it not exists, a *transfer-encoding: chunked* is set, and
2157 all the write from the funcion *AppletHTTP:send()* become a chunk.
2158
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01002159 :param class_AppletHTTP applet: An :ref:`applethttp_class`
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002160 :param string name: the header name
2161 :param string value: the header value
2162
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01002163.. js:function:: AppletHTTP.start_response(applet)
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002164
2165 This function indicates to the HTTP engine that it can process and send the
2166 response headers. After this called we cannot add headers to the response; We
2167 cannot use the *AppletHTTP:send()* function if the
2168 *AppletHTTP:start_response()* is not called.
2169
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01002170 :param class_AppletHTTP applet: An :ref:`applethttp_class`
2171
2172.. js:function:: AppletHTTP.getline(applet)
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002173
2174 This function returns a string containing one line from the http body. If the
2175 data returned doesn't contains a final '\\n' its assumed than its the last
2176 available data before the end of stream.
2177
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01002178 :param class_AppletHTTP applet: An :ref:`applethttp_class`
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002179 :returns: a string. The string can be empty if we reach the end of the stream.
2180
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01002181.. js:function:: AppletHTTP.receive(applet, [size])
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002182
2183 Reads data from the HTTP body, according to the specified read *size*. If the
2184 *size* is missing, the function tries to read all the content of the stream
2185 until the end. If the *size* is bigger than the http body, it returns the
2186 amount of data avalaible.
2187
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01002188 :param class_AppletHTTP applet: An :ref:`applethttp_class`
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002189 :param integer size: the required read size.
2190 :returns: always return a string,the string can be empty is the connexion is
2191 closed.
2192
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01002193.. js:function:: AppletHTTP.send(applet, msg)
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002194
2195 Send the message *msg* on the http request body.
2196
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01002197 :param class_AppletHTTP applet: An :ref:`applethttp_class`
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002198 :param string msg: the message to send.
2199
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01002200.. js:function:: AppletHTTP.get_priv(applet)
2201
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01002202 Return Lua data stored in the current transaction. If no data are stored,
2203 it returns a nil value.
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01002204
2205 :param class_AppletHTTP applet: An :ref:`applethttp_class`
2206 :returns: the opaque data previsously stored, or nil if nothing is
2207 avalaible.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01002208 :see: :js:func:`AppletHTTP.set_priv`
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01002209
2210.. js:function:: AppletHTTP.set_priv(applet, data)
2211
2212 Store any data in the current HAProxy transaction. This action replace the
2213 old stored data.
2214
2215 :param class_AppletHTTP applet: An :ref:`applethttp_class`
2216 :param opaque data: The data which is stored in the transaction.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01002217 :see: :js:func:`AppletHTTP.get_priv`
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01002218
Thierry FOURNIER / OZON.IOc1edafe2016-12-12 16:25:30 +01002219.. js:function:: AppletHTTP.set_var(applet, var, value)
2220
2221 Converts a Lua type in a HAProxy type and store it in a variable <var>.
2222
2223 :param class_AppletHTTP applet: An :ref:`applethttp_class`
2224 :param string var: The variable name according with the HAProxy variable syntax.
2225 :param type value: The value associated to the variable. The type ca be string or
2226 integer.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01002227 :see: :js:func:`AppletHTTP.unset_var`
2228 :see: :js:func:`AppletHTTP.get_var`
Thierry FOURNIER / OZON.IOc1edafe2016-12-12 16:25:30 +01002229
2230.. js:function:: AppletHTTP.unset_var(applet, var)
2231
2232 Unset the variable <var>.
2233
2234 :param class_AppletHTTP applet: An :ref:`applethttp_class`
2235 :param string var: The variable name according with the HAProxy variable syntax.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01002236 :see: :js:func:`AppletHTTP.set_var`
2237 :see: :js:func:`AppletHTTP.get_var`
Thierry FOURNIER / OZON.IOc1edafe2016-12-12 16:25:30 +01002238
2239.. js:function:: AppletHTTP.get_var(applet, var)
2240
2241 Returns data stored in the variable <var> converter in Lua type.
2242
2243 :param class_AppletHTTP applet: An :ref:`applethttp_class`
2244 :param string var: The variable name according with the HAProxy variable syntax.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01002245 :see: :js:func:`AppletHTTP.set_var`
2246 :see: :js:func:`AppletHTTP.unset_var`
Thierry FOURNIER / OZON.IOc1edafe2016-12-12 16:25:30 +01002247
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002248.. _applettcp_class:
2249
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002250AppletTCP class
2251===============
2252
2253.. js:class:: AppletTCP
2254
2255 This class is used with applets that requires the 'tcp' mode. The tcp applet
2256 can be registered with the *core.register_service()* function. They are used
2257 for processing a tcp stream like a server in back of HAProxy.
2258
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002259.. js:attribute:: AppletTCP.c
2260
2261 :returns: A :ref:`converters_class`
2262
2263 This attribute contains a Converters class object.
2264
2265.. js:attribute:: AppletTCP.sc
2266
2267 :returns: A :ref:`converters_class`
2268
2269 This attribute contains a Converters class object. The
2270 functions of this object returns always a string.
2271
2272.. js:attribute:: AppletTCP.f
2273
2274 :returns: A :ref:`fetches_class`
2275
2276 This attribute contains a Fetches class object.
2277
2278.. js:attribute:: AppletTCP.sf
2279
2280 :returns: A :ref:`fetches_class`
2281
2282 This attribute contains a Fetches class object.
2283
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01002284.. js:function:: AppletTCP.getline(applet)
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002285
2286 This function returns a string containing one line from the stream. If the
2287 data returned doesn't contains a final '\\n' its assumed than its the last
2288 available data before the end of stream.
2289
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01002290 :param class_AppletTCP applet: An :ref:`applettcp_class`
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002291 :returns: a string. The string can be empty if we reach the end of the stream.
2292
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01002293.. js:function:: AppletTCP.receive(applet, [size])
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002294
2295 Reads data from the TCP stream, according to the specified read *size*. If the
2296 *size* is missing, the function tries to read all the content of the stream
2297 until the end.
2298
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01002299 :param class_AppletTCP applet: An :ref:`applettcp_class`
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002300 :param integer size: the required read size.
2301 :returns: always return a string,the string can be empty is the connexion is
2302 closed.
2303
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01002304.. js:function:: AppletTCP.send(appletmsg)
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002305
2306 Send the message on the stream.
2307
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01002308 :param class_AppletTCP applet: An :ref:`applettcp_class`
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002309 :param string msg: the message to send.
2310
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01002311.. js:function:: AppletTCP.get_priv(applet)
2312
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01002313 Return Lua data stored in the current transaction. If no data are stored,
2314 it returns a nil value.
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01002315
2316 :param class_AppletTCP applet: An :ref:`applettcp_class`
2317 :returns: the opaque data previsously stored, or nil if nothing is
2318 avalaible.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01002319 :see: :js:func:`AppletTCP.set_priv`
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01002320
2321.. js:function:: AppletTCP.set_priv(applet, data)
2322
2323 Store any data in the current HAProxy transaction. This action replace the
2324 old stored data.
2325
2326 :param class_AppletTCP applet: An :ref:`applettcp_class`
2327 :param opaque data: The data which is stored in the transaction.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01002328 :see: :js:func:`AppletTCP.get_priv`
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01002329
Thierry FOURNIER / OZON.IOc1edafe2016-12-12 16:25:30 +01002330.. js:function:: AppletTCP.set_var(applet, var, value)
2331
2332 Converts a Lua type in a HAProxy type and stores it in a variable <var>.
2333
2334 :param class_AppletTCP applet: An :ref:`applettcp_class`
2335 :param string var: The variable name according with the HAProxy variable syntax.
2336 :param type value: The value associated to the variable. The type can be string or
2337 integer.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01002338 :see: :js:func:`AppletTCP.unset_var`
2339 :see: :js:func:`AppletTCP.get_var`
Thierry FOURNIER / OZON.IOc1edafe2016-12-12 16:25:30 +01002340
2341.. js:function:: AppletTCP.unset_var(applet, var)
2342
2343 Unsets the variable <var>.
2344
2345 :param class_AppletTCP applet: An :ref:`applettcp_class`
2346 :param string var: The variable name according with the HAProxy variable syntax.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01002347 :see: :js:func:`AppletTCP.unset_var`
2348 :see: :js:func:`AppletTCP.set_var`
Thierry FOURNIER / OZON.IOc1edafe2016-12-12 16:25:30 +01002349
2350.. js:function:: AppletTCP.get_var(applet, var)
2351
2352 Returns data stored in the variable <var> converter in Lua type.
2353
2354 :param class_AppletTCP applet: An :ref:`applettcp_class`
2355 :param string var: The variable name according with the HAProxy variable syntax.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01002356 :see: :js:func:`AppletTCP.unset_var`
2357 :see: :js:func:`AppletTCP.set_var`
Thierry FOURNIER / OZON.IOc1edafe2016-12-12 16:25:30 +01002358
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002359External Lua libraries
2360======================
2361
2362A lot of useful lua libraries can be found here:
2363
2364* `https://lua-toolbox.com/ <https://lua-toolbox.com/>`_
2365
2366Redis acces:
2367
2368* `https://github.com/nrk/redis-lua <https://github.com/nrk/redis-lua>`_
2369
2370This is an example about the usage of the Redis library with HAProxy. Note that
2371each call of any function of this library can throw an error if the socket
2372connection fails.
2373
2374.. code-block:: lua
2375
2376 -- load the redis library
2377 local redis = require("redis");
2378
2379 function do_something(txn)
2380
2381 -- create and connect new tcp socket
2382 local tcp = core.tcp();
2383 tcp:settimeout(1);
2384 tcp:connect("127.0.0.1", 6379);
2385
2386 -- use the redis library with this new socket
2387 local client = redis.connect({socket=tcp});
2388 client:ping();
2389
2390 end
2391
2392OpenSSL:
2393
2394* `http://mkottman.github.io/luacrypto/index.html
2395 <http://mkottman.github.io/luacrypto/index.html>`_
2396
2397* `https://github.com/brunoos/luasec/wiki
2398 <https://github.com/brunoos/luasec/wiki>`_
Thierry FOURNIER2e4893c2015-03-18 13:37:27 +01002399