blob: e513324cf223a66ac0232622e8b4c95673e17468 [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")
91 txn:close()
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
267.. js:function:: core.register_converters(name, func)
268
269 **context**: body
270
271 Register an Lua function executed as converter. All the registered converters
272 can be used in HAProxy with the prefix "lua.". An converter get a string as
273 input and return a string as output. The registered function can take up to 9
274 values as parameter. All the value are strings.
275
276 :param string name: is the name of the converter.
277 :param function func: is the Lua function called to work as converter.
278
279 The prototype of the Lua function used as argument is:
280
281.. code-block:: lua
282
283 function(str, [p1 [, p2 [, ... [, p5]]]])
284..
285
286 * **str** (*string*): this is the input value automatically converted in
287 string.
288 * **p1** .. **p5** (*string*): this is a list of string arguments declared in
289 the haroxy configuration file. The number of arguments doesn't exceed 5.
290 The order and the nature of these is conventionally choose by the
291 developper.
292
293.. js:function:: core.register_fetches(name, func)
294
295 **context**: body
296
297 Register an Lua function executed as sample fetch. All the registered sample
298 fetchs can be used in HAProxy with the prefix "lua.". A Lua sample fetch
299 return a string as output. The registered function can take up to 9 values as
300 parameter. All the value are strings.
301
302 :param string name: is the name of the converter.
303 :param function func: is the Lua function called to work as sample fetch.
304
305 The prototype of the Lua function used as argument is:
306
307.. code-block:: lua
308
309 string function(txn, [p1 [, p2 [, ... [, p5]]]])
310..
311
312 * **txn** (*class txn*): this is the txn object associated with the current
313 request.
314 * **p1** .. **p5** (*string*): this is a list of string arguments declared in
315 the haroxy configuration file. The number of arguments doesn't exceed 5.
316 The order and the nature of these is conventionally choose by the
317 developper.
318 * **Returns**: A string containing some data, ot nil if the value cannot be
319 returned now.
320
321 lua example code:
322
323.. code-block:: lua
324
325 core.register_fetches("hello", function(txn)
326 return "hello"
327 end)
328..
329
330 HAProxy example configuration:
331
332::
333
334 frontend example
335 http-request redirect location /%[lua.hello]
336
337.. js:function:: core.register_init(func)
338
339 **context**: body
340
341 Register a function executed after the configuration parsing. This is useful
342 to check any parameters.
343
344 :param fuction func: is the Lua function called to work as initializer.
345
346 The prototype of the Lua function used as argument is:
347
348.. code-block:: lua
349
350 function()
351..
352
353 It takes no input, and no output is expected.
354
355.. js:function:: core.register_task(func)
356
357 **context**: body, init, task, action, sample-fetch, converter
358
359 Register and start independent task. The task is started when the HAProxy
360 main scheduler starts. For example this type of tasks can be executed to
Thierry FOURNIER2e4893c2015-03-18 13:37:27 +0100361 perform complex health checks.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100362
363 :param fuction func: is the Lua function called to work as initializer.
364
365 The prototype of the Lua function used as argument is:
366
367.. code-block:: lua
368
369 function()
370..
371
372 It takes no input, and no output is expected.
373
374.. js:function:: core.set_nice(nice)
375
376 **context**: task, action, sample-fetch, converter
377
378 Change the nice of the current task or current session.
Thierry FOURNIER2e4893c2015-03-18 13:37:27 +0100379
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100380 :param integer nice: the nice value, it must be between -1024 and 1024.
381
382.. js:function:: core.set_map(filename, key, value)
383
384 **context**: init, task, action, sample-fetch, converter
385
386 set the value *value* associated to the key *key* in the map referenced by
387 *filename*.
388
389.. js:function:: core.sleep(int seconds)
390
391 **context**: body, init, task, action
392
393 The `core.sleep()` functions stop the Lua execution between specified seconds.
394
395 :param integer seconds: the required seconds.
396
397.. js:function:: core.tcp()
398
399 **context**: init, task, action
400
401 This function returns a new object of a *socket* class.
402
403 :returns: A socket class object.
404
Thierry FOURNIER0a99b892015-08-26 00:14:17 +0200405.. js:function:: core.done(data)
406
407 **context**: body, init, task, action, sample-fetch, converter
408
409 :param any data: Return some data for the caller. It is useful with
410 sample-fetches and sample-converters.
411
412 Immediately stops the current Lua execution and returns to the caller which
413 may be a sample fetch, a converter or an action and returns the specified
414 value (ignored for actions). It is used when the LUA process finishes its
415 work and wants to give back the control to HAProxy without executing the
416 remaining code. It can be seen as a multi-level "return".
417
Thierry FOURNIER486f5a02015-03-16 15:13:03 +0100418.. js:function:: core.yield()
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100419
420 **context**: task, action, sample-fetch, converter
421
422 Give back the hand at the HAProxy scheduler. It is used when the LUA
423 processing consumes a lot of processing time.
424
425Fetches class
426=============
427
428.. js:class:: Fetches
429
430 This class contains a lot of internal HAProxy sample fetches. See the
Thierry FOURNIER2e4893c2015-03-18 13:37:27 +0100431 HAProxy "configuration.txt" documentation for more information about her
432 usage. they are the chapters 7.3.2 to 7.3.6.
433
434 :see: TXN.f
435 :see: TXN.sf
436
437 Fetches are useful for:
438
439 * get system time,
440 * get environment variable,
441 * get random numbers,
442 * known backend status like the number of users in queue or the number of
443 connections established,
444 * client information like ip source or destination,
445 * deal with stick tables,
446 * Established SSL informations,
447 * HTTP information like headers or method.
448
449.. code-block:: lua
450
451 function action(txn)
452 -- Get source IP
453 local clientip = txn.f:src()
454 end
455..
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100456
457Converters class
458================
459
460.. js:class:: Converters
461
462 This class contains a lot of internal HAProxy sample converters. See the
Thierry FOURNIER2e4893c2015-03-18 13:37:27 +0100463 HAProxy documentation "configuration.txt" for more information about her
464 usage. Its the chapter 7.3.1.
465
466 :see: TXN.c
467 :see: TXN.sc
468
469 Converters provides statefull transformation. They are useful for:
470
471 * converting input to base64,
472 * applying hash on input string (djb2, crc32, sdbm, wt6),
473 * format date,
474 * json escape,
475 * extracting prefered language comparing two lists,
476 * turn to lower or upper chars,
477 * deal with stick tables.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100478
479Channel class
480=============
481
482.. js:class:: Channel
483
484 HAProxy uses two buffers for the processing of the requests. The first one is
485 used with the request data (from the client to the server) and the second is
486 used for the response data (from the server to the client).
487
488 Each buffer contains two types of data. The first type is the incoming data
489 waiting for a processing. The second part is the outgoing data already
490 processed. Usually, the incoming data is processed, after it is tagged as
491 outgoing data, and finally it is sent. The following functions provides tools
492 for manipulating these data in a buffer.
493
494 The following diagram shows where the channel class function are applied.
495
496 **Warning**: It is not possible to read from the response in request action,
497 and it is not possible to read for the request channel in response action.
498
Thierry FOURNIER2e4893c2015-03-18 13:37:27 +0100499.. image:: _static/channel.png
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100500
Thierry FOURNIER486f5a02015-03-16 15:13:03 +0100501.. js:function:: Channel.dup(channel)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100502
503 This function returns a string that contain the entire buffer. The data is
504 not remove from the buffer and can be reprocessed later.
505
506 If the buffer cant receive more data, a 'nil' value is returned.
507
Thierry FOURNIER486f5a02015-03-16 15:13:03 +0100508 :param class_channel channel: The manipulated Channel.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100509 :returns: a string containig all the avalaible data or nil.
510
Thierry FOURNIER486f5a02015-03-16 15:13:03 +0100511.. js:function:: Channel.get(channel)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100512
513 This function returns a string that contain the entire buffer. The data is
514 consumed from the buffer.
515
516 If the buffer cant receive more data, a 'nil' value is returned.
517
Thierry FOURNIER486f5a02015-03-16 15:13:03 +0100518 :param class_channel channel: The manipulated Channel.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100519 :returns: a string containig all the avalaible data or nil.
520
Thierry FOURNIER486f5a02015-03-16 15:13:03 +0100521.. js:function:: Channel.get_line(channel)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100522
523 This function returns a string that contain the first line of the buffer. The
524 data is consumed. If the data returned doesn't contains a final '\n' its
525 assumed than its the last available data in the buffer.
526
527 If the buffer cant receive more data, a 'nil' value is returned.
528
Thierry FOURNIER486f5a02015-03-16 15:13:03 +0100529 :param class_channel channel: The manipulated Channel.
Pieter Baauw386a1272015-08-16 15:26:24 +0200530 :returns: a string containing the available line or nil.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100531
Thierry FOURNIER486f5a02015-03-16 15:13:03 +0100532.. js:function:: Channel.set(channel, string)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100533
534 This function replace the content of the buffer by the string. The function
535 returns the copied length, otherwise, it returns -1.
536
537 The data set with this function are not send. They wait for the end of
538 HAProxy processing, so the buffer can be full.
539
Thierry FOURNIER486f5a02015-03-16 15:13:03 +0100540 :param class_channel channel: The manipulated Channel.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100541 :param string string: The data which will sent.
542 :returns: an integer containing the amount of butes copyed or -1.
543
Thierry FOURNIER486f5a02015-03-16 15:13:03 +0100544.. js:function:: Channel.append(channel, string)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100545
546 This function append the string argument to the content of the buffer. The
547 function returns the copied length, otherwise, it returns -1.
548
549 The data set with this function are not send. They wait for the end of
550 HAProxy processing, so the buffer can be full.
551
Thierry FOURNIER486f5a02015-03-16 15:13:03 +0100552 :param class_channel channel: The manipulated Channel.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100553 :param string string: The data which will sent.
554 :returns: an integer containing the amount of butes copyed or -1.
555
Thierry FOURNIER486f5a02015-03-16 15:13:03 +0100556.. js:function:: Channel.send(channel, string)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100557
558 This function required immediate send of the data. Unless if the connection
559 is close, the buffer is regularly flushed and all the string can be sent.
560
Thierry FOURNIER486f5a02015-03-16 15:13:03 +0100561 :param class_channel channel: The manipulated Channel.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100562 :param string string: The data which will sent.
563 :returns: an integer containing the amount of butes copyed or -1.
564
Thierry FOURNIER486f5a02015-03-16 15:13:03 +0100565.. js:function:: Channel.get_in_length(channel)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100566
567 This function returns the length of the input part of the buffer.
568
Thierry FOURNIER486f5a02015-03-16 15:13:03 +0100569 :param class_channel channel: The manipulated Channel.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100570 :returns: an integer containing the amount of avalaible bytes.
571
Thierry FOURNIER486f5a02015-03-16 15:13:03 +0100572.. js:function:: Channel.get_out_length(channel)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100573
574 This function returns the length of the output part of the buffer.
575
Thierry FOURNIER486f5a02015-03-16 15:13:03 +0100576 :param class_channel channel: The manipulated Channel.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100577 :returns: an integer containing the amount of avalaible bytes.
578
Thierry FOURNIER486f5a02015-03-16 15:13:03 +0100579.. js:function:: Channel.forward(channel, int)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100580
581 This function transfer bytes from the input part of the buffer to the output
582 part.
583
Thierry FOURNIER486f5a02015-03-16 15:13:03 +0100584 :param class_channel channel: The manipulated Channel.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100585 :param integer int: The amount of data which will be forwarded.
586
Thierry FOURNIER08504f42015-03-16 14:17:08 +0100587
588HTTP class
589==========
590
591.. js:class:: HTTP
592
593 This class contain all the HTTP manipulation functions.
594
Pieter Baauw386a1272015-08-16 15:26:24 +0200595.. js:function:: HTTP.req_get_headers(http)
Thierry FOURNIER08504f42015-03-16 14:17:08 +0100596
597 Returns an array containing all the request headers.
598
599 :param class_http http: The related http object.
600 :returns: array of headers.
Pieter Baauw386a1272015-08-16 15:26:24 +0200601 :see: HTTP.res_get_headers()
Thierry FOURNIER08504f42015-03-16 14:17:08 +0100602
Pieter Baauw386a1272015-08-16 15:26:24 +0200603.. js:function:: HTTP.res_get_headers(http)
Thierry FOURNIER08504f42015-03-16 14:17:08 +0100604
605 Returns an array containing all the response headers.
606
607 :param class_http http: The related http object.
608 :returns: array of headers.
Pieter Baauw386a1272015-08-16 15:26:24 +0200609 :see: HTTP.req_get_headers()
Thierry FOURNIER08504f42015-03-16 14:17:08 +0100610
Thierry FOURNIER486f5a02015-03-16 15:13:03 +0100611.. js:function:: HTTP.req_add_header(http, name, value)
Thierry FOURNIER08504f42015-03-16 14:17:08 +0100612
613 Appends an HTTP header field in the request whose name is
614 specified in "name" and whose value is defined in "value".
615
616 :param class_http http: The related http object.
617 :param string name: The header name.
618 :param string value: The header value.
Thierry FOURNIER486f5a02015-03-16 15:13:03 +0100619 :see: HTTP.res_add_header()
Thierry FOURNIER08504f42015-03-16 14:17:08 +0100620
Thierry FOURNIER486f5a02015-03-16 15:13:03 +0100621.. js:function:: HTTP.res_add_header(http, name, value)
Thierry FOURNIER08504f42015-03-16 14:17:08 +0100622
623 appends an HTTP header field in the response whose name is
624 specified in "name" and whose value is defined in "value".
625
626 :param class_http http: The related http object.
627 :param string name: The header name.
628 :param string value: The header value.
Thierry FOURNIER486f5a02015-03-16 15:13:03 +0100629 :see: HTTP.req_add_header()
Thierry FOURNIER08504f42015-03-16 14:17:08 +0100630
Thierry FOURNIER486f5a02015-03-16 15:13:03 +0100631.. js:function:: HTTP.req_del_header(http, name)
Thierry FOURNIER08504f42015-03-16 14:17:08 +0100632
633 Removes all HTTP header fields in the request whose name is
634 specified in "name".
635
636 :param class_http http: The related http object.
637 :param string name: The header name.
Thierry FOURNIER486f5a02015-03-16 15:13:03 +0100638 :see: HTTP.res_del_header()
Thierry FOURNIER08504f42015-03-16 14:17:08 +0100639
Thierry FOURNIER486f5a02015-03-16 15:13:03 +0100640.. js:function:: HTTP.res_del_header(http, name)
Thierry FOURNIER08504f42015-03-16 14:17:08 +0100641
642 Removes all HTTP header fields in the response whose name is
643 specified in "name".
644
645 :param class_http http: The related http object.
646 :param string name: The header name.
Thierry FOURNIER486f5a02015-03-16 15:13:03 +0100647 :see: HTTP.req_del_header()
Thierry FOURNIER08504f42015-03-16 14:17:08 +0100648
Thierry FOURNIER486f5a02015-03-16 15:13:03 +0100649.. js:function:: HTTP.req_set_header(http, name, value)
Thierry FOURNIER08504f42015-03-16 14:17:08 +0100650
651 This variable replace all occurence of all header "name", by only
652 one containing the "value".
653
654 :param class_http http: The related http object.
655 :param string name: The header name.
656 :param string value: The header value.
Thierry FOURNIER486f5a02015-03-16 15:13:03 +0100657 :see: HTTP.res_set_header()
Thierry FOURNIER08504f42015-03-16 14:17:08 +0100658
659 This function does the same work as the folowwing code:
660
661.. code-block:: lua
662
663 function fcn(txn)
Thierry FOURNIER486f5a02015-03-16 15:13:03 +0100664 TXN.http:req_del_header("header")
665 TXN.http:req_add_header("header", "value")
Thierry FOURNIER08504f42015-03-16 14:17:08 +0100666 end
667..
668
Thierry FOURNIER486f5a02015-03-16 15:13:03 +0100669.. js:function:: HTTP.res_set_header(http, name, value)
Thierry FOURNIER08504f42015-03-16 14:17:08 +0100670
671 This variable replace all occurence of all header "name", by only
672 one containing the "value".
673
674 :param class_http http: The related http object.
675 :param string name: The header name.
676 :param string value: The header value.
Pieter Baauw386a1272015-08-16 15:26:24 +0200677 :see: HTTP.req_rep_header()
Thierry FOURNIER08504f42015-03-16 14:17:08 +0100678
Pieter Baauw386a1272015-08-16 15:26:24 +0200679.. js:function:: HTTP.req_rep_header(http, name, regex, replace)
Thierry FOURNIER08504f42015-03-16 14:17:08 +0100680
681 Matches the regular expression in all occurrences of header field "name"
682 according to "regex", and replaces them with the "replace" argument. The
683 replacement value can contain back references like \1, \2, ... This
684 function works with the request.
685
686 :param class_http http: The related http object.
687 :param string name: The header name.
688 :param string regex: The match regular expression.
689 :param string replace: The replacement value.
Pieter Baauw386a1272015-08-16 15:26:24 +0200690 :see: HTTP.res_rep_header()
Thierry FOURNIER08504f42015-03-16 14:17:08 +0100691
Pieter Baauw386a1272015-08-16 15:26:24 +0200692.. js:function:: HTTP.res_rep_header(http, name, regex, string)
Thierry FOURNIER08504f42015-03-16 14:17:08 +0100693
694 Matches the regular expression in all occurrences of header field "name"
695 according to "regex", and replaces them with the "replace" argument. The
696 replacement value can contain back references like \1, \2, ... This
697 function works with the request.
698
699 :param class_http http: The related http object.
700 :param string name: The header name.
701 :param string regex: The match regular expression.
702 :param string replace: The replacement value.
Thierry FOURNIER486f5a02015-03-16 15:13:03 +0100703 :see: HTTP.req_replace_header()
Thierry FOURNIER08504f42015-03-16 14:17:08 +0100704
Thierry FOURNIER486f5a02015-03-16 15:13:03 +0100705.. js:function:: HTTP.req_replace_value(http, name, regex, replace)
Thierry FOURNIER08504f42015-03-16 14:17:08 +0100706
Thierry FOURNIER486f5a02015-03-16 15:13:03 +0100707 Works like "HTTP.req_replace_header()" except that it matches the regex
Thierry FOURNIER08504f42015-03-16 14:17:08 +0100708 against every comma-delimited value of the header field "name" instead of the
709 entire header.
710
711 :param class_http http: The related http object.
712 :param string name: The header name.
713 :param string regex: The match regular expression.
714 :param string replace: The replacement value.
Thierry FOURNIER486f5a02015-03-16 15:13:03 +0100715 :see: HTTP.req_replace_header()
716 :see: HTTP.res_replace_value()
Thierry FOURNIER08504f42015-03-16 14:17:08 +0100717
Thierry FOURNIER486f5a02015-03-16 15:13:03 +0100718.. js:function:: HTTP.res_replace_value(http, name, regex, replace)
Thierry FOURNIER08504f42015-03-16 14:17:08 +0100719
Thierry FOURNIER486f5a02015-03-16 15:13:03 +0100720 Works like "HTTP.res_replace_header()" except that it matches the regex
Thierry FOURNIER08504f42015-03-16 14:17:08 +0100721 against every comma-delimited value of the header field "name" instead of the
722 entire header.
723
724 :param class_http http: The related http object.
725 :param string name: The header name.
726 :param string regex: The match regular expression.
727 :param string replace: The replacement value.
Thierry FOURNIER486f5a02015-03-16 15:13:03 +0100728 :see: HTTP.res_replace_header()
729 :see: HTTP.req_replace_value()
Thierry FOURNIER08504f42015-03-16 14:17:08 +0100730
Thierry FOURNIER486f5a02015-03-16 15:13:03 +0100731.. js:function:: HTTP.req_set_method(http, method)
Thierry FOURNIER08504f42015-03-16 14:17:08 +0100732
733 Rewrites the request method with the parameter "method".
734
735 :param class_http http: The related http object.
736 :param string method: The new method.
737
Thierry FOURNIER486f5a02015-03-16 15:13:03 +0100738.. js:function:: HTTP.req_set_path(http, path)
Thierry FOURNIER08504f42015-03-16 14:17:08 +0100739
740 Rewrites the request path with the "path" parameter.
741
742 :param class_http http: The related http object.
743 :param string path: The new path.
744
Thierry FOURNIER486f5a02015-03-16 15:13:03 +0100745.. js:function:: HTTP.req_set_query(http, query)
Thierry FOURNIER08504f42015-03-16 14:17:08 +0100746
747 Rewrites the request's query string which appears after the first question
748 mark ("?") with the parameter "query".
749
750 :param class_http http: The related http object.
751 :param string query: The new query.
752
Thierry FOURNIER0d79cf62015-08-26 14:20:58 +0200753.. js:function:: HTTP.req_set_uri(http, uri)
Thierry FOURNIER08504f42015-03-16 14:17:08 +0100754
755 Rewrites the request URI with the parameter "uri".
756
757 :param class_http http: The related http object.
758 :param string uri: The new uri.
759
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100760TXN class
761=========
762
763.. js:class:: TXN
764
765 The txn class contain all the functions relative to the http or tcp
766 transaction (Note than a tcp stream is the same than a tcp transaction, but
767 an HTTP transaction is not the same than a tcp stream).
768
769 The usage of this class permits to retrieve data from the requests, alter it
770 and forward it.
771
772 All the functions provided by this class are available in the context
773 **sample-fetches** and **actions**.
774
Thierry FOURNIER486f5a02015-03-16 15:13:03 +0100775.. js:attribute:: TXN.c
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100776
777 This attribute contains a Converters class object.
778
Thierry FOURNIER486f5a02015-03-16 15:13:03 +0100779.. js:attribute:: TXN.sc
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100780
781 This attribute contains a Converters class object. The functions of
782 this object returns always a string.
783
Thierry FOURNIER486f5a02015-03-16 15:13:03 +0100784.. js:attribute:: TXN.f
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100785
786 This attribute contains a Fetches class object.
787
Thierry FOURNIER486f5a02015-03-16 15:13:03 +0100788.. js:attribute:: TXN.sf
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100789
790 This attribute contains a Fetches class object. The functions of
791 this object returns always a string.
792
Thierry FOURNIER486f5a02015-03-16 15:13:03 +0100793.. js:attribute:: TXN.req
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100794
795 This attribute contains a channel class object for the request buffer.
796
Thierry FOURNIER486f5a02015-03-16 15:13:03 +0100797.. js:attribute:: TXN.res
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100798
799 This attribute contains a channel class object for the response buffer.
800
Thierry FOURNIER486f5a02015-03-16 15:13:03 +0100801.. js:attribute:: TXN.http
Thierry FOURNIER08504f42015-03-16 14:17:08 +0100802
803 This attribute contains an HTTP class object. It is avalaible only if the
804 proxy has the "mode http" enabled.
805
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100806.. js:function:: TXN.log(TXN, loglevel, msg)
807
808 This function sends a log. The log is sent, according with the HAProxy
809 configuration file, on the default syslog server if it is configured and on
810 the stderr if it is allowed.
811
812 :param class_txn txn: The class txn object containing the data.
813 :param integer loglevel: Is the log level asociated with the message. It is a
814 number between 0 and 7.
815 :param string msg: The log content.
816 :see: core.emerg, core.alert, core.crit, core.err, core.warning, core.notice,
817 core.info, core.debug (log level definitions)
818 :see: TXN.deflog
819 :see: TXN.Debug
820 :see: TXN.Info
821 :see: TXN.Warning
822 :see: TXN.Alert
823
824.. js:function:: TXN.deflog(TXN, msg)
825
826 Sends a log line with the default loglevel for the proxy ssociated with the
827 transaction.
828
829 :param class_txn txn: The class txn object containing the data.
830 :param string msg: The log content.
831 :see: TXN.log
832
833.. js:function:: TXN.Debug(txn, msg)
834
835 :param class_txn txn: The class txn object containing the data.
836 :param string msg: The log content.
837 :see: TXN.log
838
839 Does the same job than:
840
841.. code-block:: lua
842
843 function Debug(txn, msg)
844 TXN.log(txn, core.debug, msg)
845 end
846..
847
848.. js:function:: TXN.Info(txn, msg)
849
850 :param class_txn txn: The class txn object containing the data.
851 :param string msg: The log content.
852 :see: TXN.log
853
854.. code-block:: lua
855
856 function Debug(txn, msg)
857 TXN.log(txn, core.info, msg)
858 end
859..
860
861.. js:function:: TXN.Warning(txn, msg)
862
863 :param class_txn txn: The class txn object containing the data.
864 :param string msg: The log content.
865 :see: TXN.log
866
867.. code-block:: lua
868
869 function Debug(txn, msg)
870 TXN.log(txn, core.warning, msg)
871 end
872..
873
874.. js:function:: TXN.Alert(txn, msg)
875
876 :param class_txn txn: The class txn object containing the data.
877 :param string msg: The log content.
878 :see: TXN.log
879
880.. code-block:: lua
881
882 function Debug(txn, msg)
883 TXN.log(txn, core.alert, msg)
884 end
885..
886
Thierry FOURNIER486f5a02015-03-16 15:13:03 +0100887.. js:function:: TXN.get_priv(txn)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100888
Thierry FOURNIER486f5a02015-03-16 15:13:03 +0100889 Return Lua data stored in the current transaction (with the `TXN.set_priv()`)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100890 function. If no data are stored, it returns a nil value.
891
892 :param class_txn txn: The class txn object containing the data.
893 :returns: the opaque data previsously stored, or nil if nothing is
894 avalaible.
895
Thierry FOURNIER486f5a02015-03-16 15:13:03 +0100896.. js:function:: TXN.set_priv(txn, data)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100897
898 Store any data in the current HAProxy transaction. This action replace the
899 old stored data.
900
901 :param class_txn txn: The class txn object containing the data.
902 :param opaque data: The data which is stored in the transaction.
903
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +0200904.. js:function:: TXN.set_var(TXN, var, value)
905
906 Converts an Lua type in a HAProxy type and store it in a variable <var>.
907
908 :param class_txn txn: The class txn object containing the data.
909 :param string var: The variable name according with the HAProxy variable syntax.
910 :param opaque value: The data which is stored in the variable.
911
912.. js:function:: TXN.get_var(TXN, var)
913
914 Returns data stored in the variable <var> converter in Lua type.
915
916 :param class_txn txn: The class txn object containing the data.
917 :param string var: The variable name according with the HAProxy variable syntax.
918
Thierry FOURNIER486f5a02015-03-16 15:13:03 +0100919.. js:function:: TXN.get_headers(txn)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100920
921 This function returns an array of headers.
922
923 :param class_txn txn: The class txn object containing the data.
924 :returns: an array of headers.
925
Thierry FOURNIER486f5a02015-03-16 15:13:03 +0100926.. js:function:: TXN.close(txn)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100927
928 This function close the transaction and the associated session. It can be
929 used when a critical error is detected.
930
931 :param class_txn txn: The class txn object containing the data.
932
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100933
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100934
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100935
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100936
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100937
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100938
Thierry FOURNIER486f5a02015-03-16 15:13:03 +0100939.. js:function:: TXN.set_loglevel(txn, loglevel)
Thierry FOURNIER2cce3532015-03-16 12:04:16 +0100940
941 Is used to change the log level of the current request. The "loglevel" must
942 be an integer between 0 and 7.
943
944 :param class_txn txn: The class txn object containing the data.
945 :param integer loglevel: The required log level. This variable can be one of
946 :see: core.<loglevel>
947
Thierry FOURNIER486f5a02015-03-16 15:13:03 +0100948.. js:function:: TXN.set_tos(txn, tos)
Thierry FOURNIER2cce3532015-03-16 12:04:16 +0100949
950 Is used to set the TOS or DSCP field value of packets sent to the client to
951 the value passed in "tos" on platforms which support this.
952
953 :param class_txn txn: The class txn object containing the data.
954 :param integer tos: The new TOS os DSCP.
955
Thierry FOURNIER486f5a02015-03-16 15:13:03 +0100956.. js:function:: TXN.set_mark(txn, mark)
Thierry FOURNIER2cce3532015-03-16 12:04:16 +0100957
958 Is used to set the Netfilter MARK on all packets sent to the client to the
959 value passed in "mark" on platforms which support it.
960
961 :param class_txn txn: The class txn object containing the data.
962 :param integer mark: The mark value.
963
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100964Socket class
965============
966
967.. js:class:: Socket
968
969 This class must be compatible with the Lua Socket class. Only the 'client'
970 functions are available. See the Lua Socket documentation:
971
972 `http://w3.impa.br/~diego/software/luasocket/tcp.html
973 <http://w3.impa.br/~diego/software/luasocket/tcp.html>`_
974
Thierry FOURNIER486f5a02015-03-16 15:13:03 +0100975.. js:function:: Socket.close(socket)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100976
977 Closes a TCP object. The internal socket used by the object is closed and the
978 local address to which the object was bound is made available to other
979 applications. No further operations (except for further calls to the close
Thierry FOURNIER486f5a02015-03-16 15:13:03 +0100980 method) are allowed on a closed Socket.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100981
Thierry FOURNIER486f5a02015-03-16 15:13:03 +0100982 :param class_socket socket: Is the manipulated Socket.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100983
984 Note: It is important to close all used sockets once they are not needed,
985 since, in many systems, each socket uses a file descriptor, which are limited
986 system resources. Garbage-collected objects are automatically closed before
987 destruction, though.
988
Thierry FOURNIER486f5a02015-03-16 15:13:03 +0100989.. js:function:: Socket.connect(socket, address, port)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100990
991 Attempts to connect a socket object to a remote host.
992
993 Address can be an IP address or a host name. Port must be an integer number
994 in the range [1..64K).
995
996 In case of error, the method returns nil followed by a string describing the
997 error. In case of success, the method returns 1.
998
Thierry FOURNIER486f5a02015-03-16 15:13:03 +0100999 :param class_socket socket: Is the manipulated Socket.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001000 :returns: 1 or nil.
1001
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001002 Note: The function Socket.connect is available and is a shortcut for the
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001003 creation of client sockets.
1004
1005 Note: Starting with LuaSocket 2.0, the settimeout method affects the behavior
1006 of connect, causing it to return with an error in case of a timeout. If that
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001007 happens, you can still call Socket.select with the socket in the sendt table.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001008 The socket will be writable when the connection is established.
1009
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001010.. js:function:: Socket.connect_ssl(socket, address, port)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001011
1012 Same behavior than the function socket:connect, but uses SSL.
1013
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001014 :param class_socket socket: Is the manipulated Socket.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001015 :returns: 1 or nil.
1016
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001017.. js:function:: Socket.getpeername(socket)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001018
1019 Returns information about the remote side of a connected client object.
1020
1021 Returns a string with the IP address of the peer, followed by the port number
1022 that peer is using for the connection. In case of error, the method returns
1023 nil.
1024
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001025 :param class_socket socket: Is the manipulated Socket.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001026 :returns: a string containing the server information.
1027
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001028.. js:function:: Socket.getsockname(socket)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001029
1030 Returns the local address information associated to the object.
1031
1032 The method returns a string with local IP address and a number with the port.
1033 In case of error, the method returns nil.
1034
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001035 :param class_socket socket: Is the manipulated Socket.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001036 :returns: a string containing the client information.
1037
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001038.. js:function:: Socket.receive(socket, [pattern [, prefix]])
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001039
1040 Reads data from a client object, according to the specified read pattern.
1041 Patterns follow the Lua file I/O format, and the difference in performance
1042 between all patterns is negligible.
1043
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001044 :param class_socket socket: Is the manipulated Socket.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001045 :param string|integer pattern: Describe what is required (see below).
1046 :param string prefix: A string which will be prefix the returned data.
1047 :returns: a string containing the required data or nil.
1048
1049 Pattern can be any of the following:
1050
1051 * **`*a`**: reads from the socket until the connection is closed. No
1052 end-of-line translation is performed;
1053
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001054 * **`*l`**: reads a line of text from the Socket. The line is terminated by a
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001055 LF character (ASCII 10), optionally preceded by a CR character
1056 (ASCII 13). The CR and LF characters are not included in the
1057 returned line. In fact, all CR characters are ignored by the
1058 pattern. This is the default pattern.
1059
1060 * **number**: causes the method to read a specified number of bytes from the
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001061 Socket. Prefix is an optional string to be concatenated to the
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001062 beginning of any received data before return.
1063
1064 If successful, the method returns the received pattern. In case of error, the
1065 method returns nil followed by an error message which can be the string
1066 'closed' in case the connection was closed before the transmission was
1067 completed or the string 'timeout' in case there was a timeout during the
1068 operation. Also, after the error message, the function returns the partial
1069 result of the transmission.
1070
1071 Important note: This function was changed severely. It used to support
1072 multiple patterns (but I have never seen this feature used) and now it
1073 doesn't anymore. Partial results used to be returned in the same way as
1074 successful results. This last feature violated the idea that all functions
1075 should return nil on error. Thus it was changed too.
1076
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001077.. js:function:: Socket.send(socket, data [, start [, end ]])
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001078
1079 Sends data through client object.
1080
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001081 :param class_socket socket: Is the manipulated Socket.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001082 :param string data: The data that will be sent.
1083 :param integer start: The start position in the buffer of the data which will
1084 be sent.
1085 :param integer end: The end position in the buffer of the data which will
1086 be sent.
1087 :returns: see below.
1088
1089 Data is the string to be sent. The optional arguments i and j work exactly
1090 like the standard string.sub Lua function to allow the selection of a
1091 substring to be sent.
1092
1093 If successful, the method returns the index of the last byte within [start,
1094 end] that has been sent. Notice that, if start is 1 or absent, this is
1095 effectively the total number of bytes sent. In case of error, the method
1096 returns nil, followed by an error message, followed by the index of the last
1097 byte within [start, end] that has been sent. You might want to try again from
1098 the byte following that. The error message can be 'closed' in case the
1099 connection was closed before the transmission was completed or the string
1100 'timeout' in case there was a timeout during the operation.
1101
1102 Note: Output is not buffered. For small strings, it is always better to
1103 concatenate them in Lua (with the '..' operator) and send the result in one
1104 call instead of calling the method several times.
1105
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001106.. js:function:: Socket.setoption(socket, option [, value])
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001107
1108 Just implemented for compatibility, this cal does nothing.
1109
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001110.. js:function:: Socket.settimeout(socket, value [, mode])
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001111
1112 Changes the timeout values for the object. All I/O operations are blocking.
1113 That is, any call to the methods send, receive, and accept will block
1114 indefinitely, until the operation completes. The settimeout method defines a
1115 limit on the amount of time the I/O methods can block. When a timeout time
1116 has elapsed, the affected methods give up and fail with an error code.
1117
1118 The amount of time to wait is specified as the value parameter, in seconds.
1119
1120 The timeout modes are bot implemented, the only settable timeout is the
1121 inactivity time waiting for complete the internal buffer send or waiting for
1122 receive data.
1123
Thierry FOURNIER486f5a02015-03-16 15:13:03 +01001124 :param class_socket socket: Is the manipulated Socket.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001125 :param integer value: The timeout value.
1126
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001127Map class
1128=========
1129
1130.. js:class:: Map
1131
1132 This class permits to do some lookup in HAProxy maps. The declared maps can
1133 be modified during the runtime throught the HAProxy management socket.
1134
1135.. code-block:: lua
1136
1137 default = "usa"
1138
1139 -- Create and load map
1140 geo = Map.new("geo.map", Map.ip);
1141
1142 -- Create new fetch that returns the user country
1143 core.register_fetches("country", function(txn)
1144 local src;
1145 local loc;
1146
1147 src = txn.f:fhdr("x-forwarded-for");
1148 if (src == nil) then
1149 src = txn.f:src()
1150 if (src == nil) then
1151 return default;
1152 end
1153 end
1154
1155 -- Perform lookup
1156 loc = geo:lookup(src);
1157
1158 if (loc == nil) then
1159 return default;
1160 end
1161
1162 return loc;
1163 end);
1164
1165.. js:attribute:: Map.int
1166
1167 See the HAProxy configuration.txt file, chapter "Using ACLs and fetching
1168 samples" ans subchapter "ACL basics" to understand this pattern matching
1169 method.
1170
1171.. js:attribute:: Map.ip
1172
1173 See the HAProxy configuration.txt file, chapter "Using ACLs and fetching
1174 samples" ans subchapter "ACL basics" to understand this pattern matching
1175 method.
1176
1177.. js:attribute:: Map.str
1178
1179 See the HAProxy configuration.txt file, chapter "Using ACLs and fetching
1180 samples" ans subchapter "ACL basics" to understand this pattern matching
1181 method.
1182
1183.. js:attribute:: Map.beg
1184
1185 See the HAProxy configuration.txt file, chapter "Using ACLs and fetching
1186 samples" ans subchapter "ACL basics" to understand this pattern matching
1187 method.
1188
1189.. js:attribute:: Map.sub
1190
1191 See the HAProxy configuration.txt file, chapter "Using ACLs and fetching
1192 samples" ans subchapter "ACL basics" to understand this pattern matching
1193 method.
1194
1195.. js:attribute:: Map.dir
1196
1197 See the HAProxy configuration.txt file, chapter "Using ACLs and fetching
1198 samples" ans subchapter "ACL basics" to understand this pattern matching
1199 method.
1200
1201.. js:attribute:: Map.dom
1202
1203 See the HAProxy configuration.txt file, chapter "Using ACLs and fetching
1204 samples" ans subchapter "ACL basics" to understand this pattern matching
1205 method.
1206
1207.. js:attribute:: Map.end
1208
1209 See the HAProxy configuration.txt file, chapter "Using ACLs and fetching
1210 samples" ans subchapter "ACL basics" to understand this pattern matching
1211 method.
1212
1213.. js:attribute:: Map.reg
1214
1215 See the HAProxy configuration.txt file, chapter "Using ACLs and fetching
1216 samples" ans subchapter "ACL basics" to understand this pattern matching
1217 method.
1218
1219
1220.. js:function:: Map.new(file, method)
1221
1222 Creates and load a map.
1223
1224 :param string file: Is the file containing the map.
1225 :param integer method: Is the map pattern matching method. See the attributes
1226 of the Map class.
1227 :returns: a class Map object.
1228 :see: The Map attributes.
1229
1230.. js:function:: Map.lookup(map, str)
1231
1232 Perform a lookup in a map.
1233
1234 :param class_map map: Is the class Map object.
1235 :param string str: Is the string used as key.
1236 :returns: a string containing the result or nil if no match.
1237
1238.. js:function:: Map.slookup(map, str)
1239
1240 Perform a lookup in a map.
1241
1242 :param class_map map: Is the class Map object.
1243 :param string str: Is the string used as key.
1244 :returns: a string containing the result or empty string if no match.
1245
Thierry FOURNIER17bd1522015-03-11 20:31:00 +01001246External Lua libraries
1247======================
1248
1249A lot of useful lua libraries can be found here:
1250
1251* `https://lua-toolbox.com/ <https://lua-toolbox.com/>`_
1252
1253Redis acces:
1254
1255* `https://github.com/nrk/redis-lua <https://github.com/nrk/redis-lua>`_
1256
1257This is an example about the usage of the Redis library with HAProxy. Note that
1258each call of any function of this library can throw an error if the socket
1259connection fails.
1260
1261.. code-block:: lua
1262
1263 -- load the redis library
1264 local redis = require("redis");
1265
1266 function do_something(txn)
1267
1268 -- create and connect new tcp socket
1269 local tcp = core.tcp();
1270 tcp:settimeout(1);
1271 tcp:connect("127.0.0.1", 6379);
1272
1273 -- use the redis library with this new socket
1274 local client = redis.connect({socket=tcp});
1275 client:ping();
1276
1277 end
1278
1279OpenSSL:
1280
1281* `http://mkottman.github.io/luacrypto/index.html
1282 <http://mkottman.github.io/luacrypto/index.html>`_
1283
1284* `https://github.com/brunoos/luasec/wiki
1285 <https://github.com/brunoos/luasec/wiki>`_
Thierry FOURNIER2e4893c2015-03-18 13:37:27 +01001286