blob: 6adb72ef63f73aa093c40d724923d30b0716e3d0 [file] [log] [blame]
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001.. toctree::
2 :maxdepth: 2
3
4
5How Lua runs in HAProxy
6=======================
7
8HAProxy Lua running contexts
9----------------------------
10
11The Lua code executed in HAProxy can be processed in 2 main modes. The first one
12is the **initialisation mode**, and the second is the **runtime mode**.
13
14* In the **initialisation mode**, we can perform DNS solves, but we cannot
15 perform socket I/O. In this initialisation mode, HAProxy still blocked during
16 the execution of the Lua program.
17
18* In the **runtime mode**, we cannot perform DNS solves, but we can use sockets.
19 The execution of the Lua code is multiplexed with the requests processing, so
20 the Lua code seems to be run in blocking, but it is not the case.
21
22The Lua code is loaded in one or more files. These files contains main code and
23functions. Lua have 6 execution context.
24
251. The Lua file **body context**. It is executed during the load of the Lua file
26 in the HAProxy `[global]` section with the directive `lua-load`. It is
27 executed in initialisation mode. This section is use for configuring Lua
28 bindings in HAProxy.
29
David Carlier61fdf8b2015-10-02 11:59:38 +0100302. The Lua **init context**. It is a Lua function executed just after the
Thierry FOURNIER17bd1522015-03-11 20:31:00 +010031 HAProxy configuration parsing. The execution is in initialisation mode. In
32 this context the HAProxy environment are already initialized. It is useful to
33 check configuration, or initializing socket connections or tasks. These
34 functions are declared in the body context with the Lua function
35 `core.register_init()`. The prototype of the function is a simple function
36 without return value and without parameters, like this: `function fcn()`.
37
David Carlier61fdf8b2015-10-02 11:59:38 +0100383. The Lua **task context**. It is a Lua function executed after the start
Thierry FOURNIER17bd1522015-03-11 20:31:00 +010039 of the HAProxy scheduler, and just after the declaration of the task with the
40 Lua function `core.register_task()`. This context can be concurrent with the
41 traffic processing. It is executed in runtime mode. The prototype of the
42 function is a simple function without return value and without parameters,
43 like this: `function fcn()`.
44
David Carlier61fdf8b2015-10-02 11:59:38 +0100454. The **action context**. It is a Lua function conditionally executed. These
Thierry FOURNIERa2d02252015-10-01 15:00:42 +020046 actions are registered by the Lua directives "`core.register_action()`". The
47 prototype of the Lua called function is a function with doesn't returns
48 anything and that take an object of class TXN as entry. `function fcn(txn)`.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +010049
505. The **sample-fetch context**. This function takes a TXN object as entry
51 argument and returns a string. These types of function cannot execute any
52 blocking function. They are useful to aggregate some of original HAProxy
53 sample-fetches and return the result. The prototype of the function is
54 `function string fcn(txn)`. These functions can be registered with the Lua
55 function `core.register_fetches()`. Each declared sample-fetch is prefixed by
56 the string "lua.".
57
58 **NOTE**: It is possible that this function cannot found the required data
59 in the original HAProxy sample-fetches, in this case, it cannot return the
60 result. This case is not yet supported
61
David Carlier61fdf8b2015-10-02 11:59:38 +0100626. The **converter context**. It is a Lua function that takes a string as input
Thierry FOURNIER17bd1522015-03-11 20:31:00 +010063 and returns another string as output. These types of function are stateless,
64 it cannot access to any context. They don't execute any blocking function.
65 The call prototype is `function string fcn(string)`. This function can be
66 registered with the Lua function `core.register_converters()`. Each declared
67 converter is prefixed by the string "lua.".
68
69HAProxy Lua Hello world
70-----------------------
71
72HAProxy configuration file (`hello_world.conf`):
73
74::
75
76 global
77 lua-load hello_world.lua
78
79 listen proxy
80 bind 127.0.0.1:10001
Thierry FOURNIERa2d02252015-10-01 15:00:42 +020081 tcp-request inspect-delay 1s
82 tcp-request content use-service lua.hello_world
Thierry FOURNIER17bd1522015-03-11 20:31:00 +010083
84HAProxy Lua file (`hello_world.lua`):
85
86.. code-block:: lua
87
Thierry FOURNIERa2d02252015-10-01 15:00:42 +020088 core.register_service("hello_world", "tcp", function(applet)
89 applet:send("hello world\n")
90 end)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +010091
92How to start HAProxy for testing this configuration:
93
94::
95
96 ./haproxy -f hello_world.conf
97
98On other terminal, you can test with telnet:
99
100::
101
102 #:~ telnet 127.0.0.1 10001
103 hello world
104
105Core class
106==========
107
108.. js:class:: core
109
110 The "core" class contains all the HAProxy core functions. These function are
111 useful for the controlling the execution flow, registering hooks, manipulating
112 global maps or ACL, ...
113
114 "core" class is basically provided with HAProxy. No `require` line is
115 required to uses these function.
116
David Carlier61fdf8b2015-10-02 11:59:38 +0100117 The "core" class is static, it is not possible to create a new object of this
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100118 type.
119
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100120.. js:attribute:: core.emerg
121
Thierry FOURNIERdc595002015-12-21 11:13:52 +0100122 :returns: integer
123
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100124 This attribute is an integer, it contains the value of the loglevel "emergency" (0).
125
126.. js:attribute:: core.alert
127
Thierry FOURNIERdc595002015-12-21 11:13:52 +0100128 :returns: integer
129
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100130 This attribute is an integer, it contains the value of the loglevel "alert" (1).
131
132.. js:attribute:: core.crit
133
Thierry FOURNIERdc595002015-12-21 11:13:52 +0100134 :returns: integer
135
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100136 This attribute is an integer, it contains the value of the loglevel "critical" (2).
137
138.. js:attribute:: core.err
139
Thierry FOURNIERdc595002015-12-21 11:13:52 +0100140 :returns: integer
141
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100142 This attribute is an integer, it contains the value of the loglevel "error" (3).
143
144.. js:attribute:: core.warning
145
Thierry FOURNIERdc595002015-12-21 11:13:52 +0100146 :returns: integer
147
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100148 This attribute is an integer, it contains the value of the loglevel "warning" (4).
149
150.. js:attribute:: core.notice
151
Thierry FOURNIERdc595002015-12-21 11:13:52 +0100152 :returns: integer
153
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100154 This attribute is an integer, it contains the value of the loglevel "notice" (5).
155
156.. js:attribute:: core.info
157
Thierry FOURNIERdc595002015-12-21 11:13:52 +0100158 :returns: integer
159
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100160 This attribute is an integer, it contains the value of the loglevel "info" (6).
161
162.. js:attribute:: core.debug
163
Thierry FOURNIERdc595002015-12-21 11:13:52 +0100164 :returns: integer
165
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100166 This attribute is an integer, it contains the value of the loglevel "debug" (7).
167
168.. js:function:: core.log(loglevel, msg)
169
170 **context**: body, init, task, action, sample-fetch, converter
171
David Carlier61fdf8b2015-10-02 11:59:38 +0100172 This function sends a log. The log is sent, according with the HAProxy
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100173 configuration file, on the default syslog server if it is configured and on
174 the stderr if it is allowed.
175
176 :param integer loglevel: Is the log level asociated with the message. It is a
177 number between 0 and 7.
178 :param string msg: The log content.
179 :see: core.emerg, core.alert, core.crit, core.err, core.warning, core.notice,
180 core.info, core.debug (log level definitions)
181 :see: code.Debug
182 :see: core.Info
183 :see: core.Warning
184 :see: core.Alert
185
186.. js:function:: core.Debug(msg)
187
188 **context**: body, init, task, action, sample-fetch, converter
189
190 :param string msg: The log content.
191 :see: log
192
193 Does the same job than:
194
195.. code-block:: lua
196
Thierry FOURNIERdc595002015-12-21 11:13:52 +0100197 function Debug(msg)
198 core.log(core.debug, msg)
199 end
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100200..
201
202.. js:function:: core.Info(msg)
203
204 **context**: body, init, task, action, sample-fetch, converter
205
206 :param string msg: The log content.
207 :see: log
208
209.. code-block:: lua
210
Thierry FOURNIERdc595002015-12-21 11:13:52 +0100211 function Info(msg)
212 core.log(core.info, msg)
213 end
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100214..
215
216.. js:function:: core.Warning(msg)
217
218 **context**: body, init, task, action, sample-fetch, converter
219
220 :param string msg: The log content.
221 :see: log
222
223.. code-block:: lua
224
Thierry FOURNIERdc595002015-12-21 11:13:52 +0100225 function Warning(msg)
226 core.log(core.warning, msg)
227 end
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100228..
229
230.. js:function:: core.Alert(msg)
231
232 **context**: body, init, task, action, sample-fetch, converter
233
234 :param string msg: The log content.
235 :see: log
236
237.. code-block:: lua
238
Thierry FOURNIERdc595002015-12-21 11:13:52 +0100239 function Alert(msg)
240 core.log(core.alert, msg)
241 end
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100242..
243
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100244.. js:function:: core.add_acl(filename, key)
245
246 **context**: init, task, action, sample-fetch, converter
247
248 Add the ACL *key* in the ACLs list referenced by the file *filename*.
249
250 :param string filename: the filename that reference the ACL entries.
251 :param string key: the key which will be added.
252
253.. js:function:: core.del_acl(filename, key)
254
255 **context**: init, task, action, sample-fetch, converter
256
257 Delete the ACL entry referenced by the key *key* in the list of ACLs
258 referenced by *filename*.
259
260 :param string filename: the filename that reference the ACL entries.
261 :param string key: the key which will be deleted.
262
263.. js:function:: core.del_map(filename, key)
264
265 **context**: init, task, action, sample-fetch, converter
266
267 Delete the map entry indexed with the specified key in the list of maps
268 referenced by his filename.
269
270 :param string filename: the filename that reference the map entries.
271 :param string key: the key which will be deleted.
272
273.. js:function:: core.msleep(milliseconds)
274
275 **context**: body, init, task, action
276
277 The `core.msleep()` stops the Lua execution between specified milliseconds.
278
279 :param integer milliseconds: the required milliseconds.
280
Thierry FOURNIER8255a752015-09-23 21:03:35 +0200281.. js:function:: core.register_action(name, actions, func)
282
283 **context**: body
284
David Carlier61fdf8b2015-10-02 11:59:38 +0100285 Register a Lua function executed as action. All the registered action can be
Thierry FOURNIER8255a752015-09-23 21:03:35 +0200286 used in HAProxy with the prefix "lua.". An action gets a TXN object class as
287 input.
288
289 :param string name: is the name of the converter.
Willy Tarreau61add3c2015-09-28 15:39:10 +0200290 :param table actions: is a table of string describing the HAProxy actions who
Thierry FOURNIER8255a752015-09-23 21:03:35 +0200291 want to register to. The expected actions are 'tcp-req',
292 'tcp-res', 'http-req' or 'http-res'.
293 :param function func: is the Lua function called to work as converter.
294
295 The prototype of the Lua function used as argument is:
296
297.. code-block:: lua
298
299 function(txn)
300..
301
Thierry FOURNIERdc595002015-12-21 11:13:52 +0100302 * **txn** (:ref:`txn_class`): this is a TXN object used for manipulating the
Thierry FOURNIER8255a752015-09-23 21:03:35 +0200303 current request or TCP stream.
304
Willy Tarreau61add3c2015-09-28 15:39:10 +0200305 Here, an exemple of action registration. the action juste send an 'Hello world'
Thierry FOURNIER8255a752015-09-23 21:03:35 +0200306 in the logs.
307
308.. code-block:: lua
309
310 core.register_action("hello-world", { "tcp-req", "http-req" }, function(txn)
311 txn:Info("Hello world")
312 end)
313..
314
315 This example code is used in HAproxy configuration like this:
316
317::
318
319 frontend tcp_frt
320 mode tcp
321 tcp-request content lua.hello-world
322
323 frontend http_frt
324 mode http
325 http-request lua.hello-world
326
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100327.. js:function:: core.register_converters(name, func)
328
329 **context**: body
330
David Carlier61fdf8b2015-10-02 11:59:38 +0100331 Register a Lua function executed as converter. All the registered converters
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100332 can be used in HAProxy with the prefix "lua.". An converter get a string as
333 input and return a string as output. The registered function can take up to 9
334 values as parameter. All the value are strings.
335
336 :param string name: is the name of the converter.
337 :param function func: is the Lua function called to work as converter.
338
339 The prototype of the Lua function used as argument is:
340
341.. code-block:: lua
342
343 function(str, [p1 [, p2 [, ... [, p5]]]])
344..
345
346 * **str** (*string*): this is the input value automatically converted in
347 string.
348 * **p1** .. **p5** (*string*): this is a list of string arguments declared in
349 the haroxy configuration file. The number of arguments doesn't exceed 5.
350 The order and the nature of these is conventionally choose by the
351 developper.
352
353.. js:function:: core.register_fetches(name, func)
354
355 **context**: body
356
David Carlier61fdf8b2015-10-02 11:59:38 +0100357 Register a Lua function executed as sample fetch. All the registered sample
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100358 fetchs can be used in HAProxy with the prefix "lua.". A Lua sample fetch
359 return a string as output. The registered function can take up to 9 values as
360 parameter. All the value are strings.
361
362 :param string name: is the name of the converter.
363 :param function func: is the Lua function called to work as sample fetch.
364
365 The prototype of the Lua function used as argument is:
366
367.. code-block:: lua
368
369 string function(txn, [p1 [, p2 [, ... [, p5]]]])
370..
371
Thierry FOURNIERdc595002015-12-21 11:13:52 +0100372 * **txn** (:ref:`txn_class`): this is the txn object associated with the current
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100373 request.
374 * **p1** .. **p5** (*string*): this is a list of string arguments declared in
375 the haroxy configuration file. The number of arguments doesn't exceed 5.
376 The order and the nature of these is conventionally choose by the
377 developper.
378 * **Returns**: A string containing some data, ot nil if the value cannot be
379 returned now.
380
381 lua example code:
382
383.. code-block:: lua
384
385 core.register_fetches("hello", function(txn)
386 return "hello"
387 end)
388..
389
390 HAProxy example configuration:
391
392::
393
394 frontend example
395 http-request redirect location /%[lua.hello]
396
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +0200397.. js:function:: core.register_service(name, mode, func)
398
399 **context**: body
400
David Carlier61fdf8b2015-10-02 11:59:38 +0100401 Register a Lua function executed as a service. All the registered service can
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +0200402 be used in HAProxy with the prefix "lua.". A service gets an object class as
403 input according with the required mode.
404
405 :param string name: is the name of the converter.
Willy Tarreau61add3c2015-09-28 15:39:10 +0200406 :param string mode: is string describing the required mode. Only 'tcp' or
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +0200407 'http' are allowed.
408 :param function func: is the Lua function called to work as converter.
409
410 The prototype of the Lua function used as argument is:
411
412.. code-block:: lua
413
414 function(applet)
415..
416
Thierry FOURNIERdc595002015-12-21 11:13:52 +0100417 * **applet** *applet* will be a :ref:`applettcp_class` or a
418 :ref:`applethttp_class`. It depends the type of registered applet. An applet
419 registered with the 'http' value for the *mode* parameter will gets a
420 :ref:`applethttp_class`. If the *mode* value is 'tcp', the applet will gets
421 a :ref:`applettcp_class`.
422
423 **warning**: Applets of type 'http' cannot be called from 'tcp-*'
424 rulesets. Only the 'http-*' rulesets are authorized, this means
425 that is not possible to call an HTTP applet from a proxy in tcp
426 mode. Applets of type 'tcp' can be called from anywhre.
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +0200427
Willy Tarreau61add3c2015-09-28 15:39:10 +0200428 Here, an exemple of service registration. the service just send an 'Hello world'
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +0200429 as an http response.
430
431.. code-block:: lua
432
Pieter Baauw4d7f7662015-11-08 16:38:08 +0100433 core.register_service("hello-world", "http", function(applet)
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +0200434 local response = "Hello World !"
435 applet:set_status(200)
Pieter Baauw2dcb9bc2015-10-01 22:47:12 +0200436 applet:add_header("content-length", string.len(response))
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +0200437 applet:add_header("content-type", "text/plain")
Pieter Baauw2dcb9bc2015-10-01 22:47:12 +0200438 applet:start_response()
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +0200439 applet:send(response)
440 end)
441..
442
443 This example code is used in HAproxy configuration like this:
444
445::
446
447 frontend example
448 http-request use-service lua.hello-world
449
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100450.. js:function:: core.register_init(func)
451
452 **context**: body
453
454 Register a function executed after the configuration parsing. This is useful
455 to check any parameters.
456
Pieter Baauw4d7f7662015-11-08 16:38:08 +0100457 :param function func: is the Lua function called to work as initializer.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100458
459 The prototype of the Lua function used as argument is:
460
461.. code-block:: lua
462
463 function()
464..
465
466 It takes no input, and no output is expected.
467
468.. js:function:: core.register_task(func)
469
470 **context**: body, init, task, action, sample-fetch, converter
471
472 Register and start independent task. The task is started when the HAProxy
473 main scheduler starts. For example this type of tasks can be executed to
Thierry FOURNIER2e4893c2015-03-18 13:37:27 +0100474 perform complex health checks.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100475
Pieter Baauw4d7f7662015-11-08 16:38:08 +0100476 :param function func: is the Lua function called to work as initializer.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100477
478 The prototype of the Lua function used as argument is:
479
480.. code-block:: lua
481
482 function()
483..
484
485 It takes no input, and no output is expected.
486
487.. js:function:: core.set_nice(nice)
488
489 **context**: task, action, sample-fetch, converter
490
491 Change the nice of the current task or current session.
Thierry FOURNIER2e4893c2015-03-18 13:37:27 +0100492
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100493 :param integer nice: the nice value, it must be between -1024 and 1024.
494
495.. js:function:: core.set_map(filename, key, value)
496
497 **context**: init, task, action, sample-fetch, converter
498
499 set the value *value* associated to the key *key* in the map referenced by
500 *filename*.
501
Thierry FOURNIERdc595002015-12-21 11:13:52 +0100502 :param string filename: the Map reference
503 :param string key: the key to set or replace
504 :param string value: the associated value
505
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100506.. js:function:: core.sleep(int seconds)
507
508 **context**: body, init, task, action
509
510 The `core.sleep()` functions stop the Lua execution between specified seconds.
511
512 :param integer seconds: the required seconds.
513
514.. js:function:: core.tcp()
515
516 **context**: init, task, action
517
518 This function returns a new object of a *socket* class.
519
Thierry FOURNIERdc595002015-12-21 11:13:52 +0100520 :returns: A :ref:`socket_class` object.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100521
Thierry FOURNIER0a99b892015-08-26 00:14:17 +0200522.. js:function:: core.done(data)
523
524 **context**: body, init, task, action, sample-fetch, converter
525
526 :param any data: Return some data for the caller. It is useful with
527 sample-fetches and sample-converters.
528
529 Immediately stops the current Lua execution and returns to the caller which
530 may be a sample fetch, a converter or an action and returns the specified
531 value (ignored for actions). It is used when the LUA process finishes its
532 work and wants to give back the control to HAProxy without executing the
533 remaining code. It can be seen as a multi-level "return".
534
Thierry FOURNIER486f5a02015-03-16 15:13:03 +0100535.. js:function:: core.yield()
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100536
537 **context**: task, action, sample-fetch, converter
538
539 Give back the hand at the HAProxy scheduler. It is used when the LUA
540 processing consumes a lot of processing time.
541
Thierry FOURNIERdc595002015-12-21 11:13:52 +0100542.. _fetches_class:
543
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100544Fetches class
545=============
546
547.. js:class:: Fetches
548
549 This class contains a lot of internal HAProxy sample fetches. See the
Thierry FOURNIER2e4893c2015-03-18 13:37:27 +0100550 HAProxy "configuration.txt" documentation for more information about her
551 usage. they are the chapters 7.3.2 to 7.3.6.
552
Thierry FOURNIERdc595002015-12-21 11:13:52 +0100553 **warning** some sample fetches are not available in some context. These
554 limitations are specified in this documentation when theire useful.
555
Thierry FOURNIER2e4893c2015-03-18 13:37:27 +0100556 :see: TXN.f
557 :see: TXN.sf
558
559 Fetches are useful for:
560
561 * get system time,
562 * get environment variable,
563 * get random numbers,
564 * known backend status like the number of users in queue or the number of
565 connections established,
566 * client information like ip source or destination,
567 * deal with stick tables,
568 * Established SSL informations,
569 * HTTP information like headers or method.
570
571.. code-block:: lua
572
Thierry FOURNIERdc595002015-12-21 11:13:52 +0100573 function action(txn)
574 -- Get source IP
575 local clientip = txn.f:src()
576 end
Thierry FOURNIER2e4893c2015-03-18 13:37:27 +0100577..
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100578
Thierry FOURNIERdc595002015-12-21 11:13:52 +0100579.. _converters_class:
580
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100581Converters class
582================
583
584.. js:class:: Converters
585
586 This class contains a lot of internal HAProxy sample converters. See the
Thierry FOURNIER2e4893c2015-03-18 13:37:27 +0100587 HAProxy documentation "configuration.txt" for more information about her
588 usage. Its the chapter 7.3.1.
589
590 :see: TXN.c
591 :see: TXN.sc
592
593 Converters provides statefull transformation. They are useful for:
594
595 * converting input to base64,
596 * applying hash on input string (djb2, crc32, sdbm, wt6),
597 * format date,
598 * json escape,
Pieter Baauw4d7f7662015-11-08 16:38:08 +0100599 * extracting preferred language comparing two lists,
Thierry FOURNIER2e4893c2015-03-18 13:37:27 +0100600 * turn to lower or upper chars,
601 * deal with stick tables.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100602
Thierry FOURNIERdc595002015-12-21 11:13:52 +0100603.. _channel_class:
604
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100605Channel class
606=============
607
608.. js:class:: Channel
609
610 HAProxy uses two buffers for the processing of the requests. The first one is
611 used with the request data (from the client to the server) and the second is
612 used for the response data (from the server to the client).
613
614 Each buffer contains two types of data. The first type is the incoming data
615 waiting for a processing. The second part is the outgoing data already
616 processed. Usually, the incoming data is processed, after it is tagged as
617 outgoing data, and finally it is sent. The following functions provides tools
618 for manipulating these data in a buffer.
619
620 The following diagram shows where the channel class function are applied.
621
622 **Warning**: It is not possible to read from the response in request action,
623 and it is not possible to read for the request channel in response action.
624
Thierry FOURNIER2e4893c2015-03-18 13:37:27 +0100625.. image:: _static/channel.png
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100626
Thierry FOURNIER486f5a02015-03-16 15:13:03 +0100627.. js:function:: Channel.dup(channel)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100628
629 This function returns a string that contain the entire buffer. The data is
630 not remove from the buffer and can be reprocessed later.
631
632 If the buffer cant receive more data, a 'nil' value is returned.
633
Thierry FOURNIER486f5a02015-03-16 15:13:03 +0100634 :param class_channel channel: The manipulated Channel.
Pieter Baauw4d7f7662015-11-08 16:38:08 +0100635 :returns: a string containing all the available data or nil.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100636
Thierry FOURNIER486f5a02015-03-16 15:13:03 +0100637.. js:function:: Channel.get(channel)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100638
639 This function returns a string that contain the entire buffer. The data is
640 consumed from the buffer.
641
642 If the buffer cant receive more data, a 'nil' value is returned.
643
Thierry FOURNIER486f5a02015-03-16 15:13:03 +0100644 :param class_channel channel: The manipulated Channel.
Pieter Baauw4d7f7662015-11-08 16:38:08 +0100645 :returns: a string containing all the available data or nil.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100646
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +0200647.. js:function:: Channel.getline(channel)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100648
649 This function returns a string that contain the first line of the buffer. The
650 data is consumed. If the data returned doesn't contains a final '\n' its
651 assumed than its the last available data in the buffer.
652
653 If the buffer cant receive more data, a 'nil' value is returned.
654
Thierry FOURNIER486f5a02015-03-16 15:13:03 +0100655 :param class_channel channel: The manipulated Channel.
Pieter Baauw386a1272015-08-16 15:26:24 +0200656 :returns: a string containing the available line or nil.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100657
Thierry FOURNIER486f5a02015-03-16 15:13:03 +0100658.. js:function:: Channel.set(channel, string)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100659
660 This function replace the content of the buffer by the string. The function
661 returns the copied length, otherwise, it returns -1.
662
663 The data set with this function are not send. They wait for the end of
664 HAProxy processing, so the buffer can be full.
665
Thierry FOURNIER486f5a02015-03-16 15:13:03 +0100666 :param class_channel channel: The manipulated Channel.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100667 :param string string: The data which will sent.
Pieter Baauw4d7f7662015-11-08 16:38:08 +0100668 :returns: an integer containing the amount of bytes copied or -1.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100669
Thierry FOURNIER486f5a02015-03-16 15:13:03 +0100670.. js:function:: Channel.append(channel, string)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100671
672 This function append the string argument to the content of the buffer. The
673 function returns the copied length, otherwise, it returns -1.
674
675 The data set with this function are not send. They wait for the end of
676 HAProxy processing, so the buffer can be full.
677
Thierry FOURNIER486f5a02015-03-16 15:13:03 +0100678 :param class_channel channel: The manipulated Channel.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100679 :param string string: The data which will sent.
Pieter Baauw4d7f7662015-11-08 16:38:08 +0100680 :returns: an integer containing the amount of bytes copied or -1.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100681
Thierry FOURNIER486f5a02015-03-16 15:13:03 +0100682.. js:function:: Channel.send(channel, string)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100683
684 This function required immediate send of the data. Unless if the connection
685 is close, the buffer is regularly flushed and all the string can be sent.
686
Thierry FOURNIER486f5a02015-03-16 15:13:03 +0100687 :param class_channel channel: The manipulated Channel.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100688 :param string string: The data which will sent.
Pieter Baauw4d7f7662015-11-08 16:38:08 +0100689 :returns: an integer containing the amount of bytes copied or -1.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100690
Thierry FOURNIER486f5a02015-03-16 15:13:03 +0100691.. js:function:: Channel.get_in_length(channel)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100692
693 This function returns the length of the input part of the buffer.
694
Thierry FOURNIER486f5a02015-03-16 15:13:03 +0100695 :param class_channel channel: The manipulated Channel.
Pieter Baauw4d7f7662015-11-08 16:38:08 +0100696 :returns: an integer containing the amount of available bytes.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100697
Thierry FOURNIER486f5a02015-03-16 15:13:03 +0100698.. js:function:: Channel.get_out_length(channel)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100699
700 This function returns the length of the output part of the buffer.
701
Thierry FOURNIER486f5a02015-03-16 15:13:03 +0100702 :param class_channel channel: The manipulated Channel.
Pieter Baauw4d7f7662015-11-08 16:38:08 +0100703 :returns: an integer containing the amount of available bytes.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100704
Thierry FOURNIER486f5a02015-03-16 15:13:03 +0100705.. js:function:: Channel.forward(channel, int)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100706
707 This function transfer bytes from the input part of the buffer to the output
708 part.
709
Thierry FOURNIER486f5a02015-03-16 15:13:03 +0100710 :param class_channel channel: The manipulated Channel.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100711 :param integer int: The amount of data which will be forwarded.
712
Thierry FOURNIERdc595002015-12-21 11:13:52 +0100713
714.. _http_class:
Thierry FOURNIER08504f42015-03-16 14:17:08 +0100715
716HTTP class
717==========
718
719.. js:class:: HTTP
720
721 This class contain all the HTTP manipulation functions.
722
Pieter Baauw386a1272015-08-16 15:26:24 +0200723.. js:function:: HTTP.req_get_headers(http)
Thierry FOURNIER08504f42015-03-16 14:17:08 +0100724
725 Returns an array containing all the request headers.
726
727 :param class_http http: The related http object.
728 :returns: array of headers.
Pieter Baauw386a1272015-08-16 15:26:24 +0200729 :see: HTTP.res_get_headers()
Thierry FOURNIER08504f42015-03-16 14:17:08 +0100730
Thierry FOURNIERdc595002015-12-21 11:13:52 +0100731 This is the form of the returned array:
732
733.. code-block:: lua
734
735 HTTP:req_get_headers()['<header-name>'][<header-index>] = "<header-value>"
736
737 local hdr = HTTP:req_get_headers()
738 hdr["host"][0] = "www.test.com"
739 hdr["accept"][0] = "audio/basic q=1"
740 hdr["accept"][1] = "audio/*, q=0.2"
741 hdr["accept"][2] = "*/*, q=0.1"
742..
743
Pieter Baauw386a1272015-08-16 15:26:24 +0200744.. js:function:: HTTP.res_get_headers(http)
Thierry FOURNIER08504f42015-03-16 14:17:08 +0100745
746 Returns an array containing all the response headers.
747
748 :param class_http http: The related http object.
749 :returns: array of headers.
Pieter Baauw386a1272015-08-16 15:26:24 +0200750 :see: HTTP.req_get_headers()
Thierry FOURNIER08504f42015-03-16 14:17:08 +0100751
Thierry FOURNIERdc595002015-12-21 11:13:52 +0100752 This is the form of the returned array:
753
754.. code-block:: lua
755
756 HTTP:res_get_headers()['<header-name>'][<header-index>] = "<header-value>"
757
758 local hdr = HTTP:req_get_headers()
759 hdr["host"][0] = "www.test.com"
760 hdr["accept"][0] = "audio/basic q=1"
761 hdr["accept"][1] = "audio/*, q=0.2"
762 hdr["accept"][2] = "*.*, q=0.1"
763..
764
Thierry FOURNIER486f5a02015-03-16 15:13:03 +0100765.. js:function:: HTTP.req_add_header(http, name, value)
Thierry FOURNIER08504f42015-03-16 14:17:08 +0100766
767 Appends an HTTP header field in the request whose name is
768 specified in "name" and whose value is defined in "value".
769
770 :param class_http http: The related http object.
771 :param string name: The header name.
772 :param string value: The header value.
Thierry FOURNIER486f5a02015-03-16 15:13:03 +0100773 :see: HTTP.res_add_header()
Thierry FOURNIER08504f42015-03-16 14:17:08 +0100774
Thierry FOURNIER486f5a02015-03-16 15:13:03 +0100775.. js:function:: HTTP.res_add_header(http, name, value)
Thierry FOURNIER08504f42015-03-16 14:17:08 +0100776
777 appends an HTTP header field in the response whose name is
778 specified in "name" and whose value is defined in "value".
779
780 :param class_http http: The related http object.
781 :param string name: The header name.
782 :param string value: The header value.
Thierry FOURNIER486f5a02015-03-16 15:13:03 +0100783 :see: HTTP.req_add_header()
Thierry FOURNIER08504f42015-03-16 14:17:08 +0100784
Thierry FOURNIER486f5a02015-03-16 15:13:03 +0100785.. js:function:: HTTP.req_del_header(http, name)
Thierry FOURNIER08504f42015-03-16 14:17:08 +0100786
787 Removes all HTTP header fields in the request whose name is
788 specified in "name".
789
790 :param class_http http: The related http object.
791 :param string name: The header name.
Thierry FOURNIER486f5a02015-03-16 15:13:03 +0100792 :see: HTTP.res_del_header()
Thierry FOURNIER08504f42015-03-16 14:17:08 +0100793
Thierry FOURNIER486f5a02015-03-16 15:13:03 +0100794.. js:function:: HTTP.res_del_header(http, name)
Thierry FOURNIER08504f42015-03-16 14:17:08 +0100795
796 Removes all HTTP header fields in the response whose name is
797 specified in "name".
798
799 :param class_http http: The related http object.
800 :param string name: The header name.
Thierry FOURNIER486f5a02015-03-16 15:13:03 +0100801 :see: HTTP.req_del_header()
Thierry FOURNIER08504f42015-03-16 14:17:08 +0100802
Thierry FOURNIER486f5a02015-03-16 15:13:03 +0100803.. js:function:: HTTP.req_set_header(http, name, value)
Thierry FOURNIER08504f42015-03-16 14:17:08 +0100804
805 This variable replace all occurence of all header "name", by only
806 one containing the "value".
807
808 :param class_http http: The related http object.
809 :param string name: The header name.
810 :param string value: The header value.
Thierry FOURNIER486f5a02015-03-16 15:13:03 +0100811 :see: HTTP.res_set_header()
Thierry FOURNIER08504f42015-03-16 14:17:08 +0100812
813 This function does the same work as the folowwing code:
814
815.. code-block:: lua
816
817 function fcn(txn)
Thierry FOURNIER486f5a02015-03-16 15:13:03 +0100818 TXN.http:req_del_header("header")
819 TXN.http:req_add_header("header", "value")
Thierry FOURNIER08504f42015-03-16 14:17:08 +0100820 end
821..
822
Thierry FOURNIER486f5a02015-03-16 15:13:03 +0100823.. js:function:: HTTP.res_set_header(http, name, value)
Thierry FOURNIER08504f42015-03-16 14:17:08 +0100824
825 This variable replace all occurence of all header "name", by only
826 one containing the "value".
827
828 :param class_http http: The related http object.
829 :param string name: The header name.
830 :param string value: The header value.
Pieter Baauw386a1272015-08-16 15:26:24 +0200831 :see: HTTP.req_rep_header()
Thierry FOURNIER08504f42015-03-16 14:17:08 +0100832
Pieter Baauw386a1272015-08-16 15:26:24 +0200833.. js:function:: HTTP.req_rep_header(http, name, regex, replace)
Thierry FOURNIER08504f42015-03-16 14:17:08 +0100834
835 Matches the regular expression in all occurrences of header field "name"
836 according to "regex", and replaces them with the "replace" argument. The
837 replacement value can contain back references like \1, \2, ... This
838 function works with the request.
839
840 :param class_http http: The related http object.
841 :param string name: The header name.
842 :param string regex: The match regular expression.
843 :param string replace: The replacement value.
Pieter Baauw386a1272015-08-16 15:26:24 +0200844 :see: HTTP.res_rep_header()
Thierry FOURNIER08504f42015-03-16 14:17:08 +0100845
Pieter Baauw386a1272015-08-16 15:26:24 +0200846.. js:function:: HTTP.res_rep_header(http, name, regex, string)
Thierry FOURNIER08504f42015-03-16 14:17:08 +0100847
848 Matches the regular expression in all occurrences of header field "name"
849 according to "regex", and replaces them with the "replace" argument. The
850 replacement value can contain back references like \1, \2, ... This
851 function works with the request.
852
853 :param class_http http: The related http object.
854 :param string name: The header name.
855 :param string regex: The match regular expression.
856 :param string replace: The replacement value.
Thierry FOURNIER486f5a02015-03-16 15:13:03 +0100857 :see: HTTP.req_replace_header()
Thierry FOURNIER08504f42015-03-16 14:17:08 +0100858
Thierry FOURNIER486f5a02015-03-16 15:13:03 +0100859.. js:function:: HTTP.req_replace_value(http, name, regex, replace)
Thierry FOURNIER08504f42015-03-16 14:17:08 +0100860
Thierry FOURNIER486f5a02015-03-16 15:13:03 +0100861 Works like "HTTP.req_replace_header()" except that it matches the regex
Thierry FOURNIER08504f42015-03-16 14:17:08 +0100862 against every comma-delimited value of the header field "name" instead of the
863 entire header.
864
865 :param class_http http: The related http object.
866 :param string name: The header name.
867 :param string regex: The match regular expression.
868 :param string replace: The replacement value.
Thierry FOURNIER486f5a02015-03-16 15:13:03 +0100869 :see: HTTP.req_replace_header()
870 :see: HTTP.res_replace_value()
Thierry FOURNIER08504f42015-03-16 14:17:08 +0100871
Thierry FOURNIER486f5a02015-03-16 15:13:03 +0100872.. js:function:: HTTP.res_replace_value(http, name, regex, replace)
Thierry FOURNIER08504f42015-03-16 14:17:08 +0100873
Thierry FOURNIER486f5a02015-03-16 15:13:03 +0100874 Works like "HTTP.res_replace_header()" except that it matches the regex
Thierry FOURNIER08504f42015-03-16 14:17:08 +0100875 against every comma-delimited value of the header field "name" instead of the
876 entire header.
877
878 :param class_http http: The related http object.
879 :param string name: The header name.
880 :param string regex: The match regular expression.
881 :param string replace: The replacement value.
Thierry FOURNIER486f5a02015-03-16 15:13:03 +0100882 :see: HTTP.res_replace_header()
883 :see: HTTP.req_replace_value()
Thierry FOURNIER08504f42015-03-16 14:17:08 +0100884
Thierry FOURNIER486f5a02015-03-16 15:13:03 +0100885.. js:function:: HTTP.req_set_method(http, method)
Thierry FOURNIER08504f42015-03-16 14:17:08 +0100886
887 Rewrites the request method with the parameter "method".
888
889 :param class_http http: The related http object.
890 :param string method: The new method.
891
Thierry FOURNIER486f5a02015-03-16 15:13:03 +0100892.. js:function:: HTTP.req_set_path(http, path)
Thierry FOURNIER08504f42015-03-16 14:17:08 +0100893
894 Rewrites the request path with the "path" parameter.
895
896 :param class_http http: The related http object.
897 :param string path: The new path.
898
Thierry FOURNIER486f5a02015-03-16 15:13:03 +0100899.. js:function:: HTTP.req_set_query(http, query)
Thierry FOURNIER08504f42015-03-16 14:17:08 +0100900
901 Rewrites the request's query string which appears after the first question
902 mark ("?") with the parameter "query".
903
904 :param class_http http: The related http object.
905 :param string query: The new query.
906
Thierry FOURNIER0d79cf62015-08-26 14:20:58 +0200907.. js:function:: HTTP.req_set_uri(http, uri)
Thierry FOURNIER08504f42015-03-16 14:17:08 +0100908
909 Rewrites the request URI with the parameter "uri".
910
911 :param class_http http: The related http object.
912 :param string uri: The new uri.
913
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +0200914.. js:function:: HTTP.res_set_status(http, status)
915
916 Rewrites the response status code with the parameter "code". Note that the
917 reason is automatically adapted to the new code.
918
919 :param class_http http: The related http object.
920 :param integer status: The new response status code.
921
Thierry FOURNIERdc595002015-12-21 11:13:52 +0100922.. _txn_class:
923
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100924TXN class
925=========
926
927.. js:class:: TXN
928
929 The txn class contain all the functions relative to the http or tcp
930 transaction (Note than a tcp stream is the same than a tcp transaction, but
931 an HTTP transaction is not the same than a tcp stream).
932
933 The usage of this class permits to retrieve data from the requests, alter it
934 and forward it.
935
936 All the functions provided by this class are available in the context
937 **sample-fetches** and **actions**.
938
Thierry FOURNIER486f5a02015-03-16 15:13:03 +0100939.. js:attribute:: TXN.c
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100940
Thierry FOURNIERdc595002015-12-21 11:13:52 +0100941 :returns: An :ref:`converters_class`.
942
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100943 This attribute contains a Converters class object.
944
Thierry FOURNIER486f5a02015-03-16 15:13:03 +0100945.. js:attribute:: TXN.sc
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100946
Thierry FOURNIERdc595002015-12-21 11:13:52 +0100947 :returns: An :ref:`converters_class`.
948
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100949 This attribute contains a Converters class object. The functions of
950 this object returns always a string.
951
Thierry FOURNIER486f5a02015-03-16 15:13:03 +0100952.. js:attribute:: TXN.f
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100953
Thierry FOURNIERdc595002015-12-21 11:13:52 +0100954 :returns: An :ref:`fetches_class`.
955
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100956 This attribute contains a Fetches class object.
957
Thierry FOURNIER486f5a02015-03-16 15:13:03 +0100958.. js:attribute:: TXN.sf
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100959
Thierry FOURNIERdc595002015-12-21 11:13:52 +0100960 :returns: An :ref:`fetches_class`.
961
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100962 This attribute contains a Fetches class object. The functions of
963 this object returns always a string.
964
Thierry FOURNIER486f5a02015-03-16 15:13:03 +0100965.. js:attribute:: TXN.req
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100966
Thierry FOURNIERdc595002015-12-21 11:13:52 +0100967 :returns: An :ref:`channel_class`.
968
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100969 This attribute contains a channel class object for the request buffer.
970
Thierry FOURNIER486f5a02015-03-16 15:13:03 +0100971.. js:attribute:: TXN.res
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100972
Thierry FOURNIERdc595002015-12-21 11:13:52 +0100973 :returns: An :ref:`channel_class`.
974
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100975 This attribute contains a channel class object for the response buffer.
976
Thierry FOURNIER486f5a02015-03-16 15:13:03 +0100977.. js:attribute:: TXN.http
Thierry FOURNIER08504f42015-03-16 14:17:08 +0100978
Thierry FOURNIERdc595002015-12-21 11:13:52 +0100979 :returns: An :ref:`http_class`.
980
Thierry FOURNIER08504f42015-03-16 14:17:08 +0100981 This attribute contains an HTTP class object. It is avalaible only if the
982 proxy has the "mode http" enabled.
983
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100984.. js:function:: TXN.log(TXN, loglevel, msg)
985
986 This function sends a log. The log is sent, according with the HAProxy
987 configuration file, on the default syslog server if it is configured and on
988 the stderr if it is allowed.
989
990 :param class_txn txn: The class txn object containing the data.
991 :param integer loglevel: Is the log level asociated with the message. It is a
992 number between 0 and 7.
993 :param string msg: The log content.
994 :see: core.emerg, core.alert, core.crit, core.err, core.warning, core.notice,
995 core.info, core.debug (log level definitions)
996 :see: TXN.deflog
997 :see: TXN.Debug
998 :see: TXN.Info
999 :see: TXN.Warning
1000 :see: TXN.Alert
1001
1002.. js:function:: TXN.deflog(TXN, msg)
1003
1004 Sends a log line with the default loglevel for the proxy ssociated with the
1005 transaction.
1006
1007 :param class_txn txn: The class txn object containing the data.
1008 :param string msg: The log content.
1009 :see: TXN.log
1010
1011.. js:function:: TXN.Debug(txn, msg)
1012
1013 :param class_txn txn: The class txn object containing the data.
1014 :param string msg: The log content.
1015 :see: TXN.log
1016
1017 Does the same job than:
1018
1019.. code-block:: lua
1020
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001021 function Debug(txn, msg)
1022 TXN.log(txn, core.debug, msg)
1023 end
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01001024..
1025
1026.. js:function:: TXN.Info(txn, msg)
1027
1028 :param class_txn txn: The class txn object containing the data.
1029 :param string msg: The log content.
1030 :see: TXN.log
1031
1032.. code-block:: lua
1033
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001034 function Debug(txn, msg)
1035 TXN.log(txn, core.info, msg)
1036 end
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01001037..
1038
1039.. js:function:: TXN.Warning(txn, msg)
1040
1041 :param class_txn txn: The class txn object containing the data.
1042 :param string msg: The log content.
1043 :see: TXN.log
1044
1045.. code-block:: lua
1046
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001047 function Debug(txn, msg)
1048 TXN.log(txn, core.warning, msg)
1049 end
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01001050..
1051
1052.. js:function:: TXN.Alert(txn, msg)
1053
1054 :param class_txn txn: The class txn object containing the data.
1055 :param string msg: The log content.
1056 :see: TXN.log
1057
1058.. code-block:: lua
1059
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001060 function Debug(txn, msg)
1061 TXN.log(txn, core.alert, msg)
1062 end
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01001063..
1064
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001065.. js:function:: TXN.get_priv(txn)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001066
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001067 Return Lua data stored in the current transaction (with the `TXN.set_priv()`)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001068 function. If no data are stored, it returns a nil value.
1069
1070 :param class_txn txn: The class txn object containing the data.
1071 :returns: the opaque data previsously stored, or nil if nothing is
1072 avalaible.
1073
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001074.. js:function:: TXN.set_priv(txn, data)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001075
1076 Store any data in the current HAProxy transaction. This action replace the
1077 old stored data.
1078
1079 :param class_txn txn: The class txn object containing the data.
1080 :param opaque data: The data which is stored in the transaction.
1081
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +02001082.. js:function:: TXN.set_var(TXN, var, value)
1083
David Carlier61fdf8b2015-10-02 11:59:38 +01001084 Converts a Lua type in a HAProxy type and store it in a variable <var>.
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +02001085
1086 :param class_txn txn: The class txn object containing the data.
1087 :param string var: The variable name according with the HAProxy variable syntax.
1088 :param opaque value: The data which is stored in the variable.
1089
1090.. js:function:: TXN.get_var(TXN, var)
1091
1092 Returns data stored in the variable <var> converter in Lua type.
1093
1094 :param class_txn txn: The class txn object containing the data.
1095 :param string var: The variable name according with the HAProxy variable syntax.
1096
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001097.. js:function:: TXN.get_headers(txn)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001098
1099 This function returns an array of headers.
1100
1101 :param class_txn txn: The class txn object containing the data.
1102 :returns: an array of headers.
1103
Willy Tarreaubc183a62015-08-28 10:39:11 +02001104.. js:function:: TXN.done(txn)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001105
Willy Tarreaubc183a62015-08-28 10:39:11 +02001106 This function terminates processing of the transaction and the associated
1107 session. It can be used when a critical error is detected or to terminate
1108 processing after some data have been returned to the client (eg: a redirect).
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001109
1110 :param class_txn txn: The class txn object containing the data.
1111
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001112.. js:function:: TXN.set_loglevel(txn, loglevel)
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01001113
1114 Is used to change the log level of the current request. The "loglevel" must
1115 be an integer between 0 and 7.
1116
1117 :param class_txn txn: The class txn object containing the data.
1118 :param integer loglevel: The required log level. This variable can be one of
1119 :see: core.<loglevel>
1120
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001121.. js:function:: TXN.set_tos(txn, tos)
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01001122
1123 Is used to set the TOS or DSCP field value of packets sent to the client to
1124 the value passed in "tos" on platforms which support this.
1125
1126 :param class_txn txn: The class txn object containing the data.
1127 :param integer tos: The new TOS os DSCP.
1128
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001129.. js:function:: TXN.set_mark(txn, mark)
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01001130
1131 Is used to set the Netfilter MARK on all packets sent to the client to the
1132 value passed in "mark" on platforms which support it.
1133
1134 :param class_txn txn: The class txn object containing the data.
1135 :param integer mark: The mark value.
1136
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001137.. _socket_class:
1138
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001139Socket class
1140============
1141
1142.. js:class:: Socket
1143
1144 This class must be compatible with the Lua Socket class. Only the 'client'
1145 functions are available. See the Lua Socket documentation:
1146
1147 `http://w3.impa.br/~diego/software/luasocket/tcp.html
1148 <http://w3.impa.br/~diego/software/luasocket/tcp.html>`_
1149
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001150.. js:function:: Socket.close(socket)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001151
1152 Closes a TCP object. The internal socket used by the object is closed and the
1153 local address to which the object was bound is made available to other
1154 applications. No further operations (except for further calls to the close
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001155 method) are allowed on a closed Socket.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001156
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001157 :param class_socket socket: Is the manipulated Socket.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001158
1159 Note: It is important to close all used sockets once they are not needed,
1160 since, in many systems, each socket uses a file descriptor, which are limited
1161 system resources. Garbage-collected objects are automatically closed before
1162 destruction, though.
1163
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02001164.. js:function:: Socket.connect(socket, address[, port])
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001165
1166 Attempts to connect a socket object to a remote host.
1167
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001168
1169 In case of error, the method returns nil followed by a string describing the
1170 error. In case of success, the method returns 1.
1171
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001172 :param class_socket socket: Is the manipulated Socket.
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02001173 :param string address: can be an IP address or a host name. See below for more
1174 information.
1175 :param integer port: must be an integer number in the range [1..64K].
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001176 :returns: 1 or nil.
1177
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02001178 an address field extension permits to use the connect() function to connect to
1179 other stream than TCP. The syntax containing a simpleipv4 or ipv6 address is
1180 the basically expected format. This format requires the port.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001181
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02001182 Other format accepted are a socket path like "/socket/path", it permits to
1183 connect to a socket. abstract namespaces are supported with the prefix
1184 "abns@", and finaly a filedescriotr can be passed with the prefix "fd@".
1185 The prefix "ipv4@", "ipv6@" and "unix@" are also supported. The port can be
1186 passed int the string. The syntax "127.0.0.1:1234" is valid. in this case, the
1187 parameter *port* is ignored.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001188
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001189.. js:function:: Socket.connect_ssl(socket, address, port)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001190
1191 Same behavior than the function socket:connect, but uses SSL.
1192
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001193 :param class_socket socket: Is the manipulated Socket.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001194 :returns: 1 or nil.
1195
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001196.. js:function:: Socket.getpeername(socket)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001197
1198 Returns information about the remote side of a connected client object.
1199
1200 Returns a string with the IP address of the peer, followed by the port number
1201 that peer is using for the connection. In case of error, the method returns
1202 nil.
1203
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001204 :param class_socket socket: Is the manipulated Socket.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001205 :returns: a string containing the server information.
1206
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001207.. js:function:: Socket.getsockname(socket)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001208
1209 Returns the local address information associated to the object.
1210
1211 The method returns a string with local IP address and a number with the port.
1212 In case of error, the method returns nil.
1213
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001214 :param class_socket socket: Is the manipulated Socket.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001215 :returns: a string containing the client information.
1216
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001217.. js:function:: Socket.receive(socket, [pattern [, prefix]])
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001218
1219 Reads data from a client object, according to the specified read pattern.
1220 Patterns follow the Lua file I/O format, and the difference in performance
1221 between all patterns is negligible.
1222
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001223 :param class_socket socket: Is the manipulated Socket.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001224 :param string|integer pattern: Describe what is required (see below).
1225 :param string prefix: A string which will be prefix the returned data.
1226 :returns: a string containing the required data or nil.
1227
1228 Pattern can be any of the following:
1229
1230 * **`*a`**: reads from the socket until the connection is closed. No
1231 end-of-line translation is performed;
1232
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001233 * **`*l`**: reads a line of text from the Socket. The line is terminated by a
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001234 LF character (ASCII 10), optionally preceded by a CR character
1235 (ASCII 13). The CR and LF characters are not included in the
1236 returned line. In fact, all CR characters are ignored by the
1237 pattern. This is the default pattern.
1238
1239 * **number**: causes the method to read a specified number of bytes from the
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001240 Socket. Prefix is an optional string to be concatenated to the
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001241 beginning of any received data before return.
1242
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02001243 * **empty**: If the pattern is left empty, the default option is `*l`.
1244
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001245 If successful, the method returns the received pattern. In case of error, the
1246 method returns nil followed by an error message which can be the string
1247 'closed' in case the connection was closed before the transmission was
1248 completed or the string 'timeout' in case there was a timeout during the
1249 operation. Also, after the error message, the function returns the partial
1250 result of the transmission.
1251
1252 Important note: This function was changed severely. It used to support
1253 multiple patterns (but I have never seen this feature used) and now it
1254 doesn't anymore. Partial results used to be returned in the same way as
1255 successful results. This last feature violated the idea that all functions
1256 should return nil on error. Thus it was changed too.
1257
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001258.. js:function:: Socket.send(socket, data [, start [, end ]])
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001259
1260 Sends data through client object.
1261
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001262 :param class_socket socket: Is the manipulated Socket.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001263 :param string data: The data that will be sent.
1264 :param integer start: The start position in the buffer of the data which will
1265 be sent.
1266 :param integer end: The end position in the buffer of the data which will
1267 be sent.
1268 :returns: see below.
1269
1270 Data is the string to be sent. The optional arguments i and j work exactly
1271 like the standard string.sub Lua function to allow the selection of a
1272 substring to be sent.
1273
1274 If successful, the method returns the index of the last byte within [start,
1275 end] that has been sent. Notice that, if start is 1 or absent, this is
1276 effectively the total number of bytes sent. In case of error, the method
1277 returns nil, followed by an error message, followed by the index of the last
1278 byte within [start, end] that has been sent. You might want to try again from
1279 the byte following that. The error message can be 'closed' in case the
1280 connection was closed before the transmission was completed or the string
1281 'timeout' in case there was a timeout during the operation.
1282
1283 Note: Output is not buffered. For small strings, it is always better to
1284 concatenate them in Lua (with the '..' operator) and send the result in one
1285 call instead of calling the method several times.
1286
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001287.. js:function:: Socket.setoption(socket, option [, value])
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001288
1289 Just implemented for compatibility, this cal does nothing.
1290
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001291.. js:function:: Socket.settimeout(socket, value [, mode])
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001292
1293 Changes the timeout values for the object. All I/O operations are blocking.
1294 That is, any call to the methods send, receive, and accept will block
1295 indefinitely, until the operation completes. The settimeout method defines a
1296 limit on the amount of time the I/O methods can block. When a timeout time
1297 has elapsed, the affected methods give up and fail with an error code.
1298
1299 The amount of time to wait is specified as the value parameter, in seconds.
1300
1301 The timeout modes are bot implemented, the only settable timeout is the
1302 inactivity time waiting for complete the internal buffer send or waiting for
1303 receive data.
1304
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001305 :param class_socket socket: Is the manipulated Socket.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001306 :param integer value: The timeout value.
1307
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001308.. _map_class:
1309
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001310Map class
1311=========
1312
1313.. js:class:: Map
1314
1315 This class permits to do some lookup in HAProxy maps. The declared maps can
1316 be modified during the runtime throught the HAProxy management socket.
1317
1318.. code-block:: lua
1319
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001320 default = "usa"
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001321
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001322 -- Create and load map
1323 geo = Map.new("geo.map", Map.ip);
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001324
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001325 -- Create new fetch that returns the user country
1326 core.register_fetches("country", function(txn)
1327 local src;
1328 local loc;
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001329
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001330 src = txn.f:fhdr("x-forwarded-for");
1331 if (src == nil) then
1332 src = txn.f:src()
1333 if (src == nil) then
1334 return default;
1335 end
1336 end
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001337
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001338 -- Perform lookup
1339 loc = geo:lookup(src);
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001340
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001341 if (loc == nil) then
1342 return default;
1343 end
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001344
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001345 return loc;
1346 end);
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001347
1348.. js:attribute:: Map.int
1349
1350 See the HAProxy configuration.txt file, chapter "Using ACLs and fetching
1351 samples" ans subchapter "ACL basics" to understand this pattern matching
1352 method.
1353
1354.. js:attribute:: Map.ip
1355
1356 See the HAProxy configuration.txt file, chapter "Using ACLs and fetching
1357 samples" ans subchapter "ACL basics" to understand this pattern matching
1358 method.
1359
1360.. js:attribute:: Map.str
1361
1362 See the HAProxy configuration.txt file, chapter "Using ACLs and fetching
1363 samples" ans subchapter "ACL basics" to understand this pattern matching
1364 method.
1365
1366.. js:attribute:: Map.beg
1367
1368 See the HAProxy configuration.txt file, chapter "Using ACLs and fetching
1369 samples" ans subchapter "ACL basics" to understand this pattern matching
1370 method.
1371
1372.. js:attribute:: Map.sub
1373
1374 See the HAProxy configuration.txt file, chapter "Using ACLs and fetching
1375 samples" ans subchapter "ACL basics" to understand this pattern matching
1376 method.
1377
1378.. js:attribute:: Map.dir
1379
1380 See the HAProxy configuration.txt file, chapter "Using ACLs and fetching
1381 samples" ans subchapter "ACL basics" to understand this pattern matching
1382 method.
1383
1384.. js:attribute:: Map.dom
1385
1386 See the HAProxy configuration.txt file, chapter "Using ACLs and fetching
1387 samples" ans subchapter "ACL basics" to understand this pattern matching
1388 method.
1389
1390.. js:attribute:: Map.end
1391
1392 See the HAProxy configuration.txt file, chapter "Using ACLs and fetching
1393 samples" ans subchapter "ACL basics" to understand this pattern matching
1394 method.
1395
1396.. js:attribute:: Map.reg
1397
1398 See the HAProxy configuration.txt file, chapter "Using ACLs and fetching
1399 samples" ans subchapter "ACL basics" to understand this pattern matching
1400 method.
1401
1402
1403.. js:function:: Map.new(file, method)
1404
1405 Creates and load a map.
1406
1407 :param string file: Is the file containing the map.
1408 :param integer method: Is the map pattern matching method. See the attributes
1409 of the Map class.
1410 :returns: a class Map object.
1411 :see: The Map attributes.
1412
1413.. js:function:: Map.lookup(map, str)
1414
1415 Perform a lookup in a map.
1416
1417 :param class_map map: Is the class Map object.
1418 :param string str: Is the string used as key.
1419 :returns: a string containing the result or nil if no match.
1420
1421.. js:function:: Map.slookup(map, str)
1422
1423 Perform a lookup in a map.
1424
1425 :param class_map map: Is the class Map object.
1426 :param string str: Is the string used as key.
1427 :returns: a string containing the result or empty string if no match.
1428
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001429.. _applethttp_class:
1430
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02001431AppletHTTP class
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001432================
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02001433
1434.. js:class:: AppletHTTP
1435
1436 This class is used with applets that requires the 'http' mode. The http applet
1437 can be registered with the *core.register_service()* function. They are used
1438 for processing an http request like a server in back of HAProxy.
1439
1440 This is an hello world sample code:
1441
1442.. code-block:: lua
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001443
Pieter Baauw4d7f7662015-11-08 16:38:08 +01001444 core.register_service("hello-world", "http", function(applet)
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02001445 local response = "Hello World !"
1446 applet:set_status(200)
Pieter Baauw2dcb9bc2015-10-01 22:47:12 +02001447 applet:add_header("content-length", string.len(response))
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02001448 applet:add_header("content-type", "text/plain")
Pieter Baauw2dcb9bc2015-10-01 22:47:12 +02001449 applet:start_response()
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02001450 applet:send(response)
1451 end)
1452
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001453.. js:attribute:: AppletHTTP.c
1454
1455 :returns: A :ref:`converters_class`
1456
1457 This attribute contains a Converters class object.
1458
1459.. js:attribute:: AppletHTTP.sc
1460
1461 :returns: A :ref:`converters_class`
1462
1463 This attribute contains a Converters class object. The
1464 functions of this object returns always a string.
1465
1466.. js:attribute:: AppletHTTP.f
1467
1468 :returns: A :ref:`fetches_class`
1469
1470 This attribute contains a Fetches class object. Note that the
1471 applet execution place cannot access to a valid HAProxy core HTTP
1472 transaction, so some sample fecthes related to the HTTP dependant
1473 values (hdr, path, ...) are not available.
1474
1475.. js:attribute:: AppletHTTP.sf
1476
1477 :returns: A :ref:`fetches_class`
1478
1479 This attribute contains a Fetches class object. The functions of
1480 this object returns always a string. Note that the applet
1481 execution place cannot access to a valid HAProxy core HTTP
1482 transaction, so some sample fecthes related to the HTTP dependant
1483 values (hdr, path, ...) are not available.
1484
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01001485.. js:attribute:: AppletHTTP.method
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001486
1487 :returns: string
1488
1489 The attribute method returns a string containing the HTTP
1490 method.
1491
1492.. js:attribute:: AppletHTTP.version
1493
1494 :returns: string
1495
1496 The attribute version, returns a string containing the HTTP
1497 request version.
1498
1499.. js:attribute:: AppletHTTP.path
1500
1501 :returns: string
1502
1503 The attribute path returns a string containing the HTTP
1504 request path.
1505
1506.. js:attribute:: AppletHTTP.qs
1507
1508 :returns: string
1509
1510 The attribute qs returns a string containing the HTTP
1511 request query string.
1512
1513.. js:attribute:: AppletHTTP.length
1514
1515 :returns: integer
1516
1517 The attribute length returns an integer containing the HTTP
1518 body length.
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02001519
Thierry FOURNIER841475e2015-12-11 17:10:09 +01001520.. js:attribute:: AppletHTTP.headers
1521
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001522 :returns: array
1523
1524 The attribute headers returns an array containing the HTTP
1525 headers. The header names are always in lower case. As the header name can be
1526 encountered more than once in each request, the value is indexed with 0 as
1527 first index value. The array have this form:
1528
1529.. code-block:: lua
1530
1531 AppletHTTP.headers['<header-name>'][<header-index>] = "<header-value>"
1532
1533 AppletHTTP.headers["host"][0] = "www.test.com"
1534 AppletHTTP.headers["accept"][0] = "audio/basic q=1"
1535 AppletHTTP.headers["accept"][1] = "audio/*, q=0.2"
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01001536 AppletHTTP.headers["accept"][2] = "*/*, q=0.1"
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001537..
1538
1539.. js:attribute:: AppletHTTP.headers
1540
Thierry FOURNIER841475e2015-12-11 17:10:09 +01001541 Contains an array containing all the request headers.
1542
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01001543.. js:function:: AppletHTTP.set_status(applet, code)
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02001544
1545 This function sets the HTTP status code for the response. The allowed code are
1546 from 100 to 599.
1547
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01001548 :param class_AppletHTTP applet: An :ref:`applethttp_class`
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02001549 :param integer code: the status code returned to the client.
1550
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01001551.. js:function:: AppletHTTP.add_header(applet, name, value)
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02001552
1553 This function add an header in the response. Duplicated headers are not
1554 collapsed. The special header *content-length* is used to determinate the
1555 response length. If it not exists, a *transfer-encoding: chunked* is set, and
1556 all the write from the funcion *AppletHTTP:send()* become a chunk.
1557
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01001558 :param class_AppletHTTP applet: An :ref:`applethttp_class`
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02001559 :param string name: the header name
1560 :param string value: the header value
1561
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01001562.. js:function:: AppletHTTP.start_response(applet)
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02001563
1564 This function indicates to the HTTP engine that it can process and send the
1565 response headers. After this called we cannot add headers to the response; We
1566 cannot use the *AppletHTTP:send()* function if the
1567 *AppletHTTP:start_response()* is not called.
1568
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01001569 :param class_AppletHTTP applet: An :ref:`applethttp_class`
1570
1571.. js:function:: AppletHTTP.getline(applet)
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02001572
1573 This function returns a string containing one line from the http body. If the
1574 data returned doesn't contains a final '\\n' its assumed than its the last
1575 available data before the end of stream.
1576
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01001577 :param class_AppletHTTP applet: An :ref:`applethttp_class`
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02001578 :returns: a string. The string can be empty if we reach the end of the stream.
1579
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01001580.. js:function:: AppletHTTP.receive(applet, [size])
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02001581
1582 Reads data from the HTTP body, according to the specified read *size*. If the
1583 *size* is missing, the function tries to read all the content of the stream
1584 until the end. If the *size* is bigger than the http body, it returns the
1585 amount of data avalaible.
1586
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01001587 :param class_AppletHTTP applet: An :ref:`applethttp_class`
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02001588 :param integer size: the required read size.
1589 :returns: always return a string,the string can be empty is the connexion is
1590 closed.
1591
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01001592.. js:function:: AppletHTTP.send(applet, msg)
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02001593
1594 Send the message *msg* on the http request body.
1595
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01001596 :param class_AppletHTTP applet: An :ref:`applethttp_class`
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02001597 :param string msg: the message to send.
1598
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01001599.. js:function:: AppletHTTP.get_priv(applet)
1600
1601 Return Lua data stored in the current transaction (with the
1602 `AppletHTTP.set_priv()`) function. If no data are stored, it returns a nil
1603 value.
1604
1605 :param class_AppletHTTP applet: An :ref:`applethttp_class`
1606 :returns: the opaque data previsously stored, or nil if nothing is
1607 avalaible.
1608
1609.. js:function:: AppletHTTP.set_priv(applet, data)
1610
1611 Store any data in the current HAProxy transaction. This action replace the
1612 old stored data.
1613
1614 :param class_AppletHTTP applet: An :ref:`applethttp_class`
1615 :param opaque data: The data which is stored in the transaction.
1616
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001617.. _applettcp_class:
1618
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02001619AppletTCP class
1620===============
1621
1622.. js:class:: AppletTCP
1623
1624 This class is used with applets that requires the 'tcp' mode. The tcp applet
1625 can be registered with the *core.register_service()* function. They are used
1626 for processing a tcp stream like a server in back of HAProxy.
1627
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001628.. js:attribute:: AppletTCP.c
1629
1630 :returns: A :ref:`converters_class`
1631
1632 This attribute contains a Converters class object.
1633
1634.. js:attribute:: AppletTCP.sc
1635
1636 :returns: A :ref:`converters_class`
1637
1638 This attribute contains a Converters class object. The
1639 functions of this object returns always a string.
1640
1641.. js:attribute:: AppletTCP.f
1642
1643 :returns: A :ref:`fetches_class`
1644
1645 This attribute contains a Fetches class object.
1646
1647.. js:attribute:: AppletTCP.sf
1648
1649 :returns: A :ref:`fetches_class`
1650
1651 This attribute contains a Fetches class object.
1652
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01001653.. js:function:: AppletTCP.getline(applet)
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02001654
1655 This function returns a string containing one line from the stream. If the
1656 data returned doesn't contains a final '\\n' its assumed than its the last
1657 available data before the end of stream.
1658
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01001659 :param class_AppletTCP applet: An :ref:`applettcp_class`
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02001660 :returns: a string. The string can be empty if we reach the end of the stream.
1661
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01001662.. js:function:: AppletTCP.receive(applet, [size])
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02001663
1664 Reads data from the TCP stream, according to the specified read *size*. If the
1665 *size* is missing, the function tries to read all the content of the stream
1666 until the end.
1667
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01001668 :param class_AppletTCP applet: An :ref:`applettcp_class`
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02001669 :param integer size: the required read size.
1670 :returns: always return a string,the string can be empty is the connexion is
1671 closed.
1672
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01001673.. js:function:: AppletTCP.send(appletmsg)
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02001674
1675 Send the message on the stream.
1676
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01001677 :param class_AppletTCP applet: An :ref:`applettcp_class`
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02001678 :param string msg: the message to send.
1679
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01001680.. js:function:: AppletTCP.get_priv(applet)
1681
1682 Return Lua data stored in the current transaction (with the
1683 `AppletTCP.set_priv()`) function. If no data are stored, it returns a nil
1684 value.
1685
1686 :param class_AppletTCP applet: An :ref:`applettcp_class`
1687 :returns: the opaque data previsously stored, or nil if nothing is
1688 avalaible.
1689
1690.. js:function:: AppletTCP.set_priv(applet, data)
1691
1692 Store any data in the current HAProxy transaction. This action replace the
1693 old stored data.
1694
1695 :param class_AppletTCP applet: An :ref:`applettcp_class`
1696 :param opaque data: The data which is stored in the transaction.
1697
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001698External Lua libraries
1699======================
1700
1701A lot of useful lua libraries can be found here:
1702
1703* `https://lua-toolbox.com/ <https://lua-toolbox.com/>`_
1704
1705Redis acces:
1706
1707* `https://github.com/nrk/redis-lua <https://github.com/nrk/redis-lua>`_
1708
1709This is an example about the usage of the Redis library with HAProxy. Note that
1710each call of any function of this library can throw an error if the socket
1711connection fails.
1712
1713.. code-block:: lua
1714
1715 -- load the redis library
1716 local redis = require("redis");
1717
1718 function do_something(txn)
1719
1720 -- create and connect new tcp socket
1721 local tcp = core.tcp();
1722 tcp:settimeout(1);
1723 tcp:connect("127.0.0.1", 6379);
1724
1725 -- use the redis library with this new socket
1726 local client = redis.connect({socket=tcp});
1727 client:ping();
1728
1729 end
1730
1731OpenSSL:
1732
1733* `http://mkottman.github.io/luacrypto/index.html
1734 <http://mkottman.github.io/luacrypto/index.html>`_
1735
1736* `https://github.com/brunoos/luasec/wiki
1737 <https://github.com/brunoos/luasec/wiki>`_
Thierry FOURNIER2e4893c2015-03-18 13:37:27 +01001738