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