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 |
Aurelien DARRAGON | c84899c | 2023-02-20 18:18:59 +0100 | [diff] [blame] | 23 | functions. Lua has 8 execution contexts. |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 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 | 1e9b1b6 | 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 | |
Christopher Faulet | 5a2c661 | 2021-08-15 20:35:25 +0200 | [diff] [blame] | 70 | 7. The **filter context**: It is a Lua object based on a class defining filter |
| 71 | callback functions. Lua filters are registered using |
| 72 | `core.register_filter()`. Each declared filter is prefixed by the string |
| 73 | "lua.". |
| 74 | |
Aurelien DARRAGON | c84899c | 2023-02-20 18:18:59 +0100 | [diff] [blame] | 75 | 8. The **event context**: Inside a function that handles events subscribed |
| 76 | through `core.event_sub()` or `Server.event_sub()`. |
| 77 | |
Christopher Faulet | 5a2c661 | 2021-08-15 20:35:25 +0200 | [diff] [blame] | 78 | |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 79 | HAProxy Lua Hello world |
| 80 | ----------------------- |
| 81 | |
| 82 | HAProxy configuration file (`hello_world.conf`): |
| 83 | |
| 84 | :: |
| 85 | |
| 86 | global |
| 87 | lua-load hello_world.lua |
| 88 | |
| 89 | listen proxy |
| 90 | bind 127.0.0.1:10001 |
Thierry FOURNIER | a2d0225 | 2015-10-01 15:00:42 +0200 | [diff] [blame] | 91 | tcp-request inspect-delay 1s |
| 92 | tcp-request content use-service lua.hello_world |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 93 | |
| 94 | HAProxy Lua file (`hello_world.lua`): |
| 95 | |
| 96 | .. code-block:: lua |
| 97 | |
Thierry FOURNIER | a2d0225 | 2015-10-01 15:00:42 +0200 | [diff] [blame] | 98 | core.register_service("hello_world", "tcp", function(applet) |
| 99 | applet:send("hello world\n") |
| 100 | end) |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 101 | |
| 102 | How to start HAProxy for testing this configuration: |
| 103 | |
| 104 | :: |
| 105 | |
| 106 | ./haproxy -f hello_world.conf |
| 107 | |
| 108 | On other terminal, you can test with telnet: |
| 109 | |
| 110 | :: |
| 111 | |
| 112 | #:~ telnet 127.0.0.1 10001 |
| 113 | hello world |
| 114 | |
Thierry Fournier | ae6b568 | 2022-09-19 09:04:16 +0200 | [diff] [blame] | 115 | Usage of load parameters |
| 116 | ------------------------ |
| 117 | |
Ilya Shipitsin | 4a689da | 2022-10-29 09:34:32 +0500 | [diff] [blame] | 118 | HAProxy lua-load(-per-thread) directives allow a list of parameters after |
Thierry Fournier | ae6b568 | 2022-09-19 09:04:16 +0200 | [diff] [blame] | 119 | the lua file name. These parameters are accessible through an array of args |
| 120 | using this code `local args = table.pack(...)` in the body of loaded file. |
| 121 | |
| 122 | Below, a new version of the hello world using load parameters |
| 123 | |
| 124 | HAProxy configuration file (`hello_world.conf`): |
| 125 | |
| 126 | :: |
| 127 | |
| 128 | global |
| 129 | lua-load hello_world.lua "this is not an hello world" |
| 130 | |
| 131 | listen proxy |
| 132 | bind 127.0.0.1:10001 |
| 133 | tcp-request inspect-delay 1s |
| 134 | tcp-request content use-service lua.hello_world |
| 135 | |
| 136 | HAProxy Lua file (`hello_world.lua`): |
| 137 | |
| 138 | .. code-block:: lua |
| 139 | |
| 140 | local args = table.pack(...) |
| 141 | |
| 142 | core.register_service("hello_world", "tcp", function(applet) |
| 143 | applet:send(args[1] .. "\n") |
| 144 | end) |
| 145 | |
| 146 | |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 147 | Core class |
| 148 | ========== |
| 149 | |
| 150 | .. js:class:: core |
| 151 | |
| 152 | The "core" class contains all the HAProxy core functions. These function are |
Aurelien DARRAGON | 53901f4 | 2022-10-13 19:49:42 +0200 | [diff] [blame] | 153 | useful for the controlling of the execution flow, registering hooks, manipulating |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 154 | global maps or ACL, ... |
| 155 | |
| 156 | "core" class is basically provided with HAProxy. No `require` line is |
| 157 | required to uses these function. |
| 158 | |
David Carlier | 61fdf8b | 2015-10-02 11:59:38 +0100 | [diff] [blame] | 159 | 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] | 160 | type. |
| 161 | |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 162 | .. js:attribute:: core.emerg |
| 163 | |
Thierry FOURNIER | dc59500 | 2015-12-21 11:13:52 +0100 | [diff] [blame] | 164 | :returns: integer |
| 165 | |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 166 | This attribute is an integer, it contains the value of the loglevel "emergency" (0). |
| 167 | |
| 168 | .. js:attribute:: core.alert |
| 169 | |
Thierry FOURNIER | dc59500 | 2015-12-21 11:13:52 +0100 | [diff] [blame] | 170 | :returns: integer |
| 171 | |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 172 | This attribute is an integer, it contains the value of the loglevel "alert" (1). |
| 173 | |
| 174 | .. js:attribute:: core.crit |
| 175 | |
Thierry FOURNIER | dc59500 | 2015-12-21 11:13:52 +0100 | [diff] [blame] | 176 | :returns: integer |
| 177 | |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 178 | This attribute is an integer, it contains the value of the loglevel "critical" (2). |
| 179 | |
| 180 | .. js:attribute:: core.err |
| 181 | |
Thierry FOURNIER | dc59500 | 2015-12-21 11:13:52 +0100 | [diff] [blame] | 182 | :returns: integer |
| 183 | |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 184 | This attribute is an integer, it contains the value of the loglevel "error" (3). |
| 185 | |
| 186 | .. js:attribute:: core.warning |
| 187 | |
Thierry FOURNIER | dc59500 | 2015-12-21 11:13:52 +0100 | [diff] [blame] | 188 | :returns: integer |
| 189 | |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 190 | This attribute is an integer, it contains the value of the loglevel "warning" (4). |
| 191 | |
| 192 | .. js:attribute:: core.notice |
| 193 | |
Thierry FOURNIER | dc59500 | 2015-12-21 11:13:52 +0100 | [diff] [blame] | 194 | :returns: integer |
| 195 | |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 196 | This attribute is an integer, it contains the value of the loglevel "notice" (5). |
| 197 | |
| 198 | .. js:attribute:: core.info |
| 199 | |
Thierry FOURNIER | dc59500 | 2015-12-21 11:13:52 +0100 | [diff] [blame] | 200 | :returns: integer |
| 201 | |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 202 | This attribute is an integer, it contains the value of the loglevel "info" (6). |
| 203 | |
| 204 | .. js:attribute:: core.debug |
| 205 | |
Thierry FOURNIER | dc59500 | 2015-12-21 11:13:52 +0100 | [diff] [blame] | 206 | :returns: integer |
| 207 | |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 208 | This attribute is an integer, it contains the value of the loglevel "debug" (7). |
| 209 | |
Thierry FOURNIER | 12a865d | 2016-12-14 19:40:37 +0100 | [diff] [blame] | 210 | .. js:attribute:: core.proxies |
| 211 | |
| 212 | **context**: task, action, sample-fetch, converter |
| 213 | |
Patrick Hemmer | c6a1d71 | 2018-05-01 21:30:41 -0400 | [diff] [blame] | 214 | This attribute is a table of declared proxies (frontend and backends). Each |
| 215 | proxy give an access to his list of listeners and servers. The table is |
| 216 | 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] | 217 | |
Christopher Faulet | 1e9b1b6 | 2021-08-11 10:14:30 +0200 | [diff] [blame] | 218 | .. Warning:: |
Aurelien DARRAGON | 53901f4 | 2022-10-13 19:49:42 +0200 | [diff] [blame] | 219 | if you declared a frontend and backend with the same name, only one of |
| 220 | them will be listed. |
Thierry FOURNIER | 9b82a58 | 2017-07-24 13:30:43 +0200 | [diff] [blame] | 221 | |
| 222 | :see: :js:attr:`core.backends` |
| 223 | :see: :js:attr:`core.frontends` |
| 224 | |
| 225 | .. js:attribute:: core.backends |
| 226 | |
| 227 | **context**: task, action, sample-fetch, converter |
| 228 | |
Patrick Hemmer | c6a1d71 | 2018-05-01 21:30:41 -0400 | [diff] [blame] | 229 | This attribute is a table of declared proxies with backend capability. Each |
| 230 | proxy give an access to his list of listeners and servers. The table is |
| 231 | 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] | 232 | |
| 233 | :see: :js:attr:`core.proxies` |
| 234 | :see: :js:attr:`core.frontends` |
| 235 | |
| 236 | .. js:attribute:: core.frontends |
| 237 | |
| 238 | **context**: task, action, sample-fetch, converter |
| 239 | |
Patrick Hemmer | c6a1d71 | 2018-05-01 21:30:41 -0400 | [diff] [blame] | 240 | This attribute is a table of declared proxies with frontend capability. Each |
| 241 | proxy give an access to his list of listeners and servers. The table is |
| 242 | 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] | 243 | |
| 244 | :see: :js:attr:`core.proxies` |
| 245 | :see: :js:attr:`core.backends` |
| 246 | |
Thierry Fournier | ecb83c2 | 2020-11-28 15:49:44 +0100 | [diff] [blame] | 247 | .. js:attribute:: core.thread |
| 248 | |
| 249 | **context**: task, action, sample-fetch, converter, applet |
| 250 | |
| 251 | This variable contains the executing thread number starting at 1. 0 is a |
| 252 | special case for the common lua context. So, if thread is 0, Lua scope is |
| 253 | shared by all threads, otherwise the scope is dedicated to a single thread. |
| 254 | A program which needs to execute some parts exactly once regardless of the |
| 255 | number of threads can check that core.thread is 0 or 1. |
| 256 | |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 257 | .. js:function:: core.log(loglevel, msg) |
| 258 | |
| 259 | **context**: body, init, task, action, sample-fetch, converter |
| 260 | |
David Carlier | 61fdf8b | 2015-10-02 11:59:38 +0100 | [diff] [blame] | 261 | 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] | 262 | configuration file, on the default syslog server if it is configured and on |
| 263 | the stderr if it is allowed. |
| 264 | |
Bertrand Jacquin | 874a35c | 2018-09-10 21:26:07 +0100 | [diff] [blame] | 265 | :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] | 266 | number between 0 and 7. |
| 267 | :param string msg: The log content. |
Thierry FOURNIER | 12a865d | 2016-12-14 19:40:37 +0100 | [diff] [blame] | 268 | :see: :js:attr:`core.emerg`, :js:attr:`core.alert`, :js:attr:`core.crit`, |
| 269 | :js:attr:`core.err`, :js:attr:`core.warning`, :js:attr:`core.notice`, |
| 270 | :js:attr:`core.info`, :js:attr:`core.debug` (log level definitions) |
| 271 | :see: :js:func:`core.Debug` |
| 272 | :see: :js:func:`core.Info` |
| 273 | :see: :js:func:`core.Warning` |
| 274 | :see: :js:func:`core.Alert` |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 275 | |
| 276 | .. js:function:: core.Debug(msg) |
| 277 | |
| 278 | **context**: body, init, task, action, sample-fetch, converter |
| 279 | |
| 280 | :param string msg: The log content. |
Thierry FOURNIER | 12a865d | 2016-12-14 19:40:37 +0100 | [diff] [blame] | 281 | :see: :js:func:`core.log` |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 282 | |
| 283 | Does the same job than: |
| 284 | |
| 285 | .. code-block:: lua |
| 286 | |
Thierry FOURNIER | dc59500 | 2015-12-21 11:13:52 +0100 | [diff] [blame] | 287 | function Debug(msg) |
| 288 | core.log(core.debug, msg) |
| 289 | end |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 290 | .. |
| 291 | |
| 292 | .. js:function:: core.Info(msg) |
| 293 | |
| 294 | **context**: body, init, task, action, sample-fetch, converter |
| 295 | |
| 296 | :param string msg: The log content. |
Thierry FOURNIER | 12a865d | 2016-12-14 19:40:37 +0100 | [diff] [blame] | 297 | :see: :js:func:`core.log` |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 298 | |
| 299 | .. code-block:: lua |
| 300 | |
Thierry FOURNIER | dc59500 | 2015-12-21 11:13:52 +0100 | [diff] [blame] | 301 | function Info(msg) |
| 302 | core.log(core.info, msg) |
| 303 | end |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 304 | .. |
| 305 | |
| 306 | .. js:function:: core.Warning(msg) |
| 307 | |
| 308 | **context**: body, init, task, action, sample-fetch, converter |
| 309 | |
| 310 | :param string msg: The log content. |
Thierry FOURNIER | 12a865d | 2016-12-14 19:40:37 +0100 | [diff] [blame] | 311 | :see: :js:func:`core.log` |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 312 | |
| 313 | .. code-block:: lua |
| 314 | |
Thierry FOURNIER | dc59500 | 2015-12-21 11:13:52 +0100 | [diff] [blame] | 315 | function Warning(msg) |
| 316 | core.log(core.warning, msg) |
| 317 | end |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 318 | .. |
| 319 | |
| 320 | .. js:function:: core.Alert(msg) |
| 321 | |
| 322 | **context**: body, init, task, action, sample-fetch, converter |
| 323 | |
| 324 | :param string msg: The log content. |
Thierry FOURNIER | 12a865d | 2016-12-14 19:40:37 +0100 | [diff] [blame] | 325 | :see: :js:func:`core.log` |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 326 | |
| 327 | .. code-block:: lua |
| 328 | |
Thierry FOURNIER | dc59500 | 2015-12-21 11:13:52 +0100 | [diff] [blame] | 329 | function Alert(msg) |
| 330 | core.log(core.alert, msg) |
| 331 | end |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 332 | .. |
| 333 | |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 334 | .. js:function:: core.add_acl(filename, key) |
| 335 | |
| 336 | **context**: init, task, action, sample-fetch, converter |
| 337 | |
| 338 | Add the ACL *key* in the ACLs list referenced by the file *filename*. |
| 339 | |
| 340 | :param string filename: the filename that reference the ACL entries. |
| 341 | :param string key: the key which will be added. |
| 342 | |
| 343 | .. js:function:: core.del_acl(filename, key) |
| 344 | |
| 345 | **context**: init, task, action, sample-fetch, converter |
| 346 | |
| 347 | Delete the ACL entry referenced by the key *key* in the list of ACLs |
| 348 | referenced by *filename*. |
| 349 | |
| 350 | :param string filename: the filename that reference the ACL entries. |
| 351 | :param string key: the key which will be deleted. |
| 352 | |
| 353 | .. js:function:: core.del_map(filename, key) |
| 354 | |
| 355 | **context**: init, task, action, sample-fetch, converter |
| 356 | |
| 357 | Delete the map entry indexed with the specified key in the list of maps |
| 358 | referenced by his filename. |
| 359 | |
| 360 | :param string filename: the filename that reference the map entries. |
| 361 | :param string key: the key which will be deleted. |
| 362 | |
Thierry Fournier | eea77c0 | 2016-03-18 08:47:13 +0100 | [diff] [blame] | 363 | .. js:function:: core.get_info() |
| 364 | |
| 365 | **context**: body, init, task, action, sample-fetch, converter |
| 366 | |
Aurelien DARRAGON | 53901f4 | 2022-10-13 19:49:42 +0200 | [diff] [blame] | 367 | Returns HAProxy core information. We can find information like the uptime, |
Thierry Fournier | eea77c0 | 2016-03-18 08:47:13 +0100 | [diff] [blame] | 368 | the pid, memory pool usage, tasks number, ... |
| 369 | |
Ilya Shipitsin | 5fa29b8 | 2022-12-07 09:46:19 +0500 | [diff] [blame] | 370 | This information is also returned by the management socket via the command |
Bertrand Jacquin | 874a35c | 2018-09-10 21:26:07 +0100 | [diff] [blame] | 371 | "show info". See the management socket documentation for more information |
Thierry Fournier | eea77c0 | 2016-03-18 08:47:13 +0100 | [diff] [blame] | 372 | about the content of these variables. |
| 373 | |
| 374 | :returns: an array of values. |
| 375 | |
Thierry Fournier | b1f4656 | 2016-01-21 09:46:15 +0100 | [diff] [blame] | 376 | .. js:function:: core.now() |
| 377 | |
| 378 | **context**: body, init, task, action |
| 379 | |
| 380 | 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] | 381 | 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] | 382 | call 'gettimeofday' will not be called too. The time is refreshed between each |
| 383 | Lua execution or resume, so two consecutive call to the function "now" will |
| 384 | probably returns the same result. |
| 385 | |
Patrick Hemmer | c6a1d71 | 2018-05-01 21:30:41 -0400 | [diff] [blame] | 386 | :returns: a table which contains two entries "sec" and "usec". "sec" |
Thierry Fournier | b1f4656 | 2016-01-21 09:46:15 +0100 | [diff] [blame] | 387 | contains the current at the epoch format, and "usec" contains the |
| 388 | current microseconds. |
| 389 | |
Thierry FOURNIER | a78f037 | 2016-12-14 19:04:41 +0100 | [diff] [blame] | 390 | .. js:function:: core.http_date(date) |
| 391 | |
| 392 | **context**: body, init, task, action |
| 393 | |
Bertrand Jacquin | 874a35c | 2018-09-10 21:26:07 +0100 | [diff] [blame] | 394 | This function take a string representing http date, and returns an integer |
Thierry FOURNIER | a78f037 | 2016-12-14 19:04:41 +0100 | [diff] [blame] | 395 | containing the corresponding date with a epoch format. A valid http date |
| 396 | me respect the format IMF, RFC850 or ASCTIME. |
| 397 | |
| 398 | :param string date: a date http-date formatted |
| 399 | :returns: integer containing epoch date |
| 400 | :see: :js:func:`core.imf_date`. |
| 401 | :see: :js:func:`core.rfc850_date`. |
| 402 | :see: :js:func:`core.asctime_date`. |
| 403 | :see: https://tools.ietf.org/html/rfc7231#section-7.1.1.1 |
| 404 | |
| 405 | .. js:function:: core.imf_date(date) |
| 406 | |
| 407 | **context**: body, init, task, action |
| 408 | |
Bertrand Jacquin | 874a35c | 2018-09-10 21:26:07 +0100 | [diff] [blame] | 409 | This function take a string representing IMF date, and returns an integer |
Thierry FOURNIER | a78f037 | 2016-12-14 19:04:41 +0100 | [diff] [blame] | 410 | containing the corresponding date with a epoch format. |
| 411 | |
| 412 | :param string date: a date IMF formatted |
| 413 | :returns: integer containing epoch date |
| 414 | :see: https://tools.ietf.org/html/rfc7231#section-7.1.1.1 |
| 415 | |
| 416 | The IMF format is like this: |
| 417 | |
| 418 | .. code-block:: text |
| 419 | |
| 420 | Sun, 06 Nov 1994 08:49:37 GMT |
| 421 | .. |
| 422 | |
| 423 | .. js:function:: core.rfc850_date(date) |
| 424 | |
| 425 | **context**: body, init, task, action |
| 426 | |
Bertrand Jacquin | 874a35c | 2018-09-10 21:26:07 +0100 | [diff] [blame] | 427 | This function take a string representing RFC850 date, and returns an integer |
Thierry FOURNIER | a78f037 | 2016-12-14 19:04:41 +0100 | [diff] [blame] | 428 | containing the corresponding date with a epoch format. |
| 429 | |
| 430 | :param string date: a date RFC859 formatted |
| 431 | :returns: integer containing epoch date |
| 432 | :see: https://tools.ietf.org/html/rfc7231#section-7.1.1.1 |
| 433 | |
| 434 | The RFC850 format is like this: |
| 435 | |
| 436 | .. code-block:: text |
| 437 | |
| 438 | Sunday, 06-Nov-94 08:49:37 GMT |
| 439 | .. |
| 440 | |
| 441 | .. js:function:: core.asctime_date(date) |
| 442 | |
| 443 | **context**: body, init, task, action |
| 444 | |
Bertrand Jacquin | 874a35c | 2018-09-10 21:26:07 +0100 | [diff] [blame] | 445 | This function take a string representing ASCTIME date, and returns an integer |
Thierry FOURNIER | a78f037 | 2016-12-14 19:04:41 +0100 | [diff] [blame] | 446 | containing the corresponding date with a epoch format. |
| 447 | |
| 448 | :param string date: a date ASCTIME formatted |
| 449 | :returns: integer containing epoch date |
| 450 | :see: https://tools.ietf.org/html/rfc7231#section-7.1.1.1 |
| 451 | |
| 452 | The ASCTIME format is like this: |
| 453 | |
| 454 | .. code-block:: text |
| 455 | |
| 456 | Sun Nov 6 08:49:37 1994 |
| 457 | .. |
| 458 | |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 459 | .. js:function:: core.msleep(milliseconds) |
| 460 | |
| 461 | **context**: body, init, task, action |
| 462 | |
| 463 | The `core.msleep()` stops the Lua execution between specified milliseconds. |
| 464 | |
| 465 | :param integer milliseconds: the required milliseconds. |
| 466 | |
Thierry FOURNIER | c5d11c6 | 2018-02-12 14:46:54 +0100 | [diff] [blame] | 467 | .. js:function:: core.register_action(name, actions, func [, nb_args]) |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 468 | |
| 469 | **context**: body |
| 470 | |
David Carlier | 61fdf8b | 2015-10-02 11:59:38 +0100 | [diff] [blame] | 471 | 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] | 472 | used in HAProxy with the prefix "lua.". An action gets a TXN object class as |
| 473 | input. |
| 474 | |
| 475 | :param string name: is the name of the converter. |
Willy Tarreau | 61add3c | 2015-09-28 15:39:10 +0200 | [diff] [blame] | 476 | :param table actions: is a table of string describing the HAProxy actions who |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 477 | want to register to. The expected actions are 'tcp-req', |
| 478 | 'tcp-res', 'http-req' or 'http-res'. |
Aurelien DARRAGON | 53901f4 | 2022-10-13 19:49:42 +0200 | [diff] [blame] | 479 | :param function func: is the Lua function called to work as converter. |
Thierry FOURNIER | c5d11c6 | 2018-02-12 14:46:54 +0100 | [diff] [blame] | 480 | :param integer nb_args: is the expected number of argument for the action. |
| 481 | By default the value is 0. |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 482 | |
| 483 | The prototype of the Lua function used as argument is: |
| 484 | |
| 485 | .. code-block:: lua |
| 486 | |
Thierry FOURNIER | c5d11c6 | 2018-02-12 14:46:54 +0100 | [diff] [blame] | 487 | function(txn [, arg1 [, arg2]]) |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 488 | .. |
| 489 | |
Thierry FOURNIER | dc59500 | 2015-12-21 11:13:52 +0100 | [diff] [blame] | 490 | * **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] | 491 | current request or TCP stream. |
| 492 | |
Bertrand Jacquin | 874a35c | 2018-09-10 21:26:07 +0100 | [diff] [blame] | 493 | * **argX**: this is argument provided through the HAProxy configuration file. |
Thierry FOURNIER | c5d11c6 | 2018-02-12 14:46:54 +0100 | [diff] [blame] | 494 | |
Bertrand Jacquin | 874a35c | 2018-09-10 21:26:07 +0100 | [diff] [blame] | 495 | 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] | 496 | in the logs. |
| 497 | |
| 498 | .. code-block:: lua |
| 499 | |
| 500 | core.register_action("hello-world", { "tcp-req", "http-req" }, function(txn) |
| 501 | txn:Info("Hello world") |
| 502 | end) |
| 503 | .. |
| 504 | |
Willy Tarreau | 714f345 | 2021-05-09 06:47:26 +0200 | [diff] [blame] | 505 | This example code is used in HAProxy configuration like this: |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 506 | |
| 507 | :: |
| 508 | |
| 509 | frontend tcp_frt |
| 510 | mode tcp |
| 511 | tcp-request content lua.hello-world |
| 512 | |
| 513 | frontend http_frt |
| 514 | mode http |
| 515 | http-request lua.hello-world |
Aurelien DARRAGON | d5c80d7 | 2023-03-13 19:36:13 +0100 | [diff] [blame] | 516 | |
Thierry FOURNIER | c5d11c6 | 2018-02-12 14:46:54 +0100 | [diff] [blame] | 517 | .. |
| 518 | |
Bertrand Jacquin | 874a35c | 2018-09-10 21:26:07 +0100 | [diff] [blame] | 519 | A second example using arguments |
Thierry FOURNIER | c5d11c6 | 2018-02-12 14:46:54 +0100 | [diff] [blame] | 520 | |
| 521 | .. code-block:: lua |
| 522 | |
| 523 | function hello_world(txn, arg) |
| 524 | txn:Info("Hello world for " .. arg) |
| 525 | end |
| 526 | core.register_action("hello-world", { "tcp-req", "http-req" }, hello_world, 2) |
Aurelien DARRAGON | d5c80d7 | 2023-03-13 19:36:13 +0100 | [diff] [blame] | 527 | |
Thierry FOURNIER | c5d11c6 | 2018-02-12 14:46:54 +0100 | [diff] [blame] | 528 | .. |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 529 | |
Willy Tarreau | 714f345 | 2021-05-09 06:47:26 +0200 | [diff] [blame] | 530 | This example code is used in HAProxy configuration like this: |
Thierry FOURNIER | c5d11c6 | 2018-02-12 14:46:54 +0100 | [diff] [blame] | 531 | |
| 532 | :: |
| 533 | |
| 534 | frontend tcp_frt |
| 535 | mode tcp |
| 536 | tcp-request content lua.hello-world everybody |
Aurelien DARRAGON | d5c80d7 | 2023-03-13 19:36:13 +0100 | [diff] [blame] | 537 | |
Thierry FOURNIER | c5d11c6 | 2018-02-12 14:46:54 +0100 | [diff] [blame] | 538 | .. |
Aurelien DARRAGON | 53901f4 | 2022-10-13 19:49:42 +0200 | [diff] [blame] | 539 | |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 540 | .. js:function:: core.register_converters(name, func) |
| 541 | |
| 542 | **context**: body |
| 543 | |
David Carlier | 61fdf8b | 2015-10-02 11:59:38 +0100 | [diff] [blame] | 544 | Register a Lua function executed as converter. All the registered converters |
Aurelien DARRAGON | 53901f4 | 2022-10-13 19:49:42 +0200 | [diff] [blame] | 545 | can be used in HAProxy with the prefix "lua.". A converter gets a string as |
| 546 | input and returns a string as output. The registered function can take up to 9 |
| 547 | values as parameter. All the values are strings. |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 548 | |
| 549 | :param string name: is the name of the converter. |
| 550 | :param function func: is the Lua function called to work as converter. |
| 551 | |
| 552 | The prototype of the Lua function used as argument is: |
| 553 | |
| 554 | .. code-block:: lua |
| 555 | |
| 556 | function(str, [p1 [, p2 [, ... [, p5]]]]) |
| 557 | .. |
| 558 | |
| 559 | * **str** (*string*): this is the input value automatically converted in |
| 560 | string. |
| 561 | * **p1** .. **p5** (*string*): this is a list of string arguments declared in |
Bertrand Jacquin | 874a35c | 2018-09-10 21:26:07 +0100 | [diff] [blame] | 562 | the HAProxy configuration file. The number of arguments doesn't exceed 5. |
Aurelien DARRAGON | 53901f4 | 2022-10-13 19:49:42 +0200 | [diff] [blame] | 563 | The order and the nature of these is conventionally chosen by the |
Bertrand Jacquin | 874a35c | 2018-09-10 21:26:07 +0100 | [diff] [blame] | 564 | developer. |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 565 | |
| 566 | .. js:function:: core.register_fetches(name, func) |
| 567 | |
| 568 | **context**: body |
| 569 | |
David Carlier | 61fdf8b | 2015-10-02 11:59:38 +0100 | [diff] [blame] | 570 | Register a Lua function executed as sample fetch. All the registered sample |
Bertrand Jacquin | 874a35c | 2018-09-10 21:26:07 +0100 | [diff] [blame] | 571 | fetch can be used in HAProxy with the prefix "lua.". A Lua sample fetch |
Aurelien DARRAGON | 53901f4 | 2022-10-13 19:49:42 +0200 | [diff] [blame] | 572 | returns a string as output. The registered function can take up to 9 values as |
| 573 | parameter. All the values are strings. |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 574 | |
Aurelien DARRAGON | 53901f4 | 2022-10-13 19:49:42 +0200 | [diff] [blame] | 575 | :param string name: is the name of the sample fetch. |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 576 | :param function func: is the Lua function called to work as sample fetch. |
| 577 | |
| 578 | The prototype of the Lua function used as argument is: |
| 579 | |
| 580 | .. code-block:: lua |
| 581 | |
| 582 | string function(txn, [p1 [, p2 [, ... [, p5]]]]) |
| 583 | .. |
| 584 | |
Thierry FOURNIER | dc59500 | 2015-12-21 11:13:52 +0100 | [diff] [blame] | 585 | * **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] | 586 | request. |
| 587 | * **p1** .. **p5** (*string*): this is a list of string arguments declared in |
Bertrand Jacquin | 874a35c | 2018-09-10 21:26:07 +0100 | [diff] [blame] | 588 | the HAProxy configuration file. The number of arguments doesn't exceed 5. |
Aurelien DARRAGON | 53901f4 | 2022-10-13 19:49:42 +0200 | [diff] [blame] | 589 | The order and the nature of these is conventionally chosen by the |
Bertrand Jacquin | 874a35c | 2018-09-10 21:26:07 +0100 | [diff] [blame] | 590 | developer. |
| 591 | * **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] | 592 | returned now. |
| 593 | |
| 594 | lua example code: |
| 595 | |
| 596 | .. code-block:: lua |
| 597 | |
| 598 | core.register_fetches("hello", function(txn) |
| 599 | return "hello" |
| 600 | end) |
| 601 | .. |
| 602 | |
| 603 | HAProxy example configuration: |
| 604 | |
| 605 | :: |
| 606 | |
| 607 | frontend example |
| 608 | http-request redirect location /%[lua.hello] |
| 609 | |
Christopher Faulet | 5a2c661 | 2021-08-15 20:35:25 +0200 | [diff] [blame] | 610 | .. js:function:: core.register_filter(name, Flt, func) |
| 611 | |
| 612 | **context**: body |
| 613 | |
| 614 | Register a Lua function used to declare a filter. All the registered filters |
| 615 | can by used in HAProxy with the prefix "lua.". |
| 616 | |
| 617 | :param string name: is the name of the filter. |
| 618 | :param table Flt: is a Lua class containing the filter definition (id, flags, |
| 619 | callbacks). |
| 620 | :param function func: is the Lua function called to create the Lua filter. |
| 621 | |
| 622 | The prototype of the Lua function used as argument is: |
| 623 | |
| 624 | .. code-block:: lua |
| 625 | |
| 626 | function(flt, args) |
| 627 | .. |
| 628 | |
| 629 | * **flt** : Is a filter object based on the class provided in |
| 630 | :js:func:`core.register_filter()` function. |
| 631 | |
| 632 | * **args**: Is a table of strings containing all arguments provided through |
| 633 | the HAProxy configuration file, on the filter line. |
| 634 | |
| 635 | It must return the filter to use or nil to ignore it. Here, an example of |
| 636 | filter registration. |
| 637 | |
| 638 | .. code-block:: lua |
| 639 | |
| 640 | core.register_filter("my-filter", MyFilter, function(flt, args) |
| 641 | flt.args = args -- Save arguments |
| 642 | return flt |
| 643 | end) |
| 644 | .. |
| 645 | |
| 646 | This example code is used in HAProxy configuration like this: |
| 647 | |
| 648 | :: |
| 649 | |
| 650 | frontend http |
| 651 | mode http |
| 652 | filter lua.my-filter arg1 arg2 arg3 |
Aurelien DARRAGON | d5c80d7 | 2023-03-13 19:36:13 +0100 | [diff] [blame] | 653 | |
Christopher Faulet | 5a2c661 | 2021-08-15 20:35:25 +0200 | [diff] [blame] | 654 | .. |
| 655 | |
| 656 | :see: :js:class:`Filter` |
| 657 | |
Thierry FOURNIER | a3bc513 | 2015-09-25 21:43:56 +0200 | [diff] [blame] | 658 | .. js:function:: core.register_service(name, mode, func) |
| 659 | |
| 660 | **context**: body |
| 661 | |
Aurelien DARRAGON | 53901f4 | 2022-10-13 19:49:42 +0200 | [diff] [blame] | 662 | Register a Lua function executed as a service. All the registered services can |
Thierry FOURNIER | a3bc513 | 2015-09-25 21:43:56 +0200 | [diff] [blame] | 663 | be used in HAProxy with the prefix "lua.". A service gets an object class as |
| 664 | input according with the required mode. |
| 665 | |
Aurelien DARRAGON | 53901f4 | 2022-10-13 19:49:42 +0200 | [diff] [blame] | 666 | :param string name: is the name of the service. |
Willy Tarreau | 61add3c | 2015-09-28 15:39:10 +0200 | [diff] [blame] | 667 | :param string mode: is string describing the required mode. Only 'tcp' or |
Thierry FOURNIER | a3bc513 | 2015-09-25 21:43:56 +0200 | [diff] [blame] | 668 | 'http' are allowed. |
Aurelien DARRAGON | 53901f4 | 2022-10-13 19:49:42 +0200 | [diff] [blame] | 669 | :param function func: is the Lua function called to work as service. |
Thierry FOURNIER | a3bc513 | 2015-09-25 21:43:56 +0200 | [diff] [blame] | 670 | |
| 671 | The prototype of the Lua function used as argument is: |
| 672 | |
| 673 | .. code-block:: lua |
| 674 | |
| 675 | function(applet) |
| 676 | .. |
| 677 | |
Thierry FOURNIER | dc59500 | 2015-12-21 11:13:52 +0100 | [diff] [blame] | 678 | * **applet** *applet* will be a :ref:`applettcp_class` or a |
| 679 | :ref:`applethttp_class`. It depends the type of registered applet. An applet |
| 680 | registered with the 'http' value for the *mode* parameter will gets a |
| 681 | :ref:`applethttp_class`. If the *mode* value is 'tcp', the applet will gets |
| 682 | a :ref:`applettcp_class`. |
| 683 | |
Christopher Faulet | 1e9b1b6 | 2021-08-11 10:14:30 +0200 | [diff] [blame] | 684 | .. warning:: |
| 685 | Applets of type 'http' cannot be called from 'tcp-*' rulesets. Only the |
| 686 | 'http-*' rulesets are authorized, this means that is not possible to call |
Aurelien DARRAGON | 53901f4 | 2022-10-13 19:49:42 +0200 | [diff] [blame] | 687 | a HTTP applet from a proxy in tcp mode. Applets of type 'tcp' can be |
Christopher Faulet | 1e9b1b6 | 2021-08-11 10:14:30 +0200 | [diff] [blame] | 688 | called from anywhere. |
Thierry FOURNIER | a3bc513 | 2015-09-25 21:43:56 +0200 | [diff] [blame] | 689 | |
Bertrand Jacquin | 874a35c | 2018-09-10 21:26:07 +0100 | [diff] [blame] | 690 | 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] | 691 | as an http response. |
| 692 | |
| 693 | .. code-block:: lua |
| 694 | |
Pieter Baauw | 4d7f766 | 2015-11-08 16:38:08 +0100 | [diff] [blame] | 695 | core.register_service("hello-world", "http", function(applet) |
Thierry FOURNIER | a3bc513 | 2015-09-25 21:43:56 +0200 | [diff] [blame] | 696 | local response = "Hello World !" |
| 697 | applet:set_status(200) |
Pieter Baauw | 2dcb9bc | 2015-10-01 22:47:12 +0200 | [diff] [blame] | 698 | applet:add_header("content-length", string.len(response)) |
Thierry FOURNIER | a3bc513 | 2015-09-25 21:43:56 +0200 | [diff] [blame] | 699 | applet:add_header("content-type", "text/plain") |
Pieter Baauw | 2dcb9bc | 2015-10-01 22:47:12 +0200 | [diff] [blame] | 700 | applet:start_response() |
Thierry FOURNIER | a3bc513 | 2015-09-25 21:43:56 +0200 | [diff] [blame] | 701 | applet:send(response) |
| 702 | end) |
| 703 | .. |
| 704 | |
Willy Tarreau | 714f345 | 2021-05-09 06:47:26 +0200 | [diff] [blame] | 705 | This example code is used in HAProxy configuration like this: |
Thierry FOURNIER | a3bc513 | 2015-09-25 21:43:56 +0200 | [diff] [blame] | 706 | |
| 707 | :: |
| 708 | |
| 709 | frontend example |
| 710 | http-request use-service lua.hello-world |
| 711 | |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 712 | .. js:function:: core.register_init(func) |
| 713 | |
| 714 | **context**: body |
| 715 | |
| 716 | Register a function executed after the configuration parsing. This is useful |
| 717 | to check any parameters. |
| 718 | |
Pieter Baauw | 4d7f766 | 2015-11-08 16:38:08 +0100 | [diff] [blame] | 719 | :param function func: is the Lua function called to work as initializer. |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 720 | |
| 721 | The prototype of the Lua function used as argument is: |
| 722 | |
| 723 | .. code-block:: lua |
| 724 | |
| 725 | function() |
| 726 | .. |
| 727 | |
| 728 | It takes no input, and no output is expected. |
| 729 | |
Aurelien DARRAGON | b803899 | 2023-03-09 16:48:30 +0100 | [diff] [blame] | 730 | .. js:function:: core.register_task(func[, arg1[, arg2[, ...[, arg4]]]]) |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 731 | |
Aurelien DARRAGON | c84899c | 2023-02-20 18:18:59 +0100 | [diff] [blame] | 732 | **context**: body, init, task, action, sample-fetch, converter, event |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 733 | |
| 734 | Register and start independent task. The task is started when the HAProxy |
| 735 | 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] | 736 | perform complex health checks. |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 737 | |
Aurelien DARRAGON | b803899 | 2023-03-09 16:48:30 +0100 | [diff] [blame] | 738 | :param function func: is the Lua function called to work as an async task. |
| 739 | |
| 740 | Up to 4 optional arguments (all types supported) may be passed to the function. |
| 741 | (They will be passed as-is to the task function) |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 742 | |
| 743 | The prototype of the Lua function used as argument is: |
| 744 | |
| 745 | .. code-block:: lua |
| 746 | |
Aurelien DARRAGON | b803899 | 2023-03-09 16:48:30 +0100 | [diff] [blame] | 747 | function([arg1[, arg2[, ...[, arg4]]]]) |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 748 | .. |
| 749 | |
Aurelien DARRAGON | b803899 | 2023-03-09 16:48:30 +0100 | [diff] [blame] | 750 | It takes up to 4 optional arguments (provided when registering), and no output is expected. |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 751 | |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 752 | .. js:function:: core.register_cli([path], usage, func) |
| 753 | |
| 754 | **context**: body |
| 755 | |
Aurelien DARRAGON | 53901f4 | 2022-10-13 19:49:42 +0200 | [diff] [blame] | 756 | Register a custom cli that will be available from haproxy stats socket. |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 757 | |
| 758 | :param array path: is the sequence of word for which the cli execute the Lua |
| 759 | binding. |
| 760 | :param string usage: is the usage message displayed in the help. |
| 761 | :param function func: is the Lua function called to handle the CLI commands. |
| 762 | |
| 763 | The prototype of the Lua function used as argument is: |
| 764 | |
| 765 | .. code-block:: lua |
| 766 | |
| 767 | function(AppletTCP, [arg1, [arg2, [...]]]) |
| 768 | .. |
| 769 | |
| 770 | 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] | 771 | 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] | 772 | this: |
| 773 | |
| 774 | .. code-block:: lua |
| 775 | |
| 776 | core.register_cli({"show", "ssl", "stats"}, "Display SSL stats..", function(applet, arg1, arg2, arg3, arg4, arg5) |
| 777 | end) |
| 778 | .. |
| 779 | |
| 780 | And we execute this in the prompt: |
| 781 | |
| 782 | .. code-block:: text |
| 783 | |
| 784 | > prompt |
| 785 | > show ssl stats all |
| 786 | .. |
| 787 | |
Bertrand Jacquin | 874a35c | 2018-09-10 21:26:07 +0100 | [diff] [blame] | 788 | 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] | 789 | arg4 will contain "all". arg5 contains nil. |
| 790 | |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 791 | .. js:function:: core.set_nice(nice) |
| 792 | |
| 793 | **context**: task, action, sample-fetch, converter |
| 794 | |
| 795 | Change the nice of the current task or current session. |
Thierry FOURNIER | 2e4893c | 2015-03-18 13:37:27 +0100 | [diff] [blame] | 796 | |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 797 | :param integer nice: the nice value, it must be between -1024 and 1024. |
| 798 | |
| 799 | .. js:function:: core.set_map(filename, key, value) |
| 800 | |
| 801 | **context**: init, task, action, sample-fetch, converter |
| 802 | |
Bertrand Jacquin | 874a35c | 2018-09-10 21:26:07 +0100 | [diff] [blame] | 803 | 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] | 804 | *filename*. |
| 805 | |
Thierry FOURNIER | dc59500 | 2015-12-21 11:13:52 +0100 | [diff] [blame] | 806 | :param string filename: the Map reference |
| 807 | :param string key: the key to set or replace |
| 808 | :param string value: the associated value |
| 809 | |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 810 | .. js:function:: core.sleep(int seconds) |
| 811 | |
| 812 | **context**: body, init, task, action |
| 813 | |
| 814 | The `core.sleep()` functions stop the Lua execution between specified seconds. |
| 815 | |
| 816 | :param integer seconds: the required seconds. |
| 817 | |
| 818 | .. js:function:: core.tcp() |
| 819 | |
| 820 | **context**: init, task, action |
| 821 | |
| 822 | This function returns a new object of a *socket* class. |
| 823 | |
Thierry FOURNIER | dc59500 | 2015-12-21 11:13:52 +0100 | [diff] [blame] | 824 | :returns: A :ref:`socket_class` object. |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 825 | |
William Lallemand | 00a1502 | 2021-11-19 16:02:44 +0100 | [diff] [blame] | 826 | .. js:function:: core.httpclient() |
| 827 | |
| 828 | **context**: init, task, action |
| 829 | |
| 830 | This function returns a new object of a *httpclient* class. |
| 831 | |
| 832 | :returns: A :ref:`httpclient_class` object. |
| 833 | |
Thierry Fournier | 1de1659 | 2016-01-27 09:49:07 +0100 | [diff] [blame] | 834 | .. js:function:: core.concat() |
| 835 | |
| 836 | **context**: body, init, task, action, sample-fetch, converter |
| 837 | |
Bertrand Jacquin | 874a35c | 2018-09-10 21:26:07 +0100 | [diff] [blame] | 838 | This function returns a new concat object. |
Thierry Fournier | 1de1659 | 2016-01-27 09:49:07 +0100 | [diff] [blame] | 839 | |
| 840 | :returns: A :ref:`concat_class` object. |
| 841 | |
Thierry FOURNIER | 0a99b89 | 2015-08-26 00:14:17 +0200 | [diff] [blame] | 842 | .. js:function:: core.done(data) |
| 843 | |
| 844 | **context**: body, init, task, action, sample-fetch, converter |
| 845 | |
| 846 | :param any data: Return some data for the caller. It is useful with |
| 847 | sample-fetches and sample-converters. |
| 848 | |
| 849 | Immediately stops the current Lua execution and returns to the caller which |
| 850 | 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] | 851 | value (ignored for actions and init). It is used when the LUA process finishes |
| 852 | 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] | 853 | remaining code. It can be seen as a multi-level "return". |
| 854 | |
Thierry FOURNIER | 486f5a0 | 2015-03-16 15:13:03 +0100 | [diff] [blame] | 855 | .. js:function:: core.yield() |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 856 | |
| 857 | **context**: task, action, sample-fetch, converter |
| 858 | |
| 859 | Give back the hand at the HAProxy scheduler. It is used when the LUA |
| 860 | processing consumes a lot of processing time. |
| 861 | |
Thierry FOURNIER / OZON.IO | 62fec75 | 2016-11-10 20:38:11 +0100 | [diff] [blame] | 862 | .. js:function:: core.parse_addr(address) |
| 863 | |
| 864 | **context**: body, init, task, action, sample-fetch, converter |
| 865 | |
| 866 | :param network: is a string describing an ipv4 or ipv6 address and optionally |
| 867 | its network length, like this: "127.0.0.1/8" or "aaaa::1234/32". |
| 868 | :returns: a userdata containing network or nil if an error occurs. |
| 869 | |
Bertrand Jacquin | 874a35c | 2018-09-10 21:26:07 +0100 | [diff] [blame] | 870 | Parse ipv4 or ipv6 addresses and its facultative associated network. |
Thierry FOURNIER / OZON.IO | 62fec75 | 2016-11-10 20:38:11 +0100 | [diff] [blame] | 871 | |
| 872 | .. js:function:: core.match_addr(addr1, addr2) |
| 873 | |
| 874 | **context**: body, init, task, action, sample-fetch, converter |
| 875 | |
| 876 | :param addr1: is an address created with "core.parse_addr". |
| 877 | :param addr2: is an address created with "core.parse_addr". |
Bertrand Jacquin | 874a35c | 2018-09-10 21:26:07 +0100 | [diff] [blame] | 878 | :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] | 879 | false. |
| 880 | |
Ilya Shipitsin | 2075ca8 | 2020-03-06 23:22:22 +0500 | [diff] [blame] | 881 | 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] | 882 | of network is not important. |
| 883 | |
Thierry FOURNIER / OZON.IO | 8a1027a | 2016-11-24 20:48:38 +0100 | [diff] [blame] | 884 | .. js:function:: core.tokenize(str, separators [, noblank]) |
| 885 | |
| 886 | **context**: body, init, task, action, sample-fetch, converter |
| 887 | |
| 888 | This function is useful for tokenizing an entry, or splitting some messages. |
| 889 | :param string str: The string which will be split. |
| 890 | :param string separators: A string containing a list of separators. |
| 891 | :param boolean noblank: Ignore empty entries. |
| 892 | :returns: an array of string. |
| 893 | |
| 894 | For example: |
| 895 | |
| 896 | .. code-block:: lua |
| 897 | |
| 898 | local array = core.tokenize("This function is useful, for tokenizing an entry.", "., ", true) |
| 899 | print_r(array) |
| 900 | .. |
| 901 | |
| 902 | Returns this array: |
| 903 | |
| 904 | .. code-block:: text |
| 905 | |
| 906 | (table) table: 0x21c01e0 [ |
| 907 | 1: (string) "This" |
| 908 | 2: (string) "function" |
| 909 | 3: (string) "is" |
| 910 | 4: (string) "useful" |
| 911 | 5: (string) "for" |
| 912 | 6: (string) "tokenizing" |
| 913 | 7: (string) "an" |
| 914 | 8: (string) "entry" |
| 915 | ] |
| 916 | .. |
| 917 | |
Aurelien DARRAGON | c84899c | 2023-02-20 18:18:59 +0100 | [diff] [blame] | 918 | .. js:function:: core.event_sub(event_types, func) |
| 919 | |
| 920 | **context**: body, init, task, action, sample-fetch, converter |
| 921 | |
| 922 | Register a function that will be called on specific system events. |
| 923 | |
| 924 | :param array event_types: array of string containing the event types you want to subscribe to |
| 925 | :param function func: is the Lua function called when one of the subscribed events occur. |
| 926 | :returns: A :ref:`event_sub_class` object. |
Aurelien DARRAGON | 223770d | 2023-03-10 15:34:35 +0100 | [diff] [blame^] | 927 | :see: :js:func:`Server.event_sub()`. |
Aurelien DARRAGON | c84899c | 2023-02-20 18:18:59 +0100 | [diff] [blame] | 928 | |
| 929 | List of available event types : |
| 930 | |
| 931 | **SERVER** Family: |
| 932 | |
| 933 | * **SERVER_ADD**: when a server is added |
| 934 | * **SERVER_DEL**: when a server is removed |
| 935 | * **SERVER_DOWN**: when a server state goes from UP to DOWN |
| 936 | * **SERVER_UP**: when a server state goes from DOWN to UP |
| 937 | |
| 938 | .. Note:: |
| 939 | You may also use **SERVER** in **event_types** to subscribe to all server events types at once. |
| 940 | |
| 941 | The prototype of the Lua function used as argument is: |
| 942 | |
| 943 | .. code-block:: lua |
| 944 | |
| 945 | function(event, event_data, sub) |
| 946 | .. |
| 947 | |
| 948 | * **event** (*string*): the event type (one of the **event_types** you specified when subscribing) |
| 949 | * **event_data**: specific to each event family (For **SERVER** family, a :ref:`server_event_class` object) |
| 950 | * **sub**: class to manage the subscription from within the event (a :ref:`event_sub_class` object) |
| 951 | |
| 952 | .. Warning:: |
| 953 | The callback function will only be scheduled on the very same thread that |
| 954 | performed the subscription. |
| 955 | |
| 956 | Moreover, each thread treats events sequentially. It means that if you have, |
| 957 | let's say SERVER_UP followed by a SERVER_DOWN in a short timelapse, then |
| 958 | the cb function will first be called with SERVER_UP, and once it's done |
| 959 | handling the event, the cb function will be called again with SERVER_DOWN. |
| 960 | |
| 961 | This is to ensure event consistency when it comes to logging / triggering logic |
| 962 | from lua. |
| 963 | |
| 964 | Your lua cb function may yield if needed, but you're pleased to process the |
| 965 | event as fast as possible to prevent the event queue from growing up, depending |
| 966 | on the event flow that is expected for the given subscription. |
| 967 | |
| 968 | To prevent abuses, if the event queue for the current subscription goes over |
| 969 | a certain amount of unconsumed events, the subscription will pause itself |
| 970 | automatically for as long as it takes for your handler to catch up. This would |
| 971 | lead to events being missed, so an error will be reported in the logs to warn |
| 972 | you about that. |
| 973 | This is not something you want to let happen too often, it may indicate that |
| 974 | you subscribed to an event that is occurring too frequently or/and that your |
| 975 | callback function is too slow to keep up the pace and you should review it. |
| 976 | |
| 977 | If you want to do some parallel processing because your callback functions are |
| 978 | slow: you might want to create subtasks from lua using |
| 979 | :js:func:`core.register_task()` from within your callback function to perform |
| 980 | the heavy job in a dedicated task and allow remaining events to be processed |
| 981 | more quickly. |
| 982 | |
Thierry Fournier | f61aa63 | 2016-02-19 20:56:00 +0100 | [diff] [blame] | 983 | .. _proxy_class: |
| 984 | |
| 985 | Proxy class |
| 986 | ============ |
| 987 | |
| 988 | .. js:class:: Proxy |
| 989 | |
| 990 | This class provides a way for manipulating proxy and retrieving information |
| 991 | like statistics. |
| 992 | |
Thierry FOURNIER | 817e759 | 2017-07-24 14:35:04 +0200 | [diff] [blame] | 993 | .. js:attribute:: Proxy.name |
| 994 | |
| 995 | Contain the name of the proxy. |
| 996 | |
Aurelien DARRAGON | c4b2437 | 2023-03-02 12:00:06 +0100 | [diff] [blame] | 997 | .. warning:: |
| 998 | This attribute is now deprecated and will eventually be removed. |
| 999 | Please use :js:func:`Proxy.get_name()` function instead. |
| 1000 | |
Thierry Fournier | b046773 | 2022-10-07 12:07:24 +0200 | [diff] [blame] | 1001 | .. js:function:: Proxy.get_name() |
| 1002 | |
| 1003 | Returns the name of the proxy. |
| 1004 | |
Baptiste Assmann | 46c7255 | 2017-10-26 21:51:58 +0200 | [diff] [blame] | 1005 | .. js:attribute:: Proxy.uuid |
| 1006 | |
| 1007 | Contain the unique identifier of the proxy. |
| 1008 | |
Aurelien DARRAGON | c4b2437 | 2023-03-02 12:00:06 +0100 | [diff] [blame] | 1009 | .. warning:: |
| 1010 | This attribute is now deprecated and will eventually be removed. |
| 1011 | Please use :js:func:`Proxy.get_uuid()` function instead. |
| 1012 | |
Thierry Fournier | b046773 | 2022-10-07 12:07:24 +0200 | [diff] [blame] | 1013 | .. js:function:: Proxy.get_uuid() |
| 1014 | |
| 1015 | Returns the unique identifier of the proxy. |
| 1016 | |
Thierry Fournier | f2fdc9d | 2016-02-22 08:21:39 +0100 | [diff] [blame] | 1017 | .. js:attribute:: Proxy.servers |
| 1018 | |
Patrick Hemmer | c6a1d71 | 2018-05-01 21:30:41 -0400 | [diff] [blame] | 1019 | Contain a table with the attached servers. The table is indexed by server |
| 1020 | 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] | 1021 | |
Adis Nezirovic | 8878f8e | 2018-07-13 12:18:33 +0200 | [diff] [blame] | 1022 | .. js:attribute:: Proxy.stktable |
| 1023 | |
| 1024 | Contains a stick table object attached to the proxy. |
| 1025 | |
Thierry Fournier | ff48042 | 2016-02-25 08:36:46 +0100 | [diff] [blame] | 1026 | .. js:attribute:: Proxy.listeners |
| 1027 | |
Patrick Hemmer | c6a1d71 | 2018-05-01 21:30:41 -0400 | [diff] [blame] | 1028 | Contain a table with the attached listeners. The table is indexed by listener |
| 1029 | name, and each each listeners entry is an object of type |
| 1030 | :ref:`listener_class`. |
Thierry Fournier | ff48042 | 2016-02-25 08:36:46 +0100 | [diff] [blame] | 1031 | |
Thierry Fournier | f61aa63 | 2016-02-19 20:56:00 +0100 | [diff] [blame] | 1032 | .. js:function:: Proxy.pause(px) |
| 1033 | |
| 1034 | Pause the proxy. See the management socket documentation for more information. |
| 1035 | |
| 1036 | :param class_proxy px: A :ref:`proxy_class` which indicates the manipulated |
| 1037 | proxy. |
| 1038 | |
| 1039 | .. js:function:: Proxy.resume(px) |
| 1040 | |
| 1041 | Resume the proxy. See the management socket documentation for more |
| 1042 | information. |
| 1043 | |
| 1044 | :param class_proxy px: A :ref:`proxy_class` which indicates the manipulated |
| 1045 | proxy. |
| 1046 | |
| 1047 | .. js:function:: Proxy.stop(px) |
| 1048 | |
| 1049 | Stop the proxy. See the management socket documentation for more information. |
| 1050 | |
| 1051 | :param class_proxy px: A :ref:`proxy_class` which indicates the manipulated |
| 1052 | proxy. |
| 1053 | |
| 1054 | .. js:function:: Proxy.shut_bcksess(px) |
| 1055 | |
| 1056 | Kill the session attached to a backup server. See the management socket |
| 1057 | documentation for more information. |
| 1058 | |
| 1059 | :param class_proxy px: A :ref:`proxy_class` which indicates the manipulated |
| 1060 | proxy. |
| 1061 | |
| 1062 | .. js:function:: Proxy.get_cap(px) |
| 1063 | |
| 1064 | Returns a string describing the capabilities of the proxy. |
| 1065 | |
| 1066 | :param class_proxy px: A :ref:`proxy_class` which indicates the manipulated |
| 1067 | proxy. |
| 1068 | :returns: a string "frontend", "backend", "proxy" or "ruleset". |
| 1069 | |
| 1070 | .. js:function:: Proxy.get_mode(px) |
| 1071 | |
| 1072 | Returns a string describing the mode of the current proxy. |
| 1073 | |
| 1074 | :param class_proxy px: A :ref:`proxy_class` which indicates the manipulated |
| 1075 | proxy. |
| 1076 | :returns: a string "tcp", "http", "health" or "unknown" |
| 1077 | |
| 1078 | .. js:function:: Proxy.get_stats(px) |
| 1079 | |
Bertrand Jacquin | 874a35c | 2018-09-10 21:26:07 +0100 | [diff] [blame] | 1080 | Returns a table containing the proxy statistics. The statistics returned are |
Thierry Fournier | f61aa63 | 2016-02-19 20:56:00 +0100 | [diff] [blame] | 1081 | not the same if the proxy is frontend or a backend. |
| 1082 | |
| 1083 | :param class_proxy px: A :ref:`proxy_class` which indicates the manipulated |
| 1084 | proxy. |
Patrick Hemmer | c6a1d71 | 2018-05-01 21:30:41 -0400 | [diff] [blame] | 1085 | :returns: a key/value table containing stats |
Thierry Fournier | f61aa63 | 2016-02-19 20:56:00 +0100 | [diff] [blame] | 1086 | |
Thierry Fournier | f2fdc9d | 2016-02-22 08:21:39 +0100 | [diff] [blame] | 1087 | .. _server_class: |
| 1088 | |
| 1089 | Server class |
| 1090 | ============ |
| 1091 | |
Patrick Hemmer | c6a1d71 | 2018-05-01 21:30:41 -0400 | [diff] [blame] | 1092 | .. js:class:: Server |
| 1093 | |
| 1094 | This class provides a way for manipulating servers and retrieving information. |
| 1095 | |
Patrick Hemmer | a62ae7e | 2018-04-29 14:23:48 -0400 | [diff] [blame] | 1096 | .. js:attribute:: Server.name |
| 1097 | |
| 1098 | Contain the name of the server. |
| 1099 | |
Aurelien DARRAGON | c4b2437 | 2023-03-02 12:00:06 +0100 | [diff] [blame] | 1100 | .. warning:: |
| 1101 | This attribute is now deprecated and will eventually be removed. |
| 1102 | Please use :js:func:`Server.get_name()` function instead. |
| 1103 | |
Thierry Fournier | b046773 | 2022-10-07 12:07:24 +0200 | [diff] [blame] | 1104 | .. js:function:: Server.get_name(sv) |
| 1105 | |
| 1106 | Returns the name of the server. |
| 1107 | |
Patrick Hemmer | a62ae7e | 2018-04-29 14:23:48 -0400 | [diff] [blame] | 1108 | .. js:attribute:: Server.puid |
| 1109 | |
| 1110 | Contain the proxy unique identifier of the server. |
| 1111 | |
Aurelien DARRAGON | c4b2437 | 2023-03-02 12:00:06 +0100 | [diff] [blame] | 1112 | .. warning:: |
| 1113 | This attribute is now deprecated and will eventually be removed. |
| 1114 | Please use :js:func:`Server.get_puid()` function instead. |
| 1115 | |
Thierry Fournier | b046773 | 2022-10-07 12:07:24 +0200 | [diff] [blame] | 1116 | .. js:function:: Server.get_puid(sv) |
| 1117 | |
| 1118 | Returns the proxy unique identifier of the server. |
| 1119 | |
Aurelien DARRAGON | 94ee663 | 2023-03-10 15:11:27 +0100 | [diff] [blame] | 1120 | .. js:function:: Server.get_rid(sv) |
| 1121 | |
| 1122 | Returns the rid (revision ID) of the server. |
| 1123 | It is an unsigned integer that is set upon server creation. Value is derived |
| 1124 | from a global counter that starts at 0 and is incremented each time one or |
| 1125 | multiple server deletions are followed by a server addition (meaning that |
| 1126 | old name/id reuse could occur). |
| 1127 | |
| 1128 | Combining server name/id with server rid yields a process-wide unique |
| 1129 | identifier. |
| 1130 | |
Thierry Fournier | f2fdc9d | 2016-02-22 08:21:39 +0100 | [diff] [blame] | 1131 | .. js:function:: Server.is_draining(sv) |
| 1132 | |
Patrick Hemmer | c6a1d71 | 2018-05-01 21:30:41 -0400 | [diff] [blame] | 1133 | Return true if the server is currently draining sticky connections. |
Thierry Fournier | f2fdc9d | 2016-02-22 08:21:39 +0100 | [diff] [blame] | 1134 | |
| 1135 | :param class_server sv: A :ref:`server_class` which indicates the manipulated |
| 1136 | server. |
| 1137 | :returns: a boolean |
| 1138 | |
Patrick Hemmer | 32d539f | 2018-04-29 14:25:46 -0400 | [diff] [blame] | 1139 | .. js:function:: Server.set_maxconn(sv, weight) |
| 1140 | |
| 1141 | Dynamically change the maximum connections of the server. See the management |
| 1142 | socket documentation for more information about the format of the string. |
| 1143 | |
| 1144 | :param class_server sv: A :ref:`server_class` which indicates the manipulated |
| 1145 | server. |
| 1146 | :param string maxconn: A string describing the server maximum connections. |
| 1147 | |
| 1148 | .. js:function:: Server.get_maxconn(sv, weight) |
| 1149 | |
| 1150 | This function returns an integer representing the server maximum connections. |
| 1151 | |
| 1152 | :param class_server sv: A :ref:`server_class` which indicates the manipulated |
| 1153 | server. |
| 1154 | :returns: an integer. |
| 1155 | |
Thierry Fournier | f2fdc9d | 2016-02-22 08:21:39 +0100 | [diff] [blame] | 1156 | .. js:function:: Server.set_weight(sv, weight) |
| 1157 | |
Patrick Hemmer | c6a1d71 | 2018-05-01 21:30:41 -0400 | [diff] [blame] | 1158 | Dynamically change the weight of the server. See the management socket |
Thierry Fournier | f2fdc9d | 2016-02-22 08:21:39 +0100 | [diff] [blame] | 1159 | documentation for more information about the format of the string. |
| 1160 | |
| 1161 | :param class_server sv: A :ref:`server_class` which indicates the manipulated |
| 1162 | server. |
| 1163 | :param string weight: A string describing the server weight. |
| 1164 | |
| 1165 | .. js:function:: Server.get_weight(sv) |
| 1166 | |
Patrick Hemmer | c6a1d71 | 2018-05-01 21:30:41 -0400 | [diff] [blame] | 1167 | This function returns an integer representing the server weight. |
Thierry Fournier | f2fdc9d | 2016-02-22 08:21:39 +0100 | [diff] [blame] | 1168 | |
| 1169 | :param class_server sv: A :ref:`server_class` which indicates the manipulated |
| 1170 | server. |
| 1171 | :returns: an integer. |
| 1172 | |
Joseph C. Sible | 49bbf52 | 2020-05-04 22:20:32 -0400 | [diff] [blame] | 1173 | .. js:function:: Server.set_addr(sv, addr[, port]) |
Thierry Fournier | f2fdc9d | 2016-02-22 08:21:39 +0100 | [diff] [blame] | 1174 | |
Patrick Hemmer | c6a1d71 | 2018-05-01 21:30:41 -0400 | [diff] [blame] | 1175 | Dynamically change the address of the server. See the management socket |
Thierry Fournier | f2fdc9d | 2016-02-22 08:21:39 +0100 | [diff] [blame] | 1176 | documentation for more information about the format of the string. |
| 1177 | |
| 1178 | :param class_server sv: A :ref:`server_class` which indicates the manipulated |
| 1179 | server. |
Patrick Hemmer | c6a1d71 | 2018-05-01 21:30:41 -0400 | [diff] [blame] | 1180 | :param string addr: A string describing the server address. |
Thierry Fournier | f2fdc9d | 2016-02-22 08:21:39 +0100 | [diff] [blame] | 1181 | |
| 1182 | .. js:function:: Server.get_addr(sv) |
| 1183 | |
Patrick Hemmer | c6a1d71 | 2018-05-01 21:30:41 -0400 | [diff] [blame] | 1184 | Returns a string describing the address of the server. |
Thierry Fournier | f2fdc9d | 2016-02-22 08:21:39 +0100 | [diff] [blame] | 1185 | |
| 1186 | :param class_server sv: A :ref:`server_class` which indicates the manipulated |
| 1187 | server. |
| 1188 | :returns: A string |
| 1189 | |
| 1190 | .. js:function:: Server.get_stats(sv) |
| 1191 | |
| 1192 | Returns server statistics. |
| 1193 | |
| 1194 | :param class_server sv: A :ref:`server_class` which indicates the manipulated |
| 1195 | server. |
Patrick Hemmer | c6a1d71 | 2018-05-01 21:30:41 -0400 | [diff] [blame] | 1196 | :returns: a key/value table containing stats |
Thierry Fournier | f2fdc9d | 2016-02-22 08:21:39 +0100 | [diff] [blame] | 1197 | |
| 1198 | .. js:function:: Server.shut_sess(sv) |
| 1199 | |
| 1200 | Shutdown all the sessions attached to the server. See the management socket |
| 1201 | documentation for more information about this function. |
| 1202 | |
| 1203 | :param class_server sv: A :ref:`server_class` which indicates the manipulated |
| 1204 | server. |
| 1205 | |
| 1206 | .. js:function:: Server.set_drain(sv) |
| 1207 | |
| 1208 | Drain sticky sessions. See the management socket documentation for more |
| 1209 | information about this function. |
| 1210 | |
| 1211 | :param class_server sv: A :ref:`server_class` which indicates the manipulated |
| 1212 | server. |
| 1213 | |
| 1214 | .. js:function:: Server.set_maint(sv) |
| 1215 | |
| 1216 | Set maintenance mode. See the management socket documentation for more |
| 1217 | information about this function. |
| 1218 | |
| 1219 | :param class_server sv: A :ref:`server_class` which indicates the manipulated |
| 1220 | server. |
| 1221 | |
| 1222 | .. js:function:: Server.set_ready(sv) |
| 1223 | |
| 1224 | Set normal mode. See the management socket documentation for more information |
| 1225 | about this function. |
| 1226 | |
| 1227 | :param class_server sv: A :ref:`server_class` which indicates the manipulated |
| 1228 | server. |
| 1229 | |
| 1230 | .. js:function:: Server.check_enable(sv) |
| 1231 | |
| 1232 | Enable health checks. See the management socket documentation for more |
| 1233 | information about this function. |
| 1234 | |
| 1235 | :param class_server sv: A :ref:`server_class` which indicates the manipulated |
| 1236 | server. |
| 1237 | |
| 1238 | .. js:function:: Server.check_disable(sv) |
| 1239 | |
| 1240 | Disable health checks. See the management socket documentation for more |
| 1241 | information about this function. |
| 1242 | |
| 1243 | :param class_server sv: A :ref:`server_class` which indicates the manipulated |
| 1244 | server. |
| 1245 | |
| 1246 | .. js:function:: Server.check_force_up(sv) |
| 1247 | |
| 1248 | Force health-check up. See the management socket documentation for more |
| 1249 | information about this function. |
| 1250 | |
| 1251 | :param class_server sv: A :ref:`server_class` which indicates the manipulated |
| 1252 | server. |
| 1253 | |
| 1254 | .. js:function:: Server.check_force_nolb(sv) |
| 1255 | |
| 1256 | Force health-check nolb mode. See the management socket documentation for more |
| 1257 | information about this function. |
| 1258 | |
| 1259 | :param class_server sv: A :ref:`server_class` which indicates the manipulated |
| 1260 | server. |
| 1261 | |
| 1262 | .. js:function:: Server.check_force_down(sv) |
| 1263 | |
| 1264 | Force health-check down. See the management socket documentation for more |
| 1265 | information about this function. |
| 1266 | |
| 1267 | :param class_server sv: A :ref:`server_class` which indicates the manipulated |
| 1268 | server. |
| 1269 | |
| 1270 | .. js:function:: Server.agent_enable(sv) |
| 1271 | |
| 1272 | Enable agent check. See the management socket documentation for more |
| 1273 | information about this function. |
| 1274 | |
| 1275 | :param class_server sv: A :ref:`server_class` which indicates the manipulated |
| 1276 | server. |
| 1277 | |
| 1278 | .. js:function:: Server.agent_disable(sv) |
| 1279 | |
| 1280 | Disable agent check. See the management socket documentation for more |
| 1281 | information about this function. |
| 1282 | |
| 1283 | :param class_server sv: A :ref:`server_class` which indicates the manipulated |
| 1284 | server. |
| 1285 | |
| 1286 | .. js:function:: Server.agent_force_up(sv) |
| 1287 | |
| 1288 | Force agent check up. See the management socket documentation for more |
| 1289 | information about this function. |
| 1290 | |
| 1291 | :param class_server sv: A :ref:`server_class` which indicates the manipulated |
| 1292 | server. |
| 1293 | |
| 1294 | .. js:function:: Server.agent_force_down(sv) |
| 1295 | |
| 1296 | Force agent check down. See the management socket documentation for more |
| 1297 | information about this function. |
| 1298 | |
| 1299 | :param class_server sv: A :ref:`server_class` which indicates the manipulated |
| 1300 | server. |
| 1301 | |
Aurelien DARRAGON | 223770d | 2023-03-10 15:34:35 +0100 | [diff] [blame^] | 1302 | .. js:function:: Server.event_sub(sv, event_types, func) |
| 1303 | |
| 1304 | Register a function that will be called on specific server events. |
| 1305 | It works exactly like :js:func:`core.event_sub()` except that the subscription |
| 1306 | will be performed within the server dedicated subscription list instead of the |
| 1307 | global one. |
| 1308 | (Your callback function will only be called for server events affecting sv) |
| 1309 | |
| 1310 | See :js:func:`core.event_sub()` for function usage. |
| 1311 | |
| 1312 | A key advantage to using :js:func:`Server.event_sub()` over |
| 1313 | :js:func:`core.event_sub()` for servers is that :js:func:`Server.event_sub()` |
| 1314 | allows you to be notified for servers events of a single server only. |
| 1315 | It removes the needs for extra filtering in your callback function if you only |
| 1316 | care about a single server, and also prevents useless wakeups. |
| 1317 | |
| 1318 | For instance, if you want to be notified for UP/DOWN events on a given set of |
| 1319 | servers, it is recommended to peform multiple per-server subscriptions since |
| 1320 | it will be more efficient that doing a single global subscription that will |
| 1321 | filter the received events. |
| 1322 | Unless you really want to be notified for servers events of ALL servers of |
| 1323 | course, which could make sense given you setup but should be avoided if you |
| 1324 | have an important number of servers as it will add a significant load on your |
| 1325 | haproxy process in case of multiple servers state change in a short amount of |
| 1326 | time. |
| 1327 | |
| 1328 | .. Note:: |
| 1329 | You may also combine :js:func:`core.event_sub()` with |
| 1330 | :js:func:`Server.event_sub()`. |
| 1331 | |
| 1332 | Also, don't forget that you can use :js:func:`core.register_task()` from |
| 1333 | your callback function if needed. (ie: parallel work) |
| 1334 | |
| 1335 | Here is a working example combining :js:func:`core.event_sub()` with |
| 1336 | :js:func:`Server.event_sub()` and :js:func:`core.register_task()` |
| 1337 | (This only serves as a demo, this is not necessarily useful to do so) |
| 1338 | |
| 1339 | .. code-block:: lua |
| 1340 | |
| 1341 | core.event_sub({"SERVER_ADD"}, function(event, data, sub) |
| 1342 | -- in the global event handler |
| 1343 | if data["reference"] ~= nil then |
| 1344 | print("Tracking new server: ", data["name"]) |
| 1345 | data["reference"]:event_sub({"SERVER_UP", "SERVER_DOWN"}, function(event, data, sub) |
| 1346 | -- in the per-server event handler |
| 1347 | if data["reference"] ~= nil then |
| 1348 | core.register_task(function(server) |
| 1349 | -- subtask to perform some async work (e.g.: HTTP API calls, sending emails...) |
| 1350 | print("ASYNC: SERVER ", server:get_name(), " is ", event == "SERVER_UP" and "UP" or "DOWN") |
| 1351 | end, data["reference"]) |
| 1352 | end |
| 1353 | end) |
| 1354 | end |
| 1355 | end) |
| 1356 | |
| 1357 | .. |
| 1358 | |
| 1359 | In this example, we will first track global server addition events. |
| 1360 | For each newly added server ("add server" on the cli), we will register a |
| 1361 | UP/DOWN server subscription. |
| 1362 | Then, the callback function will schedule the event handling in an async |
| 1363 | subtask which will receive the server reference as an argument. |
| 1364 | |
Thierry Fournier | ff48042 | 2016-02-25 08:36:46 +0100 | [diff] [blame] | 1365 | .. _listener_class: |
| 1366 | |
| 1367 | Listener class |
| 1368 | ============== |
| 1369 | |
| 1370 | .. js:function:: Listener.get_stats(ls) |
| 1371 | |
| 1372 | Returns server statistics. |
| 1373 | |
| 1374 | :param class_listener ls: A :ref:`listener_class` which indicates the |
| 1375 | manipulated listener. |
Patrick Hemmer | c6a1d71 | 2018-05-01 21:30:41 -0400 | [diff] [blame] | 1376 | :returns: a key/value table containing stats |
Thierry Fournier | ff48042 | 2016-02-25 08:36:46 +0100 | [diff] [blame] | 1377 | |
Aurelien DARRAGON | c84899c | 2023-02-20 18:18:59 +0100 | [diff] [blame] | 1378 | .. _event_sub_class: |
| 1379 | |
| 1380 | EventSub class |
| 1381 | ============== |
| 1382 | |
| 1383 | .. js:function:: EventSub.unsub() |
| 1384 | |
| 1385 | End the subscription, the callback function will not be called again. |
| 1386 | |
| 1387 | .. _server_event_class: |
| 1388 | |
| 1389 | ServerEvent class |
| 1390 | ================= |
| 1391 | |
| 1392 | .. js:attribute:: ServerEvent.name |
| 1393 | |
| 1394 | Contains the name of the server. |
| 1395 | |
| 1396 | .. js:attribute:: ServerEvent.puid |
| 1397 | |
| 1398 | Contains the proxy-unique uid of the server |
| 1399 | |
| 1400 | .. js:attribute:: ServerEvent.rid |
| 1401 | |
| 1402 | Contains the revision ID of the server |
| 1403 | |
| 1404 | .. js:attribute:: ServerEvent.proxy_name |
| 1405 | |
| 1406 | Contains the name of the proxy to which the server belongs |
| 1407 | |
| 1408 | .. js:attribute:: ServerEvent.reference |
| 1409 | |
| 1410 | Reference to the live server (A :ref:`server_class`). |
| 1411 | |
| 1412 | .. Warning:: |
| 1413 | Not available if the server was removed in the meantime. |
| 1414 | (Will never be set for SERVER_DEL event since the server does not exist anymore) |
| 1415 | |
Thierry Fournier | 1de1659 | 2016-01-27 09:49:07 +0100 | [diff] [blame] | 1416 | .. _concat_class: |
| 1417 | |
| 1418 | Concat class |
| 1419 | ============ |
| 1420 | |
| 1421 | .. js:class:: Concat |
| 1422 | |
| 1423 | This class provides a fast way for string concatenation. The way using native |
| 1424 | Lua concatenation like the code below is slow for some reasons. |
| 1425 | |
| 1426 | .. code-block:: lua |
| 1427 | |
| 1428 | str = "string1" |
| 1429 | str = str .. ", string2" |
| 1430 | str = str .. ", string3" |
| 1431 | .. |
| 1432 | |
| 1433 | For each concatenation, Lua: |
Aurelien DARRAGON | 53901f4 | 2022-10-13 19:49:42 +0200 | [diff] [blame] | 1434 | - allocates memory for the result, |
| 1435 | - catenates the two string copying the strings in the new memory block, |
| 1436 | - frees the old memory block containing the string which is no longer used. |
| 1437 | |
Thierry Fournier | 1de1659 | 2016-01-27 09:49:07 +0100 | [diff] [blame] | 1438 | This process does many memory move, allocation and free. In addition, the |
Aurelien DARRAGON | 53901f4 | 2022-10-13 19:49:42 +0200 | [diff] [blame] | 1439 | memory is not really freed, it is just marked as unused and waits for the |
Thierry Fournier | 1de1659 | 2016-01-27 09:49:07 +0100 | [diff] [blame] | 1440 | garbage collector. |
| 1441 | |
Aurelien DARRAGON | 53901f4 | 2022-10-13 19:49:42 +0200 | [diff] [blame] | 1442 | The Concat class provides an alternative way to concatenate strings. It uses |
Thierry Fournier | 1de1659 | 2016-01-27 09:49:07 +0100 | [diff] [blame] | 1443 | the internal Lua mechanism (it does not allocate memory), but it doesn't copy |
| 1444 | the data more than once. |
| 1445 | |
| 1446 | On my computer, the following loops spends 0.2s for the Concat method and |
| 1447 | 18.5s for the pure Lua implementation. So, the Concat class is about 1000x |
| 1448 | faster than the embedded solution. |
| 1449 | |
| 1450 | .. code-block:: lua |
| 1451 | |
| 1452 | for j = 1, 100 do |
| 1453 | c = core.concat() |
| 1454 | for i = 1, 20000 do |
| 1455 | c:add("#####") |
| 1456 | end |
| 1457 | end |
| 1458 | .. |
| 1459 | |
| 1460 | .. code-block:: lua |
| 1461 | |
| 1462 | for j = 1, 100 do |
| 1463 | c = "" |
| 1464 | for i = 1, 20000 do |
| 1465 | c = c .. "#####" |
| 1466 | end |
| 1467 | end |
| 1468 | .. |
| 1469 | |
| 1470 | .. js:function:: Concat.add(concat, string) |
| 1471 | |
| 1472 | This function adds a string to the current concatenated string. |
| 1473 | |
| 1474 | :param class_concat concat: A :ref:`concat_class` which contains the currently |
Ilya Shipitsin | 2075ca8 | 2020-03-06 23:22:22 +0500 | [diff] [blame] | 1475 | built string. |
Bertrand Jacquin | 874a35c | 2018-09-10 21:26:07 +0100 | [diff] [blame] | 1476 | :param string string: A new string to concatenate to the current built |
Thierry Fournier | 1de1659 | 2016-01-27 09:49:07 +0100 | [diff] [blame] | 1477 | string. |
| 1478 | |
| 1479 | .. js:function:: Concat.dump(concat) |
| 1480 | |
Bertrand Jacquin | 874a35c | 2018-09-10 21:26:07 +0100 | [diff] [blame] | 1481 | This function returns the concatenated string. |
Thierry Fournier | 1de1659 | 2016-01-27 09:49:07 +0100 | [diff] [blame] | 1482 | |
| 1483 | :param class_concat concat: A :ref:`concat_class` which contains the currently |
Ilya Shipitsin | 2075ca8 | 2020-03-06 23:22:22 +0500 | [diff] [blame] | 1484 | built string. |
Thierry Fournier | 1de1659 | 2016-01-27 09:49:07 +0100 | [diff] [blame] | 1485 | :returns: the concatenated string |
| 1486 | |
Thierry FOURNIER | dc59500 | 2015-12-21 11:13:52 +0100 | [diff] [blame] | 1487 | .. _fetches_class: |
| 1488 | |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 1489 | Fetches class |
| 1490 | ============= |
| 1491 | |
| 1492 | .. js:class:: Fetches |
| 1493 | |
| 1494 | This class contains a lot of internal HAProxy sample fetches. See the |
Aurelien DARRAGON | 53901f4 | 2022-10-13 19:49:42 +0200 | [diff] [blame] | 1495 | HAProxy "configuration.txt" documentation for more information. |
| 1496 | (chapters 7.3.2 to 7.3.6) |
Thierry FOURNIER | 2e4893c | 2015-03-18 13:37:27 +0100 | [diff] [blame] | 1497 | |
Christopher Faulet | 1e9b1b6 | 2021-08-11 10:14:30 +0200 | [diff] [blame] | 1498 | .. warning:: |
| 1499 | some sample fetches are not available in some context. These limitations |
| 1500 | are specified in this documentation when they're useful. |
Thierry FOURNIER | dc59500 | 2015-12-21 11:13:52 +0100 | [diff] [blame] | 1501 | |
Thierry FOURNIER | 12a865d | 2016-12-14 19:40:37 +0100 | [diff] [blame] | 1502 | :see: :js:attr:`TXN.f` |
| 1503 | :see: :js:attr:`TXN.sf` |
Thierry FOURNIER | 2e4893c | 2015-03-18 13:37:27 +0100 | [diff] [blame] | 1504 | |
Aurelien DARRAGON | 53901f4 | 2022-10-13 19:49:42 +0200 | [diff] [blame] | 1505 | Fetches are useful to: |
Thierry FOURNIER | 2e4893c | 2015-03-18 13:37:27 +0100 | [diff] [blame] | 1506 | |
| 1507 | * get system time, |
| 1508 | * get environment variable, |
| 1509 | * get random numbers, |
Aurelien DARRAGON | 53901f4 | 2022-10-13 19:49:42 +0200 | [diff] [blame] | 1510 | * know backend status like the number of users in queue or the number of |
Thierry FOURNIER | 2e4893c | 2015-03-18 13:37:27 +0100 | [diff] [blame] | 1511 | connections established, |
Aurelien DARRAGON | 53901f4 | 2022-10-13 19:49:42 +0200 | [diff] [blame] | 1512 | * get client information like ip source or destination, |
Thierry FOURNIER | 2e4893c | 2015-03-18 13:37:27 +0100 | [diff] [blame] | 1513 | * deal with stick tables, |
Aurelien DARRAGON | 53901f4 | 2022-10-13 19:49:42 +0200 | [diff] [blame] | 1514 | * fetch established SSL information, |
| 1515 | * fetch HTTP information like headers or method. |
Thierry FOURNIER | 2e4893c | 2015-03-18 13:37:27 +0100 | [diff] [blame] | 1516 | |
| 1517 | .. code-block:: lua |
| 1518 | |
Thierry FOURNIER | dc59500 | 2015-12-21 11:13:52 +0100 | [diff] [blame] | 1519 | function action(txn) |
| 1520 | -- Get source IP |
| 1521 | local clientip = txn.f:src() |
| 1522 | end |
Thierry FOURNIER | 2e4893c | 2015-03-18 13:37:27 +0100 | [diff] [blame] | 1523 | .. |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 1524 | |
Thierry FOURNIER | dc59500 | 2015-12-21 11:13:52 +0100 | [diff] [blame] | 1525 | .. _converters_class: |
| 1526 | |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 1527 | Converters class |
| 1528 | ================ |
| 1529 | |
| 1530 | .. js:class:: Converters |
| 1531 | |
| 1532 | This class contains a lot of internal HAProxy sample converters. See the |
Thierry FOURNIER | 2e4893c | 2015-03-18 13:37:27 +0100 | [diff] [blame] | 1533 | HAProxy documentation "configuration.txt" for more information about her |
| 1534 | usage. Its the chapter 7.3.1. |
| 1535 | |
Thierry FOURNIER | 12a865d | 2016-12-14 19:40:37 +0100 | [diff] [blame] | 1536 | :see: :js:attr:`TXN.c` |
| 1537 | :see: :js:attr:`TXN.sc` |
Thierry FOURNIER | 2e4893c | 2015-03-18 13:37:27 +0100 | [diff] [blame] | 1538 | |
Aurelien DARRAGON | 53901f4 | 2022-10-13 19:49:42 +0200 | [diff] [blame] | 1539 | Converters provides stateful transformation. They are useful to: |
Thierry FOURNIER | 2e4893c | 2015-03-18 13:37:27 +0100 | [diff] [blame] | 1540 | |
Aurelien DARRAGON | 53901f4 | 2022-10-13 19:49:42 +0200 | [diff] [blame] | 1541 | * convert input to base64, |
| 1542 | * apply hash on input string (djb2, crc32, sdbm, wt6), |
Thierry FOURNIER | 2e4893c | 2015-03-18 13:37:27 +0100 | [diff] [blame] | 1543 | * format date, |
| 1544 | * json escape, |
Aurelien DARRAGON | 53901f4 | 2022-10-13 19:49:42 +0200 | [diff] [blame] | 1545 | * extract preferred language comparing two lists, |
Thierry FOURNIER | 2e4893c | 2015-03-18 13:37:27 +0100 | [diff] [blame] | 1546 | * turn to lower or upper chars, |
| 1547 | * deal with stick tables. |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 1548 | |
Thierry FOURNIER | dc59500 | 2015-12-21 11:13:52 +0100 | [diff] [blame] | 1549 | .. _channel_class: |
| 1550 | |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 1551 | Channel class |
| 1552 | ============= |
| 1553 | |
| 1554 | .. js:class:: Channel |
| 1555 | |
Christopher Faulet | 5a2c661 | 2021-08-15 20:35:25 +0200 | [diff] [blame] | 1556 | **context**: action, sample-fetch, convert, filter |
| 1557 | |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 1558 | HAProxy uses two buffers for the processing of the requests. The first one is |
| 1559 | used with the request data (from the client to the server) and the second is |
| 1560 | used for the response data (from the server to the client). |
| 1561 | |
| 1562 | Each buffer contains two types of data. The first type is the incoming data |
| 1563 | waiting for a processing. The second part is the outgoing data already |
| 1564 | processed. Usually, the incoming data is processed, after it is tagged as |
| 1565 | outgoing data, and finally it is sent. The following functions provides tools |
| 1566 | for manipulating these data in a buffer. |
| 1567 | |
| 1568 | The following diagram shows where the channel class function are applied. |
| 1569 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 1570 | .. image:: _static/channel.png |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 1571 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 1572 | .. warning:: |
| 1573 | It is not possible to read from the response in request action, and it is |
Boyang Li | 60cfe8b | 2022-05-10 18:11:00 +0000 | [diff] [blame] | 1574 | not possible to read from the request channel in response action. |
Christopher Faulet | 0953039 | 2021-06-14 11:43:18 +0200 | [diff] [blame] | 1575 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 1576 | .. warning:: |
| 1577 | It is forbidden to alter the Channels buffer from HTTP contexts. So only |
| 1578 | :js:func:`Channel.input`, :js:func:`Channel.output`, |
| 1579 | :js:func:`Channel.may_recv`, :js:func:`Channel.is_full` and |
Aurelien DARRAGON | 53901f4 | 2022-10-13 19:49:42 +0200 | [diff] [blame] | 1580 | :js:func:`Channel.is_resp` can be called from a HTTP context. |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 1581 | |
Christopher Faulet | 5a2c661 | 2021-08-15 20:35:25 +0200 | [diff] [blame] | 1582 | All the functions provided by this class are available in the |
| 1583 | **sample-fetches**, **actions** and **filters** contexts. For **filters**, |
| 1584 | incoming data (offset and length) are relative to the filter. Some functions |
Boyang Li | 60cfe8b | 2022-05-10 18:11:00 +0000 | [diff] [blame] | 1585 | may yield, but only for **actions**. Yield is not possible for |
Christopher Faulet | 5a2c661 | 2021-08-15 20:35:25 +0200 | [diff] [blame] | 1586 | **sample-fetches**, **converters** and **filters**. |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 1587 | |
| 1588 | .. js:function:: Channel.append(channel, string) |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 1589 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 1590 | This function copies the string **string** at the end of incoming data of the |
| 1591 | channel buffer. The function returns the copied length on success or -1 if |
| 1592 | data cannot be copied. |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 1593 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 1594 | Same that :js:func:`Channel.insert(channel, string, channel:input())`. |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 1595 | |
Thierry FOURNIER | 486f5a0 | 2015-03-16 15:13:03 +0100 | [diff] [blame] | 1596 | :param class_channel channel: The manipulated Channel. |
Aurelien DARRAGON | 53901f4 | 2022-10-13 19:49:42 +0200 | [diff] [blame] | 1597 | :param string string: The data to copy at the end of incoming data. |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 1598 | :returns: an integer containing the amount of bytes copied or -1. |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 1599 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 1600 | .. js:function:: Channel.data(channel [, offset [, length]]) |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 1601 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 1602 | This function returns **length** bytes of incoming data from the channel |
| 1603 | buffer, starting at the offset **offset**. The data are not removed from the |
| 1604 | buffer. |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 1605 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 1606 | By default, if no length is provided, all incoming data found, starting at the |
| 1607 | given offset, are returned. If **length** is set to -1, the function tries to |
Christopher Faulet | 5a2c661 | 2021-08-15 20:35:25 +0200 | [diff] [blame] | 1608 | retrieve a maximum of data and, if called by an action, it yields if |
| 1609 | necessary. It also waits for more data if the requested length exceeds the |
Aurelien DARRAGON | 53901f4 | 2022-10-13 19:49:42 +0200 | [diff] [blame] | 1610 | available amount of incoming data. Not providing an offset is the same as |
Ilya Shipitsin | ff0f278 | 2021-08-22 22:18:07 +0500 | [diff] [blame] | 1611 | setting it to 0. A positive offset is relative to the beginning of incoming |
Aurelien DARRAGON | 53901f4 | 2022-10-13 19:49:42 +0200 | [diff] [blame] | 1612 | data of the channel buffer while negative offset is relative to the end. |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 1613 | |
| 1614 | If there is no incoming data and the channel can't receive more data, a 'nil' |
| 1615 | value is returned. |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 1616 | |
Thierry FOURNIER | 486f5a0 | 2015-03-16 15:13:03 +0100 | [diff] [blame] | 1617 | :param class_channel channel: The manipulated Channel. |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 1618 | :param integer offset: *optional* The offset in incoming data to start to get |
| 1619 | data. 0 by default. May be negative to be relative to |
| 1620 | the end of incoming data. |
| 1621 | :param integer length: *optional* The expected length of data to retrieve. All |
| 1622 | incoming data by default. May be set to -1 to get a |
| 1623 | maximum of data. |
| 1624 | :returns: a string containing the data found or nil. |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 1625 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 1626 | .. js:function:: Channel.forward(channel, length) |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 1627 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 1628 | This function forwards **length** bytes of data from the channel buffer. If |
Christopher Faulet | 5a2c661 | 2021-08-15 20:35:25 +0200 | [diff] [blame] | 1629 | the requested length exceeds the available amount of incoming data, and if |
| 1630 | called by an action, the function yields, waiting for more data to forward. It |
| 1631 | returns the amount of data forwarded. |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 1632 | |
| 1633 | :param class_channel channel: The manipulated Channel. |
| 1634 | :param integer int: The amount of data to forward. |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 1635 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 1636 | .. js:function:: Channel.input(channel) |
| 1637 | |
Christopher Faulet | 5a2c661 | 2021-08-15 20:35:25 +0200 | [diff] [blame] | 1638 | This function returns the length of incoming data in the channel buffer. When |
| 1639 | called by a filter, this value is relative to the filter. |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 1640 | |
Thierry FOURNIER | 486f5a0 | 2015-03-16 15:13:03 +0100 | [diff] [blame] | 1641 | :param class_channel channel: The manipulated Channel. |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 1642 | :returns: an integer containing the amount of available bytes. |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 1643 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 1644 | .. js:function:: Channel.insert(channel, string [, offset]) |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 1645 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 1646 | This function copies the string **string** at the offset **offset** in |
| 1647 | incoming data of the channel buffer. The function returns the copied length on |
| 1648 | success or -1 if data cannot be copied. |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 1649 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 1650 | By default, if no offset is provided, the string is copied in front of |
Ilya Shipitsin | ff0f278 | 2021-08-22 22:18:07 +0500 | [diff] [blame] | 1651 | incoming data. A positive offset is relative to the beginning of incoming data |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 1652 | of the channel buffer while negative offset is relative to their end. |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 1653 | |
Thierry FOURNIER | 486f5a0 | 2015-03-16 15:13:03 +0100 | [diff] [blame] | 1654 | :param class_channel channel: The manipulated Channel. |
Aurelien DARRAGON | 53901f4 | 2022-10-13 19:49:42 +0200 | [diff] [blame] | 1655 | :param string string: The data to copy into incoming data. |
| 1656 | :param integer offset: *optional* The offset in incoming data where to copy |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 1657 | data. 0 by default. May be negative to be relative to |
| 1658 | the end of incoming data. |
Pieter Baauw | 4d7f766 | 2015-11-08 16:38:08 +0100 | [diff] [blame] | 1659 | :returns: an integer containing the amount of bytes copied or -1. |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 1660 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 1661 | .. js:function:: Channel.is_full(channel) |
| 1662 | |
| 1663 | This function returns true if the channel buffer is full. |
| 1664 | |
Christopher Faulet | 5a2c661 | 2021-08-15 20:35:25 +0200 | [diff] [blame] | 1665 | :param class_channel channel: The manipulated Channel. |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 1666 | :returns: a boolean |
| 1667 | |
| 1668 | .. js:function:: Channel.is_resp(channel) |
| 1669 | |
| 1670 | This function returns true if the channel is the response one. |
| 1671 | |
Christopher Faulet | 5a2c661 | 2021-08-15 20:35:25 +0200 | [diff] [blame] | 1672 | :param class_channel channel: The manipulated Channel. |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 1673 | :returns: a boolean |
| 1674 | |
| 1675 | .. js:function:: Channel.line(channel [, offset [, length]]) |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 1676 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 1677 | This function parses **length** bytes of incoming data of the channel buffer, |
| 1678 | starting at offset **offset**, and returns the first line found, including the |
Christopher Faulet | 5a2c661 | 2021-08-15 20:35:25 +0200 | [diff] [blame] | 1679 | '\\n'. The data are not removed from the buffer. If no line is found, all data |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 1680 | are returned. |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 1681 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 1682 | By default, if no length is provided, all incoming data, starting at the given |
| 1683 | offset, are evaluated. If **length** is set to -1, the function tries to |
Christopher Faulet | 5a2c661 | 2021-08-15 20:35:25 +0200 | [diff] [blame] | 1684 | retrieve a maximum of data and, if called by an action, yields if |
| 1685 | necessary. It also waits for more data if the requested length exceeds the |
Aurelien DARRAGON | 53901f4 | 2022-10-13 19:49:42 +0200 | [diff] [blame] | 1686 | available amount of incoming data. Not providing an offset is the same as |
Ilya Shipitsin | ff0f278 | 2021-08-22 22:18:07 +0500 | [diff] [blame] | 1687 | setting it to 0. A positive offset is relative to the beginning of incoming |
Aurelien DARRAGON | 53901f4 | 2022-10-13 19:49:42 +0200 | [diff] [blame] | 1688 | data of the channel buffer while negative offset is relative to the end. |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 1689 | |
| 1690 | If there is no incoming data and the channel can't receive more data, a 'nil' |
| 1691 | value is returned. |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 1692 | |
Thierry FOURNIER | 486f5a0 | 2015-03-16 15:13:03 +0100 | [diff] [blame] | 1693 | :param class_channel channel: The manipulated Channel. |
Aurelien DARRAGON | 53901f4 | 2022-10-13 19:49:42 +0200 | [diff] [blame] | 1694 | :param integer offset: *optional* The offset in incoming data to start to |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 1695 | parse data. 0 by default. May be negative to be |
| 1696 | relative to the end of incoming data. |
| 1697 | :param integer length: *optional* The length of data to parse. All incoming |
| 1698 | data by default. May be set to -1 to get a maximum of |
| 1699 | data. |
| 1700 | :returns: a string containing the line found or nil. |
| 1701 | |
| 1702 | .. js:function:: Channel.may_recv(channel) |
| 1703 | |
| 1704 | This function returns true if the channel may still receive data. |
| 1705 | |
Christopher Faulet | 5a2c661 | 2021-08-15 20:35:25 +0200 | [diff] [blame] | 1706 | :param class_channel channel: The manipulated Channel. |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 1707 | :returns: a boolean |
| 1708 | |
| 1709 | .. js:function:: Channel.output(channel) |
| 1710 | |
Christopher Faulet | 5a2c661 | 2021-08-15 20:35:25 +0200 | [diff] [blame] | 1711 | This function returns the length of outgoing data of the channel buffer. When |
| 1712 | called by a filter, this value is relative to the filter. |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 1713 | |
| 1714 | :param class_channel channel: The manipulated Channel. |
| 1715 | :returns: an integer containing the amount of available bytes. |
| 1716 | |
| 1717 | .. js:function:: Channel.prepend(channel, string) |
| 1718 | |
| 1719 | This function copies the string **string** in front of incoming data of the |
| 1720 | channel buffer. The function returns the copied length on success or -1 if |
| 1721 | data cannot be copied. |
| 1722 | |
| 1723 | Same that :js:func:`Channel.insert(channel, string, 0)`. |
| 1724 | |
| 1725 | :param class_channel channel: The manipulated Channel. |
Aurelien DARRAGON | 53901f4 | 2022-10-13 19:49:42 +0200 | [diff] [blame] | 1726 | :param string string: The data to copy in front of incoming data. |
Pieter Baauw | 4d7f766 | 2015-11-08 16:38:08 +0100 | [diff] [blame] | 1727 | :returns: an integer containing the amount of bytes copied or -1. |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 1728 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 1729 | .. js:function:: Channel.remove(channel [, offset [, length]]) |
| 1730 | |
| 1731 | This function removes **length** bytes of incoming data of the channel buffer, |
| 1732 | starting at offset **offset**. This function returns number of bytes removed |
| 1733 | on success. |
| 1734 | |
| 1735 | By default, if no length is provided, all incoming data, starting at the given |
Aurelien DARRAGON | 53901f4 | 2022-10-13 19:49:42 +0200 | [diff] [blame] | 1736 | offset, are removed. Not providing an offset is the same as setting it |
Ilya Shipitsin | ff0f278 | 2021-08-22 22:18:07 +0500 | [diff] [blame] | 1737 | to 0. A positive offset is relative to the beginning of incoming data of the |
Aurelien DARRAGON | 53901f4 | 2022-10-13 19:49:42 +0200 | [diff] [blame] | 1738 | channel buffer while negative offset is relative to the end. |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 1739 | |
| 1740 | :param class_channel channel: The manipulated Channel. |
Aurelien DARRAGON | 53901f4 | 2022-10-13 19:49:42 +0200 | [diff] [blame] | 1741 | :param integer offset: *optional* The offset in incoming data where to start |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 1742 | to remove data. 0 by default. May be negative to |
| 1743 | be relative to the end of incoming data. |
| 1744 | :param integer length: *optional* The length of data to remove. All incoming |
| 1745 | data by default. |
| 1746 | :returns: an integer containing the amount of bytes removed. |
| 1747 | |
Thierry FOURNIER | 486f5a0 | 2015-03-16 15:13:03 +0100 | [diff] [blame] | 1748 | .. js:function:: Channel.send(channel, string) |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 1749 | |
Christopher Faulet | 5a2c661 | 2021-08-15 20:35:25 +0200 | [diff] [blame] | 1750 | This function requires immediate send of the string **string**. It means the |
Ilya Shipitsin | ff0f278 | 2021-08-22 22:18:07 +0500 | [diff] [blame] | 1751 | string is copied at the beginning of incoming data of the channel buffer and |
Christopher Faulet | 5a2c661 | 2021-08-15 20:35:25 +0200 | [diff] [blame] | 1752 | immediately forwarded. Unless if the connection is close, and if called by an |
Aurelien DARRAGON | 53901f4 | 2022-10-13 19:49:42 +0200 | [diff] [blame] | 1753 | action, this function yields to copy and forward all the string. |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 1754 | |
Thierry FOURNIER | 486f5a0 | 2015-03-16 15:13:03 +0100 | [diff] [blame] | 1755 | :param class_channel channel: The manipulated Channel. |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 1756 | :param string string: The data to send. |
Pieter Baauw | 4d7f766 | 2015-11-08 16:38:08 +0100 | [diff] [blame] | 1757 | :returns: an integer containing the amount of bytes copied or -1. |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 1758 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 1759 | .. js:function:: Channel.set(channel, string [, offset [, length]]) |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 1760 | |
Aurelien DARRAGON | 53901f4 | 2022-10-13 19:49:42 +0200 | [diff] [blame] | 1761 | This function replaces **length** bytes of incoming data of the channel buffer, |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 1762 | starting at offset **offset**, by the string **string**. The function returns |
| 1763 | the copied length on success or -1 if data cannot be copied. |
| 1764 | |
| 1765 | By default, if no length is provided, all incoming data, starting at the given |
Aurelien DARRAGON | 53901f4 | 2022-10-13 19:49:42 +0200 | [diff] [blame] | 1766 | offset, are replaced. Not providing an offset is the same as setting it |
Ilya Shipitsin | ff0f278 | 2021-08-22 22:18:07 +0500 | [diff] [blame] | 1767 | to 0. A positive offset is relative to the beginning of incoming data of the |
Aurelien DARRAGON | 53901f4 | 2022-10-13 19:49:42 +0200 | [diff] [blame] | 1768 | channel buffer while negative offset is relative to the end. |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 1769 | |
Thierry FOURNIER | 486f5a0 | 2015-03-16 15:13:03 +0100 | [diff] [blame] | 1770 | :param class_channel channel: The manipulated Channel. |
Aurelien DARRAGON | 53901f4 | 2022-10-13 19:49:42 +0200 | [diff] [blame] | 1771 | :param string string: The data to copy into incoming data. |
| 1772 | :param integer offset: *optional* The offset in incoming data where to start |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 1773 | the data replacement. 0 by default. May be negative to |
| 1774 | be relative to the end of incoming data. |
| 1775 | :param integer length: *optional* The length of data to replace. All incoming |
| 1776 | data by default. |
| 1777 | :returns: an integer containing the amount of bytes copied or -1. |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 1778 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 1779 | .. js:function:: Channel.dup(channel) |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 1780 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 1781 | **DEPRECATED** |
| 1782 | |
| 1783 | This function returns all incoming data found in the channel buffer. The data |
Boyang Li | 60cfe8b | 2022-05-10 18:11:00 +0000 | [diff] [blame] | 1784 | are not removed from the buffer and can be reprocessed later. |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 1785 | |
| 1786 | If there is no incoming data and the channel can't receive more data, a 'nil' |
| 1787 | value is returned. |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 1788 | |
Thierry FOURNIER | 486f5a0 | 2015-03-16 15:13:03 +0100 | [diff] [blame] | 1789 | :param class_channel channel: The manipulated Channel. |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 1790 | :returns: a string containing all data found or nil. |
| 1791 | |
| 1792 | .. warning:: |
| 1793 | This function is deprecated. :js:func:`Channel.data()` must be used |
| 1794 | instead. |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 1795 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 1796 | .. js:function:: Channel.get(channel) |
| 1797 | |
| 1798 | **DEPRECATED** |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 1799 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 1800 | This function returns all incoming data found in the channel buffer and remove |
| 1801 | them from the buffer. |
| 1802 | |
| 1803 | If there is no incoming data and the channel can't receive more data, a 'nil' |
| 1804 | value is returned. |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 1805 | |
Thierry FOURNIER | 486f5a0 | 2015-03-16 15:13:03 +0100 | [diff] [blame] | 1806 | :param class_channel channel: The manipulated Channel. |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 1807 | :returns: a string containing all the data found or nil. |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 1808 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 1809 | .. warning:: |
| 1810 | This function is deprecated. :js:func:`Channel.data()` must be used to |
| 1811 | retrieve data followed by a call to :js:func:`Channel:remove()` to remove |
| 1812 | data. |
Thierry FOURNIER / OZON.IO | 65192f3 | 2016-11-07 15:28:40 +0100 | [diff] [blame] | 1813 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 1814 | .. code-block:: lua |
Thierry FOURNIER / OZON.IO | 65192f3 | 2016-11-07 15:28:40 +0100 | [diff] [blame] | 1815 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 1816 | local data = chn:data() |
| 1817 | chn:remove(0, data:len()) |
| 1818 | |
| 1819 | .. |
| 1820 | |
| 1821 | .. js:function:: Channel.getline(channel) |
| 1822 | |
| 1823 | **DEPRECATED** |
| 1824 | |
| 1825 | This function returns the first line found in incoming data of the channel |
Christopher Faulet | 5a2c661 | 2021-08-15 20:35:25 +0200 | [diff] [blame] | 1826 | buffer, including the '\\n'. The returned data are removed from the buffer. If |
| 1827 | no line is found, and if called by an action, this function yields to wait for |
| 1828 | more data, except if the channel can't receive more data. In this case all |
| 1829 | data are returned. |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 1830 | |
| 1831 | If there is no incoming data and the channel can't receive more data, a 'nil' |
| 1832 | value is returned. |
| 1833 | |
| 1834 | :param class_channel channel: The manipulated Channel. |
| 1835 | :returns: a string containing the line found or nil. |
| 1836 | |
| 1837 | .. warning:: |
Boyang Li | 60cfe8b | 2022-05-10 18:11:00 +0000 | [diff] [blame] | 1838 | This function is deprecated. :js:func:`Channel.line()` must be used to |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 1839 | retrieve a line followed by a call to :js:func:`Channel:remove()` to remove |
| 1840 | data. |
| 1841 | |
| 1842 | .. code-block:: lua |
| 1843 | |
| 1844 | local line = chn:line(0, -1) |
| 1845 | chn:remove(0, line:len()) |
| 1846 | |
| 1847 | .. |
| 1848 | |
| 1849 | .. js:function:: Channel.get_in_len(channel) |
| 1850 | |
Boyang Li | 60cfe8b | 2022-05-10 18:11:00 +0000 | [diff] [blame] | 1851 | **DEPRECATED** |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 1852 | |
Christopher Faulet | 5a2c661 | 2021-08-15 20:35:25 +0200 | [diff] [blame] | 1853 | This function returns the length of the input part of the buffer. When called |
| 1854 | by a filter, this value is relative to the filter. |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 1855 | |
| 1856 | :param class_channel channel: The manipulated Channel. |
| 1857 | :returns: an integer containing the amount of available bytes. |
| 1858 | |
| 1859 | .. warning:: |
| 1860 | This function is deprecated. :js:func:`Channel.input()` must be used |
| 1861 | instead. |
| 1862 | |
| 1863 | .. js:function:: Channel.get_out_len(channel) |
| 1864 | |
Boyang Li | 60cfe8b | 2022-05-10 18:11:00 +0000 | [diff] [blame] | 1865 | **DEPRECATED** |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 1866 | |
Christopher Faulet | 5a2c661 | 2021-08-15 20:35:25 +0200 | [diff] [blame] | 1867 | This function returns the length of the output part of the buffer. When called |
| 1868 | by a filter, this value is relative to the filter. |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 1869 | |
| 1870 | :param class_channel channel: The manipulated Channel. |
| 1871 | :returns: an integer containing the amount of available bytes. |
| 1872 | |
| 1873 | .. warning:: |
| 1874 | This function is deprecated. :js:func:`Channel.output()` must be used |
| 1875 | instead. |
Thierry FOURNIER | dc59500 | 2015-12-21 11:13:52 +0100 | [diff] [blame] | 1876 | |
| 1877 | .. _http_class: |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 1878 | |
| 1879 | HTTP class |
| 1880 | ========== |
| 1881 | |
| 1882 | .. js:class:: HTTP |
| 1883 | |
| 1884 | This class contain all the HTTP manipulation functions. |
| 1885 | |
Pieter Baauw | 386a127 | 2015-08-16 15:26:24 +0200 | [diff] [blame] | 1886 | .. js:function:: HTTP.req_get_headers(http) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 1887 | |
Patrick Hemmer | c6a1d71 | 2018-05-01 21:30:41 -0400 | [diff] [blame] | 1888 | Returns a table containing all the request headers. |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 1889 | |
| 1890 | :param class_http http: The related http object. |
Patrick Hemmer | c6a1d71 | 2018-05-01 21:30:41 -0400 | [diff] [blame] | 1891 | :returns: table of headers. |
Thierry FOURNIER | 12a865d | 2016-12-14 19:40:37 +0100 | [diff] [blame] | 1892 | :see: :js:func:`HTTP.res_get_headers` |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 1893 | |
Patrick Hemmer | c6a1d71 | 2018-05-01 21:30:41 -0400 | [diff] [blame] | 1894 | This is the form of the returned table: |
Thierry FOURNIER | dc59500 | 2015-12-21 11:13:52 +0100 | [diff] [blame] | 1895 | |
| 1896 | .. code-block:: lua |
| 1897 | |
| 1898 | HTTP:req_get_headers()['<header-name>'][<header-index>] = "<header-value>" |
| 1899 | |
| 1900 | local hdr = HTTP:req_get_headers() |
| 1901 | hdr["host"][0] = "www.test.com" |
| 1902 | hdr["accept"][0] = "audio/basic q=1" |
| 1903 | hdr["accept"][1] = "audio/*, q=0.2" |
| 1904 | hdr["accept"][2] = "*/*, q=0.1" |
| 1905 | .. |
| 1906 | |
Pieter Baauw | 386a127 | 2015-08-16 15:26:24 +0200 | [diff] [blame] | 1907 | .. js:function:: HTTP.res_get_headers(http) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 1908 | |
Patrick Hemmer | c6a1d71 | 2018-05-01 21:30:41 -0400 | [diff] [blame] | 1909 | Returns a table containing all the response headers. |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 1910 | |
| 1911 | :param class_http http: The related http object. |
Patrick Hemmer | c6a1d71 | 2018-05-01 21:30:41 -0400 | [diff] [blame] | 1912 | :returns: table of headers. |
Thierry FOURNIER | 12a865d | 2016-12-14 19:40:37 +0100 | [diff] [blame] | 1913 | :see: :js:func:`HTTP.req_get_headers` |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 1914 | |
Patrick Hemmer | c6a1d71 | 2018-05-01 21:30:41 -0400 | [diff] [blame] | 1915 | This is the form of the returned table: |
Thierry FOURNIER | dc59500 | 2015-12-21 11:13:52 +0100 | [diff] [blame] | 1916 | |
| 1917 | .. code-block:: lua |
| 1918 | |
| 1919 | HTTP:res_get_headers()['<header-name>'][<header-index>] = "<header-value>" |
| 1920 | |
| 1921 | local hdr = HTTP:req_get_headers() |
| 1922 | hdr["host"][0] = "www.test.com" |
| 1923 | hdr["accept"][0] = "audio/basic q=1" |
| 1924 | hdr["accept"][1] = "audio/*, q=0.2" |
| 1925 | hdr["accept"][2] = "*.*, q=0.1" |
| 1926 | .. |
| 1927 | |
Thierry FOURNIER | 486f5a0 | 2015-03-16 15:13:03 +0100 | [diff] [blame] | 1928 | .. js:function:: HTTP.req_add_header(http, name, value) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 1929 | |
Aurelien DARRAGON | 53901f4 | 2022-10-13 19:49:42 +0200 | [diff] [blame] | 1930 | Appends a HTTP header field in the request whose name is |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 1931 | specified in "name" and whose value is defined in "value". |
| 1932 | |
| 1933 | :param class_http http: The related http object. |
| 1934 | :param string name: The header name. |
| 1935 | :param string value: The header value. |
Thierry FOURNIER | 12a865d | 2016-12-14 19:40:37 +0100 | [diff] [blame] | 1936 | :see: :js:func:`HTTP.res_add_header` |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 1937 | |
Thierry FOURNIER | 486f5a0 | 2015-03-16 15:13:03 +0100 | [diff] [blame] | 1938 | .. js:function:: HTTP.res_add_header(http, name, value) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 1939 | |
Aurelien DARRAGON | 53901f4 | 2022-10-13 19:49:42 +0200 | [diff] [blame] | 1940 | Appends a HTTP header field in the response whose name is |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 1941 | specified in "name" and whose value is defined in "value". |
| 1942 | |
| 1943 | :param class_http http: The related http object. |
| 1944 | :param string name: The header name. |
| 1945 | :param string value: The header value. |
Thierry FOURNIER | 12a865d | 2016-12-14 19:40:37 +0100 | [diff] [blame] | 1946 | :see: :js:func:`HTTP.req_add_header` |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 1947 | |
Thierry FOURNIER | 486f5a0 | 2015-03-16 15:13:03 +0100 | [diff] [blame] | 1948 | .. js:function:: HTTP.req_del_header(http, name) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 1949 | |
| 1950 | Removes all HTTP header fields in the request whose name is |
| 1951 | specified in "name". |
| 1952 | |
| 1953 | :param class_http http: The related http object. |
| 1954 | :param string name: The header name. |
Thierry FOURNIER | 12a865d | 2016-12-14 19:40:37 +0100 | [diff] [blame] | 1955 | :see: :js:func:`HTTP.res_del_header` |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 1956 | |
Thierry FOURNIER | 486f5a0 | 2015-03-16 15:13:03 +0100 | [diff] [blame] | 1957 | .. js:function:: HTTP.res_del_header(http, name) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 1958 | |
| 1959 | Removes all HTTP header fields in the response whose name is |
| 1960 | specified in "name". |
| 1961 | |
| 1962 | :param class_http http: The related http object. |
| 1963 | :param string name: The header name. |
Thierry FOURNIER | 12a865d | 2016-12-14 19:40:37 +0100 | [diff] [blame] | 1964 | :see: :js:func:`HTTP.req_del_header` |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 1965 | |
Thierry FOURNIER | 486f5a0 | 2015-03-16 15:13:03 +0100 | [diff] [blame] | 1966 | .. js:function:: HTTP.req_set_header(http, name, value) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 1967 | |
Bertrand Jacquin | 874a35c | 2018-09-10 21:26:07 +0100 | [diff] [blame] | 1968 | This variable replace all occurrence of all header "name", by only |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 1969 | one containing the "value". |
| 1970 | |
| 1971 | :param class_http http: The related http object. |
| 1972 | :param string name: The header name. |
| 1973 | :param string value: The header value. |
Thierry FOURNIER | 12a865d | 2016-12-14 19:40:37 +0100 | [diff] [blame] | 1974 | :see: :js:func:`HTTP.res_set_header` |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 1975 | |
Bertrand Jacquin | 874a35c | 2018-09-10 21:26:07 +0100 | [diff] [blame] | 1976 | This function does the same work as the following code: |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 1977 | |
| 1978 | .. code-block:: lua |
| 1979 | |
| 1980 | function fcn(txn) |
Thierry FOURNIER | 486f5a0 | 2015-03-16 15:13:03 +0100 | [diff] [blame] | 1981 | TXN.http:req_del_header("header") |
| 1982 | TXN.http:req_add_header("header", "value") |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 1983 | end |
| 1984 | .. |
| 1985 | |
Thierry FOURNIER | 486f5a0 | 2015-03-16 15:13:03 +0100 | [diff] [blame] | 1986 | .. js:function:: HTTP.res_set_header(http, name, value) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 1987 | |
Aurelien DARRAGON | 53901f4 | 2022-10-13 19:49:42 +0200 | [diff] [blame] | 1988 | This function replaces all occurrence of all header "name", by only |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 1989 | one containing the "value". |
| 1990 | |
| 1991 | :param class_http http: The related http object. |
| 1992 | :param string name: The header name. |
| 1993 | :param string value: The header value. |
Thierry FOURNIER | 12a865d | 2016-12-14 19:40:37 +0100 | [diff] [blame] | 1994 | :see: :js:func:`HTTP.req_rep_header()` |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 1995 | |
Pieter Baauw | 386a127 | 2015-08-16 15:26:24 +0200 | [diff] [blame] | 1996 | .. js:function:: HTTP.req_rep_header(http, name, regex, replace) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 1997 | |
| 1998 | Matches the regular expression in all occurrences of header field "name" |
| 1999 | according to "regex", and replaces them with the "replace" argument. The |
| 2000 | replacement value can contain back references like \1, \2, ... This |
| 2001 | function works with the request. |
| 2002 | |
| 2003 | :param class_http http: The related http object. |
| 2004 | :param string name: The header name. |
| 2005 | :param string regex: The match regular expression. |
| 2006 | :param string replace: The replacement value. |
Thierry FOURNIER | 12a865d | 2016-12-14 19:40:37 +0100 | [diff] [blame] | 2007 | :see: :js:func:`HTTP.res_rep_header()` |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 2008 | |
Pieter Baauw | 386a127 | 2015-08-16 15:26:24 +0200 | [diff] [blame] | 2009 | .. js:function:: HTTP.res_rep_header(http, name, regex, string) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 2010 | |
| 2011 | Matches the regular expression in all occurrences of header field "name" |
| 2012 | according to "regex", and replaces them with the "replace" argument. The |
| 2013 | replacement value can contain back references like \1, \2, ... This |
| 2014 | function works with the request. |
| 2015 | |
| 2016 | :param class_http http: The related http object. |
| 2017 | :param string name: The header name. |
| 2018 | :param string regex: The match regular expression. |
| 2019 | :param string replace: The replacement value. |
Thierry FOURNIER | 12a865d | 2016-12-14 19:40:37 +0100 | [diff] [blame] | 2020 | :see: :js:func:`HTTP.req_rep_header()` |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 2021 | |
Thierry FOURNIER | 486f5a0 | 2015-03-16 15:13:03 +0100 | [diff] [blame] | 2022 | .. js:function:: HTTP.req_set_method(http, method) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 2023 | |
| 2024 | Rewrites the request method with the parameter "method". |
| 2025 | |
| 2026 | :param class_http http: The related http object. |
| 2027 | :param string method: The new method. |
| 2028 | |
Thierry FOURNIER | 486f5a0 | 2015-03-16 15:13:03 +0100 | [diff] [blame] | 2029 | .. js:function:: HTTP.req_set_path(http, path) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 2030 | |
| 2031 | Rewrites the request path with the "path" parameter. |
| 2032 | |
| 2033 | :param class_http http: The related http object. |
| 2034 | :param string path: The new path. |
| 2035 | |
Thierry FOURNIER | 486f5a0 | 2015-03-16 15:13:03 +0100 | [diff] [blame] | 2036 | .. js:function:: HTTP.req_set_query(http, query) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 2037 | |
| 2038 | Rewrites the request's query string which appears after the first question |
| 2039 | mark ("?") with the parameter "query". |
| 2040 | |
| 2041 | :param class_http http: The related http object. |
| 2042 | :param string query: The new query. |
| 2043 | |
Thierry FOURNIER | 0d79cf6 | 2015-08-26 14:20:58 +0200 | [diff] [blame] | 2044 | .. js:function:: HTTP.req_set_uri(http, uri) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 2045 | |
| 2046 | Rewrites the request URI with the parameter "uri". |
| 2047 | |
| 2048 | :param class_http http: The related http object. |
| 2049 | :param string uri: The new uri. |
| 2050 | |
Robin H. Johnson | 52f5db2 | 2017-01-01 13:10:52 -0800 | [diff] [blame] | 2051 | .. js:function:: HTTP.res_set_status(http, status [, reason]) |
Thierry FOURNIER | 35d70ef | 2015-08-26 16:21:56 +0200 | [diff] [blame] | 2052 | |
Robin H. Johnson | 52f5db2 | 2017-01-01 13:10:52 -0800 | [diff] [blame] | 2053 | Rewrites the response status code with the parameter "code". |
| 2054 | |
| 2055 | 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] | 2056 | |
| 2057 | :param class_http http: The related http object. |
| 2058 | :param integer status: The new response status code. |
Robin H. Johnson | 52f5db2 | 2017-01-01 13:10:52 -0800 | [diff] [blame] | 2059 | :param string reason: The new response reason (optional). |
Thierry FOURNIER | 35d70ef | 2015-08-26 16:21:56 +0200 | [diff] [blame] | 2060 | |
William Lallemand | 00a1502 | 2021-11-19 16:02:44 +0100 | [diff] [blame] | 2061 | .. _httpclient_class: |
| 2062 | |
| 2063 | HTTPClient class |
| 2064 | ================ |
| 2065 | |
| 2066 | .. js:class:: HTTPClient |
| 2067 | |
| 2068 | The httpclient class allows issue of outbound HTTP requests through a simple |
| 2069 | API without the knowledge of HAProxy internals. |
| 2070 | |
| 2071 | .. js:function:: HTTPClient.get(httpclient, request) |
| 2072 | .. js:function:: HTTPClient.head(httpclient, request) |
| 2073 | .. js:function:: HTTPClient.put(httpclient, request) |
| 2074 | .. js:function:: HTTPClient.post(httpclient, request) |
| 2075 | .. js:function:: HTTPClient.delete(httpclient, request) |
| 2076 | |
Aurelien DARRAGON | 53901f4 | 2022-10-13 19:49:42 +0200 | [diff] [blame] | 2077 | Send a HTTP request and wait for a response. GET, HEAD PUT, POST and DELETE methods can be used. |
| 2078 | The HTTPClient will send asynchronously the data and is able to send and receive more than HAProxy bufsize. |
William Lallemand | 00a1502 | 2021-11-19 16:02:44 +0100 | [diff] [blame] | 2079 | |
William Lallemand | a925619 | 2022-10-21 11:48:24 +0200 | [diff] [blame] | 2080 | The HTTPClient interface is not able to decompress responses, it is not |
| 2081 | recommended to send an Accept-Encoding in the request so the response is |
| 2082 | received uncompressed. |
William Lallemand | 00a1502 | 2021-11-19 16:02:44 +0100 | [diff] [blame] | 2083 | |
| 2084 | :param class httpclient: Is the manipulated HTTPClient. |
| 2085 | :param table request: Is a table containing the parameters of the request that will be send. |
| 2086 | :param string request.url: Is a mandatory parameter for the request that contains the URL. |
| 2087 | :param string request.body: Is an optional parameter for the request that contains the body to send. |
| 2088 | :param table request.headers: Is an optional parameter for the request that contains the headers to send. |
William Lallemand | 1834030 | 2022-02-23 15:57:45 +0100 | [diff] [blame] | 2089 | :param string request.dst: Is an optional parameter for the destination in haproxy address format. |
William Lallemand | b4a4ef6 | 2022-02-23 14:18:16 +0100 | [diff] [blame] | 2090 | :param integer request.timeout: Optional timeout parameter, set a "timeout server" on the connections. |
William Lallemand | 00a1502 | 2021-11-19 16:02:44 +0100 | [diff] [blame] | 2091 | :returns: Lua table containing the response |
| 2092 | |
| 2093 | |
| 2094 | .. code-block:: lua |
| 2095 | |
| 2096 | local httpclient = core.httpclient() |
William Lallemand | 4f4f2b7 | 2022-02-17 20:00:23 +0100 | [diff] [blame] | 2097 | local response = httpclient:post{url="http://127.0.0.1", body=body, dst="unix@/var/run/http.sock"} |
William Lallemand | 00a1502 | 2021-11-19 16:02:44 +0100 | [diff] [blame] | 2098 | |
| 2099 | .. |
| 2100 | |
| 2101 | .. code-block:: lua |
| 2102 | |
| 2103 | response = { |
| 2104 | status = 400, |
| 2105 | reason = "Bad request", |
| 2106 | headers = { |
| 2107 | ["content-type"] = { "text/html" }, |
| 2108 | ["cache-control"] = { "no-cache", "no-store" }, |
| 2109 | }, |
William Lallemand | 4f4f2b7 | 2022-02-17 20:00:23 +0100 | [diff] [blame] | 2110 | body = "<html><body><h1>invalid request<h1></body></html>", |
William Lallemand | 00a1502 | 2021-11-19 16:02:44 +0100 | [diff] [blame] | 2111 | } |
| 2112 | .. |
| 2113 | |
| 2114 | |
Thierry FOURNIER | dc59500 | 2015-12-21 11:13:52 +0100 | [diff] [blame] | 2115 | .. _txn_class: |
| 2116 | |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 2117 | TXN class |
| 2118 | ========= |
| 2119 | |
| 2120 | .. js:class:: TXN |
| 2121 | |
| 2122 | The txn class contain all the functions relative to the http or tcp |
| 2123 | transaction (Note than a tcp stream is the same than a tcp transaction, but |
Aurelien DARRAGON | 53901f4 | 2022-10-13 19:49:42 +0200 | [diff] [blame] | 2124 | a HTTP transaction is not the same than a tcp stream). |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 2125 | |
| 2126 | The usage of this class permits to retrieve data from the requests, alter it |
| 2127 | and forward it. |
| 2128 | |
| 2129 | All the functions provided by this class are available in the context |
Christopher Faulet | 5a2c661 | 2021-08-15 20:35:25 +0200 | [diff] [blame] | 2130 | **sample-fetches**, **actions** and **filters**. |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 2131 | |
Thierry FOURNIER | 486f5a0 | 2015-03-16 15:13:03 +0100 | [diff] [blame] | 2132 | .. js:attribute:: TXN.c |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 2133 | |
Thierry FOURNIER | dc59500 | 2015-12-21 11:13:52 +0100 | [diff] [blame] | 2134 | :returns: An :ref:`converters_class`. |
| 2135 | |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 2136 | This attribute contains a Converters class object. |
| 2137 | |
Thierry FOURNIER | 486f5a0 | 2015-03-16 15:13:03 +0100 | [diff] [blame] | 2138 | .. js:attribute:: TXN.sc |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 2139 | |
Thierry FOURNIER | dc59500 | 2015-12-21 11:13:52 +0100 | [diff] [blame] | 2140 | :returns: An :ref:`converters_class`. |
| 2141 | |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 2142 | This attribute contains a Converters class object. The functions of |
| 2143 | this object returns always a string. |
| 2144 | |
Thierry FOURNIER | 486f5a0 | 2015-03-16 15:13:03 +0100 | [diff] [blame] | 2145 | .. js:attribute:: TXN.f |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 2146 | |
Thierry FOURNIER | dc59500 | 2015-12-21 11:13:52 +0100 | [diff] [blame] | 2147 | :returns: An :ref:`fetches_class`. |
| 2148 | |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 2149 | This attribute contains a Fetches class object. |
| 2150 | |
Thierry FOURNIER | 486f5a0 | 2015-03-16 15:13:03 +0100 | [diff] [blame] | 2151 | .. js:attribute:: TXN.sf |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 2152 | |
Thierry FOURNIER | dc59500 | 2015-12-21 11:13:52 +0100 | [diff] [blame] | 2153 | :returns: An :ref:`fetches_class`. |
| 2154 | |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 2155 | This attribute contains a Fetches class object. The functions of |
| 2156 | this object returns always a string. |
| 2157 | |
Thierry FOURNIER | 486f5a0 | 2015-03-16 15:13:03 +0100 | [diff] [blame] | 2158 | .. js:attribute:: TXN.req |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 2159 | |
Thierry FOURNIER | dc59500 | 2015-12-21 11:13:52 +0100 | [diff] [blame] | 2160 | :returns: An :ref:`channel_class`. |
| 2161 | |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 2162 | This attribute contains a channel class object for the request buffer. |
| 2163 | |
Thierry FOURNIER | 486f5a0 | 2015-03-16 15:13:03 +0100 | [diff] [blame] | 2164 | .. js:attribute:: TXN.res |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 2165 | |
Thierry FOURNIER | dc59500 | 2015-12-21 11:13:52 +0100 | [diff] [blame] | 2166 | :returns: An :ref:`channel_class`. |
| 2167 | |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 2168 | This attribute contains a channel class object for the response buffer. |
| 2169 | |
Thierry FOURNIER | 486f5a0 | 2015-03-16 15:13:03 +0100 | [diff] [blame] | 2170 | .. js:attribute:: TXN.http |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 2171 | |
Thierry FOURNIER | dc59500 | 2015-12-21 11:13:52 +0100 | [diff] [blame] | 2172 | :returns: An :ref:`http_class`. |
| 2173 | |
Aurelien DARRAGON | 53901f4 | 2022-10-13 19:49:42 +0200 | [diff] [blame] | 2174 | This attribute contains a HTTP class object. It is available only if the |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 2175 | proxy has the "mode http" enabled. |
| 2176 | |
Christopher Faulet | 5a2c661 | 2021-08-15 20:35:25 +0200 | [diff] [blame] | 2177 | .. js:attribute:: TXN.http_req |
| 2178 | |
| 2179 | :returns: An :ref:`httpmessage_class`. |
| 2180 | |
| 2181 | This attribute contains the request HTTPMessage class object. It is available |
| 2182 | only if the proxy has the "mode http" enabled and only in the **filters** |
| 2183 | context. |
| 2184 | |
| 2185 | .. js:attribute:: TXN.http_res |
| 2186 | |
| 2187 | :returns: An :ref:`httpmessage_class`. |
| 2188 | |
| 2189 | This attribute contains the response HTTPMessage class object. It is available |
| 2190 | only if the proxy has the "mode http" enabled and only in the **filters** |
| 2191 | context. |
| 2192 | |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 2193 | .. js:function:: TXN.log(TXN, loglevel, msg) |
| 2194 | |
| 2195 | This function sends a log. The log is sent, according with the HAProxy |
| 2196 | configuration file, on the default syslog server if it is configured and on |
| 2197 | the stderr if it is allowed. |
| 2198 | |
| 2199 | :param class_txn txn: The class txn object containing the data. |
Bertrand Jacquin | 874a35c | 2018-09-10 21:26:07 +0100 | [diff] [blame] | 2200 | :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] | 2201 | number between 0 and 7. |
| 2202 | :param string msg: The log content. |
Thierry FOURNIER | 12a865d | 2016-12-14 19:40:37 +0100 | [diff] [blame] | 2203 | :see: :js:attr:`core.emerg`, :js:attr:`core.alert`, :js:attr:`core.crit`, |
| 2204 | :js:attr:`core.err`, :js:attr:`core.warning`, :js:attr:`core.notice`, |
| 2205 | :js:attr:`core.info`, :js:attr:`core.debug` (log level definitions) |
| 2206 | :see: :js:func:`TXN.deflog` |
| 2207 | :see: :js:func:`TXN.Debug` |
| 2208 | :see: :js:func:`TXN.Info` |
| 2209 | :see: :js:func:`TXN.Warning` |
| 2210 | :see: :js:func:`TXN.Alert` |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 2211 | |
| 2212 | .. js:function:: TXN.deflog(TXN, msg) |
| 2213 | |
Bertrand Jacquin | 874a35c | 2018-09-10 21:26:07 +0100 | [diff] [blame] | 2214 | 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] | 2215 | transaction. |
| 2216 | |
| 2217 | :param class_txn txn: The class txn object containing the data. |
| 2218 | :param string msg: The log content. |
Aurelien DARRAGON | 53901f4 | 2022-10-13 19:49:42 +0200 | [diff] [blame] | 2219 | :see: :js:func:`TXN.log` |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 2220 | |
| 2221 | .. js:function:: TXN.Debug(txn, msg) |
| 2222 | |
| 2223 | :param class_txn txn: The class txn object containing the data. |
| 2224 | :param string msg: The log content. |
Thierry FOURNIER | 12a865d | 2016-12-14 19:40:37 +0100 | [diff] [blame] | 2225 | :see: :js:func:`TXN.log` |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 2226 | |
Aurelien DARRAGON | 53901f4 | 2022-10-13 19:49:42 +0200 | [diff] [blame] | 2227 | Does the same job as: |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 2228 | |
| 2229 | .. code-block:: lua |
| 2230 | |
Thierry FOURNIER | dc59500 | 2015-12-21 11:13:52 +0100 | [diff] [blame] | 2231 | function Debug(txn, msg) |
| 2232 | TXN.log(txn, core.debug, msg) |
| 2233 | end |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 2234 | .. |
| 2235 | |
| 2236 | .. js:function:: TXN.Info(txn, msg) |
| 2237 | |
| 2238 | :param class_txn txn: The class txn object containing the data. |
| 2239 | :param string msg: The log content. |
Thierry FOURNIER | 12a865d | 2016-12-14 19:40:37 +0100 | [diff] [blame] | 2240 | :see: :js:func:`TXN.log` |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 2241 | |
Aurelien DARRAGON | 53901f4 | 2022-10-13 19:49:42 +0200 | [diff] [blame] | 2242 | Does the same job as: |
| 2243 | |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 2244 | .. code-block:: lua |
| 2245 | |
Aurelien DARRAGON | 53901f4 | 2022-10-13 19:49:42 +0200 | [diff] [blame] | 2246 | function Info(txn, msg) |
Thierry FOURNIER | dc59500 | 2015-12-21 11:13:52 +0100 | [diff] [blame] | 2247 | TXN.log(txn, core.info, msg) |
| 2248 | end |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 2249 | .. |
| 2250 | |
| 2251 | .. js:function:: TXN.Warning(txn, msg) |
| 2252 | |
| 2253 | :param class_txn txn: The class txn object containing the data. |
| 2254 | :param string msg: The log content. |
Thierry FOURNIER | 12a865d | 2016-12-14 19:40:37 +0100 | [diff] [blame] | 2255 | :see: :js:func:`TXN.log` |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 2256 | |
Aurelien DARRAGON | 53901f4 | 2022-10-13 19:49:42 +0200 | [diff] [blame] | 2257 | Does the same job as: |
| 2258 | |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 2259 | .. code-block:: lua |
| 2260 | |
Aurelien DARRAGON | 53901f4 | 2022-10-13 19:49:42 +0200 | [diff] [blame] | 2261 | function Warning(txn, msg) |
Thierry FOURNIER | dc59500 | 2015-12-21 11:13:52 +0100 | [diff] [blame] | 2262 | TXN.log(txn, core.warning, msg) |
| 2263 | end |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 2264 | .. |
| 2265 | |
| 2266 | .. js:function:: TXN.Alert(txn, msg) |
| 2267 | |
| 2268 | :param class_txn txn: The class txn object containing the data. |
| 2269 | :param string msg: The log content. |
Thierry FOURNIER | 12a865d | 2016-12-14 19:40:37 +0100 | [diff] [blame] | 2270 | :see: :js:func:`TXN.log` |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 2271 | |
Aurelien DARRAGON | 53901f4 | 2022-10-13 19:49:42 +0200 | [diff] [blame] | 2272 | Does the same job as: |
| 2273 | |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 2274 | .. code-block:: lua |
| 2275 | |
Aurelien DARRAGON | 53901f4 | 2022-10-13 19:49:42 +0200 | [diff] [blame] | 2276 | function Alert(txn, msg) |
Thierry FOURNIER | dc59500 | 2015-12-21 11:13:52 +0100 | [diff] [blame] | 2277 | TXN.log(txn, core.alert, msg) |
| 2278 | end |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 2279 | .. |
| 2280 | |
Thierry FOURNIER | 486f5a0 | 2015-03-16 15:13:03 +0100 | [diff] [blame] | 2281 | .. js:function:: TXN.get_priv(txn) |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 2282 | |
Thierry FOURNIER | 486f5a0 | 2015-03-16 15:13:03 +0100 | [diff] [blame] | 2283 | 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] | 2284 | function. If no data are stored, it returns a nil value. |
| 2285 | |
| 2286 | :param class_txn txn: The class txn object containing the data. |
Bertrand Jacquin | 874a35c | 2018-09-10 21:26:07 +0100 | [diff] [blame] | 2287 | :returns: the opaque data previously stored, or nil if nothing is |
| 2288 | available. |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 2289 | |
Thierry FOURNIER | 486f5a0 | 2015-03-16 15:13:03 +0100 | [diff] [blame] | 2290 | .. js:function:: TXN.set_priv(txn, data) |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 2291 | |
Aurelien DARRAGON | 53901f4 | 2022-10-13 19:49:42 +0200 | [diff] [blame] | 2292 | Store any data in the current HAProxy transaction. This action replaces the |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 2293 | old stored data. |
| 2294 | |
| 2295 | :param class_txn txn: The class txn object containing the data. |
| 2296 | :param opaque data: The data which is stored in the transaction. |
| 2297 | |
Tim Duesterhus | 4e172c9 | 2020-05-19 13:49:42 +0200 | [diff] [blame] | 2298 | .. js:function:: TXN.set_var(TXN, var, value[, ifexist]) |
Thierry FOURNIER | 053ba8ad | 2015-06-08 13:05:33 +0200 | [diff] [blame] | 2299 | |
David Carlier | 61fdf8b | 2015-10-02 11:59:38 +0100 | [diff] [blame] | 2300 | 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] | 2301 | |
| 2302 | :param class_txn txn: The class txn object containing the data. |
| 2303 | :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] | 2304 | :param type value: The value associated to the variable. The type can be string or |
| 2305 | integer. |
Aurelien DARRAGON | 53901f4 | 2022-10-13 19:49:42 +0200 | [diff] [blame] | 2306 | :param boolean ifexist: If this parameter is set to true the variable |
Tim Duesterhus | 4e172c9 | 2020-05-19 13:49:42 +0200 | [diff] [blame] | 2307 | will only be set if it was defined elsewhere (i.e. used |
Willy Tarreau | 7978c5c | 2021-09-07 14:24:07 +0200 | [diff] [blame] | 2308 | within the configuration). For global variables (using the |
| 2309 | "proc" scope), they will only be updated and never created. |
| 2310 | It is highly recommended to always set this to true. |
Christopher Faulet | 85d79c9 | 2016-11-09 16:54:56 +0100 | [diff] [blame] | 2311 | |
| 2312 | .. js:function:: TXN.unset_var(TXN, var) |
| 2313 | |
| 2314 | Unset the variable <var>. |
| 2315 | |
| 2316 | :param class_txn txn: The class txn object containing the data. |
| 2317 | :param string var: The variable name according with the HAProxy variable syntax. |
Thierry FOURNIER | 053ba8ad | 2015-06-08 13:05:33 +0200 | [diff] [blame] | 2318 | |
| 2319 | .. js:function:: TXN.get_var(TXN, var) |
| 2320 | |
| 2321 | Returns data stored in the variable <var> converter in Lua type. |
| 2322 | |
| 2323 | :param class_txn txn: The class txn object containing the data. |
| 2324 | :param string var: The variable name according with the HAProxy variable syntax. |
| 2325 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 2326 | .. js:function:: TXN.reply([reply]) |
| 2327 | |
| 2328 | Return a new reply object |
| 2329 | |
| 2330 | :param table reply: A table containing info to initialize the reply fields. |
| 2331 | :returns: A :ref:`reply_class` object. |
| 2332 | |
| 2333 | The table used to initialized the reply object may contain following entries : |
| 2334 | |
| 2335 | * status : The reply status code. the code 200 is used by default. |
| 2336 | * reason : The reply reason. The reason corresponding to the status code is |
| 2337 | used by default. |
Aurelien DARRAGON | 53901f4 | 2022-10-13 19:49:42 +0200 | [diff] [blame] | 2338 | * headers : A list of headers, indexed by header name. Empty by default. For |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 2339 | a given name, multiple values are possible, stored in an ordered list. |
| 2340 | * body : The reply body, empty by default. |
| 2341 | |
| 2342 | .. code-block:: lua |
| 2343 | |
| 2344 | local reply = txn:reply{ |
| 2345 | status = 400, |
| 2346 | reason = "Bad request", |
| 2347 | headers = { |
| 2348 | ["content-type"] = { "text/html" }, |
| 2349 | ["cache-control"] = {"no-cache", "no-store" } |
| 2350 | }, |
| 2351 | body = "<html><body><h1>invalid request<h1></body></html>" |
| 2352 | } |
| 2353 | .. |
| 2354 | :see: :js:class:`Reply` |
| 2355 | |
| 2356 | .. js:function:: TXN.done(txn[, reply]) |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 2357 | |
Willy Tarreau | bc183a6 | 2015-08-28 10:39:11 +0200 | [diff] [blame] | 2358 | This function terminates processing of the transaction and the associated |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 2359 | session and optionally reply to the client for HTTP sessions. |
| 2360 | |
| 2361 | :param class_txn txn: The class txn object containing the data. |
| 2362 | :param class_reply reply: The class reply object to return to the client. |
| 2363 | |
| 2364 | 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] | 2365 | 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] | 2366 | To do so, a reply may be provided. This object is optional and may contain a |
| 2367 | status code, a reason, a header list and a body. All these fields are |
Christopher Faulet | 7855b19 | 2021-11-09 18:39:51 +0100 | [diff] [blame] | 2368 | optional. When not provided, the default values are used. By default, with an |
| 2369 | empty reply object, an empty HTTP 200 response is returned to the client. If |
| 2370 | no reply object is provided, the transaction is terminated without any |
| 2371 | reply. If a reply object is provided, it must not exceed the buffer size once |
Aurelien DARRAGON | 53901f4 | 2022-10-13 19:49:42 +0200 | [diff] [blame] | 2372 | converted into the internal HTTP representation. Because for now there is no |
Christopher Faulet | 7855b19 | 2021-11-09 18:39:51 +0100 | [diff] [blame] | 2373 | easy way to be sure it fits, it is probably better to keep it reasonably |
| 2374 | small. |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 2375 | |
| 2376 | The reply object may be fully created in lua or the class Reply may be used to |
| 2377 | create it. |
| 2378 | |
| 2379 | .. code-block:: lua |
| 2380 | |
| 2381 | local reply = txn:reply() |
| 2382 | reply:set_status(400, "Bad request") |
| 2383 | reply:add_header("content-type", "text/html") |
| 2384 | reply:add_header("cache-control", "no-cache") |
| 2385 | reply:add_header("cache-control", "no-store") |
| 2386 | reply:set_body("<html><body><h1>invalid request<h1></body></html>") |
| 2387 | txn:done(reply) |
| 2388 | .. |
| 2389 | |
| 2390 | .. code-block:: lua |
| 2391 | |
| 2392 | txn:done{ |
| 2393 | status = 400, |
| 2394 | reason = "Bad request", |
| 2395 | headers = { |
| 2396 | ["content-type"] = { "text/html" }, |
| 2397 | ["cache-control"] = { "no-cache", "no-store" }, |
| 2398 | }, |
| 2399 | body = "<html><body><h1>invalid request<h1></body></html>" |
| 2400 | } |
| 2401 | .. |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 2402 | |
Christopher Faulet | 1e9b1b6 | 2021-08-11 10:14:30 +0200 | [diff] [blame] | 2403 | .. warning:: |
Aurelien DARRAGON | 53901f4 | 2022-10-13 19:49:42 +0200 | [diff] [blame] | 2404 | It does not make sense to call this function from sample-fetches. In this case |
| 2405 | the behavior is the same than core.done(): it finishes the Lua |
Christopher Faulet | 1e9b1b6 | 2021-08-11 10:14:30 +0200 | [diff] [blame] | 2406 | execution. The transaction is really aborted only from an action registered |
| 2407 | function. |
Thierry FOURNIER | ab00df6 | 2016-07-14 11:42:37 +0200 | [diff] [blame] | 2408 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 2409 | :see: :js:func:`TXN.reply`, :js:class:`Reply` |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 2410 | |
Thierry FOURNIER | 486f5a0 | 2015-03-16 15:13:03 +0100 | [diff] [blame] | 2411 | .. js:function:: TXN.set_loglevel(txn, loglevel) |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 2412 | |
| 2413 | Is used to change the log level of the current request. The "loglevel" must |
| 2414 | be an integer between 0 and 7. |
| 2415 | |
| 2416 | :param class_txn txn: The class txn object containing the data. |
| 2417 | :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] | 2418 | :see: :js:attr:`core.emerg`, :js:attr:`core.alert`, :js:attr:`core.crit`, |
| 2419 | :js:attr:`core.err`, :js:attr:`core.warning`, :js:attr:`core.notice`, |
| 2420 | :js:attr:`core.info`, :js:attr:`core.debug` (log level definitions) |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 2421 | |
Thierry FOURNIER | 486f5a0 | 2015-03-16 15:13:03 +0100 | [diff] [blame] | 2422 | .. js:function:: TXN.set_tos(txn, tos) |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 2423 | |
| 2424 | Is used to set the TOS or DSCP field value of packets sent to the client to |
| 2425 | the value passed in "tos" on platforms which support this. |
| 2426 | |
| 2427 | :param class_txn txn: The class txn object containing the data. |
| 2428 | :param integer tos: The new TOS os DSCP. |
| 2429 | |
Thierry FOURNIER | 486f5a0 | 2015-03-16 15:13:03 +0100 | [diff] [blame] | 2430 | .. js:function:: TXN.set_mark(txn, mark) |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 2431 | |
| 2432 | Is used to set the Netfilter MARK on all packets sent to the client to the |
| 2433 | value passed in "mark" on platforms which support it. |
| 2434 | |
| 2435 | :param class_txn txn: The class txn object containing the data. |
| 2436 | :param integer mark: The mark value. |
| 2437 | |
Patrick Hemmer | 268a707 | 2018-05-11 12:52:31 -0400 | [diff] [blame] | 2438 | .. js:function:: TXN.set_priority_class(txn, prio) |
| 2439 | |
| 2440 | This function adjusts the priority class of the transaction. The value should |
| 2441 | be within the range -2047..2047. Values outside this range will be |
| 2442 | truncated. |
| 2443 | |
| 2444 | See the HAProxy configuration.txt file keyword "http-request" action |
| 2445 | "set-priority-class" for details. |
| 2446 | |
| 2447 | .. js:function:: TXN.set_priority_offset(txn, prio) |
| 2448 | |
| 2449 | This function adjusts the priority offset of the transaction. The value |
| 2450 | should be within the range -524287..524287. Values outside this range will be |
| 2451 | truncated. |
| 2452 | |
| 2453 | See the HAProxy configuration.txt file keyword "http-request" action |
| 2454 | "set-priority-offset" for details. |
| 2455 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 2456 | .. _reply_class: |
| 2457 | |
| 2458 | Reply class |
| 2459 | ============ |
| 2460 | |
| 2461 | .. js:class:: Reply |
| 2462 | |
| 2463 | **context**: action |
| 2464 | |
Aurelien DARRAGON | 53901f4 | 2022-10-13 19:49:42 +0200 | [diff] [blame] | 2465 | This class represents a HTTP response message. It provides some methods to |
Christopher Faulet | 7855b19 | 2021-11-09 18:39:51 +0100 | [diff] [blame] | 2466 | enrich it. Once converted into the internal HTTP representation, the response |
| 2467 | message must not exceed the buffer size. Because for now there is no |
| 2468 | easy way to be sure it fits, it is probably better to keep it reasonably |
| 2469 | small. |
| 2470 | |
Aurelien DARRAGON | 53901f4 | 2022-10-13 19:49:42 +0200 | [diff] [blame] | 2471 | See tune.bufsize in the configuration manual for details. |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 2472 | |
| 2473 | .. code-block:: lua |
| 2474 | |
| 2475 | local reply = txn:reply({status = 400}) -- default HTTP 400 reason-phase used |
| 2476 | reply:add_header("content-type", "text/html") |
| 2477 | reply:add_header("cache-control", "no-cache") |
| 2478 | reply:add_header("cache-control", "no-store") |
| 2479 | reply:set_body("<html><body><h1>invalid request<h1></body></html>") |
| 2480 | .. |
| 2481 | |
| 2482 | :see: :js:func:`TXN.reply` |
| 2483 | |
| 2484 | .. js:attribute:: Reply.status |
| 2485 | |
| 2486 | The reply status code. By default, the status code is set to 200. |
| 2487 | |
| 2488 | :returns: integer |
| 2489 | |
| 2490 | .. js:attribute:: Reply.reason |
| 2491 | |
| 2492 | The reason string describing the status code. |
| 2493 | |
| 2494 | :returns: string |
| 2495 | |
| 2496 | .. js:attribute:: Reply.headers |
| 2497 | |
| 2498 | A table indexing all reply headers by name. To each name is associated an |
| 2499 | ordered list of values. |
| 2500 | |
| 2501 | :returns: Lua table |
| 2502 | |
| 2503 | .. code-block:: lua |
| 2504 | |
| 2505 | { |
| 2506 | ["content-type"] = { "text/html" }, |
| 2507 | ["cache-control"] = {"no-cache", "no-store" }, |
| 2508 | x_header_name = { "value1", "value2", ... } |
| 2509 | ... |
| 2510 | } |
| 2511 | .. |
| 2512 | |
| 2513 | .. js:attribute:: Reply.body |
| 2514 | |
| 2515 | The reply payload. |
| 2516 | |
| 2517 | :returns: string |
| 2518 | |
| 2519 | .. js:function:: Reply.set_status(REPLY, status[, reason]) |
| 2520 | |
| 2521 | Set the reply status code and optionally the reason-phrase. If the reason is |
| 2522 | not provided, the default reason corresponding to the status code is used. |
| 2523 | |
| 2524 | :param class_reply reply: The related Reply object. |
| 2525 | :param integer status: The reply status code. |
| 2526 | :param string reason: The reply status reason (optional). |
| 2527 | |
| 2528 | .. js:function:: Reply.add_header(REPLY, name, value) |
| 2529 | |
| 2530 | Add a header to the reply object. If the header does not already exist, a new |
| 2531 | entry is created with its name as index and a one-element list containing its |
| 2532 | value as value. Otherwise, the header value is appended to the ordered list of |
| 2533 | values associated to the header name. |
| 2534 | |
| 2535 | :param class_reply reply: The related Reply object. |
| 2536 | :param string name: The header field name. |
| 2537 | :param string value: The header field value. |
| 2538 | |
| 2539 | .. js:function:: Reply.del_header(REPLY, name) |
| 2540 | |
| 2541 | Remove all occurrences of a header name from the reply object. |
| 2542 | |
| 2543 | :param class_reply reply: The related Reply object. |
| 2544 | :param string name: The header field name. |
| 2545 | |
| 2546 | .. js:function:: Reply.set_body(REPLY, body) |
| 2547 | |
| 2548 | Set the reply payload. |
| 2549 | |
| 2550 | :param class_reply reply: The related Reply object. |
| 2551 | :param string body: The reply payload. |
| 2552 | |
Thierry FOURNIER | dc59500 | 2015-12-21 11:13:52 +0100 | [diff] [blame] | 2553 | .. _socket_class: |
| 2554 | |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 2555 | Socket class |
| 2556 | ============ |
| 2557 | |
| 2558 | .. js:class:: Socket |
| 2559 | |
| 2560 | This class must be compatible with the Lua Socket class. Only the 'client' |
| 2561 | functions are available. See the Lua Socket documentation: |
| 2562 | |
| 2563 | `http://w3.impa.br/~diego/software/luasocket/tcp.html |
| 2564 | <http://w3.impa.br/~diego/software/luasocket/tcp.html>`_ |
| 2565 | |
Thierry FOURNIER | 486f5a0 | 2015-03-16 15:13:03 +0100 | [diff] [blame] | 2566 | .. js:function:: Socket.close(socket) |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 2567 | |
| 2568 | Closes a TCP object. The internal socket used by the object is closed and the |
| 2569 | local address to which the object was bound is made available to other |
| 2570 | applications. No further operations (except for further calls to the close |
Thierry FOURNIER | 486f5a0 | 2015-03-16 15:13:03 +0100 | [diff] [blame] | 2571 | method) are allowed on a closed Socket. |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 2572 | |
Thierry FOURNIER | 486f5a0 | 2015-03-16 15:13:03 +0100 | [diff] [blame] | 2573 | :param class_socket socket: Is the manipulated Socket. |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 2574 | |
| 2575 | Note: It is important to close all used sockets once they are not needed, |
| 2576 | since, in many systems, each socket uses a file descriptor, which are limited |
| 2577 | system resources. Garbage-collected objects are automatically closed before |
| 2578 | destruction, though. |
| 2579 | |
Thierry FOURNIER | a3bc513 | 2015-09-25 21:43:56 +0200 | [diff] [blame] | 2580 | .. js:function:: Socket.connect(socket, address[, port]) |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 2581 | |
| 2582 | Attempts to connect a socket object to a remote host. |
| 2583 | |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 2584 | |
| 2585 | In case of error, the method returns nil followed by a string describing the |
| 2586 | error. In case of success, the method returns 1. |
| 2587 | |
Thierry FOURNIER | 486f5a0 | 2015-03-16 15:13:03 +0100 | [diff] [blame] | 2588 | :param class_socket socket: Is the manipulated Socket. |
Thierry FOURNIER | a3bc513 | 2015-09-25 21:43:56 +0200 | [diff] [blame] | 2589 | :param string address: can be an IP address or a host name. See below for more |
| 2590 | information. |
| 2591 | :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] | 2592 | :returns: 1 or nil. |
| 2593 | |
Bertrand Jacquin | 874a35c | 2018-09-10 21:26:07 +0100 | [diff] [blame] | 2594 | 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] | 2595 | other stream than TCP. The syntax containing a simpleipv4 or ipv6 address is |
| 2596 | the basically expected format. This format requires the port. |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 2597 | |
Thierry FOURNIER | a3bc513 | 2015-09-25 21:43:56 +0200 | [diff] [blame] | 2598 | 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] | 2599 | connect to a socket. Abstract namespaces are supported with the prefix |
Joseph Herlant | 02cedc4 | 2018-11-13 19:45:17 -0800 | [diff] [blame] | 2600 | "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] | 2601 | 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] | 2602 | 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] | 2603 | parameter *port* must not be set. |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 2604 | |
Thierry FOURNIER | 486f5a0 | 2015-03-16 15:13:03 +0100 | [diff] [blame] | 2605 | .. js:function:: Socket.connect_ssl(socket, address, port) |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 2606 | |
| 2607 | Same behavior than the function socket:connect, but uses SSL. |
| 2608 | |
Thierry FOURNIER | 486f5a0 | 2015-03-16 15:13:03 +0100 | [diff] [blame] | 2609 | :param class_socket socket: Is the manipulated Socket. |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 2610 | :returns: 1 or nil. |
| 2611 | |
Thierry FOURNIER | 486f5a0 | 2015-03-16 15:13:03 +0100 | [diff] [blame] | 2612 | .. js:function:: Socket.getpeername(socket) |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 2613 | |
| 2614 | Returns information about the remote side of a connected client object. |
| 2615 | |
| 2616 | Returns a string with the IP address of the peer, followed by the port number |
| 2617 | that peer is using for the connection. In case of error, the method returns |
| 2618 | nil. |
| 2619 | |
Thierry FOURNIER | 486f5a0 | 2015-03-16 15:13:03 +0100 | [diff] [blame] | 2620 | :param class_socket socket: Is the manipulated Socket. |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 2621 | :returns: a string containing the server information. |
| 2622 | |
Thierry FOURNIER | 486f5a0 | 2015-03-16 15:13:03 +0100 | [diff] [blame] | 2623 | .. js:function:: Socket.getsockname(socket) |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 2624 | |
| 2625 | Returns the local address information associated to the object. |
| 2626 | |
| 2627 | The method returns a string with local IP address and a number with the port. |
| 2628 | In case of error, the method returns nil. |
| 2629 | |
Thierry FOURNIER | 486f5a0 | 2015-03-16 15:13:03 +0100 | [diff] [blame] | 2630 | :param class_socket socket: Is the manipulated Socket. |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 2631 | :returns: a string containing the client information. |
| 2632 | |
Thierry FOURNIER | 486f5a0 | 2015-03-16 15:13:03 +0100 | [diff] [blame] | 2633 | .. js:function:: Socket.receive(socket, [pattern [, prefix]]) |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 2634 | |
| 2635 | Reads data from a client object, according to the specified read pattern. |
| 2636 | Patterns follow the Lua file I/O format, and the difference in performance |
| 2637 | between all patterns is negligible. |
| 2638 | |
Thierry FOURNIER | 486f5a0 | 2015-03-16 15:13:03 +0100 | [diff] [blame] | 2639 | :param class_socket socket: Is the manipulated Socket. |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 2640 | :param string|integer pattern: Describe what is required (see below). |
| 2641 | :param string prefix: A string which will be prefix the returned data. |
| 2642 | :returns: a string containing the required data or nil. |
| 2643 | |
| 2644 | Pattern can be any of the following: |
| 2645 | |
| 2646 | * **`*a`**: reads from the socket until the connection is closed. No |
| 2647 | end-of-line translation is performed; |
| 2648 | |
Thierry FOURNIER | 486f5a0 | 2015-03-16 15:13:03 +0100 | [diff] [blame] | 2649 | * **`*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] | 2650 | LF character (ASCII 10), optionally preceded by a CR character |
| 2651 | (ASCII 13). The CR and LF characters are not included in the |
| 2652 | returned line. In fact, all CR characters are ignored by the |
| 2653 | pattern. This is the default pattern. |
| 2654 | |
| 2655 | * **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] | 2656 | Socket. Prefix is an optional string to be concatenated to the |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 2657 | beginning of any received data before return. |
| 2658 | |
Thierry FOURNIER | a3bc513 | 2015-09-25 21:43:56 +0200 | [diff] [blame] | 2659 | * **empty**: If the pattern is left empty, the default option is `*l`. |
| 2660 | |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 2661 | If successful, the method returns the received pattern. In case of error, the |
| 2662 | method returns nil followed by an error message which can be the string |
| 2663 | 'closed' in case the connection was closed before the transmission was |
| 2664 | completed or the string 'timeout' in case there was a timeout during the |
| 2665 | operation. Also, after the error message, the function returns the partial |
| 2666 | result of the transmission. |
| 2667 | |
| 2668 | Important note: This function was changed severely. It used to support |
| 2669 | multiple patterns (but I have never seen this feature used) and now it |
| 2670 | doesn't anymore. Partial results used to be returned in the same way as |
| 2671 | successful results. This last feature violated the idea that all functions |
| 2672 | should return nil on error. Thus it was changed too. |
| 2673 | |
Thierry FOURNIER | 486f5a0 | 2015-03-16 15:13:03 +0100 | [diff] [blame] | 2674 | .. js:function:: Socket.send(socket, data [, start [, end ]]) |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 2675 | |
| 2676 | Sends data through client object. |
| 2677 | |
Thierry FOURNIER | 486f5a0 | 2015-03-16 15:13:03 +0100 | [diff] [blame] | 2678 | :param class_socket socket: Is the manipulated Socket. |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 2679 | :param string data: The data that will be sent. |
| 2680 | :param integer start: The start position in the buffer of the data which will |
| 2681 | be sent. |
| 2682 | :param integer end: The end position in the buffer of the data which will |
| 2683 | be sent. |
| 2684 | :returns: see below. |
| 2685 | |
| 2686 | Data is the string to be sent. The optional arguments i and j work exactly |
| 2687 | like the standard string.sub Lua function to allow the selection of a |
| 2688 | substring to be sent. |
| 2689 | |
| 2690 | If successful, the method returns the index of the last byte within [start, |
| 2691 | end] that has been sent. Notice that, if start is 1 or absent, this is |
| 2692 | effectively the total number of bytes sent. In case of error, the method |
| 2693 | returns nil, followed by an error message, followed by the index of the last |
| 2694 | byte within [start, end] that has been sent. You might want to try again from |
| 2695 | the byte following that. The error message can be 'closed' in case the |
| 2696 | connection was closed before the transmission was completed or the string |
| 2697 | 'timeout' in case there was a timeout during the operation. |
| 2698 | |
| 2699 | Note: Output is not buffered. For small strings, it is always better to |
| 2700 | concatenate them in Lua (with the '..' operator) and send the result in one |
| 2701 | call instead of calling the method several times. |
| 2702 | |
Thierry FOURNIER | 486f5a0 | 2015-03-16 15:13:03 +0100 | [diff] [blame] | 2703 | .. js:function:: Socket.setoption(socket, option [, value]) |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 2704 | |
| 2705 | Just implemented for compatibility, this cal does nothing. |
| 2706 | |
Thierry FOURNIER | 486f5a0 | 2015-03-16 15:13:03 +0100 | [diff] [blame] | 2707 | .. js:function:: Socket.settimeout(socket, value [, mode]) |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 2708 | |
| 2709 | Changes the timeout values for the object. All I/O operations are blocking. |
| 2710 | That is, any call to the methods send, receive, and accept will block |
| 2711 | indefinitely, until the operation completes. The settimeout method defines a |
| 2712 | limit on the amount of time the I/O methods can block. When a timeout time |
| 2713 | has elapsed, the affected methods give up and fail with an error code. |
| 2714 | |
| 2715 | The amount of time to wait is specified as the value parameter, in seconds. |
| 2716 | |
Mark Lakes | 56cc125 | 2018-03-27 09:48:06 +0200 | [diff] [blame] | 2717 | The timeout modes are not implemented, the only settable timeout is the |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 2718 | inactivity time waiting for complete the internal buffer send or waiting for |
| 2719 | receive data. |
| 2720 | |
Thierry FOURNIER | 486f5a0 | 2015-03-16 15:13:03 +0100 | [diff] [blame] | 2721 | :param class_socket socket: Is the manipulated Socket. |
Bertrand Jacquin | 874a35c | 2018-09-10 21:26:07 +0100 | [diff] [blame] | 2722 | :param float value: The timeout value. Use floating point to specify |
Mark Lakes | 56cc125 | 2018-03-27 09:48:06 +0200 | [diff] [blame] | 2723 | milliseconds. |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 2724 | |
Thierry FOURNIER | 3190427 | 2017-10-25 12:59:51 +0200 | [diff] [blame] | 2725 | .. _regex_class: |
| 2726 | |
| 2727 | Regex class |
| 2728 | =========== |
| 2729 | |
| 2730 | .. js:class:: Regex |
| 2731 | |
| 2732 | This class allows the usage of HAProxy regexes because classic lua doesn't |
| 2733 | provides regexes. This class inherits the HAProxy compilation options, so the |
| 2734 | regexes can be libc regex, pcre regex or pcre JIT regex. |
| 2735 | |
| 2736 | The expression matching number is limited to 20 per regex. The only available |
| 2737 | option is case sensitive. |
| 2738 | |
| 2739 | Because regexes compilation is a heavy process, it is better to define all |
| 2740 | your regexes in the **body context** and use it during the runtime. |
| 2741 | |
| 2742 | .. code-block:: lua |
| 2743 | |
| 2744 | -- Create the regex |
| 2745 | st, regex = Regex.new("needle (..) (...)", true); |
| 2746 | |
| 2747 | -- Check compilation errors |
| 2748 | if st == false then |
| 2749 | print "error: " .. regex |
| 2750 | end |
| 2751 | |
| 2752 | -- Match the regexes |
| 2753 | print(regex:exec("Looking for a needle in the haystack")) -- true |
| 2754 | print(regex:exec("Lokking for a cat in the haystack")) -- false |
| 2755 | |
| 2756 | -- Extract words |
| 2757 | st, list = regex:match("Looking for a needle in the haystack") |
| 2758 | print(st) -- true |
| 2759 | print(list[1]) -- needle in the |
| 2760 | print(list[2]) -- in |
| 2761 | print(list[3]) -- the |
| 2762 | |
| 2763 | .. js:function:: Regex.new(regex, case_sensitive) |
| 2764 | |
| 2765 | Create and compile a regex. |
| 2766 | |
| 2767 | :param string regex: The regular expression according with the libc or pcre |
| 2768 | standard |
| 2769 | :param boolean case_sensitive: Match is case sensitive or not. |
| 2770 | :returns: boolean status and :ref:`regex_class` or string containing fail reason. |
| 2771 | |
| 2772 | .. js:function:: Regex.exec(regex, str) |
| 2773 | |
| 2774 | Execute the regex. |
| 2775 | |
| 2776 | :param class_regex regex: A :ref:`regex_class` object. |
| 2777 | :param string str: The input string will be compared with the compiled regex. |
| 2778 | :returns: a boolean status according with the match result. |
| 2779 | |
| 2780 | .. js:function:: Regex.match(regex, str) |
| 2781 | |
| 2782 | Execute the regex and return matched expressions. |
| 2783 | |
| 2784 | :param class_map map: A :ref:`regex_class` object. |
| 2785 | :param string str: The input string will be compared with the compiled regex. |
| 2786 | :returns: a boolean status according with the match result, and |
| 2787 | a table containing all the string matched in order of declaration. |
| 2788 | |
Thierry FOURNIER | dc59500 | 2015-12-21 11:13:52 +0100 | [diff] [blame] | 2789 | .. _map_class: |
| 2790 | |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 2791 | Map class |
| 2792 | ========= |
| 2793 | |
| 2794 | .. js:class:: Map |
| 2795 | |
Aurelien DARRAGON | 53901f4 | 2022-10-13 19:49:42 +0200 | [diff] [blame] | 2796 | This class permits to do some lookups in HAProxy maps. The declared maps can |
Bertrand Jacquin | 874a35c | 2018-09-10 21:26:07 +0100 | [diff] [blame] | 2797 | be modified during the runtime through the HAProxy management socket. |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 2798 | |
| 2799 | .. code-block:: lua |
| 2800 | |
Thierry FOURNIER | dc59500 | 2015-12-21 11:13:52 +0100 | [diff] [blame] | 2801 | default = "usa" |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 2802 | |
Thierry FOURNIER | dc59500 | 2015-12-21 11:13:52 +0100 | [diff] [blame] | 2803 | -- Create and load map |
Thierry FOURNIER | 4dc7197 | 2017-01-28 08:33:08 +0100 | [diff] [blame] | 2804 | geo = Map.new("geo.map", Map._ip); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 2805 | |
Thierry FOURNIER | dc59500 | 2015-12-21 11:13:52 +0100 | [diff] [blame] | 2806 | -- Create new fetch that returns the user country |
| 2807 | core.register_fetches("country", function(txn) |
| 2808 | local src; |
| 2809 | local loc; |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 2810 | |
Thierry FOURNIER | dc59500 | 2015-12-21 11:13:52 +0100 | [diff] [blame] | 2811 | src = txn.f:fhdr("x-forwarded-for"); |
| 2812 | if (src == nil) then |
| 2813 | src = txn.f:src() |
| 2814 | if (src == nil) then |
| 2815 | return default; |
| 2816 | end |
| 2817 | end |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 2818 | |
Thierry FOURNIER | dc59500 | 2015-12-21 11:13:52 +0100 | [diff] [blame] | 2819 | -- Perform lookup |
| 2820 | loc = geo:lookup(src); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 2821 | |
Thierry FOURNIER | dc59500 | 2015-12-21 11:13:52 +0100 | [diff] [blame] | 2822 | if (loc == nil) then |
| 2823 | return default; |
| 2824 | end |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 2825 | |
Thierry FOURNIER | dc59500 | 2015-12-21 11:13:52 +0100 | [diff] [blame] | 2826 | return loc; |
| 2827 | end); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 2828 | |
Thierry FOURNIER | 4dc7197 | 2017-01-28 08:33:08 +0100 | [diff] [blame] | 2829 | .. js:attribute:: Map._int |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 2830 | |
| 2831 | See the HAProxy configuration.txt file, chapter "Using ACLs and fetching |
Ilya Shipitsin | 2075ca8 | 2020-03-06 23:22:22 +0500 | [diff] [blame] | 2832 | samples" and subchapter "ACL basics" to understand this pattern matching |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 2833 | method. |
| 2834 | |
Thierry FOURNIER | 4dc7197 | 2017-01-28 08:33:08 +0100 | [diff] [blame] | 2835 | Note that :js:attr:`Map.int` is also available for compatibility. |
| 2836 | |
| 2837 | .. js:attribute:: Map._ip |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 2838 | |
| 2839 | See the HAProxy configuration.txt file, chapter "Using ACLs and fetching |
Ilya Shipitsin | 2075ca8 | 2020-03-06 23:22:22 +0500 | [diff] [blame] | 2840 | samples" and subchapter "ACL basics" to understand this pattern matching |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 2841 | method. |
| 2842 | |
Thierry FOURNIER | 4dc7197 | 2017-01-28 08:33:08 +0100 | [diff] [blame] | 2843 | Note that :js:attr:`Map.ip` is also available for compatibility. |
| 2844 | |
| 2845 | .. js:attribute:: Map._str |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 2846 | |
| 2847 | See the HAProxy configuration.txt file, chapter "Using ACLs and fetching |
Ilya Shipitsin | 2075ca8 | 2020-03-06 23:22:22 +0500 | [diff] [blame] | 2848 | samples" and subchapter "ACL basics" to understand this pattern matching |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 2849 | method. |
| 2850 | |
Thierry FOURNIER | 4dc7197 | 2017-01-28 08:33:08 +0100 | [diff] [blame] | 2851 | Note that :js:attr:`Map.str` is also available for compatibility. |
| 2852 | |
| 2853 | .. js:attribute:: Map._beg |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 2854 | |
| 2855 | See the HAProxy configuration.txt file, chapter "Using ACLs and fetching |
Ilya Shipitsin | 2075ca8 | 2020-03-06 23:22:22 +0500 | [diff] [blame] | 2856 | samples" and subchapter "ACL basics" to understand this pattern matching |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 2857 | method. |
| 2858 | |
Thierry FOURNIER | 4dc7197 | 2017-01-28 08:33:08 +0100 | [diff] [blame] | 2859 | Note that :js:attr:`Map.beg` is also available for compatibility. |
| 2860 | |
| 2861 | .. js:attribute:: Map._sub |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 2862 | |
| 2863 | See the HAProxy configuration.txt file, chapter "Using ACLs and fetching |
Ilya Shipitsin | 2075ca8 | 2020-03-06 23:22:22 +0500 | [diff] [blame] | 2864 | samples" and subchapter "ACL basics" to understand this pattern matching |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 2865 | method. |
| 2866 | |
Thierry FOURNIER | 4dc7197 | 2017-01-28 08:33:08 +0100 | [diff] [blame] | 2867 | Note that :js:attr:`Map.sub` is also available for compatibility. |
| 2868 | |
| 2869 | .. js:attribute:: Map._dir |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 2870 | |
| 2871 | See the HAProxy configuration.txt file, chapter "Using ACLs and fetching |
Ilya Shipitsin | 2075ca8 | 2020-03-06 23:22:22 +0500 | [diff] [blame] | 2872 | samples" and subchapter "ACL basics" to understand this pattern matching |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 2873 | method. |
| 2874 | |
Thierry FOURNIER | 4dc7197 | 2017-01-28 08:33:08 +0100 | [diff] [blame] | 2875 | Note that :js:attr:`Map.dir` is also available for compatibility. |
| 2876 | |
| 2877 | .. js:attribute:: Map._dom |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 2878 | |
| 2879 | See the HAProxy configuration.txt file, chapter "Using ACLs and fetching |
Ilya Shipitsin | 2075ca8 | 2020-03-06 23:22:22 +0500 | [diff] [blame] | 2880 | samples" and subchapter "ACL basics" to understand this pattern matching |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 2881 | method. |
| 2882 | |
Thierry FOURNIER | 4dc7197 | 2017-01-28 08:33:08 +0100 | [diff] [blame] | 2883 | Note that :js:attr:`Map.dom` is also available for compatibility. |
| 2884 | |
| 2885 | .. js:attribute:: Map._end |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 2886 | |
| 2887 | See the HAProxy configuration.txt file, chapter "Using ACLs and fetching |
Ilya Shipitsin | 2075ca8 | 2020-03-06 23:22:22 +0500 | [diff] [blame] | 2888 | samples" and subchapter "ACL basics" to understand this pattern matching |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 2889 | method. |
| 2890 | |
Thierry FOURNIER | 4dc7197 | 2017-01-28 08:33:08 +0100 | [diff] [blame] | 2891 | .. js:attribute:: Map._reg |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 2892 | |
| 2893 | See the HAProxy configuration.txt file, chapter "Using ACLs and fetching |
Ilya Shipitsin | 2075ca8 | 2020-03-06 23:22:22 +0500 | [diff] [blame] | 2894 | samples" and subchapter "ACL basics" to understand this pattern matching |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 2895 | method. |
| 2896 | |
Thierry FOURNIER | 4dc7197 | 2017-01-28 08:33:08 +0100 | [diff] [blame] | 2897 | Note that :js:attr:`Map.reg` is also available for compatibility. |
| 2898 | |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 2899 | |
| 2900 | .. js:function:: Map.new(file, method) |
| 2901 | |
| 2902 | Creates and load a map. |
| 2903 | |
| 2904 | :param string file: Is the file containing the map. |
| 2905 | :param integer method: Is the map pattern matching method. See the attributes |
| 2906 | of the Map class. |
| 2907 | :returns: a class Map object. |
Thierry FOURNIER | 4dc7197 | 2017-01-28 08:33:08 +0100 | [diff] [blame] | 2908 | :see: The Map attributes: :js:attr:`Map._int`, :js:attr:`Map._ip`, |
| 2909 | :js:attr:`Map._str`, :js:attr:`Map._beg`, :js:attr:`Map._sub`, |
| 2910 | :js:attr:`Map._dir`, :js:attr:`Map._dom`, :js:attr:`Map._end` and |
| 2911 | :js:attr:`Map._reg`. |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 2912 | |
| 2913 | .. js:function:: Map.lookup(map, str) |
| 2914 | |
| 2915 | Perform a lookup in a map. |
| 2916 | |
| 2917 | :param class_map map: Is the class Map object. |
| 2918 | :param string str: Is the string used as key. |
| 2919 | :returns: a string containing the result or nil if no match. |
| 2920 | |
| 2921 | .. js:function:: Map.slookup(map, str) |
| 2922 | |
| 2923 | Perform a lookup in a map. |
| 2924 | |
| 2925 | :param class_map map: Is the class Map object. |
| 2926 | :param string str: Is the string used as key. |
| 2927 | :returns: a string containing the result or empty string if no match. |
| 2928 | |
Thierry FOURNIER | dc59500 | 2015-12-21 11:13:52 +0100 | [diff] [blame] | 2929 | .. _applethttp_class: |
| 2930 | |
Thierry FOURNIER | a3bc513 | 2015-09-25 21:43:56 +0200 | [diff] [blame] | 2931 | AppletHTTP class |
Thierry FOURNIER | dc59500 | 2015-12-21 11:13:52 +0100 | [diff] [blame] | 2932 | ================ |
Thierry FOURNIER | a3bc513 | 2015-09-25 21:43:56 +0200 | [diff] [blame] | 2933 | |
| 2934 | .. js:class:: AppletHTTP |
| 2935 | |
| 2936 | This class is used with applets that requires the 'http' mode. The http applet |
| 2937 | can be registered with the *core.register_service()* function. They are used |
| 2938 | for processing an http request like a server in back of HAProxy. |
| 2939 | |
| 2940 | This is an hello world sample code: |
| 2941 | |
| 2942 | .. code-block:: lua |
Thierry FOURNIER | dc59500 | 2015-12-21 11:13:52 +0100 | [diff] [blame] | 2943 | |
Pieter Baauw | 4d7f766 | 2015-11-08 16:38:08 +0100 | [diff] [blame] | 2944 | core.register_service("hello-world", "http", function(applet) |
Thierry FOURNIER | a3bc513 | 2015-09-25 21:43:56 +0200 | [diff] [blame] | 2945 | local response = "Hello World !" |
| 2946 | applet:set_status(200) |
Pieter Baauw | 2dcb9bc | 2015-10-01 22:47:12 +0200 | [diff] [blame] | 2947 | applet:add_header("content-length", string.len(response)) |
Thierry FOURNIER | a3bc513 | 2015-09-25 21:43:56 +0200 | [diff] [blame] | 2948 | applet:add_header("content-type", "text/plain") |
Pieter Baauw | 2dcb9bc | 2015-10-01 22:47:12 +0200 | [diff] [blame] | 2949 | applet:start_response() |
Thierry FOURNIER | a3bc513 | 2015-09-25 21:43:56 +0200 | [diff] [blame] | 2950 | applet:send(response) |
| 2951 | end) |
| 2952 | |
Thierry FOURNIER | dc59500 | 2015-12-21 11:13:52 +0100 | [diff] [blame] | 2953 | .. js:attribute:: AppletHTTP.c |
| 2954 | |
| 2955 | :returns: A :ref:`converters_class` |
| 2956 | |
| 2957 | This attribute contains a Converters class object. |
| 2958 | |
| 2959 | .. js:attribute:: AppletHTTP.sc |
| 2960 | |
| 2961 | :returns: A :ref:`converters_class` |
| 2962 | |
| 2963 | This attribute contains a Converters class object. The |
Aurelien DARRAGON | 53901f4 | 2022-10-13 19:49:42 +0200 | [diff] [blame] | 2964 | functions of this object always return a string. |
Thierry FOURNIER | dc59500 | 2015-12-21 11:13:52 +0100 | [diff] [blame] | 2965 | |
| 2966 | .. js:attribute:: AppletHTTP.f |
| 2967 | |
| 2968 | :returns: A :ref:`fetches_class` |
| 2969 | |
| 2970 | This attribute contains a Fetches class object. Note that the |
| 2971 | applet execution place cannot access to a valid HAProxy core HTTP |
Ilya Shipitsin | 2075ca8 | 2020-03-06 23:22:22 +0500 | [diff] [blame] | 2972 | transaction, so some sample fetches related to the HTTP dependent |
Thierry FOURNIER | dc59500 | 2015-12-21 11:13:52 +0100 | [diff] [blame] | 2973 | values (hdr, path, ...) are not available. |
| 2974 | |
| 2975 | .. js:attribute:: AppletHTTP.sf |
| 2976 | |
| 2977 | :returns: A :ref:`fetches_class` |
| 2978 | |
| 2979 | This attribute contains a Fetches class object. The functions of |
Aurelien DARRAGON | 53901f4 | 2022-10-13 19:49:42 +0200 | [diff] [blame] | 2980 | this object always return a string. Note that the applet |
Thierry FOURNIER | dc59500 | 2015-12-21 11:13:52 +0100 | [diff] [blame] | 2981 | execution place cannot access to a valid HAProxy core HTTP |
Ilya Shipitsin | 2075ca8 | 2020-03-06 23:22:22 +0500 | [diff] [blame] | 2982 | transaction, so some sample fetches related to the HTTP dependent |
Thierry FOURNIER | dc59500 | 2015-12-21 11:13:52 +0100 | [diff] [blame] | 2983 | values (hdr, path, ...) are not available. |
| 2984 | |
Thierry FOURNIER | e34a78e | 2015-12-25 01:31:35 +0100 | [diff] [blame] | 2985 | .. js:attribute:: AppletHTTP.method |
Thierry FOURNIER | dc59500 | 2015-12-21 11:13:52 +0100 | [diff] [blame] | 2986 | |
| 2987 | :returns: string |
| 2988 | |
| 2989 | The attribute method returns a string containing the HTTP |
| 2990 | method. |
| 2991 | |
| 2992 | .. js:attribute:: AppletHTTP.version |
| 2993 | |
| 2994 | :returns: string |
| 2995 | |
| 2996 | The attribute version, returns a string containing the HTTP |
| 2997 | request version. |
| 2998 | |
| 2999 | .. js:attribute:: AppletHTTP.path |
| 3000 | |
| 3001 | :returns: string |
| 3002 | |
| 3003 | The attribute path returns a string containing the HTTP |
| 3004 | request path. |
| 3005 | |
| 3006 | .. js:attribute:: AppletHTTP.qs |
| 3007 | |
| 3008 | :returns: string |
| 3009 | |
| 3010 | The attribute qs returns a string containing the HTTP |
| 3011 | request query string. |
| 3012 | |
| 3013 | .. js:attribute:: AppletHTTP.length |
| 3014 | |
| 3015 | :returns: integer |
| 3016 | |
| 3017 | The attribute length returns an integer containing the HTTP |
| 3018 | body length. |
Thierry FOURNIER | a3bc513 | 2015-09-25 21:43:56 +0200 | [diff] [blame] | 3019 | |
Thierry FOURNIER | 841475e | 2015-12-11 17:10:09 +0100 | [diff] [blame] | 3020 | .. js:attribute:: AppletHTTP.headers |
| 3021 | |
Patrick Hemmer | c6a1d71 | 2018-05-01 21:30:41 -0400 | [diff] [blame] | 3022 | :returns: table |
Thierry FOURNIER | dc59500 | 2015-12-21 11:13:52 +0100 | [diff] [blame] | 3023 | |
Patrick Hemmer | c6a1d71 | 2018-05-01 21:30:41 -0400 | [diff] [blame] | 3024 | The attribute headers returns a table containing the HTTP |
Thierry FOURNIER | dc59500 | 2015-12-21 11:13:52 +0100 | [diff] [blame] | 3025 | headers. The header names are always in lower case. As the header name can be |
| 3026 | encountered more than once in each request, the value is indexed with 0 as |
Aurelien DARRAGON | 53901f4 | 2022-10-13 19:49:42 +0200 | [diff] [blame] | 3027 | first index value. The table has this form: |
Thierry FOURNIER | dc59500 | 2015-12-21 11:13:52 +0100 | [diff] [blame] | 3028 | |
| 3029 | .. code-block:: lua |
| 3030 | |
| 3031 | AppletHTTP.headers['<header-name>'][<header-index>] = "<header-value>" |
| 3032 | |
| 3033 | AppletHTTP.headers["host"][0] = "www.test.com" |
| 3034 | AppletHTTP.headers["accept"][0] = "audio/basic q=1" |
| 3035 | AppletHTTP.headers["accept"][1] = "audio/*, q=0.2" |
Thierry FOURNIER | e34a78e | 2015-12-25 01:31:35 +0100 | [diff] [blame] | 3036 | AppletHTTP.headers["accept"][2] = "*/*, q=0.1" |
Thierry FOURNIER | dc59500 | 2015-12-21 11:13:52 +0100 | [diff] [blame] | 3037 | .. |
| 3038 | |
Robin H. Johnson | 52f5db2 | 2017-01-01 13:10:52 -0800 | [diff] [blame] | 3039 | .. js:function:: AppletHTTP.set_status(applet, code [, reason]) |
Thierry FOURNIER | a3bc513 | 2015-09-25 21:43:56 +0200 | [diff] [blame] | 3040 | |
| 3041 | This function sets the HTTP status code for the response. The allowed code are |
| 3042 | from 100 to 599. |
| 3043 | |
Thierry FOURNIER | e34a78e | 2015-12-25 01:31:35 +0100 | [diff] [blame] | 3044 | :param class_AppletHTTP applet: An :ref:`applethttp_class` |
Thierry FOURNIER | a3bc513 | 2015-09-25 21:43:56 +0200 | [diff] [blame] | 3045 | :param integer code: the status code returned to the client. |
Robin H. Johnson | 52f5db2 | 2017-01-01 13:10:52 -0800 | [diff] [blame] | 3046 | :param string reason: the status reason returned to the client (optional). |
Thierry FOURNIER | a3bc513 | 2015-09-25 21:43:56 +0200 | [diff] [blame] | 3047 | |
Thierry FOURNIER | e34a78e | 2015-12-25 01:31:35 +0100 | [diff] [blame] | 3048 | .. js:function:: AppletHTTP.add_header(applet, name, value) |
Thierry FOURNIER | a3bc513 | 2015-09-25 21:43:56 +0200 | [diff] [blame] | 3049 | |
Aurelien DARRAGON | 53901f4 | 2022-10-13 19:49:42 +0200 | [diff] [blame] | 3050 | This function adds a header in the response. Duplicated headers are not |
Thierry FOURNIER | a3bc513 | 2015-09-25 21:43:56 +0200 | [diff] [blame] | 3051 | collapsed. The special header *content-length* is used to determinate the |
Aurelien DARRAGON | 53901f4 | 2022-10-13 19:49:42 +0200 | [diff] [blame] | 3052 | response length. If it does not exist, a *transfer-encoding: chunked* is set, and |
Ilya Shipitsin | 2075ca8 | 2020-03-06 23:22:22 +0500 | [diff] [blame] | 3053 | all the write from the function *AppletHTTP:send()* become a chunk. |
Thierry FOURNIER | a3bc513 | 2015-09-25 21:43:56 +0200 | [diff] [blame] | 3054 | |
Thierry FOURNIER | e34a78e | 2015-12-25 01:31:35 +0100 | [diff] [blame] | 3055 | :param class_AppletHTTP applet: An :ref:`applethttp_class` |
Thierry FOURNIER | a3bc513 | 2015-09-25 21:43:56 +0200 | [diff] [blame] | 3056 | :param string name: the header name |
| 3057 | :param string value: the header value |
| 3058 | |
Thierry FOURNIER | e34a78e | 2015-12-25 01:31:35 +0100 | [diff] [blame] | 3059 | .. js:function:: AppletHTTP.start_response(applet) |
Thierry FOURNIER | a3bc513 | 2015-09-25 21:43:56 +0200 | [diff] [blame] | 3060 | |
| 3061 | This function indicates to the HTTP engine that it can process and send the |
| 3062 | response headers. After this called we cannot add headers to the response; We |
| 3063 | cannot use the *AppletHTTP:send()* function if the |
| 3064 | *AppletHTTP:start_response()* is not called. |
| 3065 | |
Thierry FOURNIER | e34a78e | 2015-12-25 01:31:35 +0100 | [diff] [blame] | 3066 | :param class_AppletHTTP applet: An :ref:`applethttp_class` |
| 3067 | |
| 3068 | .. js:function:: AppletHTTP.getline(applet) |
Thierry FOURNIER | a3bc513 | 2015-09-25 21:43:56 +0200 | [diff] [blame] | 3069 | |
| 3070 | This function returns a string containing one line from the http body. If the |
| 3071 | data returned doesn't contains a final '\\n' its assumed than its the last |
| 3072 | available data before the end of stream. |
| 3073 | |
Thierry FOURNIER | e34a78e | 2015-12-25 01:31:35 +0100 | [diff] [blame] | 3074 | :param class_AppletHTTP applet: An :ref:`applethttp_class` |
Thierry FOURNIER | a3bc513 | 2015-09-25 21:43:56 +0200 | [diff] [blame] | 3075 | :returns: a string. The string can be empty if we reach the end of the stream. |
| 3076 | |
Thierry FOURNIER | e34a78e | 2015-12-25 01:31:35 +0100 | [diff] [blame] | 3077 | .. js:function:: AppletHTTP.receive(applet, [size]) |
Thierry FOURNIER | a3bc513 | 2015-09-25 21:43:56 +0200 | [diff] [blame] | 3078 | |
| 3079 | Reads data from the HTTP body, according to the specified read *size*. If the |
| 3080 | *size* is missing, the function tries to read all the content of the stream |
| 3081 | 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] | 3082 | amount of data available. |
Thierry FOURNIER | a3bc513 | 2015-09-25 21:43:56 +0200 | [diff] [blame] | 3083 | |
Thierry FOURNIER | e34a78e | 2015-12-25 01:31:35 +0100 | [diff] [blame] | 3084 | :param class_AppletHTTP applet: An :ref:`applethttp_class` |
Thierry FOURNIER | a3bc513 | 2015-09-25 21:43:56 +0200 | [diff] [blame] | 3085 | :param integer size: the required read size. |
Ilya Shipitsin | 11057a3 | 2020-06-21 21:18:27 +0500 | [diff] [blame] | 3086 | :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] | 3087 | closed. |
| 3088 | |
Thierry FOURNIER | e34a78e | 2015-12-25 01:31:35 +0100 | [diff] [blame] | 3089 | .. js:function:: AppletHTTP.send(applet, msg) |
Thierry FOURNIER | a3bc513 | 2015-09-25 21:43:56 +0200 | [diff] [blame] | 3090 | |
| 3091 | Send the message *msg* on the http request body. |
| 3092 | |
Thierry FOURNIER | e34a78e | 2015-12-25 01:31:35 +0100 | [diff] [blame] | 3093 | :param class_AppletHTTP applet: An :ref:`applethttp_class` |
Thierry FOURNIER | a3bc513 | 2015-09-25 21:43:56 +0200 | [diff] [blame] | 3094 | :param string msg: the message to send. |
| 3095 | |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 3096 | .. js:function:: AppletHTTP.get_priv(applet) |
| 3097 | |
Thierry FOURNIER | 12a865d | 2016-12-14 19:40:37 +0100 | [diff] [blame] | 3098 | Return Lua data stored in the current transaction. If no data are stored, |
| 3099 | it returns a nil value. |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 3100 | |
| 3101 | :param class_AppletHTTP applet: An :ref:`applethttp_class` |
Bertrand Jacquin | 874a35c | 2018-09-10 21:26:07 +0100 | [diff] [blame] | 3102 | :returns: the opaque data previously stored, or nil if nothing is |
| 3103 | available. |
Thierry FOURNIER | 12a865d | 2016-12-14 19:40:37 +0100 | [diff] [blame] | 3104 | :see: :js:func:`AppletHTTP.set_priv` |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 3105 | |
| 3106 | .. js:function:: AppletHTTP.set_priv(applet, data) |
| 3107 | |
Aurelien DARRAGON | 53901f4 | 2022-10-13 19:49:42 +0200 | [diff] [blame] | 3108 | Store any data in the current HAProxy transaction. This action replaces the |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 3109 | old stored data. |
| 3110 | |
| 3111 | :param class_AppletHTTP applet: An :ref:`applethttp_class` |
| 3112 | :param opaque data: The data which is stored in the transaction. |
Thierry FOURNIER | 12a865d | 2016-12-14 19:40:37 +0100 | [diff] [blame] | 3113 | :see: :js:func:`AppletHTTP.get_priv` |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 3114 | |
Tim Duesterhus | 4e172c9 | 2020-05-19 13:49:42 +0200 | [diff] [blame] | 3115 | .. js:function:: AppletHTTP.set_var(applet, var, value[, ifexist]) |
Thierry FOURNIER / OZON.IO | c1edafe | 2016-12-12 16:25:30 +0100 | [diff] [blame] | 3116 | |
| 3117 | Converts a Lua type in a HAProxy type and store it in a variable <var>. |
| 3118 | |
| 3119 | :param class_AppletHTTP applet: An :ref:`applethttp_class` |
| 3120 | :param string var: The variable name according with the HAProxy variable syntax. |
| 3121 | :param type value: The value associated to the variable. The type ca be string or |
| 3122 | integer. |
Aurelien DARRAGON | 53901f4 | 2022-10-13 19:49:42 +0200 | [diff] [blame] | 3123 | :param boolean ifexist: If this parameter is set to true the variable |
Tim Duesterhus | 4e172c9 | 2020-05-19 13:49:42 +0200 | [diff] [blame] | 3124 | will only be set if it was defined elsewhere (i.e. used |
Willy Tarreau | 7978c5c | 2021-09-07 14:24:07 +0200 | [diff] [blame] | 3125 | within the configuration). For global variables (using the |
| 3126 | "proc" scope), they will only be updated and never created. |
Aurelien DARRAGON | 21f7ebb | 2023-03-13 19:49:31 +0100 | [diff] [blame] | 3127 | It is highly recommended to always set this to true. |
Aurelien DARRAGON | 53901f4 | 2022-10-13 19:49:42 +0200 | [diff] [blame] | 3128 | |
Thierry FOURNIER | 12a865d | 2016-12-14 19:40:37 +0100 | [diff] [blame] | 3129 | :see: :js:func:`AppletHTTP.unset_var` |
| 3130 | :see: :js:func:`AppletHTTP.get_var` |
Thierry FOURNIER / OZON.IO | c1edafe | 2016-12-12 16:25:30 +0100 | [diff] [blame] | 3131 | |
| 3132 | .. js:function:: AppletHTTP.unset_var(applet, var) |
| 3133 | |
| 3134 | Unset the variable <var>. |
| 3135 | |
| 3136 | :param class_AppletHTTP applet: An :ref:`applethttp_class` |
| 3137 | :param string var: The variable name according with the HAProxy variable syntax. |
Thierry FOURNIER | 12a865d | 2016-12-14 19:40:37 +0100 | [diff] [blame] | 3138 | :see: :js:func:`AppletHTTP.set_var` |
| 3139 | :see: :js:func:`AppletHTTP.get_var` |
Thierry FOURNIER / OZON.IO | c1edafe | 2016-12-12 16:25:30 +0100 | [diff] [blame] | 3140 | |
| 3141 | .. js:function:: AppletHTTP.get_var(applet, var) |
| 3142 | |
| 3143 | Returns data stored in the variable <var> converter in Lua type. |
| 3144 | |
| 3145 | :param class_AppletHTTP applet: An :ref:`applethttp_class` |
| 3146 | :param string var: The variable name according with the HAProxy variable syntax. |
Thierry FOURNIER | 12a865d | 2016-12-14 19:40:37 +0100 | [diff] [blame] | 3147 | :see: :js:func:`AppletHTTP.set_var` |
| 3148 | :see: :js:func:`AppletHTTP.unset_var` |
Thierry FOURNIER / OZON.IO | c1edafe | 2016-12-12 16:25:30 +0100 | [diff] [blame] | 3149 | |
Thierry FOURNIER | dc59500 | 2015-12-21 11:13:52 +0100 | [diff] [blame] | 3150 | .. _applettcp_class: |
| 3151 | |
Thierry FOURNIER | a3bc513 | 2015-09-25 21:43:56 +0200 | [diff] [blame] | 3152 | AppletTCP class |
| 3153 | =============== |
| 3154 | |
| 3155 | .. js:class:: AppletTCP |
| 3156 | |
| 3157 | This class is used with applets that requires the 'tcp' mode. The tcp applet |
| 3158 | can be registered with the *core.register_service()* function. They are used |
| 3159 | for processing a tcp stream like a server in back of HAProxy. |
| 3160 | |
Thierry FOURNIER | dc59500 | 2015-12-21 11:13:52 +0100 | [diff] [blame] | 3161 | .. js:attribute:: AppletTCP.c |
| 3162 | |
| 3163 | :returns: A :ref:`converters_class` |
| 3164 | |
| 3165 | This attribute contains a Converters class object. |
| 3166 | |
| 3167 | .. js:attribute:: AppletTCP.sc |
| 3168 | |
| 3169 | :returns: A :ref:`converters_class` |
| 3170 | |
| 3171 | This attribute contains a Converters class object. The |
Aurelien DARRAGON | 53901f4 | 2022-10-13 19:49:42 +0200 | [diff] [blame] | 3172 | functions of this object always return a string. |
Thierry FOURNIER | dc59500 | 2015-12-21 11:13:52 +0100 | [diff] [blame] | 3173 | |
| 3174 | .. js:attribute:: AppletTCP.f |
| 3175 | |
| 3176 | :returns: A :ref:`fetches_class` |
| 3177 | |
| 3178 | This attribute contains a Fetches class object. |
| 3179 | |
| 3180 | .. js:attribute:: AppletTCP.sf |
| 3181 | |
| 3182 | :returns: A :ref:`fetches_class` |
| 3183 | |
| 3184 | This attribute contains a Fetches class object. |
| 3185 | |
Thierry FOURNIER | e34a78e | 2015-12-25 01:31:35 +0100 | [diff] [blame] | 3186 | .. js:function:: AppletTCP.getline(applet) |
Thierry FOURNIER | a3bc513 | 2015-09-25 21:43:56 +0200 | [diff] [blame] | 3187 | |
| 3188 | This function returns a string containing one line from the stream. If the |
| 3189 | data returned doesn't contains a final '\\n' its assumed than its the last |
| 3190 | available data before the end of stream. |
| 3191 | |
Thierry FOURNIER | e34a78e | 2015-12-25 01:31:35 +0100 | [diff] [blame] | 3192 | :param class_AppletTCP applet: An :ref:`applettcp_class` |
Thierry FOURNIER | a3bc513 | 2015-09-25 21:43:56 +0200 | [diff] [blame] | 3193 | :returns: a string. The string can be empty if we reach the end of the stream. |
| 3194 | |
Thierry FOURNIER | e34a78e | 2015-12-25 01:31:35 +0100 | [diff] [blame] | 3195 | .. js:function:: AppletTCP.receive(applet, [size]) |
Thierry FOURNIER | a3bc513 | 2015-09-25 21:43:56 +0200 | [diff] [blame] | 3196 | |
| 3197 | Reads data from the TCP stream, according to the specified read *size*. If the |
| 3198 | *size* is missing, the function tries to read all the content of the stream |
| 3199 | until the end. |
| 3200 | |
Thierry FOURNIER | e34a78e | 2015-12-25 01:31:35 +0100 | [diff] [blame] | 3201 | :param class_AppletTCP applet: An :ref:`applettcp_class` |
Thierry FOURNIER | a3bc513 | 2015-09-25 21:43:56 +0200 | [diff] [blame] | 3202 | :param integer size: the required read size. |
Aurelien DARRAGON | 53901f4 | 2022-10-13 19:49:42 +0200 | [diff] [blame] | 3203 | :returns: always return a string, the string can be empty if the connection is |
Thierry FOURNIER | a3bc513 | 2015-09-25 21:43:56 +0200 | [diff] [blame] | 3204 | closed. |
| 3205 | |
Thierry FOURNIER | e34a78e | 2015-12-25 01:31:35 +0100 | [diff] [blame] | 3206 | .. js:function:: AppletTCP.send(appletmsg) |
Thierry FOURNIER | a3bc513 | 2015-09-25 21:43:56 +0200 | [diff] [blame] | 3207 | |
| 3208 | Send the message on the stream. |
| 3209 | |
Thierry FOURNIER | e34a78e | 2015-12-25 01:31:35 +0100 | [diff] [blame] | 3210 | :param class_AppletTCP applet: An :ref:`applettcp_class` |
Thierry FOURNIER | a3bc513 | 2015-09-25 21:43:56 +0200 | [diff] [blame] | 3211 | :param string msg: the message to send. |
| 3212 | |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 3213 | .. js:function:: AppletTCP.get_priv(applet) |
| 3214 | |
Thierry FOURNIER | 12a865d | 2016-12-14 19:40:37 +0100 | [diff] [blame] | 3215 | Return Lua data stored in the current transaction. If no data are stored, |
| 3216 | it returns a nil value. |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 3217 | |
| 3218 | :param class_AppletTCP applet: An :ref:`applettcp_class` |
Bertrand Jacquin | 874a35c | 2018-09-10 21:26:07 +0100 | [diff] [blame] | 3219 | :returns: the opaque data previously stored, or nil if nothing is |
| 3220 | available. |
Thierry FOURNIER | 12a865d | 2016-12-14 19:40:37 +0100 | [diff] [blame] | 3221 | :see: :js:func:`AppletTCP.set_priv` |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 3222 | |
| 3223 | .. js:function:: AppletTCP.set_priv(applet, data) |
| 3224 | |
Aurelien DARRAGON | 53901f4 | 2022-10-13 19:49:42 +0200 | [diff] [blame] | 3225 | Store any data in the current HAProxy transaction. This action replaces the |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 3226 | old stored data. |
| 3227 | |
| 3228 | :param class_AppletTCP applet: An :ref:`applettcp_class` |
| 3229 | :param opaque data: The data which is stored in the transaction. |
Thierry FOURNIER | 12a865d | 2016-12-14 19:40:37 +0100 | [diff] [blame] | 3230 | :see: :js:func:`AppletTCP.get_priv` |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 3231 | |
Tim Duesterhus | 4e172c9 | 2020-05-19 13:49:42 +0200 | [diff] [blame] | 3232 | .. js:function:: AppletTCP.set_var(applet, var, value[, ifexist]) |
Thierry FOURNIER / OZON.IO | c1edafe | 2016-12-12 16:25:30 +0100 | [diff] [blame] | 3233 | |
| 3234 | Converts a Lua type in a HAProxy type and stores it in a variable <var>. |
| 3235 | |
| 3236 | :param class_AppletTCP applet: An :ref:`applettcp_class` |
| 3237 | :param string var: The variable name according with the HAProxy variable syntax. |
| 3238 | :param type value: The value associated to the variable. The type can be string or |
| 3239 | integer. |
Aurelien DARRAGON | 53901f4 | 2022-10-13 19:49:42 +0200 | [diff] [blame] | 3240 | :param boolean ifexist: If this parameter is set to true the variable |
Tim Duesterhus | 4e172c9 | 2020-05-19 13:49:42 +0200 | [diff] [blame] | 3241 | will only be set if it was defined elsewhere (i.e. used |
Willy Tarreau | 7978c5c | 2021-09-07 14:24:07 +0200 | [diff] [blame] | 3242 | within the configuration). For global variables (using the |
| 3243 | "proc" scope), they will only be updated and never created. |
Aurelien DARRAGON | 21f7ebb | 2023-03-13 19:49:31 +0100 | [diff] [blame] | 3244 | It is highly recommended to always set this to true. |
Aurelien DARRAGON | 53901f4 | 2022-10-13 19:49:42 +0200 | [diff] [blame] | 3245 | |
Thierry FOURNIER | 12a865d | 2016-12-14 19:40:37 +0100 | [diff] [blame] | 3246 | :see: :js:func:`AppletTCP.unset_var` |
| 3247 | :see: :js:func:`AppletTCP.get_var` |
Thierry FOURNIER / OZON.IO | c1edafe | 2016-12-12 16:25:30 +0100 | [diff] [blame] | 3248 | |
| 3249 | .. js:function:: AppletTCP.unset_var(applet, var) |
| 3250 | |
| 3251 | Unsets the variable <var>. |
| 3252 | |
| 3253 | :param class_AppletTCP applet: An :ref:`applettcp_class` |
| 3254 | :param string var: The variable name according with the HAProxy variable syntax. |
Thierry FOURNIER | 12a865d | 2016-12-14 19:40:37 +0100 | [diff] [blame] | 3255 | :see: :js:func:`AppletTCP.unset_var` |
| 3256 | :see: :js:func:`AppletTCP.set_var` |
Thierry FOURNIER / OZON.IO | c1edafe | 2016-12-12 16:25:30 +0100 | [diff] [blame] | 3257 | |
| 3258 | .. js:function:: AppletTCP.get_var(applet, var) |
| 3259 | |
| 3260 | Returns data stored in the variable <var> converter in Lua type. |
| 3261 | |
| 3262 | :param class_AppletTCP applet: An :ref:`applettcp_class` |
| 3263 | :param string var: The variable name according with the HAProxy variable syntax. |
Thierry FOURNIER | 12a865d | 2016-12-14 19:40:37 +0100 | [diff] [blame] | 3264 | :see: :js:func:`AppletTCP.unset_var` |
| 3265 | :see: :js:func:`AppletTCP.set_var` |
Thierry FOURNIER / OZON.IO | c1edafe | 2016-12-12 16:25:30 +0100 | [diff] [blame] | 3266 | |
Adis Nezirovic | 8878f8e | 2018-07-13 12:18:33 +0200 | [diff] [blame] | 3267 | StickTable class |
| 3268 | ================ |
| 3269 | |
| 3270 | .. js:class:: StickTable |
| 3271 | |
| 3272 | **context**: task, action, sample-fetch |
| 3273 | |
| 3274 | This class can be used to access the HAProxy stick tables from Lua. |
| 3275 | |
| 3276 | .. js:function:: StickTable.info() |
| 3277 | |
| 3278 | Returns stick table attributes as a Lua table. See HAProxy documentation for |
Ilya Shipitsin | 2272d8a | 2020-12-21 01:22:40 +0500 | [diff] [blame] | 3279 | "stick-table" for canonical info, or check out example below. |
Adis Nezirovic | 8878f8e | 2018-07-13 12:18:33 +0200 | [diff] [blame] | 3280 | |
| 3281 | :returns: Lua table |
| 3282 | |
| 3283 | Assume our table has IPv4 key and gpc0 and conn_rate "columns": |
| 3284 | |
| 3285 | .. code-block:: lua |
| 3286 | |
| 3287 | { |
| 3288 | expire=<int>, # Value in ms |
| 3289 | size=<int>, # Maximum table size |
| 3290 | used=<int>, # Actual number of entries in table |
| 3291 | data={ # Data columns, with types as key, and periods as values |
| 3292 | (-1 if type is not rate counter) |
| 3293 | conn_rate=<int>, |
| 3294 | gpc0=-1 |
| 3295 | }, |
| 3296 | length=<int>, # max string length for string table keys, key length |
| 3297 | # otherwise |
| 3298 | nopurge=<boolean>, # purge oldest entries when table is full |
| 3299 | type="ip" # can be "ip", "ipv6", "integer", "string", "binary" |
| 3300 | } |
| 3301 | |
| 3302 | .. js:function:: StickTable.lookup(key) |
| 3303 | |
| 3304 | Returns stick table entry for given <key> |
| 3305 | |
| 3306 | :param string key: Stick table key (IP addresses and strings are supported) |
| 3307 | :returns: Lua table |
| 3308 | |
| 3309 | .. js:function:: StickTable.dump([filter]) |
| 3310 | |
| 3311 | Returns all entries in stick table. An optional filter can be used |
| 3312 | to extract entries with specific data values. Filter is a table with valid |
| 3313 | comparison operators as keys followed by data type name and value pairs. |
| 3314 | Check out the HAProxy docs for "show table" for more details. For the |
| 3315 | reference, the supported operators are: |
Aurelien DARRAGON | 21f7ebb | 2023-03-13 19:49:31 +0100 | [diff] [blame] | 3316 | |
Adis Nezirovic | 8878f8e | 2018-07-13 12:18:33 +0200 | [diff] [blame] | 3317 | "eq", "ne", "le", "lt", "ge", "gt" |
| 3318 | |
| 3319 | For large tables, execution of this function can take a long time (for |
| 3320 | HAProxy standards). That's also true when filter is used, so take care and |
| 3321 | measure the impact. |
| 3322 | |
| 3323 | :param table filter: Stick table filter |
| 3324 | :returns: Stick table entries (table) |
| 3325 | |
| 3326 | See below for example filter, which contains 4 entries (or comparisons). |
| 3327 | (Maximum number of filter entries is 4, defined in the source code) |
| 3328 | |
| 3329 | .. code-block:: lua |
| 3330 | |
| 3331 | local filter = { |
| 3332 | {"gpc0", "gt", 30}, {"gpc1", "gt", 20}}, {"conn_rate", "le", 10} |
| 3333 | } |
| 3334 | |
Christopher Faulet | 0f3c890 | 2020-01-31 18:57:12 +0100 | [diff] [blame] | 3335 | .. _action_class: |
| 3336 | |
| 3337 | Action class |
| 3338 | ============= |
| 3339 | |
| 3340 | .. js:class:: Act |
| 3341 | |
| 3342 | **context**: action |
| 3343 | |
| 3344 | This class contains all return codes an action may return. It is the lua |
| 3345 | equivalent to HAProxy "ACT_RET_*" code. |
| 3346 | |
| 3347 | .. code-block:: lua |
| 3348 | |
| 3349 | core.register_action("deny", { "http-req" }, function (txn) |
| 3350 | return act.DENY |
| 3351 | end) |
| 3352 | .. |
| 3353 | .. js:attribute:: act.CONTINUE |
| 3354 | |
| 3355 | This attribute is an integer (0). It instructs HAProxy to continue the current |
| 3356 | ruleset processing on the message. It is the default return code for a lua |
| 3357 | action. |
| 3358 | |
| 3359 | :returns: integer |
| 3360 | |
| 3361 | .. js:attribute:: act.STOP |
| 3362 | |
| 3363 | This attribute is an integer (1). It instructs HAProxy to stop the current |
| 3364 | ruleset processing on the message. |
| 3365 | |
| 3366 | .. js:attribute:: act.YIELD |
| 3367 | |
| 3368 | This attribute is an integer (2). It instructs HAProxy to temporarily pause |
| 3369 | the message processing. It will be resumed later on the same rule. The |
| 3370 | corresponding lua script is re-executed for the start. |
| 3371 | |
| 3372 | .. js:attribute:: act.ERROR |
| 3373 | |
| 3374 | This attribute is an integer (3). It triggers an internal errors The message |
| 3375 | processing is stopped and the transaction is terminated. For HTTP streams, an |
| 3376 | HTTP 500 error is returned to the client. |
| 3377 | |
| 3378 | :returns: integer |
| 3379 | |
| 3380 | .. js:attribute:: act.DONE |
| 3381 | |
| 3382 | This attribute is an integer (4). It instructs HAProxy to stop the message |
| 3383 | processing. |
| 3384 | |
| 3385 | :returns: integer |
| 3386 | |
| 3387 | .. js:attribute:: act.DENY |
| 3388 | |
| 3389 | This attribute is an integer (5). It denies the current message. The message |
| 3390 | processing is stopped and the transaction is terminated. For HTTP streams, an |
| 3391 | HTTP 403 error is returned to the client if the deny is returned during the |
Aurelien DARRAGON | 53901f4 | 2022-10-13 19:49:42 +0200 | [diff] [blame] | 3392 | request analysis. During the response analysis, a HTTP 502 error is returned |
Christopher Faulet | 0f3c890 | 2020-01-31 18:57:12 +0100 | [diff] [blame] | 3393 | and the server response is discarded. |
| 3394 | |
| 3395 | :returns: integer |
| 3396 | |
| 3397 | .. js:attribute:: act.ABORT |
| 3398 | |
| 3399 | This attribute is an integer (6). It aborts the current message. The message |
| 3400 | processing is stopped and the transaction is terminated. For HTTP streams, |
Willy Tarreau | 714f345 | 2021-05-09 06:47:26 +0200 | [diff] [blame] | 3401 | 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] | 3402 | actions point of view, when this code is used, the transaction is terminated |
| 3403 | with no reply. |
| 3404 | |
| 3405 | :returns: integer |
| 3406 | |
| 3407 | .. js:attribute:: act.INVALID |
| 3408 | |
| 3409 | This attribute is an integer (7). It triggers an internal errors. The message |
| 3410 | processing is stopped and the transaction is terminated. For HTTP streams, an |
| 3411 | HTTP 400 error is returned to the client if the error is returned during the |
Aurelien DARRAGON | 53901f4 | 2022-10-13 19:49:42 +0200 | [diff] [blame] | 3412 | request analysis. During the response analysis, a HTTP 502 error is returned |
Christopher Faulet | 0f3c890 | 2020-01-31 18:57:12 +0100 | [diff] [blame] | 3413 | and the server response is discarded. |
| 3414 | |
| 3415 | :returns: integer |
Adis Nezirovic | 8878f8e | 2018-07-13 12:18:33 +0200 | [diff] [blame] | 3416 | |
Christopher Faulet | 2c2c2e3 | 2020-01-31 19:07:52 +0100 | [diff] [blame] | 3417 | .. js:function:: act:wake_time(milliseconds) |
| 3418 | |
| 3419 | **context**: action |
| 3420 | |
| 3421 | Set the script pause timeout to the specified time, defined in |
| 3422 | milliseconds. |
| 3423 | |
| 3424 | :param integer milliseconds: the required milliseconds. |
| 3425 | |
| 3426 | This function may be used when a lua action returns `act.YIELD`, to force its |
| 3427 | wake-up at most after the specified number of milliseconds. |
| 3428 | |
Christopher Faulet | 5a2c661 | 2021-08-15 20:35:25 +0200 | [diff] [blame] | 3429 | .. _filter_class: |
| 3430 | |
| 3431 | Filter class |
| 3432 | ============= |
| 3433 | |
| 3434 | .. js:class:: filter |
| 3435 | |
| 3436 | **context**: filter |
| 3437 | |
| 3438 | This class contains return codes some filter callback functions may return. It |
| 3439 | also contains configuration flags and some helper functions. To understand how |
| 3440 | the filter API works, see `doc/internal/filters.txt` documentation. |
| 3441 | |
| 3442 | .. js:attribute:: filter.CONTINUE |
| 3443 | |
| 3444 | This attribute is an integer (1). It may be returned by some filter callback |
| 3445 | functions to instruct this filtering step is finished for this filter. |
| 3446 | |
| 3447 | .. js:attribute:: filter.WAIT |
| 3448 | |
| 3449 | This attribute is an integer (0). It may be returned by some filter callback |
| 3450 | functions to instruct the filtering must be paused, waiting for more data or |
| 3451 | for an external event depending on this filter. |
| 3452 | |
| 3453 | .. js:attribute:: filter.ERROR |
| 3454 | |
| 3455 | This attribute is an integer (-1). It may be returned by some filter callback |
| 3456 | functions to trigger an error. |
| 3457 | |
| 3458 | .. js:attribute:: filter.FLT_CFG_FL_HTX |
| 3459 | |
| 3460 | This attribute is a flag corresponding to the filter flag FLT_CFG_FL_HTX. When |
| 3461 | it is set for a filter, it means the filter is able to filter HTTP streams. |
| 3462 | |
| 3463 | .. js:function:: filter.register_data_filter(chn) |
| 3464 | |
| 3465 | **context**: filter |
| 3466 | |
| 3467 | Enable the data filtering on the channel **chn** for the current filter. It |
Ilya Shipitsin | ff0f278 | 2021-08-22 22:18:07 +0500 | [diff] [blame] | 3468 | may be called at any time from any callback functions proceeding the data |
Christopher Faulet | 5a2c661 | 2021-08-15 20:35:25 +0200 | [diff] [blame] | 3469 | analysis. |
| 3470 | |
| 3471 | :param class_Channel chn: A :ref:`channel_class`. |
| 3472 | |
| 3473 | .. js:function:: filter.unregister_data_filter(chn) |
| 3474 | |
| 3475 | **context**: filter |
| 3476 | |
| 3477 | Disable the data filtering on the channel **chn** for the current filter. It |
| 3478 | may be called at any time from any callback functions. |
| 3479 | |
| 3480 | :param class_Channel chn: A :ref:`channel_class`. |
| 3481 | |
| 3482 | .. js:function:: filter.wake_time(milliseconds) |
| 3483 | |
| 3484 | **context**: filter |
| 3485 | |
| 3486 | Set the script pause timeout to the specified time, defined in |
| 3487 | milliseconds. |
| 3488 | |
| 3489 | :param integer milliseconds: the required milliseconds. |
| 3490 | |
| 3491 | This function may be used from any lua filter callback function to force its |
| 3492 | wake-up at most after the specified number of milliseconds. Especially, when |
| 3493 | `filter.CONTINUE` is returned. |
| 3494 | |
| 3495 | |
| 3496 | A filters is declared using :js:func:`core.register_filter()` function. The |
| 3497 | provided class will be used to instantiate filters. It may define following |
| 3498 | attributes: |
| 3499 | |
| 3500 | * id: The filter identifier. It is a string that identifies the filter and is |
| 3501 | optional. |
| 3502 | |
| 3503 | * flags: The filter flags. Only :js:attr:`filter.FLT_CFG_FL_HTX` may be set for now. |
| 3504 | |
| 3505 | Such filter class must also define all required callback functions in the |
| 3506 | following list. Note that :js:func:`Filter.new()` must be defined otherwise the |
Ilya Shipitsin | ff0f278 | 2021-08-22 22:18:07 +0500 | [diff] [blame] | 3507 | filter is ignored. Others are optional. |
Christopher Faulet | 5a2c661 | 2021-08-15 20:35:25 +0200 | [diff] [blame] | 3508 | |
| 3509 | * .. js:function:: FILTER.new() |
| 3510 | |
| 3511 | Called to instantiate a new filter. This function must be defined. |
| 3512 | |
| 3513 | :returns: a Lua object that will be used as filter instance for the current |
| 3514 | stream. |
| 3515 | |
| 3516 | * .. js:function:: FILTER.start_analyze(flt, txn, chn) |
| 3517 | |
| 3518 | Called when the analysis starts on the channel **chn**. |
| 3519 | |
| 3520 | * .. js:function:: FILTER.end_analyze(flt, txn, chn) |
| 3521 | |
| 3522 | Called when the analysis ends on the channel **chn**. |
| 3523 | |
| 3524 | * .. js:function:: FILTER.http_headers(flt, txn, http_msg) |
| 3525 | |
| 3526 | Called just before the HTTP payload analysis and after any processing on the |
| 3527 | HTTP message **http_msg**. This callback functions is only called for HTTP |
| 3528 | streams. |
| 3529 | |
| 3530 | * .. js:function:: FILTER.http_payload(flt, txn, http_msg) |
| 3531 | |
| 3532 | Called during the HTTP payload analysis on the HTTP message **http_msg**. This |
| 3533 | callback functions is only called for HTTP streams. |
| 3534 | |
| 3535 | * .. js:function:: FILTER.http_end(flt, txn, http_msg) |
| 3536 | |
| 3537 | Called after the HTTP payload analysis on the HTTP message **http_msg**. This |
| 3538 | callback functions is only called for HTTP streams. |
| 3539 | |
| 3540 | * .. js:function:: FILTER.tcp_payload(flt, txn, chn) |
| 3541 | |
| 3542 | Called during the TCP payload analysis on the channel **chn**. |
| 3543 | |
Aurelien DARRAGON | 53901f4 | 2022-10-13 19:49:42 +0200 | [diff] [blame] | 3544 | Here is a full example: |
Christopher Faulet | 5a2c661 | 2021-08-15 20:35:25 +0200 | [diff] [blame] | 3545 | |
| 3546 | .. code-block:: lua |
| 3547 | |
| 3548 | Trace = {} |
| 3549 | Trace.id = "Lua trace filter" |
| 3550 | Trace.flags = filter.FLT_CFG_FL_HTX; |
| 3551 | Trace.__index = Trace |
| 3552 | |
| 3553 | function Trace:new() |
| 3554 | local trace = {} |
| 3555 | setmetatable(trace, Trace) |
| 3556 | trace.req_len = 0 |
| 3557 | trace.res_len = 0 |
| 3558 | return trace |
| 3559 | end |
| 3560 | |
| 3561 | function Trace:start_analyze(txn, chn) |
| 3562 | if chn:is_resp() then |
| 3563 | print("Start response analysis") |
| 3564 | else |
| 3565 | print("Start request analysis") |
| 3566 | end |
| 3567 | filter.register_data_filter(self, chn) |
| 3568 | end |
| 3569 | |
| 3570 | function Trace:end_analyze(txn, chn) |
| 3571 | if chn:is_resp() then |
| 3572 | print("End response analysis: "..self.res_len.." bytes filtered") |
| 3573 | else |
| 3574 | print("End request analysis: "..self.req_len.." bytes filtered") |
| 3575 | end |
| 3576 | end |
| 3577 | |
| 3578 | function Trace:http_headers(txn, http_msg) |
| 3579 | stline = http_msg:get_stline() |
| 3580 | if http_msg.channel:is_resp() then |
| 3581 | print("response:") |
| 3582 | print(stline.version.." "..stline.code.." "..stline.reason) |
| 3583 | else |
| 3584 | print("request:") |
| 3585 | print(stline.method.." "..stline.uri.." "..stline.version) |
| 3586 | end |
| 3587 | |
| 3588 | for n, hdrs in pairs(http_msg:get_headers()) do |
| 3589 | for i,v in pairs(hdrs) do |
| 3590 | print(n..": "..v) |
| 3591 | end |
| 3592 | end |
| 3593 | return filter.CONTINUE |
| 3594 | end |
| 3595 | |
| 3596 | function Trace:http_payload(txn, http_msg) |
| 3597 | body = http_msg:body(-20000) |
| 3598 | if http_msg.channel:is_resp() then |
| 3599 | self.res_len = self.res_len + body:len() |
| 3600 | else |
| 3601 | self.req_len = self.req_len + body:len() |
| 3602 | end |
| 3603 | end |
| 3604 | |
| 3605 | core.register_filter("trace", Trace, function(trace, args) |
| 3606 | return trace |
| 3607 | end) |
| 3608 | |
| 3609 | .. |
| 3610 | |
| 3611 | .. _httpmessage_class: |
| 3612 | |
| 3613 | HTTPMessage class |
| 3614 | =================== |
| 3615 | |
| 3616 | .. js:class:: HTTPMessage |
| 3617 | |
| 3618 | **context**: filter |
| 3619 | |
Aurelien DARRAGON | 53901f4 | 2022-10-13 19:49:42 +0200 | [diff] [blame] | 3620 | This class contains all functions to manipulate a HTTP message. For now, this |
Christopher Faulet | 5a2c661 | 2021-08-15 20:35:25 +0200 | [diff] [blame] | 3621 | class is only available from a filter context. |
| 3622 | |
| 3623 | .. js:function:: HTTPMessage.add_header(http_msg, name, value) |
| 3624 | |
Aurelien DARRAGON | 53901f4 | 2022-10-13 19:49:42 +0200 | [diff] [blame] | 3625 | Appends a HTTP header field in the HTTP message **http_msg** whose name is |
Christopher Faulet | 5a2c661 | 2021-08-15 20:35:25 +0200 | [diff] [blame] | 3626 | specified in **name** and whose value is defined in **value**. |
| 3627 | |
| 3628 | :param class_httpmessage http_msg: The manipulated HTTP message. |
| 3629 | :param string name: The header name. |
| 3630 | :param string value: The header value. |
| 3631 | |
| 3632 | .. js:function:: HTTPMessage.append(http_msg, string) |
| 3633 | |
| 3634 | This function copies the string **string** at the end of incoming data of the |
| 3635 | HTTP message **http_msg**. The function returns the copied length on success |
| 3636 | or -1 if data cannot be copied. |
| 3637 | |
| 3638 | Same that :js:func:`HTTPMessage.insert(http_msg, string, http_msg:input())`. |
| 3639 | |
| 3640 | :param class_httpmessage http_msg: The manipulated HTTP message. |
Aurelien DARRAGON | 53901f4 | 2022-10-13 19:49:42 +0200 | [diff] [blame] | 3641 | :param string string: The data to copy at the end of incoming data. |
Christopher Faulet | 5a2c661 | 2021-08-15 20:35:25 +0200 | [diff] [blame] | 3642 | :returns: an integer containing the amount of bytes copied or -1. |
| 3643 | |
| 3644 | .. js:function:: HTTPMessage.body(http_msgl[, offset[, length]]) |
| 3645 | |
| 3646 | This function returns **length** bytes of incoming data from the HTTP message |
| 3647 | **http_msg**, starting at the offset **offset**. The data are not removed from |
| 3648 | the buffer. |
| 3649 | |
| 3650 | By default, if no length is provided, all incoming data found, starting at the |
| 3651 | given offset, are returned. If **length** is set to -1, the function tries to |
| 3652 | retrieve a maximum of data. Because it is called in the filter context, it |
Aurelien DARRAGON | 53901f4 | 2022-10-13 19:49:42 +0200 | [diff] [blame] | 3653 | never yield. Not providing an offset is the same as setting it to 0. A |
Ilya Shipitsin | ff0f278 | 2021-08-22 22:18:07 +0500 | [diff] [blame] | 3654 | positive offset is relative to the beginning of incoming data of the |
Christopher Faulet | 5a2c661 | 2021-08-15 20:35:25 +0200 | [diff] [blame] | 3655 | http_message buffer while negative offset is relative to their end. |
| 3656 | |
| 3657 | If there is no incoming data and the HTTP message can't receive more data, a 'nil' |
| 3658 | value is returned. |
| 3659 | |
| 3660 | :param class_httpmessage http_msg: The manipulated HTTP message. |
| 3661 | :param integer offset: *optional* The offset in incoming data to start to get |
| 3662 | data. 0 by default. May be negative to be relative to |
| 3663 | the end of incoming data. |
| 3664 | :param integer length: *optional* The expected length of data to retrieve. All |
| 3665 | incoming data by default. May be set to -1 to get a |
| 3666 | maximum of data. |
| 3667 | :returns: a string containing the data found or nil. |
| 3668 | |
| 3669 | .. js:function:: HTTPMessage.eom(http_msg) |
| 3670 | |
| 3671 | This function returns true if the end of message is reached for the HTTP |
| 3672 | message **http_msg**. |
| 3673 | |
| 3674 | :param class_httpmessage http_msg: The manipulated HTTP message. |
| 3675 | :returns: an integer containing the amount of available bytes. |
| 3676 | |
| 3677 | .. js:function:: HTTPMessage.del_header(http_msg, name) |
| 3678 | |
| 3679 | Removes all HTTP header fields in the HTTP message **http_msg** whose name is |
| 3680 | specified in **name**. |
| 3681 | |
| 3682 | :param class_httpmessage http_msg: The manipulated http message. |
| 3683 | :param string name: The header name. |
| 3684 | |
| 3685 | .. js:function:: HTTPMessage.get_headers(http_msg) |
| 3686 | |
| 3687 | Returns a table containing all the headers of the HTTP message **http_msg**. |
| 3688 | |
| 3689 | :param class_httpmessage http_msg: The manipulated http message. |
| 3690 | :returns: table of headers. |
| 3691 | |
| 3692 | This is the form of the returned table: |
| 3693 | |
| 3694 | .. code-block:: lua |
| 3695 | |
| 3696 | http_msg:get_headers()['<header-name>'][<header-index>] = "<header-value>" |
| 3697 | |
| 3698 | local hdr = http_msg:get_headers() |
| 3699 | hdr["host"][0] = "www.test.com" |
| 3700 | hdr["accept"][0] = "audio/basic q=1" |
| 3701 | hdr["accept"][1] = "audio/*, q=0.2" |
| 3702 | hdr["accept"][2] = "*.*, q=0.1" |
| 3703 | .. |
| 3704 | |
| 3705 | .. js:function:: HTTPMessage.get_stline(http_msg) |
| 3706 | |
| 3707 | Returns a table containing the start-line of the HTTP message **http_msg**. |
| 3708 | |
| 3709 | :param class_httpmessage http_msg: The manipulated http message. |
| 3710 | :returns: the start-line. |
| 3711 | |
| 3712 | This is the form of the returned table: |
| 3713 | |
| 3714 | .. code-block:: lua |
| 3715 | |
| 3716 | -- for the request : |
| 3717 | {"method" = string, "uri" = string, "version" = string} |
| 3718 | |
| 3719 | -- for the response: |
| 3720 | {"version" = string, "code" = string, "reason" = string} |
| 3721 | .. |
| 3722 | |
| 3723 | .. js:function:: HTTPMessage.forward(http_msg, length) |
| 3724 | |
| 3725 | This function forwards **length** bytes of data from the HTTP message |
Aurelien DARRAGON | 53901f4 | 2022-10-13 19:49:42 +0200 | [diff] [blame] | 3726 | **http_msg**. Because it is called in the filter context, it never yields. Only |
Christopher Faulet | 5a2c661 | 2021-08-15 20:35:25 +0200 | [diff] [blame] | 3727 | available incoming data may be forwarded, event if the requested length |
| 3728 | exceeds the available amount of incoming data. It returns the amount of data |
| 3729 | forwarded. |
| 3730 | |
| 3731 | :param class_httpmessage http_msg: The manipulated HTTP message. |
| 3732 | :param integer int: The amount of data to forward. |
| 3733 | |
| 3734 | .. js:function:: HTTPMessage.input(http_msg) |
| 3735 | |
| 3736 | This function returns the length of incoming data in the HTTP message |
Aurelien DARRAGON | 53901f4 | 2022-10-13 19:49:42 +0200 | [diff] [blame] | 3737 | **http_msg** from the filter point of view. |
Christopher Faulet | 5a2c661 | 2021-08-15 20:35:25 +0200 | [diff] [blame] | 3738 | |
| 3739 | :param class_httpmessage http_msg: The manipulated HTTP message. |
| 3740 | :returns: an integer containing the amount of available bytes. |
| 3741 | |
| 3742 | .. js:function:: HTTPMessage.insert(http_msg, string[, offset]) |
| 3743 | |
| 3744 | This function copies the string **string** at the offset **offset** in |
| 3745 | incoming data of the HTTP message **http_msg**. The function returns the |
| 3746 | copied length on success or -1 if data cannot be copied. |
| 3747 | |
| 3748 | By default, if no offset is provided, the string is copied in front of |
Ilya Shipitsin | ff0f278 | 2021-08-22 22:18:07 +0500 | [diff] [blame] | 3749 | incoming data. A positive offset is relative to the beginning of incoming data |
Christopher Faulet | 5a2c661 | 2021-08-15 20:35:25 +0200 | [diff] [blame] | 3750 | of the HTTP message while negative offset is relative to their end. |
| 3751 | |
| 3752 | :param class_httpmessage http_msg: The manipulated HTTP message. |
Aurelien DARRAGON | 53901f4 | 2022-10-13 19:49:42 +0200 | [diff] [blame] | 3753 | :param string string: The data to copy into incoming data. |
| 3754 | :param integer offset: *optional* The offset in incoming data where to copy |
Christopher Faulet | 5a2c661 | 2021-08-15 20:35:25 +0200 | [diff] [blame] | 3755 | data. 0 by default. May be negative to be relative to |
| 3756 | the end of incoming data. |
| 3757 | :returns: an integer containing the amount of bytes copied or -1. |
| 3758 | |
| 3759 | .. js:function:: HTTPMessage.is_full(http_msg) |
| 3760 | |
| 3761 | This function returns true if the HTTP message **http_msg** is full. |
| 3762 | |
| 3763 | :param class_httpmessage http_msg: The manipulated HTTP message. |
| 3764 | :returns: a boolean |
| 3765 | |
| 3766 | .. js:function:: HTTPMessage.is_resp(http_msg) |
| 3767 | |
| 3768 | This function returns true if the HTTP message **http_msg** is the response |
| 3769 | one. |
| 3770 | |
| 3771 | :param class_httpmessage http_msg: The manipulated HTTP message. |
| 3772 | :returns: a boolean |
| 3773 | |
| 3774 | .. js:function:: HTTPMessage.may_recv(http_msg) |
| 3775 | |
| 3776 | This function returns true if the HTTP message **http_msg** may still receive |
| 3777 | data. |
| 3778 | |
| 3779 | :param class_httpmessage http_msg: The manipulated HTTP message. |
| 3780 | :returns: a boolean |
| 3781 | |
| 3782 | .. js:function:: HTTPMessage.output(http_msg) |
| 3783 | |
| 3784 | This function returns the length of outgoing data of the HTTP message |
| 3785 | **http_msg**. |
| 3786 | |
| 3787 | :param class_httpmessage http_msg: The manipulated HTTP message. |
| 3788 | :returns: an integer containing the amount of available bytes. |
| 3789 | |
| 3790 | .. js:function:: HTTPMessage.prepend(http_msg, string) |
| 3791 | |
| 3792 | This function copies the string **string** in front of incoming data of the |
| 3793 | HTTP message **http_msg**. The function returns the copied length on success |
| 3794 | or -1 if data cannot be copied. |
| 3795 | |
| 3796 | Same that :js:func:`HTTPMessage.insert(http_msg, string, 0)`. |
| 3797 | |
| 3798 | :param class_httpmessage http_msg: The manipulated HTTP message. |
Aurelien DARRAGON | 53901f4 | 2022-10-13 19:49:42 +0200 | [diff] [blame] | 3799 | :param string string: The data to copy in front of incoming data. |
Christopher Faulet | 5a2c661 | 2021-08-15 20:35:25 +0200 | [diff] [blame] | 3800 | :returns: an integer containing the amount of bytes copied or -1. |
| 3801 | |
| 3802 | .. js:function:: HTTPMessage.remove(http_msg[, offset[, length]]) |
| 3803 | |
| 3804 | This function removes **length** bytes of incoming data of the HTTP message |
| 3805 | **http_msg**, starting at offset **offset**. This function returns number of |
| 3806 | bytes removed on success. |
| 3807 | |
| 3808 | By default, if no length is provided, all incoming data, starting at the given |
Aurelien DARRAGON | 53901f4 | 2022-10-13 19:49:42 +0200 | [diff] [blame] | 3809 | offset, are removed. Not providing an offset is the same that setting it |
Ilya Shipitsin | ff0f278 | 2021-08-22 22:18:07 +0500 | [diff] [blame] | 3810 | to 0. A positive offset is relative to the beginning of incoming data of the |
Aurelien DARRAGON | 53901f4 | 2022-10-13 19:49:42 +0200 | [diff] [blame] | 3811 | HTTP message while negative offset is relative to the end. |
Christopher Faulet | 5a2c661 | 2021-08-15 20:35:25 +0200 | [diff] [blame] | 3812 | |
| 3813 | :param class_httpmessage http_msg: The manipulated HTTP message. |
Aurelien DARRAGON | 53901f4 | 2022-10-13 19:49:42 +0200 | [diff] [blame] | 3814 | :param integer offset: *optional* The offset in incoming data where to start |
Christopher Faulet | 5a2c661 | 2021-08-15 20:35:25 +0200 | [diff] [blame] | 3815 | to remove data. 0 by default. May be negative to |
| 3816 | be relative to the end of incoming data. |
| 3817 | :param integer length: *optional* The length of data to remove. All incoming |
| 3818 | data by default. |
| 3819 | :returns: an integer containing the amount of bytes removed. |
| 3820 | |
| 3821 | .. js:function:: HTTPMessage.rep_header(http_msg, name, regex, replace) |
| 3822 | |
| 3823 | Matches the regular expression in all occurrences of header field **name** |
| 3824 | according to regex **regex**, and replaces them with the string **replace**. |
| 3825 | The replacement value can contain back references like \1, \2, ... This |
| 3826 | function acts on whole header lines, regardless of the number of values they |
| 3827 | may contain. |
| 3828 | |
| 3829 | :param class_httpmessage http_msg: The manipulated HTTP message. |
| 3830 | :param string name: The header name. |
| 3831 | :param string regex: The match regular expression. |
| 3832 | :param string replace: The replacement value. |
| 3833 | |
| 3834 | .. js:function:: HTTPMessage.rep_value(http_msg, name, regex, replace) |
| 3835 | |
| 3836 | Matches the regular expression on every comma-delimited value of header field |
| 3837 | **name** according to regex **regex**, and replaces them with the string |
| 3838 | **replace**. The replacement value can contain back references like \1, \2, |
| 3839 | ... |
| 3840 | |
| 3841 | :param class_httpmessage http_msg: The manipulated HTTP message. |
| 3842 | :param string name: The header name. |
| 3843 | :param string regex: The match regular expression. |
| 3844 | :param string replace: The replacement value. |
| 3845 | |
| 3846 | .. js:function:: HTTPMessage.send(http_msg, string) |
| 3847 | |
Aurelien DARRAGON | 53901f4 | 2022-10-13 19:49:42 +0200 | [diff] [blame] | 3848 | This function requires immediate send of the string **string**. It means the |
Ilya Shipitsin | ff0f278 | 2021-08-22 22:18:07 +0500 | [diff] [blame] | 3849 | string is copied at the beginning of incoming data of the HTTP message |
Christopher Faulet | 5a2c661 | 2021-08-15 20:35:25 +0200 | [diff] [blame] | 3850 | **http_msg** and immediately forwarded. Because it is called in the filter |
Aurelien DARRAGON | 53901f4 | 2022-10-13 19:49:42 +0200 | [diff] [blame] | 3851 | context, it never yields. |
Christopher Faulet | 5a2c661 | 2021-08-15 20:35:25 +0200 | [diff] [blame] | 3852 | |
| 3853 | :param class_httpmessage http_msg: The manipulated HTTP message. |
| 3854 | :param string string: The data to send. |
| 3855 | :returns: an integer containing the amount of bytes copied or -1. |
| 3856 | |
| 3857 | .. js:function:: HTTPMessage.set(http_msg, string[, offset[, length]]) |
| 3858 | |
Aurelien DARRAGON | 53901f4 | 2022-10-13 19:49:42 +0200 | [diff] [blame] | 3859 | This function replaces **length** bytes of incoming data of the HTTP message |
Christopher Faulet | 5a2c661 | 2021-08-15 20:35:25 +0200 | [diff] [blame] | 3860 | **http_msg**, starting at offset **offset**, by the string **string**. The |
| 3861 | function returns the copied length on success or -1 if data cannot be copied. |
| 3862 | |
| 3863 | By default, if no length is provided, all incoming data, starting at the given |
Aurelien DARRAGON | 53901f4 | 2022-10-13 19:49:42 +0200 | [diff] [blame] | 3864 | offset, are replaced. Not providing an offset is the same as setting it |
Ilya Shipitsin | ff0f278 | 2021-08-22 22:18:07 +0500 | [diff] [blame] | 3865 | to 0. A positive offset is relative to the beginning of incoming data of the |
Aurelien DARRAGON | 53901f4 | 2022-10-13 19:49:42 +0200 | [diff] [blame] | 3866 | HTTP message while negative offset is relative to the end. |
Christopher Faulet | 5a2c661 | 2021-08-15 20:35:25 +0200 | [diff] [blame] | 3867 | |
| 3868 | :param class_httpmessage http_msg: The manipulated HTTP message. |
Aurelien DARRAGON | 53901f4 | 2022-10-13 19:49:42 +0200 | [diff] [blame] | 3869 | :param string string: The data to copy into incoming data. |
| 3870 | :param integer offset: *optional* The offset in incoming data where to start |
Christopher Faulet | 5a2c661 | 2021-08-15 20:35:25 +0200 | [diff] [blame] | 3871 | the data replacement. 0 by default. May be negative to |
| 3872 | be relative to the end of incoming data. |
| 3873 | :param integer length: *optional* The length of data to replace. All incoming |
| 3874 | data by default. |
| 3875 | :returns: an integer containing the amount of bytes copied or -1. |
| 3876 | |
| 3877 | .. js:function:: HTTPMessage.set_eom(http_msg) |
| 3878 | |
| 3879 | This function set the end of message for the HTTP message **http_msg**. |
| 3880 | |
| 3881 | :param class_httpmessage http_msg: The manipulated HTTP message. |
| 3882 | |
| 3883 | .. js:function:: HTTPMessage.set_header(http_msg, name, value) |
| 3884 | |
| 3885 | This variable replace all occurrence of all header matching the name **name**, |
| 3886 | by only one containing the value **value**. |
| 3887 | |
| 3888 | :param class_httpmessage http_msg: The manipulated HTTP message. |
| 3889 | :param string name: The header name. |
| 3890 | :param string value: The header value. |
| 3891 | |
| 3892 | This function does the same work as the following code: |
| 3893 | |
| 3894 | .. code-block:: lua |
| 3895 | |
| 3896 | http_msg:del_header("header") |
| 3897 | http_msg:add_header("header", "value") |
| 3898 | .. |
| 3899 | |
| 3900 | .. js:function:: HTTPMessage.set_method(http_msg, method) |
| 3901 | |
| 3902 | Rewrites the request method with the string **method**. The HTTP message |
| 3903 | **http_msg** must be the request. |
| 3904 | |
| 3905 | :param class_httpmessage http_msg: The manipulated HTTP message. |
| 3906 | :param string method: The new method. |
| 3907 | |
| 3908 | .. js:function:: HTTPMessage.set_path(http_msg, path) |
| 3909 | |
| 3910 | Rewrites the request path with the string **path**. The HTTP message |
| 3911 | **http_msg** must be the request. |
| 3912 | |
| 3913 | :param class_httpmessage http_msg: The manipulated HTTP message. |
| 3914 | :param string method: The new method. |
| 3915 | |
| 3916 | .. js:function:: HTTPMessage.set_query(http_msg, query) |
| 3917 | |
| 3918 | Rewrites the request's query string which appears after the first question |
| 3919 | mark ("?") with the string **query**. The HTTP message **http_msg** must be |
| 3920 | the request. |
| 3921 | |
| 3922 | :param class_httpmessage http_msg: The manipulated HTTP message. |
| 3923 | :param string query: The new query. |
| 3924 | |
| 3925 | .. js:function:: HTTPMessage.set_status(http_msg, status[, reason]) |
| 3926 | |
Ilya Shipitsin | ff0f278 | 2021-08-22 22:18:07 +0500 | [diff] [blame] | 3927 | Rewrites the response status code with the integer **code** and optional the |
Christopher Faulet | 5a2c661 | 2021-08-15 20:35:25 +0200 | [diff] [blame] | 3928 | reason **reason**. If no custom reason is provided, it will be generated from |
| 3929 | the status. The HTTP message **http_msg** must be the response. |
| 3930 | |
| 3931 | :param class_httpmessage http_msg: The manipulated HTTP message. |
| 3932 | :param integer status: The new response status code. |
| 3933 | :param string reason: The new response reason (optional). |
| 3934 | |
| 3935 | .. js:function:: HTTPMessage.set_uri(http_msg, uri) |
| 3936 | |
| 3937 | Rewrites the request URI with the string **uri**. The HTTP message |
| 3938 | **http_msg** must be the request. |
| 3939 | |
| 3940 | :param class_httpmessage http_msg: The manipulated HTTP message. |
| 3941 | :param string uri: The new uri. |
| 3942 | |
| 3943 | .. js:function:: HTTPMessage.unset_eom(http_msg) |
| 3944 | |
| 3945 | This function remove the end of message for the HTTP message **http_msg**. |
| 3946 | |
| 3947 | :param class_httpmessage http_msg: The manipulated HTTP message. |
| 3948 | |
William Lallemand | 10cea5c | 2022-03-30 16:02:43 +0200 | [diff] [blame] | 3949 | .. _CertCache_class: |
| 3950 | |
| 3951 | CertCache class |
| 3952 | ================ |
| 3953 | |
| 3954 | .. js:class:: CertCache |
| 3955 | |
| 3956 | This class allows to update an SSL certificate file in the memory of the |
| 3957 | current HAProxy process. It will do the same as "set ssl cert" + "commit ssl |
| 3958 | cert" over the HAProxy CLI. |
| 3959 | |
| 3960 | .. js:function:: CertCache.set(certificate) |
| 3961 | |
| 3962 | This function updates a certificate in memory. |
| 3963 | |
| 3964 | :param table certificate: A table containing the fields to update. |
| 3965 | :param string certificate.filename: The mandatory filename of the certificate |
| 3966 | to update, it must already exist in memory. |
| 3967 | :param string certificate.crt: A certificate in the PEM format. It can also |
| 3968 | contain a private key. |
| 3969 | :param string certificate.key: A private key in the PEM format. |
| 3970 | :param string certificate.ocsp: An OCSP response in base64. (cf management.txt) |
| 3971 | :param string certificate.issuer: The certificate of the OCSP issuer. |
| 3972 | :param string certificate.sctl: An SCTL file. |
| 3973 | |
| 3974 | .. code-block:: lua |
| 3975 | |
| 3976 | CertCache.set{filename="certs/localhost9994.pem.rsa", crt=crt} |
| 3977 | |
| 3978 | |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 3979 | External Lua libraries |
| 3980 | ====================== |
| 3981 | |
| 3982 | A lot of useful lua libraries can be found here: |
| 3983 | |
Aurelien DARRAGON | 846fc7d | 2022-10-14 08:48:57 +0200 | [diff] [blame] | 3984 | * Lua toolbox has been superseded by `https://luarocks.org/ <https://luarocks.org/>`_ |
| 3985 | The old lua toolbox source code is still available here `https://github.com/catwell/lua-toolbox <https://github.com/catwell/lua-toolbox>`_ (DEPRECATED) |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 3986 | |
Ilya Shipitsin | 2075ca8 | 2020-03-06 23:22:22 +0500 | [diff] [blame] | 3987 | Redis client library: |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 3988 | |
| 3989 | * `https://github.com/nrk/redis-lua <https://github.com/nrk/redis-lua>`_ |
| 3990 | |
Aurelien DARRAGON | 53901f4 | 2022-10-13 19:49:42 +0200 | [diff] [blame] | 3991 | This is an example about the usage of the Redis library within HAProxy. Note that |
| 3992 | each call to any function of this library can throw an error if the socket |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 3993 | connection fails. |
| 3994 | |
| 3995 | .. code-block:: lua |
| 3996 | |
| 3997 | -- load the redis library |
| 3998 | local redis = require("redis"); |
| 3999 | |
| 4000 | function do_something(txn) |
| 4001 | |
| 4002 | -- create and connect new tcp socket |
| 4003 | local tcp = core.tcp(); |
| 4004 | tcp:settimeout(1); |
| 4005 | tcp:connect("127.0.0.1", 6379); |
| 4006 | |
| 4007 | -- use the redis library with this new socket |
| 4008 | local client = redis.connect({socket=tcp}); |
| 4009 | client:ping(); |
| 4010 | |
| 4011 | end |
| 4012 | |
| 4013 | OpenSSL: |
| 4014 | |
| 4015 | * `http://mkottman.github.io/luacrypto/index.html |
| 4016 | <http://mkottman.github.io/luacrypto/index.html>`_ |
| 4017 | |
| 4018 | * `https://github.com/brunoos/luasec/wiki |
| 4019 | <https://github.com/brunoos/luasec/wiki>`_ |