blob: 7e5277841d395aa58d98ec2f4c66de3cd55835ce [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
122.. js:function:: core.add_acl(filename, key)
123
124 **context**: init, task, action, sample-fetch, converter
125
126 Add the ACL *key* in the ACLs list referenced by the file *filename*.
127
128 :param string filename: the filename that reference the ACL entries.
129 :param string key: the key which will be added.
130
131.. js:function:: core.del_acl(filename, key)
132
133 **context**: init, task, action, sample-fetch, converter
134
135 Delete the ACL entry referenced by the key *key* in the list of ACLs
136 referenced by *filename*.
137
138 :param string filename: the filename that reference the ACL entries.
139 :param string key: the key which will be deleted.
140
141.. js:function:: core.del_map(filename, key)
142
143 **context**: init, task, action, sample-fetch, converter
144
145 Delete the map entry indexed with the specified key in the list of maps
146 referenced by his filename.
147
148 :param string filename: the filename that reference the map entries.
149 :param string key: the key which will be deleted.
150
151.. js:function:: core.msleep(milliseconds)
152
153 **context**: body, init, task, action
154
155 The `core.msleep()` stops the Lua execution between specified milliseconds.
156
157 :param integer milliseconds: the required milliseconds.
158
159.. js:function:: core.register_converters(name, func)
160
161 **context**: body
162
163 Register an Lua function executed as converter. All the registered converters
164 can be used in HAProxy with the prefix "lua.". An converter get a string as
165 input and return a string as output. The registered function can take up to 9
166 values as parameter. All the value are strings.
167
168 :param string name: is the name of the converter.
169 :param function func: is the Lua function called to work as converter.
170
171 The prototype of the Lua function used as argument is:
172
173.. code-block:: lua
174
175 function(str, [p1 [, p2 [, ... [, p5]]]])
176..
177
178 * **str** (*string*): this is the input value automatically converted in
179 string.
180 * **p1** .. **p5** (*string*): this is a list of string arguments declared in
181 the haroxy configuration file. The number of arguments doesn't exceed 5.
182 The order and the nature of these is conventionally choose by the
183 developper.
184
185.. js:function:: core.register_fetches(name, func)
186
187 **context**: body
188
189 Register an Lua function executed as sample fetch. All the registered sample
190 fetchs can be used in HAProxy with the prefix "lua.". A Lua sample fetch
191 return a string as output. The registered function can take up to 9 values as
192 parameter. All the value are strings.
193
194 :param string name: is the name of the converter.
195 :param function func: is the Lua function called to work as sample fetch.
196
197 The prototype of the Lua function used as argument is:
198
199.. code-block:: lua
200
201 string function(txn, [p1 [, p2 [, ... [, p5]]]])
202..
203
204 * **txn** (*class txn*): this is the txn object associated with the current
205 request.
206 * **p1** .. **p5** (*string*): this is a list of string arguments declared in
207 the haroxy configuration file. The number of arguments doesn't exceed 5.
208 The order and the nature of these is conventionally choose by the
209 developper.
210 * **Returns**: A string containing some data, ot nil if the value cannot be
211 returned now.
212
213 lua example code:
214
215.. code-block:: lua
216
217 core.register_fetches("hello", function(txn)
218 return "hello"
219 end)
220..
221
222 HAProxy example configuration:
223
224::
225
226 frontend example
227 http-request redirect location /%[lua.hello]
228
229.. js:function:: core.register_init(func)
230
231 **context**: body
232
233 Register a function executed after the configuration parsing. This is useful
234 to check any parameters.
235
236 :param fuction func: is the Lua function called to work as initializer.
237
238 The prototype of the Lua function used as argument is:
239
240.. code-block:: lua
241
242 function()
243..
244
245 It takes no input, and no output is expected.
246
247.. js:function:: core.register_task(func)
248
249 **context**: body, init, task, action, sample-fetch, converter
250
251 Register and start independent task. The task is started when the HAProxy
252 main scheduler starts. For example this type of tasks can be executed to
253 perform complex health checks.
254
255 :param fuction func: is the Lua function called to work as initializer.
256
257 The prototype of the Lua function used as argument is:
258
259.. code-block:: lua
260
261 function()
262..
263
264 It takes no input, and no output is expected.
265
266.. js:function:: core.set_nice(nice)
267
268 **context**: task, action, sample-fetch, converter
269
270 Change the nice of the current task or current session.
271
272 :param integer nice: the nice value, it must be between -1024 and 1024.
273
274.. js:function:: core.set_map(filename, key, value)
275
276 **context**: init, task, action, sample-fetch, converter
277
278 set the value *value* associated to the key *key* in the map referenced by
279 *filename*.
280
281.. js:function:: core.sleep(int seconds)
282
283 **context**: body, init, task, action
284
285 The `core.sleep()` functions stop the Lua execution between specified seconds.
286
287 :param integer seconds: the required seconds.
288
289.. js:function:: core.tcp()
290
291 **context**: init, task, action
292
293 This function returns a new object of a *socket* class.
294
295 :returns: A socket class object.
296
Thierry FOURNIER486f5a02015-03-16 15:13:03 +0100297.. js:function:: core.yield()
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100298
299 **context**: task, action, sample-fetch, converter
300
301 Give back the hand at the HAProxy scheduler. It is used when the LUA
302 processing consumes a lot of processing time.
303
304Fetches class
305=============
306
307.. js:class:: Fetches
308
309 This class contains a lot of internal HAProxy sample fetches. See the
310 HAProxy documentation for more information about her usage.
311
312Converters class
313================
314
315.. js:class:: Converters
316
317 This class contains a lot of internal HAProxy sample converters. See the
318 HAProxy documentation for more information about her usage.
319
320Channel class
321=============
322
323.. js:class:: Channel
324
325 HAProxy uses two buffers for the processing of the requests. The first one is
326 used with the request data (from the client to the server) and the second is
327 used for the response data (from the server to the client).
328
329 Each buffer contains two types of data. The first type is the incoming data
330 waiting for a processing. The second part is the outgoing data already
331 processed. Usually, the incoming data is processed, after it is tagged as
332 outgoing data, and finally it is sent. The following functions provides tools
333 for manipulating these data in a buffer.
334
335 The following diagram shows where the channel class function are applied.
336
337 **Warning**: It is not possible to read from the response in request action,
338 and it is not possible to read for the request channel in response action.
339
Thierry FOURNIER486f5a02015-03-16 15:13:03 +0100340.. image:: _static/Channel.png
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100341
Thierry FOURNIER486f5a02015-03-16 15:13:03 +0100342.. js:function:: Channel.dup(channel)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100343
344 This function returns a string that contain the entire buffer. The data is
345 not remove from the buffer and can be reprocessed later.
346
347 If the buffer cant receive more data, a 'nil' value is returned.
348
Thierry FOURNIER486f5a02015-03-16 15:13:03 +0100349 :param class_channel channel: The manipulated Channel.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100350 :returns: a string containig all the avalaible data or nil.
351
Thierry FOURNIER486f5a02015-03-16 15:13:03 +0100352.. js:function:: Channel.get(channel)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100353
354 This function returns a string that contain the entire buffer. The data is
355 consumed from the buffer.
356
357 If the buffer cant receive more data, a 'nil' value is returned.
358
Thierry FOURNIER486f5a02015-03-16 15:13:03 +0100359 :param class_channel channel: The manipulated Channel.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100360 :returns: a string containig all the avalaible data or nil.
361
Thierry FOURNIER486f5a02015-03-16 15:13:03 +0100362.. js:function:: Channel.get_line(channel)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100363
364 This function returns a string that contain the first line of the buffer. The
365 data is consumed. If the data returned doesn't contains a final '\n' its
366 assumed than its the last available data in the buffer.
367
368 If the buffer cant receive more data, a 'nil' value is returned.
369
Thierry FOURNIER486f5a02015-03-16 15:13:03 +0100370 :param class_channel channel: The manipulated Channel.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100371 :returns: a string containig the avalaiable line or nil.
372
Thierry FOURNIER486f5a02015-03-16 15:13:03 +0100373.. js:function:: Channel.set(channel, string)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100374
375 This function replace the content of the buffer by the string. The function
376 returns the copied length, otherwise, it returns -1.
377
378 The data set with this function are not send. They wait for the end of
379 HAProxy processing, so the buffer can be full.
380
Thierry FOURNIER486f5a02015-03-16 15:13:03 +0100381 :param class_channel channel: The manipulated Channel.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100382 :param string string: The data which will sent.
383 :returns: an integer containing the amount of butes copyed or -1.
384
Thierry FOURNIER486f5a02015-03-16 15:13:03 +0100385.. js:function:: Channel.append(channel, string)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100386
387 This function append the string argument to the content of the buffer. The
388 function returns the copied length, otherwise, it returns -1.
389
390 The data set with this function are not send. They wait for the end of
391 HAProxy processing, so the buffer can be full.
392
Thierry FOURNIER486f5a02015-03-16 15:13:03 +0100393 :param class_channel channel: The manipulated Channel.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100394 :param string string: The data which will sent.
395 :returns: an integer containing the amount of butes copyed or -1.
396
Thierry FOURNIER486f5a02015-03-16 15:13:03 +0100397.. js:function:: Channel.send(channel, string)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100398
399 This function required immediate send of the data. Unless if the connection
400 is close, the buffer is regularly flushed and all the string can be sent.
401
Thierry FOURNIER486f5a02015-03-16 15:13:03 +0100402 :param class_channel channel: The manipulated Channel.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100403 :param string string: The data which will sent.
404 :returns: an integer containing the amount of butes copyed or -1.
405
Thierry FOURNIER486f5a02015-03-16 15:13:03 +0100406.. js:function:: Channel.get_in_length(channel)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100407
408 This function returns the length of the input part of the buffer.
409
Thierry FOURNIER486f5a02015-03-16 15:13:03 +0100410 :param class_channel channel: The manipulated Channel.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100411 :returns: an integer containing the amount of avalaible bytes.
412
Thierry FOURNIER486f5a02015-03-16 15:13:03 +0100413.. js:function:: Channel.get_out_length(channel)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100414
415 This function returns the length of the output part of the buffer.
416
Thierry FOURNIER486f5a02015-03-16 15:13:03 +0100417 :param class_channel channel: The manipulated Channel.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100418 :returns: an integer containing the amount of avalaible bytes.
419
Thierry FOURNIER486f5a02015-03-16 15:13:03 +0100420.. js:function:: Channel.forward(channel, int)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100421
422 This function transfer bytes from the input part of the buffer to the output
423 part.
424
Thierry FOURNIER486f5a02015-03-16 15:13:03 +0100425 :param class_channel channel: The manipulated Channel.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100426 :param integer int: The amount of data which will be forwarded.
427
Thierry FOURNIER08504f42015-03-16 14:17:08 +0100428
429HTTP class
430==========
431
432.. js:class:: HTTP
433
434 This class contain all the HTTP manipulation functions.
435
Thierry FOURNIER486f5a02015-03-16 15:13:03 +0100436.. js:function:: HTTP.req_get_header(http)
Thierry FOURNIER08504f42015-03-16 14:17:08 +0100437
438 Returns an array containing all the request headers.
439
440 :param class_http http: The related http object.
441 :returns: array of headers.
Thierry FOURNIER486f5a02015-03-16 15:13:03 +0100442 :see: HTTP.res_get_header()
Thierry FOURNIER08504f42015-03-16 14:17:08 +0100443
Thierry FOURNIER486f5a02015-03-16 15:13:03 +0100444.. js:function:: HTTP.res_get_header(http)
Thierry FOURNIER08504f42015-03-16 14:17:08 +0100445
446 Returns an array containing all the response headers.
447
448 :param class_http http: The related http object.
449 :returns: array of headers.
Thierry FOURNIER486f5a02015-03-16 15:13:03 +0100450 :see: HTTP.req_get_header()
Thierry FOURNIER08504f42015-03-16 14:17:08 +0100451
Thierry FOURNIER486f5a02015-03-16 15:13:03 +0100452.. js:function:: HTTP.req_add_header(http, name, value)
Thierry FOURNIER08504f42015-03-16 14:17:08 +0100453
454 Appends an HTTP header field in the request whose name is
455 specified in "name" and whose value is defined in "value".
456
457 :param class_http http: The related http object.
458 :param string name: The header name.
459 :param string value: The header value.
Thierry FOURNIER486f5a02015-03-16 15:13:03 +0100460 :see: HTTP.res_add_header()
Thierry FOURNIER08504f42015-03-16 14:17:08 +0100461
Thierry FOURNIER486f5a02015-03-16 15:13:03 +0100462.. js:function:: HTTP.res_add_header(http, name, value)
Thierry FOURNIER08504f42015-03-16 14:17:08 +0100463
464 appends an HTTP header field in the response whose name is
465 specified in "name" and whose value is defined in "value".
466
467 :param class_http http: The related http object.
468 :param string name: The header name.
469 :param string value: The header value.
Thierry FOURNIER486f5a02015-03-16 15:13:03 +0100470 :see: HTTP.req_add_header()
Thierry FOURNIER08504f42015-03-16 14:17:08 +0100471
Thierry FOURNIER486f5a02015-03-16 15:13:03 +0100472.. js:function:: HTTP.req_del_header(http, name)
Thierry FOURNIER08504f42015-03-16 14:17:08 +0100473
474 Removes all HTTP header fields in the request whose name is
475 specified in "name".
476
477 :param class_http http: The related http object.
478 :param string name: The header name.
Thierry FOURNIER486f5a02015-03-16 15:13:03 +0100479 :see: HTTP.res_del_header()
Thierry FOURNIER08504f42015-03-16 14:17:08 +0100480
Thierry FOURNIER486f5a02015-03-16 15:13:03 +0100481.. js:function:: HTTP.res_del_header(http, name)
Thierry FOURNIER08504f42015-03-16 14:17:08 +0100482
483 Removes all HTTP header fields in the response whose name is
484 specified in "name".
485
486 :param class_http http: The related http object.
487 :param string name: The header name.
Thierry FOURNIER486f5a02015-03-16 15:13:03 +0100488 :see: HTTP.req_del_header()
Thierry FOURNIER08504f42015-03-16 14:17:08 +0100489
Thierry FOURNIER486f5a02015-03-16 15:13:03 +0100490.. js:function:: HTTP.req_set_header(http, name, value)
Thierry FOURNIER08504f42015-03-16 14:17:08 +0100491
492 This variable replace all occurence of all header "name", by only
493 one containing the "value".
494
495 :param class_http http: The related http object.
496 :param string name: The header name.
497 :param string value: The header value.
Thierry FOURNIER486f5a02015-03-16 15:13:03 +0100498 :see: HTTP.res_set_header()
Thierry FOURNIER08504f42015-03-16 14:17:08 +0100499
500 This function does the same work as the folowwing code:
501
502.. code-block:: lua
503
504 function fcn(txn)
Thierry FOURNIER486f5a02015-03-16 15:13:03 +0100505 TXN.http:req_del_header("header")
506 TXN.http:req_add_header("header", "value")
Thierry FOURNIER08504f42015-03-16 14:17:08 +0100507 end
508..
509
Thierry FOURNIER486f5a02015-03-16 15:13:03 +0100510.. js:function:: HTTP.res_set_header(http, name, value)
Thierry FOURNIER08504f42015-03-16 14:17:08 +0100511
512 This variable replace all occurence of all header "name", by only
513 one containing the "value".
514
515 :param class_http http: The related http object.
516 :param string name: The header name.
517 :param string value: The header value.
Thierry FOURNIER486f5a02015-03-16 15:13:03 +0100518 :see: HTTP.req_set_header()
Thierry FOURNIER08504f42015-03-16 14:17:08 +0100519
Thierry FOURNIER486f5a02015-03-16 15:13:03 +0100520.. js:function:: HTTP.req_replace_header(http, name, regex, replace)
Thierry FOURNIER08504f42015-03-16 14:17:08 +0100521
522 Matches the regular expression in all occurrences of header field "name"
523 according to "regex", and replaces them with the "replace" argument. The
524 replacement value can contain back references like \1, \2, ... This
525 function works with the request.
526
527 :param class_http http: The related http object.
528 :param string name: The header name.
529 :param string regex: The match regular expression.
530 :param string replace: The replacement value.
Thierry FOURNIER486f5a02015-03-16 15:13:03 +0100531 :see: HTTP.res_replace_header()
Thierry FOURNIER08504f42015-03-16 14:17:08 +0100532
Thierry FOURNIER486f5a02015-03-16 15:13:03 +0100533.. js:function:: HTTP.res_replace_header(http, name, regex, string)
Thierry FOURNIER08504f42015-03-16 14:17:08 +0100534
535 Matches the regular expression in all occurrences of header field "name"
536 according to "regex", and replaces them with the "replace" argument. The
537 replacement value can contain back references like \1, \2, ... This
538 function works with the request.
539
540 :param class_http http: The related http object.
541 :param string name: The header name.
542 :param string regex: The match regular expression.
543 :param string replace: The replacement value.
Thierry FOURNIER486f5a02015-03-16 15:13:03 +0100544 :see: HTTP.req_replace_header()
Thierry FOURNIER08504f42015-03-16 14:17:08 +0100545
Thierry FOURNIER486f5a02015-03-16 15:13:03 +0100546.. js:function:: HTTP.req_replace_value(http, name, regex, replace)
Thierry FOURNIER08504f42015-03-16 14:17:08 +0100547
Thierry FOURNIER486f5a02015-03-16 15:13:03 +0100548 Works like "HTTP.req_replace_header()" except that it matches the regex
Thierry FOURNIER08504f42015-03-16 14:17:08 +0100549 against every comma-delimited value of the header field "name" instead of the
550 entire header.
551
552 :param class_http http: The related http object.
553 :param string name: The header name.
554 :param string regex: The match regular expression.
555 :param string replace: The replacement value.
Thierry FOURNIER486f5a02015-03-16 15:13:03 +0100556 :see: HTTP.req_replace_header()
557 :see: HTTP.res_replace_value()
Thierry FOURNIER08504f42015-03-16 14:17:08 +0100558
Thierry FOURNIER486f5a02015-03-16 15:13:03 +0100559.. js:function:: HTTP.res_replace_value(http, name, regex, replace)
Thierry FOURNIER08504f42015-03-16 14:17:08 +0100560
Thierry FOURNIER486f5a02015-03-16 15:13:03 +0100561 Works like "HTTP.res_replace_header()" except that it matches the regex
Thierry FOURNIER08504f42015-03-16 14:17:08 +0100562 against every comma-delimited value of the header field "name" instead of the
563 entire header.
564
565 :param class_http http: The related http object.
566 :param string name: The header name.
567 :param string regex: The match regular expression.
568 :param string replace: The replacement value.
Thierry FOURNIER486f5a02015-03-16 15:13:03 +0100569 :see: HTTP.res_replace_header()
570 :see: HTTP.req_replace_value()
Thierry FOURNIER08504f42015-03-16 14:17:08 +0100571
Thierry FOURNIER486f5a02015-03-16 15:13:03 +0100572.. js:function:: HTTP.req_set_method(http, method)
Thierry FOURNIER08504f42015-03-16 14:17:08 +0100573
574 Rewrites the request method with the parameter "method".
575
576 :param class_http http: The related http object.
577 :param string method: The new method.
578
Thierry FOURNIER486f5a02015-03-16 15:13:03 +0100579.. js:function:: HTTP.req_set_path(http, path)
Thierry FOURNIER08504f42015-03-16 14:17:08 +0100580
581 Rewrites the request path with the "path" parameter.
582
583 :param class_http http: The related http object.
584 :param string path: The new path.
585
Thierry FOURNIER486f5a02015-03-16 15:13:03 +0100586.. js:function:: HTTP.req_set_query(http, query)
Thierry FOURNIER08504f42015-03-16 14:17:08 +0100587
588 Rewrites the request's query string which appears after the first question
589 mark ("?") with the parameter "query".
590
591 :param class_http http: The related http object.
592 :param string query: The new query.
593
Thierry FOURNIER486f5a02015-03-16 15:13:03 +0100594.. js:function:: HTTP.req.set_uri(http, uri)
Thierry FOURNIER08504f42015-03-16 14:17:08 +0100595
596 Rewrites the request URI with the parameter "uri".
597
598 :param class_http http: The related http object.
599 :param string uri: The new uri.
600
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100601TXN class
602=========
603
604.. js:class:: TXN
605
606 The txn class contain all the functions relative to the http or tcp
607 transaction (Note than a tcp stream is the same than a tcp transaction, but
608 an HTTP transaction is not the same than a tcp stream).
609
610 The usage of this class permits to retrieve data from the requests, alter it
611 and forward it.
612
613 All the functions provided by this class are available in the context
614 **sample-fetches** and **actions**.
615
Thierry FOURNIER486f5a02015-03-16 15:13:03 +0100616.. js:attribute:: TXN.c
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100617
618 This attribute contains a Converters class object.
619
Thierry FOURNIER486f5a02015-03-16 15:13:03 +0100620.. js:attribute:: TXN.sc
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100621
622 This attribute contains a Converters class object. The functions of
623 this object returns always a string.
624
Thierry FOURNIER486f5a02015-03-16 15:13:03 +0100625.. js:attribute:: TXN.f
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100626
627 This attribute contains a Fetches class object.
628
Thierry FOURNIER486f5a02015-03-16 15:13:03 +0100629.. js:attribute:: TXN.sf
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100630
631 This attribute contains a Fetches class object. The functions of
632 this object returns always a string.
633
Thierry FOURNIER486f5a02015-03-16 15:13:03 +0100634.. js:attribute:: TXN.req
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100635
636 This attribute contains a channel class object for the request buffer.
637
Thierry FOURNIER486f5a02015-03-16 15:13:03 +0100638.. js:attribute:: TXN.res
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100639
640 This attribute contains a channel class object for the response buffer.
641
Thierry FOURNIER486f5a02015-03-16 15:13:03 +0100642.. js:attribute:: TXN.http
Thierry FOURNIER08504f42015-03-16 14:17:08 +0100643
644 This attribute contains an HTTP class object. It is avalaible only if the
645 proxy has the "mode http" enabled.
646
Thierry FOURNIER486f5a02015-03-16 15:13:03 +0100647.. js:function:: TXN.get_priv(txn)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100648
Thierry FOURNIER486f5a02015-03-16 15:13:03 +0100649 Return Lua data stored in the current transaction (with the `TXN.set_priv()`)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100650 function. If no data are stored, it returns a nil value.
651
652 :param class_txn txn: The class txn object containing the data.
653 :returns: the opaque data previsously stored, or nil if nothing is
654 avalaible.
655
Thierry FOURNIER486f5a02015-03-16 15:13:03 +0100656.. js:function:: TXN.set_priv(txn, data)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100657
658 Store any data in the current HAProxy transaction. This action replace the
659 old stored data.
660
661 :param class_txn txn: The class txn object containing the data.
662 :param opaque data: The data which is stored in the transaction.
663
Thierry FOURNIER486f5a02015-03-16 15:13:03 +0100664.. js:function:: TXN.get_headers(txn)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100665
666 This function returns an array of headers.
667
668 :param class_txn txn: The class txn object containing the data.
669 :returns: an array of headers.
670
Thierry FOURNIER486f5a02015-03-16 15:13:03 +0100671.. js:function:: TXN.close(txn)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100672
673 This function close the transaction and the associated session. It can be
674 used when a critical error is detected.
675
676 :param class_txn txn: The class txn object containing the data.
677
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100678
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100679
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100680
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100681
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100682
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100683
Thierry FOURNIER486f5a02015-03-16 15:13:03 +0100684.. js:function:: TXN.set_loglevel(txn, loglevel)
Thierry FOURNIER2cce3532015-03-16 12:04:16 +0100685
686 Is used to change the log level of the current request. The "loglevel" must
687 be an integer between 0 and 7.
688
689 :param class_txn txn: The class txn object containing the data.
690 :param integer loglevel: The required log level. This variable can be one of
691 :see: core.<loglevel>
692
Thierry FOURNIER486f5a02015-03-16 15:13:03 +0100693.. js:function:: TXN.set_tos(txn, tos)
Thierry FOURNIER2cce3532015-03-16 12:04:16 +0100694
695 Is used to set the TOS or DSCP field value of packets sent to the client to
696 the value passed in "tos" on platforms which support this.
697
698 :param class_txn txn: The class txn object containing the data.
699 :param integer tos: The new TOS os DSCP.
700
Thierry FOURNIER486f5a02015-03-16 15:13:03 +0100701.. js:function:: TXN.set_mark(txn, mark)
Thierry FOURNIER2cce3532015-03-16 12:04:16 +0100702
703 Is used to set the Netfilter MARK on all packets sent to the client to the
704 value passed in "mark" on platforms which support it.
705
706 :param class_txn txn: The class txn object containing the data.
707 :param integer mark: The mark value.
708
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100709Socket class
710============
711
712.. js:class:: Socket
713
714 This class must be compatible with the Lua Socket class. Only the 'client'
715 functions are available. See the Lua Socket documentation:
716
717 `http://w3.impa.br/~diego/software/luasocket/tcp.html
718 <http://w3.impa.br/~diego/software/luasocket/tcp.html>`_
719
Thierry FOURNIER486f5a02015-03-16 15:13:03 +0100720.. js:function:: Socket.close(socket)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100721
722 Closes a TCP object. The internal socket used by the object is closed and the
723 local address to which the object was bound is made available to other
724 applications. No further operations (except for further calls to the close
Thierry FOURNIER486f5a02015-03-16 15:13:03 +0100725 method) are allowed on a closed Socket.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100726
Thierry FOURNIER486f5a02015-03-16 15:13:03 +0100727 :param class_socket socket: Is the manipulated Socket.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100728
729 Note: It is important to close all used sockets once they are not needed,
730 since, in many systems, each socket uses a file descriptor, which are limited
731 system resources. Garbage-collected objects are automatically closed before
732 destruction, though.
733
Thierry FOURNIER486f5a02015-03-16 15:13:03 +0100734.. js:function:: Socket.connect(socket, address, port)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100735
736 Attempts to connect a socket object to a remote host.
737
738 Address can be an IP address or a host name. Port must be an integer number
739 in the range [1..64K).
740
741 In case of error, the method returns nil followed by a string describing the
742 error. In case of success, the method returns 1.
743
Thierry FOURNIER486f5a02015-03-16 15:13:03 +0100744 :param class_socket socket: Is the manipulated Socket.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100745 :returns: 1 or nil.
746
Thierry FOURNIER486f5a02015-03-16 15:13:03 +0100747 Note: The function Socket.connect is available and is a shortcut for the
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100748 creation of client sockets.
749
750 Note: Starting with LuaSocket 2.0, the settimeout method affects the behavior
751 of connect, causing it to return with an error in case of a timeout. If that
Thierry FOURNIER486f5a02015-03-16 15:13:03 +0100752 happens, you can still call Socket.select with the socket in the sendt table.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100753 The socket will be writable when the connection is established.
754
Thierry FOURNIER486f5a02015-03-16 15:13:03 +0100755.. js:function:: Socket.connect_ssl(socket, address, port)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100756
757 Same behavior than the function socket:connect, but uses SSL.
758
Thierry FOURNIER486f5a02015-03-16 15:13:03 +0100759 :param class_socket socket: Is the manipulated Socket.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100760 :returns: 1 or nil.
761
Thierry FOURNIER486f5a02015-03-16 15:13:03 +0100762.. js:function:: Socket.getpeername(socket)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100763
764 Returns information about the remote side of a connected client object.
765
766 Returns a string with the IP address of the peer, followed by the port number
767 that peer is using for the connection. In case of error, the method returns
768 nil.
769
Thierry FOURNIER486f5a02015-03-16 15:13:03 +0100770 :param class_socket socket: Is the manipulated Socket.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100771 :returns: a string containing the server information.
772
Thierry FOURNIER486f5a02015-03-16 15:13:03 +0100773.. js:function:: Socket.getsockname(socket)
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100774
775 Returns the local address information associated to the object.
776
777 The method returns a string with local IP address and a number with the port.
778 In case of error, the method returns nil.
779
Thierry FOURNIER486f5a02015-03-16 15:13:03 +0100780 :param class_socket socket: Is the manipulated Socket.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100781 :returns: a string containing the client information.
782
Thierry FOURNIER486f5a02015-03-16 15:13:03 +0100783.. js:function:: Socket.receive(socket, [pattern [, prefix]])
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100784
785 Reads data from a client object, according to the specified read pattern.
786 Patterns follow the Lua file I/O format, and the difference in performance
787 between all patterns is negligible.
788
Thierry FOURNIER486f5a02015-03-16 15:13:03 +0100789 :param class_socket socket: Is the manipulated Socket.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100790 :param string|integer pattern: Describe what is required (see below).
791 :param string prefix: A string which will be prefix the returned data.
792 :returns: a string containing the required data or nil.
793
794 Pattern can be any of the following:
795
796 * **`*a`**: reads from the socket until the connection is closed. No
797 end-of-line translation is performed;
798
Thierry FOURNIER486f5a02015-03-16 15:13:03 +0100799 * **`*l`**: reads a line of text from the Socket. The line is terminated by a
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100800 LF character (ASCII 10), optionally preceded by a CR character
801 (ASCII 13). The CR and LF characters are not included in the
802 returned line. In fact, all CR characters are ignored by the
803 pattern. This is the default pattern.
804
805 * **number**: causes the method to read a specified number of bytes from the
Thierry FOURNIER486f5a02015-03-16 15:13:03 +0100806 Socket. Prefix is an optional string to be concatenated to the
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100807 beginning of any received data before return.
808
809 If successful, the method returns the received pattern. In case of error, the
810 method returns nil followed by an error message which can be the string
811 'closed' in case the connection was closed before the transmission was
812 completed or the string 'timeout' in case there was a timeout during the
813 operation. Also, after the error message, the function returns the partial
814 result of the transmission.
815
816 Important note: This function was changed severely. It used to support
817 multiple patterns (but I have never seen this feature used) and now it
818 doesn't anymore. Partial results used to be returned in the same way as
819 successful results. This last feature violated the idea that all functions
820 should return nil on error. Thus it was changed too.
821
Thierry FOURNIER486f5a02015-03-16 15:13:03 +0100822.. js:function:: Socket.send(socket, data [, start [, end ]])
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100823
824 Sends data through client object.
825
Thierry FOURNIER486f5a02015-03-16 15:13:03 +0100826 :param class_socket socket: Is the manipulated Socket.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100827 :param string data: The data that will be sent.
828 :param integer start: The start position in the buffer of the data which will
829 be sent.
830 :param integer end: The end position in the buffer of the data which will
831 be sent.
832 :returns: see below.
833
834 Data is the string to be sent. The optional arguments i and j work exactly
835 like the standard string.sub Lua function to allow the selection of a
836 substring to be sent.
837
838 If successful, the method returns the index of the last byte within [start,
839 end] that has been sent. Notice that, if start is 1 or absent, this is
840 effectively the total number of bytes sent. In case of error, the method
841 returns nil, followed by an error message, followed by the index of the last
842 byte within [start, end] that has been sent. You might want to try again from
843 the byte following that. The error message can be 'closed' in case the
844 connection was closed before the transmission was completed or the string
845 'timeout' in case there was a timeout during the operation.
846
847 Note: Output is not buffered. For small strings, it is always better to
848 concatenate them in Lua (with the '..' operator) and send the result in one
849 call instead of calling the method several times.
850
Thierry FOURNIER486f5a02015-03-16 15:13:03 +0100851.. js:function:: Socket.setoption(socket, option [, value])
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100852
853 Just implemented for compatibility, this cal does nothing.
854
Thierry FOURNIER486f5a02015-03-16 15:13:03 +0100855.. js:function:: Socket.settimeout(socket, value [, mode])
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100856
857 Changes the timeout values for the object. All I/O operations are blocking.
858 That is, any call to the methods send, receive, and accept will block
859 indefinitely, until the operation completes. The settimeout method defines a
860 limit on the amount of time the I/O methods can block. When a timeout time
861 has elapsed, the affected methods give up and fail with an error code.
862
863 The amount of time to wait is specified as the value parameter, in seconds.
864
865 The timeout modes are bot implemented, the only settable timeout is the
866 inactivity time waiting for complete the internal buffer send or waiting for
867 receive data.
868
Thierry FOURNIER486f5a02015-03-16 15:13:03 +0100869 :param class_socket socket: Is the manipulated Socket.
Thierry FOURNIER17bd1522015-03-11 20:31:00 +0100870 :param integer value: The timeout value.
871
872External Lua libraries
873======================
874
875A lot of useful lua libraries can be found here:
876
877* `https://lua-toolbox.com/ <https://lua-toolbox.com/>`_
878
879Redis acces:
880
881* `https://github.com/nrk/redis-lua <https://github.com/nrk/redis-lua>`_
882
883This is an example about the usage of the Redis library with HAProxy. Note that
884each call of any function of this library can throw an error if the socket
885connection fails.
886
887.. code-block:: lua
888
889 -- load the redis library
890 local redis = require("redis");
891
892 function do_something(txn)
893
894 -- create and connect new tcp socket
895 local tcp = core.tcp();
896 tcp:settimeout(1);
897 tcp:connect("127.0.0.1", 6379);
898
899 -- use the redis library with this new socket
900 local client = redis.connect({socket=tcp});
901 client:ping();
902
903 end
904
905OpenSSL:
906
907* `http://mkottman.github.io/luacrypto/index.html
908 <http://mkottman.github.io/luacrypto/index.html>`_
909
910* `https://github.com/brunoos/luasec/wiki
911 <https://github.com/brunoos/luasec/wiki>`_
912