Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 1 | .. toctree:: |
| 2 | :maxdepth: 2 |
| 3 | |
| 4 | |
| 5 | How Lua runs in HAProxy |
| 6 | ======================= |
| 7 | |
| 8 | HAProxy Lua running contexts |
| 9 | ---------------------------- |
| 10 | |
| 11 | The Lua code executed in HAProxy can be processed in 2 main modes. The first one |
| 12 | is 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 | |
| 22 | The Lua code is loaded in one or more files. These files contains main code and |
| 23 | functions. Lua have 6 execution context. |
| 24 | |
| 25 | 1. The Lua file **body context**. It is executed during the load of the Lua file |
| 26 | in the HAProxy `[global]` section with the directive `lua-load`. It is |
| 27 | executed in initialisation mode. This section is use for configuring Lua |
| 28 | bindings in HAProxy. |
| 29 | |
David Carlier | 61fdf8b | 2015-10-02 11:59:38 +0100 | [diff] [blame] | 30 | 2. The Lua **init context**. It is a Lua function executed just after the |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 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 | |
David Carlier | 61fdf8b | 2015-10-02 11:59:38 +0100 | [diff] [blame] | 38 | 3. The Lua **task context**. It is a Lua function executed after the start |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 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 | |
David Carlier | 61fdf8b | 2015-10-02 11:59:38 +0100 | [diff] [blame] | 45 | 4. The **action context**. It is a Lua function conditionally executed. These |
Thierry FOURNIER | a2d0225 | 2015-10-01 15:00:42 +0200 | [diff] [blame] | 46 | actions are registered by the Lua directives "`core.register_action()`". The |
| 47 | prototype of the Lua called function is a function with doesn't returns |
| 48 | anything and that take an object of class TXN as entry. `function fcn(txn)`. |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 49 | |
| 50 | 5. The **sample-fetch context**. This function takes a TXN object as entry |
| 51 | argument and returns a string. These types of function cannot execute any |
| 52 | blocking function. They are useful to aggregate some of original HAProxy |
| 53 | sample-fetches and return the result. The prototype of the function is |
| 54 | `function string fcn(txn)`. These functions can be registered with the Lua |
| 55 | function `core.register_fetches()`. Each declared sample-fetch is prefixed by |
| 56 | the string "lua.". |
| 57 | |
| 58 | **NOTE**: It is possible that this function cannot found the required data |
| 59 | in the original HAProxy sample-fetches, in this case, it cannot return the |
| 60 | result. This case is not yet supported |
| 61 | |
David Carlier | 61fdf8b | 2015-10-02 11:59:38 +0100 | [diff] [blame] | 62 | 6. The **converter context**. It is a Lua function that takes a string as input |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 63 | and returns another string as output. These types of function are stateless, |
| 64 | it cannot access to any context. They don't execute any blocking function. |
| 65 | The call prototype is `function string fcn(string)`. This function can be |
| 66 | registered with the Lua function `core.register_converters()`. Each declared |
| 67 | converter is prefixed by the string "lua.". |
| 68 | |
| 69 | HAProxy Lua Hello world |
| 70 | ----------------------- |
| 71 | |
| 72 | HAProxy configuration file (`hello_world.conf`): |
| 73 | |
| 74 | :: |
| 75 | |
| 76 | global |
| 77 | lua-load hello_world.lua |
| 78 | |
| 79 | listen proxy |
| 80 | bind 127.0.0.1:10001 |
Thierry FOURNIER | a2d0225 | 2015-10-01 15:00:42 +0200 | [diff] [blame] | 81 | tcp-request inspect-delay 1s |
| 82 | tcp-request content use-service lua.hello_world |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 83 | |
| 84 | HAProxy Lua file (`hello_world.lua`): |
| 85 | |
| 86 | .. code-block:: lua |
| 87 | |
Thierry FOURNIER | a2d0225 | 2015-10-01 15:00:42 +0200 | [diff] [blame] | 88 | core.register_service("hello_world", "tcp", function(applet) |
| 89 | applet:send("hello world\n") |
| 90 | end) |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 91 | |
| 92 | How to start HAProxy for testing this configuration: |
| 93 | |
| 94 | :: |
| 95 | |
| 96 | ./haproxy -f hello_world.conf |
| 97 | |
| 98 | On other terminal, you can test with telnet: |
| 99 | |
| 100 | :: |
| 101 | |
| 102 | #:~ telnet 127.0.0.1 10001 |
| 103 | hello world |
| 104 | |
| 105 | Core class |
| 106 | ========== |
| 107 | |
| 108 | .. js:class:: core |
| 109 | |
| 110 | The "core" class contains all the HAProxy core functions. These function are |
| 111 | useful for the controlling the execution flow, registering hooks, manipulating |
| 112 | global maps or ACL, ... |
| 113 | |
| 114 | "core" class is basically provided with HAProxy. No `require` line is |
| 115 | required to uses these function. |
| 116 | |
David Carlier | 61fdf8b | 2015-10-02 11:59:38 +0100 | [diff] [blame] | 117 | The "core" class is static, it is not possible to create a new object of this |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 118 | type. |
| 119 | |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 120 | .. js:attribute:: core.emerg |
| 121 | |
Thierry FOURNIER | dc59500 | 2015-12-21 11:13:52 +0100 | [diff] [blame] | 122 | :returns: integer |
| 123 | |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 124 | This attribute is an integer, it contains the value of the loglevel "emergency" (0). |
| 125 | |
| 126 | .. js:attribute:: core.alert |
| 127 | |
Thierry FOURNIER | dc59500 | 2015-12-21 11:13:52 +0100 | [diff] [blame] | 128 | :returns: integer |
| 129 | |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 130 | This attribute is an integer, it contains the value of the loglevel "alert" (1). |
| 131 | |
| 132 | .. js:attribute:: core.crit |
| 133 | |
Thierry FOURNIER | dc59500 | 2015-12-21 11:13:52 +0100 | [diff] [blame] | 134 | :returns: integer |
| 135 | |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 136 | This attribute is an integer, it contains the value of the loglevel "critical" (2). |
| 137 | |
| 138 | .. js:attribute:: core.err |
| 139 | |
Thierry FOURNIER | dc59500 | 2015-12-21 11:13:52 +0100 | [diff] [blame] | 140 | :returns: integer |
| 141 | |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 142 | This attribute is an integer, it contains the value of the loglevel "error" (3). |
| 143 | |
| 144 | .. js:attribute:: core.warning |
| 145 | |
Thierry FOURNIER | dc59500 | 2015-12-21 11:13:52 +0100 | [diff] [blame] | 146 | :returns: integer |
| 147 | |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 148 | This attribute is an integer, it contains the value of the loglevel "warning" (4). |
| 149 | |
| 150 | .. js:attribute:: core.notice |
| 151 | |
Thierry FOURNIER | dc59500 | 2015-12-21 11:13:52 +0100 | [diff] [blame] | 152 | :returns: integer |
| 153 | |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 154 | This attribute is an integer, it contains the value of the loglevel "notice" (5). |
| 155 | |
| 156 | .. js:attribute:: core.info |
| 157 | |
Thierry FOURNIER | dc59500 | 2015-12-21 11:13:52 +0100 | [diff] [blame] | 158 | :returns: integer |
| 159 | |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 160 | This attribute is an integer, it contains the value of the loglevel "info" (6). |
| 161 | |
| 162 | .. js:attribute:: core.debug |
| 163 | |
Thierry FOURNIER | dc59500 | 2015-12-21 11:13:52 +0100 | [diff] [blame] | 164 | :returns: integer |
| 165 | |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 166 | This attribute is an integer, it contains the value of the loglevel "debug" (7). |
| 167 | |
| 168 | .. js:function:: core.log(loglevel, msg) |
| 169 | |
| 170 | **context**: body, init, task, action, sample-fetch, converter |
| 171 | |
David Carlier | 61fdf8b | 2015-10-02 11:59:38 +0100 | [diff] [blame] | 172 | This function sends a log. The log is sent, according with the HAProxy |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 173 | configuration file, on the default syslog server if it is configured and on |
| 174 | the stderr if it is allowed. |
| 175 | |
| 176 | :param integer loglevel: Is the log level asociated with the message. It is a |
| 177 | number between 0 and 7. |
| 178 | :param string msg: The log content. |
| 179 | :see: core.emerg, core.alert, core.crit, core.err, core.warning, core.notice, |
| 180 | core.info, core.debug (log level definitions) |
| 181 | :see: code.Debug |
| 182 | :see: core.Info |
| 183 | :see: core.Warning |
| 184 | :see: core.Alert |
| 185 | |
| 186 | .. js:function:: core.Debug(msg) |
| 187 | |
| 188 | **context**: body, init, task, action, sample-fetch, converter |
| 189 | |
| 190 | :param string msg: The log content. |
| 191 | :see: log |
| 192 | |
| 193 | Does the same job than: |
| 194 | |
| 195 | .. code-block:: lua |
| 196 | |
Thierry FOURNIER | dc59500 | 2015-12-21 11:13:52 +0100 | [diff] [blame] | 197 | function Debug(msg) |
| 198 | core.log(core.debug, msg) |
| 199 | end |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 200 | .. |
| 201 | |
| 202 | .. js:function:: core.Info(msg) |
| 203 | |
| 204 | **context**: body, init, task, action, sample-fetch, converter |
| 205 | |
| 206 | :param string msg: The log content. |
| 207 | :see: log |
| 208 | |
| 209 | .. code-block:: lua |
| 210 | |
Thierry FOURNIER | dc59500 | 2015-12-21 11:13:52 +0100 | [diff] [blame] | 211 | function Info(msg) |
| 212 | core.log(core.info, msg) |
| 213 | end |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 214 | .. |
| 215 | |
| 216 | .. js:function:: core.Warning(msg) |
| 217 | |
| 218 | **context**: body, init, task, action, sample-fetch, converter |
| 219 | |
| 220 | :param string msg: The log content. |
| 221 | :see: log |
| 222 | |
| 223 | .. code-block:: lua |
| 224 | |
Thierry FOURNIER | dc59500 | 2015-12-21 11:13:52 +0100 | [diff] [blame] | 225 | function Warning(msg) |
| 226 | core.log(core.warning, msg) |
| 227 | end |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 228 | .. |
| 229 | |
| 230 | .. js:function:: core.Alert(msg) |
| 231 | |
| 232 | **context**: body, init, task, action, sample-fetch, converter |
| 233 | |
| 234 | :param string msg: The log content. |
| 235 | :see: log |
| 236 | |
| 237 | .. code-block:: lua |
| 238 | |
Thierry FOURNIER | dc59500 | 2015-12-21 11:13:52 +0100 | [diff] [blame] | 239 | function Alert(msg) |
| 240 | core.log(core.alert, msg) |
| 241 | end |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 242 | .. |
| 243 | |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 244 | .. js:function:: core.add_acl(filename, key) |
| 245 | |
| 246 | **context**: init, task, action, sample-fetch, converter |
| 247 | |
| 248 | Add the ACL *key* in the ACLs list referenced by the file *filename*. |
| 249 | |
| 250 | :param string filename: the filename that reference the ACL entries. |
| 251 | :param string key: the key which will be added. |
| 252 | |
| 253 | .. js:function:: core.del_acl(filename, key) |
| 254 | |
| 255 | **context**: init, task, action, sample-fetch, converter |
| 256 | |
| 257 | Delete the ACL entry referenced by the key *key* in the list of ACLs |
| 258 | referenced by *filename*. |
| 259 | |
| 260 | :param string filename: the filename that reference the ACL entries. |
| 261 | :param string key: the key which will be deleted. |
| 262 | |
| 263 | .. js:function:: core.del_map(filename, key) |
| 264 | |
| 265 | **context**: init, task, action, sample-fetch, converter |
| 266 | |
| 267 | Delete the map entry indexed with the specified key in the list of maps |
| 268 | referenced by his filename. |
| 269 | |
| 270 | :param string filename: the filename that reference the map entries. |
| 271 | :param string key: the key which will be deleted. |
| 272 | |
Thierry Fournier | b1f4656 | 2016-01-21 09:46:15 +0100 | [diff] [blame] | 273 | .. js:function:: core.now() |
| 274 | |
| 275 | **context**: body, init, task, action |
| 276 | |
| 277 | This function returns the current time. The time returned is fixed by the |
| 278 | HAProxy core and assures than the hour will be monotnic and that the system |
| 279 | call 'gettimeofday' will not be called too. The time is refreshed between each |
| 280 | Lua execution or resume, so two consecutive call to the function "now" will |
| 281 | probably returns the same result. |
| 282 | |
| 283 | :returns: an array which contains two entries "sec" and "usec". "sec" |
| 284 | contains the current at the epoch format, and "usec" contains the |
| 285 | current microseconds. |
| 286 | |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 287 | .. js:function:: core.msleep(milliseconds) |
| 288 | |
| 289 | **context**: body, init, task, action |
| 290 | |
| 291 | The `core.msleep()` stops the Lua execution between specified milliseconds. |
| 292 | |
| 293 | :param integer milliseconds: the required milliseconds. |
| 294 | |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 295 | .. js:function:: core.register_action(name, actions, func) |
| 296 | |
| 297 | **context**: body |
| 298 | |
David Carlier | 61fdf8b | 2015-10-02 11:59:38 +0100 | [diff] [blame] | 299 | Register a Lua function executed as action. All the registered action can be |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 300 | used in HAProxy with the prefix "lua.". An action gets a TXN object class as |
| 301 | input. |
| 302 | |
| 303 | :param string name: is the name of the converter. |
Willy Tarreau | 61add3c | 2015-09-28 15:39:10 +0200 | [diff] [blame] | 304 | :param table actions: is a table of string describing the HAProxy actions who |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 305 | want to register to. The expected actions are 'tcp-req', |
| 306 | 'tcp-res', 'http-req' or 'http-res'. |
| 307 | :param function func: is the Lua function called to work as converter. |
| 308 | |
| 309 | The prototype of the Lua function used as argument is: |
| 310 | |
| 311 | .. code-block:: lua |
| 312 | |
| 313 | function(txn) |
| 314 | .. |
| 315 | |
Thierry FOURNIER | dc59500 | 2015-12-21 11:13:52 +0100 | [diff] [blame] | 316 | * **txn** (:ref:`txn_class`): this is a TXN object used for manipulating the |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 317 | current request or TCP stream. |
| 318 | |
Willy Tarreau | 61add3c | 2015-09-28 15:39:10 +0200 | [diff] [blame] | 319 | Here, an exemple of action registration. the action juste send an 'Hello world' |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 320 | in the logs. |
| 321 | |
| 322 | .. code-block:: lua |
| 323 | |
| 324 | core.register_action("hello-world", { "tcp-req", "http-req" }, function(txn) |
| 325 | txn:Info("Hello world") |
| 326 | end) |
| 327 | .. |
| 328 | |
| 329 | This example code is used in HAproxy configuration like this: |
| 330 | |
| 331 | :: |
| 332 | |
| 333 | frontend tcp_frt |
| 334 | mode tcp |
| 335 | tcp-request content lua.hello-world |
| 336 | |
| 337 | frontend http_frt |
| 338 | mode http |
| 339 | http-request lua.hello-world |
| 340 | |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 341 | .. js:function:: core.register_converters(name, func) |
| 342 | |
| 343 | **context**: body |
| 344 | |
David Carlier | 61fdf8b | 2015-10-02 11:59:38 +0100 | [diff] [blame] | 345 | Register a Lua function executed as converter. All the registered converters |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 346 | can be used in HAProxy with the prefix "lua.". An converter get a string as |
| 347 | input and return a string as output. The registered function can take up to 9 |
| 348 | values as parameter. All the value are strings. |
| 349 | |
| 350 | :param string name: is the name of the converter. |
| 351 | :param function func: is the Lua function called to work as converter. |
| 352 | |
| 353 | The prototype of the Lua function used as argument is: |
| 354 | |
| 355 | .. code-block:: lua |
| 356 | |
| 357 | function(str, [p1 [, p2 [, ... [, p5]]]]) |
| 358 | .. |
| 359 | |
| 360 | * **str** (*string*): this is the input value automatically converted in |
| 361 | string. |
| 362 | * **p1** .. **p5** (*string*): this is a list of string arguments declared in |
| 363 | the haroxy configuration file. The number of arguments doesn't exceed 5. |
| 364 | The order and the nature of these is conventionally choose by the |
| 365 | developper. |
| 366 | |
| 367 | .. js:function:: core.register_fetches(name, func) |
| 368 | |
| 369 | **context**: body |
| 370 | |
David Carlier | 61fdf8b | 2015-10-02 11:59:38 +0100 | [diff] [blame] | 371 | Register a Lua function executed as sample fetch. All the registered sample |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 372 | fetchs can be used in HAProxy with the prefix "lua.". A Lua sample fetch |
| 373 | return a string as output. The registered function can take up to 9 values as |
| 374 | parameter. All the value are strings. |
| 375 | |
| 376 | :param string name: is the name of the converter. |
| 377 | :param function func: is the Lua function called to work as sample fetch. |
| 378 | |
| 379 | The prototype of the Lua function used as argument is: |
| 380 | |
| 381 | .. code-block:: lua |
| 382 | |
| 383 | string function(txn, [p1 [, p2 [, ... [, p5]]]]) |
| 384 | .. |
| 385 | |
Thierry FOURNIER | dc59500 | 2015-12-21 11:13:52 +0100 | [diff] [blame] | 386 | * **txn** (:ref:`txn_class`): this is the txn object associated with the current |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 387 | request. |
| 388 | * **p1** .. **p5** (*string*): this is a list of string arguments declared in |
| 389 | the haroxy configuration file. The number of arguments doesn't exceed 5. |
| 390 | The order and the nature of these is conventionally choose by the |
| 391 | developper. |
| 392 | * **Returns**: A string containing some data, ot nil if the value cannot be |
| 393 | returned now. |
| 394 | |
| 395 | lua example code: |
| 396 | |
| 397 | .. code-block:: lua |
| 398 | |
| 399 | core.register_fetches("hello", function(txn) |
| 400 | return "hello" |
| 401 | end) |
| 402 | .. |
| 403 | |
| 404 | HAProxy example configuration: |
| 405 | |
| 406 | :: |
| 407 | |
| 408 | frontend example |
| 409 | http-request redirect location /%[lua.hello] |
| 410 | |
Thierry FOURNIER | a3bc513 | 2015-09-25 21:43:56 +0200 | [diff] [blame] | 411 | .. js:function:: core.register_service(name, mode, func) |
| 412 | |
| 413 | **context**: body |
| 414 | |
David Carlier | 61fdf8b | 2015-10-02 11:59:38 +0100 | [diff] [blame] | 415 | Register a Lua function executed as a service. All the registered service can |
Thierry FOURNIER | a3bc513 | 2015-09-25 21:43:56 +0200 | [diff] [blame] | 416 | be used in HAProxy with the prefix "lua.". A service gets an object class as |
| 417 | input according with the required mode. |
| 418 | |
| 419 | :param string name: is the name of the converter. |
Willy Tarreau | 61add3c | 2015-09-28 15:39:10 +0200 | [diff] [blame] | 420 | :param string mode: is string describing the required mode. Only 'tcp' or |
Thierry FOURNIER | a3bc513 | 2015-09-25 21:43:56 +0200 | [diff] [blame] | 421 | 'http' are allowed. |
| 422 | :param function func: is the Lua function called to work as converter. |
| 423 | |
| 424 | The prototype of the Lua function used as argument is: |
| 425 | |
| 426 | .. code-block:: lua |
| 427 | |
| 428 | function(applet) |
| 429 | .. |
| 430 | |
Thierry FOURNIER | dc59500 | 2015-12-21 11:13:52 +0100 | [diff] [blame] | 431 | * **applet** *applet* will be a :ref:`applettcp_class` or a |
| 432 | :ref:`applethttp_class`. It depends the type of registered applet. An applet |
| 433 | registered with the 'http' value for the *mode* parameter will gets a |
| 434 | :ref:`applethttp_class`. If the *mode* value is 'tcp', the applet will gets |
| 435 | a :ref:`applettcp_class`. |
| 436 | |
| 437 | **warning**: Applets of type 'http' cannot be called from 'tcp-*' |
| 438 | rulesets. Only the 'http-*' rulesets are authorized, this means |
| 439 | that is not possible to call an HTTP applet from a proxy in tcp |
| 440 | mode. Applets of type 'tcp' can be called from anywhre. |
Thierry FOURNIER | a3bc513 | 2015-09-25 21:43:56 +0200 | [diff] [blame] | 441 | |
Willy Tarreau | 61add3c | 2015-09-28 15:39:10 +0200 | [diff] [blame] | 442 | Here, an exemple of service registration. the service just send an 'Hello world' |
Thierry FOURNIER | a3bc513 | 2015-09-25 21:43:56 +0200 | [diff] [blame] | 443 | as an http response. |
| 444 | |
| 445 | .. code-block:: lua |
| 446 | |
Pieter Baauw | 4d7f766 | 2015-11-08 16:38:08 +0100 | [diff] [blame] | 447 | core.register_service("hello-world", "http", function(applet) |
Thierry FOURNIER | a3bc513 | 2015-09-25 21:43:56 +0200 | [diff] [blame] | 448 | local response = "Hello World !" |
| 449 | applet:set_status(200) |
Pieter Baauw | 2dcb9bc | 2015-10-01 22:47:12 +0200 | [diff] [blame] | 450 | applet:add_header("content-length", string.len(response)) |
Thierry FOURNIER | a3bc513 | 2015-09-25 21:43:56 +0200 | [diff] [blame] | 451 | applet:add_header("content-type", "text/plain") |
Pieter Baauw | 2dcb9bc | 2015-10-01 22:47:12 +0200 | [diff] [blame] | 452 | applet:start_response() |
Thierry FOURNIER | a3bc513 | 2015-09-25 21:43:56 +0200 | [diff] [blame] | 453 | applet:send(response) |
| 454 | end) |
| 455 | .. |
| 456 | |
| 457 | This example code is used in HAproxy configuration like this: |
| 458 | |
| 459 | :: |
| 460 | |
| 461 | frontend example |
| 462 | http-request use-service lua.hello-world |
| 463 | |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 464 | .. js:function:: core.register_init(func) |
| 465 | |
| 466 | **context**: body |
| 467 | |
| 468 | Register a function executed after the configuration parsing. This is useful |
| 469 | to check any parameters. |
| 470 | |
Pieter Baauw | 4d7f766 | 2015-11-08 16:38:08 +0100 | [diff] [blame] | 471 | :param function func: is the Lua function called to work as initializer. |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 472 | |
| 473 | The prototype of the Lua function used as argument is: |
| 474 | |
| 475 | .. code-block:: lua |
| 476 | |
| 477 | function() |
| 478 | .. |
| 479 | |
| 480 | It takes no input, and no output is expected. |
| 481 | |
| 482 | .. js:function:: core.register_task(func) |
| 483 | |
| 484 | **context**: body, init, task, action, sample-fetch, converter |
| 485 | |
| 486 | Register and start independent task. The task is started when the HAProxy |
| 487 | main scheduler starts. For example this type of tasks can be executed to |
Thierry FOURNIER | 2e4893c | 2015-03-18 13:37:27 +0100 | [diff] [blame] | 488 | perform complex health checks. |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 489 | |
Pieter Baauw | 4d7f766 | 2015-11-08 16:38:08 +0100 | [diff] [blame] | 490 | :param function func: is the Lua function called to work as initializer. |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 491 | |
| 492 | The prototype of the Lua function used as argument is: |
| 493 | |
| 494 | .. code-block:: lua |
| 495 | |
| 496 | function() |
| 497 | .. |
| 498 | |
| 499 | It takes no input, and no output is expected. |
| 500 | |
| 501 | .. js:function:: core.set_nice(nice) |
| 502 | |
| 503 | **context**: task, action, sample-fetch, converter |
| 504 | |
| 505 | Change the nice of the current task or current session. |
Thierry FOURNIER | 2e4893c | 2015-03-18 13:37:27 +0100 | [diff] [blame] | 506 | |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 507 | :param integer nice: the nice value, it must be between -1024 and 1024. |
| 508 | |
| 509 | .. js:function:: core.set_map(filename, key, value) |
| 510 | |
| 511 | **context**: init, task, action, sample-fetch, converter |
| 512 | |
| 513 | set the value *value* associated to the key *key* in the map referenced by |
| 514 | *filename*. |
| 515 | |
Thierry FOURNIER | dc59500 | 2015-12-21 11:13:52 +0100 | [diff] [blame] | 516 | :param string filename: the Map reference |
| 517 | :param string key: the key to set or replace |
| 518 | :param string value: the associated value |
| 519 | |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 520 | .. js:function:: core.sleep(int seconds) |
| 521 | |
| 522 | **context**: body, init, task, action |
| 523 | |
| 524 | The `core.sleep()` functions stop the Lua execution between specified seconds. |
| 525 | |
| 526 | :param integer seconds: the required seconds. |
| 527 | |
| 528 | .. js:function:: core.tcp() |
| 529 | |
| 530 | **context**: init, task, action |
| 531 | |
| 532 | This function returns a new object of a *socket* class. |
| 533 | |
Thierry FOURNIER | dc59500 | 2015-12-21 11:13:52 +0100 | [diff] [blame] | 534 | :returns: A :ref:`socket_class` object. |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 535 | |
Thierry Fournier | 1de1659 | 2016-01-27 09:49:07 +0100 | [diff] [blame] | 536 | .. js:function:: core.concat() |
| 537 | |
| 538 | **context**: body, init, task, action, sample-fetch, converter |
| 539 | |
| 540 | This function retruns a new concat object. |
| 541 | |
| 542 | :returns: A :ref:`concat_class` object. |
| 543 | |
Thierry FOURNIER | 0a99b89 | 2015-08-26 00:14:17 +0200 | [diff] [blame] | 544 | .. js:function:: core.done(data) |
| 545 | |
| 546 | **context**: body, init, task, action, sample-fetch, converter |
| 547 | |
| 548 | :param any data: Return some data for the caller. It is useful with |
| 549 | sample-fetches and sample-converters. |
| 550 | |
| 551 | Immediately stops the current Lua execution and returns to the caller which |
| 552 | may be a sample fetch, a converter or an action and returns the specified |
| 553 | value (ignored for actions). It is used when the LUA process finishes its |
| 554 | work and wants to give back the control to HAProxy without executing the |
| 555 | remaining code. It can be seen as a multi-level "return". |
| 556 | |
Thierry FOURNIER | 486f5a0 | 2015-03-16 15:13:03 +0100 | [diff] [blame] | 557 | .. js:function:: core.yield() |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 558 | |
| 559 | **context**: task, action, sample-fetch, converter |
| 560 | |
| 561 | Give back the hand at the HAProxy scheduler. It is used when the LUA |
| 562 | processing consumes a lot of processing time. |
| 563 | |
Thierry Fournier | 1de1659 | 2016-01-27 09:49:07 +0100 | [diff] [blame] | 564 | .. _concat_class: |
| 565 | |
| 566 | Concat class |
| 567 | ============ |
| 568 | |
| 569 | .. js:class:: Concat |
| 570 | |
| 571 | This class provides a fast way for string concatenation. The way using native |
| 572 | Lua concatenation like the code below is slow for some reasons. |
| 573 | |
| 574 | .. code-block:: lua |
| 575 | |
| 576 | str = "string1" |
| 577 | str = str .. ", string2" |
| 578 | str = str .. ", string3" |
| 579 | .. |
| 580 | |
| 581 | For each concatenation, Lua: |
| 582 | * allocate memory for the result, |
| 583 | * catenate the two string copying the strings in the new memory bloc, |
| 584 | * free the old memory block containing the string whoch is no longer used. |
| 585 | This process does many memory move, allocation and free. In addition, the |
| 586 | memory is not really freed, it is just mark mark as unsused and wait for the |
| 587 | garbage collector. |
| 588 | |
| 589 | The Concat class provide an alternative way for catenating strings. It uses |
| 590 | the internal Lua mechanism (it does not allocate memory), but it doesn't copy |
| 591 | the data more than once. |
| 592 | |
| 593 | On my computer, the following loops spends 0.2s for the Concat method and |
| 594 | 18.5s for the pure Lua implementation. So, the Concat class is about 1000x |
| 595 | faster than the embedded solution. |
| 596 | |
| 597 | .. code-block:: lua |
| 598 | |
| 599 | for j = 1, 100 do |
| 600 | c = core.concat() |
| 601 | for i = 1, 20000 do |
| 602 | c:add("#####") |
| 603 | end |
| 604 | end |
| 605 | .. |
| 606 | |
| 607 | .. code-block:: lua |
| 608 | |
| 609 | for j = 1, 100 do |
| 610 | c = "" |
| 611 | for i = 1, 20000 do |
| 612 | c = c .. "#####" |
| 613 | end |
| 614 | end |
| 615 | .. |
| 616 | |
| 617 | .. js:function:: Concat.add(concat, string) |
| 618 | |
| 619 | This function adds a string to the current concatenated string. |
| 620 | |
| 621 | :param class_concat concat: A :ref:`concat_class` which contains the currently |
| 622 | builded string. |
| 623 | :param string string: A new string to concatenate to the current builded |
| 624 | string. |
| 625 | |
| 626 | .. js:function:: Concat.dump(concat) |
| 627 | |
| 628 | This function returns the concanated string. |
| 629 | |
| 630 | :param class_concat concat: A :ref:`concat_class` which contains the currently |
| 631 | builded string. |
| 632 | :returns: the concatenated string |
| 633 | |
Thierry FOURNIER | dc59500 | 2015-12-21 11:13:52 +0100 | [diff] [blame] | 634 | .. _fetches_class: |
| 635 | |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 636 | Fetches class |
| 637 | ============= |
| 638 | |
| 639 | .. js:class:: Fetches |
| 640 | |
| 641 | This class contains a lot of internal HAProxy sample fetches. See the |
Thierry FOURNIER | 2e4893c | 2015-03-18 13:37:27 +0100 | [diff] [blame] | 642 | HAProxy "configuration.txt" documentation for more information about her |
| 643 | usage. they are the chapters 7.3.2 to 7.3.6. |
| 644 | |
Thierry FOURNIER | dc59500 | 2015-12-21 11:13:52 +0100 | [diff] [blame] | 645 | **warning** some sample fetches are not available in some context. These |
| 646 | limitations are specified in this documentation when theire useful. |
| 647 | |
Thierry FOURNIER | 2e4893c | 2015-03-18 13:37:27 +0100 | [diff] [blame] | 648 | :see: TXN.f |
| 649 | :see: TXN.sf |
| 650 | |
| 651 | Fetches are useful for: |
| 652 | |
| 653 | * get system time, |
| 654 | * get environment variable, |
| 655 | * get random numbers, |
| 656 | * known backend status like the number of users in queue or the number of |
| 657 | connections established, |
| 658 | * client information like ip source or destination, |
| 659 | * deal with stick tables, |
| 660 | * Established SSL informations, |
| 661 | * HTTP information like headers or method. |
| 662 | |
| 663 | .. code-block:: lua |
| 664 | |
Thierry FOURNIER | dc59500 | 2015-12-21 11:13:52 +0100 | [diff] [blame] | 665 | function action(txn) |
| 666 | -- Get source IP |
| 667 | local clientip = txn.f:src() |
| 668 | end |
Thierry FOURNIER | 2e4893c | 2015-03-18 13:37:27 +0100 | [diff] [blame] | 669 | .. |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 670 | |
Thierry FOURNIER | dc59500 | 2015-12-21 11:13:52 +0100 | [diff] [blame] | 671 | .. _converters_class: |
| 672 | |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 673 | Converters class |
| 674 | ================ |
| 675 | |
| 676 | .. js:class:: Converters |
| 677 | |
| 678 | This class contains a lot of internal HAProxy sample converters. See the |
Thierry FOURNIER | 2e4893c | 2015-03-18 13:37:27 +0100 | [diff] [blame] | 679 | HAProxy documentation "configuration.txt" for more information about her |
| 680 | usage. Its the chapter 7.3.1. |
| 681 | |
| 682 | :see: TXN.c |
| 683 | :see: TXN.sc |
| 684 | |
| 685 | Converters provides statefull transformation. They are useful for: |
| 686 | |
| 687 | * converting input to base64, |
| 688 | * applying hash on input string (djb2, crc32, sdbm, wt6), |
| 689 | * format date, |
| 690 | * json escape, |
Pieter Baauw | 4d7f766 | 2015-11-08 16:38:08 +0100 | [diff] [blame] | 691 | * extracting preferred language comparing two lists, |
Thierry FOURNIER | 2e4893c | 2015-03-18 13:37:27 +0100 | [diff] [blame] | 692 | * turn to lower or upper chars, |
| 693 | * deal with stick tables. |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 694 | |
Thierry FOURNIER | dc59500 | 2015-12-21 11:13:52 +0100 | [diff] [blame] | 695 | .. _channel_class: |
| 696 | |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 697 | Channel class |
| 698 | ============= |
| 699 | |
| 700 | .. js:class:: Channel |
| 701 | |
| 702 | HAProxy uses two buffers for the processing of the requests. The first one is |
| 703 | used with the request data (from the client to the server) and the second is |
| 704 | used for the response data (from the server to the client). |
| 705 | |
| 706 | Each buffer contains two types of data. The first type is the incoming data |
| 707 | waiting for a processing. The second part is the outgoing data already |
| 708 | processed. Usually, the incoming data is processed, after it is tagged as |
| 709 | outgoing data, and finally it is sent. The following functions provides tools |
| 710 | for manipulating these data in a buffer. |
| 711 | |
| 712 | The following diagram shows where the channel class function are applied. |
| 713 | |
| 714 | **Warning**: It is not possible to read from the response in request action, |
| 715 | and it is not possible to read for the request channel in response action. |
| 716 | |
Thierry FOURNIER | 2e4893c | 2015-03-18 13:37:27 +0100 | [diff] [blame] | 717 | .. image:: _static/channel.png |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 718 | |
Thierry FOURNIER | 486f5a0 | 2015-03-16 15:13:03 +0100 | [diff] [blame] | 719 | .. js:function:: Channel.dup(channel) |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 720 | |
| 721 | This function returns a string that contain the entire buffer. The data is |
| 722 | not remove from the buffer and can be reprocessed later. |
| 723 | |
| 724 | If the buffer cant receive more data, a 'nil' value is returned. |
| 725 | |
Thierry FOURNIER | 486f5a0 | 2015-03-16 15:13:03 +0100 | [diff] [blame] | 726 | :param class_channel channel: The manipulated Channel. |
Pieter Baauw | 4d7f766 | 2015-11-08 16:38:08 +0100 | [diff] [blame] | 727 | :returns: a string containing all the available data or nil. |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 728 | |
Thierry FOURNIER | 486f5a0 | 2015-03-16 15:13:03 +0100 | [diff] [blame] | 729 | .. js:function:: Channel.get(channel) |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 730 | |
| 731 | This function returns a string that contain the entire buffer. The data is |
| 732 | consumed from the buffer. |
| 733 | |
| 734 | If the buffer cant receive more data, a 'nil' value is returned. |
| 735 | |
Thierry FOURNIER | 486f5a0 | 2015-03-16 15:13:03 +0100 | [diff] [blame] | 736 | :param class_channel channel: The manipulated Channel. |
Pieter Baauw | 4d7f766 | 2015-11-08 16:38:08 +0100 | [diff] [blame] | 737 | :returns: a string containing all the available data or nil. |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 738 | |
Thierry FOURNIER | a3bc513 | 2015-09-25 21:43:56 +0200 | [diff] [blame] | 739 | .. js:function:: Channel.getline(channel) |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 740 | |
| 741 | This function returns a string that contain the first line of the buffer. The |
| 742 | data is consumed. If the data returned doesn't contains a final '\n' its |
| 743 | assumed than its the last available data in the buffer. |
| 744 | |
| 745 | If the buffer cant receive more data, a 'nil' value is returned. |
| 746 | |
Thierry FOURNIER | 486f5a0 | 2015-03-16 15:13:03 +0100 | [diff] [blame] | 747 | :param class_channel channel: The manipulated Channel. |
Pieter Baauw | 386a127 | 2015-08-16 15:26:24 +0200 | [diff] [blame] | 748 | :returns: a string containing the available line or nil. |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 749 | |
Thierry FOURNIER | 486f5a0 | 2015-03-16 15:13:03 +0100 | [diff] [blame] | 750 | .. js:function:: Channel.set(channel, string) |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 751 | |
| 752 | This function replace the content of the buffer by the string. The function |
| 753 | returns the copied length, otherwise, it returns -1. |
| 754 | |
| 755 | The data set with this function are not send. They wait for the end of |
| 756 | HAProxy processing, so the buffer can be full. |
| 757 | |
Thierry FOURNIER | 486f5a0 | 2015-03-16 15:13:03 +0100 | [diff] [blame] | 758 | :param class_channel channel: The manipulated Channel. |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 759 | :param string string: The data which will sent. |
Pieter Baauw | 4d7f766 | 2015-11-08 16:38:08 +0100 | [diff] [blame] | 760 | :returns: an integer containing the amount of bytes copied or -1. |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 761 | |
Thierry FOURNIER | 486f5a0 | 2015-03-16 15:13:03 +0100 | [diff] [blame] | 762 | .. js:function:: Channel.append(channel, string) |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 763 | |
| 764 | This function append the string argument to the content of the buffer. The |
| 765 | function returns the copied length, otherwise, it returns -1. |
| 766 | |
| 767 | The data set with this function are not send. They wait for the end of |
| 768 | HAProxy processing, so the buffer can be full. |
| 769 | |
Thierry FOURNIER | 486f5a0 | 2015-03-16 15:13:03 +0100 | [diff] [blame] | 770 | :param class_channel channel: The manipulated Channel. |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 771 | :param string string: The data which will sent. |
Pieter Baauw | 4d7f766 | 2015-11-08 16:38:08 +0100 | [diff] [blame] | 772 | :returns: an integer containing the amount of bytes copied or -1. |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 773 | |
Thierry FOURNIER | 486f5a0 | 2015-03-16 15:13:03 +0100 | [diff] [blame] | 774 | .. js:function:: Channel.send(channel, string) |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 775 | |
| 776 | This function required immediate send of the data. Unless if the connection |
| 777 | is close, the buffer is regularly flushed and all the string can be sent. |
| 778 | |
Thierry FOURNIER | 486f5a0 | 2015-03-16 15:13:03 +0100 | [diff] [blame] | 779 | :param class_channel channel: The manipulated Channel. |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 780 | :param string string: The data which will sent. |
Pieter Baauw | 4d7f766 | 2015-11-08 16:38:08 +0100 | [diff] [blame] | 781 | :returns: an integer containing the amount of bytes copied or -1. |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 782 | |
Thierry FOURNIER | 486f5a0 | 2015-03-16 15:13:03 +0100 | [diff] [blame] | 783 | .. js:function:: Channel.get_in_length(channel) |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 784 | |
| 785 | This function returns the length of the input part of the buffer. |
| 786 | |
Thierry FOURNIER | 486f5a0 | 2015-03-16 15:13:03 +0100 | [diff] [blame] | 787 | :param class_channel channel: The manipulated Channel. |
Pieter Baauw | 4d7f766 | 2015-11-08 16:38:08 +0100 | [diff] [blame] | 788 | :returns: an integer containing the amount of available bytes. |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 789 | |
Thierry FOURNIER | 486f5a0 | 2015-03-16 15:13:03 +0100 | [diff] [blame] | 790 | .. js:function:: Channel.get_out_length(channel) |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 791 | |
| 792 | This function returns the length of the output part of the buffer. |
| 793 | |
Thierry FOURNIER | 486f5a0 | 2015-03-16 15:13:03 +0100 | [diff] [blame] | 794 | :param class_channel channel: The manipulated Channel. |
Pieter Baauw | 4d7f766 | 2015-11-08 16:38:08 +0100 | [diff] [blame] | 795 | :returns: an integer containing the amount of available bytes. |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 796 | |
Thierry FOURNIER | 486f5a0 | 2015-03-16 15:13:03 +0100 | [diff] [blame] | 797 | .. js:function:: Channel.forward(channel, int) |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 798 | |
| 799 | This function transfer bytes from the input part of the buffer to the output |
| 800 | part. |
| 801 | |
Thierry FOURNIER | 486f5a0 | 2015-03-16 15:13:03 +0100 | [diff] [blame] | 802 | :param class_channel channel: The manipulated Channel. |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 803 | :param integer int: The amount of data which will be forwarded. |
| 804 | |
Thierry FOURNIER | dc59500 | 2015-12-21 11:13:52 +0100 | [diff] [blame] | 805 | |
| 806 | .. _http_class: |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 807 | |
| 808 | HTTP class |
| 809 | ========== |
| 810 | |
| 811 | .. js:class:: HTTP |
| 812 | |
| 813 | This class contain all the HTTP manipulation functions. |
| 814 | |
Pieter Baauw | 386a127 | 2015-08-16 15:26:24 +0200 | [diff] [blame] | 815 | .. js:function:: HTTP.req_get_headers(http) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 816 | |
| 817 | Returns an array containing all the request headers. |
| 818 | |
| 819 | :param class_http http: The related http object. |
| 820 | :returns: array of headers. |
Pieter Baauw | 386a127 | 2015-08-16 15:26:24 +0200 | [diff] [blame] | 821 | :see: HTTP.res_get_headers() |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 822 | |
Thierry FOURNIER | dc59500 | 2015-12-21 11:13:52 +0100 | [diff] [blame] | 823 | This is the form of the returned array: |
| 824 | |
| 825 | .. code-block:: lua |
| 826 | |
| 827 | HTTP:req_get_headers()['<header-name>'][<header-index>] = "<header-value>" |
| 828 | |
| 829 | local hdr = HTTP:req_get_headers() |
| 830 | hdr["host"][0] = "www.test.com" |
| 831 | hdr["accept"][0] = "audio/basic q=1" |
| 832 | hdr["accept"][1] = "audio/*, q=0.2" |
| 833 | hdr["accept"][2] = "*/*, q=0.1" |
| 834 | .. |
| 835 | |
Pieter Baauw | 386a127 | 2015-08-16 15:26:24 +0200 | [diff] [blame] | 836 | .. js:function:: HTTP.res_get_headers(http) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 837 | |
| 838 | Returns an array containing all the response headers. |
| 839 | |
| 840 | :param class_http http: The related http object. |
| 841 | :returns: array of headers. |
Pieter Baauw | 386a127 | 2015-08-16 15:26:24 +0200 | [diff] [blame] | 842 | :see: HTTP.req_get_headers() |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 843 | |
Thierry FOURNIER | dc59500 | 2015-12-21 11:13:52 +0100 | [diff] [blame] | 844 | This is the form of the returned array: |
| 845 | |
| 846 | .. code-block:: lua |
| 847 | |
| 848 | HTTP:res_get_headers()['<header-name>'][<header-index>] = "<header-value>" |
| 849 | |
| 850 | local hdr = HTTP:req_get_headers() |
| 851 | hdr["host"][0] = "www.test.com" |
| 852 | hdr["accept"][0] = "audio/basic q=1" |
| 853 | hdr["accept"][1] = "audio/*, q=0.2" |
| 854 | hdr["accept"][2] = "*.*, q=0.1" |
| 855 | .. |
| 856 | |
Thierry FOURNIER | 486f5a0 | 2015-03-16 15:13:03 +0100 | [diff] [blame] | 857 | .. js:function:: HTTP.req_add_header(http, name, value) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 858 | |
| 859 | Appends an HTTP header field in the request whose name is |
| 860 | specified in "name" and whose value is defined in "value". |
| 861 | |
| 862 | :param class_http http: The related http object. |
| 863 | :param string name: The header name. |
| 864 | :param string value: The header value. |
Thierry FOURNIER | 486f5a0 | 2015-03-16 15:13:03 +0100 | [diff] [blame] | 865 | :see: HTTP.res_add_header() |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 866 | |
Thierry FOURNIER | 486f5a0 | 2015-03-16 15:13:03 +0100 | [diff] [blame] | 867 | .. js:function:: HTTP.res_add_header(http, name, value) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 868 | |
| 869 | appends an HTTP header field in the response whose name is |
| 870 | specified in "name" and whose value is defined in "value". |
| 871 | |
| 872 | :param class_http http: The related http object. |
| 873 | :param string name: The header name. |
| 874 | :param string value: The header value. |
Thierry FOURNIER | 486f5a0 | 2015-03-16 15:13:03 +0100 | [diff] [blame] | 875 | :see: HTTP.req_add_header() |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 876 | |
Thierry FOURNIER | 486f5a0 | 2015-03-16 15:13:03 +0100 | [diff] [blame] | 877 | .. js:function:: HTTP.req_del_header(http, name) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 878 | |
| 879 | Removes all HTTP header fields in the request whose name is |
| 880 | specified in "name". |
| 881 | |
| 882 | :param class_http http: The related http object. |
| 883 | :param string name: The header name. |
Thierry FOURNIER | 486f5a0 | 2015-03-16 15:13:03 +0100 | [diff] [blame] | 884 | :see: HTTP.res_del_header() |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 885 | |
Thierry FOURNIER | 486f5a0 | 2015-03-16 15:13:03 +0100 | [diff] [blame] | 886 | .. js:function:: HTTP.res_del_header(http, name) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 887 | |
| 888 | Removes all HTTP header fields in the response whose name is |
| 889 | specified in "name". |
| 890 | |
| 891 | :param class_http http: The related http object. |
| 892 | :param string name: The header name. |
Thierry FOURNIER | 486f5a0 | 2015-03-16 15:13:03 +0100 | [diff] [blame] | 893 | :see: HTTP.req_del_header() |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 894 | |
Thierry FOURNIER | 486f5a0 | 2015-03-16 15:13:03 +0100 | [diff] [blame] | 895 | .. js:function:: HTTP.req_set_header(http, name, value) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 896 | |
| 897 | This variable replace all occurence of all header "name", by only |
| 898 | one containing the "value". |
| 899 | |
| 900 | :param class_http http: The related http object. |
| 901 | :param string name: The header name. |
| 902 | :param string value: The header value. |
Thierry FOURNIER | 486f5a0 | 2015-03-16 15:13:03 +0100 | [diff] [blame] | 903 | :see: HTTP.res_set_header() |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 904 | |
| 905 | This function does the same work as the folowwing code: |
| 906 | |
| 907 | .. code-block:: lua |
| 908 | |
| 909 | function fcn(txn) |
Thierry FOURNIER | 486f5a0 | 2015-03-16 15:13:03 +0100 | [diff] [blame] | 910 | TXN.http:req_del_header("header") |
| 911 | TXN.http:req_add_header("header", "value") |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 912 | end |
| 913 | .. |
| 914 | |
Thierry FOURNIER | 486f5a0 | 2015-03-16 15:13:03 +0100 | [diff] [blame] | 915 | .. js:function:: HTTP.res_set_header(http, name, value) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 916 | |
| 917 | This variable replace all occurence of all header "name", by only |
| 918 | one containing the "value". |
| 919 | |
| 920 | :param class_http http: The related http object. |
| 921 | :param string name: The header name. |
| 922 | :param string value: The header value. |
Pieter Baauw | 386a127 | 2015-08-16 15:26:24 +0200 | [diff] [blame] | 923 | :see: HTTP.req_rep_header() |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 924 | |
Pieter Baauw | 386a127 | 2015-08-16 15:26:24 +0200 | [diff] [blame] | 925 | .. js:function:: HTTP.req_rep_header(http, name, regex, replace) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 926 | |
| 927 | Matches the regular expression in all occurrences of header field "name" |
| 928 | according to "regex", and replaces them with the "replace" argument. The |
| 929 | replacement value can contain back references like \1, \2, ... This |
| 930 | function works with the request. |
| 931 | |
| 932 | :param class_http http: The related http object. |
| 933 | :param string name: The header name. |
| 934 | :param string regex: The match regular expression. |
| 935 | :param string replace: The replacement value. |
Pieter Baauw | 386a127 | 2015-08-16 15:26:24 +0200 | [diff] [blame] | 936 | :see: HTTP.res_rep_header() |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 937 | |
Pieter Baauw | 386a127 | 2015-08-16 15:26:24 +0200 | [diff] [blame] | 938 | .. js:function:: HTTP.res_rep_header(http, name, regex, string) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 939 | |
| 940 | Matches the regular expression in all occurrences of header field "name" |
| 941 | according to "regex", and replaces them with the "replace" argument. The |
| 942 | replacement value can contain back references like \1, \2, ... This |
| 943 | function works with the request. |
| 944 | |
| 945 | :param class_http http: The related http object. |
| 946 | :param string name: The header name. |
| 947 | :param string regex: The match regular expression. |
| 948 | :param string replace: The replacement value. |
Thierry FOURNIER | 486f5a0 | 2015-03-16 15:13:03 +0100 | [diff] [blame] | 949 | :see: HTTP.req_replace_header() |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 950 | |
Thierry FOURNIER | 486f5a0 | 2015-03-16 15:13:03 +0100 | [diff] [blame] | 951 | .. js:function:: HTTP.req_replace_value(http, name, regex, replace) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 952 | |
Thierry FOURNIER | 486f5a0 | 2015-03-16 15:13:03 +0100 | [diff] [blame] | 953 | Works like "HTTP.req_replace_header()" except that it matches the regex |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 954 | against every comma-delimited value of the header field "name" instead of the |
| 955 | entire header. |
| 956 | |
| 957 | :param class_http http: The related http object. |
| 958 | :param string name: The header name. |
| 959 | :param string regex: The match regular expression. |
| 960 | :param string replace: The replacement value. |
Thierry FOURNIER | 486f5a0 | 2015-03-16 15:13:03 +0100 | [diff] [blame] | 961 | :see: HTTP.req_replace_header() |
| 962 | :see: HTTP.res_replace_value() |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 963 | |
Thierry FOURNIER | 486f5a0 | 2015-03-16 15:13:03 +0100 | [diff] [blame] | 964 | .. js:function:: HTTP.res_replace_value(http, name, regex, replace) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 965 | |
Thierry FOURNIER | 486f5a0 | 2015-03-16 15:13:03 +0100 | [diff] [blame] | 966 | Works like "HTTP.res_replace_header()" except that it matches the regex |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 967 | against every comma-delimited value of the header field "name" instead of the |
| 968 | entire header. |
| 969 | |
| 970 | :param class_http http: The related http object. |
| 971 | :param string name: The header name. |
| 972 | :param string regex: The match regular expression. |
| 973 | :param string replace: The replacement value. |
Thierry FOURNIER | 486f5a0 | 2015-03-16 15:13:03 +0100 | [diff] [blame] | 974 | :see: HTTP.res_replace_header() |
| 975 | :see: HTTP.req_replace_value() |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 976 | |
Thierry FOURNIER | 486f5a0 | 2015-03-16 15:13:03 +0100 | [diff] [blame] | 977 | .. js:function:: HTTP.req_set_method(http, method) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 978 | |
| 979 | Rewrites the request method with the parameter "method". |
| 980 | |
| 981 | :param class_http http: The related http object. |
| 982 | :param string method: The new method. |
| 983 | |
Thierry FOURNIER | 486f5a0 | 2015-03-16 15:13:03 +0100 | [diff] [blame] | 984 | .. js:function:: HTTP.req_set_path(http, path) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 985 | |
| 986 | Rewrites the request path with the "path" parameter. |
| 987 | |
| 988 | :param class_http http: The related http object. |
| 989 | :param string path: The new path. |
| 990 | |
Thierry FOURNIER | 486f5a0 | 2015-03-16 15:13:03 +0100 | [diff] [blame] | 991 | .. js:function:: HTTP.req_set_query(http, query) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 992 | |
| 993 | Rewrites the request's query string which appears after the first question |
| 994 | mark ("?") with the parameter "query". |
| 995 | |
| 996 | :param class_http http: The related http object. |
| 997 | :param string query: The new query. |
| 998 | |
Thierry FOURNIER | 0d79cf6 | 2015-08-26 14:20:58 +0200 | [diff] [blame] | 999 | .. js:function:: HTTP.req_set_uri(http, uri) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 1000 | |
| 1001 | Rewrites the request URI with the parameter "uri". |
| 1002 | |
| 1003 | :param class_http http: The related http object. |
| 1004 | :param string uri: The new uri. |
| 1005 | |
Thierry FOURNIER | 35d70ef | 2015-08-26 16:21:56 +0200 | [diff] [blame] | 1006 | .. js:function:: HTTP.res_set_status(http, status) |
| 1007 | |
| 1008 | Rewrites the response status code with the parameter "code". Note that the |
| 1009 | reason is automatically adapted to the new code. |
| 1010 | |
| 1011 | :param class_http http: The related http object. |
| 1012 | :param integer status: The new response status code. |
| 1013 | |
Thierry FOURNIER | dc59500 | 2015-12-21 11:13:52 +0100 | [diff] [blame] | 1014 | .. _txn_class: |
| 1015 | |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 1016 | TXN class |
| 1017 | ========= |
| 1018 | |
| 1019 | .. js:class:: TXN |
| 1020 | |
| 1021 | The txn class contain all the functions relative to the http or tcp |
| 1022 | transaction (Note than a tcp stream is the same than a tcp transaction, but |
| 1023 | an HTTP transaction is not the same than a tcp stream). |
| 1024 | |
| 1025 | The usage of this class permits to retrieve data from the requests, alter it |
| 1026 | and forward it. |
| 1027 | |
| 1028 | All the functions provided by this class are available in the context |
| 1029 | **sample-fetches** and **actions**. |
| 1030 | |
Thierry FOURNIER | 486f5a0 | 2015-03-16 15:13:03 +0100 | [diff] [blame] | 1031 | .. js:attribute:: TXN.c |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 1032 | |
Thierry FOURNIER | dc59500 | 2015-12-21 11:13:52 +0100 | [diff] [blame] | 1033 | :returns: An :ref:`converters_class`. |
| 1034 | |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 1035 | This attribute contains a Converters class object. |
| 1036 | |
Thierry FOURNIER | 486f5a0 | 2015-03-16 15:13:03 +0100 | [diff] [blame] | 1037 | .. js:attribute:: TXN.sc |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 1038 | |
Thierry FOURNIER | dc59500 | 2015-12-21 11:13:52 +0100 | [diff] [blame] | 1039 | :returns: An :ref:`converters_class`. |
| 1040 | |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 1041 | This attribute contains a Converters class object. The functions of |
| 1042 | this object returns always a string. |
| 1043 | |
Thierry FOURNIER | 486f5a0 | 2015-03-16 15:13:03 +0100 | [diff] [blame] | 1044 | .. js:attribute:: TXN.f |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 1045 | |
Thierry FOURNIER | dc59500 | 2015-12-21 11:13:52 +0100 | [diff] [blame] | 1046 | :returns: An :ref:`fetches_class`. |
| 1047 | |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 1048 | This attribute contains a Fetches class object. |
| 1049 | |
Thierry FOURNIER | 486f5a0 | 2015-03-16 15:13:03 +0100 | [diff] [blame] | 1050 | .. js:attribute:: TXN.sf |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 1051 | |
Thierry FOURNIER | dc59500 | 2015-12-21 11:13:52 +0100 | [diff] [blame] | 1052 | :returns: An :ref:`fetches_class`. |
| 1053 | |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 1054 | This attribute contains a Fetches class object. The functions of |
| 1055 | this object returns always a string. |
| 1056 | |
Thierry FOURNIER | 486f5a0 | 2015-03-16 15:13:03 +0100 | [diff] [blame] | 1057 | .. js:attribute:: TXN.req |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 1058 | |
Thierry FOURNIER | dc59500 | 2015-12-21 11:13:52 +0100 | [diff] [blame] | 1059 | :returns: An :ref:`channel_class`. |
| 1060 | |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 1061 | This attribute contains a channel class object for the request buffer. |
| 1062 | |
Thierry FOURNIER | 486f5a0 | 2015-03-16 15:13:03 +0100 | [diff] [blame] | 1063 | .. js:attribute:: TXN.res |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 1064 | |
Thierry FOURNIER | dc59500 | 2015-12-21 11:13:52 +0100 | [diff] [blame] | 1065 | :returns: An :ref:`channel_class`. |
| 1066 | |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 1067 | This attribute contains a channel class object for the response buffer. |
| 1068 | |
Thierry FOURNIER | 486f5a0 | 2015-03-16 15:13:03 +0100 | [diff] [blame] | 1069 | .. js:attribute:: TXN.http |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 1070 | |
Thierry FOURNIER | dc59500 | 2015-12-21 11:13:52 +0100 | [diff] [blame] | 1071 | :returns: An :ref:`http_class`. |
| 1072 | |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 1073 | This attribute contains an HTTP class object. It is avalaible only if the |
| 1074 | proxy has the "mode http" enabled. |
| 1075 | |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 1076 | .. js:function:: TXN.log(TXN, loglevel, msg) |
| 1077 | |
| 1078 | This function sends a log. The log is sent, according with the HAProxy |
| 1079 | configuration file, on the default syslog server if it is configured and on |
| 1080 | the stderr if it is allowed. |
| 1081 | |
| 1082 | :param class_txn txn: The class txn object containing the data. |
| 1083 | :param integer loglevel: Is the log level asociated with the message. It is a |
| 1084 | number between 0 and 7. |
| 1085 | :param string msg: The log content. |
| 1086 | :see: core.emerg, core.alert, core.crit, core.err, core.warning, core.notice, |
| 1087 | core.info, core.debug (log level definitions) |
| 1088 | :see: TXN.deflog |
| 1089 | :see: TXN.Debug |
| 1090 | :see: TXN.Info |
| 1091 | :see: TXN.Warning |
| 1092 | :see: TXN.Alert |
| 1093 | |
| 1094 | .. js:function:: TXN.deflog(TXN, msg) |
| 1095 | |
| 1096 | Sends a log line with the default loglevel for the proxy ssociated with the |
| 1097 | transaction. |
| 1098 | |
| 1099 | :param class_txn txn: The class txn object containing the data. |
| 1100 | :param string msg: The log content. |
| 1101 | :see: TXN.log |
| 1102 | |
| 1103 | .. js:function:: TXN.Debug(txn, msg) |
| 1104 | |
| 1105 | :param class_txn txn: The class txn object containing the data. |
| 1106 | :param string msg: The log content. |
| 1107 | :see: TXN.log |
| 1108 | |
| 1109 | Does the same job than: |
| 1110 | |
| 1111 | .. code-block:: lua |
| 1112 | |
Thierry FOURNIER | dc59500 | 2015-12-21 11:13:52 +0100 | [diff] [blame] | 1113 | function Debug(txn, msg) |
| 1114 | TXN.log(txn, core.debug, msg) |
| 1115 | end |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 1116 | .. |
| 1117 | |
| 1118 | .. js:function:: TXN.Info(txn, msg) |
| 1119 | |
| 1120 | :param class_txn txn: The class txn object containing the data. |
| 1121 | :param string msg: The log content. |
| 1122 | :see: TXN.log |
| 1123 | |
| 1124 | .. code-block:: lua |
| 1125 | |
Thierry FOURNIER | dc59500 | 2015-12-21 11:13:52 +0100 | [diff] [blame] | 1126 | function Debug(txn, msg) |
| 1127 | TXN.log(txn, core.info, msg) |
| 1128 | end |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 1129 | .. |
| 1130 | |
| 1131 | .. js:function:: TXN.Warning(txn, msg) |
| 1132 | |
| 1133 | :param class_txn txn: The class txn object containing the data. |
| 1134 | :param string msg: The log content. |
| 1135 | :see: TXN.log |
| 1136 | |
| 1137 | .. code-block:: lua |
| 1138 | |
Thierry FOURNIER | dc59500 | 2015-12-21 11:13:52 +0100 | [diff] [blame] | 1139 | function Debug(txn, msg) |
| 1140 | TXN.log(txn, core.warning, msg) |
| 1141 | end |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 1142 | .. |
| 1143 | |
| 1144 | .. js:function:: TXN.Alert(txn, msg) |
| 1145 | |
| 1146 | :param class_txn txn: The class txn object containing the data. |
| 1147 | :param string msg: The log content. |
| 1148 | :see: TXN.log |
| 1149 | |
| 1150 | .. code-block:: lua |
| 1151 | |
Thierry FOURNIER | dc59500 | 2015-12-21 11:13:52 +0100 | [diff] [blame] | 1152 | function Debug(txn, msg) |
| 1153 | TXN.log(txn, core.alert, msg) |
| 1154 | end |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 1155 | .. |
| 1156 | |
Thierry FOURNIER | 486f5a0 | 2015-03-16 15:13:03 +0100 | [diff] [blame] | 1157 | .. js:function:: TXN.get_priv(txn) |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 1158 | |
Thierry FOURNIER | 486f5a0 | 2015-03-16 15:13:03 +0100 | [diff] [blame] | 1159 | Return Lua data stored in the current transaction (with the `TXN.set_priv()`) |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 1160 | function. If no data are stored, it returns a nil value. |
| 1161 | |
| 1162 | :param class_txn txn: The class txn object containing the data. |
| 1163 | :returns: the opaque data previsously stored, or nil if nothing is |
| 1164 | avalaible. |
| 1165 | |
Thierry FOURNIER | 486f5a0 | 2015-03-16 15:13:03 +0100 | [diff] [blame] | 1166 | .. js:function:: TXN.set_priv(txn, data) |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 1167 | |
| 1168 | Store any data in the current HAProxy transaction. This action replace the |
| 1169 | old stored data. |
| 1170 | |
| 1171 | :param class_txn txn: The class txn object containing the data. |
| 1172 | :param opaque data: The data which is stored in the transaction. |
| 1173 | |
Thierry FOURNIER | 053ba8ad | 2015-06-08 13:05:33 +0200 | [diff] [blame] | 1174 | .. js:function:: TXN.set_var(TXN, var, value) |
| 1175 | |
David Carlier | 61fdf8b | 2015-10-02 11:59:38 +0100 | [diff] [blame] | 1176 | Converts a Lua type in a HAProxy type and store it in a variable <var>. |
Thierry FOURNIER | 053ba8ad | 2015-06-08 13:05:33 +0200 | [diff] [blame] | 1177 | |
| 1178 | :param class_txn txn: The class txn object containing the data. |
| 1179 | :param string var: The variable name according with the HAProxy variable syntax. |
| 1180 | :param opaque value: The data which is stored in the variable. |
| 1181 | |
| 1182 | .. js:function:: TXN.get_var(TXN, var) |
| 1183 | |
| 1184 | Returns data stored in the variable <var> converter in Lua type. |
| 1185 | |
| 1186 | :param class_txn txn: The class txn object containing the data. |
| 1187 | :param string var: The variable name according with the HAProxy variable syntax. |
| 1188 | |
Willy Tarreau | bc183a6 | 2015-08-28 10:39:11 +0200 | [diff] [blame] | 1189 | .. js:function:: TXN.done(txn) |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 1190 | |
Willy Tarreau | bc183a6 | 2015-08-28 10:39:11 +0200 | [diff] [blame] | 1191 | This function terminates processing of the transaction and the associated |
| 1192 | session. It can be used when a critical error is detected or to terminate |
| 1193 | processing after some data have been returned to the client (eg: a redirect). |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 1194 | |
| 1195 | :param class_txn txn: The class txn object containing the data. |
| 1196 | |
Thierry FOURNIER | 486f5a0 | 2015-03-16 15:13:03 +0100 | [diff] [blame] | 1197 | .. js:function:: TXN.set_loglevel(txn, loglevel) |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 1198 | |
| 1199 | Is used to change the log level of the current request. The "loglevel" must |
| 1200 | be an integer between 0 and 7. |
| 1201 | |
| 1202 | :param class_txn txn: The class txn object containing the data. |
| 1203 | :param integer loglevel: The required log level. This variable can be one of |
| 1204 | :see: core.<loglevel> |
| 1205 | |
Thierry FOURNIER | 486f5a0 | 2015-03-16 15:13:03 +0100 | [diff] [blame] | 1206 | .. js:function:: TXN.set_tos(txn, tos) |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 1207 | |
| 1208 | Is used to set the TOS or DSCP field value of packets sent to the client to |
| 1209 | the value passed in "tos" on platforms which support this. |
| 1210 | |
| 1211 | :param class_txn txn: The class txn object containing the data. |
| 1212 | :param integer tos: The new TOS os DSCP. |
| 1213 | |
Thierry FOURNIER | 486f5a0 | 2015-03-16 15:13:03 +0100 | [diff] [blame] | 1214 | .. js:function:: TXN.set_mark(txn, mark) |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 1215 | |
| 1216 | Is used to set the Netfilter MARK on all packets sent to the client to the |
| 1217 | value passed in "mark" on platforms which support it. |
| 1218 | |
| 1219 | :param class_txn txn: The class txn object containing the data. |
| 1220 | :param integer mark: The mark value. |
| 1221 | |
Thierry FOURNIER | dc59500 | 2015-12-21 11:13:52 +0100 | [diff] [blame] | 1222 | .. _socket_class: |
| 1223 | |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 1224 | Socket class |
| 1225 | ============ |
| 1226 | |
| 1227 | .. js:class:: Socket |
| 1228 | |
| 1229 | This class must be compatible with the Lua Socket class. Only the 'client' |
| 1230 | functions are available. See the Lua Socket documentation: |
| 1231 | |
| 1232 | `http://w3.impa.br/~diego/software/luasocket/tcp.html |
| 1233 | <http://w3.impa.br/~diego/software/luasocket/tcp.html>`_ |
| 1234 | |
Thierry FOURNIER | 486f5a0 | 2015-03-16 15:13:03 +0100 | [diff] [blame] | 1235 | .. js:function:: Socket.close(socket) |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 1236 | |
| 1237 | Closes a TCP object. The internal socket used by the object is closed and the |
| 1238 | local address to which the object was bound is made available to other |
| 1239 | applications. No further operations (except for further calls to the close |
Thierry FOURNIER | 486f5a0 | 2015-03-16 15:13:03 +0100 | [diff] [blame] | 1240 | method) are allowed on a closed Socket. |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 1241 | |
Thierry FOURNIER | 486f5a0 | 2015-03-16 15:13:03 +0100 | [diff] [blame] | 1242 | :param class_socket socket: Is the manipulated Socket. |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 1243 | |
| 1244 | Note: It is important to close all used sockets once they are not needed, |
| 1245 | since, in many systems, each socket uses a file descriptor, which are limited |
| 1246 | system resources. Garbage-collected objects are automatically closed before |
| 1247 | destruction, though. |
| 1248 | |
Thierry FOURNIER | a3bc513 | 2015-09-25 21:43:56 +0200 | [diff] [blame] | 1249 | .. js:function:: Socket.connect(socket, address[, port]) |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 1250 | |
| 1251 | Attempts to connect a socket object to a remote host. |
| 1252 | |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 1253 | |
| 1254 | In case of error, the method returns nil followed by a string describing the |
| 1255 | error. In case of success, the method returns 1. |
| 1256 | |
Thierry FOURNIER | 486f5a0 | 2015-03-16 15:13:03 +0100 | [diff] [blame] | 1257 | :param class_socket socket: Is the manipulated Socket. |
Thierry FOURNIER | a3bc513 | 2015-09-25 21:43:56 +0200 | [diff] [blame] | 1258 | :param string address: can be an IP address or a host name. See below for more |
| 1259 | information. |
| 1260 | :param integer port: must be an integer number in the range [1..64K]. |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 1261 | :returns: 1 or nil. |
| 1262 | |
Thierry FOURNIER | a3bc513 | 2015-09-25 21:43:56 +0200 | [diff] [blame] | 1263 | an address field extension permits to use the connect() function to connect to |
| 1264 | other stream than TCP. The syntax containing a simpleipv4 or ipv6 address is |
| 1265 | the basically expected format. This format requires the port. |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 1266 | |
Thierry FOURNIER | a3bc513 | 2015-09-25 21:43:56 +0200 | [diff] [blame] | 1267 | Other format accepted are a socket path like "/socket/path", it permits to |
| 1268 | connect to a socket. abstract namespaces are supported with the prefix |
| 1269 | "abns@", and finaly a filedescriotr can be passed with the prefix "fd@". |
| 1270 | The prefix "ipv4@", "ipv6@" and "unix@" are also supported. The port can be |
| 1271 | passed int the string. The syntax "127.0.0.1:1234" is valid. in this case, the |
| 1272 | parameter *port* is ignored. |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 1273 | |
Thierry FOURNIER | 486f5a0 | 2015-03-16 15:13:03 +0100 | [diff] [blame] | 1274 | .. js:function:: Socket.connect_ssl(socket, address, port) |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 1275 | |
| 1276 | Same behavior than the function socket:connect, but uses SSL. |
| 1277 | |
Thierry FOURNIER | 486f5a0 | 2015-03-16 15:13:03 +0100 | [diff] [blame] | 1278 | :param class_socket socket: Is the manipulated Socket. |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 1279 | :returns: 1 or nil. |
| 1280 | |
Thierry FOURNIER | 486f5a0 | 2015-03-16 15:13:03 +0100 | [diff] [blame] | 1281 | .. js:function:: Socket.getpeername(socket) |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 1282 | |
| 1283 | Returns information about the remote side of a connected client object. |
| 1284 | |
| 1285 | Returns a string with the IP address of the peer, followed by the port number |
| 1286 | that peer is using for the connection. In case of error, the method returns |
| 1287 | nil. |
| 1288 | |
Thierry FOURNIER | 486f5a0 | 2015-03-16 15:13:03 +0100 | [diff] [blame] | 1289 | :param class_socket socket: Is the manipulated Socket. |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 1290 | :returns: a string containing the server information. |
| 1291 | |
Thierry FOURNIER | 486f5a0 | 2015-03-16 15:13:03 +0100 | [diff] [blame] | 1292 | .. js:function:: Socket.getsockname(socket) |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 1293 | |
| 1294 | Returns the local address information associated to the object. |
| 1295 | |
| 1296 | The method returns a string with local IP address and a number with the port. |
| 1297 | In case of error, the method returns nil. |
| 1298 | |
Thierry FOURNIER | 486f5a0 | 2015-03-16 15:13:03 +0100 | [diff] [blame] | 1299 | :param class_socket socket: Is the manipulated Socket. |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 1300 | :returns: a string containing the client information. |
| 1301 | |
Thierry FOURNIER | 486f5a0 | 2015-03-16 15:13:03 +0100 | [diff] [blame] | 1302 | .. js:function:: Socket.receive(socket, [pattern [, prefix]]) |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 1303 | |
| 1304 | Reads data from a client object, according to the specified read pattern. |
| 1305 | Patterns follow the Lua file I/O format, and the difference in performance |
| 1306 | between all patterns is negligible. |
| 1307 | |
Thierry FOURNIER | 486f5a0 | 2015-03-16 15:13:03 +0100 | [diff] [blame] | 1308 | :param class_socket socket: Is the manipulated Socket. |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 1309 | :param string|integer pattern: Describe what is required (see below). |
| 1310 | :param string prefix: A string which will be prefix the returned data. |
| 1311 | :returns: a string containing the required data or nil. |
| 1312 | |
| 1313 | Pattern can be any of the following: |
| 1314 | |
| 1315 | * **`*a`**: reads from the socket until the connection is closed. No |
| 1316 | end-of-line translation is performed; |
| 1317 | |
Thierry FOURNIER | 486f5a0 | 2015-03-16 15:13:03 +0100 | [diff] [blame] | 1318 | * **`*l`**: reads a line of text from the Socket. The line is terminated by a |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 1319 | LF character (ASCII 10), optionally preceded by a CR character |
| 1320 | (ASCII 13). The CR and LF characters are not included in the |
| 1321 | returned line. In fact, all CR characters are ignored by the |
| 1322 | pattern. This is the default pattern. |
| 1323 | |
| 1324 | * **number**: causes the method to read a specified number of bytes from the |
Thierry FOURNIER | 486f5a0 | 2015-03-16 15:13:03 +0100 | [diff] [blame] | 1325 | Socket. Prefix is an optional string to be concatenated to the |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 1326 | beginning of any received data before return. |
| 1327 | |
Thierry FOURNIER | a3bc513 | 2015-09-25 21:43:56 +0200 | [diff] [blame] | 1328 | * **empty**: If the pattern is left empty, the default option is `*l`. |
| 1329 | |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 1330 | If successful, the method returns the received pattern. In case of error, the |
| 1331 | method returns nil followed by an error message which can be the string |
| 1332 | 'closed' in case the connection was closed before the transmission was |
| 1333 | completed or the string 'timeout' in case there was a timeout during the |
| 1334 | operation. Also, after the error message, the function returns the partial |
| 1335 | result of the transmission. |
| 1336 | |
| 1337 | Important note: This function was changed severely. It used to support |
| 1338 | multiple patterns (but I have never seen this feature used) and now it |
| 1339 | doesn't anymore. Partial results used to be returned in the same way as |
| 1340 | successful results. This last feature violated the idea that all functions |
| 1341 | should return nil on error. Thus it was changed too. |
| 1342 | |
Thierry FOURNIER | 486f5a0 | 2015-03-16 15:13:03 +0100 | [diff] [blame] | 1343 | .. js:function:: Socket.send(socket, data [, start [, end ]]) |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 1344 | |
| 1345 | Sends data through client object. |
| 1346 | |
Thierry FOURNIER | 486f5a0 | 2015-03-16 15:13:03 +0100 | [diff] [blame] | 1347 | :param class_socket socket: Is the manipulated Socket. |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 1348 | :param string data: The data that will be sent. |
| 1349 | :param integer start: The start position in the buffer of the data which will |
| 1350 | be sent. |
| 1351 | :param integer end: The end position in the buffer of the data which will |
| 1352 | be sent. |
| 1353 | :returns: see below. |
| 1354 | |
| 1355 | Data is the string to be sent. The optional arguments i and j work exactly |
| 1356 | like the standard string.sub Lua function to allow the selection of a |
| 1357 | substring to be sent. |
| 1358 | |
| 1359 | If successful, the method returns the index of the last byte within [start, |
| 1360 | end] that has been sent. Notice that, if start is 1 or absent, this is |
| 1361 | effectively the total number of bytes sent. In case of error, the method |
| 1362 | returns nil, followed by an error message, followed by the index of the last |
| 1363 | byte within [start, end] that has been sent. You might want to try again from |
| 1364 | the byte following that. The error message can be 'closed' in case the |
| 1365 | connection was closed before the transmission was completed or the string |
| 1366 | 'timeout' in case there was a timeout during the operation. |
| 1367 | |
| 1368 | Note: Output is not buffered. For small strings, it is always better to |
| 1369 | concatenate them in Lua (with the '..' operator) and send the result in one |
| 1370 | call instead of calling the method several times. |
| 1371 | |
Thierry FOURNIER | 486f5a0 | 2015-03-16 15:13:03 +0100 | [diff] [blame] | 1372 | .. js:function:: Socket.setoption(socket, option [, value]) |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 1373 | |
| 1374 | Just implemented for compatibility, this cal does nothing. |
| 1375 | |
Thierry FOURNIER | 486f5a0 | 2015-03-16 15:13:03 +0100 | [diff] [blame] | 1376 | .. js:function:: Socket.settimeout(socket, value [, mode]) |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 1377 | |
| 1378 | Changes the timeout values for the object. All I/O operations are blocking. |
| 1379 | That is, any call to the methods send, receive, and accept will block |
| 1380 | indefinitely, until the operation completes. The settimeout method defines a |
| 1381 | limit on the amount of time the I/O methods can block. When a timeout time |
| 1382 | has elapsed, the affected methods give up and fail with an error code. |
| 1383 | |
| 1384 | The amount of time to wait is specified as the value parameter, in seconds. |
| 1385 | |
| 1386 | The timeout modes are bot implemented, the only settable timeout is the |
| 1387 | inactivity time waiting for complete the internal buffer send or waiting for |
| 1388 | receive data. |
| 1389 | |
Thierry FOURNIER | 486f5a0 | 2015-03-16 15:13:03 +0100 | [diff] [blame] | 1390 | :param class_socket socket: Is the manipulated Socket. |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 1391 | :param integer value: The timeout value. |
| 1392 | |
Thierry FOURNIER | dc59500 | 2015-12-21 11:13:52 +0100 | [diff] [blame] | 1393 | .. _map_class: |
| 1394 | |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1395 | Map class |
| 1396 | ========= |
| 1397 | |
| 1398 | .. js:class:: Map |
| 1399 | |
| 1400 | This class permits to do some lookup in HAProxy maps. The declared maps can |
| 1401 | be modified during the runtime throught the HAProxy management socket. |
| 1402 | |
| 1403 | .. code-block:: lua |
| 1404 | |
Thierry FOURNIER | dc59500 | 2015-12-21 11:13:52 +0100 | [diff] [blame] | 1405 | default = "usa" |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1406 | |
Thierry FOURNIER | dc59500 | 2015-12-21 11:13:52 +0100 | [diff] [blame] | 1407 | -- Create and load map |
| 1408 | geo = Map.new("geo.map", Map.ip); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1409 | |
Thierry FOURNIER | dc59500 | 2015-12-21 11:13:52 +0100 | [diff] [blame] | 1410 | -- Create new fetch that returns the user country |
| 1411 | core.register_fetches("country", function(txn) |
| 1412 | local src; |
| 1413 | local loc; |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1414 | |
Thierry FOURNIER | dc59500 | 2015-12-21 11:13:52 +0100 | [diff] [blame] | 1415 | src = txn.f:fhdr("x-forwarded-for"); |
| 1416 | if (src == nil) then |
| 1417 | src = txn.f:src() |
| 1418 | if (src == nil) then |
| 1419 | return default; |
| 1420 | end |
| 1421 | end |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1422 | |
Thierry FOURNIER | dc59500 | 2015-12-21 11:13:52 +0100 | [diff] [blame] | 1423 | -- Perform lookup |
| 1424 | loc = geo:lookup(src); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1425 | |
Thierry FOURNIER | dc59500 | 2015-12-21 11:13:52 +0100 | [diff] [blame] | 1426 | if (loc == nil) then |
| 1427 | return default; |
| 1428 | end |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1429 | |
Thierry FOURNIER | dc59500 | 2015-12-21 11:13:52 +0100 | [diff] [blame] | 1430 | return loc; |
| 1431 | end); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1432 | |
| 1433 | .. js:attribute:: Map.int |
| 1434 | |
| 1435 | See the HAProxy configuration.txt file, chapter "Using ACLs and fetching |
| 1436 | samples" ans subchapter "ACL basics" to understand this pattern matching |
| 1437 | method. |
| 1438 | |
| 1439 | .. js:attribute:: Map.ip |
| 1440 | |
| 1441 | See the HAProxy configuration.txt file, chapter "Using ACLs and fetching |
| 1442 | samples" ans subchapter "ACL basics" to understand this pattern matching |
| 1443 | method. |
| 1444 | |
| 1445 | .. js:attribute:: Map.str |
| 1446 | |
| 1447 | See the HAProxy configuration.txt file, chapter "Using ACLs and fetching |
| 1448 | samples" ans subchapter "ACL basics" to understand this pattern matching |
| 1449 | method. |
| 1450 | |
| 1451 | .. js:attribute:: Map.beg |
| 1452 | |
| 1453 | See the HAProxy configuration.txt file, chapter "Using ACLs and fetching |
| 1454 | samples" ans subchapter "ACL basics" to understand this pattern matching |
| 1455 | method. |
| 1456 | |
| 1457 | .. js:attribute:: Map.sub |
| 1458 | |
| 1459 | See the HAProxy configuration.txt file, chapter "Using ACLs and fetching |
| 1460 | samples" ans subchapter "ACL basics" to understand this pattern matching |
| 1461 | method. |
| 1462 | |
| 1463 | .. js:attribute:: Map.dir |
| 1464 | |
| 1465 | See the HAProxy configuration.txt file, chapter "Using ACLs and fetching |
| 1466 | samples" ans subchapter "ACL basics" to understand this pattern matching |
| 1467 | method. |
| 1468 | |
| 1469 | .. js:attribute:: Map.dom |
| 1470 | |
| 1471 | See the HAProxy configuration.txt file, chapter "Using ACLs and fetching |
| 1472 | samples" ans subchapter "ACL basics" to understand this pattern matching |
| 1473 | method. |
| 1474 | |
| 1475 | .. js:attribute:: Map.end |
| 1476 | |
| 1477 | See the HAProxy configuration.txt file, chapter "Using ACLs and fetching |
| 1478 | samples" ans subchapter "ACL basics" to understand this pattern matching |
| 1479 | method. |
| 1480 | |
| 1481 | .. js:attribute:: Map.reg |
| 1482 | |
| 1483 | See the HAProxy configuration.txt file, chapter "Using ACLs and fetching |
| 1484 | samples" ans subchapter "ACL basics" to understand this pattern matching |
| 1485 | method. |
| 1486 | |
| 1487 | |
| 1488 | .. js:function:: Map.new(file, method) |
| 1489 | |
| 1490 | Creates and load a map. |
| 1491 | |
| 1492 | :param string file: Is the file containing the map. |
| 1493 | :param integer method: Is the map pattern matching method. See the attributes |
| 1494 | of the Map class. |
| 1495 | :returns: a class Map object. |
| 1496 | :see: The Map attributes. |
| 1497 | |
| 1498 | .. js:function:: Map.lookup(map, str) |
| 1499 | |
| 1500 | Perform a lookup in a map. |
| 1501 | |
| 1502 | :param class_map map: Is the class Map object. |
| 1503 | :param string str: Is the string used as key. |
| 1504 | :returns: a string containing the result or nil if no match. |
| 1505 | |
| 1506 | .. js:function:: Map.slookup(map, str) |
| 1507 | |
| 1508 | Perform a lookup in a map. |
| 1509 | |
| 1510 | :param class_map map: Is the class Map object. |
| 1511 | :param string str: Is the string used as key. |
| 1512 | :returns: a string containing the result or empty string if no match. |
| 1513 | |
Thierry FOURNIER | dc59500 | 2015-12-21 11:13:52 +0100 | [diff] [blame] | 1514 | .. _applethttp_class: |
| 1515 | |
Thierry FOURNIER | a3bc513 | 2015-09-25 21:43:56 +0200 | [diff] [blame] | 1516 | AppletHTTP class |
Thierry FOURNIER | dc59500 | 2015-12-21 11:13:52 +0100 | [diff] [blame] | 1517 | ================ |
Thierry FOURNIER | a3bc513 | 2015-09-25 21:43:56 +0200 | [diff] [blame] | 1518 | |
| 1519 | .. js:class:: AppletHTTP |
| 1520 | |
| 1521 | This class is used with applets that requires the 'http' mode. The http applet |
| 1522 | can be registered with the *core.register_service()* function. They are used |
| 1523 | for processing an http request like a server in back of HAProxy. |
| 1524 | |
| 1525 | This is an hello world sample code: |
| 1526 | |
| 1527 | .. code-block:: lua |
Thierry FOURNIER | dc59500 | 2015-12-21 11:13:52 +0100 | [diff] [blame] | 1528 | |
Pieter Baauw | 4d7f766 | 2015-11-08 16:38:08 +0100 | [diff] [blame] | 1529 | core.register_service("hello-world", "http", function(applet) |
Thierry FOURNIER | a3bc513 | 2015-09-25 21:43:56 +0200 | [diff] [blame] | 1530 | local response = "Hello World !" |
| 1531 | applet:set_status(200) |
Pieter Baauw | 2dcb9bc | 2015-10-01 22:47:12 +0200 | [diff] [blame] | 1532 | applet:add_header("content-length", string.len(response)) |
Thierry FOURNIER | a3bc513 | 2015-09-25 21:43:56 +0200 | [diff] [blame] | 1533 | applet:add_header("content-type", "text/plain") |
Pieter Baauw | 2dcb9bc | 2015-10-01 22:47:12 +0200 | [diff] [blame] | 1534 | applet:start_response() |
Thierry FOURNIER | a3bc513 | 2015-09-25 21:43:56 +0200 | [diff] [blame] | 1535 | applet:send(response) |
| 1536 | end) |
| 1537 | |
Thierry FOURNIER | dc59500 | 2015-12-21 11:13:52 +0100 | [diff] [blame] | 1538 | .. js:attribute:: AppletHTTP.c |
| 1539 | |
| 1540 | :returns: A :ref:`converters_class` |
| 1541 | |
| 1542 | This attribute contains a Converters class object. |
| 1543 | |
| 1544 | .. js:attribute:: AppletHTTP.sc |
| 1545 | |
| 1546 | :returns: A :ref:`converters_class` |
| 1547 | |
| 1548 | This attribute contains a Converters class object. The |
| 1549 | functions of this object returns always a string. |
| 1550 | |
| 1551 | .. js:attribute:: AppletHTTP.f |
| 1552 | |
| 1553 | :returns: A :ref:`fetches_class` |
| 1554 | |
| 1555 | This attribute contains a Fetches class object. Note that the |
| 1556 | applet execution place cannot access to a valid HAProxy core HTTP |
| 1557 | transaction, so some sample fecthes related to the HTTP dependant |
| 1558 | values (hdr, path, ...) are not available. |
| 1559 | |
| 1560 | .. js:attribute:: AppletHTTP.sf |
| 1561 | |
| 1562 | :returns: A :ref:`fetches_class` |
| 1563 | |
| 1564 | This attribute contains a Fetches class object. The functions of |
| 1565 | this object returns always a string. Note that the applet |
| 1566 | execution place cannot access to a valid HAProxy core HTTP |
| 1567 | transaction, so some sample fecthes related to the HTTP dependant |
| 1568 | values (hdr, path, ...) are not available. |
| 1569 | |
Thierry FOURNIER | e34a78e | 2015-12-25 01:31:35 +0100 | [diff] [blame] | 1570 | .. js:attribute:: AppletHTTP.method |
Thierry FOURNIER | dc59500 | 2015-12-21 11:13:52 +0100 | [diff] [blame] | 1571 | |
| 1572 | :returns: string |
| 1573 | |
| 1574 | The attribute method returns a string containing the HTTP |
| 1575 | method. |
| 1576 | |
| 1577 | .. js:attribute:: AppletHTTP.version |
| 1578 | |
| 1579 | :returns: string |
| 1580 | |
| 1581 | The attribute version, returns a string containing the HTTP |
| 1582 | request version. |
| 1583 | |
| 1584 | .. js:attribute:: AppletHTTP.path |
| 1585 | |
| 1586 | :returns: string |
| 1587 | |
| 1588 | The attribute path returns a string containing the HTTP |
| 1589 | request path. |
| 1590 | |
| 1591 | .. js:attribute:: AppletHTTP.qs |
| 1592 | |
| 1593 | :returns: string |
| 1594 | |
| 1595 | The attribute qs returns a string containing the HTTP |
| 1596 | request query string. |
| 1597 | |
| 1598 | .. js:attribute:: AppletHTTP.length |
| 1599 | |
| 1600 | :returns: integer |
| 1601 | |
| 1602 | The attribute length returns an integer containing the HTTP |
| 1603 | body length. |
Thierry FOURNIER | a3bc513 | 2015-09-25 21:43:56 +0200 | [diff] [blame] | 1604 | |
Thierry FOURNIER | 841475e | 2015-12-11 17:10:09 +0100 | [diff] [blame] | 1605 | .. js:attribute:: AppletHTTP.headers |
| 1606 | |
Thierry FOURNIER | dc59500 | 2015-12-21 11:13:52 +0100 | [diff] [blame] | 1607 | :returns: array |
| 1608 | |
| 1609 | The attribute headers returns an array containing the HTTP |
| 1610 | headers. The header names are always in lower case. As the header name can be |
| 1611 | encountered more than once in each request, the value is indexed with 0 as |
| 1612 | first index value. The array have this form: |
| 1613 | |
| 1614 | .. code-block:: lua |
| 1615 | |
| 1616 | AppletHTTP.headers['<header-name>'][<header-index>] = "<header-value>" |
| 1617 | |
| 1618 | AppletHTTP.headers["host"][0] = "www.test.com" |
| 1619 | AppletHTTP.headers["accept"][0] = "audio/basic q=1" |
| 1620 | AppletHTTP.headers["accept"][1] = "audio/*, q=0.2" |
Thierry FOURNIER | e34a78e | 2015-12-25 01:31:35 +0100 | [diff] [blame] | 1621 | AppletHTTP.headers["accept"][2] = "*/*, q=0.1" |
Thierry FOURNIER | dc59500 | 2015-12-21 11:13:52 +0100 | [diff] [blame] | 1622 | .. |
| 1623 | |
| 1624 | .. js:attribute:: AppletHTTP.headers |
| 1625 | |
Thierry FOURNIER | 841475e | 2015-12-11 17:10:09 +0100 | [diff] [blame] | 1626 | Contains an array containing all the request headers. |
| 1627 | |
Thierry FOURNIER | e34a78e | 2015-12-25 01:31:35 +0100 | [diff] [blame] | 1628 | .. js:function:: AppletHTTP.set_status(applet, code) |
Thierry FOURNIER | a3bc513 | 2015-09-25 21:43:56 +0200 | [diff] [blame] | 1629 | |
| 1630 | This function sets the HTTP status code for the response. The allowed code are |
| 1631 | from 100 to 599. |
| 1632 | |
Thierry FOURNIER | e34a78e | 2015-12-25 01:31:35 +0100 | [diff] [blame] | 1633 | :param class_AppletHTTP applet: An :ref:`applethttp_class` |
Thierry FOURNIER | a3bc513 | 2015-09-25 21:43:56 +0200 | [diff] [blame] | 1634 | :param integer code: the status code returned to the client. |
| 1635 | |
Thierry FOURNIER | e34a78e | 2015-12-25 01:31:35 +0100 | [diff] [blame] | 1636 | .. js:function:: AppletHTTP.add_header(applet, name, value) |
Thierry FOURNIER | a3bc513 | 2015-09-25 21:43:56 +0200 | [diff] [blame] | 1637 | |
| 1638 | This function add an header in the response. Duplicated headers are not |
| 1639 | collapsed. The special header *content-length* is used to determinate the |
| 1640 | response length. If it not exists, a *transfer-encoding: chunked* is set, and |
| 1641 | all the write from the funcion *AppletHTTP:send()* become a chunk. |
| 1642 | |
Thierry FOURNIER | e34a78e | 2015-12-25 01:31:35 +0100 | [diff] [blame] | 1643 | :param class_AppletHTTP applet: An :ref:`applethttp_class` |
Thierry FOURNIER | a3bc513 | 2015-09-25 21:43:56 +0200 | [diff] [blame] | 1644 | :param string name: the header name |
| 1645 | :param string value: the header value |
| 1646 | |
Thierry FOURNIER | e34a78e | 2015-12-25 01:31:35 +0100 | [diff] [blame] | 1647 | .. js:function:: AppletHTTP.start_response(applet) |
Thierry FOURNIER | a3bc513 | 2015-09-25 21:43:56 +0200 | [diff] [blame] | 1648 | |
| 1649 | This function indicates to the HTTP engine that it can process and send the |
| 1650 | response headers. After this called we cannot add headers to the response; We |
| 1651 | cannot use the *AppletHTTP:send()* function if the |
| 1652 | *AppletHTTP:start_response()* is not called. |
| 1653 | |
Thierry FOURNIER | e34a78e | 2015-12-25 01:31:35 +0100 | [diff] [blame] | 1654 | :param class_AppletHTTP applet: An :ref:`applethttp_class` |
| 1655 | |
| 1656 | .. js:function:: AppletHTTP.getline(applet) |
Thierry FOURNIER | a3bc513 | 2015-09-25 21:43:56 +0200 | [diff] [blame] | 1657 | |
| 1658 | This function returns a string containing one line from the http body. If the |
| 1659 | data returned doesn't contains a final '\\n' its assumed than its the last |
| 1660 | available data before the end of stream. |
| 1661 | |
Thierry FOURNIER | e34a78e | 2015-12-25 01:31:35 +0100 | [diff] [blame] | 1662 | :param class_AppletHTTP applet: An :ref:`applethttp_class` |
Thierry FOURNIER | a3bc513 | 2015-09-25 21:43:56 +0200 | [diff] [blame] | 1663 | :returns: a string. The string can be empty if we reach the end of the stream. |
| 1664 | |
Thierry FOURNIER | e34a78e | 2015-12-25 01:31:35 +0100 | [diff] [blame] | 1665 | .. js:function:: AppletHTTP.receive(applet, [size]) |
Thierry FOURNIER | a3bc513 | 2015-09-25 21:43:56 +0200 | [diff] [blame] | 1666 | |
| 1667 | Reads data from the HTTP body, according to the specified read *size*. If the |
| 1668 | *size* is missing, the function tries to read all the content of the stream |
| 1669 | until the end. If the *size* is bigger than the http body, it returns the |
| 1670 | amount of data avalaible. |
| 1671 | |
Thierry FOURNIER | e34a78e | 2015-12-25 01:31:35 +0100 | [diff] [blame] | 1672 | :param class_AppletHTTP applet: An :ref:`applethttp_class` |
Thierry FOURNIER | a3bc513 | 2015-09-25 21:43:56 +0200 | [diff] [blame] | 1673 | :param integer size: the required read size. |
| 1674 | :returns: always return a string,the string can be empty is the connexion is |
| 1675 | closed. |
| 1676 | |
Thierry FOURNIER | e34a78e | 2015-12-25 01:31:35 +0100 | [diff] [blame] | 1677 | .. js:function:: AppletHTTP.send(applet, msg) |
Thierry FOURNIER | a3bc513 | 2015-09-25 21:43:56 +0200 | [diff] [blame] | 1678 | |
| 1679 | Send the message *msg* on the http request body. |
| 1680 | |
Thierry FOURNIER | e34a78e | 2015-12-25 01:31:35 +0100 | [diff] [blame] | 1681 | :param class_AppletHTTP applet: An :ref:`applethttp_class` |
Thierry FOURNIER | a3bc513 | 2015-09-25 21:43:56 +0200 | [diff] [blame] | 1682 | :param string msg: the message to send. |
| 1683 | |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 1684 | .. js:function:: AppletHTTP.get_priv(applet) |
| 1685 | |
| 1686 | Return Lua data stored in the current transaction (with the |
| 1687 | `AppletHTTP.set_priv()`) function. If no data are stored, it returns a nil |
| 1688 | value. |
| 1689 | |
| 1690 | :param class_AppletHTTP applet: An :ref:`applethttp_class` |
| 1691 | :returns: the opaque data previsously stored, or nil if nothing is |
| 1692 | avalaible. |
| 1693 | |
| 1694 | .. js:function:: AppletHTTP.set_priv(applet, data) |
| 1695 | |
| 1696 | Store any data in the current HAProxy transaction. This action replace the |
| 1697 | old stored data. |
| 1698 | |
| 1699 | :param class_AppletHTTP applet: An :ref:`applethttp_class` |
| 1700 | :param opaque data: The data which is stored in the transaction. |
| 1701 | |
Thierry FOURNIER | dc59500 | 2015-12-21 11:13:52 +0100 | [diff] [blame] | 1702 | .. _applettcp_class: |
| 1703 | |
Thierry FOURNIER | a3bc513 | 2015-09-25 21:43:56 +0200 | [diff] [blame] | 1704 | AppletTCP class |
| 1705 | =============== |
| 1706 | |
| 1707 | .. js:class:: AppletTCP |
| 1708 | |
| 1709 | This class is used with applets that requires the 'tcp' mode. The tcp applet |
| 1710 | can be registered with the *core.register_service()* function. They are used |
| 1711 | for processing a tcp stream like a server in back of HAProxy. |
| 1712 | |
Thierry FOURNIER | dc59500 | 2015-12-21 11:13:52 +0100 | [diff] [blame] | 1713 | .. js:attribute:: AppletTCP.c |
| 1714 | |
| 1715 | :returns: A :ref:`converters_class` |
| 1716 | |
| 1717 | This attribute contains a Converters class object. |
| 1718 | |
| 1719 | .. js:attribute:: AppletTCP.sc |
| 1720 | |
| 1721 | :returns: A :ref:`converters_class` |
| 1722 | |
| 1723 | This attribute contains a Converters class object. The |
| 1724 | functions of this object returns always a string. |
| 1725 | |
| 1726 | .. js:attribute:: AppletTCP.f |
| 1727 | |
| 1728 | :returns: A :ref:`fetches_class` |
| 1729 | |
| 1730 | This attribute contains a Fetches class object. |
| 1731 | |
| 1732 | .. js:attribute:: AppletTCP.sf |
| 1733 | |
| 1734 | :returns: A :ref:`fetches_class` |
| 1735 | |
| 1736 | This attribute contains a Fetches class object. |
| 1737 | |
Thierry FOURNIER | e34a78e | 2015-12-25 01:31:35 +0100 | [diff] [blame] | 1738 | .. js:function:: AppletTCP.getline(applet) |
Thierry FOURNIER | a3bc513 | 2015-09-25 21:43:56 +0200 | [diff] [blame] | 1739 | |
| 1740 | This function returns a string containing one line from the stream. If the |
| 1741 | data returned doesn't contains a final '\\n' its assumed than its the last |
| 1742 | available data before the end of stream. |
| 1743 | |
Thierry FOURNIER | e34a78e | 2015-12-25 01:31:35 +0100 | [diff] [blame] | 1744 | :param class_AppletTCP applet: An :ref:`applettcp_class` |
Thierry FOURNIER | a3bc513 | 2015-09-25 21:43:56 +0200 | [diff] [blame] | 1745 | :returns: a string. The string can be empty if we reach the end of the stream. |
| 1746 | |
Thierry FOURNIER | e34a78e | 2015-12-25 01:31:35 +0100 | [diff] [blame] | 1747 | .. js:function:: AppletTCP.receive(applet, [size]) |
Thierry FOURNIER | a3bc513 | 2015-09-25 21:43:56 +0200 | [diff] [blame] | 1748 | |
| 1749 | Reads data from the TCP stream, according to the specified read *size*. If the |
| 1750 | *size* is missing, the function tries to read all the content of the stream |
| 1751 | until the end. |
| 1752 | |
Thierry FOURNIER | e34a78e | 2015-12-25 01:31:35 +0100 | [diff] [blame] | 1753 | :param class_AppletTCP applet: An :ref:`applettcp_class` |
Thierry FOURNIER | a3bc513 | 2015-09-25 21:43:56 +0200 | [diff] [blame] | 1754 | :param integer size: the required read size. |
| 1755 | :returns: always return a string,the string can be empty is the connexion is |
| 1756 | closed. |
| 1757 | |
Thierry FOURNIER | e34a78e | 2015-12-25 01:31:35 +0100 | [diff] [blame] | 1758 | .. js:function:: AppletTCP.send(appletmsg) |
Thierry FOURNIER | a3bc513 | 2015-09-25 21:43:56 +0200 | [diff] [blame] | 1759 | |
| 1760 | Send the message on the stream. |
| 1761 | |
Thierry FOURNIER | e34a78e | 2015-12-25 01:31:35 +0100 | [diff] [blame] | 1762 | :param class_AppletTCP applet: An :ref:`applettcp_class` |
Thierry FOURNIER | a3bc513 | 2015-09-25 21:43:56 +0200 | [diff] [blame] | 1763 | :param string msg: the message to send. |
| 1764 | |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 1765 | .. js:function:: AppletTCP.get_priv(applet) |
| 1766 | |
| 1767 | Return Lua data stored in the current transaction (with the |
| 1768 | `AppletTCP.set_priv()`) function. If no data are stored, it returns a nil |
| 1769 | value. |
| 1770 | |
| 1771 | :param class_AppletTCP applet: An :ref:`applettcp_class` |
| 1772 | :returns: the opaque data previsously stored, or nil if nothing is |
| 1773 | avalaible. |
| 1774 | |
| 1775 | .. js:function:: AppletTCP.set_priv(applet, data) |
| 1776 | |
| 1777 | Store any data in the current HAProxy transaction. This action replace the |
| 1778 | old stored data. |
| 1779 | |
| 1780 | :param class_AppletTCP applet: An :ref:`applettcp_class` |
| 1781 | :param opaque data: The data which is stored in the transaction. |
| 1782 | |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 1783 | External Lua libraries |
| 1784 | ====================== |
| 1785 | |
| 1786 | A lot of useful lua libraries can be found here: |
| 1787 | |
| 1788 | * `https://lua-toolbox.com/ <https://lua-toolbox.com/>`_ |
| 1789 | |
| 1790 | Redis acces: |
| 1791 | |
| 1792 | * `https://github.com/nrk/redis-lua <https://github.com/nrk/redis-lua>`_ |
| 1793 | |
| 1794 | This is an example about the usage of the Redis library with HAProxy. Note that |
| 1795 | each call of any function of this library can throw an error if the socket |
| 1796 | connection fails. |
| 1797 | |
| 1798 | .. code-block:: lua |
| 1799 | |
| 1800 | -- load the redis library |
| 1801 | local redis = require("redis"); |
| 1802 | |
| 1803 | function do_something(txn) |
| 1804 | |
| 1805 | -- create and connect new tcp socket |
| 1806 | local tcp = core.tcp(); |
| 1807 | tcp:settimeout(1); |
| 1808 | tcp:connect("127.0.0.1", 6379); |
| 1809 | |
| 1810 | -- use the redis library with this new socket |
| 1811 | local client = redis.connect({socket=tcp}); |
| 1812 | client:ping(); |
| 1813 | |
| 1814 | end |
| 1815 | |
| 1816 | OpenSSL: |
| 1817 | |
| 1818 | * `http://mkottman.github.io/luacrypto/index.html |
| 1819 | <http://mkottman.github.io/luacrypto/index.html>`_ |
| 1820 | |
| 1821 | * `https://github.com/brunoos/luasec/wiki |
| 1822 | <https://github.com/brunoos/luasec/wiki>`_ |
Thierry FOURNIER | 2e4893c | 2015-03-18 13:37:27 +0100 | [diff] [blame] | 1823 | |