blob: 1f02541ffe1b4e35e4e828c6819ade9c486e5668 [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
302. The Lua **init context**. It is an Lua function executed just after the
31 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
383. The Lua **task context**. It is an Lua function executed after the start
39 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
454. The **action context**. It is an Lua function conditionally executed. These
46 actions are declared by the HAProxy directives "`tcp-request content lua
47 <function>`", "`tcp-response content lua <function>`", "`http-request lua
48 <function>`" and "`http-response lua <function>`". The prototype of the
49 Lua called function is a function with doesn't returns anything and that take
50 an object of class TXN as entry. `function fcn(txn)`
51
525. The **sample-fetch context**. This function takes a TXN object as entry
53 argument and returns a string. These types of function cannot execute any
54 blocking function. They are useful to aggregate some of original HAProxy
55 sample-fetches and return the result. The prototype of the function is
56 `function string fcn(txn)`. These functions can be registered with the Lua
57 function `core.register_fetches()`. Each declared sample-fetch is prefixed by
58 the string "lua.".
59
60 **NOTE**: It is possible that this function cannot found the required data
61 in the original HAProxy sample-fetches, in this case, it cannot return the
62 result. This case is not yet supported
63
646. The **converter context**. It is an Lua function that takes a string as input
65 and returns another string as output. These types of function are stateless,
66 it cannot access to any context. They don't execute any blocking function.
67 The call prototype is `function string fcn(string)`. This function can be
68 registered with the Lua function `core.register_converters()`. Each declared
69 converter is prefixed by the string "lua.".
70
71HAProxy Lua Hello world
72-----------------------
73
74HAProxy configuration file (`hello_world.conf`):
75
76::
77
78 global
79 lua-load hello_world.lua
80
81 listen proxy
82 bind 127.0.0.1:10001
83 tcp-request content lua hello_world
84
85HAProxy Lua file (`hello_world.lua`):
86
87.. code-block:: lua
88
89 function hello_world(txn)
Thierry FOURNIER486f5a02015-03-16 15:13:03 +010090 txn.res:send("hello world\n")
Willy Tarreau99a36dd2015-08-27 14:57:04 +020091 txn:done()
Thierry FOURNIER17bd1522015-03-11 20:31:00 +010092 end
93
94How to start HAProxy for testing this configuration:
95
96::
97
98 ./haproxy -f hello_world.conf
99
100On other terminal, you can test with telnet:
101
102::
103
104 #:~ telnet 127.0.0.1 10001
105 hello world
106
107Core class
108==========
109
110.. js:class:: core
111
112 The "core" class contains all the HAProxy core functions. These function are
113 useful for the controlling the execution flow, registering hooks, manipulating
114 global maps or ACL, ...
115
116 "core" class is basically provided with HAProxy. No `require` line is
117 required to uses these function.
118
119 The "core" class is static, t is not possible to create a new object of this
120 type.
121
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100122.. js:attribute:: core.emerg
123
124 This attribute is an integer, it contains the value of the loglevel "emergency" (0).
125
126.. js:attribute:: core.alert
127
128 This attribute is an integer, it contains the value of the loglevel "alert" (1).
129
130.. js:attribute:: core.crit
131
132 This attribute is an integer, it contains the value of the loglevel "critical" (2).
133
134.. js:attribute:: core.err
135
136 This attribute is an integer, it contains the value of the loglevel "error" (3).
137
138.. js:attribute:: core.warning
139
140 This attribute is an integer, it contains the value of the loglevel "warning" (4).
141
142.. js:attribute:: core.notice
143
144 This attribute is an integer, it contains the value of the loglevel "notice" (5).
145
146.. js:attribute:: core.info
147
148 This attribute is an integer, it contains the value of the loglevel "info" (6).
149
150.. js:attribute:: core.debug
151
152 This attribute is an integer, it contains the value of the loglevel "debug" (7).
153
154.. js:function:: core.log(loglevel, msg)
155
156 **context**: body, init, task, action, sample-fetch, converter
157
158 This fucntion sends a log. The log is sent, according with the HAProxy
159 configuration file, on the default syslog server if it is configured and on
160 the stderr if it is allowed.
161
162 :param integer loglevel: Is the log level asociated with the message. It is a
163 number between 0 and 7.
164 :param string msg: The log content.
165 :see: core.emerg, core.alert, core.crit, core.err, core.warning, core.notice,
166 core.info, core.debug (log level definitions)
167 :see: code.Debug
168 :see: core.Info
169 :see: core.Warning
170 :see: core.Alert
171
172.. js:function:: core.Debug(msg)
173
174 **context**: body, init, task, action, sample-fetch, converter
175
176 :param string msg: The log content.
177 :see: log
178
179 Does the same job than:
180
181.. code-block:: lua
182
183 function Debug(msg)
184 core.log(core.debug, msg)
185 end
186..
187
188.. js:function:: core.Info(msg)
189
190 **context**: body, init, task, action, sample-fetch, converter
191
192 :param string msg: The log content.
193 :see: log
194
195.. code-block:: lua
196
197 function Info(msg)
198 core.log(core.info, msg)
199 end
200..
201
202.. js:function:: core.Warning(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
211 function Warning(msg)
212 core.log(core.warning, msg)
213 end
214..
215
216.. js:function:: core.Alert(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
225 function Alert(msg)
226 core.log(core.alert, msg)
227 end
228..
229
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100230.. js:function:: core.add_acl(filename, key)
231
232 **context**: init, task, action, sample-fetch, converter
233
234 Add the ACL *key* in the ACLs list referenced by the file *filename*.
235
236 :param string filename: the filename that reference the ACL entries.
237 :param string key: the key which will be added.
238
239.. js:function:: core.del_acl(filename, key)
240
241 **context**: init, task, action, sample-fetch, converter
242
243 Delete the ACL entry referenced by the key *key* in the list of ACLs
244 referenced by *filename*.
245
246 :param string filename: the filename that reference the ACL entries.
247 :param string key: the key which will be deleted.
248
249.. js:function:: core.del_map(filename, key)
250
251 **context**: init, task, action, sample-fetch, converter
252
253 Delete the map entry indexed with the specified key in the list of maps
254 referenced by his filename.
255
256 :param string filename: the filename that reference the map entries.
257 :param string key: the key which will be deleted.
258
259.. js:function:: core.msleep(milliseconds)
260
261 **context**: body, init, task, action
262
263 The `core.msleep()` stops the Lua execution between specified milliseconds.
264
265 :param integer milliseconds: the required milliseconds.
266
Thierry FOURNIER8255a752015-09-23 21:03:35 +0200267.. js:function:: core.register_action(name, actions, func)
268
269 **context**: body
270
271 Register an Lua function executed as action. All the registered action can be
272 used in HAProxy with the prefix "lua.". An action gets a TXN object class as
273 input.
274
275 :param string name: is the name of the converter.
Willy Tarreau61add3c2015-09-28 15:39:10 +0200276 :param table actions: is a table of string describing the HAProxy actions who
Thierry FOURNIER8255a752015-09-23 21:03:35 +0200277 want to register to. The expected actions are 'tcp-req',
278 'tcp-res', 'http-req' or 'http-res'.
279 :param function func: is the Lua function called to work as converter.
280
281 The prototype of the Lua function used as argument is:
282
283.. code-block:: lua
284
285 function(txn)
286..
287
288 * **txn** (*class TXN*): this is a TXN object used for manipulating the
289 current request or TCP stream.
290
Willy Tarreau61add3c2015-09-28 15:39:10 +0200291 Here, an exemple of action registration. the action juste send an 'Hello world'
Thierry FOURNIER8255a752015-09-23 21:03:35 +0200292 in the logs.
293
294.. code-block:: lua
295
296 core.register_action("hello-world", { "tcp-req", "http-req" }, function(txn)
297 txn:Info("Hello world")
298 end)
299..
300
301 This example code is used in HAproxy configuration like this:
302
303::
304
305 frontend tcp_frt
306 mode tcp
307 tcp-request content lua.hello-world
308
309 frontend http_frt
310 mode http
311 http-request lua.hello-world
312
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100313.. js:function:: core.register_converters(name, func)
314
315 **context**: body
316
317 Register an Lua function executed as converter. All the registered converters
318 can be used in HAProxy with the prefix "lua.". An converter get a string as
319 input and return a string as output. The registered function can take up to 9
320 values as parameter. All the value are strings.
321
322 :param string name: is the name of the converter.
323 :param function func: is the Lua function called to work as converter.
324
325 The prototype of the Lua function used as argument is:
326
327.. code-block:: lua
328
329 function(str, [p1 [, p2 [, ... [, p5]]]])
330..
331
332 * **str** (*string*): this is the input value automatically converted in
333 string.
334 * **p1** .. **p5** (*string*): this is a list of string arguments declared in
335 the haroxy configuration file. The number of arguments doesn't exceed 5.
336 The order and the nature of these is conventionally choose by the
337 developper.
338
339.. js:function:: core.register_fetches(name, func)
340
341 **context**: body
342
343 Register an Lua function executed as sample fetch. All the registered sample
344 fetchs can be used in HAProxy with the prefix "lua.". A Lua sample fetch
345 return a string as output. The registered function can take up to 9 values as
346 parameter. All the value are strings.
347
348 :param string name: is the name of the converter.
349 :param function func: is the Lua function called to work as sample fetch.
350
351 The prototype of the Lua function used as argument is:
352
353.. code-block:: lua
354
355 string function(txn, [p1 [, p2 [, ... [, p5]]]])
356..
357
358 * **txn** (*class txn*): this is the txn object associated with the current
359 request.
360 * **p1** .. **p5** (*string*): this is a list of string arguments declared in
361 the haroxy configuration file. The number of arguments doesn't exceed 5.
362 The order and the nature of these is conventionally choose by the
363 developper.
364 * **Returns**: A string containing some data, ot nil if the value cannot be
365 returned now.
366
367 lua example code:
368
369.. code-block:: lua
370
371 core.register_fetches("hello", function(txn)
372 return "hello"
373 end)
374..
375
376 HAProxy example configuration:
377
378::
379
380 frontend example
381 http-request redirect location /%[lua.hello]
382
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +0200383.. js:function:: core.register_service(name, mode, func)
384
385 **context**: body
386
387 Register an Lua function executed as a service. All the registered service can
388 be used in HAProxy with the prefix "lua.". A service gets an object class as
389 input according with the required mode.
390
391 :param string name: is the name of the converter.
Willy Tarreau61add3c2015-09-28 15:39:10 +0200392 :param string mode: is string describing the required mode. Only 'tcp' or
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +0200393 'http' are allowed.
394 :param function func: is the Lua function called to work as converter.
395
396 The prototype of the Lua function used as argument is:
397
398.. code-block:: lua
399
400 function(applet)
401..
402
403 * **txn** (*class AppletTCP*) or (*class AppletHTTP*): this is an object used
404 for manipulating the current HTTP request or TCP stream.
405
Willy Tarreau61add3c2015-09-28 15:39:10 +0200406 Here, an exemple of service registration. the service just send an 'Hello world'
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +0200407 as an http response.
408
409.. code-block:: lua
410
411 core.register_service("hello-world", "http" }, function(txn)
412 local response = "Hello World !"
413 applet:set_status(200)
414 applet:add_header("content-length", string.length(response))
415 applet:add_header("content-type", "text/plain")
416 applet:start_reponse()
417 applet:send(response)
418 end)
419..
420
421 This example code is used in HAproxy configuration like this:
422
423::
424
425 frontend example
426 http-request use-service lua.hello-world
427
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100428.. js:function:: core.register_init(func)
429
430 **context**: body
431
432 Register a function executed after the configuration parsing. This is useful
433 to check any parameters.
434
435 :param fuction func: is the Lua function called to work as initializer.
436
437 The prototype of the Lua function used as argument is:
438
439.. code-block:: lua
440
441 function()
442..
443
444 It takes no input, and no output is expected.
445
446.. js:function:: core.register_task(func)
447
448 **context**: body, init, task, action, sample-fetch, converter
449
450 Register and start independent task. The task is started when the HAProxy
451 main scheduler starts. For example this type of tasks can be executed to
Thierry FOURNIER2e4893c2015-03-18 13:37:27 +0100452 perform complex health checks.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100453
454 :param fuction func: is the Lua function called to work as initializer.
455
456 The prototype of the Lua function used as argument is:
457
458.. code-block:: lua
459
460 function()
461..
462
463 It takes no input, and no output is expected.
464
465.. js:function:: core.set_nice(nice)
466
467 **context**: task, action, sample-fetch, converter
468
469 Change the nice of the current task or current session.
Thierry FOURNIER2e4893c2015-03-18 13:37:27 +0100470
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100471 :param integer nice: the nice value, it must be between -1024 and 1024.
472
473.. js:function:: core.set_map(filename, key, value)
474
475 **context**: init, task, action, sample-fetch, converter
476
477 set the value *value* associated to the key *key* in the map referenced by
478 *filename*.
479
480.. js:function:: core.sleep(int seconds)
481
482 **context**: body, init, task, action
483
484 The `core.sleep()` functions stop the Lua execution between specified seconds.
485
486 :param integer seconds: the required seconds.
487
488.. js:function:: core.tcp()
489
490 **context**: init, task, action
491
492 This function returns a new object of a *socket* class.
493
494 :returns: A socket class object.
495
Thierry FOURNIER0a99b892015-08-26 00:14:17 +0200496.. js:function:: core.done(data)
497
498 **context**: body, init, task, action, sample-fetch, converter
499
500 :param any data: Return some data for the caller. It is useful with
501 sample-fetches and sample-converters.
502
503 Immediately stops the current Lua execution and returns to the caller which
504 may be a sample fetch, a converter or an action and returns the specified
505 value (ignored for actions). It is used when the LUA process finishes its
506 work and wants to give back the control to HAProxy without executing the
507 remaining code. It can be seen as a multi-level "return".
508
Thierry FOURNIER486f5a02015-03-16 15:13:03 +0100509.. js:function:: core.yield()
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100510
511 **context**: task, action, sample-fetch, converter
512
513 Give back the hand at the HAProxy scheduler. It is used when the LUA
514 processing consumes a lot of processing time.
515
516Fetches class
517=============
518
519.. js:class:: Fetches
520
521 This class contains a lot of internal HAProxy sample fetches. See the
Thierry FOURNIER2e4893c2015-03-18 13:37:27 +0100522 HAProxy "configuration.txt" documentation for more information about her
523 usage. they are the chapters 7.3.2 to 7.3.6.
524
525 :see: TXN.f
526 :see: TXN.sf
527
528 Fetches are useful for:
529
530 * get system time,
531 * get environment variable,
532 * get random numbers,
533 * known backend status like the number of users in queue or the number of
534 connections established,
535 * client information like ip source or destination,
536 * deal with stick tables,
537 * Established SSL informations,
538 * HTTP information like headers or method.
539
540.. code-block:: lua
541
542 function action(txn)
543 -- Get source IP
544 local clientip = txn.f:src()
545 end
546..
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100547
548Converters class
549================
550
551.. js:class:: Converters
552
553 This class contains a lot of internal HAProxy sample converters. See the
Thierry FOURNIER2e4893c2015-03-18 13:37:27 +0100554 HAProxy documentation "configuration.txt" for more information about her
555 usage. Its the chapter 7.3.1.
556
557 :see: TXN.c
558 :see: TXN.sc
559
560 Converters provides statefull transformation. They are useful for:
561
562 * converting input to base64,
563 * applying hash on input string (djb2, crc32, sdbm, wt6),
564 * format date,
565 * json escape,
566 * extracting prefered language comparing two lists,
567 * turn to lower or upper chars,
568 * deal with stick tables.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100569
570Channel class
571=============
572
573.. js:class:: Channel
574
575 HAProxy uses two buffers for the processing of the requests. The first one is
576 used with the request data (from the client to the server) and the second is
577 used for the response data (from the server to the client).
578
579 Each buffer contains two types of data. The first type is the incoming data
580 waiting for a processing. The second part is the outgoing data already
581 processed. Usually, the incoming data is processed, after it is tagged as
582 outgoing data, and finally it is sent. The following functions provides tools
583 for manipulating these data in a buffer.
584
585 The following diagram shows where the channel class function are applied.
586
587 **Warning**: It is not possible to read from the response in request action,
588 and it is not possible to read for the request channel in response action.
589
Thierry FOURNIER2e4893c2015-03-18 13:37:27 +0100590.. image:: _static/channel.png
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100591
Thierry FOURNIER486f5a02015-03-16 15:13:03 +0100592.. js:function:: Channel.dup(channel)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100593
594 This function returns a string that contain the entire buffer. The data is
595 not remove from the buffer and can be reprocessed later.
596
597 If the buffer cant receive more data, a 'nil' value is returned.
598
Thierry FOURNIER486f5a02015-03-16 15:13:03 +0100599 :param class_channel channel: The manipulated Channel.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100600 :returns: a string containig all the avalaible data or nil.
601
Thierry FOURNIER486f5a02015-03-16 15:13:03 +0100602.. js:function:: Channel.get(channel)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100603
604 This function returns a string that contain the entire buffer. The data is
605 consumed from the buffer.
606
607 If the buffer cant receive more data, a 'nil' value is returned.
608
Thierry FOURNIER486f5a02015-03-16 15:13:03 +0100609 :param class_channel channel: The manipulated Channel.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100610 :returns: a string containig all the avalaible data or nil.
611
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +0200612.. js:function:: Channel.getline(channel)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100613
614 This function returns a string that contain the first line of the buffer. The
615 data is consumed. If the data returned doesn't contains a final '\n' its
616 assumed than its the last available data in the buffer.
617
618 If the buffer cant receive more data, a 'nil' value is returned.
619
Thierry FOURNIER486f5a02015-03-16 15:13:03 +0100620 :param class_channel channel: The manipulated Channel.
Pieter Baauw386a1272015-08-16 15:26:24 +0200621 :returns: a string containing the available line or nil.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100622
Thierry FOURNIER486f5a02015-03-16 15:13:03 +0100623.. js:function:: Channel.set(channel, string)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100624
625 This function replace the content of the buffer by the string. The function
626 returns the copied length, otherwise, it returns -1.
627
628 The data set with this function are not send. They wait for the end of
629 HAProxy processing, so the buffer can be full.
630
Thierry FOURNIER486f5a02015-03-16 15:13:03 +0100631 :param class_channel channel: The manipulated Channel.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100632 :param string string: The data which will sent.
633 :returns: an integer containing the amount of butes copyed or -1.
634
Thierry FOURNIER486f5a02015-03-16 15:13:03 +0100635.. js:function:: Channel.append(channel, string)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100636
637 This function append the string argument to the content of the buffer. The
638 function returns the copied length, otherwise, it returns -1.
639
640 The data set with this function are not send. They wait for the end of
641 HAProxy processing, so the buffer can be full.
642
Thierry FOURNIER486f5a02015-03-16 15:13:03 +0100643 :param class_channel channel: The manipulated Channel.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100644 :param string string: The data which will sent.
645 :returns: an integer containing the amount of butes copyed or -1.
646
Thierry FOURNIER486f5a02015-03-16 15:13:03 +0100647.. js:function:: Channel.send(channel, string)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100648
649 This function required immediate send of the data. Unless if the connection
650 is close, the buffer is regularly flushed and all the string can be sent.
651
Thierry FOURNIER486f5a02015-03-16 15:13:03 +0100652 :param class_channel channel: The manipulated Channel.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100653 :param string string: The data which will sent.
654 :returns: an integer containing the amount of butes copyed or -1.
655
Thierry FOURNIER486f5a02015-03-16 15:13:03 +0100656.. js:function:: Channel.get_in_length(channel)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100657
658 This function returns the length of the input part of the buffer.
659
Thierry FOURNIER486f5a02015-03-16 15:13:03 +0100660 :param class_channel channel: The manipulated Channel.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100661 :returns: an integer containing the amount of avalaible bytes.
662
Thierry FOURNIER486f5a02015-03-16 15:13:03 +0100663.. js:function:: Channel.get_out_length(channel)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100664
665 This function returns the length of the output part of the buffer.
666
Thierry FOURNIER486f5a02015-03-16 15:13:03 +0100667 :param class_channel channel: The manipulated Channel.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100668 :returns: an integer containing the amount of avalaible bytes.
669
Thierry FOURNIER486f5a02015-03-16 15:13:03 +0100670.. js:function:: Channel.forward(channel, int)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100671
672 This function transfer bytes from the input part of the buffer to the output
673 part.
674
Thierry FOURNIER486f5a02015-03-16 15:13:03 +0100675 :param class_channel channel: The manipulated Channel.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100676 :param integer int: The amount of data which will be forwarded.
677
Thierry FOURNIER08504f42015-03-16 14:17:08 +0100678
679HTTP class
680==========
681
682.. js:class:: HTTP
683
684 This class contain all the HTTP manipulation functions.
685
Pieter Baauw386a1272015-08-16 15:26:24 +0200686.. js:function:: HTTP.req_get_headers(http)
Thierry FOURNIER08504f42015-03-16 14:17:08 +0100687
688 Returns an array containing all the request headers.
689
690 :param class_http http: The related http object.
691 :returns: array of headers.
Pieter Baauw386a1272015-08-16 15:26:24 +0200692 :see: HTTP.res_get_headers()
Thierry FOURNIER08504f42015-03-16 14:17:08 +0100693
Pieter Baauw386a1272015-08-16 15:26:24 +0200694.. js:function:: HTTP.res_get_headers(http)
Thierry FOURNIER08504f42015-03-16 14:17:08 +0100695
696 Returns an array containing all the response headers.
697
698 :param class_http http: The related http object.
699 :returns: array of headers.
Pieter Baauw386a1272015-08-16 15:26:24 +0200700 :see: HTTP.req_get_headers()
Thierry FOURNIER08504f42015-03-16 14:17:08 +0100701
Thierry FOURNIER486f5a02015-03-16 15:13:03 +0100702.. js:function:: HTTP.req_add_header(http, name, value)
Thierry FOURNIER08504f42015-03-16 14:17:08 +0100703
704 Appends an HTTP header field in the request whose name is
705 specified in "name" and whose value is defined in "value".
706
707 :param class_http http: The related http object.
708 :param string name: The header name.
709 :param string value: The header value.
Thierry FOURNIER486f5a02015-03-16 15:13:03 +0100710 :see: HTTP.res_add_header()
Thierry FOURNIER08504f42015-03-16 14:17:08 +0100711
Thierry FOURNIER486f5a02015-03-16 15:13:03 +0100712.. js:function:: HTTP.res_add_header(http, name, value)
Thierry FOURNIER08504f42015-03-16 14:17:08 +0100713
714 appends an HTTP header field in the response whose name is
715 specified in "name" and whose value is defined in "value".
716
717 :param class_http http: The related http object.
718 :param string name: The header name.
719 :param string value: The header value.
Thierry FOURNIER486f5a02015-03-16 15:13:03 +0100720 :see: HTTP.req_add_header()
Thierry FOURNIER08504f42015-03-16 14:17:08 +0100721
Thierry FOURNIER486f5a02015-03-16 15:13:03 +0100722.. js:function:: HTTP.req_del_header(http, name)
Thierry FOURNIER08504f42015-03-16 14:17:08 +0100723
724 Removes all HTTP header fields in the request whose name is
725 specified in "name".
726
727 :param class_http http: The related http object.
728 :param string name: The header name.
Thierry FOURNIER486f5a02015-03-16 15:13:03 +0100729 :see: HTTP.res_del_header()
Thierry FOURNIER08504f42015-03-16 14:17:08 +0100730
Thierry FOURNIER486f5a02015-03-16 15:13:03 +0100731.. js:function:: HTTP.res_del_header(http, name)
Thierry FOURNIER08504f42015-03-16 14:17:08 +0100732
733 Removes all HTTP header fields in the response whose name is
734 specified in "name".
735
736 :param class_http http: The related http object.
737 :param string name: The header name.
Thierry FOURNIER486f5a02015-03-16 15:13:03 +0100738 :see: HTTP.req_del_header()
Thierry FOURNIER08504f42015-03-16 14:17:08 +0100739
Thierry FOURNIER486f5a02015-03-16 15:13:03 +0100740.. js:function:: HTTP.req_set_header(http, name, value)
Thierry FOURNIER08504f42015-03-16 14:17:08 +0100741
742 This variable replace all occurence of all header "name", by only
743 one containing the "value".
744
745 :param class_http http: The related http object.
746 :param string name: The header name.
747 :param string value: The header value.
Thierry FOURNIER486f5a02015-03-16 15:13:03 +0100748 :see: HTTP.res_set_header()
Thierry FOURNIER08504f42015-03-16 14:17:08 +0100749
750 This function does the same work as the folowwing code:
751
752.. code-block:: lua
753
754 function fcn(txn)
Thierry FOURNIER486f5a02015-03-16 15:13:03 +0100755 TXN.http:req_del_header("header")
756 TXN.http:req_add_header("header", "value")
Thierry FOURNIER08504f42015-03-16 14:17:08 +0100757 end
758..
759
Thierry FOURNIER486f5a02015-03-16 15:13:03 +0100760.. js:function:: HTTP.res_set_header(http, name, value)
Thierry FOURNIER08504f42015-03-16 14:17:08 +0100761
762 This variable replace all occurence of all header "name", by only
763 one containing the "value".
764
765 :param class_http http: The related http object.
766 :param string name: The header name.
767 :param string value: The header value.
Pieter Baauw386a1272015-08-16 15:26:24 +0200768 :see: HTTP.req_rep_header()
Thierry FOURNIER08504f42015-03-16 14:17:08 +0100769
Pieter Baauw386a1272015-08-16 15:26:24 +0200770.. js:function:: HTTP.req_rep_header(http, name, regex, replace)
Thierry FOURNIER08504f42015-03-16 14:17:08 +0100771
772 Matches the regular expression in all occurrences of header field "name"
773 according to "regex", and replaces them with the "replace" argument. The
774 replacement value can contain back references like \1, \2, ... This
775 function works with the request.
776
777 :param class_http http: The related http object.
778 :param string name: The header name.
779 :param string regex: The match regular expression.
780 :param string replace: The replacement value.
Pieter Baauw386a1272015-08-16 15:26:24 +0200781 :see: HTTP.res_rep_header()
Thierry FOURNIER08504f42015-03-16 14:17:08 +0100782
Pieter Baauw386a1272015-08-16 15:26:24 +0200783.. js:function:: HTTP.res_rep_header(http, name, regex, string)
Thierry FOURNIER08504f42015-03-16 14:17:08 +0100784
785 Matches the regular expression in all occurrences of header field "name"
786 according to "regex", and replaces them with the "replace" argument. The
787 replacement value can contain back references like \1, \2, ... This
788 function works with the request.
789
790 :param class_http http: The related http object.
791 :param string name: The header name.
792 :param string regex: The match regular expression.
793 :param string replace: The replacement value.
Thierry FOURNIER486f5a02015-03-16 15:13:03 +0100794 :see: HTTP.req_replace_header()
Thierry FOURNIER08504f42015-03-16 14:17:08 +0100795
Thierry FOURNIER486f5a02015-03-16 15:13:03 +0100796.. js:function:: HTTP.req_replace_value(http, name, regex, replace)
Thierry FOURNIER08504f42015-03-16 14:17:08 +0100797
Thierry FOURNIER486f5a02015-03-16 15:13:03 +0100798 Works like "HTTP.req_replace_header()" except that it matches the regex
Thierry FOURNIER08504f42015-03-16 14:17:08 +0100799 against every comma-delimited value of the header field "name" instead of the
800 entire header.
801
802 :param class_http http: The related http object.
803 :param string name: The header name.
804 :param string regex: The match regular expression.
805 :param string replace: The replacement value.
Thierry FOURNIER486f5a02015-03-16 15:13:03 +0100806 :see: HTTP.req_replace_header()
807 :see: HTTP.res_replace_value()
Thierry FOURNIER08504f42015-03-16 14:17:08 +0100808
Thierry FOURNIER486f5a02015-03-16 15:13:03 +0100809.. js:function:: HTTP.res_replace_value(http, name, regex, replace)
Thierry FOURNIER08504f42015-03-16 14:17:08 +0100810
Thierry FOURNIER486f5a02015-03-16 15:13:03 +0100811 Works like "HTTP.res_replace_header()" except that it matches the regex
Thierry FOURNIER08504f42015-03-16 14:17:08 +0100812 against every comma-delimited value of the header field "name" instead of the
813 entire header.
814
815 :param class_http http: The related http object.
816 :param string name: The header name.
817 :param string regex: The match regular expression.
818 :param string replace: The replacement value.
Thierry FOURNIER486f5a02015-03-16 15:13:03 +0100819 :see: HTTP.res_replace_header()
820 :see: HTTP.req_replace_value()
Thierry FOURNIER08504f42015-03-16 14:17:08 +0100821
Thierry FOURNIER486f5a02015-03-16 15:13:03 +0100822.. js:function:: HTTP.req_set_method(http, method)
Thierry FOURNIER08504f42015-03-16 14:17:08 +0100823
824 Rewrites the request method with the parameter "method".
825
826 :param class_http http: The related http object.
827 :param string method: The new method.
828
Thierry FOURNIER486f5a02015-03-16 15:13:03 +0100829.. js:function:: HTTP.req_set_path(http, path)
Thierry FOURNIER08504f42015-03-16 14:17:08 +0100830
831 Rewrites the request path with the "path" parameter.
832
833 :param class_http http: The related http object.
834 :param string path: The new path.
835
Thierry FOURNIER486f5a02015-03-16 15:13:03 +0100836.. js:function:: HTTP.req_set_query(http, query)
Thierry FOURNIER08504f42015-03-16 14:17:08 +0100837
838 Rewrites the request's query string which appears after the first question
839 mark ("?") with the parameter "query".
840
841 :param class_http http: The related http object.
842 :param string query: The new query.
843
Thierry FOURNIER0d79cf62015-08-26 14:20:58 +0200844.. js:function:: HTTP.req_set_uri(http, uri)
Thierry FOURNIER08504f42015-03-16 14:17:08 +0100845
846 Rewrites the request URI with the parameter "uri".
847
848 :param class_http http: The related http object.
849 :param string uri: The new uri.
850
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +0200851.. js:function:: HTTP.res_set_status(http, status)
852
853 Rewrites the response status code with the parameter "code". Note that the
854 reason is automatically adapted to the new code.
855
856 :param class_http http: The related http object.
857 :param integer status: The new response status code.
858
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100859TXN class
860=========
861
862.. js:class:: TXN
863
864 The txn class contain all the functions relative to the http or tcp
865 transaction (Note than a tcp stream is the same than a tcp transaction, but
866 an HTTP transaction is not the same than a tcp stream).
867
868 The usage of this class permits to retrieve data from the requests, alter it
869 and forward it.
870
871 All the functions provided by this class are available in the context
872 **sample-fetches** and **actions**.
873
Thierry FOURNIER486f5a02015-03-16 15:13:03 +0100874.. js:attribute:: TXN.c
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100875
876 This attribute contains a Converters class object.
877
Thierry FOURNIER486f5a02015-03-16 15:13:03 +0100878.. js:attribute:: TXN.sc
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100879
880 This attribute contains a Converters class object. The functions of
881 this object returns always a string.
882
Thierry FOURNIER486f5a02015-03-16 15:13:03 +0100883.. js:attribute:: TXN.f
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100884
885 This attribute contains a Fetches class object.
886
Thierry FOURNIER486f5a02015-03-16 15:13:03 +0100887.. js:attribute:: TXN.sf
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100888
889 This attribute contains a Fetches class object. The functions of
890 this object returns always a string.
891
Thierry FOURNIER486f5a02015-03-16 15:13:03 +0100892.. js:attribute:: TXN.req
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100893
894 This attribute contains a channel class object for the request buffer.
895
Thierry FOURNIER486f5a02015-03-16 15:13:03 +0100896.. js:attribute:: TXN.res
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100897
898 This attribute contains a channel class object for the response buffer.
899
Thierry FOURNIER486f5a02015-03-16 15:13:03 +0100900.. js:attribute:: TXN.http
Thierry FOURNIER08504f42015-03-16 14:17:08 +0100901
902 This attribute contains an HTTP class object. It is avalaible only if the
903 proxy has the "mode http" enabled.
904
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100905.. js:function:: TXN.log(TXN, loglevel, msg)
906
907 This function sends a log. The log is sent, according with the HAProxy
908 configuration file, on the default syslog server if it is configured and on
909 the stderr if it is allowed.
910
911 :param class_txn txn: The class txn object containing the data.
912 :param integer loglevel: Is the log level asociated with the message. It is a
913 number between 0 and 7.
914 :param string msg: The log content.
915 :see: core.emerg, core.alert, core.crit, core.err, core.warning, core.notice,
916 core.info, core.debug (log level definitions)
917 :see: TXN.deflog
918 :see: TXN.Debug
919 :see: TXN.Info
920 :see: TXN.Warning
921 :see: TXN.Alert
922
923.. js:function:: TXN.deflog(TXN, msg)
924
925 Sends a log line with the default loglevel for the proxy ssociated with the
926 transaction.
927
928 :param class_txn txn: The class txn object containing the data.
929 :param string msg: The log content.
930 :see: TXN.log
931
932.. js:function:: TXN.Debug(txn, msg)
933
934 :param class_txn txn: The class txn object containing the data.
935 :param string msg: The log content.
936 :see: TXN.log
937
938 Does the same job than:
939
940.. code-block:: lua
941
942 function Debug(txn, msg)
943 TXN.log(txn, core.debug, msg)
944 end
945..
946
947.. js:function:: TXN.Info(txn, msg)
948
949 :param class_txn txn: The class txn object containing the data.
950 :param string msg: The log content.
951 :see: TXN.log
952
953.. code-block:: lua
954
955 function Debug(txn, msg)
956 TXN.log(txn, core.info, msg)
957 end
958..
959
960.. js:function:: TXN.Warning(txn, msg)
961
962 :param class_txn txn: The class txn object containing the data.
963 :param string msg: The log content.
964 :see: TXN.log
965
966.. code-block:: lua
967
968 function Debug(txn, msg)
969 TXN.log(txn, core.warning, msg)
970 end
971..
972
973.. js:function:: TXN.Alert(txn, msg)
974
975 :param class_txn txn: The class txn object containing the data.
976 :param string msg: The log content.
977 :see: TXN.log
978
979.. code-block:: lua
980
981 function Debug(txn, msg)
982 TXN.log(txn, core.alert, msg)
983 end
984..
985
Thierry FOURNIER486f5a02015-03-16 15:13:03 +0100986.. js:function:: TXN.get_priv(txn)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100987
Thierry FOURNIER486f5a02015-03-16 15:13:03 +0100988 Return Lua data stored in the current transaction (with the `TXN.set_priv()`)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100989 function. If no data are stored, it returns a nil value.
990
991 :param class_txn txn: The class txn object containing the data.
992 :returns: the opaque data previsously stored, or nil if nothing is
993 avalaible.
994
Thierry FOURNIER486f5a02015-03-16 15:13:03 +0100995.. js:function:: TXN.set_priv(txn, data)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100996
997 Store any data in the current HAProxy transaction. This action replace the
998 old stored data.
999
1000 :param class_txn txn: The class txn object containing the data.
1001 :param opaque data: The data which is stored in the transaction.
1002
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +02001003.. js:function:: TXN.set_var(TXN, var, value)
1004
1005 Converts an Lua type in a HAProxy type and store it in a variable <var>.
1006
1007 :param class_txn txn: The class txn object containing the data.
1008 :param string var: The variable name according with the HAProxy variable syntax.
1009 :param opaque value: The data which is stored in the variable.
1010
1011.. js:function:: TXN.get_var(TXN, var)
1012
1013 Returns data stored in the variable <var> converter in Lua type.
1014
1015 :param class_txn txn: The class txn object containing the data.
1016 :param string var: The variable name according with the HAProxy variable syntax.
1017
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001018.. js:function:: TXN.get_headers(txn)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001019
1020 This function returns an array of headers.
1021
1022 :param class_txn txn: The class txn object containing the data.
1023 :returns: an array of headers.
1024
Willy Tarreaubc183a62015-08-28 10:39:11 +02001025.. js:function:: TXN.done(txn)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001026
Willy Tarreaubc183a62015-08-28 10:39:11 +02001027 This function terminates processing of the transaction and the associated
1028 session. It can be used when a critical error is detected or to terminate
1029 processing after some data have been returned to the client (eg: a redirect).
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001030
1031 :param class_txn txn: The class txn object containing the data.
1032
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001033
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001034
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001035
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001036
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001037
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001038
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001039.. js:function:: TXN.set_loglevel(txn, loglevel)
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01001040
1041 Is used to change the log level of the current request. The "loglevel" must
1042 be an integer between 0 and 7.
1043
1044 :param class_txn txn: The class txn object containing the data.
1045 :param integer loglevel: The required log level. This variable can be one of
1046 :see: core.<loglevel>
1047
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001048.. js:function:: TXN.set_tos(txn, tos)
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01001049
1050 Is used to set the TOS or DSCP field value of packets sent to the client to
1051 the value passed in "tos" on platforms which support this.
1052
1053 :param class_txn txn: The class txn object containing the data.
1054 :param integer tos: The new TOS os DSCP.
1055
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001056.. js:function:: TXN.set_mark(txn, mark)
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01001057
1058 Is used to set the Netfilter MARK on all packets sent to the client to the
1059 value passed in "mark" on platforms which support it.
1060
1061 :param class_txn txn: The class txn object containing the data.
1062 :param integer mark: The mark value.
1063
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001064Socket class
1065============
1066
1067.. js:class:: Socket
1068
1069 This class must be compatible with the Lua Socket class. Only the 'client'
1070 functions are available. See the Lua Socket documentation:
1071
1072 `http://w3.impa.br/~diego/software/luasocket/tcp.html
1073 <http://w3.impa.br/~diego/software/luasocket/tcp.html>`_
1074
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001075.. js:function:: Socket.close(socket)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001076
1077 Closes a TCP object. The internal socket used by the object is closed and the
1078 local address to which the object was bound is made available to other
1079 applications. No further operations (except for further calls to the close
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001080 method) are allowed on a closed Socket.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001081
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001082 :param class_socket socket: Is the manipulated Socket.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001083
1084 Note: It is important to close all used sockets once they are not needed,
1085 since, in many systems, each socket uses a file descriptor, which are limited
1086 system resources. Garbage-collected objects are automatically closed before
1087 destruction, though.
1088
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02001089.. js:function:: Socket.connect(socket, address[, port])
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001090
1091 Attempts to connect a socket object to a remote host.
1092
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001093
1094 In case of error, the method returns nil followed by a string describing the
1095 error. In case of success, the method returns 1.
1096
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001097 :param class_socket socket: Is the manipulated Socket.
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02001098 :param string address: can be an IP address or a host name. See below for more
1099 information.
1100 :param integer port: must be an integer number in the range [1..64K].
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001101 :returns: 1 or nil.
1102
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02001103 an address field extension permits to use the connect() function to connect to
1104 other stream than TCP. The syntax containing a simpleipv4 or ipv6 address is
1105 the basically expected format. This format requires the port.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001106
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02001107 Other format accepted are a socket path like "/socket/path", it permits to
1108 connect to a socket. abstract namespaces are supported with the prefix
1109 "abns@", and finaly a filedescriotr can be passed with the prefix "fd@".
1110 The prefix "ipv4@", "ipv6@" and "unix@" are also supported. The port can be
1111 passed int the string. The syntax "127.0.0.1:1234" is valid. in this case, the
1112 parameter *port* is ignored.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001113
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001114.. js:function:: Socket.connect_ssl(socket, address, port)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001115
1116 Same behavior than the function socket:connect, but uses SSL.
1117
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001118 :param class_socket socket: Is the manipulated Socket.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001119 :returns: 1 or nil.
1120
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001121.. js:function:: Socket.getpeername(socket)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001122
1123 Returns information about the remote side of a connected client object.
1124
1125 Returns a string with the IP address of the peer, followed by the port number
1126 that peer is using for the connection. In case of error, the method returns
1127 nil.
1128
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001129 :param class_socket socket: Is the manipulated Socket.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001130 :returns: a string containing the server information.
1131
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001132.. js:function:: Socket.getsockname(socket)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001133
1134 Returns the local address information associated to the object.
1135
1136 The method returns a string with local IP address and a number with the port.
1137 In case of error, the method returns nil.
1138
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001139 :param class_socket socket: Is the manipulated Socket.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001140 :returns: a string containing the client information.
1141
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001142.. js:function:: Socket.receive(socket, [pattern [, prefix]])
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001143
1144 Reads data from a client object, according to the specified read pattern.
1145 Patterns follow the Lua file I/O format, and the difference in performance
1146 between all patterns is negligible.
1147
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001148 :param class_socket socket: Is the manipulated Socket.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001149 :param string|integer pattern: Describe what is required (see below).
1150 :param string prefix: A string which will be prefix the returned data.
1151 :returns: a string containing the required data or nil.
1152
1153 Pattern can be any of the following:
1154
1155 * **`*a`**: reads from the socket until the connection is closed. No
1156 end-of-line translation is performed;
1157
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001158 * **`*l`**: reads a line of text from the Socket. The line is terminated by a
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001159 LF character (ASCII 10), optionally preceded by a CR character
1160 (ASCII 13). The CR and LF characters are not included in the
1161 returned line. In fact, all CR characters are ignored by the
1162 pattern. This is the default pattern.
1163
1164 * **number**: causes the method to read a specified number of bytes from the
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001165 Socket. Prefix is an optional string to be concatenated to the
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001166 beginning of any received data before return.
1167
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02001168 * **empty**: If the pattern is left empty, the default option is `*l`.
1169
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001170 If successful, the method returns the received pattern. In case of error, the
1171 method returns nil followed by an error message which can be the string
1172 'closed' in case the connection was closed before the transmission was
1173 completed or the string 'timeout' in case there was a timeout during the
1174 operation. Also, after the error message, the function returns the partial
1175 result of the transmission.
1176
1177 Important note: This function was changed severely. It used to support
1178 multiple patterns (but I have never seen this feature used) and now it
1179 doesn't anymore. Partial results used to be returned in the same way as
1180 successful results. This last feature violated the idea that all functions
1181 should return nil on error. Thus it was changed too.
1182
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001183.. js:function:: Socket.send(socket, data [, start [, end ]])
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001184
1185 Sends data through client object.
1186
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001187 :param class_socket socket: Is the manipulated Socket.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001188 :param string data: The data that will be sent.
1189 :param integer start: The start position in the buffer of the data which will
1190 be sent.
1191 :param integer end: The end position in the buffer of the data which will
1192 be sent.
1193 :returns: see below.
1194
1195 Data is the string to be sent. The optional arguments i and j work exactly
1196 like the standard string.sub Lua function to allow the selection of a
1197 substring to be sent.
1198
1199 If successful, the method returns the index of the last byte within [start,
1200 end] that has been sent. Notice that, if start is 1 or absent, this is
1201 effectively the total number of bytes sent. In case of error, the method
1202 returns nil, followed by an error message, followed by the index of the last
1203 byte within [start, end] that has been sent. You might want to try again from
1204 the byte following that. The error message can be 'closed' in case the
1205 connection was closed before the transmission was completed or the string
1206 'timeout' in case there was a timeout during the operation.
1207
1208 Note: Output is not buffered. For small strings, it is always better to
1209 concatenate them in Lua (with the '..' operator) and send the result in one
1210 call instead of calling the method several times.
1211
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001212.. js:function:: Socket.setoption(socket, option [, value])
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001213
1214 Just implemented for compatibility, this cal does nothing.
1215
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001216.. js:function:: Socket.settimeout(socket, value [, mode])
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001217
1218 Changes the timeout values for the object. All I/O operations are blocking.
1219 That is, any call to the methods send, receive, and accept will block
1220 indefinitely, until the operation completes. The settimeout method defines a
1221 limit on the amount of time the I/O methods can block. When a timeout time
1222 has elapsed, the affected methods give up and fail with an error code.
1223
1224 The amount of time to wait is specified as the value parameter, in seconds.
1225
1226 The timeout modes are bot implemented, the only settable timeout is the
1227 inactivity time waiting for complete the internal buffer send or waiting for
1228 receive data.
1229
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001230 :param class_socket socket: Is the manipulated Socket.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001231 :param integer value: The timeout value.
1232
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001233Map class
1234=========
1235
1236.. js:class:: Map
1237
1238 This class permits to do some lookup in HAProxy maps. The declared maps can
1239 be modified during the runtime throught the HAProxy management socket.
1240
1241.. code-block:: lua
1242
1243 default = "usa"
1244
1245 -- Create and load map
1246 geo = Map.new("geo.map", Map.ip);
1247
1248 -- Create new fetch that returns the user country
1249 core.register_fetches("country", function(txn)
1250 local src;
1251 local loc;
1252
1253 src = txn.f:fhdr("x-forwarded-for");
1254 if (src == nil) then
1255 src = txn.f:src()
1256 if (src == nil) then
1257 return default;
1258 end
1259 end
1260
1261 -- Perform lookup
1262 loc = geo:lookup(src);
1263
1264 if (loc == nil) then
1265 return default;
1266 end
1267
1268 return loc;
1269 end);
1270
1271.. js:attribute:: Map.int
1272
1273 See the HAProxy configuration.txt file, chapter "Using ACLs and fetching
1274 samples" ans subchapter "ACL basics" to understand this pattern matching
1275 method.
1276
1277.. js:attribute:: Map.ip
1278
1279 See the HAProxy configuration.txt file, chapter "Using ACLs and fetching
1280 samples" ans subchapter "ACL basics" to understand this pattern matching
1281 method.
1282
1283.. js:attribute:: Map.str
1284
1285 See the HAProxy configuration.txt file, chapter "Using ACLs and fetching
1286 samples" ans subchapter "ACL basics" to understand this pattern matching
1287 method.
1288
1289.. js:attribute:: Map.beg
1290
1291 See the HAProxy configuration.txt file, chapter "Using ACLs and fetching
1292 samples" ans subchapter "ACL basics" to understand this pattern matching
1293 method.
1294
1295.. js:attribute:: Map.sub
1296
1297 See the HAProxy configuration.txt file, chapter "Using ACLs and fetching
1298 samples" ans subchapter "ACL basics" to understand this pattern matching
1299 method.
1300
1301.. js:attribute:: Map.dir
1302
1303 See the HAProxy configuration.txt file, chapter "Using ACLs and fetching
1304 samples" ans subchapter "ACL basics" to understand this pattern matching
1305 method.
1306
1307.. js:attribute:: Map.dom
1308
1309 See the HAProxy configuration.txt file, chapter "Using ACLs and fetching
1310 samples" ans subchapter "ACL basics" to understand this pattern matching
1311 method.
1312
1313.. js:attribute:: Map.end
1314
1315 See the HAProxy configuration.txt file, chapter "Using ACLs and fetching
1316 samples" ans subchapter "ACL basics" to understand this pattern matching
1317 method.
1318
1319.. js:attribute:: Map.reg
1320
1321 See the HAProxy configuration.txt file, chapter "Using ACLs and fetching
1322 samples" ans subchapter "ACL basics" to understand this pattern matching
1323 method.
1324
1325
1326.. js:function:: Map.new(file, method)
1327
1328 Creates and load a map.
1329
1330 :param string file: Is the file containing the map.
1331 :param integer method: Is the map pattern matching method. See the attributes
1332 of the Map class.
1333 :returns: a class Map object.
1334 :see: The Map attributes.
1335
1336.. js:function:: Map.lookup(map, str)
1337
1338 Perform a lookup in a map.
1339
1340 :param class_map map: Is the class Map object.
1341 :param string str: Is the string used as key.
1342 :returns: a string containing the result or nil if no match.
1343
1344.. js:function:: Map.slookup(map, str)
1345
1346 Perform a lookup in a map.
1347
1348 :param class_map map: Is the class Map object.
1349 :param string str: Is the string used as key.
1350 :returns: a string containing the result or empty string if no match.
1351
Thierry FOURNIERa3bc5132015-09-25 21:43:56 +02001352AppletHTTP class
1353===============
1354
1355.. js:class:: AppletHTTP
1356
1357 This class is used with applets that requires the 'http' mode. The http applet
1358 can be registered with the *core.register_service()* function. They are used
1359 for processing an http request like a server in back of HAProxy.
1360
1361 This is an hello world sample code:
1362
1363.. code-block:: lua
1364 core.register_service("hello-world", "http" }, function(txn)
1365 local response = "Hello World !"
1366 applet:set_status(200)
1367 applet:add_header("content-length", string.length(response))
1368 applet:add_header("content-type", "text/plain")
1369 applet:start_reponse()
1370 applet:send(response)
1371 end)
1372
1373
1374.. js:function:: AppletHTTP.set_status(code)
1375
1376 This function sets the HTTP status code for the response. The allowed code are
1377 from 100 to 599.
1378
1379 :param integer code: the status code returned to the client.
1380
1381.. js:function:: AppletHTTP.add_header(name, value)
1382
1383 This function add an header in the response. Duplicated headers are not
1384 collapsed. The special header *content-length* is used to determinate the
1385 response length. If it not exists, a *transfer-encoding: chunked* is set, and
1386 all the write from the funcion *AppletHTTP:send()* become a chunk.
1387
1388 :param string name: the header name
1389 :param string value: the header value
1390
1391.. js:function:: AppletHTTP.start_response()
1392
1393 This function indicates to the HTTP engine that it can process and send the
1394 response headers. After this called we cannot add headers to the response; We
1395 cannot use the *AppletHTTP:send()* function if the
1396 *AppletHTTP:start_response()* is not called.
1397
1398.. js:function:: AppletHTTP.getline()
1399
1400 This function returns a string containing one line from the http body. If the
1401 data returned doesn't contains a final '\\n' its assumed than its the last
1402 available data before the end of stream.
1403
1404 :returns: a string. The string can be empty if we reach the end of the stream.
1405
1406.. js:function:: AppletHTTP.receive([size])
1407
1408 Reads data from the HTTP body, according to the specified read *size*. If the
1409 *size* is missing, the function tries to read all the content of the stream
1410 until the end. If the *size* is bigger than the http body, it returns the
1411 amount of data avalaible.
1412
1413 :param integer size: the required read size.
1414 :returns: always return a string,the string can be empty is the connexion is
1415 closed.
1416
1417.. js:function:: AppletHTTP.send(msg)
1418
1419 Send the message *msg* on the http request body.
1420
1421 :param string msg: the message to send.
1422
1423AppletTCP class
1424===============
1425
1426.. js:class:: AppletTCP
1427
1428 This class is used with applets that requires the 'tcp' mode. The tcp applet
1429 can be registered with the *core.register_service()* function. They are used
1430 for processing a tcp stream like a server in back of HAProxy.
1431
1432.. js:function:: AppletTCP.getline()
1433
1434 This function returns a string containing one line from the stream. If the
1435 data returned doesn't contains a final '\\n' its assumed than its the last
1436 available data before the end of stream.
1437
1438 :returns: a string. The string can be empty if we reach the end of the stream.
1439
1440.. js:function:: AppletTCP.receive([size])
1441
1442 Reads data from the TCP stream, according to the specified read *size*. If the
1443 *size* is missing, the function tries to read all the content of the stream
1444 until the end.
1445
1446 :param integer size: the required read size.
1447 :returns: always return a string,the string can be empty is the connexion is
1448 closed.
1449
1450.. js:function:: AppletTCP.send(msg)
1451
1452 Send the message on the stream.
1453
1454 :param string msg: the message to send.
1455
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001456External Lua libraries
1457======================
1458
1459A lot of useful lua libraries can be found here:
1460
1461* `https://lua-toolbox.com/ <https://lua-toolbox.com/>`_
1462
1463Redis acces:
1464
1465* `https://github.com/nrk/redis-lua <https://github.com/nrk/redis-lua>`_
1466
1467This is an example about the usage of the Redis library with HAProxy. Note that
1468each call of any function of this library can throw an error if the socket
1469connection fails.
1470
1471.. code-block:: lua
1472
1473 -- load the redis library
1474 local redis = require("redis");
1475
1476 function do_something(txn)
1477
1478 -- create and connect new tcp socket
1479 local tcp = core.tcp();
1480 tcp:settimeout(1);
1481 tcp:connect("127.0.0.1", 6379);
1482
1483 -- use the redis library with this new socket
1484 local client = redis.connect({socket=tcp});
1485 client:ping();
1486
1487 end
1488
1489OpenSSL:
1490
1491* `http://mkottman.github.io/luacrypto/index.html
1492 <http://mkottman.github.io/luacrypto/index.html>`_
1493
1494* `https://github.com/brunoos/luasec/wiki
1495 <https://github.com/brunoos/luasec/wiki>`_
Thierry FOURNIER2e4893c2015-03-18 13:37:27 +01001496