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 | eea77c0 | 2016-03-18 08:47:13 +0100 | [diff] [blame] | 273 | .. js:function:: core.get_info() |
| 274 | |
| 275 | **context**: body, init, task, action, sample-fetch, converter |
| 276 | |
| 277 | Returns HAProxy core informations. We can found information like the uptime, |
| 278 | the pid, memory pool usage, tasks number, ... |
| 279 | |
| 280 | These information are also returned by the management sockat via the command |
| 281 | "show info". See the management socket documentation fpor more information |
| 282 | about the content of these variables. |
| 283 | |
| 284 | :returns: an array of values. |
| 285 | |
Thierry Fournier | b1f4656 | 2016-01-21 09:46:15 +0100 | [diff] [blame] | 286 | .. js:function:: core.now() |
| 287 | |
| 288 | **context**: body, init, task, action |
| 289 | |
| 290 | This function returns the current time. The time returned is fixed by the |
| 291 | HAProxy core and assures than the hour will be monotnic and that the system |
| 292 | call 'gettimeofday' will not be called too. The time is refreshed between each |
| 293 | Lua execution or resume, so two consecutive call to the function "now" will |
| 294 | probably returns the same result. |
| 295 | |
| 296 | :returns: an array which contains two entries "sec" and "usec". "sec" |
| 297 | contains the current at the epoch format, and "usec" contains the |
| 298 | current microseconds. |
| 299 | |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 300 | .. js:function:: core.msleep(milliseconds) |
| 301 | |
| 302 | **context**: body, init, task, action |
| 303 | |
| 304 | The `core.msleep()` stops the Lua execution between specified milliseconds. |
| 305 | |
| 306 | :param integer milliseconds: the required milliseconds. |
| 307 | |
Thierry Fournier | f61aa63 | 2016-02-19 20:56:00 +0100 | [diff] [blame] | 308 | .. js:attribute:: core.proxies |
| 309 | |
| 310 | **context**: body, init, task, action, sample-fetch, converter |
| 311 | |
| 312 | proxies is an array containing the list of all proxies declared in the |
| 313 | configuration file. Each entry of the proxies array is an object of type |
| 314 | :ref:`proxy_class` |
| 315 | |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 316 | .. js:function:: core.register_action(name, actions, func) |
| 317 | |
| 318 | **context**: body |
| 319 | |
David Carlier | 61fdf8b | 2015-10-02 11:59:38 +0100 | [diff] [blame] | 320 | 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] | 321 | used in HAProxy with the prefix "lua.". An action gets a TXN object class as |
| 322 | input. |
| 323 | |
| 324 | :param string name: is the name of the converter. |
Willy Tarreau | 61add3c | 2015-09-28 15:39:10 +0200 | [diff] [blame] | 325 | :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] | 326 | want to register to. The expected actions are 'tcp-req', |
| 327 | 'tcp-res', 'http-req' or 'http-res'. |
| 328 | :param function func: is the Lua function called to work as converter. |
| 329 | |
| 330 | The prototype of the Lua function used as argument is: |
| 331 | |
| 332 | .. code-block:: lua |
| 333 | |
| 334 | function(txn) |
| 335 | .. |
| 336 | |
Thierry FOURNIER | dc59500 | 2015-12-21 11:13:52 +0100 | [diff] [blame] | 337 | * **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] | 338 | current request or TCP stream. |
| 339 | |
Willy Tarreau | 61add3c | 2015-09-28 15:39:10 +0200 | [diff] [blame] | 340 | 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] | 341 | in the logs. |
| 342 | |
| 343 | .. code-block:: lua |
| 344 | |
| 345 | core.register_action("hello-world", { "tcp-req", "http-req" }, function(txn) |
| 346 | txn:Info("Hello world") |
| 347 | end) |
| 348 | .. |
| 349 | |
| 350 | This example code is used in HAproxy configuration like this: |
| 351 | |
| 352 | :: |
| 353 | |
| 354 | frontend tcp_frt |
| 355 | mode tcp |
| 356 | tcp-request content lua.hello-world |
| 357 | |
| 358 | frontend http_frt |
| 359 | mode http |
| 360 | http-request lua.hello-world |
| 361 | |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 362 | .. js:function:: core.register_converters(name, func) |
| 363 | |
| 364 | **context**: body |
| 365 | |
David Carlier | 61fdf8b | 2015-10-02 11:59:38 +0100 | [diff] [blame] | 366 | Register a Lua function executed as converter. All the registered converters |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 367 | can be used in HAProxy with the prefix "lua.". An converter get a string as |
| 368 | input and return a string as output. The registered function can take up to 9 |
| 369 | values as parameter. All the value are strings. |
| 370 | |
| 371 | :param string name: is the name of the converter. |
| 372 | :param function func: is the Lua function called to work as converter. |
| 373 | |
| 374 | The prototype of the Lua function used as argument is: |
| 375 | |
| 376 | .. code-block:: lua |
| 377 | |
| 378 | function(str, [p1 [, p2 [, ... [, p5]]]]) |
| 379 | .. |
| 380 | |
| 381 | * **str** (*string*): this is the input value automatically converted in |
| 382 | string. |
| 383 | * **p1** .. **p5** (*string*): this is a list of string arguments declared in |
| 384 | the haroxy configuration file. The number of arguments doesn't exceed 5. |
| 385 | The order and the nature of these is conventionally choose by the |
| 386 | developper. |
| 387 | |
| 388 | .. js:function:: core.register_fetches(name, func) |
| 389 | |
| 390 | **context**: body |
| 391 | |
David Carlier | 61fdf8b | 2015-10-02 11:59:38 +0100 | [diff] [blame] | 392 | Register a Lua function executed as sample fetch. All the registered sample |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 393 | fetchs can be used in HAProxy with the prefix "lua.". A Lua sample fetch |
| 394 | return a string as output. The registered function can take up to 9 values as |
| 395 | parameter. All the value are strings. |
| 396 | |
| 397 | :param string name: is the name of the converter. |
| 398 | :param function func: is the Lua function called to work as sample fetch. |
| 399 | |
| 400 | The prototype of the Lua function used as argument is: |
| 401 | |
| 402 | .. code-block:: lua |
| 403 | |
| 404 | string function(txn, [p1 [, p2 [, ... [, p5]]]]) |
| 405 | .. |
| 406 | |
Thierry FOURNIER | dc59500 | 2015-12-21 11:13:52 +0100 | [diff] [blame] | 407 | * **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] | 408 | request. |
| 409 | * **p1** .. **p5** (*string*): this is a list of string arguments declared in |
| 410 | the haroxy configuration file. The number of arguments doesn't exceed 5. |
| 411 | The order and the nature of these is conventionally choose by the |
| 412 | developper. |
| 413 | * **Returns**: A string containing some data, ot nil if the value cannot be |
| 414 | returned now. |
| 415 | |
| 416 | lua example code: |
| 417 | |
| 418 | .. code-block:: lua |
| 419 | |
| 420 | core.register_fetches("hello", function(txn) |
| 421 | return "hello" |
| 422 | end) |
| 423 | .. |
| 424 | |
| 425 | HAProxy example configuration: |
| 426 | |
| 427 | :: |
| 428 | |
| 429 | frontend example |
| 430 | http-request redirect location /%[lua.hello] |
| 431 | |
Thierry FOURNIER | a3bc513 | 2015-09-25 21:43:56 +0200 | [diff] [blame] | 432 | .. js:function:: core.register_service(name, mode, func) |
| 433 | |
| 434 | **context**: body |
| 435 | |
David Carlier | 61fdf8b | 2015-10-02 11:59:38 +0100 | [diff] [blame] | 436 | 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] | 437 | be used in HAProxy with the prefix "lua.". A service gets an object class as |
| 438 | input according with the required mode. |
| 439 | |
| 440 | :param string name: is the name of the converter. |
Willy Tarreau | 61add3c | 2015-09-28 15:39:10 +0200 | [diff] [blame] | 441 | :param string mode: is string describing the required mode. Only 'tcp' or |
Thierry FOURNIER | a3bc513 | 2015-09-25 21:43:56 +0200 | [diff] [blame] | 442 | 'http' are allowed. |
| 443 | :param function func: is the Lua function called to work as converter. |
| 444 | |
| 445 | The prototype of the Lua function used as argument is: |
| 446 | |
| 447 | .. code-block:: lua |
| 448 | |
| 449 | function(applet) |
| 450 | .. |
| 451 | |
Thierry FOURNIER | dc59500 | 2015-12-21 11:13:52 +0100 | [diff] [blame] | 452 | * **applet** *applet* will be a :ref:`applettcp_class` or a |
| 453 | :ref:`applethttp_class`. It depends the type of registered applet. An applet |
| 454 | registered with the 'http' value for the *mode* parameter will gets a |
| 455 | :ref:`applethttp_class`. If the *mode* value is 'tcp', the applet will gets |
| 456 | a :ref:`applettcp_class`. |
| 457 | |
| 458 | **warning**: Applets of type 'http' cannot be called from 'tcp-*' |
| 459 | rulesets. Only the 'http-*' rulesets are authorized, this means |
| 460 | that is not possible to call an HTTP applet from a proxy in tcp |
| 461 | mode. Applets of type 'tcp' can be called from anywhre. |
Thierry FOURNIER | a3bc513 | 2015-09-25 21:43:56 +0200 | [diff] [blame] | 462 | |
Willy Tarreau | 61add3c | 2015-09-28 15:39:10 +0200 | [diff] [blame] | 463 | 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] | 464 | as an http response. |
| 465 | |
| 466 | .. code-block:: lua |
| 467 | |
Pieter Baauw | 4d7f766 | 2015-11-08 16:38:08 +0100 | [diff] [blame] | 468 | core.register_service("hello-world", "http", function(applet) |
Thierry FOURNIER | a3bc513 | 2015-09-25 21:43:56 +0200 | [diff] [blame] | 469 | local response = "Hello World !" |
| 470 | applet:set_status(200) |
Pieter Baauw | 2dcb9bc | 2015-10-01 22:47:12 +0200 | [diff] [blame] | 471 | applet:add_header("content-length", string.len(response)) |
Thierry FOURNIER | a3bc513 | 2015-09-25 21:43:56 +0200 | [diff] [blame] | 472 | applet:add_header("content-type", "text/plain") |
Pieter Baauw | 2dcb9bc | 2015-10-01 22:47:12 +0200 | [diff] [blame] | 473 | applet:start_response() |
Thierry FOURNIER | a3bc513 | 2015-09-25 21:43:56 +0200 | [diff] [blame] | 474 | applet:send(response) |
| 475 | end) |
| 476 | .. |
| 477 | |
| 478 | This example code is used in HAproxy configuration like this: |
| 479 | |
| 480 | :: |
| 481 | |
| 482 | frontend example |
| 483 | http-request use-service lua.hello-world |
| 484 | |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 485 | .. js:function:: core.register_init(func) |
| 486 | |
| 487 | **context**: body |
| 488 | |
| 489 | Register a function executed after the configuration parsing. This is useful |
| 490 | to check any parameters. |
| 491 | |
Pieter Baauw | 4d7f766 | 2015-11-08 16:38:08 +0100 | [diff] [blame] | 492 | :param function func: is the Lua function called to work as initializer. |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 493 | |
| 494 | The prototype of the Lua function used as argument is: |
| 495 | |
| 496 | .. code-block:: lua |
| 497 | |
| 498 | function() |
| 499 | .. |
| 500 | |
| 501 | It takes no input, and no output is expected. |
| 502 | |
| 503 | .. js:function:: core.register_task(func) |
| 504 | |
| 505 | **context**: body, init, task, action, sample-fetch, converter |
| 506 | |
| 507 | Register and start independent task. The task is started when the HAProxy |
| 508 | 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] | 509 | perform complex health checks. |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 510 | |
Pieter Baauw | 4d7f766 | 2015-11-08 16:38:08 +0100 | [diff] [blame] | 511 | :param function func: is the Lua function called to work as initializer. |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 512 | |
| 513 | The prototype of the Lua function used as argument is: |
| 514 | |
| 515 | .. code-block:: lua |
| 516 | |
| 517 | function() |
| 518 | .. |
| 519 | |
| 520 | It takes no input, and no output is expected. |
| 521 | |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 522 | .. js:function:: core.register_cli([path], usage, func) |
| 523 | |
| 524 | **context**: body |
| 525 | |
| 526 | Register and start independent task. The task is started when the HAProxy |
| 527 | main scheduler starts. For example this type of tasks can be executed to |
| 528 | perform complex health checks. |
| 529 | |
| 530 | :param array path: is the sequence of word for which the cli execute the Lua |
| 531 | binding. |
| 532 | :param string usage: is the usage message displayed in the help. |
| 533 | :param function func: is the Lua function called to handle the CLI commands. |
| 534 | |
| 535 | The prototype of the Lua function used as argument is: |
| 536 | |
| 537 | .. code-block:: lua |
| 538 | |
| 539 | function(AppletTCP, [arg1, [arg2, [...]]]) |
| 540 | .. |
| 541 | |
| 542 | I/O are managed with the :ref:`applettcp_class` object. Args are given as |
| 543 | paramter. The args embbed the registred path. If the path is declared like |
| 544 | this: |
| 545 | |
| 546 | .. code-block:: lua |
| 547 | |
| 548 | core.register_cli({"show", "ssl", "stats"}, "Display SSL stats..", function(applet, arg1, arg2, arg3, arg4, arg5) |
| 549 | end) |
| 550 | .. |
| 551 | |
| 552 | And we execute this in the prompt: |
| 553 | |
| 554 | .. code-block:: text |
| 555 | |
| 556 | > prompt |
| 557 | > show ssl stats all |
| 558 | .. |
| 559 | |
| 560 | Then, arg1, arg2 and arg3 will contains respectivey "show", "ssl" and "stats". |
| 561 | arg4 will contain "all". arg5 contains nil. |
| 562 | |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 563 | .. js:function:: core.set_nice(nice) |
| 564 | |
| 565 | **context**: task, action, sample-fetch, converter |
| 566 | |
| 567 | Change the nice of the current task or current session. |
Thierry FOURNIER | 2e4893c | 2015-03-18 13:37:27 +0100 | [diff] [blame] | 568 | |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 569 | :param integer nice: the nice value, it must be between -1024 and 1024. |
| 570 | |
| 571 | .. js:function:: core.set_map(filename, key, value) |
| 572 | |
| 573 | **context**: init, task, action, sample-fetch, converter |
| 574 | |
| 575 | set the value *value* associated to the key *key* in the map referenced by |
| 576 | *filename*. |
| 577 | |
Thierry FOURNIER | dc59500 | 2015-12-21 11:13:52 +0100 | [diff] [blame] | 578 | :param string filename: the Map reference |
| 579 | :param string key: the key to set or replace |
| 580 | :param string value: the associated value |
| 581 | |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 582 | .. js:function:: core.sleep(int seconds) |
| 583 | |
| 584 | **context**: body, init, task, action |
| 585 | |
| 586 | The `core.sleep()` functions stop the Lua execution between specified seconds. |
| 587 | |
| 588 | :param integer seconds: the required seconds. |
| 589 | |
| 590 | .. js:function:: core.tcp() |
| 591 | |
| 592 | **context**: init, task, action |
| 593 | |
| 594 | This function returns a new object of a *socket* class. |
| 595 | |
Thierry FOURNIER | dc59500 | 2015-12-21 11:13:52 +0100 | [diff] [blame] | 596 | :returns: A :ref:`socket_class` object. |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 597 | |
Thierry Fournier | 1de1659 | 2016-01-27 09:49:07 +0100 | [diff] [blame] | 598 | .. js:function:: core.concat() |
| 599 | |
| 600 | **context**: body, init, task, action, sample-fetch, converter |
| 601 | |
| 602 | This function retruns a new concat object. |
| 603 | |
| 604 | :returns: A :ref:`concat_class` object. |
| 605 | |
Thierry FOURNIER | 0a99b89 | 2015-08-26 00:14:17 +0200 | [diff] [blame] | 606 | .. js:function:: core.done(data) |
| 607 | |
| 608 | **context**: body, init, task, action, sample-fetch, converter |
| 609 | |
| 610 | :param any data: Return some data for the caller. It is useful with |
| 611 | sample-fetches and sample-converters. |
| 612 | |
| 613 | Immediately stops the current Lua execution and returns to the caller which |
| 614 | may be a sample fetch, a converter or an action and returns the specified |
| 615 | value (ignored for actions). It is used when the LUA process finishes its |
| 616 | work and wants to give back the control to HAProxy without executing the |
| 617 | remaining code. It can be seen as a multi-level "return". |
| 618 | |
Thierry FOURNIER | 486f5a0 | 2015-03-16 15:13:03 +0100 | [diff] [blame] | 619 | .. js:function:: core.yield() |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 620 | |
| 621 | **context**: task, action, sample-fetch, converter |
| 622 | |
| 623 | Give back the hand at the HAProxy scheduler. It is used when the LUA |
| 624 | processing consumes a lot of processing time. |
| 625 | |
Thierry FOURNIER / OZON.IO | 62fec75 | 2016-11-10 20:38:11 +0100 | [diff] [blame] | 626 | .. js:function:: core.parse_addr(address) |
| 627 | |
| 628 | **context**: body, init, task, action, sample-fetch, converter |
| 629 | |
| 630 | :param network: is a string describing an ipv4 or ipv6 address and optionally |
| 631 | its network length, like this: "127.0.0.1/8" or "aaaa::1234/32". |
| 632 | :returns: a userdata containing network or nil if an error occurs. |
| 633 | |
| 634 | Parse ipv4 or ipv6 adresses and its facultative associated network. |
| 635 | |
| 636 | .. js:function:: core.match_addr(addr1, addr2) |
| 637 | |
| 638 | **context**: body, init, task, action, sample-fetch, converter |
| 639 | |
| 640 | :param addr1: is an address created with "core.parse_addr". |
| 641 | :param addr2: is an address created with "core.parse_addr". |
| 642 | :returns: boolean, true if the network of the addresses matche, else returns |
| 643 | false. |
| 644 | |
| 645 | Match two networks. For example "127.0.0.1/32" matchs "127.0.0.0/8". The order |
| 646 | of network is not important. |
| 647 | |
Thierry FOURNIER / OZON.IO | 8a1027a | 2016-11-24 20:48:38 +0100 | [diff] [blame] | 648 | .. js:function:: core.tokenize(str, separators [, noblank]) |
| 649 | |
| 650 | **context**: body, init, task, action, sample-fetch, converter |
| 651 | |
| 652 | This function is useful for tokenizing an entry, or splitting some messages. |
| 653 | :param string str: The string which will be split. |
| 654 | :param string separators: A string containing a list of separators. |
| 655 | :param boolean noblank: Ignore empty entries. |
| 656 | :returns: an array of string. |
| 657 | |
| 658 | For example: |
| 659 | |
| 660 | .. code-block:: lua |
| 661 | |
| 662 | local array = core.tokenize("This function is useful, for tokenizing an entry.", "., ", true) |
| 663 | print_r(array) |
| 664 | .. |
| 665 | |
| 666 | Returns this array: |
| 667 | |
| 668 | .. code-block:: text |
| 669 | |
| 670 | (table) table: 0x21c01e0 [ |
| 671 | 1: (string) "This" |
| 672 | 2: (string) "function" |
| 673 | 3: (string) "is" |
| 674 | 4: (string) "useful" |
| 675 | 5: (string) "for" |
| 676 | 6: (string) "tokenizing" |
| 677 | 7: (string) "an" |
| 678 | 8: (string) "entry" |
| 679 | ] |
| 680 | .. |
| 681 | |
Thierry Fournier | f61aa63 | 2016-02-19 20:56:00 +0100 | [diff] [blame] | 682 | .. _proxy_class: |
| 683 | |
| 684 | Proxy class |
| 685 | ============ |
| 686 | |
| 687 | .. js:class:: Proxy |
| 688 | |
| 689 | This class provides a way for manipulating proxy and retrieving information |
| 690 | like statistics. |
| 691 | |
Thierry Fournier | f2fdc9d | 2016-02-22 08:21:39 +0100 | [diff] [blame] | 692 | .. js:attribute:: Proxy.servers |
| 693 | |
| 694 | Contain an array with the attached servers. Each server entry is an object of |
| 695 | type :ref:`server_class`. |
| 696 | |
Thierry Fournier | ff48042 | 2016-02-25 08:36:46 +0100 | [diff] [blame] | 697 | .. js:attribute:: Proxy.listeners |
| 698 | |
| 699 | Contain an array with the attached listeners. Each listeners entry is an |
| 700 | object of type :ref:`listener_class`. |
| 701 | |
Thierry Fournier | f61aa63 | 2016-02-19 20:56:00 +0100 | [diff] [blame] | 702 | .. js:function:: Proxy.pause(px) |
| 703 | |
| 704 | Pause the proxy. See the management socket documentation for more information. |
| 705 | |
| 706 | :param class_proxy px: A :ref:`proxy_class` which indicates the manipulated |
| 707 | proxy. |
| 708 | |
| 709 | .. js:function:: Proxy.resume(px) |
| 710 | |
| 711 | Resume the proxy. See the management socket documentation for more |
| 712 | information. |
| 713 | |
| 714 | :param class_proxy px: A :ref:`proxy_class` which indicates the manipulated |
| 715 | proxy. |
| 716 | |
| 717 | .. js:function:: Proxy.stop(px) |
| 718 | |
| 719 | Stop the proxy. See the management socket documentation for more information. |
| 720 | |
| 721 | :param class_proxy px: A :ref:`proxy_class` which indicates the manipulated |
| 722 | proxy. |
| 723 | |
| 724 | .. js:function:: Proxy.shut_bcksess(px) |
| 725 | |
| 726 | Kill the session attached to a backup server. See the management socket |
| 727 | documentation for more information. |
| 728 | |
| 729 | :param class_proxy px: A :ref:`proxy_class` which indicates the manipulated |
| 730 | proxy. |
| 731 | |
| 732 | .. js:function:: Proxy.get_cap(px) |
| 733 | |
| 734 | Returns a string describing the capabilities of the proxy. |
| 735 | |
| 736 | :param class_proxy px: A :ref:`proxy_class` which indicates the manipulated |
| 737 | proxy. |
| 738 | :returns: a string "frontend", "backend", "proxy" or "ruleset". |
| 739 | |
| 740 | .. js:function:: Proxy.get_mode(px) |
| 741 | |
| 742 | Returns a string describing the mode of the current proxy. |
| 743 | |
| 744 | :param class_proxy px: A :ref:`proxy_class` which indicates the manipulated |
| 745 | proxy. |
| 746 | :returns: a string "tcp", "http", "health" or "unknown" |
| 747 | |
| 748 | .. js:function:: Proxy.get_stats(px) |
| 749 | |
| 750 | Returns an array containg the proxy statistics. The statistics returned are |
| 751 | not the same if the proxy is frontend or a backend. |
| 752 | |
| 753 | :param class_proxy px: A :ref:`proxy_class` which indicates the manipulated |
| 754 | proxy. |
| 755 | :returns: a key/value array containing stats |
| 756 | |
Thierry Fournier | f2fdc9d | 2016-02-22 08:21:39 +0100 | [diff] [blame] | 757 | .. _server_class: |
| 758 | |
| 759 | Server class |
| 760 | ============ |
| 761 | |
| 762 | .. js:function:: Server.is_draining(sv) |
| 763 | |
| 764 | Return true if the server is currently draining stiky connections. |
| 765 | |
| 766 | :param class_server sv: A :ref:`server_class` which indicates the manipulated |
| 767 | server. |
| 768 | :returns: a boolean |
| 769 | |
| 770 | .. js:function:: Server.set_weight(sv, weight) |
| 771 | |
| 772 | Dynamically change the weight of the serveur. See the management socket |
| 773 | documentation for more information about the format of the string. |
| 774 | |
| 775 | :param class_server sv: A :ref:`server_class` which indicates the manipulated |
| 776 | server. |
| 777 | :param string weight: A string describing the server weight. |
| 778 | |
| 779 | .. js:function:: Server.get_weight(sv) |
| 780 | |
| 781 | This function returns an integer representing the serveur weight. |
| 782 | |
| 783 | :param class_server sv: A :ref:`server_class` which indicates the manipulated |
| 784 | server. |
| 785 | :returns: an integer. |
| 786 | |
| 787 | .. js:function:: Server.set_addr(sv, addr) |
| 788 | |
| 789 | Dynamically change the address of the serveur. See the management socket |
| 790 | documentation for more information about the format of the string. |
| 791 | |
| 792 | :param class_server sv: A :ref:`server_class` which indicates the manipulated |
| 793 | server. |
| 794 | :param string weight: A string describing the server address. |
| 795 | |
| 796 | .. js:function:: Server.get_addr(sv) |
| 797 | |
| 798 | Returns a string describing the address of the serveur. |
| 799 | |
| 800 | :param class_server sv: A :ref:`server_class` which indicates the manipulated |
| 801 | server. |
| 802 | :returns: A string |
| 803 | |
| 804 | .. js:function:: Server.get_stats(sv) |
| 805 | |
| 806 | Returns server statistics. |
| 807 | |
| 808 | :param class_server sv: A :ref:`server_class` which indicates the manipulated |
| 809 | server. |
| 810 | :returns: a key/value array containing stats |
| 811 | |
| 812 | .. js:function:: Server.shut_sess(sv) |
| 813 | |
| 814 | Shutdown all the sessions attached to the server. See the management socket |
| 815 | documentation for more information about this function. |
| 816 | |
| 817 | :param class_server sv: A :ref:`server_class` which indicates the manipulated |
| 818 | server. |
| 819 | |
| 820 | .. js:function:: Server.set_drain(sv) |
| 821 | |
| 822 | Drain sticky sessions. See the management socket documentation for more |
| 823 | information about this function. |
| 824 | |
| 825 | :param class_server sv: A :ref:`server_class` which indicates the manipulated |
| 826 | server. |
| 827 | |
| 828 | .. js:function:: Server.set_maint(sv) |
| 829 | |
| 830 | Set maintenance mode. See the management socket documentation for more |
| 831 | information about this function. |
| 832 | |
| 833 | :param class_server sv: A :ref:`server_class` which indicates the manipulated |
| 834 | server. |
| 835 | |
| 836 | .. js:function:: Server.set_ready(sv) |
| 837 | |
| 838 | Set normal mode. See the management socket documentation for more information |
| 839 | about this function. |
| 840 | |
| 841 | :param class_server sv: A :ref:`server_class` which indicates the manipulated |
| 842 | server. |
| 843 | |
| 844 | .. js:function:: Server.check_enable(sv) |
| 845 | |
| 846 | Enable health checks. See the management socket documentation for more |
| 847 | information about this function. |
| 848 | |
| 849 | :param class_server sv: A :ref:`server_class` which indicates the manipulated |
| 850 | server. |
| 851 | |
| 852 | .. js:function:: Server.check_disable(sv) |
| 853 | |
| 854 | Disable health checks. See the management socket documentation for more |
| 855 | information about this function. |
| 856 | |
| 857 | :param class_server sv: A :ref:`server_class` which indicates the manipulated |
| 858 | server. |
| 859 | |
| 860 | .. js:function:: Server.check_force_up(sv) |
| 861 | |
| 862 | Force health-check up. See the management socket documentation for more |
| 863 | information about this function. |
| 864 | |
| 865 | :param class_server sv: A :ref:`server_class` which indicates the manipulated |
| 866 | server. |
| 867 | |
| 868 | .. js:function:: Server.check_force_nolb(sv) |
| 869 | |
| 870 | Force health-check nolb mode. See the management socket documentation for more |
| 871 | information about this function. |
| 872 | |
| 873 | :param class_server sv: A :ref:`server_class` which indicates the manipulated |
| 874 | server. |
| 875 | |
| 876 | .. js:function:: Server.check_force_down(sv) |
| 877 | |
| 878 | Force health-check down. See the management socket documentation for more |
| 879 | information about this function. |
| 880 | |
| 881 | :param class_server sv: A :ref:`server_class` which indicates the manipulated |
| 882 | server. |
| 883 | |
| 884 | .. js:function:: Server.agent_enable(sv) |
| 885 | |
| 886 | Enable agent check. See the management socket documentation for more |
| 887 | information about this function. |
| 888 | |
| 889 | :param class_server sv: A :ref:`server_class` which indicates the manipulated |
| 890 | server. |
| 891 | |
| 892 | .. js:function:: Server.agent_disable(sv) |
| 893 | |
| 894 | Disable agent check. See the management socket documentation for more |
| 895 | information about this function. |
| 896 | |
| 897 | :param class_server sv: A :ref:`server_class` which indicates the manipulated |
| 898 | server. |
| 899 | |
| 900 | .. js:function:: Server.agent_force_up(sv) |
| 901 | |
| 902 | Force agent check up. See the management socket documentation for more |
| 903 | information about this function. |
| 904 | |
| 905 | :param class_server sv: A :ref:`server_class` which indicates the manipulated |
| 906 | server. |
| 907 | |
| 908 | .. js:function:: Server.agent_force_down(sv) |
| 909 | |
| 910 | Force agent check down. See the management socket documentation for more |
| 911 | information about this function. |
| 912 | |
| 913 | :param class_server sv: A :ref:`server_class` which indicates the manipulated |
| 914 | server. |
| 915 | |
Thierry Fournier | ff48042 | 2016-02-25 08:36:46 +0100 | [diff] [blame] | 916 | .. _listener_class: |
| 917 | |
| 918 | Listener class |
| 919 | ============== |
| 920 | |
| 921 | .. js:function:: Listener.get_stats(ls) |
| 922 | |
| 923 | Returns server statistics. |
| 924 | |
| 925 | :param class_listener ls: A :ref:`listener_class` which indicates the |
| 926 | manipulated listener. |
| 927 | :returns: a key/value array containing stats |
| 928 | |
Thierry Fournier | 1de1659 | 2016-01-27 09:49:07 +0100 | [diff] [blame] | 929 | .. _concat_class: |
| 930 | |
| 931 | Concat class |
| 932 | ============ |
| 933 | |
| 934 | .. js:class:: Concat |
| 935 | |
| 936 | This class provides a fast way for string concatenation. The way using native |
| 937 | Lua concatenation like the code below is slow for some reasons. |
| 938 | |
| 939 | .. code-block:: lua |
| 940 | |
| 941 | str = "string1" |
| 942 | str = str .. ", string2" |
| 943 | str = str .. ", string3" |
| 944 | .. |
| 945 | |
| 946 | For each concatenation, Lua: |
| 947 | * allocate memory for the result, |
| 948 | * catenate the two string copying the strings in the new memory bloc, |
| 949 | * free the old memory block containing the string whoch is no longer used. |
| 950 | This process does many memory move, allocation and free. In addition, the |
| 951 | memory is not really freed, it is just mark mark as unsused and wait for the |
| 952 | garbage collector. |
| 953 | |
| 954 | The Concat class provide an alternative way for catenating strings. It uses |
| 955 | the internal Lua mechanism (it does not allocate memory), but it doesn't copy |
| 956 | the data more than once. |
| 957 | |
| 958 | On my computer, the following loops spends 0.2s for the Concat method and |
| 959 | 18.5s for the pure Lua implementation. So, the Concat class is about 1000x |
| 960 | faster than the embedded solution. |
| 961 | |
| 962 | .. code-block:: lua |
| 963 | |
| 964 | for j = 1, 100 do |
| 965 | c = core.concat() |
| 966 | for i = 1, 20000 do |
| 967 | c:add("#####") |
| 968 | end |
| 969 | end |
| 970 | .. |
| 971 | |
| 972 | .. code-block:: lua |
| 973 | |
| 974 | for j = 1, 100 do |
| 975 | c = "" |
| 976 | for i = 1, 20000 do |
| 977 | c = c .. "#####" |
| 978 | end |
| 979 | end |
| 980 | .. |
| 981 | |
| 982 | .. js:function:: Concat.add(concat, string) |
| 983 | |
| 984 | This function adds a string to the current concatenated string. |
| 985 | |
| 986 | :param class_concat concat: A :ref:`concat_class` which contains the currently |
| 987 | builded string. |
| 988 | :param string string: A new string to concatenate to the current builded |
| 989 | string. |
| 990 | |
| 991 | .. js:function:: Concat.dump(concat) |
| 992 | |
| 993 | This function returns the concanated string. |
| 994 | |
| 995 | :param class_concat concat: A :ref:`concat_class` which contains the currently |
| 996 | builded string. |
| 997 | :returns: the concatenated string |
| 998 | |
Thierry FOURNIER | dc59500 | 2015-12-21 11:13:52 +0100 | [diff] [blame] | 999 | .. _fetches_class: |
| 1000 | |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 1001 | Fetches class |
| 1002 | ============= |
| 1003 | |
| 1004 | .. js:class:: Fetches |
| 1005 | |
| 1006 | This class contains a lot of internal HAProxy sample fetches. See the |
Thierry FOURNIER | 2e4893c | 2015-03-18 13:37:27 +0100 | [diff] [blame] | 1007 | HAProxy "configuration.txt" documentation for more information about her |
| 1008 | usage. they are the chapters 7.3.2 to 7.3.6. |
| 1009 | |
Thierry FOURNIER | dc59500 | 2015-12-21 11:13:52 +0100 | [diff] [blame] | 1010 | **warning** some sample fetches are not available in some context. These |
| 1011 | limitations are specified in this documentation when theire useful. |
| 1012 | |
Thierry FOURNIER | 2e4893c | 2015-03-18 13:37:27 +0100 | [diff] [blame] | 1013 | :see: TXN.f |
| 1014 | :see: TXN.sf |
| 1015 | |
| 1016 | Fetches are useful for: |
| 1017 | |
| 1018 | * get system time, |
| 1019 | * get environment variable, |
| 1020 | * get random numbers, |
| 1021 | * known backend status like the number of users in queue or the number of |
| 1022 | connections established, |
| 1023 | * client information like ip source or destination, |
| 1024 | * deal with stick tables, |
| 1025 | * Established SSL informations, |
| 1026 | * HTTP information like headers or method. |
| 1027 | |
| 1028 | .. code-block:: lua |
| 1029 | |
Thierry FOURNIER | dc59500 | 2015-12-21 11:13:52 +0100 | [diff] [blame] | 1030 | function action(txn) |
| 1031 | -- Get source IP |
| 1032 | local clientip = txn.f:src() |
| 1033 | end |
Thierry FOURNIER | 2e4893c | 2015-03-18 13:37:27 +0100 | [diff] [blame] | 1034 | .. |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 1035 | |
Thierry FOURNIER | dc59500 | 2015-12-21 11:13:52 +0100 | [diff] [blame] | 1036 | .. _converters_class: |
| 1037 | |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 1038 | Converters class |
| 1039 | ================ |
| 1040 | |
| 1041 | .. js:class:: Converters |
| 1042 | |
| 1043 | This class contains a lot of internal HAProxy sample converters. See the |
Thierry FOURNIER | 2e4893c | 2015-03-18 13:37:27 +0100 | [diff] [blame] | 1044 | HAProxy documentation "configuration.txt" for more information about her |
| 1045 | usage. Its the chapter 7.3.1. |
| 1046 | |
| 1047 | :see: TXN.c |
| 1048 | :see: TXN.sc |
| 1049 | |
| 1050 | Converters provides statefull transformation. They are useful for: |
| 1051 | |
| 1052 | * converting input to base64, |
| 1053 | * applying hash on input string (djb2, crc32, sdbm, wt6), |
| 1054 | * format date, |
| 1055 | * json escape, |
Pieter Baauw | 4d7f766 | 2015-11-08 16:38:08 +0100 | [diff] [blame] | 1056 | * extracting preferred language comparing two lists, |
Thierry FOURNIER | 2e4893c | 2015-03-18 13:37:27 +0100 | [diff] [blame] | 1057 | * turn to lower or upper chars, |
| 1058 | * deal with stick tables. |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 1059 | |
Thierry FOURNIER | dc59500 | 2015-12-21 11:13:52 +0100 | [diff] [blame] | 1060 | .. _channel_class: |
| 1061 | |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 1062 | Channel class |
| 1063 | ============= |
| 1064 | |
| 1065 | .. js:class:: Channel |
| 1066 | |
| 1067 | HAProxy uses two buffers for the processing of the requests. The first one is |
| 1068 | used with the request data (from the client to the server) and the second is |
| 1069 | used for the response data (from the server to the client). |
| 1070 | |
| 1071 | Each buffer contains two types of data. The first type is the incoming data |
| 1072 | waiting for a processing. The second part is the outgoing data already |
| 1073 | processed. Usually, the incoming data is processed, after it is tagged as |
| 1074 | outgoing data, and finally it is sent. The following functions provides tools |
| 1075 | for manipulating these data in a buffer. |
| 1076 | |
| 1077 | The following diagram shows where the channel class function are applied. |
| 1078 | |
| 1079 | **Warning**: It is not possible to read from the response in request action, |
| 1080 | and it is not possible to read for the request channel in response action. |
| 1081 | |
Thierry FOURNIER | 2e4893c | 2015-03-18 13:37:27 +0100 | [diff] [blame] | 1082 | .. image:: _static/channel.png |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 1083 | |
Thierry FOURNIER | 486f5a0 | 2015-03-16 15:13:03 +0100 | [diff] [blame] | 1084 | .. js:function:: Channel.dup(channel) |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 1085 | |
| 1086 | This function returns a string that contain the entire buffer. The data is |
| 1087 | not remove from the buffer and can be reprocessed later. |
| 1088 | |
| 1089 | If the buffer cant receive more data, a 'nil' value is returned. |
| 1090 | |
Thierry FOURNIER | 486f5a0 | 2015-03-16 15:13:03 +0100 | [diff] [blame] | 1091 | :param class_channel channel: The manipulated Channel. |
Pieter Baauw | 4d7f766 | 2015-11-08 16:38:08 +0100 | [diff] [blame] | 1092 | :returns: a string containing all the available data or nil. |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 1093 | |
Thierry FOURNIER | 486f5a0 | 2015-03-16 15:13:03 +0100 | [diff] [blame] | 1094 | .. js:function:: Channel.get(channel) |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 1095 | |
| 1096 | This function returns a string that contain the entire buffer. The data is |
| 1097 | consumed from the buffer. |
| 1098 | |
| 1099 | If the buffer cant receive more data, a 'nil' value is returned. |
| 1100 | |
Thierry FOURNIER | 486f5a0 | 2015-03-16 15:13:03 +0100 | [diff] [blame] | 1101 | :param class_channel channel: The manipulated Channel. |
Pieter Baauw | 4d7f766 | 2015-11-08 16:38:08 +0100 | [diff] [blame] | 1102 | :returns: a string containing all the available data or nil. |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 1103 | |
Thierry FOURNIER | a3bc513 | 2015-09-25 21:43:56 +0200 | [diff] [blame] | 1104 | .. js:function:: Channel.getline(channel) |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 1105 | |
| 1106 | This function returns a string that contain the first line of the buffer. The |
| 1107 | data is consumed. If the data returned doesn't contains a final '\n' its |
| 1108 | assumed than its the last available data in the buffer. |
| 1109 | |
| 1110 | If the buffer cant receive more data, a 'nil' value is returned. |
| 1111 | |
Thierry FOURNIER | 486f5a0 | 2015-03-16 15:13:03 +0100 | [diff] [blame] | 1112 | :param class_channel channel: The manipulated Channel. |
Pieter Baauw | 386a127 | 2015-08-16 15:26:24 +0200 | [diff] [blame] | 1113 | :returns: a string containing the available line or nil. |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 1114 | |
Thierry FOURNIER | 486f5a0 | 2015-03-16 15:13:03 +0100 | [diff] [blame] | 1115 | .. js:function:: Channel.set(channel, string) |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 1116 | |
| 1117 | This function replace the content of the buffer by the string. The function |
| 1118 | returns the copied length, otherwise, it returns -1. |
| 1119 | |
| 1120 | The data set with this function are not send. They wait for the end of |
| 1121 | HAProxy processing, so the buffer can be full. |
| 1122 | |
Thierry FOURNIER | 486f5a0 | 2015-03-16 15:13:03 +0100 | [diff] [blame] | 1123 | :param class_channel channel: The manipulated Channel. |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 1124 | :param string string: The data which will sent. |
Pieter Baauw | 4d7f766 | 2015-11-08 16:38:08 +0100 | [diff] [blame] | 1125 | :returns: an integer containing the amount of bytes copied or -1. |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 1126 | |
Thierry FOURNIER | 486f5a0 | 2015-03-16 15:13:03 +0100 | [diff] [blame] | 1127 | .. js:function:: Channel.append(channel, string) |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 1128 | |
| 1129 | This function append the string argument to the content of the buffer. The |
| 1130 | function returns the copied length, otherwise, it returns -1. |
| 1131 | |
| 1132 | The data set with this function are not send. They wait for the end of |
| 1133 | HAProxy processing, so the buffer can be full. |
| 1134 | |
Thierry FOURNIER | 486f5a0 | 2015-03-16 15:13:03 +0100 | [diff] [blame] | 1135 | :param class_channel channel: The manipulated Channel. |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 1136 | :param string string: The data which will sent. |
Pieter Baauw | 4d7f766 | 2015-11-08 16:38:08 +0100 | [diff] [blame] | 1137 | :returns: an integer containing the amount of bytes copied or -1. |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 1138 | |
Thierry FOURNIER | 486f5a0 | 2015-03-16 15:13:03 +0100 | [diff] [blame] | 1139 | .. js:function:: Channel.send(channel, string) |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 1140 | |
| 1141 | This function required immediate send of the data. Unless if the connection |
| 1142 | is close, the buffer is regularly flushed and all the string can be sent. |
| 1143 | |
Thierry FOURNIER | 486f5a0 | 2015-03-16 15:13:03 +0100 | [diff] [blame] | 1144 | :param class_channel channel: The manipulated Channel. |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 1145 | :param string string: The data which will sent. |
Pieter Baauw | 4d7f766 | 2015-11-08 16:38:08 +0100 | [diff] [blame] | 1146 | :returns: an integer containing the amount of bytes copied or -1. |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 1147 | |
Thierry FOURNIER | 486f5a0 | 2015-03-16 15:13:03 +0100 | [diff] [blame] | 1148 | .. js:function:: Channel.get_in_length(channel) |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 1149 | |
| 1150 | This function returns the length of the input part of the buffer. |
| 1151 | |
Thierry FOURNIER | 486f5a0 | 2015-03-16 15:13:03 +0100 | [diff] [blame] | 1152 | :param class_channel channel: The manipulated Channel. |
Pieter Baauw | 4d7f766 | 2015-11-08 16:38:08 +0100 | [diff] [blame] | 1153 | :returns: an integer containing the amount of available bytes. |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 1154 | |
Thierry FOURNIER | 486f5a0 | 2015-03-16 15:13:03 +0100 | [diff] [blame] | 1155 | .. js:function:: Channel.get_out_length(channel) |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 1156 | |
| 1157 | This function returns the length of the output part of the buffer. |
| 1158 | |
Thierry FOURNIER | 486f5a0 | 2015-03-16 15:13:03 +0100 | [diff] [blame] | 1159 | :param class_channel channel: The manipulated Channel. |
Pieter Baauw | 4d7f766 | 2015-11-08 16:38:08 +0100 | [diff] [blame] | 1160 | :returns: an integer containing the amount of available bytes. |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 1161 | |
Thierry FOURNIER | 486f5a0 | 2015-03-16 15:13:03 +0100 | [diff] [blame] | 1162 | .. js:function:: Channel.forward(channel, int) |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 1163 | |
| 1164 | This function transfer bytes from the input part of the buffer to the output |
| 1165 | part. |
| 1166 | |
Thierry FOURNIER | 486f5a0 | 2015-03-16 15:13:03 +0100 | [diff] [blame] | 1167 | :param class_channel channel: The manipulated Channel. |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 1168 | :param integer int: The amount of data which will be forwarded. |
| 1169 | |
Thierry FOURNIER / OZON.IO | 65192f3 | 2016-11-07 15:28:40 +0100 | [diff] [blame] | 1170 | .. js:function:: Channel.is_full(channel) |
| 1171 | |
| 1172 | This function returns true if the buffer channel is full. |
| 1173 | |
| 1174 | :returns: a boolean |
Thierry FOURNIER | dc59500 | 2015-12-21 11:13:52 +0100 | [diff] [blame] | 1175 | |
| 1176 | .. _http_class: |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 1177 | |
| 1178 | HTTP class |
| 1179 | ========== |
| 1180 | |
| 1181 | .. js:class:: HTTP |
| 1182 | |
| 1183 | This class contain all the HTTP manipulation functions. |
| 1184 | |
Pieter Baauw | 386a127 | 2015-08-16 15:26:24 +0200 | [diff] [blame] | 1185 | .. js:function:: HTTP.req_get_headers(http) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 1186 | |
| 1187 | Returns an array containing all the request headers. |
| 1188 | |
| 1189 | :param class_http http: The related http object. |
| 1190 | :returns: array of headers. |
Pieter Baauw | 386a127 | 2015-08-16 15:26:24 +0200 | [diff] [blame] | 1191 | :see: HTTP.res_get_headers() |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 1192 | |
Thierry FOURNIER | dc59500 | 2015-12-21 11:13:52 +0100 | [diff] [blame] | 1193 | This is the form of the returned array: |
| 1194 | |
| 1195 | .. code-block:: lua |
| 1196 | |
| 1197 | HTTP:req_get_headers()['<header-name>'][<header-index>] = "<header-value>" |
| 1198 | |
| 1199 | local hdr = HTTP:req_get_headers() |
| 1200 | hdr["host"][0] = "www.test.com" |
| 1201 | hdr["accept"][0] = "audio/basic q=1" |
| 1202 | hdr["accept"][1] = "audio/*, q=0.2" |
| 1203 | hdr["accept"][2] = "*/*, q=0.1" |
| 1204 | .. |
| 1205 | |
Pieter Baauw | 386a127 | 2015-08-16 15:26:24 +0200 | [diff] [blame] | 1206 | .. js:function:: HTTP.res_get_headers(http) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 1207 | |
| 1208 | Returns an array containing all the response headers. |
| 1209 | |
| 1210 | :param class_http http: The related http object. |
| 1211 | :returns: array of headers. |
Pieter Baauw | 386a127 | 2015-08-16 15:26:24 +0200 | [diff] [blame] | 1212 | :see: HTTP.req_get_headers() |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 1213 | |
Thierry FOURNIER | dc59500 | 2015-12-21 11:13:52 +0100 | [diff] [blame] | 1214 | This is the form of the returned array: |
| 1215 | |
| 1216 | .. code-block:: lua |
| 1217 | |
| 1218 | HTTP:res_get_headers()['<header-name>'][<header-index>] = "<header-value>" |
| 1219 | |
| 1220 | local hdr = HTTP:req_get_headers() |
| 1221 | hdr["host"][0] = "www.test.com" |
| 1222 | hdr["accept"][0] = "audio/basic q=1" |
| 1223 | hdr["accept"][1] = "audio/*, q=0.2" |
| 1224 | hdr["accept"][2] = "*.*, q=0.1" |
| 1225 | .. |
| 1226 | |
Thierry FOURNIER | 486f5a0 | 2015-03-16 15:13:03 +0100 | [diff] [blame] | 1227 | .. js:function:: HTTP.req_add_header(http, name, value) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 1228 | |
| 1229 | Appends an HTTP header field in the request whose name is |
| 1230 | specified in "name" and whose value is defined in "value". |
| 1231 | |
| 1232 | :param class_http http: The related http object. |
| 1233 | :param string name: The header name. |
| 1234 | :param string value: The header value. |
Thierry FOURNIER | 486f5a0 | 2015-03-16 15:13:03 +0100 | [diff] [blame] | 1235 | :see: HTTP.res_add_header() |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 1236 | |
Thierry FOURNIER | 486f5a0 | 2015-03-16 15:13:03 +0100 | [diff] [blame] | 1237 | .. js:function:: HTTP.res_add_header(http, name, value) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 1238 | |
| 1239 | appends an HTTP header field in the response whose name is |
| 1240 | specified in "name" and whose value is defined in "value". |
| 1241 | |
| 1242 | :param class_http http: The related http object. |
| 1243 | :param string name: The header name. |
| 1244 | :param string value: The header value. |
Thierry FOURNIER | 486f5a0 | 2015-03-16 15:13:03 +0100 | [diff] [blame] | 1245 | :see: HTTP.req_add_header() |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 1246 | |
Thierry FOURNIER | 486f5a0 | 2015-03-16 15:13:03 +0100 | [diff] [blame] | 1247 | .. js:function:: HTTP.req_del_header(http, name) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 1248 | |
| 1249 | Removes all HTTP header fields in the request whose name is |
| 1250 | specified in "name". |
| 1251 | |
| 1252 | :param class_http http: The related http object. |
| 1253 | :param string name: The header name. |
Thierry FOURNIER | 486f5a0 | 2015-03-16 15:13:03 +0100 | [diff] [blame] | 1254 | :see: HTTP.res_del_header() |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 1255 | |
Thierry FOURNIER | 486f5a0 | 2015-03-16 15:13:03 +0100 | [diff] [blame] | 1256 | .. js:function:: HTTP.res_del_header(http, name) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 1257 | |
| 1258 | Removes all HTTP header fields in the response whose name is |
| 1259 | specified in "name". |
| 1260 | |
| 1261 | :param class_http http: The related http object. |
| 1262 | :param string name: The header name. |
Thierry FOURNIER | 486f5a0 | 2015-03-16 15:13:03 +0100 | [diff] [blame] | 1263 | :see: HTTP.req_del_header() |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 1264 | |
Thierry FOURNIER | 486f5a0 | 2015-03-16 15:13:03 +0100 | [diff] [blame] | 1265 | .. js:function:: HTTP.req_set_header(http, name, value) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 1266 | |
| 1267 | This variable replace all occurence of all header "name", by only |
| 1268 | one containing the "value". |
| 1269 | |
| 1270 | :param class_http http: The related http object. |
| 1271 | :param string name: The header name. |
| 1272 | :param string value: The header value. |
Thierry FOURNIER | 486f5a0 | 2015-03-16 15:13:03 +0100 | [diff] [blame] | 1273 | :see: HTTP.res_set_header() |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 1274 | |
| 1275 | This function does the same work as the folowwing code: |
| 1276 | |
| 1277 | .. code-block:: lua |
| 1278 | |
| 1279 | function fcn(txn) |
Thierry FOURNIER | 486f5a0 | 2015-03-16 15:13:03 +0100 | [diff] [blame] | 1280 | TXN.http:req_del_header("header") |
| 1281 | TXN.http:req_add_header("header", "value") |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 1282 | end |
| 1283 | .. |
| 1284 | |
Thierry FOURNIER | 486f5a0 | 2015-03-16 15:13:03 +0100 | [diff] [blame] | 1285 | .. js:function:: HTTP.res_set_header(http, name, value) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 1286 | |
| 1287 | This variable replace all occurence of all header "name", by only |
| 1288 | one containing the "value". |
| 1289 | |
| 1290 | :param class_http http: The related http object. |
| 1291 | :param string name: The header name. |
| 1292 | :param string value: The header value. |
Pieter Baauw | 386a127 | 2015-08-16 15:26:24 +0200 | [diff] [blame] | 1293 | :see: HTTP.req_rep_header() |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 1294 | |
Pieter Baauw | 386a127 | 2015-08-16 15:26:24 +0200 | [diff] [blame] | 1295 | .. js:function:: HTTP.req_rep_header(http, name, regex, replace) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 1296 | |
| 1297 | Matches the regular expression in all occurrences of header field "name" |
| 1298 | according to "regex", and replaces them with the "replace" argument. The |
| 1299 | replacement value can contain back references like \1, \2, ... This |
| 1300 | function works with the request. |
| 1301 | |
| 1302 | :param class_http http: The related http object. |
| 1303 | :param string name: The header name. |
| 1304 | :param string regex: The match regular expression. |
| 1305 | :param string replace: The replacement value. |
Pieter Baauw | 386a127 | 2015-08-16 15:26:24 +0200 | [diff] [blame] | 1306 | :see: HTTP.res_rep_header() |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 1307 | |
Pieter Baauw | 386a127 | 2015-08-16 15:26:24 +0200 | [diff] [blame] | 1308 | .. js:function:: HTTP.res_rep_header(http, name, regex, string) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 1309 | |
| 1310 | Matches the regular expression in all occurrences of header field "name" |
| 1311 | according to "regex", and replaces them with the "replace" argument. The |
| 1312 | replacement value can contain back references like \1, \2, ... This |
| 1313 | function works with the request. |
| 1314 | |
| 1315 | :param class_http http: The related http object. |
| 1316 | :param string name: The header name. |
| 1317 | :param string regex: The match regular expression. |
| 1318 | :param string replace: The replacement value. |
Thierry FOURNIER | 486f5a0 | 2015-03-16 15:13:03 +0100 | [diff] [blame] | 1319 | :see: HTTP.req_replace_header() |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 1320 | |
Thierry FOURNIER | 486f5a0 | 2015-03-16 15:13:03 +0100 | [diff] [blame] | 1321 | .. js:function:: HTTP.req_set_method(http, method) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 1322 | |
| 1323 | Rewrites the request method with the parameter "method". |
| 1324 | |
| 1325 | :param class_http http: The related http object. |
| 1326 | :param string method: The new method. |
| 1327 | |
Thierry FOURNIER | 486f5a0 | 2015-03-16 15:13:03 +0100 | [diff] [blame] | 1328 | .. js:function:: HTTP.req_set_path(http, path) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 1329 | |
| 1330 | Rewrites the request path with the "path" parameter. |
| 1331 | |
| 1332 | :param class_http http: The related http object. |
| 1333 | :param string path: The new path. |
| 1334 | |
Thierry FOURNIER | 486f5a0 | 2015-03-16 15:13:03 +0100 | [diff] [blame] | 1335 | .. js:function:: HTTP.req_set_query(http, query) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 1336 | |
| 1337 | Rewrites the request's query string which appears after the first question |
| 1338 | mark ("?") with the parameter "query". |
| 1339 | |
| 1340 | :param class_http http: The related http object. |
| 1341 | :param string query: The new query. |
| 1342 | |
Thierry FOURNIER | 0d79cf6 | 2015-08-26 14:20:58 +0200 | [diff] [blame] | 1343 | .. js:function:: HTTP.req_set_uri(http, uri) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 1344 | |
| 1345 | Rewrites the request URI with the parameter "uri". |
| 1346 | |
| 1347 | :param class_http http: The related http object. |
| 1348 | :param string uri: The new uri. |
| 1349 | |
Thierry FOURNIER | 35d70ef | 2015-08-26 16:21:56 +0200 | [diff] [blame] | 1350 | .. js:function:: HTTP.res_set_status(http, status) |
| 1351 | |
| 1352 | Rewrites the response status code with the parameter "code". Note that the |
| 1353 | reason is automatically adapted to the new code. |
| 1354 | |
| 1355 | :param class_http http: The related http object. |
| 1356 | :param integer status: The new response status code. |
| 1357 | |
Thierry FOURNIER | dc59500 | 2015-12-21 11:13:52 +0100 | [diff] [blame] | 1358 | .. _txn_class: |
| 1359 | |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 1360 | TXN class |
| 1361 | ========= |
| 1362 | |
| 1363 | .. js:class:: TXN |
| 1364 | |
| 1365 | The txn class contain all the functions relative to the http or tcp |
| 1366 | transaction (Note than a tcp stream is the same than a tcp transaction, but |
| 1367 | an HTTP transaction is not the same than a tcp stream). |
| 1368 | |
| 1369 | The usage of this class permits to retrieve data from the requests, alter it |
| 1370 | and forward it. |
| 1371 | |
| 1372 | All the functions provided by this class are available in the context |
| 1373 | **sample-fetches** and **actions**. |
| 1374 | |
Thierry FOURNIER | 486f5a0 | 2015-03-16 15:13:03 +0100 | [diff] [blame] | 1375 | .. js:attribute:: TXN.c |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 1376 | |
Thierry FOURNIER | dc59500 | 2015-12-21 11:13:52 +0100 | [diff] [blame] | 1377 | :returns: An :ref:`converters_class`. |
| 1378 | |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 1379 | This attribute contains a Converters class object. |
| 1380 | |
Thierry FOURNIER | 486f5a0 | 2015-03-16 15:13:03 +0100 | [diff] [blame] | 1381 | .. js:attribute:: TXN.sc |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 1382 | |
Thierry FOURNIER | dc59500 | 2015-12-21 11:13:52 +0100 | [diff] [blame] | 1383 | :returns: An :ref:`converters_class`. |
| 1384 | |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 1385 | This attribute contains a Converters class object. The functions of |
| 1386 | this object returns always a string. |
| 1387 | |
Thierry FOURNIER | 486f5a0 | 2015-03-16 15:13:03 +0100 | [diff] [blame] | 1388 | .. js:attribute:: TXN.f |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 1389 | |
Thierry FOURNIER | dc59500 | 2015-12-21 11:13:52 +0100 | [diff] [blame] | 1390 | :returns: An :ref:`fetches_class`. |
| 1391 | |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 1392 | This attribute contains a Fetches class object. |
| 1393 | |
Thierry FOURNIER | 486f5a0 | 2015-03-16 15:13:03 +0100 | [diff] [blame] | 1394 | .. js:attribute:: TXN.sf |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 1395 | |
Thierry FOURNIER | dc59500 | 2015-12-21 11:13:52 +0100 | [diff] [blame] | 1396 | :returns: An :ref:`fetches_class`. |
| 1397 | |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 1398 | This attribute contains a Fetches class object. The functions of |
| 1399 | this object returns always a string. |
| 1400 | |
Thierry FOURNIER | 486f5a0 | 2015-03-16 15:13:03 +0100 | [diff] [blame] | 1401 | .. js:attribute:: TXN.req |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 1402 | |
Thierry FOURNIER | dc59500 | 2015-12-21 11:13:52 +0100 | [diff] [blame] | 1403 | :returns: An :ref:`channel_class`. |
| 1404 | |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 1405 | This attribute contains a channel class object for the request buffer. |
| 1406 | |
Thierry FOURNIER | 486f5a0 | 2015-03-16 15:13:03 +0100 | [diff] [blame] | 1407 | .. js:attribute:: TXN.res |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 1408 | |
Thierry FOURNIER | dc59500 | 2015-12-21 11:13:52 +0100 | [diff] [blame] | 1409 | :returns: An :ref:`channel_class`. |
| 1410 | |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 1411 | This attribute contains a channel class object for the response buffer. |
| 1412 | |
Thierry FOURNIER | 486f5a0 | 2015-03-16 15:13:03 +0100 | [diff] [blame] | 1413 | .. js:attribute:: TXN.http |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 1414 | |
Thierry FOURNIER | dc59500 | 2015-12-21 11:13:52 +0100 | [diff] [blame] | 1415 | :returns: An :ref:`http_class`. |
| 1416 | |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 1417 | This attribute contains an HTTP class object. It is avalaible only if the |
| 1418 | proxy has the "mode http" enabled. |
| 1419 | |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 1420 | .. js:function:: TXN.log(TXN, loglevel, msg) |
| 1421 | |
| 1422 | This function sends a log. The log is sent, according with the HAProxy |
| 1423 | configuration file, on the default syslog server if it is configured and on |
| 1424 | the stderr if it is allowed. |
| 1425 | |
| 1426 | :param class_txn txn: The class txn object containing the data. |
| 1427 | :param integer loglevel: Is the log level asociated with the message. It is a |
| 1428 | number between 0 and 7. |
| 1429 | :param string msg: The log content. |
| 1430 | :see: core.emerg, core.alert, core.crit, core.err, core.warning, core.notice, |
| 1431 | core.info, core.debug (log level definitions) |
| 1432 | :see: TXN.deflog |
| 1433 | :see: TXN.Debug |
| 1434 | :see: TXN.Info |
| 1435 | :see: TXN.Warning |
| 1436 | :see: TXN.Alert |
| 1437 | |
| 1438 | .. js:function:: TXN.deflog(TXN, msg) |
| 1439 | |
| 1440 | Sends a log line with the default loglevel for the proxy ssociated with the |
| 1441 | transaction. |
| 1442 | |
| 1443 | :param class_txn txn: The class txn object containing the data. |
| 1444 | :param string msg: The log content. |
| 1445 | :see: TXN.log |
| 1446 | |
| 1447 | .. js:function:: TXN.Debug(txn, msg) |
| 1448 | |
| 1449 | :param class_txn txn: The class txn object containing the data. |
| 1450 | :param string msg: The log content. |
| 1451 | :see: TXN.log |
| 1452 | |
| 1453 | Does the same job than: |
| 1454 | |
| 1455 | .. code-block:: lua |
| 1456 | |
Thierry FOURNIER | dc59500 | 2015-12-21 11:13:52 +0100 | [diff] [blame] | 1457 | function Debug(txn, msg) |
| 1458 | TXN.log(txn, core.debug, msg) |
| 1459 | end |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 1460 | .. |
| 1461 | |
| 1462 | .. js:function:: TXN.Info(txn, msg) |
| 1463 | |
| 1464 | :param class_txn txn: The class txn object containing the data. |
| 1465 | :param string msg: The log content. |
| 1466 | :see: TXN.log |
| 1467 | |
| 1468 | .. code-block:: lua |
| 1469 | |
Thierry FOURNIER | dc59500 | 2015-12-21 11:13:52 +0100 | [diff] [blame] | 1470 | function Debug(txn, msg) |
| 1471 | TXN.log(txn, core.info, msg) |
| 1472 | end |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 1473 | .. |
| 1474 | |
| 1475 | .. js:function:: TXN.Warning(txn, msg) |
| 1476 | |
| 1477 | :param class_txn txn: The class txn object containing the data. |
| 1478 | :param string msg: The log content. |
| 1479 | :see: TXN.log |
| 1480 | |
| 1481 | .. code-block:: lua |
| 1482 | |
Thierry FOURNIER | dc59500 | 2015-12-21 11:13:52 +0100 | [diff] [blame] | 1483 | function Debug(txn, msg) |
| 1484 | TXN.log(txn, core.warning, msg) |
| 1485 | end |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 1486 | .. |
| 1487 | |
| 1488 | .. js:function:: TXN.Alert(txn, msg) |
| 1489 | |
| 1490 | :param class_txn txn: The class txn object containing the data. |
| 1491 | :param string msg: The log content. |
| 1492 | :see: TXN.log |
| 1493 | |
| 1494 | .. code-block:: lua |
| 1495 | |
Thierry FOURNIER | dc59500 | 2015-12-21 11:13:52 +0100 | [diff] [blame] | 1496 | function Debug(txn, msg) |
| 1497 | TXN.log(txn, core.alert, msg) |
| 1498 | end |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 1499 | .. |
| 1500 | |
Thierry FOURNIER | 486f5a0 | 2015-03-16 15:13:03 +0100 | [diff] [blame] | 1501 | .. js:function:: TXN.get_priv(txn) |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 1502 | |
Thierry FOURNIER | 486f5a0 | 2015-03-16 15:13:03 +0100 | [diff] [blame] | 1503 | 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] | 1504 | function. If no data are stored, it returns a nil value. |
| 1505 | |
| 1506 | :param class_txn txn: The class txn object containing the data. |
| 1507 | :returns: the opaque data previsously stored, or nil if nothing is |
| 1508 | avalaible. |
| 1509 | |
Thierry FOURNIER | 486f5a0 | 2015-03-16 15:13:03 +0100 | [diff] [blame] | 1510 | .. js:function:: TXN.set_priv(txn, data) |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 1511 | |
| 1512 | Store any data in the current HAProxy transaction. This action replace the |
| 1513 | old stored data. |
| 1514 | |
| 1515 | :param class_txn txn: The class txn object containing the data. |
| 1516 | :param opaque data: The data which is stored in the transaction. |
| 1517 | |
Thierry FOURNIER | 053ba8ad | 2015-06-08 13:05:33 +0200 | [diff] [blame] | 1518 | .. js:function:: TXN.set_var(TXN, var, value) |
| 1519 | |
David Carlier | 61fdf8b | 2015-10-02 11:59:38 +0100 | [diff] [blame] | 1520 | 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] | 1521 | |
| 1522 | :param class_txn txn: The class txn object containing the data. |
| 1523 | :param string var: The variable name according with the HAProxy variable syntax. |
Christopher Faulet | 85d79c9 | 2016-11-09 16:54:56 +0100 | [diff] [blame] | 1524 | |
| 1525 | .. js:function:: TXN.unset_var(TXN, var) |
| 1526 | |
| 1527 | Unset the variable <var>. |
| 1528 | |
| 1529 | :param class_txn txn: The class txn object containing the data. |
| 1530 | :param string var: The variable name according with the HAProxy variable syntax. |
Thierry FOURNIER | 053ba8ad | 2015-06-08 13:05:33 +0200 | [diff] [blame] | 1531 | |
| 1532 | .. js:function:: TXN.get_var(TXN, var) |
| 1533 | |
| 1534 | Returns data stored in the variable <var> converter in Lua type. |
| 1535 | |
| 1536 | :param class_txn txn: The class txn object containing the data. |
| 1537 | :param string var: The variable name according with the HAProxy variable syntax. |
| 1538 | |
Willy Tarreau | bc183a6 | 2015-08-28 10:39:11 +0200 | [diff] [blame] | 1539 | .. js:function:: TXN.done(txn) |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 1540 | |
Willy Tarreau | bc183a6 | 2015-08-28 10:39:11 +0200 | [diff] [blame] | 1541 | This function terminates processing of the transaction and the associated |
| 1542 | session. It can be used when a critical error is detected or to terminate |
| 1543 | 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] | 1544 | |
Thierry FOURNIER | ab00df6 | 2016-07-14 11:42:37 +0200 | [diff] [blame] | 1545 | *Warning*: It not make sense to call this function from sample-fetches. In |
| 1546 | this case the behaviour of this one is the same than core.done(): it quit |
| 1547 | the Lua execution. The transaction is really aborted only from an action |
| 1548 | registered function. |
| 1549 | |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 1550 | :param class_txn txn: The class txn object containing the data. |
| 1551 | |
Thierry FOURNIER | 486f5a0 | 2015-03-16 15:13:03 +0100 | [diff] [blame] | 1552 | .. js:function:: TXN.set_loglevel(txn, loglevel) |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 1553 | |
| 1554 | Is used to change the log level of the current request. The "loglevel" must |
| 1555 | be an integer between 0 and 7. |
| 1556 | |
| 1557 | :param class_txn txn: The class txn object containing the data. |
| 1558 | :param integer loglevel: The required log level. This variable can be one of |
| 1559 | :see: core.<loglevel> |
| 1560 | |
Thierry FOURNIER | 486f5a0 | 2015-03-16 15:13:03 +0100 | [diff] [blame] | 1561 | .. js:function:: TXN.set_tos(txn, tos) |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 1562 | |
| 1563 | Is used to set the TOS or DSCP field value of packets sent to the client to |
| 1564 | the value passed in "tos" on platforms which support this. |
| 1565 | |
| 1566 | :param class_txn txn: The class txn object containing the data. |
| 1567 | :param integer tos: The new TOS os DSCP. |
| 1568 | |
Thierry FOURNIER | 486f5a0 | 2015-03-16 15:13:03 +0100 | [diff] [blame] | 1569 | .. js:function:: TXN.set_mark(txn, mark) |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 1570 | |
| 1571 | Is used to set the Netfilter MARK on all packets sent to the client to the |
| 1572 | value passed in "mark" on platforms which support it. |
| 1573 | |
| 1574 | :param class_txn txn: The class txn object containing the data. |
| 1575 | :param integer mark: The mark value. |
| 1576 | |
Thierry FOURNIER | dc59500 | 2015-12-21 11:13:52 +0100 | [diff] [blame] | 1577 | .. _socket_class: |
| 1578 | |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 1579 | Socket class |
| 1580 | ============ |
| 1581 | |
| 1582 | .. js:class:: Socket |
| 1583 | |
| 1584 | This class must be compatible with the Lua Socket class. Only the 'client' |
| 1585 | functions are available. See the Lua Socket documentation: |
| 1586 | |
| 1587 | `http://w3.impa.br/~diego/software/luasocket/tcp.html |
| 1588 | <http://w3.impa.br/~diego/software/luasocket/tcp.html>`_ |
| 1589 | |
Thierry FOURNIER | 486f5a0 | 2015-03-16 15:13:03 +0100 | [diff] [blame] | 1590 | .. js:function:: Socket.close(socket) |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 1591 | |
| 1592 | Closes a TCP object. The internal socket used by the object is closed and the |
| 1593 | local address to which the object was bound is made available to other |
| 1594 | applications. No further operations (except for further calls to the close |
Thierry FOURNIER | 486f5a0 | 2015-03-16 15:13:03 +0100 | [diff] [blame] | 1595 | method) are allowed on a closed Socket. |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 1596 | |
Thierry FOURNIER | 486f5a0 | 2015-03-16 15:13:03 +0100 | [diff] [blame] | 1597 | :param class_socket socket: Is the manipulated Socket. |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 1598 | |
| 1599 | Note: It is important to close all used sockets once they are not needed, |
| 1600 | since, in many systems, each socket uses a file descriptor, which are limited |
| 1601 | system resources. Garbage-collected objects are automatically closed before |
| 1602 | destruction, though. |
| 1603 | |
Thierry FOURNIER | a3bc513 | 2015-09-25 21:43:56 +0200 | [diff] [blame] | 1604 | .. js:function:: Socket.connect(socket, address[, port]) |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 1605 | |
| 1606 | Attempts to connect a socket object to a remote host. |
| 1607 | |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 1608 | |
| 1609 | In case of error, the method returns nil followed by a string describing the |
| 1610 | error. In case of success, the method returns 1. |
| 1611 | |
Thierry FOURNIER | 486f5a0 | 2015-03-16 15:13:03 +0100 | [diff] [blame] | 1612 | :param class_socket socket: Is the manipulated Socket. |
Thierry FOURNIER | a3bc513 | 2015-09-25 21:43:56 +0200 | [diff] [blame] | 1613 | :param string address: can be an IP address or a host name. See below for more |
| 1614 | information. |
| 1615 | :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] | 1616 | :returns: 1 or nil. |
| 1617 | |
Thierry FOURNIER | a3bc513 | 2015-09-25 21:43:56 +0200 | [diff] [blame] | 1618 | an address field extension permits to use the connect() function to connect to |
| 1619 | other stream than TCP. The syntax containing a simpleipv4 or ipv6 address is |
| 1620 | the basically expected format. This format requires the port. |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 1621 | |
Thierry FOURNIER | a3bc513 | 2015-09-25 21:43:56 +0200 | [diff] [blame] | 1622 | Other format accepted are a socket path like "/socket/path", it permits to |
| 1623 | connect to a socket. abstract namespaces are supported with the prefix |
| 1624 | "abns@", and finaly a filedescriotr can be passed with the prefix "fd@". |
| 1625 | The prefix "ipv4@", "ipv6@" and "unix@" are also supported. The port can be |
| 1626 | passed int the string. The syntax "127.0.0.1:1234" is valid. in this case, the |
| 1627 | parameter *port* is ignored. |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 1628 | |
Thierry FOURNIER | 486f5a0 | 2015-03-16 15:13:03 +0100 | [diff] [blame] | 1629 | .. js:function:: Socket.connect_ssl(socket, address, port) |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 1630 | |
| 1631 | Same behavior than the function socket:connect, but uses SSL. |
| 1632 | |
Thierry FOURNIER | 486f5a0 | 2015-03-16 15:13:03 +0100 | [diff] [blame] | 1633 | :param class_socket socket: Is the manipulated Socket. |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 1634 | :returns: 1 or nil. |
| 1635 | |
Thierry FOURNIER | 486f5a0 | 2015-03-16 15:13:03 +0100 | [diff] [blame] | 1636 | .. js:function:: Socket.getpeername(socket) |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 1637 | |
| 1638 | Returns information about the remote side of a connected client object. |
| 1639 | |
| 1640 | Returns a string with the IP address of the peer, followed by the port number |
| 1641 | that peer is using for the connection. In case of error, the method returns |
| 1642 | nil. |
| 1643 | |
Thierry FOURNIER | 486f5a0 | 2015-03-16 15:13:03 +0100 | [diff] [blame] | 1644 | :param class_socket socket: Is the manipulated Socket. |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 1645 | :returns: a string containing the server information. |
| 1646 | |
Thierry FOURNIER | 486f5a0 | 2015-03-16 15:13:03 +0100 | [diff] [blame] | 1647 | .. js:function:: Socket.getsockname(socket) |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 1648 | |
| 1649 | Returns the local address information associated to the object. |
| 1650 | |
| 1651 | The method returns a string with local IP address and a number with the port. |
| 1652 | In case of error, the method returns nil. |
| 1653 | |
Thierry FOURNIER | 486f5a0 | 2015-03-16 15:13:03 +0100 | [diff] [blame] | 1654 | :param class_socket socket: Is the manipulated Socket. |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 1655 | :returns: a string containing the client information. |
| 1656 | |
Thierry FOURNIER | 486f5a0 | 2015-03-16 15:13:03 +0100 | [diff] [blame] | 1657 | .. js:function:: Socket.receive(socket, [pattern [, prefix]]) |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 1658 | |
| 1659 | Reads data from a client object, according to the specified read pattern. |
| 1660 | Patterns follow the Lua file I/O format, and the difference in performance |
| 1661 | between all patterns is negligible. |
| 1662 | |
Thierry FOURNIER | 486f5a0 | 2015-03-16 15:13:03 +0100 | [diff] [blame] | 1663 | :param class_socket socket: Is the manipulated Socket. |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 1664 | :param string|integer pattern: Describe what is required (see below). |
| 1665 | :param string prefix: A string which will be prefix the returned data. |
| 1666 | :returns: a string containing the required data or nil. |
| 1667 | |
| 1668 | Pattern can be any of the following: |
| 1669 | |
| 1670 | * **`*a`**: reads from the socket until the connection is closed. No |
| 1671 | end-of-line translation is performed; |
| 1672 | |
Thierry FOURNIER | 486f5a0 | 2015-03-16 15:13:03 +0100 | [diff] [blame] | 1673 | * **`*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] | 1674 | LF character (ASCII 10), optionally preceded by a CR character |
| 1675 | (ASCII 13). The CR and LF characters are not included in the |
| 1676 | returned line. In fact, all CR characters are ignored by the |
| 1677 | pattern. This is the default pattern. |
| 1678 | |
| 1679 | * **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] | 1680 | Socket. Prefix is an optional string to be concatenated to the |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 1681 | beginning of any received data before return. |
| 1682 | |
Thierry FOURNIER | a3bc513 | 2015-09-25 21:43:56 +0200 | [diff] [blame] | 1683 | * **empty**: If the pattern is left empty, the default option is `*l`. |
| 1684 | |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 1685 | If successful, the method returns the received pattern. In case of error, the |
| 1686 | method returns nil followed by an error message which can be the string |
| 1687 | 'closed' in case the connection was closed before the transmission was |
| 1688 | completed or the string 'timeout' in case there was a timeout during the |
| 1689 | operation. Also, after the error message, the function returns the partial |
| 1690 | result of the transmission. |
| 1691 | |
| 1692 | Important note: This function was changed severely. It used to support |
| 1693 | multiple patterns (but I have never seen this feature used) and now it |
| 1694 | doesn't anymore. Partial results used to be returned in the same way as |
| 1695 | successful results. This last feature violated the idea that all functions |
| 1696 | should return nil on error. Thus it was changed too. |
| 1697 | |
Thierry FOURNIER | 486f5a0 | 2015-03-16 15:13:03 +0100 | [diff] [blame] | 1698 | .. js:function:: Socket.send(socket, data [, start [, end ]]) |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 1699 | |
| 1700 | Sends data through client object. |
| 1701 | |
Thierry FOURNIER | 486f5a0 | 2015-03-16 15:13:03 +0100 | [diff] [blame] | 1702 | :param class_socket socket: Is the manipulated Socket. |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 1703 | :param string data: The data that will be sent. |
| 1704 | :param integer start: The start position in the buffer of the data which will |
| 1705 | be sent. |
| 1706 | :param integer end: The end position in the buffer of the data which will |
| 1707 | be sent. |
| 1708 | :returns: see below. |
| 1709 | |
| 1710 | Data is the string to be sent. The optional arguments i and j work exactly |
| 1711 | like the standard string.sub Lua function to allow the selection of a |
| 1712 | substring to be sent. |
| 1713 | |
| 1714 | If successful, the method returns the index of the last byte within [start, |
| 1715 | end] that has been sent. Notice that, if start is 1 or absent, this is |
| 1716 | effectively the total number of bytes sent. In case of error, the method |
| 1717 | returns nil, followed by an error message, followed by the index of the last |
| 1718 | byte within [start, end] that has been sent. You might want to try again from |
| 1719 | the byte following that. The error message can be 'closed' in case the |
| 1720 | connection was closed before the transmission was completed or the string |
| 1721 | 'timeout' in case there was a timeout during the operation. |
| 1722 | |
| 1723 | Note: Output is not buffered. For small strings, it is always better to |
| 1724 | concatenate them in Lua (with the '..' operator) and send the result in one |
| 1725 | call instead of calling the method several times. |
| 1726 | |
Thierry FOURNIER | 486f5a0 | 2015-03-16 15:13:03 +0100 | [diff] [blame] | 1727 | .. js:function:: Socket.setoption(socket, option [, value]) |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 1728 | |
| 1729 | Just implemented for compatibility, this cal does nothing. |
| 1730 | |
Thierry FOURNIER | 486f5a0 | 2015-03-16 15:13:03 +0100 | [diff] [blame] | 1731 | .. js:function:: Socket.settimeout(socket, value [, mode]) |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 1732 | |
| 1733 | Changes the timeout values for the object. All I/O operations are blocking. |
| 1734 | That is, any call to the methods send, receive, and accept will block |
| 1735 | indefinitely, until the operation completes. The settimeout method defines a |
| 1736 | limit on the amount of time the I/O methods can block. When a timeout time |
| 1737 | has elapsed, the affected methods give up and fail with an error code. |
| 1738 | |
| 1739 | The amount of time to wait is specified as the value parameter, in seconds. |
| 1740 | |
| 1741 | The timeout modes are bot implemented, the only settable timeout is the |
| 1742 | inactivity time waiting for complete the internal buffer send or waiting for |
| 1743 | receive data. |
| 1744 | |
Thierry FOURNIER | 486f5a0 | 2015-03-16 15:13:03 +0100 | [diff] [blame] | 1745 | :param class_socket socket: Is the manipulated Socket. |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 1746 | :param integer value: The timeout value. |
| 1747 | |
Thierry FOURNIER | dc59500 | 2015-12-21 11:13:52 +0100 | [diff] [blame] | 1748 | .. _map_class: |
| 1749 | |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1750 | Map class |
| 1751 | ========= |
| 1752 | |
| 1753 | .. js:class:: Map |
| 1754 | |
| 1755 | This class permits to do some lookup in HAProxy maps. The declared maps can |
| 1756 | be modified during the runtime throught the HAProxy management socket. |
| 1757 | |
| 1758 | .. code-block:: lua |
| 1759 | |
Thierry FOURNIER | dc59500 | 2015-12-21 11:13:52 +0100 | [diff] [blame] | 1760 | default = "usa" |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1761 | |
Thierry FOURNIER | dc59500 | 2015-12-21 11:13:52 +0100 | [diff] [blame] | 1762 | -- Create and load map |
| 1763 | geo = Map.new("geo.map", Map.ip); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1764 | |
Thierry FOURNIER | dc59500 | 2015-12-21 11:13:52 +0100 | [diff] [blame] | 1765 | -- Create new fetch that returns the user country |
| 1766 | core.register_fetches("country", function(txn) |
| 1767 | local src; |
| 1768 | local loc; |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1769 | |
Thierry FOURNIER | dc59500 | 2015-12-21 11:13:52 +0100 | [diff] [blame] | 1770 | src = txn.f:fhdr("x-forwarded-for"); |
| 1771 | if (src == nil) then |
| 1772 | src = txn.f:src() |
| 1773 | if (src == nil) then |
| 1774 | return default; |
| 1775 | end |
| 1776 | end |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1777 | |
Thierry FOURNIER | dc59500 | 2015-12-21 11:13:52 +0100 | [diff] [blame] | 1778 | -- Perform lookup |
| 1779 | loc = geo:lookup(src); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1780 | |
Thierry FOURNIER | dc59500 | 2015-12-21 11:13:52 +0100 | [diff] [blame] | 1781 | if (loc == nil) then |
| 1782 | return default; |
| 1783 | end |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1784 | |
Thierry FOURNIER | dc59500 | 2015-12-21 11:13:52 +0100 | [diff] [blame] | 1785 | return loc; |
| 1786 | end); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1787 | |
| 1788 | .. js:attribute:: Map.int |
| 1789 | |
| 1790 | See the HAProxy configuration.txt file, chapter "Using ACLs and fetching |
| 1791 | samples" ans subchapter "ACL basics" to understand this pattern matching |
| 1792 | method. |
| 1793 | |
| 1794 | .. js:attribute:: Map.ip |
| 1795 | |
| 1796 | See the HAProxy configuration.txt file, chapter "Using ACLs and fetching |
| 1797 | samples" ans subchapter "ACL basics" to understand this pattern matching |
| 1798 | method. |
| 1799 | |
| 1800 | .. js:attribute:: Map.str |
| 1801 | |
| 1802 | See the HAProxy configuration.txt file, chapter "Using ACLs and fetching |
| 1803 | samples" ans subchapter "ACL basics" to understand this pattern matching |
| 1804 | method. |
| 1805 | |
| 1806 | .. js:attribute:: Map.beg |
| 1807 | |
| 1808 | See the HAProxy configuration.txt file, chapter "Using ACLs and fetching |
| 1809 | samples" ans subchapter "ACL basics" to understand this pattern matching |
| 1810 | method. |
| 1811 | |
| 1812 | .. js:attribute:: Map.sub |
| 1813 | |
| 1814 | See the HAProxy configuration.txt file, chapter "Using ACLs and fetching |
| 1815 | samples" ans subchapter "ACL basics" to understand this pattern matching |
| 1816 | method. |
| 1817 | |
| 1818 | .. js:attribute:: Map.dir |
| 1819 | |
| 1820 | See the HAProxy configuration.txt file, chapter "Using ACLs and fetching |
| 1821 | samples" ans subchapter "ACL basics" to understand this pattern matching |
| 1822 | method. |
| 1823 | |
| 1824 | .. js:attribute:: Map.dom |
| 1825 | |
| 1826 | See the HAProxy configuration.txt file, chapter "Using ACLs and fetching |
| 1827 | samples" ans subchapter "ACL basics" to understand this pattern matching |
| 1828 | method. |
| 1829 | |
| 1830 | .. js:attribute:: Map.end |
| 1831 | |
| 1832 | See the HAProxy configuration.txt file, chapter "Using ACLs and fetching |
| 1833 | samples" ans subchapter "ACL basics" to understand this pattern matching |
| 1834 | method. |
| 1835 | |
| 1836 | .. js:attribute:: Map.reg |
| 1837 | |
| 1838 | See the HAProxy configuration.txt file, chapter "Using ACLs and fetching |
| 1839 | samples" ans subchapter "ACL basics" to understand this pattern matching |
| 1840 | method. |
| 1841 | |
| 1842 | |
| 1843 | .. js:function:: Map.new(file, method) |
| 1844 | |
| 1845 | Creates and load a map. |
| 1846 | |
| 1847 | :param string file: Is the file containing the map. |
| 1848 | :param integer method: Is the map pattern matching method. See the attributes |
| 1849 | of the Map class. |
| 1850 | :returns: a class Map object. |
| 1851 | :see: The Map attributes. |
| 1852 | |
| 1853 | .. js:function:: Map.lookup(map, str) |
| 1854 | |
| 1855 | Perform a lookup in a map. |
| 1856 | |
| 1857 | :param class_map map: Is the class Map object. |
| 1858 | :param string str: Is the string used as key. |
| 1859 | :returns: a string containing the result or nil if no match. |
| 1860 | |
| 1861 | .. js:function:: Map.slookup(map, str) |
| 1862 | |
| 1863 | Perform a lookup in a map. |
| 1864 | |
| 1865 | :param class_map map: Is the class Map object. |
| 1866 | :param string str: Is the string used as key. |
| 1867 | :returns: a string containing the result or empty string if no match. |
| 1868 | |
Thierry FOURNIER | dc59500 | 2015-12-21 11:13:52 +0100 | [diff] [blame] | 1869 | .. _applethttp_class: |
| 1870 | |
Thierry FOURNIER | a3bc513 | 2015-09-25 21:43:56 +0200 | [diff] [blame] | 1871 | AppletHTTP class |
Thierry FOURNIER | dc59500 | 2015-12-21 11:13:52 +0100 | [diff] [blame] | 1872 | ================ |
Thierry FOURNIER | a3bc513 | 2015-09-25 21:43:56 +0200 | [diff] [blame] | 1873 | |
| 1874 | .. js:class:: AppletHTTP |
| 1875 | |
| 1876 | This class is used with applets that requires the 'http' mode. The http applet |
| 1877 | can be registered with the *core.register_service()* function. They are used |
| 1878 | for processing an http request like a server in back of HAProxy. |
| 1879 | |
| 1880 | This is an hello world sample code: |
| 1881 | |
| 1882 | .. code-block:: lua |
Thierry FOURNIER | dc59500 | 2015-12-21 11:13:52 +0100 | [diff] [blame] | 1883 | |
Pieter Baauw | 4d7f766 | 2015-11-08 16:38:08 +0100 | [diff] [blame] | 1884 | core.register_service("hello-world", "http", function(applet) |
Thierry FOURNIER | a3bc513 | 2015-09-25 21:43:56 +0200 | [diff] [blame] | 1885 | local response = "Hello World !" |
| 1886 | applet:set_status(200) |
Pieter Baauw | 2dcb9bc | 2015-10-01 22:47:12 +0200 | [diff] [blame] | 1887 | applet:add_header("content-length", string.len(response)) |
Thierry FOURNIER | a3bc513 | 2015-09-25 21:43:56 +0200 | [diff] [blame] | 1888 | applet:add_header("content-type", "text/plain") |
Pieter Baauw | 2dcb9bc | 2015-10-01 22:47:12 +0200 | [diff] [blame] | 1889 | applet:start_response() |
Thierry FOURNIER | a3bc513 | 2015-09-25 21:43:56 +0200 | [diff] [blame] | 1890 | applet:send(response) |
| 1891 | end) |
| 1892 | |
Thierry FOURNIER | dc59500 | 2015-12-21 11:13:52 +0100 | [diff] [blame] | 1893 | .. js:attribute:: AppletHTTP.c |
| 1894 | |
| 1895 | :returns: A :ref:`converters_class` |
| 1896 | |
| 1897 | This attribute contains a Converters class object. |
| 1898 | |
| 1899 | .. js:attribute:: AppletHTTP.sc |
| 1900 | |
| 1901 | :returns: A :ref:`converters_class` |
| 1902 | |
| 1903 | This attribute contains a Converters class object. The |
| 1904 | functions of this object returns always a string. |
| 1905 | |
| 1906 | .. js:attribute:: AppletHTTP.f |
| 1907 | |
| 1908 | :returns: A :ref:`fetches_class` |
| 1909 | |
| 1910 | This attribute contains a Fetches class object. Note that the |
| 1911 | applet execution place cannot access to a valid HAProxy core HTTP |
| 1912 | transaction, so some sample fecthes related to the HTTP dependant |
| 1913 | values (hdr, path, ...) are not available. |
| 1914 | |
| 1915 | .. js:attribute:: AppletHTTP.sf |
| 1916 | |
| 1917 | :returns: A :ref:`fetches_class` |
| 1918 | |
| 1919 | This attribute contains a Fetches class object. The functions of |
| 1920 | this object returns always a string. Note that the applet |
| 1921 | execution place cannot access to a valid HAProxy core HTTP |
| 1922 | transaction, so some sample fecthes related to the HTTP dependant |
| 1923 | values (hdr, path, ...) are not available. |
| 1924 | |
Thierry FOURNIER | e34a78e | 2015-12-25 01:31:35 +0100 | [diff] [blame] | 1925 | .. js:attribute:: AppletHTTP.method |
Thierry FOURNIER | dc59500 | 2015-12-21 11:13:52 +0100 | [diff] [blame] | 1926 | |
| 1927 | :returns: string |
| 1928 | |
| 1929 | The attribute method returns a string containing the HTTP |
| 1930 | method. |
| 1931 | |
| 1932 | .. js:attribute:: AppletHTTP.version |
| 1933 | |
| 1934 | :returns: string |
| 1935 | |
| 1936 | The attribute version, returns a string containing the HTTP |
| 1937 | request version. |
| 1938 | |
| 1939 | .. js:attribute:: AppletHTTP.path |
| 1940 | |
| 1941 | :returns: string |
| 1942 | |
| 1943 | The attribute path returns a string containing the HTTP |
| 1944 | request path. |
| 1945 | |
| 1946 | .. js:attribute:: AppletHTTP.qs |
| 1947 | |
| 1948 | :returns: string |
| 1949 | |
| 1950 | The attribute qs returns a string containing the HTTP |
| 1951 | request query string. |
| 1952 | |
| 1953 | .. js:attribute:: AppletHTTP.length |
| 1954 | |
| 1955 | :returns: integer |
| 1956 | |
| 1957 | The attribute length returns an integer containing the HTTP |
| 1958 | body length. |
Thierry FOURNIER | a3bc513 | 2015-09-25 21:43:56 +0200 | [diff] [blame] | 1959 | |
Thierry FOURNIER | 841475e | 2015-12-11 17:10:09 +0100 | [diff] [blame] | 1960 | .. js:attribute:: AppletHTTP.headers |
| 1961 | |
Thierry FOURNIER | dc59500 | 2015-12-21 11:13:52 +0100 | [diff] [blame] | 1962 | :returns: array |
| 1963 | |
| 1964 | The attribute headers returns an array containing the HTTP |
| 1965 | headers. The header names are always in lower case. As the header name can be |
| 1966 | encountered more than once in each request, the value is indexed with 0 as |
| 1967 | first index value. The array have this form: |
| 1968 | |
| 1969 | .. code-block:: lua |
| 1970 | |
| 1971 | AppletHTTP.headers['<header-name>'][<header-index>] = "<header-value>" |
| 1972 | |
| 1973 | AppletHTTP.headers["host"][0] = "www.test.com" |
| 1974 | AppletHTTP.headers["accept"][0] = "audio/basic q=1" |
| 1975 | AppletHTTP.headers["accept"][1] = "audio/*, q=0.2" |
Thierry FOURNIER | e34a78e | 2015-12-25 01:31:35 +0100 | [diff] [blame] | 1976 | AppletHTTP.headers["accept"][2] = "*/*, q=0.1" |
Thierry FOURNIER | dc59500 | 2015-12-21 11:13:52 +0100 | [diff] [blame] | 1977 | .. |
| 1978 | |
| 1979 | .. js:attribute:: AppletHTTP.headers |
| 1980 | |
Thierry FOURNIER | 841475e | 2015-12-11 17:10:09 +0100 | [diff] [blame] | 1981 | Contains an array containing all the request headers. |
| 1982 | |
Thierry FOURNIER | e34a78e | 2015-12-25 01:31:35 +0100 | [diff] [blame] | 1983 | .. js:function:: AppletHTTP.set_status(applet, code) |
Thierry FOURNIER | a3bc513 | 2015-09-25 21:43:56 +0200 | [diff] [blame] | 1984 | |
| 1985 | This function sets the HTTP status code for the response. The allowed code are |
| 1986 | from 100 to 599. |
| 1987 | |
Thierry FOURNIER | e34a78e | 2015-12-25 01:31:35 +0100 | [diff] [blame] | 1988 | :param class_AppletHTTP applet: An :ref:`applethttp_class` |
Thierry FOURNIER | a3bc513 | 2015-09-25 21:43:56 +0200 | [diff] [blame] | 1989 | :param integer code: the status code returned to the client. |
| 1990 | |
Thierry FOURNIER | e34a78e | 2015-12-25 01:31:35 +0100 | [diff] [blame] | 1991 | .. js:function:: AppletHTTP.add_header(applet, name, value) |
Thierry FOURNIER | a3bc513 | 2015-09-25 21:43:56 +0200 | [diff] [blame] | 1992 | |
| 1993 | This function add an header in the response. Duplicated headers are not |
| 1994 | collapsed. The special header *content-length* is used to determinate the |
| 1995 | response length. If it not exists, a *transfer-encoding: chunked* is set, and |
| 1996 | all the write from the funcion *AppletHTTP:send()* become a chunk. |
| 1997 | |
Thierry FOURNIER | e34a78e | 2015-12-25 01:31:35 +0100 | [diff] [blame] | 1998 | :param class_AppletHTTP applet: An :ref:`applethttp_class` |
Thierry FOURNIER | a3bc513 | 2015-09-25 21:43:56 +0200 | [diff] [blame] | 1999 | :param string name: the header name |
| 2000 | :param string value: the header value |
| 2001 | |
Thierry FOURNIER | e34a78e | 2015-12-25 01:31:35 +0100 | [diff] [blame] | 2002 | .. js:function:: AppletHTTP.start_response(applet) |
Thierry FOURNIER | a3bc513 | 2015-09-25 21:43:56 +0200 | [diff] [blame] | 2003 | |
| 2004 | This function indicates to the HTTP engine that it can process and send the |
| 2005 | response headers. After this called we cannot add headers to the response; We |
| 2006 | cannot use the *AppletHTTP:send()* function if the |
| 2007 | *AppletHTTP:start_response()* is not called. |
| 2008 | |
Thierry FOURNIER | e34a78e | 2015-12-25 01:31:35 +0100 | [diff] [blame] | 2009 | :param class_AppletHTTP applet: An :ref:`applethttp_class` |
| 2010 | |
| 2011 | .. js:function:: AppletHTTP.getline(applet) |
Thierry FOURNIER | a3bc513 | 2015-09-25 21:43:56 +0200 | [diff] [blame] | 2012 | |
| 2013 | This function returns a string containing one line from the http body. If the |
| 2014 | data returned doesn't contains a final '\\n' its assumed than its the last |
| 2015 | available data before the end of stream. |
| 2016 | |
Thierry FOURNIER | e34a78e | 2015-12-25 01:31:35 +0100 | [diff] [blame] | 2017 | :param class_AppletHTTP applet: An :ref:`applethttp_class` |
Thierry FOURNIER | a3bc513 | 2015-09-25 21:43:56 +0200 | [diff] [blame] | 2018 | :returns: a string. The string can be empty if we reach the end of the stream. |
| 2019 | |
Thierry FOURNIER | e34a78e | 2015-12-25 01:31:35 +0100 | [diff] [blame] | 2020 | .. js:function:: AppletHTTP.receive(applet, [size]) |
Thierry FOURNIER | a3bc513 | 2015-09-25 21:43:56 +0200 | [diff] [blame] | 2021 | |
| 2022 | Reads data from the HTTP body, according to the specified read *size*. If the |
| 2023 | *size* is missing, the function tries to read all the content of the stream |
| 2024 | until the end. If the *size* is bigger than the http body, it returns the |
| 2025 | amount of data avalaible. |
| 2026 | |
Thierry FOURNIER | e34a78e | 2015-12-25 01:31:35 +0100 | [diff] [blame] | 2027 | :param class_AppletHTTP applet: An :ref:`applethttp_class` |
Thierry FOURNIER | a3bc513 | 2015-09-25 21:43:56 +0200 | [diff] [blame] | 2028 | :param integer size: the required read size. |
| 2029 | :returns: always return a string,the string can be empty is the connexion is |
| 2030 | closed. |
| 2031 | |
Thierry FOURNIER | e34a78e | 2015-12-25 01:31:35 +0100 | [diff] [blame] | 2032 | .. js:function:: AppletHTTP.send(applet, msg) |
Thierry FOURNIER | a3bc513 | 2015-09-25 21:43:56 +0200 | [diff] [blame] | 2033 | |
| 2034 | Send the message *msg* on the http request body. |
| 2035 | |
Thierry FOURNIER | e34a78e | 2015-12-25 01:31:35 +0100 | [diff] [blame] | 2036 | :param class_AppletHTTP applet: An :ref:`applethttp_class` |
Thierry FOURNIER | a3bc513 | 2015-09-25 21:43:56 +0200 | [diff] [blame] | 2037 | :param string msg: the message to send. |
| 2038 | |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 2039 | .. js:function:: AppletHTTP.get_priv(applet) |
| 2040 | |
| 2041 | Return Lua data stored in the current transaction (with the |
| 2042 | `AppletHTTP.set_priv()`) function. If no data are stored, it returns a nil |
| 2043 | value. |
| 2044 | |
| 2045 | :param class_AppletHTTP applet: An :ref:`applethttp_class` |
| 2046 | :returns: the opaque data previsously stored, or nil if nothing is |
| 2047 | avalaible. |
| 2048 | |
| 2049 | .. js:function:: AppletHTTP.set_priv(applet, data) |
| 2050 | |
| 2051 | Store any data in the current HAProxy transaction. This action replace the |
| 2052 | old stored data. |
| 2053 | |
| 2054 | :param class_AppletHTTP applet: An :ref:`applethttp_class` |
| 2055 | :param opaque data: The data which is stored in the transaction. |
| 2056 | |
Thierry FOURNIER | dc59500 | 2015-12-21 11:13:52 +0100 | [diff] [blame] | 2057 | .. _applettcp_class: |
| 2058 | |
Thierry FOURNIER | a3bc513 | 2015-09-25 21:43:56 +0200 | [diff] [blame] | 2059 | AppletTCP class |
| 2060 | =============== |
| 2061 | |
| 2062 | .. js:class:: AppletTCP |
| 2063 | |
| 2064 | This class is used with applets that requires the 'tcp' mode. The tcp applet |
| 2065 | can be registered with the *core.register_service()* function. They are used |
| 2066 | for processing a tcp stream like a server in back of HAProxy. |
| 2067 | |
Thierry FOURNIER | dc59500 | 2015-12-21 11:13:52 +0100 | [diff] [blame] | 2068 | .. js:attribute:: AppletTCP.c |
| 2069 | |
| 2070 | :returns: A :ref:`converters_class` |
| 2071 | |
| 2072 | This attribute contains a Converters class object. |
| 2073 | |
| 2074 | .. js:attribute:: AppletTCP.sc |
| 2075 | |
| 2076 | :returns: A :ref:`converters_class` |
| 2077 | |
| 2078 | This attribute contains a Converters class object. The |
| 2079 | functions of this object returns always a string. |
| 2080 | |
| 2081 | .. js:attribute:: AppletTCP.f |
| 2082 | |
| 2083 | :returns: A :ref:`fetches_class` |
| 2084 | |
| 2085 | This attribute contains a Fetches class object. |
| 2086 | |
| 2087 | .. js:attribute:: AppletTCP.sf |
| 2088 | |
| 2089 | :returns: A :ref:`fetches_class` |
| 2090 | |
| 2091 | This attribute contains a Fetches class object. |
| 2092 | |
Thierry FOURNIER | e34a78e | 2015-12-25 01:31:35 +0100 | [diff] [blame] | 2093 | .. js:function:: AppletTCP.getline(applet) |
Thierry FOURNIER | a3bc513 | 2015-09-25 21:43:56 +0200 | [diff] [blame] | 2094 | |
| 2095 | This function returns a string containing one line from the stream. If the |
| 2096 | data returned doesn't contains a final '\\n' its assumed than its the last |
| 2097 | available data before the end of stream. |
| 2098 | |
Thierry FOURNIER | e34a78e | 2015-12-25 01:31:35 +0100 | [diff] [blame] | 2099 | :param class_AppletTCP applet: An :ref:`applettcp_class` |
Thierry FOURNIER | a3bc513 | 2015-09-25 21:43:56 +0200 | [diff] [blame] | 2100 | :returns: a string. The string can be empty if we reach the end of the stream. |
| 2101 | |
Thierry FOURNIER | e34a78e | 2015-12-25 01:31:35 +0100 | [diff] [blame] | 2102 | .. js:function:: AppletTCP.receive(applet, [size]) |
Thierry FOURNIER | a3bc513 | 2015-09-25 21:43:56 +0200 | [diff] [blame] | 2103 | |
| 2104 | Reads data from the TCP stream, according to the specified read *size*. If the |
| 2105 | *size* is missing, the function tries to read all the content of the stream |
| 2106 | until the end. |
| 2107 | |
Thierry FOURNIER | e34a78e | 2015-12-25 01:31:35 +0100 | [diff] [blame] | 2108 | :param class_AppletTCP applet: An :ref:`applettcp_class` |
Thierry FOURNIER | a3bc513 | 2015-09-25 21:43:56 +0200 | [diff] [blame] | 2109 | :param integer size: the required read size. |
| 2110 | :returns: always return a string,the string can be empty is the connexion is |
| 2111 | closed. |
| 2112 | |
Thierry FOURNIER | e34a78e | 2015-12-25 01:31:35 +0100 | [diff] [blame] | 2113 | .. js:function:: AppletTCP.send(appletmsg) |
Thierry FOURNIER | a3bc513 | 2015-09-25 21:43:56 +0200 | [diff] [blame] | 2114 | |
| 2115 | Send the message on the stream. |
| 2116 | |
Thierry FOURNIER | e34a78e | 2015-12-25 01:31:35 +0100 | [diff] [blame] | 2117 | :param class_AppletTCP applet: An :ref:`applettcp_class` |
Thierry FOURNIER | a3bc513 | 2015-09-25 21:43:56 +0200 | [diff] [blame] | 2118 | :param string msg: the message to send. |
| 2119 | |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 2120 | .. js:function:: AppletTCP.get_priv(applet) |
| 2121 | |
| 2122 | Return Lua data stored in the current transaction (with the |
| 2123 | `AppletTCP.set_priv()`) function. If no data are stored, it returns a nil |
| 2124 | value. |
| 2125 | |
| 2126 | :param class_AppletTCP applet: An :ref:`applettcp_class` |
| 2127 | :returns: the opaque data previsously stored, or nil if nothing is |
| 2128 | avalaible. |
| 2129 | |
| 2130 | .. js:function:: AppletTCP.set_priv(applet, data) |
| 2131 | |
| 2132 | Store any data in the current HAProxy transaction. This action replace the |
| 2133 | old stored data. |
| 2134 | |
| 2135 | :param class_AppletTCP applet: An :ref:`applettcp_class` |
| 2136 | :param opaque data: The data which is stored in the transaction. |
| 2137 | |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 2138 | External Lua libraries |
| 2139 | ====================== |
| 2140 | |
| 2141 | A lot of useful lua libraries can be found here: |
| 2142 | |
| 2143 | * `https://lua-toolbox.com/ <https://lua-toolbox.com/>`_ |
| 2144 | |
| 2145 | Redis acces: |
| 2146 | |
| 2147 | * `https://github.com/nrk/redis-lua <https://github.com/nrk/redis-lua>`_ |
| 2148 | |
| 2149 | This is an example about the usage of the Redis library with HAProxy. Note that |
| 2150 | each call of any function of this library can throw an error if the socket |
| 2151 | connection fails. |
| 2152 | |
| 2153 | .. code-block:: lua |
| 2154 | |
| 2155 | -- load the redis library |
| 2156 | local redis = require("redis"); |
| 2157 | |
| 2158 | function do_something(txn) |
| 2159 | |
| 2160 | -- create and connect new tcp socket |
| 2161 | local tcp = core.tcp(); |
| 2162 | tcp:settimeout(1); |
| 2163 | tcp:connect("127.0.0.1", 6379); |
| 2164 | |
| 2165 | -- use the redis library with this new socket |
| 2166 | local client = redis.connect({socket=tcp}); |
| 2167 | client:ping(); |
| 2168 | |
| 2169 | end |
| 2170 | |
| 2171 | OpenSSL: |
| 2172 | |
| 2173 | * `http://mkottman.github.io/luacrypto/index.html |
| 2174 | <http://mkottman.github.io/luacrypto/index.html>`_ |
| 2175 | |
| 2176 | * `https://github.com/brunoos/luasec/wiki |
| 2177 | <https://github.com/brunoos/luasec/wiki>`_ |
Thierry FOURNIER | 2e4893c | 2015-03-18 13:37:27 +0100 | [diff] [blame] | 2178 | |