blob: cab894303fdce9bbcbe5c5984a3b3fd349887eae [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 FOURNIER17bd1522015-03-11 20:31:00 +01001112
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001113
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001114
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001115
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001116
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001117
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001118.. js:function:: TXN.set_loglevel(txn, loglevel)
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01001119
1120 Is used to change the log level of the current request. The "loglevel" must
1121 be an integer between 0 and 7.
1122
1123 :param class_txn txn: The class txn object containing the data.
1124 :param integer loglevel: The required log level. This variable can be one of
1125 :see: core.<loglevel>
1126
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001127.. js:function:: TXN.set_tos(txn, tos)
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01001128
1129 Is used to set the TOS or DSCP field value of packets sent to the client to
1130 the value passed in "tos" on platforms which support this.
1131
1132 :param class_txn txn: The class txn object containing the data.
1133 :param integer tos: The new TOS os DSCP.
1134
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001135.. js:function:: TXN.set_mark(txn, mark)
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01001136
1137 Is used to set the Netfilter MARK on all packets sent to the client to the
1138 value passed in "mark" on platforms which support it.
1139
1140 :param class_txn txn: The class txn object containing the data.
1141 :param integer mark: The mark value.
1142
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001143.. _socket_class:
1144
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001145Socket class
1146============
1147
1148.. js:class:: Socket
1149
1150 This class must be compatible with the Lua Socket class. Only the 'client'
1151 functions are available. See the Lua Socket documentation:
1152
1153 `http://w3.impa.br/~diego/software/luasocket/tcp.html
1154 <http://w3.impa.br/~diego/software/luasocket/tcp.html>`_
1155
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001156.. js:function:: Socket.close(socket)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001157
1158 Closes a TCP object. The internal socket used by the object is closed and the
1159 local address to which the object was bound is made available to other
1160 applications. No further operations (except for further calls to the close
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001161 method) are allowed on a closed Socket.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001162
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001163 :param class_socket socket: Is the manipulated Socket.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001164
1165 Note: It is important to close all used sockets once they are not needed,
1166 since, in many systems, each socket uses a file descriptor, which are limited
1167 system resources. Garbage-collected objects are automatically closed before
1168 destruction, though.
1169
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02001170.. js:function:: Socket.connect(socket, address[, port])
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001171
1172 Attempts to connect a socket object to a remote host.
1173
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001174
1175 In case of error, the method returns nil followed by a string describing the
1176 error. In case of success, the method returns 1.
1177
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001178 :param class_socket socket: Is the manipulated Socket.
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02001179 :param string address: can be an IP address or a host name. See below for more
1180 information.
1181 :param integer port: must be an integer number in the range [1..64K].
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001182 :returns: 1 or nil.
1183
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02001184 an address field extension permits to use the connect() function to connect to
1185 other stream than TCP. The syntax containing a simpleipv4 or ipv6 address is
1186 the basically expected format. This format requires the port.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001187
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02001188 Other format accepted are a socket path like "/socket/path", it permits to
1189 connect to a socket. abstract namespaces are supported with the prefix
1190 "abns@", and finaly a filedescriotr can be passed with the prefix "fd@".
1191 The prefix "ipv4@", "ipv6@" and "unix@" are also supported. The port can be
1192 passed int the string. The syntax "127.0.0.1:1234" is valid. in this case, the
1193 parameter *port* is ignored.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001194
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001195.. js:function:: Socket.connect_ssl(socket, address, port)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001196
1197 Same behavior than the function socket:connect, but uses SSL.
1198
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001199 :param class_socket socket: Is the manipulated Socket.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001200 :returns: 1 or nil.
1201
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001202.. js:function:: Socket.getpeername(socket)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001203
1204 Returns information about the remote side of a connected client object.
1205
1206 Returns a string with the IP address of the peer, followed by the port number
1207 that peer is using for the connection. In case of error, the method returns
1208 nil.
1209
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001210 :param class_socket socket: Is the manipulated Socket.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001211 :returns: a string containing the server information.
1212
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001213.. js:function:: Socket.getsockname(socket)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001214
1215 Returns the local address information associated to the object.
1216
1217 The method returns a string with local IP address and a number with the port.
1218 In case of error, the method returns nil.
1219
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001220 :param class_socket socket: Is the manipulated Socket.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001221 :returns: a string containing the client information.
1222
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001223.. js:function:: Socket.receive(socket, [pattern [, prefix]])
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001224
1225 Reads data from a client object, according to the specified read pattern.
1226 Patterns follow the Lua file I/O format, and the difference in performance
1227 between all patterns is negligible.
1228
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001229 :param class_socket socket: Is the manipulated Socket.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001230 :param string|integer pattern: Describe what is required (see below).
1231 :param string prefix: A string which will be prefix the returned data.
1232 :returns: a string containing the required data or nil.
1233
1234 Pattern can be any of the following:
1235
1236 * **`*a`**: reads from the socket until the connection is closed. No
1237 end-of-line translation is performed;
1238
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001239 * **`*l`**: reads a line of text from the Socket. The line is terminated by a
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001240 LF character (ASCII 10), optionally preceded by a CR character
1241 (ASCII 13). The CR and LF characters are not included in the
1242 returned line. In fact, all CR characters are ignored by the
1243 pattern. This is the default pattern.
1244
1245 * **number**: causes the method to read a specified number of bytes from the
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001246 Socket. Prefix is an optional string to be concatenated to the
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001247 beginning of any received data before return.
1248
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02001249 * **empty**: If the pattern is left empty, the default option is `*l`.
1250
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001251 If successful, the method returns the received pattern. In case of error, the
1252 method returns nil followed by an error message which can be the string
1253 'closed' in case the connection was closed before the transmission was
1254 completed or the string 'timeout' in case there was a timeout during the
1255 operation. Also, after the error message, the function returns the partial
1256 result of the transmission.
1257
1258 Important note: This function was changed severely. It used to support
1259 multiple patterns (but I have never seen this feature used) and now it
1260 doesn't anymore. Partial results used to be returned in the same way as
1261 successful results. This last feature violated the idea that all functions
1262 should return nil on error. Thus it was changed too.
1263
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001264.. js:function:: Socket.send(socket, data [, start [, end ]])
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001265
1266 Sends data through client object.
1267
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001268 :param class_socket socket: Is the manipulated Socket.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001269 :param string data: The data that will be sent.
1270 :param integer start: The start position in the buffer of the data which will
1271 be sent.
1272 :param integer end: The end position in the buffer of the data which will
1273 be sent.
1274 :returns: see below.
1275
1276 Data is the string to be sent. The optional arguments i and j work exactly
1277 like the standard string.sub Lua function to allow the selection of a
1278 substring to be sent.
1279
1280 If successful, the method returns the index of the last byte within [start,
1281 end] that has been sent. Notice that, if start is 1 or absent, this is
1282 effectively the total number of bytes sent. In case of error, the method
1283 returns nil, followed by an error message, followed by the index of the last
1284 byte within [start, end] that has been sent. You might want to try again from
1285 the byte following that. The error message can be 'closed' in case the
1286 connection was closed before the transmission was completed or the string
1287 'timeout' in case there was a timeout during the operation.
1288
1289 Note: Output is not buffered. For small strings, it is always better to
1290 concatenate them in Lua (with the '..' operator) and send the result in one
1291 call instead of calling the method several times.
1292
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001293.. js:function:: Socket.setoption(socket, option [, value])
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001294
1295 Just implemented for compatibility, this cal does nothing.
1296
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001297.. js:function:: Socket.settimeout(socket, value [, mode])
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001298
1299 Changes the timeout values for the object. All I/O operations are blocking.
1300 That is, any call to the methods send, receive, and accept will block
1301 indefinitely, until the operation completes. The settimeout method defines a
1302 limit on the amount of time the I/O methods can block. When a timeout time
1303 has elapsed, the affected methods give up and fail with an error code.
1304
1305 The amount of time to wait is specified as the value parameter, in seconds.
1306
1307 The timeout modes are bot implemented, the only settable timeout is the
1308 inactivity time waiting for complete the internal buffer send or waiting for
1309 receive data.
1310
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001311 :param class_socket socket: Is the manipulated Socket.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001312 :param integer value: The timeout value.
1313
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001314.. _map_class:
1315
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001316Map class
1317=========
1318
1319.. js:class:: Map
1320
1321 This class permits to do some lookup in HAProxy maps. The declared maps can
1322 be modified during the runtime throught the HAProxy management socket.
1323
1324.. code-block:: lua
1325
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001326 default = "usa"
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001327
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001328 -- Create and load map
1329 geo = Map.new("geo.map", Map.ip);
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001330
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001331 -- Create new fetch that returns the user country
1332 core.register_fetches("country", function(txn)
1333 local src;
1334 local loc;
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001335
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001336 src = txn.f:fhdr("x-forwarded-for");
1337 if (src == nil) then
1338 src = txn.f:src()
1339 if (src == nil) then
1340 return default;
1341 end
1342 end
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001343
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001344 -- Perform lookup
1345 loc = geo:lookup(src);
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001346
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001347 if (loc == nil) then
1348 return default;
1349 end
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001350
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001351 return loc;
1352 end);
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001353
1354.. js:attribute:: Map.int
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.ip
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.str
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.beg
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.sub
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.dir
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.dom
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.end
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.. js:attribute:: Map.reg
1403
1404 See the HAProxy configuration.txt file, chapter "Using ACLs and fetching
1405 samples" ans subchapter "ACL basics" to understand this pattern matching
1406 method.
1407
1408
1409.. js:function:: Map.new(file, method)
1410
1411 Creates and load a map.
1412
1413 :param string file: Is the file containing the map.
1414 :param integer method: Is the map pattern matching method. See the attributes
1415 of the Map class.
1416 :returns: a class Map object.
1417 :see: The Map attributes.
1418
1419.. js:function:: Map.lookup(map, str)
1420
1421 Perform a lookup in a map.
1422
1423 :param class_map map: Is the class Map object.
1424 :param string str: Is the string used as key.
1425 :returns: a string containing the result or nil if no match.
1426
1427.. js:function:: Map.slookup(map, str)
1428
1429 Perform a lookup in a map.
1430
1431 :param class_map map: Is the class Map object.
1432 :param string str: Is the string used as key.
1433 :returns: a string containing the result or empty string if no match.
1434
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001435.. _applethttp_class:
1436
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02001437AppletHTTP class
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001438================
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02001439
1440.. js:class:: AppletHTTP
1441
1442 This class is used with applets that requires the 'http' mode. The http applet
1443 can be registered with the *core.register_service()* function. They are used
1444 for processing an http request like a server in back of HAProxy.
1445
1446 This is an hello world sample code:
1447
1448.. code-block:: lua
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001449
Pieter Baauw4d7f7662015-11-08 16:38:08 +01001450 core.register_service("hello-world", "http", function(applet)
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02001451 local response = "Hello World !"
1452 applet:set_status(200)
Pieter Baauw2dcb9bc2015-10-01 22:47:12 +02001453 applet:add_header("content-length", string.len(response))
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02001454 applet:add_header("content-type", "text/plain")
Pieter Baauw2dcb9bc2015-10-01 22:47:12 +02001455 applet:start_response()
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02001456 applet:send(response)
1457 end)
1458
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001459.. js:attribute:: AppletHTTP.c
1460
1461 :returns: A :ref:`converters_class`
1462
1463 This attribute contains a Converters class object.
1464
1465.. js:attribute:: AppletHTTP.sc
1466
1467 :returns: A :ref:`converters_class`
1468
1469 This attribute contains a Converters class object. The
1470 functions of this object returns always a string.
1471
1472.. js:attribute:: AppletHTTP.f
1473
1474 :returns: A :ref:`fetches_class`
1475
1476 This attribute contains a Fetches class object. Note that the
1477 applet execution place cannot access to a valid HAProxy core HTTP
1478 transaction, so some sample fecthes related to the HTTP dependant
1479 values (hdr, path, ...) are not available.
1480
1481.. js:attribute:: AppletHTTP.sf
1482
1483 :returns: A :ref:`fetches_class`
1484
1485 This attribute contains a Fetches class object. The functions of
1486 this object returns always a string. Note that the applet
1487 execution place cannot access to a valid HAProxy core HTTP
1488 transaction, so some sample fecthes related to the HTTP dependant
1489 values (hdr, path, ...) are not available.
1490
1491.. js:attribute:: AppletHTTP.method (string)
1492
1493 :returns: string
1494
1495 The attribute method returns a string containing the HTTP
1496 method.
1497
1498.. js:attribute:: AppletHTTP.version
1499
1500 :returns: string
1501
1502 The attribute version, returns a string containing the HTTP
1503 request version.
1504
1505.. js:attribute:: AppletHTTP.path
1506
1507 :returns: string
1508
1509 The attribute path returns a string containing the HTTP
1510 request path.
1511
1512.. js:attribute:: AppletHTTP.qs
1513
1514 :returns: string
1515
1516 The attribute qs returns a string containing the HTTP
1517 request query string.
1518
1519.. js:attribute:: AppletHTTP.length
1520
1521 :returns: integer
1522
1523 The attribute length returns an integer containing the HTTP
1524 body length.
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02001525
Thierry FOURNIER841475e2015-12-11 17:10:09 +01001526.. js:attribute:: AppletHTTP.headers
1527
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001528 :returns: array
1529
1530 The attribute headers returns an array containing the HTTP
1531 headers. The header names are always in lower case. As the header name can be
1532 encountered more than once in each request, the value is indexed with 0 as
1533 first index value. The array have this form:
1534
1535.. code-block:: lua
1536
1537 AppletHTTP.headers['<header-name>'][<header-index>] = "<header-value>"
1538
1539 AppletHTTP.headers["host"][0] = "www.test.com"
1540 AppletHTTP.headers["accept"][0] = "audio/basic q=1"
1541 AppletHTTP.headers["accept"][1] = "audio/*, q=0.2"
1542 AppletHTTP.headers["accept"][2] = "*.*, q=0.1"
1543..
1544
1545.. js:attribute:: AppletHTTP.headers
1546
Thierry FOURNIER841475e2015-12-11 17:10:09 +01001547 Contains an array containing all the request headers.
1548
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02001549.. js:function:: AppletHTTP.set_status(code)
1550
1551 This function sets the HTTP status code for the response. The allowed code are
1552 from 100 to 599.
1553
1554 :param integer code: the status code returned to the client.
1555
1556.. js:function:: AppletHTTP.add_header(name, value)
1557
1558 This function add an header in the response. Duplicated headers are not
1559 collapsed. The special header *content-length* is used to determinate the
1560 response length. If it not exists, a *transfer-encoding: chunked* is set, and
1561 all the write from the funcion *AppletHTTP:send()* become a chunk.
1562
1563 :param string name: the header name
1564 :param string value: the header value
1565
1566.. js:function:: AppletHTTP.start_response()
1567
1568 This function indicates to the HTTP engine that it can process and send the
1569 response headers. After this called we cannot add headers to the response; We
1570 cannot use the *AppletHTTP:send()* function if the
1571 *AppletHTTP:start_response()* is not called.
1572
1573.. js:function:: AppletHTTP.getline()
1574
1575 This function returns a string containing one line from the http body. If the
1576 data returned doesn't contains a final '\\n' its assumed than its the last
1577 available data before the end of stream.
1578
1579 :returns: a string. The string can be empty if we reach the end of the stream.
1580
1581.. js:function:: AppletHTTP.receive([size])
1582
1583 Reads data from the HTTP body, according to the specified read *size*. If the
1584 *size* is missing, the function tries to read all the content of the stream
1585 until the end. If the *size* is bigger than the http body, it returns the
1586 amount of data avalaible.
1587
1588 :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
1592.. js:function:: AppletHTTP.send(msg)
1593
1594 Send the message *msg* on the http request body.
1595
1596 :param string msg: the message to send.
1597
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001598.. _applettcp_class:
1599
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02001600AppletTCP class
1601===============
1602
1603.. js:class:: AppletTCP
1604
1605 This class is used with applets that requires the 'tcp' mode. The tcp applet
1606 can be registered with the *core.register_service()* function. They are used
1607 for processing a tcp stream like a server in back of HAProxy.
1608
Thierry FOURNIERdc595002015-12-21 11:13:52 +01001609.. js:attribute:: AppletTCP.c
1610
1611 :returns: A :ref:`converters_class`
1612
1613 This attribute contains a Converters class object.
1614
1615.. js:attribute:: AppletTCP.sc
1616
1617 :returns: A :ref:`converters_class`
1618
1619 This attribute contains a Converters class object. The
1620 functions of this object returns always a string.
1621
1622.. js:attribute:: AppletTCP.f
1623
1624 :returns: A :ref:`fetches_class`
1625
1626 This attribute contains a Fetches class object.
1627
1628.. js:attribute:: AppletTCP.sf
1629
1630 :returns: A :ref:`fetches_class`
1631
1632 This attribute contains a Fetches class object.
1633
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02001634.. js:function:: AppletTCP.getline()
1635
1636 This function returns a string containing one line from the stream. If the
1637 data returned doesn't contains a final '\\n' its assumed than its the last
1638 available data before the end of stream.
1639
1640 :returns: a string. The string can be empty if we reach the end of the stream.
1641
1642.. js:function:: AppletTCP.receive([size])
1643
1644 Reads data from the TCP stream, according to the specified read *size*. If the
1645 *size* is missing, the function tries to read all the content of the stream
1646 until the end.
1647
1648 :param integer size: the required read size.
1649 :returns: always return a string,the string can be empty is the connexion is
1650 closed.
1651
1652.. js:function:: AppletTCP.send(msg)
1653
1654 Send the message on the stream.
1655
1656 :param string msg: the message to send.
1657
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001658External Lua libraries
1659======================
1660
1661A lot of useful lua libraries can be found here:
1662
1663* `https://lua-toolbox.com/ <https://lua-toolbox.com/>`_
1664
1665Redis acces:
1666
1667* `https://github.com/nrk/redis-lua <https://github.com/nrk/redis-lua>`_
1668
1669This is an example about the usage of the Redis library with HAProxy. Note that
1670each call of any function of this library can throw an error if the socket
1671connection fails.
1672
1673.. code-block:: lua
1674
1675 -- load the redis library
1676 local redis = require("redis");
1677
1678 function do_something(txn)
1679
1680 -- create and connect new tcp socket
1681 local tcp = core.tcp();
1682 tcp:settimeout(1);
1683 tcp:connect("127.0.0.1", 6379);
1684
1685 -- use the redis library with this new socket
1686 local client = redis.connect({socket=tcp});
1687 client:ping();
1688
1689 end
1690
1691OpenSSL:
1692
1693* `http://mkottman.github.io/luacrypto/index.html
1694 <http://mkottman.github.io/luacrypto/index.html>`_
1695
1696* `https://github.com/brunoos/luasec/wiki
1697 <https://github.com/brunoos/luasec/wiki>`_
Thierry FOURNIER2e4893c2015-03-18 13:37:27 +01001698