blob: 822f8bc978375501c26f7cf6538284f62cdbb64c [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
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +0100826.. js:attribute:: Proxy.servers
827
828 Contain an array with the attached servers. Each server entry is an object of
829 type :ref:`server_class`.
830
Thierry Fournierff480422016-02-25 08:36:46 +0100831.. js:attribute:: Proxy.listeners
832
833 Contain an array with the attached listeners. Each listeners entry is an
834 object of type :ref:`listener_class`.
835
Thierry Fournierf61aa632016-02-19 20:56:00 +0100836.. js:function:: Proxy.pause(px)
837
838 Pause the proxy. See the management socket documentation for more information.
839
840 :param class_proxy px: A :ref:`proxy_class` which indicates the manipulated
841 proxy.
842
843.. js:function:: Proxy.resume(px)
844
845 Resume the proxy. See the management socket documentation for more
846 information.
847
848 :param class_proxy px: A :ref:`proxy_class` which indicates the manipulated
849 proxy.
850
851.. js:function:: Proxy.stop(px)
852
853 Stop the proxy. See the management socket documentation for more information.
854
855 :param class_proxy px: A :ref:`proxy_class` which indicates the manipulated
856 proxy.
857
858.. js:function:: Proxy.shut_bcksess(px)
859
860 Kill the session attached to a backup server. See the management socket
861 documentation for more information.
862
863 :param class_proxy px: A :ref:`proxy_class` which indicates the manipulated
864 proxy.
865
866.. js:function:: Proxy.get_cap(px)
867
868 Returns a string describing the capabilities of the proxy.
869
870 :param class_proxy px: A :ref:`proxy_class` which indicates the manipulated
871 proxy.
872 :returns: a string "frontend", "backend", "proxy" or "ruleset".
873
874.. js:function:: Proxy.get_mode(px)
875
876 Returns a string describing the mode of the current proxy.
877
878 :param class_proxy px: A :ref:`proxy_class` which indicates the manipulated
879 proxy.
880 :returns: a string "tcp", "http", "health" or "unknown"
881
882.. js:function:: Proxy.get_stats(px)
883
884 Returns an array containg the proxy statistics. The statistics returned are
885 not the same if the proxy is frontend or a backend.
886
887 :param class_proxy px: A :ref:`proxy_class` which indicates the manipulated
888 proxy.
889 :returns: a key/value array containing stats
890
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +0100891.. _server_class:
892
893Server class
894============
895
896.. js:function:: Server.is_draining(sv)
897
898 Return true if the server is currently draining stiky connections.
899
900 :param class_server sv: A :ref:`server_class` which indicates the manipulated
901 server.
902 :returns: a boolean
903
904.. js:function:: Server.set_weight(sv, weight)
905
906 Dynamically change the weight of the serveur. See the management socket
907 documentation for more information about the format of the string.
908
909 :param class_server sv: A :ref:`server_class` which indicates the manipulated
910 server.
911 :param string weight: A string describing the server weight.
912
913.. js:function:: Server.get_weight(sv)
914
915 This function returns an integer representing the serveur weight.
916
917 :param class_server sv: A :ref:`server_class` which indicates the manipulated
918 server.
919 :returns: an integer.
920
921.. js:function:: Server.set_addr(sv, addr)
922
923 Dynamically change the address of the serveur. See the management socket
924 documentation for more information about the format of the string.
925
926 :param class_server sv: A :ref:`server_class` which indicates the manipulated
927 server.
928 :param string weight: A string describing the server address.
929
930.. js:function:: Server.get_addr(sv)
931
932 Returns a string describing the address of the serveur.
933
934 :param class_server sv: A :ref:`server_class` which indicates the manipulated
935 server.
936 :returns: A string
937
938.. js:function:: Server.get_stats(sv)
939
940 Returns server statistics.
941
942 :param class_server sv: A :ref:`server_class` which indicates the manipulated
943 server.
944 :returns: a key/value array containing stats
945
946.. js:function:: Server.shut_sess(sv)
947
948 Shutdown all the sessions attached to the server. See the management socket
949 documentation for more information about this function.
950
951 :param class_server sv: A :ref:`server_class` which indicates the manipulated
952 server.
953
954.. js:function:: Server.set_drain(sv)
955
956 Drain sticky sessions. See the management socket documentation for more
957 information about this function.
958
959 :param class_server sv: A :ref:`server_class` which indicates the manipulated
960 server.
961
962.. js:function:: Server.set_maint(sv)
963
964 Set maintenance mode. See the management socket documentation for more
965 information about this function.
966
967 :param class_server sv: A :ref:`server_class` which indicates the manipulated
968 server.
969
970.. js:function:: Server.set_ready(sv)
971
972 Set normal mode. See the management socket documentation for more information
973 about this function.
974
975 :param class_server sv: A :ref:`server_class` which indicates the manipulated
976 server.
977
978.. js:function:: Server.check_enable(sv)
979
980 Enable health checks. See the management socket documentation for more
981 information about this function.
982
983 :param class_server sv: A :ref:`server_class` which indicates the manipulated
984 server.
985
986.. js:function:: Server.check_disable(sv)
987
988 Disable health checks. See the management socket documentation for more
989 information about this function.
990
991 :param class_server sv: A :ref:`server_class` which indicates the manipulated
992 server.
993
994.. js:function:: Server.check_force_up(sv)
995
996 Force health-check up. See the management socket documentation for more
997 information about this function.
998
999 :param class_server sv: A :ref:`server_class` which indicates the manipulated
1000 server.
1001
1002.. js:function:: Server.check_force_nolb(sv)
1003
1004 Force health-check nolb mode. See the management socket documentation for more
1005 information about this function.
1006
1007 :param class_server sv: A :ref:`server_class` which indicates the manipulated
1008 server.
1009
1010.. js:function:: Server.check_force_down(sv)
1011
1012 Force health-check down. See the management socket documentation for more
1013 information about this function.
1014
1015 :param class_server sv: A :ref:`server_class` which indicates the manipulated
1016 server.
1017
1018.. js:function:: Server.agent_enable(sv)
1019
1020 Enable agent check. See the management socket documentation for more
1021 information about this function.
1022
1023 :param class_server sv: A :ref:`server_class` which indicates the manipulated
1024 server.
1025
1026.. js:function:: Server.agent_disable(sv)
1027
1028 Disable agent check. See the management socket documentation for more
1029 information about this function.
1030
1031 :param class_server sv: A :ref:`server_class` which indicates the manipulated
1032 server.
1033
1034.. js:function:: Server.agent_force_up(sv)
1035
1036 Force agent check up. See the management socket documentation for more
1037 information about this function.
1038
1039 :param class_server sv: A :ref:`server_class` which indicates the manipulated
1040 server.
1041
1042.. js:function:: Server.agent_force_down(sv)
1043
1044 Force agent check down. See the management socket documentation for more
1045 information about this function.
1046
1047 :param class_server sv: A :ref:`server_class` which indicates the manipulated
1048 server.
1049
Thierry Fournierff480422016-02-25 08:36:46 +01001050.. _listener_class:
1051
1052Listener class
1053==============
1054
1055.. js:function:: Listener.get_stats(ls)
1056
1057 Returns server statistics.
1058
1059 :param class_listener ls: A :ref:`listener_class` which indicates the
1060 manipulated listener.
1061 :returns: a key/value array containing stats
1062
Thierry Fournier1de16592016-01-27 09:49:07 +01001063.. _concat_class:
1064
1065Concat class
1066============
1067
1068.. js:class:: Concat
1069
1070 This class provides a fast way for string concatenation. The way using native
1071 Lua concatenation like the code below is slow for some reasons.
1072
1073.. code-block:: lua
1074
1075 str = "string1"
1076 str = str .. ", string2"
1077 str = str .. ", string3"
1078..
1079
1080 For each concatenation, Lua:
1081 * allocate memory for the result,
1082 * catenate the two string copying the strings in the new memory bloc,
1083 * free the old memory block containing the string whoch is no longer used.
1084 This process does many memory move, allocation and free. In addition, the
1085 memory is not really freed, it is just mark mark as unsused and wait for the
1086 garbage collector.
1087
1088 The Concat class provide an alternative way for catenating strings. It uses
1089 the internal Lua mechanism (it does not allocate memory), but it doesn't copy
1090 the data more than once.
1091
1092 On my computer, the following loops spends 0.2s for the Concat method and
1093 18.5s for the pure Lua implementation. So, the Concat class is about 1000x
1094 faster than the embedded solution.
1095
1096.. code-block:: lua
1097
1098 for j = 1, 100 do
1099 c = core.concat()
1100 for i = 1, 20000 do
1101 c:add("#####")
1102 end
1103 end
1104..
1105
1106.. code-block:: lua
1107
1108 for j = 1, 100 do
1109 c = ""
1110 for i = 1, 20000 do
1111 c = c .. "#####"
1112 end
1113 end
1114..
1115
1116.. js:function:: Concat.add(concat, string)
1117
1118 This function adds a string to the current concatenated string.
1119
1120 :param class_concat concat: A :ref:`concat_class` which contains the currently
1121 builded string.
1122 :param string string: A new string to concatenate to the current builded
1123 string.
1124
1125.. js:function:: Concat.dump(concat)
1126
1127 This function returns the concanated string.
1128
1129 :param class_concat concat: A :ref:`concat_class` which contains the currently
1130 builded string.
1131 :returns: the concatenated string
1132
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001133.. _fetches_class:
1134
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001135Fetches class
1136=============
1137
1138.. js:class:: Fetches
1139
1140 This class contains a lot of internal HAProxy sample fetches. See the
Thierry FOURNIER2e4893c2015-03-18 13:37:27 +01001141 HAProxy "configuration.txt" documentation for more information about her
1142 usage. they are the chapters 7.3.2 to 7.3.6.
1143
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001144 **warning** some sample fetches are not available in some context. These
1145 limitations are specified in this documentation when theire useful.
1146
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01001147 :see: :js:attr:`TXN.f`
1148 :see: :js:attr:`TXN.sf`
Thierry FOURNIER2e4893c2015-03-18 13:37:27 +01001149
1150 Fetches are useful for:
1151
1152 * get system time,
1153 * get environment variable,
1154 * get random numbers,
1155 * known backend status like the number of users in queue or the number of
1156 connections established,
1157 * client information like ip source or destination,
1158 * deal with stick tables,
1159 * Established SSL informations,
1160 * HTTP information like headers or method.
1161
1162.. code-block:: lua
1163
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001164 function action(txn)
1165 -- Get source IP
1166 local clientip = txn.f:src()
1167 end
Thierry FOURNIER2e4893c2015-03-18 13:37:27 +01001168..
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001169
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001170.. _converters_class:
1171
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001172Converters class
1173================
1174
1175.. js:class:: Converters
1176
1177 This class contains a lot of internal HAProxy sample converters. See the
Thierry FOURNIER2e4893c2015-03-18 13:37:27 +01001178 HAProxy documentation "configuration.txt" for more information about her
1179 usage. Its the chapter 7.3.1.
1180
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01001181 :see: :js:attr:`TXN.c`
1182 :see: :js:attr:`TXN.sc`
Thierry FOURNIER2e4893c2015-03-18 13:37:27 +01001183
1184 Converters provides statefull transformation. They are useful for:
1185
1186 * converting input to base64,
1187 * applying hash on input string (djb2, crc32, sdbm, wt6),
1188 * format date,
1189 * json escape,
Pieter Baauw4d7f7662015-11-08 16:38:08 +01001190 * extracting preferred language comparing two lists,
Thierry FOURNIER2e4893c2015-03-18 13:37:27 +01001191 * turn to lower or upper chars,
1192 * deal with stick tables.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001193
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001194.. _channel_class:
1195
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001196Channel class
1197=============
1198
1199.. js:class:: Channel
1200
1201 HAProxy uses two buffers for the processing of the requests. The first one is
1202 used with the request data (from the client to the server) and the second is
1203 used for the response data (from the server to the client).
1204
1205 Each buffer contains two types of data. The first type is the incoming data
1206 waiting for a processing. The second part is the outgoing data already
1207 processed. Usually, the incoming data is processed, after it is tagged as
1208 outgoing data, and finally it is sent. The following functions provides tools
1209 for manipulating these data in a buffer.
1210
1211 The following diagram shows where the channel class function are applied.
1212
1213 **Warning**: It is not possible to read from the response in request action,
1214 and it is not possible to read for the request channel in response action.
1215
Thierry FOURNIER2e4893c2015-03-18 13:37:27 +01001216.. image:: _static/channel.png
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001217
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001218.. js:function:: Channel.dup(channel)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001219
1220 This function returns a string that contain the entire buffer. The data is
1221 not remove from the buffer and can be reprocessed later.
1222
1223 If the buffer cant receive more data, a 'nil' value is returned.
1224
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001225 :param class_channel channel: The manipulated Channel.
Pieter Baauw4d7f7662015-11-08 16:38:08 +01001226 :returns: a string containing all the available data or nil.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001227
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001228.. js:function:: Channel.get(channel)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001229
1230 This function returns a string that contain the entire buffer. The data is
1231 consumed from the buffer.
1232
1233 If the buffer cant receive more data, a 'nil' value is returned.
1234
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001235 :param class_channel channel: The manipulated Channel.
Pieter Baauw4d7f7662015-11-08 16:38:08 +01001236 :returns: a string containing all the available data or nil.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001237
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02001238.. js:function:: Channel.getline(channel)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001239
1240 This function returns a string that contain the first line of the buffer. The
1241 data is consumed. If the data returned doesn't contains a final '\n' its
1242 assumed than its the last available data in the buffer.
1243
1244 If the buffer cant receive more data, a 'nil' value is returned.
1245
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001246 :param class_channel channel: The manipulated Channel.
Pieter Baauw386a1272015-08-16 15:26:24 +02001247 :returns: a string containing the available line or nil.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001248
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001249.. js:function:: Channel.set(channel, string)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001250
1251 This function replace the content of the buffer by the string. The function
1252 returns the copied length, otherwise, it returns -1.
1253
1254 The data set with this function are not send. They wait for the end of
1255 HAProxy processing, so the buffer can be full.
1256
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001257 :param class_channel channel: The manipulated Channel.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001258 :param string string: The data which will sent.
Pieter Baauw4d7f7662015-11-08 16:38:08 +01001259 :returns: an integer containing the amount of bytes copied or -1.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001260
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001261.. js:function:: Channel.append(channel, string)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001262
1263 This function append the string argument to the content of the buffer. The
1264 function returns the copied length, otherwise, it returns -1.
1265
1266 The data set with this function are not send. They wait for the end of
1267 HAProxy processing, so the buffer can be full.
1268
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001269 :param class_channel channel: The manipulated Channel.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001270 :param string string: The data which will sent.
Pieter Baauw4d7f7662015-11-08 16:38:08 +01001271 :returns: an integer containing the amount of bytes copied or -1.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001272
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001273.. js:function:: Channel.send(channel, string)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001274
1275 This function required immediate send of the data. Unless if the connection
1276 is close, the buffer is regularly flushed and all the string can be sent.
1277
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001278 :param class_channel channel: The manipulated Channel.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001279 :param string string: The data which will sent.
Pieter Baauw4d7f7662015-11-08 16:38:08 +01001280 :returns: an integer containing the amount of bytes copied or -1.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001281
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001282.. js:function:: Channel.get_in_length(channel)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001283
1284 This function returns the length of the input part of the buffer.
1285
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001286 :param class_channel channel: The manipulated Channel.
Pieter Baauw4d7f7662015-11-08 16:38:08 +01001287 :returns: an integer containing the amount of available bytes.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001288
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001289.. js:function:: Channel.get_out_length(channel)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001290
1291 This function returns the length of the output part of the buffer.
1292
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001293 :param class_channel channel: The manipulated Channel.
Pieter Baauw4d7f7662015-11-08 16:38:08 +01001294 :returns: an integer containing the amount of available bytes.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001295
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001296.. js:function:: Channel.forward(channel, int)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001297
1298 This function transfer bytes from the input part of the buffer to the output
1299 part.
1300
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001301 :param class_channel channel: The manipulated Channel.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001302 :param integer int: The amount of data which will be forwarded.
1303
Thierry FOURNIER / OZON.IO65192f32016-11-07 15:28:40 +01001304.. js:function:: Channel.is_full(channel)
1305
1306 This function returns true if the buffer channel is full.
1307
1308 :returns: a boolean
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001309
1310.. _http_class:
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001311
1312HTTP class
1313==========
1314
1315.. js:class:: HTTP
1316
1317 This class contain all the HTTP manipulation functions.
1318
Pieter Baauw386a1272015-08-16 15:26:24 +02001319.. js:function:: HTTP.req_get_headers(http)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001320
1321 Returns an array containing all the request headers.
1322
1323 :param class_http http: The related http object.
1324 :returns: array of headers.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01001325 :see: :js:func:`HTTP.res_get_headers`
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001326
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001327 This is the form of the returned array:
1328
1329.. code-block:: lua
1330
1331 HTTP:req_get_headers()['<header-name>'][<header-index>] = "<header-value>"
1332
1333 local hdr = HTTP:req_get_headers()
1334 hdr["host"][0] = "www.test.com"
1335 hdr["accept"][0] = "audio/basic q=1"
1336 hdr["accept"][1] = "audio/*, q=0.2"
1337 hdr["accept"][2] = "*/*, q=0.1"
1338..
1339
Pieter Baauw386a1272015-08-16 15:26:24 +02001340.. js:function:: HTTP.res_get_headers(http)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001341
1342 Returns an array containing all the response headers.
1343
1344 :param class_http http: The related http object.
1345 :returns: array of headers.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01001346 :see: :js:func:`HTTP.req_get_headers`
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001347
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001348 This is the form of the returned array:
1349
1350.. code-block:: lua
1351
1352 HTTP:res_get_headers()['<header-name>'][<header-index>] = "<header-value>"
1353
1354 local hdr = HTTP:req_get_headers()
1355 hdr["host"][0] = "www.test.com"
1356 hdr["accept"][0] = "audio/basic q=1"
1357 hdr["accept"][1] = "audio/*, q=0.2"
1358 hdr["accept"][2] = "*.*, q=0.1"
1359..
1360
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001361.. js:function:: HTTP.req_add_header(http, name, value)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001362
1363 Appends an HTTP header field in the request whose name is
1364 specified in "name" and whose value is defined in "value".
1365
1366 :param class_http http: The related http object.
1367 :param string name: The header name.
1368 :param string value: The header value.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01001369 :see: :js:func:`HTTP.res_add_header`
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001370
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001371.. js:function:: HTTP.res_add_header(http, name, value)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001372
1373 appends an HTTP header field in the response whose name is
1374 specified in "name" and whose value is defined in "value".
1375
1376 :param class_http http: The related http object.
1377 :param string name: The header name.
1378 :param string value: The header value.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01001379 :see: :js:func:`HTTP.req_add_header`
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001380
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001381.. js:function:: HTTP.req_del_header(http, name)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001382
1383 Removes all HTTP header fields in the request whose name is
1384 specified in "name".
1385
1386 :param class_http http: The related http object.
1387 :param string name: The header name.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01001388 :see: :js:func:`HTTP.res_del_header`
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001389
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001390.. js:function:: HTTP.res_del_header(http, name)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001391
1392 Removes all HTTP header fields in the response whose name is
1393 specified in "name".
1394
1395 :param class_http http: The related http object.
1396 :param string name: The header name.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01001397 :see: :js:func:`HTTP.req_del_header`
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001398
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001399.. js:function:: HTTP.req_set_header(http, name, value)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001400
1401 This variable replace all occurence of all header "name", by only
1402 one containing the "value".
1403
1404 :param class_http http: The related http object.
1405 :param string name: The header name.
1406 :param string value: The header value.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01001407 :see: :js:func:`HTTP.res_set_header`
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001408
1409 This function does the same work as the folowwing code:
1410
1411.. code-block:: lua
1412
1413 function fcn(txn)
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001414 TXN.http:req_del_header("header")
1415 TXN.http:req_add_header("header", "value")
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001416 end
1417..
1418
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001419.. js:function:: HTTP.res_set_header(http, name, value)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001420
1421 This variable replace all occurence of all header "name", by only
1422 one containing the "value".
1423
1424 :param class_http http: The related http object.
1425 :param string name: The header name.
1426 :param string value: The header value.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01001427 :see: :js:func:`HTTP.req_rep_header()`
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001428
Pieter Baauw386a1272015-08-16 15:26:24 +02001429.. js:function:: HTTP.req_rep_header(http, name, regex, replace)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001430
1431 Matches the regular expression in all occurrences of header field "name"
1432 according to "regex", and replaces them with the "replace" argument. The
1433 replacement value can contain back references like \1, \2, ... This
1434 function works with the request.
1435
1436 :param class_http http: The related http object.
1437 :param string name: The header name.
1438 :param string regex: The match regular expression.
1439 :param string replace: The replacement value.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01001440 :see: :js:func:`HTTP.res_rep_header()`
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001441
Pieter Baauw386a1272015-08-16 15:26:24 +02001442.. js:function:: HTTP.res_rep_header(http, name, regex, string)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001443
1444 Matches the regular expression in all occurrences of header field "name"
1445 according to "regex", and replaces them with the "replace" argument. The
1446 replacement value can contain back references like \1, \2, ... This
1447 function works with the request.
1448
1449 :param class_http http: The related http object.
1450 :param string name: The header name.
1451 :param string regex: The match regular expression.
1452 :param string replace: The replacement value.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01001453 :see: :js:func:`HTTP.req_rep_header()`
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001454
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001455.. js:function:: HTTP.req_set_method(http, method)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001456
1457 Rewrites the request method with the parameter "method".
1458
1459 :param class_http http: The related http object.
1460 :param string method: The new method.
1461
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001462.. js:function:: HTTP.req_set_path(http, path)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001463
1464 Rewrites the request path with the "path" parameter.
1465
1466 :param class_http http: The related http object.
1467 :param string path: The new path.
1468
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001469.. js:function:: HTTP.req_set_query(http, query)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001470
1471 Rewrites the request's query string which appears after the first question
1472 mark ("?") with the parameter "query".
1473
1474 :param class_http http: The related http object.
1475 :param string query: The new query.
1476
Thierry FOURNIER0d79cf62015-08-26 14:20:58 +02001477.. js:function:: HTTP.req_set_uri(http, uri)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001478
1479 Rewrites the request URI with the parameter "uri".
1480
1481 :param class_http http: The related http object.
1482 :param string uri: The new uri.
1483
Robin H. Johnson52f5db22017-01-01 13:10:52 -08001484.. js:function:: HTTP.res_set_status(http, status [, reason])
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +02001485
Robin H. Johnson52f5db22017-01-01 13:10:52 -08001486 Rewrites the response status code with the parameter "code".
1487
1488 If no custom reason is provided, it will be generated from the status.
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +02001489
1490 :param class_http http: The related http object.
1491 :param integer status: The new response status code.
Robin H. Johnson52f5db22017-01-01 13:10:52 -08001492 :param string reason: The new response reason (optional).
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +02001493
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001494.. _txn_class:
1495
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001496TXN class
1497=========
1498
1499.. js:class:: TXN
1500
1501 The txn class contain all the functions relative to the http or tcp
1502 transaction (Note than a tcp stream is the same than a tcp transaction, but
1503 an HTTP transaction is not the same than a tcp stream).
1504
1505 The usage of this class permits to retrieve data from the requests, alter it
1506 and forward it.
1507
1508 All the functions provided by this class are available in the context
1509 **sample-fetches** and **actions**.
1510
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001511.. js:attribute:: TXN.c
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001512
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001513 :returns: An :ref:`converters_class`.
1514
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001515 This attribute contains a Converters class object.
1516
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001517.. js:attribute:: TXN.sc
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001518
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001519 :returns: An :ref:`converters_class`.
1520
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001521 This attribute contains a Converters class object. The functions of
1522 this object returns always a string.
1523
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001524.. js:attribute:: TXN.f
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001525
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001526 :returns: An :ref:`fetches_class`.
1527
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001528 This attribute contains a Fetches class object.
1529
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001530.. js:attribute:: TXN.sf
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001531
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001532 :returns: An :ref:`fetches_class`.
1533
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001534 This attribute contains a Fetches class object. The functions of
1535 this object returns always a string.
1536
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001537.. js:attribute:: TXN.req
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001538
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001539 :returns: An :ref:`channel_class`.
1540
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001541 This attribute contains a channel class object for the request buffer.
1542
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001543.. js:attribute:: TXN.res
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001544
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001545 :returns: An :ref:`channel_class`.
1546
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001547 This attribute contains a channel class object for the response buffer.
1548
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001549.. js:attribute:: TXN.http
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001550
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001551 :returns: An :ref:`http_class`.
1552
Thierry FOURNIER08504f42015-03-16 14:17:08 +01001553 This attribute contains an HTTP class object. It is avalaible only if the
1554 proxy has the "mode http" enabled.
1555
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01001556.. js:function:: TXN.log(TXN, loglevel, msg)
1557
1558 This function sends a log. The log is sent, according with the HAProxy
1559 configuration file, on the default syslog server if it is configured and on
1560 the stderr if it is allowed.
1561
1562 :param class_txn txn: The class txn object containing the data.
1563 :param integer loglevel: Is the log level asociated with the message. It is a
1564 number between 0 and 7.
1565 :param string msg: The log content.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01001566 :see: :js:attr:`core.emerg`, :js:attr:`core.alert`, :js:attr:`core.crit`,
1567 :js:attr:`core.err`, :js:attr:`core.warning`, :js:attr:`core.notice`,
1568 :js:attr:`core.info`, :js:attr:`core.debug` (log level definitions)
1569 :see: :js:func:`TXN.deflog`
1570 :see: :js:func:`TXN.Debug`
1571 :see: :js:func:`TXN.Info`
1572 :see: :js:func:`TXN.Warning`
1573 :see: :js:func:`TXN.Alert`
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01001574
1575.. js:function:: TXN.deflog(TXN, msg)
1576
1577 Sends a log line with the default loglevel for the proxy ssociated with the
1578 transaction.
1579
1580 :param class_txn txn: The class txn object containing the data.
1581 :param string msg: The log content.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01001582 :see: :js:func:`TXN.log
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01001583
1584.. js:function:: TXN.Debug(txn, msg)
1585
1586 :param class_txn txn: The class txn object containing the data.
1587 :param string msg: The log content.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01001588 :see: :js:func:`TXN.log`
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01001589
1590 Does the same job than:
1591
1592.. code-block:: lua
1593
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001594 function Debug(txn, msg)
1595 TXN.log(txn, core.debug, msg)
1596 end
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01001597..
1598
1599.. js:function:: TXN.Info(txn, msg)
1600
1601 :param class_txn txn: The class txn object containing the data.
1602 :param string msg: The log content.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01001603 :see: :js:func:`TXN.log`
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01001604
1605.. code-block:: lua
1606
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001607 function Debug(txn, msg)
1608 TXN.log(txn, core.info, msg)
1609 end
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01001610..
1611
1612.. js:function:: TXN.Warning(txn, msg)
1613
1614 :param class_txn txn: The class txn object containing the data.
1615 :param string msg: The log content.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01001616 :see: :js:func:`TXN.log`
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01001617
1618.. code-block:: lua
1619
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001620 function Debug(txn, msg)
1621 TXN.log(txn, core.warning, msg)
1622 end
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01001623..
1624
1625.. js:function:: TXN.Alert(txn, msg)
1626
1627 :param class_txn txn: The class txn object containing the data.
1628 :param string msg: The log content.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01001629 :see: :js:func:`TXN.log`
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01001630
1631.. code-block:: lua
1632
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001633 function Debug(txn, msg)
1634 TXN.log(txn, core.alert, msg)
1635 end
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01001636..
1637
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001638.. js:function:: TXN.get_priv(txn)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001639
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001640 Return Lua data stored in the current transaction (with the `TXN.set_priv()`)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001641 function. If no data are stored, it returns a nil value.
1642
1643 :param class_txn txn: The class txn object containing the data.
1644 :returns: the opaque data previsously stored, or nil if nothing is
1645 avalaible.
1646
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001647.. js:function:: TXN.set_priv(txn, data)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001648
1649 Store any data in the current HAProxy transaction. This action replace the
1650 old stored data.
1651
1652 :param class_txn txn: The class txn object containing the data.
1653 :param opaque data: The data which is stored in the transaction.
1654
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +02001655.. js:function:: TXN.set_var(TXN, var, value)
1656
David Carlier61fdf8b2015-10-02 11:59:38 +01001657 Converts a Lua type in a HAProxy type and store it in a variable <var>.
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +02001658
1659 :param class_txn txn: The class txn object containing the data.
1660 :param string var: The variable name according with the HAProxy variable syntax.
Thierry FOURNIER / OZON.IOb210bcc2016-12-12 16:24:16 +01001661 :param type value: The value associated to the variable. The type can be string or
1662 integer.
Christopher Faulet85d79c92016-11-09 16:54:56 +01001663
1664.. js:function:: TXN.unset_var(TXN, var)
1665
1666 Unset the variable <var>.
1667
1668 :param class_txn txn: The class txn object containing the data.
1669 :param string var: The variable name according with the HAProxy variable syntax.
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +02001670
1671.. js:function:: TXN.get_var(TXN, var)
1672
1673 Returns data stored in the variable <var> converter in Lua type.
1674
1675 :param class_txn txn: The class txn object containing the data.
1676 :param string var: The variable name according with the HAProxy variable syntax.
1677
Willy Tarreaubc183a62015-08-28 10:39:11 +02001678.. js:function:: TXN.done(txn)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001679
Willy Tarreaubc183a62015-08-28 10:39:11 +02001680 This function terminates processing of the transaction and the associated
1681 session. It can be used when a critical error is detected or to terminate
1682 processing after some data have been returned to the client (eg: a redirect).
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001683
Thierry FOURNIERab00df62016-07-14 11:42:37 +02001684 *Warning*: It not make sense to call this function from sample-fetches. In
1685 this case the behaviour of this one is the same than core.done(): it quit
1686 the Lua execution. The transaction is really aborted only from an action
1687 registered function.
1688
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001689 :param class_txn txn: The class txn object containing the data.
1690
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001691.. js:function:: TXN.set_loglevel(txn, loglevel)
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01001692
1693 Is used to change the log level of the current request. The "loglevel" must
1694 be an integer between 0 and 7.
1695
1696 :param class_txn txn: The class txn object containing the data.
1697 :param integer loglevel: The required log level. This variable can be one of
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01001698 :see: :js:attr:`core.emerg`, :js:attr:`core.alert`, :js:attr:`core.crit`,
1699 :js:attr:`core.err`, :js:attr:`core.warning`, :js:attr:`core.notice`,
1700 :js:attr:`core.info`, :js:attr:`core.debug` (log level definitions)
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01001701
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001702.. js:function:: TXN.set_tos(txn, tos)
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01001703
1704 Is used to set the TOS or DSCP field value of packets sent to the client to
1705 the value passed in "tos" on platforms which support this.
1706
1707 :param class_txn txn: The class txn object containing the data.
1708 :param integer tos: The new TOS os DSCP.
1709
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001710.. js:function:: TXN.set_mark(txn, mark)
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01001711
1712 Is used to set the Netfilter MARK on all packets sent to the client to the
1713 value passed in "mark" on platforms which support it.
1714
1715 :param class_txn txn: The class txn object containing the data.
1716 :param integer mark: The mark value.
1717
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001718.. _socket_class:
1719
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001720Socket class
1721============
1722
1723.. js:class:: Socket
1724
1725 This class must be compatible with the Lua Socket class. Only the 'client'
1726 functions are available. See the Lua Socket documentation:
1727
1728 `http://w3.impa.br/~diego/software/luasocket/tcp.html
1729 <http://w3.impa.br/~diego/software/luasocket/tcp.html>`_
1730
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001731.. js:function:: Socket.close(socket)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001732
1733 Closes a TCP object. The internal socket used by the object is closed and the
1734 local address to which the object was bound is made available to other
1735 applications. No further operations (except for further calls to the close
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001736 method) are allowed on a closed Socket.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001737
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001738 :param class_socket socket: Is the manipulated Socket.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001739
1740 Note: It is important to close all used sockets once they are not needed,
1741 since, in many systems, each socket uses a file descriptor, which are limited
1742 system resources. Garbage-collected objects are automatically closed before
1743 destruction, though.
1744
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02001745.. js:function:: Socket.connect(socket, address[, port])
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001746
1747 Attempts to connect a socket object to a remote host.
1748
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001749
1750 In case of error, the method returns nil followed by a string describing the
1751 error. In case of success, the method returns 1.
1752
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001753 :param class_socket socket: Is the manipulated Socket.
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02001754 :param string address: can be an IP address or a host name. See below for more
1755 information.
1756 :param integer port: must be an integer number in the range [1..64K].
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001757 :returns: 1 or nil.
1758
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02001759 an address field extension permits to use the connect() function to connect to
1760 other stream than TCP. The syntax containing a simpleipv4 or ipv6 address is
1761 the basically expected format. This format requires the port.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001762
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02001763 Other format accepted are a socket path like "/socket/path", it permits to
1764 connect to a socket. abstract namespaces are supported with the prefix
1765 "abns@", and finaly a filedescriotr can be passed with the prefix "fd@".
1766 The prefix "ipv4@", "ipv6@" and "unix@" are also supported. The port can be
1767 passed int the string. The syntax "127.0.0.1:1234" is valid. in this case, the
1768 parameter *port* is ignored.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001769
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001770.. js:function:: Socket.connect_ssl(socket, address, port)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001771
1772 Same behavior than the function socket:connect, but uses SSL.
1773
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001774 :param class_socket socket: Is the manipulated Socket.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001775 :returns: 1 or nil.
1776
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001777.. js:function:: Socket.getpeername(socket)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001778
1779 Returns information about the remote side of a connected client object.
1780
1781 Returns a string with the IP address of the peer, followed by the port number
1782 that peer is using for the connection. In case of error, the method returns
1783 nil.
1784
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001785 :param class_socket socket: Is the manipulated Socket.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001786 :returns: a string containing the server information.
1787
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001788.. js:function:: Socket.getsockname(socket)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001789
1790 Returns the local address information associated to the object.
1791
1792 The method returns a string with local IP address and a number with the port.
1793 In case of error, the method returns nil.
1794
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001795 :param class_socket socket: Is the manipulated Socket.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001796 :returns: a string containing the client information.
1797
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001798.. js:function:: Socket.receive(socket, [pattern [, prefix]])
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001799
1800 Reads data from a client object, according to the specified read pattern.
1801 Patterns follow the Lua file I/O format, and the difference in performance
1802 between all patterns is negligible.
1803
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001804 :param class_socket socket: Is the manipulated Socket.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001805 :param string|integer pattern: Describe what is required (see below).
1806 :param string prefix: A string which will be prefix the returned data.
1807 :returns: a string containing the required data or nil.
1808
1809 Pattern can be any of the following:
1810
1811 * **`*a`**: reads from the socket until the connection is closed. No
1812 end-of-line translation is performed;
1813
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001814 * **`*l`**: reads a line of text from the Socket. The line is terminated by a
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001815 LF character (ASCII 10), optionally preceded by a CR character
1816 (ASCII 13). The CR and LF characters are not included in the
1817 returned line. In fact, all CR characters are ignored by the
1818 pattern. This is the default pattern.
1819
1820 * **number**: causes the method to read a specified number of bytes from the
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001821 Socket. Prefix is an optional string to be concatenated to the
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001822 beginning of any received data before return.
1823
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02001824 * **empty**: If the pattern is left empty, the default option is `*l`.
1825
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001826 If successful, the method returns the received pattern. In case of error, the
1827 method returns nil followed by an error message which can be the string
1828 'closed' in case the connection was closed before the transmission was
1829 completed or the string 'timeout' in case there was a timeout during the
1830 operation. Also, after the error message, the function returns the partial
1831 result of the transmission.
1832
1833 Important note: This function was changed severely. It used to support
1834 multiple patterns (but I have never seen this feature used) and now it
1835 doesn't anymore. Partial results used to be returned in the same way as
1836 successful results. This last feature violated the idea that all functions
1837 should return nil on error. Thus it was changed too.
1838
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001839.. js:function:: Socket.send(socket, data [, start [, end ]])
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001840
1841 Sends data through client object.
1842
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001843 :param class_socket socket: Is the manipulated Socket.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001844 :param string data: The data that will be sent.
1845 :param integer start: The start position in the buffer of the data which will
1846 be sent.
1847 :param integer end: The end position in the buffer of the data which will
1848 be sent.
1849 :returns: see below.
1850
1851 Data is the string to be sent. The optional arguments i and j work exactly
1852 like the standard string.sub Lua function to allow the selection of a
1853 substring to be sent.
1854
1855 If successful, the method returns the index of the last byte within [start,
1856 end] that has been sent. Notice that, if start is 1 or absent, this is
1857 effectively the total number of bytes sent. In case of error, the method
1858 returns nil, followed by an error message, followed by the index of the last
1859 byte within [start, end] that has been sent. You might want to try again from
1860 the byte following that. The error message can be 'closed' in case the
1861 connection was closed before the transmission was completed or the string
1862 'timeout' in case there was a timeout during the operation.
1863
1864 Note: Output is not buffered. For small strings, it is always better to
1865 concatenate them in Lua (with the '..' operator) and send the result in one
1866 call instead of calling the method several times.
1867
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001868.. js:function:: Socket.setoption(socket, option [, value])
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001869
1870 Just implemented for compatibility, this cal does nothing.
1871
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001872.. js:function:: Socket.settimeout(socket, value [, mode])
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001873
1874 Changes the timeout values for the object. All I/O operations are blocking.
1875 That is, any call to the methods send, receive, and accept will block
1876 indefinitely, until the operation completes. The settimeout method defines a
1877 limit on the amount of time the I/O methods can block. When a timeout time
1878 has elapsed, the affected methods give up and fail with an error code.
1879
1880 The amount of time to wait is specified as the value parameter, in seconds.
1881
1882 The timeout modes are bot implemented, the only settable timeout is the
1883 inactivity time waiting for complete the internal buffer send or waiting for
1884 receive data.
1885
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001886 :param class_socket socket: Is the manipulated Socket.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001887 :param integer value: The timeout value.
1888
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001889.. _map_class:
1890
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001891Map class
1892=========
1893
1894.. js:class:: Map
1895
1896 This class permits to do some lookup in HAProxy maps. The declared maps can
1897 be modified during the runtime throught the HAProxy management socket.
1898
1899.. code-block:: lua
1900
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001901 default = "usa"
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001902
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001903 -- Create and load map
Thierry FOURNIER4dc71972017-01-28 08:33:08 +01001904 geo = Map.new("geo.map", Map._ip);
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001905
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001906 -- Create new fetch that returns the user country
1907 core.register_fetches("country", function(txn)
1908 local src;
1909 local loc;
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001910
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001911 src = txn.f:fhdr("x-forwarded-for");
1912 if (src == nil) then
1913 src = txn.f:src()
1914 if (src == nil) then
1915 return default;
1916 end
1917 end
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001918
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001919 -- Perform lookup
1920 loc = geo:lookup(src);
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001921
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001922 if (loc == nil) then
1923 return default;
1924 end
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001925
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001926 return loc;
1927 end);
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001928
Thierry FOURNIER4dc71972017-01-28 08:33:08 +01001929.. js:attribute:: Map._int
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001930
1931 See the HAProxy configuration.txt file, chapter "Using ACLs and fetching
1932 samples" ans subchapter "ACL basics" to understand this pattern matching
1933 method.
1934
Thierry FOURNIER4dc71972017-01-28 08:33:08 +01001935 Note that :js:attr:`Map.int` is also available for compatibility.
1936
1937.. js:attribute:: Map._ip
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001938
1939 See the HAProxy configuration.txt file, chapter "Using ACLs and fetching
1940 samples" ans subchapter "ACL basics" to understand this pattern matching
1941 method.
1942
Thierry FOURNIER4dc71972017-01-28 08:33:08 +01001943 Note that :js:attr:`Map.ip` is also available for compatibility.
1944
1945.. js:attribute:: Map._str
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001946
1947 See the HAProxy configuration.txt file, chapter "Using ACLs and fetching
1948 samples" ans subchapter "ACL basics" to understand this pattern matching
1949 method.
1950
Thierry FOURNIER4dc71972017-01-28 08:33:08 +01001951 Note that :js:attr:`Map.str` is also available for compatibility.
1952
1953.. js:attribute:: Map._beg
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001954
1955 See the HAProxy configuration.txt file, chapter "Using ACLs and fetching
1956 samples" ans subchapter "ACL basics" to understand this pattern matching
1957 method.
1958
Thierry FOURNIER4dc71972017-01-28 08:33:08 +01001959 Note that :js:attr:`Map.beg` is also available for compatibility.
1960
1961.. js:attribute:: Map._sub
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001962
1963 See the HAProxy configuration.txt file, chapter "Using ACLs and fetching
1964 samples" ans subchapter "ACL basics" to understand this pattern matching
1965 method.
1966
Thierry FOURNIER4dc71972017-01-28 08:33:08 +01001967 Note that :js:attr:`Map.sub` is also available for compatibility.
1968
1969.. js:attribute:: Map._dir
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001970
1971 See the HAProxy configuration.txt file, chapter "Using ACLs and fetching
1972 samples" ans subchapter "ACL basics" to understand this pattern matching
1973 method.
1974
Thierry FOURNIER4dc71972017-01-28 08:33:08 +01001975 Note that :js:attr:`Map.dir` is also available for compatibility.
1976
1977.. js:attribute:: Map._dom
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001978
1979 See the HAProxy configuration.txt file, chapter "Using ACLs and fetching
1980 samples" ans subchapter "ACL basics" to understand this pattern matching
1981 method.
1982
Thierry FOURNIER4dc71972017-01-28 08:33:08 +01001983 Note that :js:attr:`Map.dom` is also available for compatibility.
1984
1985.. js:attribute:: Map._end
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001986
1987 See the HAProxy configuration.txt file, chapter "Using ACLs and fetching
1988 samples" ans subchapter "ACL basics" to understand this pattern matching
1989 method.
1990
Thierry FOURNIER4dc71972017-01-28 08:33:08 +01001991.. js:attribute:: Map._reg
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001992
1993 See the HAProxy configuration.txt file, chapter "Using ACLs and fetching
1994 samples" ans subchapter "ACL basics" to understand this pattern matching
1995 method.
1996
Thierry FOURNIER4dc71972017-01-28 08:33:08 +01001997 Note that :js:attr:`Map.reg` is also available for compatibility.
1998
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001999
2000.. js:function:: Map.new(file, method)
2001
2002 Creates and load a map.
2003
2004 :param string file: Is the file containing the map.
2005 :param integer method: Is the map pattern matching method. See the attributes
2006 of the Map class.
2007 :returns: a class Map object.
Thierry FOURNIER4dc71972017-01-28 08:33:08 +01002008 :see: The Map attributes: :js:attr:`Map._int`, :js:attr:`Map._ip`,
2009 :js:attr:`Map._str`, :js:attr:`Map._beg`, :js:attr:`Map._sub`,
2010 :js:attr:`Map._dir`, :js:attr:`Map._dom`, :js:attr:`Map._end` and
2011 :js:attr:`Map._reg`.
Thierry FOURNIER3def3932015-04-07 11:27:54 +02002012
2013.. js:function:: Map.lookup(map, str)
2014
2015 Perform a lookup in a map.
2016
2017 :param class_map map: Is the class Map object.
2018 :param string str: Is the string used as key.
2019 :returns: a string containing the result or nil if no match.
2020
2021.. js:function:: Map.slookup(map, str)
2022
2023 Perform a lookup in a map.
2024
2025 :param class_map map: Is the class Map object.
2026 :param string str: Is the string used as key.
2027 :returns: a string containing the result or empty string if no match.
2028
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002029.. _applethttp_class:
2030
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002031AppletHTTP class
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002032================
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002033
2034.. js:class:: AppletHTTP
2035
2036 This class is used with applets that requires the 'http' mode. The http applet
2037 can be registered with the *core.register_service()* function. They are used
2038 for processing an http request like a server in back of HAProxy.
2039
2040 This is an hello world sample code:
2041
2042.. code-block:: lua
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002043
Pieter Baauw4d7f7662015-11-08 16:38:08 +01002044 core.register_service("hello-world", "http", function(applet)
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002045 local response = "Hello World !"
2046 applet:set_status(200)
Pieter Baauw2dcb9bc2015-10-01 22:47:12 +02002047 applet:add_header("content-length", string.len(response))
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002048 applet:add_header("content-type", "text/plain")
Pieter Baauw2dcb9bc2015-10-01 22:47:12 +02002049 applet:start_response()
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002050 applet:send(response)
2051 end)
2052
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002053.. js:attribute:: AppletHTTP.c
2054
2055 :returns: A :ref:`converters_class`
2056
2057 This attribute contains a Converters class object.
2058
2059.. js:attribute:: AppletHTTP.sc
2060
2061 :returns: A :ref:`converters_class`
2062
2063 This attribute contains a Converters class object. The
2064 functions of this object returns always a string.
2065
2066.. js:attribute:: AppletHTTP.f
2067
2068 :returns: A :ref:`fetches_class`
2069
2070 This attribute contains a Fetches class object. Note that the
2071 applet execution place cannot access to a valid HAProxy core HTTP
2072 transaction, so some sample fecthes related to the HTTP dependant
2073 values (hdr, path, ...) are not available.
2074
2075.. js:attribute:: AppletHTTP.sf
2076
2077 :returns: A :ref:`fetches_class`
2078
2079 This attribute contains a Fetches class object. The functions of
2080 this object returns always a string. Note that the applet
2081 execution place cannot access to a valid HAProxy core HTTP
2082 transaction, so some sample fecthes related to the HTTP dependant
2083 values (hdr, path, ...) are not available.
2084
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01002085.. js:attribute:: AppletHTTP.method
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002086
2087 :returns: string
2088
2089 The attribute method returns a string containing the HTTP
2090 method.
2091
2092.. js:attribute:: AppletHTTP.version
2093
2094 :returns: string
2095
2096 The attribute version, returns a string containing the HTTP
2097 request version.
2098
2099.. js:attribute:: AppletHTTP.path
2100
2101 :returns: string
2102
2103 The attribute path returns a string containing the HTTP
2104 request path.
2105
2106.. js:attribute:: AppletHTTP.qs
2107
2108 :returns: string
2109
2110 The attribute qs returns a string containing the HTTP
2111 request query string.
2112
2113.. js:attribute:: AppletHTTP.length
2114
2115 :returns: integer
2116
2117 The attribute length returns an integer containing the HTTP
2118 body length.
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002119
Thierry FOURNIER841475e2015-12-11 17:10:09 +01002120.. js:attribute:: AppletHTTP.headers
2121
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002122 :returns: array
2123
2124 The attribute headers returns an array containing the HTTP
2125 headers. The header names are always in lower case. As the header name can be
2126 encountered more than once in each request, the value is indexed with 0 as
2127 first index value. The array have this form:
2128
2129.. code-block:: lua
2130
2131 AppletHTTP.headers['<header-name>'][<header-index>] = "<header-value>"
2132
2133 AppletHTTP.headers["host"][0] = "www.test.com"
2134 AppletHTTP.headers["accept"][0] = "audio/basic q=1"
2135 AppletHTTP.headers["accept"][1] = "audio/*, q=0.2"
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01002136 AppletHTTP.headers["accept"][2] = "*/*, q=0.1"
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002137..
2138
Robin H. Johnson52f5db22017-01-01 13:10:52 -08002139.. js:function:: AppletHTTP.set_status(applet, code [, reason])
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002140
2141 This function sets the HTTP status code for the response. The allowed code are
2142 from 100 to 599.
2143
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01002144 :param class_AppletHTTP applet: An :ref:`applethttp_class`
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002145 :param integer code: the status code returned to the client.
Robin H. Johnson52f5db22017-01-01 13:10:52 -08002146 :param string reason: the status reason returned to the client (optional).
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002147
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01002148.. js:function:: AppletHTTP.add_header(applet, name, value)
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002149
2150 This function add an header in the response. Duplicated headers are not
2151 collapsed. The special header *content-length* is used to determinate the
2152 response length. If it not exists, a *transfer-encoding: chunked* is set, and
2153 all the write from the funcion *AppletHTTP:send()* become a chunk.
2154
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01002155 :param class_AppletHTTP applet: An :ref:`applethttp_class`
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002156 :param string name: the header name
2157 :param string value: the header value
2158
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01002159.. js:function:: AppletHTTP.start_response(applet)
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002160
2161 This function indicates to the HTTP engine that it can process and send the
2162 response headers. After this called we cannot add headers to the response; We
2163 cannot use the *AppletHTTP:send()* function if the
2164 *AppletHTTP:start_response()* is not called.
2165
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01002166 :param class_AppletHTTP applet: An :ref:`applethttp_class`
2167
2168.. js:function:: AppletHTTP.getline(applet)
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002169
2170 This function returns a string containing one line from the http body. If the
2171 data returned doesn't contains a final '\\n' its assumed than its the last
2172 available data before the end of stream.
2173
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01002174 :param class_AppletHTTP applet: An :ref:`applethttp_class`
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002175 :returns: a string. The string can be empty if we reach the end of the stream.
2176
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01002177.. js:function:: AppletHTTP.receive(applet, [size])
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002178
2179 Reads data from the HTTP body, according to the specified read *size*. If the
2180 *size* is missing, the function tries to read all the content of the stream
2181 until the end. If the *size* is bigger than the http body, it returns the
2182 amount of data avalaible.
2183
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01002184 :param class_AppletHTTP applet: An :ref:`applethttp_class`
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002185 :param integer size: the required read size.
2186 :returns: always return a string,the string can be empty is the connexion is
2187 closed.
2188
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01002189.. js:function:: AppletHTTP.send(applet, msg)
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002190
2191 Send the message *msg* on the http request body.
2192
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01002193 :param class_AppletHTTP applet: An :ref:`applethttp_class`
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002194 :param string msg: the message to send.
2195
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01002196.. js:function:: AppletHTTP.get_priv(applet)
2197
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01002198 Return Lua data stored in the current transaction. If no data are stored,
2199 it returns a nil value.
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01002200
2201 :param class_AppletHTTP applet: An :ref:`applethttp_class`
2202 :returns: the opaque data previsously stored, or nil if nothing is
2203 avalaible.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01002204 :see: :js:func:`AppletHTTP.set_priv`
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01002205
2206.. js:function:: AppletHTTP.set_priv(applet, data)
2207
2208 Store any data in the current HAProxy transaction. This action replace the
2209 old stored data.
2210
2211 :param class_AppletHTTP applet: An :ref:`applethttp_class`
2212 :param opaque data: The data which is stored in the transaction.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01002213 :see: :js:func:`AppletHTTP.get_priv`
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01002214
Thierry FOURNIER / OZON.IOc1edafe2016-12-12 16:25:30 +01002215.. js:function:: AppletHTTP.set_var(applet, var, value)
2216
2217 Converts a Lua type in a HAProxy type and store it in a variable <var>.
2218
2219 :param class_AppletHTTP applet: An :ref:`applethttp_class`
2220 :param string var: The variable name according with the HAProxy variable syntax.
2221 :param type value: The value associated to the variable. The type ca be string or
2222 integer.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01002223 :see: :js:func:`AppletHTTP.unset_var`
2224 :see: :js:func:`AppletHTTP.get_var`
Thierry FOURNIER / OZON.IOc1edafe2016-12-12 16:25:30 +01002225
2226.. js:function:: AppletHTTP.unset_var(applet, var)
2227
2228 Unset the variable <var>.
2229
2230 :param class_AppletHTTP applet: An :ref:`applethttp_class`
2231 :param string var: The variable name according with the HAProxy variable syntax.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01002232 :see: :js:func:`AppletHTTP.set_var`
2233 :see: :js:func:`AppletHTTP.get_var`
Thierry FOURNIER / OZON.IOc1edafe2016-12-12 16:25:30 +01002234
2235.. js:function:: AppletHTTP.get_var(applet, var)
2236
2237 Returns data stored in the variable <var> converter in Lua type.
2238
2239 :param class_AppletHTTP applet: An :ref:`applethttp_class`
2240 :param string var: The variable name according with the HAProxy variable syntax.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01002241 :see: :js:func:`AppletHTTP.set_var`
2242 :see: :js:func:`AppletHTTP.unset_var`
Thierry FOURNIER / OZON.IOc1edafe2016-12-12 16:25:30 +01002243
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002244.. _applettcp_class:
2245
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002246AppletTCP class
2247===============
2248
2249.. js:class:: AppletTCP
2250
2251 This class is used with applets that requires the 'tcp' mode. The tcp applet
2252 can be registered with the *core.register_service()* function. They are used
2253 for processing a tcp stream like a server in back of HAProxy.
2254
Thierry FOURNIERdc595002015-12-21 11:13:52 +01002255.. js:attribute:: AppletTCP.c
2256
2257 :returns: A :ref:`converters_class`
2258
2259 This attribute contains a Converters class object.
2260
2261.. js:attribute:: AppletTCP.sc
2262
2263 :returns: A :ref:`converters_class`
2264
2265 This attribute contains a Converters class object. The
2266 functions of this object returns always a string.
2267
2268.. js:attribute:: AppletTCP.f
2269
2270 :returns: A :ref:`fetches_class`
2271
2272 This attribute contains a Fetches class object.
2273
2274.. js:attribute:: AppletTCP.sf
2275
2276 :returns: A :ref:`fetches_class`
2277
2278 This attribute contains a Fetches class object.
2279
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01002280.. js:function:: AppletTCP.getline(applet)
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002281
2282 This function returns a string containing one line from the stream. If the
2283 data returned doesn't contains a final '\\n' its assumed than its the last
2284 available data before the end of stream.
2285
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01002286 :param class_AppletTCP applet: An :ref:`applettcp_class`
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002287 :returns: a string. The string can be empty if we reach the end of the stream.
2288
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01002289.. js:function:: AppletTCP.receive(applet, [size])
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002290
2291 Reads data from the TCP stream, according to the specified read *size*. If the
2292 *size* is missing, the function tries to read all the content of the stream
2293 until the end.
2294
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01002295 :param class_AppletTCP applet: An :ref:`applettcp_class`
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002296 :param integer size: the required read size.
2297 :returns: always return a string,the string can be empty is the connexion is
2298 closed.
2299
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01002300.. js:function:: AppletTCP.send(appletmsg)
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002301
2302 Send the message on the stream.
2303
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01002304 :param class_AppletTCP applet: An :ref:`applettcp_class`
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02002305 :param string msg: the message to send.
2306
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01002307.. js:function:: AppletTCP.get_priv(applet)
2308
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01002309 Return Lua data stored in the current transaction. If no data are stored,
2310 it returns a nil value.
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01002311
2312 :param class_AppletTCP applet: An :ref:`applettcp_class`
2313 :returns: the opaque data previsously stored, or nil if nothing is
2314 avalaible.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01002315 :see: :js:func:`AppletTCP.set_priv`
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01002316
2317.. js:function:: AppletTCP.set_priv(applet, data)
2318
2319 Store any data in the current HAProxy transaction. This action replace the
2320 old stored data.
2321
2322 :param class_AppletTCP applet: An :ref:`applettcp_class`
2323 :param opaque data: The data which is stored in the transaction.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01002324 :see: :js:func:`AppletTCP.get_priv`
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01002325
Thierry FOURNIER / OZON.IOc1edafe2016-12-12 16:25:30 +01002326.. js:function:: AppletTCP.set_var(applet, var, value)
2327
2328 Converts a Lua type in a HAProxy type and stores it in a variable <var>.
2329
2330 :param class_AppletTCP applet: An :ref:`applettcp_class`
2331 :param string var: The variable name according with the HAProxy variable syntax.
2332 :param type value: The value associated to the variable. The type can be string or
2333 integer.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01002334 :see: :js:func:`AppletTCP.unset_var`
2335 :see: :js:func:`AppletTCP.get_var`
Thierry FOURNIER / OZON.IOc1edafe2016-12-12 16:25:30 +01002336
2337.. js:function:: AppletTCP.unset_var(applet, var)
2338
2339 Unsets the variable <var>.
2340
2341 :param class_AppletTCP applet: An :ref:`applettcp_class`
2342 :param string var: The variable name according with the HAProxy variable syntax.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01002343 :see: :js:func:`AppletTCP.unset_var`
2344 :see: :js:func:`AppletTCP.set_var`
Thierry FOURNIER / OZON.IOc1edafe2016-12-12 16:25:30 +01002345
2346.. js:function:: AppletTCP.get_var(applet, var)
2347
2348 Returns data stored in the variable <var> converter in Lua type.
2349
2350 :param class_AppletTCP applet: An :ref:`applettcp_class`
2351 :param string var: The variable name according with the HAProxy variable syntax.
Thierry FOURNIER12a865d2016-12-14 19:40:37 +01002352 :see: :js:func:`AppletTCP.unset_var`
2353 :see: :js:func:`AppletTCP.set_var`
Thierry FOURNIER / OZON.IOc1edafe2016-12-12 16:25:30 +01002354
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01002355External Lua libraries
2356======================
2357
2358A lot of useful lua libraries can be found here:
2359
2360* `https://lua-toolbox.com/ <https://lua-toolbox.com/>`_
2361
2362Redis acces:
2363
2364* `https://github.com/nrk/redis-lua <https://github.com/nrk/redis-lua>`_
2365
2366This is an example about the usage of the Redis library with HAProxy. Note that
2367each call of any function of this library can throw an error if the socket
2368connection fails.
2369
2370.. code-block:: lua
2371
2372 -- load the redis library
2373 local redis = require("redis");
2374
2375 function do_something(txn)
2376
2377 -- create and connect new tcp socket
2378 local tcp = core.tcp();
2379 tcp:settimeout(1);
2380 tcp:connect("127.0.0.1", 6379);
2381
2382 -- use the redis library with this new socket
2383 local client = redis.connect({socket=tcp});
2384 client:ping();
2385
2386 end
2387
2388OpenSSL:
2389
2390* `http://mkottman.github.io/luacrypto/index.html
2391 <http://mkottman.github.io/luacrypto/index.html>`_
2392
2393* `https://github.com/brunoos/luasec/wiki
2394 <https://github.com/brunoos/luasec/wiki>`_
Thierry FOURNIER2e4893c2015-03-18 13:37:27 +01002395