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