blob: 7a1f44edb819adb4a340e85340ad56123c29f7ea [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
Willy Tarreaubc183a62015-08-28 10:39:11 +02001097.. js:function:: TXN.done(txn)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001098
Willy Tarreaubc183a62015-08-28 10:39:11 +02001099 This function terminates processing of the transaction and the associated
1100 session. It can be used when a critical error is detected or to terminate
1101 processing after some data have been returned to the client (eg: a redirect).
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001102
1103 :param class_txn txn: The class txn object containing the data.
1104
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001105.. js:function:: TXN.set_loglevel(txn, loglevel)
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01001106
1107 Is used to change the log level of the current request. The "loglevel" must
1108 be an integer between 0 and 7.
1109
1110 :param class_txn txn: The class txn object containing the data.
1111 :param integer loglevel: The required log level. This variable can be one of
1112 :see: core.<loglevel>
1113
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001114.. js:function:: TXN.set_tos(txn, tos)
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01001115
1116 Is used to set the TOS or DSCP field value of packets sent to the client to
1117 the value passed in "tos" on platforms which support this.
1118
1119 :param class_txn txn: The class txn object containing the data.
1120 :param integer tos: The new TOS os DSCP.
1121
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001122.. js:function:: TXN.set_mark(txn, mark)
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01001123
1124 Is used to set the Netfilter MARK on all packets sent to the client to the
1125 value passed in "mark" on platforms which support it.
1126
1127 :param class_txn txn: The class txn object containing the data.
1128 :param integer mark: The mark value.
1129
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001130.. _socket_class:
1131
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001132Socket class
1133============
1134
1135.. js:class:: Socket
1136
1137 This class must be compatible with the Lua Socket class. Only the 'client'
1138 functions are available. See the Lua Socket documentation:
1139
1140 `http://w3.impa.br/~diego/software/luasocket/tcp.html
1141 <http://w3.impa.br/~diego/software/luasocket/tcp.html>`_
1142
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001143.. js:function:: Socket.close(socket)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001144
1145 Closes a TCP object. The internal socket used by the object is closed and the
1146 local address to which the object was bound is made available to other
1147 applications. No further operations (except for further calls to the close
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001148 method) are allowed on a closed Socket.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001149
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001150 :param class_socket socket: Is the manipulated Socket.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001151
1152 Note: It is important to close all used sockets once they are not needed,
1153 since, in many systems, each socket uses a file descriptor, which are limited
1154 system resources. Garbage-collected objects are automatically closed before
1155 destruction, though.
1156
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02001157.. js:function:: Socket.connect(socket, address[, port])
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001158
1159 Attempts to connect a socket object to a remote host.
1160
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001161
1162 In case of error, the method returns nil followed by a string describing the
1163 error. In case of success, the method returns 1.
1164
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001165 :param class_socket socket: Is the manipulated Socket.
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02001166 :param string address: can be an IP address or a host name. See below for more
1167 information.
1168 :param integer port: must be an integer number in the range [1..64K].
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001169 :returns: 1 or nil.
1170
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02001171 an address field extension permits to use the connect() function to connect to
1172 other stream than TCP. The syntax containing a simpleipv4 or ipv6 address is
1173 the basically expected format. This format requires the port.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001174
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02001175 Other format accepted are a socket path like "/socket/path", it permits to
1176 connect to a socket. abstract namespaces are supported with the prefix
1177 "abns@", and finaly a filedescriotr can be passed with the prefix "fd@".
1178 The prefix "ipv4@", "ipv6@" and "unix@" are also supported. The port can be
1179 passed int the string. The syntax "127.0.0.1:1234" is valid. in this case, the
1180 parameter *port* is ignored.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001181
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001182.. js:function:: Socket.connect_ssl(socket, address, port)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001183
1184 Same behavior than the function socket:connect, but uses SSL.
1185
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001186 :param class_socket socket: Is the manipulated Socket.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001187 :returns: 1 or nil.
1188
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001189.. js:function:: Socket.getpeername(socket)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001190
1191 Returns information about the remote side of a connected client object.
1192
1193 Returns a string with the IP address of the peer, followed by the port number
1194 that peer is using for the connection. In case of error, the method returns
1195 nil.
1196
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001197 :param class_socket socket: Is the manipulated Socket.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001198 :returns: a string containing the server information.
1199
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001200.. js:function:: Socket.getsockname(socket)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001201
1202 Returns the local address information associated to the object.
1203
1204 The method returns a string with local IP address and a number with the port.
1205 In case of error, the method returns nil.
1206
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001207 :param class_socket socket: Is the manipulated Socket.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001208 :returns: a string containing the client information.
1209
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001210.. js:function:: Socket.receive(socket, [pattern [, prefix]])
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001211
1212 Reads data from a client object, according to the specified read pattern.
1213 Patterns follow the Lua file I/O format, and the difference in performance
1214 between all patterns is negligible.
1215
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001216 :param class_socket socket: Is the manipulated Socket.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001217 :param string|integer pattern: Describe what is required (see below).
1218 :param string prefix: A string which will be prefix the returned data.
1219 :returns: a string containing the required data or nil.
1220
1221 Pattern can be any of the following:
1222
1223 * **`*a`**: reads from the socket until the connection is closed. No
1224 end-of-line translation is performed;
1225
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001226 * **`*l`**: reads a line of text from the Socket. The line is terminated by a
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001227 LF character (ASCII 10), optionally preceded by a CR character
1228 (ASCII 13). The CR and LF characters are not included in the
1229 returned line. In fact, all CR characters are ignored by the
1230 pattern. This is the default pattern.
1231
1232 * **number**: causes the method to read a specified number of bytes from the
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001233 Socket. Prefix is an optional string to be concatenated to the
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001234 beginning of any received data before return.
1235
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02001236 * **empty**: If the pattern is left empty, the default option is `*l`.
1237
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001238 If successful, the method returns the received pattern. In case of error, the
1239 method returns nil followed by an error message which can be the string
1240 'closed' in case the connection was closed before the transmission was
1241 completed or the string 'timeout' in case there was a timeout during the
1242 operation. Also, after the error message, the function returns the partial
1243 result of the transmission.
1244
1245 Important note: This function was changed severely. It used to support
1246 multiple patterns (but I have never seen this feature used) and now it
1247 doesn't anymore. Partial results used to be returned in the same way as
1248 successful results. This last feature violated the idea that all functions
1249 should return nil on error. Thus it was changed too.
1250
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001251.. js:function:: Socket.send(socket, data [, start [, end ]])
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001252
1253 Sends data through client object.
1254
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001255 :param class_socket socket: Is the manipulated Socket.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001256 :param string data: The data that will be sent.
1257 :param integer start: The start position in the buffer of the data which will
1258 be sent.
1259 :param integer end: The end position in the buffer of the data which will
1260 be sent.
1261 :returns: see below.
1262
1263 Data is the string to be sent. The optional arguments i and j work exactly
1264 like the standard string.sub Lua function to allow the selection of a
1265 substring to be sent.
1266
1267 If successful, the method returns the index of the last byte within [start,
1268 end] that has been sent. Notice that, if start is 1 or absent, this is
1269 effectively the total number of bytes sent. In case of error, the method
1270 returns nil, followed by an error message, followed by the index of the last
1271 byte within [start, end] that has been sent. You might want to try again from
1272 the byte following that. The error message can be 'closed' in case the
1273 connection was closed before the transmission was completed or the string
1274 'timeout' in case there was a timeout during the operation.
1275
1276 Note: Output is not buffered. For small strings, it is always better to
1277 concatenate them in Lua (with the '..' operator) and send the result in one
1278 call instead of calling the method several times.
1279
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001280.. js:function:: Socket.setoption(socket, option [, value])
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001281
1282 Just implemented for compatibility, this cal does nothing.
1283
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001284.. js:function:: Socket.settimeout(socket, value [, mode])
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001285
1286 Changes the timeout values for the object. All I/O operations are blocking.
1287 That is, any call to the methods send, receive, and accept will block
1288 indefinitely, until the operation completes. The settimeout method defines a
1289 limit on the amount of time the I/O methods can block. When a timeout time
1290 has elapsed, the affected methods give up and fail with an error code.
1291
1292 The amount of time to wait is specified as the value parameter, in seconds.
1293
1294 The timeout modes are bot implemented, the only settable timeout is the
1295 inactivity time waiting for complete the internal buffer send or waiting for
1296 receive data.
1297
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001298 :param class_socket socket: Is the manipulated Socket.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001299 :param integer value: The timeout value.
1300
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001301.. _map_class:
1302
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001303Map class
1304=========
1305
1306.. js:class:: Map
1307
1308 This class permits to do some lookup in HAProxy maps. The declared maps can
1309 be modified during the runtime throught the HAProxy management socket.
1310
1311.. code-block:: lua
1312
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001313 default = "usa"
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001314
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001315 -- Create and load map
1316 geo = Map.new("geo.map", Map.ip);
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001317
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001318 -- Create new fetch that returns the user country
1319 core.register_fetches("country", function(txn)
1320 local src;
1321 local loc;
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001322
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001323 src = txn.f:fhdr("x-forwarded-for");
1324 if (src == nil) then
1325 src = txn.f:src()
1326 if (src == nil) then
1327 return default;
1328 end
1329 end
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001330
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001331 -- Perform lookup
1332 loc = geo:lookup(src);
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001333
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001334 if (loc == nil) then
1335 return default;
1336 end
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001337
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001338 return loc;
1339 end);
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001340
1341.. js:attribute:: Map.int
1342
1343 See the HAProxy configuration.txt file, chapter "Using ACLs and fetching
1344 samples" ans subchapter "ACL basics" to understand this pattern matching
1345 method.
1346
1347.. js:attribute:: Map.ip
1348
1349 See the HAProxy configuration.txt file, chapter "Using ACLs and fetching
1350 samples" ans subchapter "ACL basics" to understand this pattern matching
1351 method.
1352
1353.. js:attribute:: Map.str
1354
1355 See the HAProxy configuration.txt file, chapter "Using ACLs and fetching
1356 samples" ans subchapter "ACL basics" to understand this pattern matching
1357 method.
1358
1359.. js:attribute:: Map.beg
1360
1361 See the HAProxy configuration.txt file, chapter "Using ACLs and fetching
1362 samples" ans subchapter "ACL basics" to understand this pattern matching
1363 method.
1364
1365.. js:attribute:: Map.sub
1366
1367 See the HAProxy configuration.txt file, chapter "Using ACLs and fetching
1368 samples" ans subchapter "ACL basics" to understand this pattern matching
1369 method.
1370
1371.. js:attribute:: Map.dir
1372
1373 See the HAProxy configuration.txt file, chapter "Using ACLs and fetching
1374 samples" ans subchapter "ACL basics" to understand this pattern matching
1375 method.
1376
1377.. js:attribute:: Map.dom
1378
1379 See the HAProxy configuration.txt file, chapter "Using ACLs and fetching
1380 samples" ans subchapter "ACL basics" to understand this pattern matching
1381 method.
1382
1383.. js:attribute:: Map.end
1384
1385 See the HAProxy configuration.txt file, chapter "Using ACLs and fetching
1386 samples" ans subchapter "ACL basics" to understand this pattern matching
1387 method.
1388
1389.. js:attribute:: Map.reg
1390
1391 See the HAProxy configuration.txt file, chapter "Using ACLs and fetching
1392 samples" ans subchapter "ACL basics" to understand this pattern matching
1393 method.
1394
1395
1396.. js:function:: Map.new(file, method)
1397
1398 Creates and load a map.
1399
1400 :param string file: Is the file containing the map.
1401 :param integer method: Is the map pattern matching method. See the attributes
1402 of the Map class.
1403 :returns: a class Map object.
1404 :see: The Map attributes.
1405
1406.. js:function:: Map.lookup(map, str)
1407
1408 Perform a lookup in a map.
1409
1410 :param class_map map: Is the class Map object.
1411 :param string str: Is the string used as key.
1412 :returns: a string containing the result or nil if no match.
1413
1414.. js:function:: Map.slookup(map, str)
1415
1416 Perform a lookup in a map.
1417
1418 :param class_map map: Is the class Map object.
1419 :param string str: Is the string used as key.
1420 :returns: a string containing the result or empty string if no match.
1421
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001422.. _applethttp_class:
1423
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02001424AppletHTTP class
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001425================
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02001426
1427.. js:class:: AppletHTTP
1428
1429 This class is used with applets that requires the 'http' mode. The http applet
1430 can be registered with the *core.register_service()* function. They are used
1431 for processing an http request like a server in back of HAProxy.
1432
1433 This is an hello world sample code:
1434
1435.. code-block:: lua
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001436
Pieter Baauw4d7f7662015-11-08 16:38:08 +01001437 core.register_service("hello-world", "http", function(applet)
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02001438 local response = "Hello World !"
1439 applet:set_status(200)
Pieter Baauw2dcb9bc2015-10-01 22:47:12 +02001440 applet:add_header("content-length", string.len(response))
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02001441 applet:add_header("content-type", "text/plain")
Pieter Baauw2dcb9bc2015-10-01 22:47:12 +02001442 applet:start_response()
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02001443 applet:send(response)
1444 end)
1445
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001446.. js:attribute:: AppletHTTP.c
1447
1448 :returns: A :ref:`converters_class`
1449
1450 This attribute contains a Converters class object.
1451
1452.. js:attribute:: AppletHTTP.sc
1453
1454 :returns: A :ref:`converters_class`
1455
1456 This attribute contains a Converters class object. The
1457 functions of this object returns always a string.
1458
1459.. js:attribute:: AppletHTTP.f
1460
1461 :returns: A :ref:`fetches_class`
1462
1463 This attribute contains a Fetches class object. Note that the
1464 applet execution place cannot access to a valid HAProxy core HTTP
1465 transaction, so some sample fecthes related to the HTTP dependant
1466 values (hdr, path, ...) are not available.
1467
1468.. js:attribute:: AppletHTTP.sf
1469
1470 :returns: A :ref:`fetches_class`
1471
1472 This attribute contains a Fetches class object. The functions of
1473 this object returns always a string. Note that the applet
1474 execution place cannot access to a valid HAProxy core HTTP
1475 transaction, so some sample fecthes related to the HTTP dependant
1476 values (hdr, path, ...) are not available.
1477
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01001478.. js:attribute:: AppletHTTP.method
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001479
1480 :returns: string
1481
1482 The attribute method returns a string containing the HTTP
1483 method.
1484
1485.. js:attribute:: AppletHTTP.version
1486
1487 :returns: string
1488
1489 The attribute version, returns a string containing the HTTP
1490 request version.
1491
1492.. js:attribute:: AppletHTTP.path
1493
1494 :returns: string
1495
1496 The attribute path returns a string containing the HTTP
1497 request path.
1498
1499.. js:attribute:: AppletHTTP.qs
1500
1501 :returns: string
1502
1503 The attribute qs returns a string containing the HTTP
1504 request query string.
1505
1506.. js:attribute:: AppletHTTP.length
1507
1508 :returns: integer
1509
1510 The attribute length returns an integer containing the HTTP
1511 body length.
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02001512
Thierry FOURNIER841475e2015-12-11 17:10:09 +01001513.. js:attribute:: AppletHTTP.headers
1514
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001515 :returns: array
1516
1517 The attribute headers returns an array containing the HTTP
1518 headers. The header names are always in lower case. As the header name can be
1519 encountered more than once in each request, the value is indexed with 0 as
1520 first index value. The array have this form:
1521
1522.. code-block:: lua
1523
1524 AppletHTTP.headers['<header-name>'][<header-index>] = "<header-value>"
1525
1526 AppletHTTP.headers["host"][0] = "www.test.com"
1527 AppletHTTP.headers["accept"][0] = "audio/basic q=1"
1528 AppletHTTP.headers["accept"][1] = "audio/*, q=0.2"
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01001529 AppletHTTP.headers["accept"][2] = "*/*, q=0.1"
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001530..
1531
1532.. js:attribute:: AppletHTTP.headers
1533
Thierry FOURNIER841475e2015-12-11 17:10:09 +01001534 Contains an array containing all the request headers.
1535
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01001536.. js:function:: AppletHTTP.set_status(applet, code)
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02001537
1538 This function sets the HTTP status code for the response. The allowed code are
1539 from 100 to 599.
1540
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01001541 :param class_AppletHTTP applet: An :ref:`applethttp_class`
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02001542 :param integer code: the status code returned to the client.
1543
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01001544.. js:function:: AppletHTTP.add_header(applet, name, value)
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02001545
1546 This function add an header in the response. Duplicated headers are not
1547 collapsed. The special header *content-length* is used to determinate the
1548 response length. If it not exists, a *transfer-encoding: chunked* is set, and
1549 all the write from the funcion *AppletHTTP:send()* become a chunk.
1550
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01001551 :param class_AppletHTTP applet: An :ref:`applethttp_class`
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02001552 :param string name: the header name
1553 :param string value: the header value
1554
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01001555.. js:function:: AppletHTTP.start_response(applet)
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02001556
1557 This function indicates to the HTTP engine that it can process and send the
1558 response headers. After this called we cannot add headers to the response; We
1559 cannot use the *AppletHTTP:send()* function if the
1560 *AppletHTTP:start_response()* is not called.
1561
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01001562 :param class_AppletHTTP applet: An :ref:`applethttp_class`
1563
1564.. js:function:: AppletHTTP.getline(applet)
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02001565
1566 This function returns a string containing one line from the http body. If the
1567 data returned doesn't contains a final '\\n' its assumed than its the last
1568 available data before the end of stream.
1569
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01001570 :param class_AppletHTTP applet: An :ref:`applethttp_class`
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02001571 :returns: a string. The string can be empty if we reach the end of the stream.
1572
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01001573.. js:function:: AppletHTTP.receive(applet, [size])
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02001574
1575 Reads data from the HTTP body, according to the specified read *size*. If the
1576 *size* is missing, the function tries to read all the content of the stream
1577 until the end. If the *size* is bigger than the http body, it returns the
1578 amount of data avalaible.
1579
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01001580 :param class_AppletHTTP applet: An :ref:`applethttp_class`
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02001581 :param integer size: the required read size.
1582 :returns: always return a string,the string can be empty is the connexion is
1583 closed.
1584
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01001585.. js:function:: AppletHTTP.send(applet, msg)
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02001586
1587 Send the message *msg* on the http request body.
1588
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01001589 :param class_AppletHTTP applet: An :ref:`applethttp_class`
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02001590 :param string msg: the message to send.
1591
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01001592.. js:function:: AppletHTTP.get_priv(applet)
1593
1594 Return Lua data stored in the current transaction (with the
1595 `AppletHTTP.set_priv()`) function. If no data are stored, it returns a nil
1596 value.
1597
1598 :param class_AppletHTTP applet: An :ref:`applethttp_class`
1599 :returns: the opaque data previsously stored, or nil if nothing is
1600 avalaible.
1601
1602.. js:function:: AppletHTTP.set_priv(applet, data)
1603
1604 Store any data in the current HAProxy transaction. This action replace the
1605 old stored data.
1606
1607 :param class_AppletHTTP applet: An :ref:`applethttp_class`
1608 :param opaque data: The data which is stored in the transaction.
1609
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001610.. _applettcp_class:
1611
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02001612AppletTCP class
1613===============
1614
1615.. js:class:: AppletTCP
1616
1617 This class is used with applets that requires the 'tcp' mode. The tcp applet
1618 can be registered with the *core.register_service()* function. They are used
1619 for processing a tcp stream like a server in back of HAProxy.
1620
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001621.. js:attribute:: AppletTCP.c
1622
1623 :returns: A :ref:`converters_class`
1624
1625 This attribute contains a Converters class object.
1626
1627.. js:attribute:: AppletTCP.sc
1628
1629 :returns: A :ref:`converters_class`
1630
1631 This attribute contains a Converters class object. The
1632 functions of this object returns always a string.
1633
1634.. js:attribute:: AppletTCP.f
1635
1636 :returns: A :ref:`fetches_class`
1637
1638 This attribute contains a Fetches class object.
1639
1640.. js:attribute:: AppletTCP.sf
1641
1642 :returns: A :ref:`fetches_class`
1643
1644 This attribute contains a Fetches class object.
1645
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01001646.. js:function:: AppletTCP.getline(applet)
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02001647
1648 This function returns a string containing one line from the stream. If the
1649 data returned doesn't contains a final '\\n' its assumed than its the last
1650 available data before the end of stream.
1651
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01001652 :param class_AppletTCP applet: An :ref:`applettcp_class`
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02001653 :returns: a string. The string can be empty if we reach the end of the stream.
1654
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01001655.. js:function:: AppletTCP.receive(applet, [size])
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02001656
1657 Reads data from the TCP stream, according to the specified read *size*. If the
1658 *size* is missing, the function tries to read all the content of the stream
1659 until the end.
1660
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01001661 :param class_AppletTCP applet: An :ref:`applettcp_class`
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02001662 :param integer size: the required read size.
1663 :returns: always return a string,the string can be empty is the connexion is
1664 closed.
1665
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01001666.. js:function:: AppletTCP.send(appletmsg)
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02001667
1668 Send the message on the stream.
1669
Thierry FOURNIERe34a78e2015-12-25 01:31:35 +01001670 :param class_AppletTCP applet: An :ref:`applettcp_class`
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02001671 :param string msg: the message to send.
1672
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01001673.. js:function:: AppletTCP.get_priv(applet)
1674
1675 Return Lua data stored in the current transaction (with the
1676 `AppletTCP.set_priv()`) function. If no data are stored, it returns a nil
1677 value.
1678
1679 :param class_AppletTCP applet: An :ref:`applettcp_class`
1680 :returns: the opaque data previsously stored, or nil if nothing is
1681 avalaible.
1682
1683.. js:function:: AppletTCP.set_priv(applet, data)
1684
1685 Store any data in the current HAProxy transaction. This action replace the
1686 old stored data.
1687
1688 :param class_AppletTCP applet: An :ref:`applettcp_class`
1689 :param opaque data: The data which is stored in the transaction.
1690
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001691External Lua libraries
1692======================
1693
1694A lot of useful lua libraries can be found here:
1695
1696* `https://lua-toolbox.com/ <https://lua-toolbox.com/>`_
1697
1698Redis acces:
1699
1700* `https://github.com/nrk/redis-lua <https://github.com/nrk/redis-lua>`_
1701
1702This is an example about the usage of the Redis library with HAProxy. Note that
1703each call of any function of this library can throw an error if the socket
1704connection fails.
1705
1706.. code-block:: lua
1707
1708 -- load the redis library
1709 local redis = require("redis");
1710
1711 function do_something(txn)
1712
1713 -- create and connect new tcp socket
1714 local tcp = core.tcp();
1715 tcp:settimeout(1);
1716 tcp:connect("127.0.0.1", 6379);
1717
1718 -- use the redis library with this new socket
1719 local client = redis.connect({socket=tcp});
1720 client:ping();
1721
1722 end
1723
1724OpenSSL:
1725
1726* `http://mkottman.github.io/luacrypto/index.html
1727 <http://mkottman.github.io/luacrypto/index.html>`_
1728
1729* `https://github.com/brunoos/luasec/wiki
1730 <https://github.com/brunoos/luasec/wiki>`_
Thierry FOURNIER2e4893c2015-03-18 13:37:27 +01001731