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