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