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 | |
Aurelien DARRAGON | 096b383 | 2023-04-20 11:32:46 +0200 | [diff] [blame^] | 945 | function(event, event_data, sub, when) |
Aurelien DARRAGON | c84899c | 2023-02-20 18:18:59 +0100 | [diff] [blame] | 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) |
Aurelien DARRAGON | 096b383 | 2023-04-20 11:32:46 +0200 | [diff] [blame^] | 951 | * **when**: timestamp corresponding to the date when the event was generated. |
| 952 | It is an integer representing the number of seconds elapsed since Epoch. |
| 953 | It may be provided as optional argument to `os.date()` lua function to |
| 954 | convert it to a string according to a given format string. |
Aurelien DARRAGON | c84899c | 2023-02-20 18:18:59 +0100 | [diff] [blame] | 955 | |
| 956 | .. Warning:: |
| 957 | The callback function will only be scheduled on the very same thread that |
| 958 | performed the subscription. |
| 959 | |
| 960 | Moreover, each thread treats events sequentially. It means that if you have, |
| 961 | let's say SERVER_UP followed by a SERVER_DOWN in a short timelapse, then |
| 962 | the cb function will first be called with SERVER_UP, and once it's done |
| 963 | handling the event, the cb function will be called again with SERVER_DOWN. |
| 964 | |
| 965 | This is to ensure event consistency when it comes to logging / triggering logic |
| 966 | from lua. |
| 967 | |
| 968 | Your lua cb function may yield if needed, but you're pleased to process the |
| 969 | event as fast as possible to prevent the event queue from growing up, depending |
| 970 | on the event flow that is expected for the given subscription. |
| 971 | |
| 972 | To prevent abuses, if the event queue for the current subscription goes over |
| 973 | a certain amount of unconsumed events, the subscription will pause itself |
| 974 | automatically for as long as it takes for your handler to catch up. This would |
| 975 | lead to events being missed, so an error will be reported in the logs to warn |
| 976 | you about that. |
| 977 | This is not something you want to let happen too often, it may indicate that |
| 978 | you subscribed to an event that is occurring too frequently or/and that your |
| 979 | callback function is too slow to keep up the pace and you should review it. |
| 980 | |
| 981 | If you want to do some parallel processing because your callback functions are |
| 982 | slow: you might want to create subtasks from lua using |
| 983 | :js:func:`core.register_task()` from within your callback function to perform |
| 984 | the heavy job in a dedicated task and allow remaining events to be processed |
| 985 | more quickly. |
| 986 | |
Thierry Fournier | f61aa63 | 2016-02-19 20:56:00 +0100 | [diff] [blame] | 987 | .. _proxy_class: |
| 988 | |
| 989 | Proxy class |
| 990 | ============ |
| 991 | |
| 992 | .. js:class:: Proxy |
| 993 | |
| 994 | This class provides a way for manipulating proxy and retrieving information |
| 995 | like statistics. |
| 996 | |
Thierry FOURNIER | 817e759 | 2017-07-24 14:35:04 +0200 | [diff] [blame] | 997 | .. js:attribute:: Proxy.name |
| 998 | |
| 999 | Contain the name of the proxy. |
| 1000 | |
Aurelien DARRAGON | c4b2437 | 2023-03-02 12:00:06 +0100 | [diff] [blame] | 1001 | .. warning:: |
| 1002 | This attribute is now deprecated and will eventually be removed. |
| 1003 | Please use :js:func:`Proxy.get_name()` function instead. |
| 1004 | |
Thierry Fournier | b046773 | 2022-10-07 12:07:24 +0200 | [diff] [blame] | 1005 | .. js:function:: Proxy.get_name() |
| 1006 | |
| 1007 | Returns the name of the proxy. |
| 1008 | |
Baptiste Assmann | 46c7255 | 2017-10-26 21:51:58 +0200 | [diff] [blame] | 1009 | .. js:attribute:: Proxy.uuid |
| 1010 | |
| 1011 | Contain the unique identifier of the proxy. |
| 1012 | |
Aurelien DARRAGON | c4b2437 | 2023-03-02 12:00:06 +0100 | [diff] [blame] | 1013 | .. warning:: |
| 1014 | This attribute is now deprecated and will eventually be removed. |
| 1015 | Please use :js:func:`Proxy.get_uuid()` function instead. |
| 1016 | |
Thierry Fournier | b046773 | 2022-10-07 12:07:24 +0200 | [diff] [blame] | 1017 | .. js:function:: Proxy.get_uuid() |
| 1018 | |
| 1019 | Returns the unique identifier of the proxy. |
| 1020 | |
Thierry Fournier | f2fdc9d | 2016-02-22 08:21:39 +0100 | [diff] [blame] | 1021 | .. js:attribute:: Proxy.servers |
| 1022 | |
Patrick Hemmer | c6a1d71 | 2018-05-01 21:30:41 -0400 | [diff] [blame] | 1023 | Contain a table with the attached servers. The table is indexed by server |
| 1024 | 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] | 1025 | |
Adis Nezirovic | 8878f8e | 2018-07-13 12:18:33 +0200 | [diff] [blame] | 1026 | .. js:attribute:: Proxy.stktable |
| 1027 | |
| 1028 | Contains a stick table object attached to the proxy. |
| 1029 | |
Thierry Fournier | ff48042 | 2016-02-25 08:36:46 +0100 | [diff] [blame] | 1030 | .. js:attribute:: Proxy.listeners |
| 1031 | |
Patrick Hemmer | c6a1d71 | 2018-05-01 21:30:41 -0400 | [diff] [blame] | 1032 | Contain a table with the attached listeners. The table is indexed by listener |
| 1033 | name, and each each listeners entry is an object of type |
| 1034 | :ref:`listener_class`. |
Thierry Fournier | ff48042 | 2016-02-25 08:36:46 +0100 | [diff] [blame] | 1035 | |
Thierry Fournier | f61aa63 | 2016-02-19 20:56:00 +0100 | [diff] [blame] | 1036 | .. js:function:: Proxy.pause(px) |
| 1037 | |
| 1038 | Pause the proxy. See the management socket documentation for more information. |
| 1039 | |
| 1040 | :param class_proxy px: A :ref:`proxy_class` which indicates the manipulated |
| 1041 | proxy. |
| 1042 | |
| 1043 | .. js:function:: Proxy.resume(px) |
| 1044 | |
| 1045 | Resume the proxy. See the management socket documentation for more |
| 1046 | information. |
| 1047 | |
| 1048 | :param class_proxy px: A :ref:`proxy_class` which indicates the manipulated |
| 1049 | proxy. |
| 1050 | |
| 1051 | .. js:function:: Proxy.stop(px) |
| 1052 | |
| 1053 | Stop the proxy. See the management socket documentation for more information. |
| 1054 | |
| 1055 | :param class_proxy px: A :ref:`proxy_class` which indicates the manipulated |
| 1056 | proxy. |
| 1057 | |
| 1058 | .. js:function:: Proxy.shut_bcksess(px) |
| 1059 | |
| 1060 | Kill the session attached to a backup server. See the management socket |
| 1061 | documentation for more information. |
| 1062 | |
| 1063 | :param class_proxy px: A :ref:`proxy_class` which indicates the manipulated |
| 1064 | proxy. |
| 1065 | |
| 1066 | .. js:function:: Proxy.get_cap(px) |
| 1067 | |
| 1068 | Returns a string describing the capabilities of the proxy. |
| 1069 | |
| 1070 | :param class_proxy px: A :ref:`proxy_class` which indicates the manipulated |
| 1071 | proxy. |
| 1072 | :returns: a string "frontend", "backend", "proxy" or "ruleset". |
| 1073 | |
| 1074 | .. js:function:: Proxy.get_mode(px) |
| 1075 | |
| 1076 | Returns a string describing the mode of the current proxy. |
| 1077 | |
| 1078 | :param class_proxy px: A :ref:`proxy_class` which indicates the manipulated |
| 1079 | proxy. |
| 1080 | :returns: a string "tcp", "http", "health" or "unknown" |
| 1081 | |
| 1082 | .. js:function:: Proxy.get_stats(px) |
| 1083 | |
Bertrand Jacquin | 874a35c | 2018-09-10 21:26:07 +0100 | [diff] [blame] | 1084 | Returns a table containing the proxy statistics. The statistics returned are |
Thierry Fournier | f61aa63 | 2016-02-19 20:56:00 +0100 | [diff] [blame] | 1085 | not the same if the proxy is frontend or a backend. |
| 1086 | |
| 1087 | :param class_proxy px: A :ref:`proxy_class` which indicates the manipulated |
| 1088 | proxy. |
Patrick Hemmer | c6a1d71 | 2018-05-01 21:30:41 -0400 | [diff] [blame] | 1089 | :returns: a key/value table containing stats |
Thierry Fournier | f61aa63 | 2016-02-19 20:56:00 +0100 | [diff] [blame] | 1090 | |
Thierry Fournier | f2fdc9d | 2016-02-22 08:21:39 +0100 | [diff] [blame] | 1091 | .. _server_class: |
| 1092 | |
| 1093 | Server class |
| 1094 | ============ |
| 1095 | |
Patrick Hemmer | c6a1d71 | 2018-05-01 21:30:41 -0400 | [diff] [blame] | 1096 | .. js:class:: Server |
| 1097 | |
| 1098 | This class provides a way for manipulating servers and retrieving information. |
| 1099 | |
Patrick Hemmer | a62ae7e | 2018-04-29 14:23:48 -0400 | [diff] [blame] | 1100 | .. js:attribute:: Server.name |
| 1101 | |
| 1102 | Contain the name of the server. |
| 1103 | |
Aurelien DARRAGON | c4b2437 | 2023-03-02 12:00:06 +0100 | [diff] [blame] | 1104 | .. warning:: |
| 1105 | This attribute is now deprecated and will eventually be removed. |
| 1106 | Please use :js:func:`Server.get_name()` function instead. |
| 1107 | |
Thierry Fournier | b046773 | 2022-10-07 12:07:24 +0200 | [diff] [blame] | 1108 | .. js:function:: Server.get_name(sv) |
| 1109 | |
| 1110 | Returns the name of the server. |
| 1111 | |
Patrick Hemmer | a62ae7e | 2018-04-29 14:23:48 -0400 | [diff] [blame] | 1112 | .. js:attribute:: Server.puid |
| 1113 | |
| 1114 | Contain the proxy unique identifier of the server. |
| 1115 | |
Aurelien DARRAGON | c4b2437 | 2023-03-02 12:00:06 +0100 | [diff] [blame] | 1116 | .. warning:: |
| 1117 | This attribute is now deprecated and will eventually be removed. |
| 1118 | Please use :js:func:`Server.get_puid()` function instead. |
| 1119 | |
Thierry Fournier | b046773 | 2022-10-07 12:07:24 +0200 | [diff] [blame] | 1120 | .. js:function:: Server.get_puid(sv) |
| 1121 | |
| 1122 | Returns the proxy unique identifier of the server. |
| 1123 | |
Aurelien DARRAGON | 94ee663 | 2023-03-10 15:11:27 +0100 | [diff] [blame] | 1124 | .. js:function:: Server.get_rid(sv) |
| 1125 | |
| 1126 | Returns the rid (revision ID) of the server. |
| 1127 | It is an unsigned integer that is set upon server creation. Value is derived |
| 1128 | from a global counter that starts at 0 and is incremented each time one or |
| 1129 | multiple server deletions are followed by a server addition (meaning that |
| 1130 | old name/id reuse could occur). |
| 1131 | |
| 1132 | Combining server name/id with server rid yields a process-wide unique |
| 1133 | identifier. |
| 1134 | |
Thierry Fournier | f2fdc9d | 2016-02-22 08:21:39 +0100 | [diff] [blame] | 1135 | .. js:function:: Server.is_draining(sv) |
| 1136 | |
Patrick Hemmer | c6a1d71 | 2018-05-01 21:30:41 -0400 | [diff] [blame] | 1137 | Return true if the server is currently draining sticky connections. |
Thierry Fournier | f2fdc9d | 2016-02-22 08:21:39 +0100 | [diff] [blame] | 1138 | |
| 1139 | :param class_server sv: A :ref:`server_class` which indicates the manipulated |
| 1140 | server. |
| 1141 | :returns: a boolean |
| 1142 | |
Patrick Hemmer | 32d539f | 2018-04-29 14:25:46 -0400 | [diff] [blame] | 1143 | .. js:function:: Server.set_maxconn(sv, weight) |
| 1144 | |
| 1145 | Dynamically change the maximum connections of the server. See the management |
| 1146 | socket documentation for more information about the format of the string. |
| 1147 | |
| 1148 | :param class_server sv: A :ref:`server_class` which indicates the manipulated |
| 1149 | server. |
| 1150 | :param string maxconn: A string describing the server maximum connections. |
| 1151 | |
| 1152 | .. js:function:: Server.get_maxconn(sv, weight) |
| 1153 | |
| 1154 | This function returns an integer representing the server maximum connections. |
| 1155 | |
| 1156 | :param class_server sv: A :ref:`server_class` which indicates the manipulated |
| 1157 | server. |
| 1158 | :returns: an integer. |
| 1159 | |
Thierry Fournier | f2fdc9d | 2016-02-22 08:21:39 +0100 | [diff] [blame] | 1160 | .. js:function:: Server.set_weight(sv, weight) |
| 1161 | |
Patrick Hemmer | c6a1d71 | 2018-05-01 21:30:41 -0400 | [diff] [blame] | 1162 | Dynamically change the weight of the server. See the management socket |
Thierry Fournier | f2fdc9d | 2016-02-22 08:21:39 +0100 | [diff] [blame] | 1163 | documentation for more information about the format of the string. |
| 1164 | |
| 1165 | :param class_server sv: A :ref:`server_class` which indicates the manipulated |
| 1166 | server. |
| 1167 | :param string weight: A string describing the server weight. |
| 1168 | |
| 1169 | .. js:function:: Server.get_weight(sv) |
| 1170 | |
Patrick Hemmer | c6a1d71 | 2018-05-01 21:30:41 -0400 | [diff] [blame] | 1171 | This function returns an integer representing the server weight. |
Thierry Fournier | f2fdc9d | 2016-02-22 08:21:39 +0100 | [diff] [blame] | 1172 | |
| 1173 | :param class_server sv: A :ref:`server_class` which indicates the manipulated |
| 1174 | server. |
| 1175 | :returns: an integer. |
| 1176 | |
Joseph C. Sible | 49bbf52 | 2020-05-04 22:20:32 -0400 | [diff] [blame] | 1177 | .. js:function:: Server.set_addr(sv, addr[, port]) |
Thierry Fournier | f2fdc9d | 2016-02-22 08:21:39 +0100 | [diff] [blame] | 1178 | |
Patrick Hemmer | c6a1d71 | 2018-05-01 21:30:41 -0400 | [diff] [blame] | 1179 | Dynamically change the address of the server. See the management socket |
Thierry Fournier | f2fdc9d | 2016-02-22 08:21:39 +0100 | [diff] [blame] | 1180 | documentation for more information about the format of the string. |
| 1181 | |
| 1182 | :param class_server sv: A :ref:`server_class` which indicates the manipulated |
| 1183 | server. |
Patrick Hemmer | c6a1d71 | 2018-05-01 21:30:41 -0400 | [diff] [blame] | 1184 | :param string addr: A string describing the server address. |
Thierry Fournier | f2fdc9d | 2016-02-22 08:21:39 +0100 | [diff] [blame] | 1185 | |
| 1186 | .. js:function:: Server.get_addr(sv) |
| 1187 | |
Patrick Hemmer | c6a1d71 | 2018-05-01 21:30:41 -0400 | [diff] [blame] | 1188 | Returns a string describing the address of the server. |
Thierry Fournier | f2fdc9d | 2016-02-22 08:21:39 +0100 | [diff] [blame] | 1189 | |
| 1190 | :param class_server sv: A :ref:`server_class` which indicates the manipulated |
| 1191 | server. |
| 1192 | :returns: A string |
| 1193 | |
| 1194 | .. js:function:: Server.get_stats(sv) |
| 1195 | |
| 1196 | Returns server statistics. |
| 1197 | |
| 1198 | :param class_server sv: A :ref:`server_class` which indicates the manipulated |
| 1199 | server. |
Patrick Hemmer | c6a1d71 | 2018-05-01 21:30:41 -0400 | [diff] [blame] | 1200 | :returns: a key/value table containing stats |
Thierry Fournier | f2fdc9d | 2016-02-22 08:21:39 +0100 | [diff] [blame] | 1201 | |
| 1202 | .. js:function:: Server.shut_sess(sv) |
| 1203 | |
| 1204 | Shutdown all the sessions attached to the server. See the management socket |
| 1205 | documentation for more information about this function. |
| 1206 | |
| 1207 | :param class_server sv: A :ref:`server_class` which indicates the manipulated |
| 1208 | server. |
| 1209 | |
| 1210 | .. js:function:: Server.set_drain(sv) |
| 1211 | |
| 1212 | Drain sticky sessions. See the management socket documentation for more |
| 1213 | information about this function. |
| 1214 | |
| 1215 | :param class_server sv: A :ref:`server_class` which indicates the manipulated |
| 1216 | server. |
| 1217 | |
| 1218 | .. js:function:: Server.set_maint(sv) |
| 1219 | |
| 1220 | Set maintenance mode. See the management socket documentation for more |
| 1221 | information about this function. |
| 1222 | |
| 1223 | :param class_server sv: A :ref:`server_class` which indicates the manipulated |
| 1224 | server. |
| 1225 | |
| 1226 | .. js:function:: Server.set_ready(sv) |
| 1227 | |
| 1228 | Set normal mode. See the management socket documentation for more information |
| 1229 | about this function. |
| 1230 | |
| 1231 | :param class_server sv: A :ref:`server_class` which indicates the manipulated |
| 1232 | server. |
| 1233 | |
| 1234 | .. js:function:: Server.check_enable(sv) |
| 1235 | |
| 1236 | Enable health checks. See the management socket documentation for more |
| 1237 | information about this function. |
| 1238 | |
| 1239 | :param class_server sv: A :ref:`server_class` which indicates the manipulated |
| 1240 | server. |
| 1241 | |
| 1242 | .. js:function:: Server.check_disable(sv) |
| 1243 | |
| 1244 | Disable health checks. See the management socket documentation for more |
| 1245 | information about this function. |
| 1246 | |
| 1247 | :param class_server sv: A :ref:`server_class` which indicates the manipulated |
| 1248 | server. |
| 1249 | |
| 1250 | .. js:function:: Server.check_force_up(sv) |
| 1251 | |
| 1252 | Force health-check up. See the management socket documentation for more |
| 1253 | information about this function. |
| 1254 | |
| 1255 | :param class_server sv: A :ref:`server_class` which indicates the manipulated |
| 1256 | server. |
| 1257 | |
| 1258 | .. js:function:: Server.check_force_nolb(sv) |
| 1259 | |
| 1260 | Force health-check nolb mode. See the management socket documentation for more |
| 1261 | information about this function. |
| 1262 | |
| 1263 | :param class_server sv: A :ref:`server_class` which indicates the manipulated |
| 1264 | server. |
| 1265 | |
| 1266 | .. js:function:: Server.check_force_down(sv) |
| 1267 | |
| 1268 | Force health-check down. See the management socket documentation for more |
| 1269 | information about this function. |
| 1270 | |
| 1271 | :param class_server sv: A :ref:`server_class` which indicates the manipulated |
| 1272 | server. |
| 1273 | |
| 1274 | .. js:function:: Server.agent_enable(sv) |
| 1275 | |
| 1276 | Enable agent check. See the management socket documentation for more |
| 1277 | information about this function. |
| 1278 | |
| 1279 | :param class_server sv: A :ref:`server_class` which indicates the manipulated |
| 1280 | server. |
| 1281 | |
| 1282 | .. js:function:: Server.agent_disable(sv) |
| 1283 | |
| 1284 | Disable agent check. See the management socket documentation for more |
| 1285 | information about this function. |
| 1286 | |
| 1287 | :param class_server sv: A :ref:`server_class` which indicates the manipulated |
| 1288 | server. |
| 1289 | |
| 1290 | .. js:function:: Server.agent_force_up(sv) |
| 1291 | |
| 1292 | Force agent check up. See the management socket documentation for more |
| 1293 | information about this function. |
| 1294 | |
| 1295 | :param class_server sv: A :ref:`server_class` which indicates the manipulated |
| 1296 | server. |
| 1297 | |
| 1298 | .. js:function:: Server.agent_force_down(sv) |
| 1299 | |
| 1300 | Force agent check down. See the management socket documentation for more |
| 1301 | information about this function. |
| 1302 | |
| 1303 | :param class_server sv: A :ref:`server_class` which indicates the manipulated |
| 1304 | server. |
| 1305 | |
Aurelien DARRAGON | 223770d | 2023-03-10 15:34:35 +0100 | [diff] [blame] | 1306 | .. js:function:: Server.event_sub(sv, event_types, func) |
| 1307 | |
| 1308 | Register a function that will be called on specific server events. |
| 1309 | It works exactly like :js:func:`core.event_sub()` except that the subscription |
| 1310 | will be performed within the server dedicated subscription list instead of the |
| 1311 | global one. |
| 1312 | (Your callback function will only be called for server events affecting sv) |
| 1313 | |
| 1314 | See :js:func:`core.event_sub()` for function usage. |
| 1315 | |
| 1316 | A key advantage to using :js:func:`Server.event_sub()` over |
| 1317 | :js:func:`core.event_sub()` for servers is that :js:func:`Server.event_sub()` |
| 1318 | allows you to be notified for servers events of a single server only. |
| 1319 | It removes the needs for extra filtering in your callback function if you only |
| 1320 | care about a single server, and also prevents useless wakeups. |
| 1321 | |
| 1322 | For instance, if you want to be notified for UP/DOWN events on a given set of |
| 1323 | servers, it is recommended to peform multiple per-server subscriptions since |
| 1324 | it will be more efficient that doing a single global subscription that will |
| 1325 | filter the received events. |
| 1326 | Unless you really want to be notified for servers events of ALL servers of |
| 1327 | course, which could make sense given you setup but should be avoided if you |
| 1328 | have an important number of servers as it will add a significant load on your |
| 1329 | haproxy process in case of multiple servers state change in a short amount of |
| 1330 | time. |
| 1331 | |
| 1332 | .. Note:: |
| 1333 | You may also combine :js:func:`core.event_sub()` with |
| 1334 | :js:func:`Server.event_sub()`. |
| 1335 | |
| 1336 | Also, don't forget that you can use :js:func:`core.register_task()` from |
| 1337 | your callback function if needed. (ie: parallel work) |
| 1338 | |
| 1339 | Here is a working example combining :js:func:`core.event_sub()` with |
| 1340 | :js:func:`Server.event_sub()` and :js:func:`core.register_task()` |
| 1341 | (This only serves as a demo, this is not necessarily useful to do so) |
| 1342 | |
| 1343 | .. code-block:: lua |
| 1344 | |
| 1345 | core.event_sub({"SERVER_ADD"}, function(event, data, sub) |
| 1346 | -- in the global event handler |
| 1347 | if data["reference"] ~= nil then |
| 1348 | print("Tracking new server: ", data["name"]) |
| 1349 | data["reference"]:event_sub({"SERVER_UP", "SERVER_DOWN"}, function(event, data, sub) |
| 1350 | -- in the per-server event handler |
| 1351 | if data["reference"] ~= nil then |
| 1352 | core.register_task(function(server) |
| 1353 | -- subtask to perform some async work (e.g.: HTTP API calls, sending emails...) |
| 1354 | print("ASYNC: SERVER ", server:get_name(), " is ", event == "SERVER_UP" and "UP" or "DOWN") |
| 1355 | end, data["reference"]) |
| 1356 | end |
| 1357 | end) |
| 1358 | end |
| 1359 | end) |
| 1360 | |
| 1361 | .. |
| 1362 | |
| 1363 | In this example, we will first track global server addition events. |
| 1364 | For each newly added server ("add server" on the cli), we will register a |
| 1365 | UP/DOWN server subscription. |
| 1366 | Then, the callback function will schedule the event handling in an async |
| 1367 | subtask which will receive the server reference as an argument. |
| 1368 | |
Thierry Fournier | ff48042 | 2016-02-25 08:36:46 +0100 | [diff] [blame] | 1369 | .. _listener_class: |
| 1370 | |
| 1371 | Listener class |
| 1372 | ============== |
| 1373 | |
| 1374 | .. js:function:: Listener.get_stats(ls) |
| 1375 | |
| 1376 | Returns server statistics. |
| 1377 | |
| 1378 | :param class_listener ls: A :ref:`listener_class` which indicates the |
| 1379 | manipulated listener. |
Patrick Hemmer | c6a1d71 | 2018-05-01 21:30:41 -0400 | [diff] [blame] | 1380 | :returns: a key/value table containing stats |
Thierry Fournier | ff48042 | 2016-02-25 08:36:46 +0100 | [diff] [blame] | 1381 | |
Aurelien DARRAGON | c84899c | 2023-02-20 18:18:59 +0100 | [diff] [blame] | 1382 | .. _event_sub_class: |
| 1383 | |
| 1384 | EventSub class |
| 1385 | ============== |
| 1386 | |
| 1387 | .. js:function:: EventSub.unsub() |
| 1388 | |
| 1389 | End the subscription, the callback function will not be called again. |
| 1390 | |
| 1391 | .. _server_event_class: |
| 1392 | |
| 1393 | ServerEvent class |
| 1394 | ================= |
| 1395 | |
| 1396 | .. js:attribute:: ServerEvent.name |
| 1397 | |
| 1398 | Contains the name of the server. |
| 1399 | |
| 1400 | .. js:attribute:: ServerEvent.puid |
| 1401 | |
| 1402 | Contains the proxy-unique uid of the server |
| 1403 | |
| 1404 | .. js:attribute:: ServerEvent.rid |
| 1405 | |
| 1406 | Contains the revision ID of the server |
| 1407 | |
| 1408 | .. js:attribute:: ServerEvent.proxy_name |
| 1409 | |
| 1410 | Contains the name of the proxy to which the server belongs |
| 1411 | |
Aurelien DARRAGON | 55f84c7 | 2023-03-22 17:49:04 +0100 | [diff] [blame] | 1412 | .. js:attribute:: ServerEvent.proxy_uuid |
| 1413 | |
| 1414 | Contains the uuid of the proxy to which the server belongs |
| 1415 | |
Aurelien DARRAGON | c84899c | 2023-02-20 18:18:59 +0100 | [diff] [blame] | 1416 | .. js:attribute:: ServerEvent.reference |
| 1417 | |
| 1418 | Reference to the live server (A :ref:`server_class`). |
| 1419 | |
| 1420 | .. Warning:: |
| 1421 | Not available if the server was removed in the meantime. |
| 1422 | (Will never be set for SERVER_DEL event since the server does not exist anymore) |
| 1423 | |
Thierry Fournier | 1de1659 | 2016-01-27 09:49:07 +0100 | [diff] [blame] | 1424 | .. _concat_class: |
| 1425 | |
| 1426 | Concat class |
| 1427 | ============ |
| 1428 | |
| 1429 | .. js:class:: Concat |
| 1430 | |
| 1431 | This class provides a fast way for string concatenation. The way using native |
| 1432 | Lua concatenation like the code below is slow for some reasons. |
| 1433 | |
| 1434 | .. code-block:: lua |
| 1435 | |
| 1436 | str = "string1" |
| 1437 | str = str .. ", string2" |
| 1438 | str = str .. ", string3" |
| 1439 | .. |
| 1440 | |
| 1441 | For each concatenation, Lua: |
Aurelien DARRAGON | 53901f4 | 2022-10-13 19:49:42 +0200 | [diff] [blame] | 1442 | - allocates memory for the result, |
| 1443 | - catenates the two string copying the strings in the new memory block, |
| 1444 | - frees the old memory block containing the string which is no longer used. |
| 1445 | |
Thierry Fournier | 1de1659 | 2016-01-27 09:49:07 +0100 | [diff] [blame] | 1446 | This process does many memory move, allocation and free. In addition, the |
Aurelien DARRAGON | 53901f4 | 2022-10-13 19:49:42 +0200 | [diff] [blame] | 1447 | 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] | 1448 | garbage collector. |
| 1449 | |
Aurelien DARRAGON | 53901f4 | 2022-10-13 19:49:42 +0200 | [diff] [blame] | 1450 | The Concat class provides an alternative way to concatenate strings. It uses |
Thierry Fournier | 1de1659 | 2016-01-27 09:49:07 +0100 | [diff] [blame] | 1451 | the internal Lua mechanism (it does not allocate memory), but it doesn't copy |
| 1452 | the data more than once. |
| 1453 | |
| 1454 | On my computer, the following loops spends 0.2s for the Concat method and |
| 1455 | 18.5s for the pure Lua implementation. So, the Concat class is about 1000x |
| 1456 | faster than the embedded solution. |
| 1457 | |
| 1458 | .. code-block:: lua |
| 1459 | |
| 1460 | for j = 1, 100 do |
| 1461 | c = core.concat() |
| 1462 | for i = 1, 20000 do |
| 1463 | c:add("#####") |
| 1464 | end |
| 1465 | end |
| 1466 | .. |
| 1467 | |
| 1468 | .. code-block:: lua |
| 1469 | |
| 1470 | for j = 1, 100 do |
| 1471 | c = "" |
| 1472 | for i = 1, 20000 do |
| 1473 | c = c .. "#####" |
| 1474 | end |
| 1475 | end |
| 1476 | .. |
| 1477 | |
| 1478 | .. js:function:: Concat.add(concat, string) |
| 1479 | |
| 1480 | This function adds a string to the current concatenated string. |
| 1481 | |
| 1482 | :param class_concat concat: A :ref:`concat_class` which contains the currently |
Ilya Shipitsin | 2075ca8 | 2020-03-06 23:22:22 +0500 | [diff] [blame] | 1483 | built string. |
Bertrand Jacquin | 874a35c | 2018-09-10 21:26:07 +0100 | [diff] [blame] | 1484 | :param string string: A new string to concatenate to the current built |
Thierry Fournier | 1de1659 | 2016-01-27 09:49:07 +0100 | [diff] [blame] | 1485 | string. |
| 1486 | |
| 1487 | .. js:function:: Concat.dump(concat) |
| 1488 | |
Bertrand Jacquin | 874a35c | 2018-09-10 21:26:07 +0100 | [diff] [blame] | 1489 | This function returns the concatenated string. |
Thierry Fournier | 1de1659 | 2016-01-27 09:49:07 +0100 | [diff] [blame] | 1490 | |
| 1491 | :param class_concat concat: A :ref:`concat_class` which contains the currently |
Ilya Shipitsin | 2075ca8 | 2020-03-06 23:22:22 +0500 | [diff] [blame] | 1492 | built string. |
Thierry Fournier | 1de1659 | 2016-01-27 09:49:07 +0100 | [diff] [blame] | 1493 | :returns: the concatenated string |
| 1494 | |
Thierry FOURNIER | dc59500 | 2015-12-21 11:13:52 +0100 | [diff] [blame] | 1495 | .. _fetches_class: |
| 1496 | |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 1497 | Fetches class |
| 1498 | ============= |
| 1499 | |
| 1500 | .. js:class:: Fetches |
| 1501 | |
| 1502 | This class contains a lot of internal HAProxy sample fetches. See the |
Aurelien DARRAGON | 53901f4 | 2022-10-13 19:49:42 +0200 | [diff] [blame] | 1503 | HAProxy "configuration.txt" documentation for more information. |
| 1504 | (chapters 7.3.2 to 7.3.6) |
Thierry FOURNIER | 2e4893c | 2015-03-18 13:37:27 +0100 | [diff] [blame] | 1505 | |
Christopher Faulet | 1e9b1b6 | 2021-08-11 10:14:30 +0200 | [diff] [blame] | 1506 | .. warning:: |
| 1507 | some sample fetches are not available in some context. These limitations |
| 1508 | are specified in this documentation when they're useful. |
Thierry FOURNIER | dc59500 | 2015-12-21 11:13:52 +0100 | [diff] [blame] | 1509 | |
Thierry FOURNIER | 12a865d | 2016-12-14 19:40:37 +0100 | [diff] [blame] | 1510 | :see: :js:attr:`TXN.f` |
| 1511 | :see: :js:attr:`TXN.sf` |
Thierry FOURNIER | 2e4893c | 2015-03-18 13:37:27 +0100 | [diff] [blame] | 1512 | |
Aurelien DARRAGON | 53901f4 | 2022-10-13 19:49:42 +0200 | [diff] [blame] | 1513 | Fetches are useful to: |
Thierry FOURNIER | 2e4893c | 2015-03-18 13:37:27 +0100 | [diff] [blame] | 1514 | |
| 1515 | * get system time, |
| 1516 | * get environment variable, |
| 1517 | * get random numbers, |
Aurelien DARRAGON | 53901f4 | 2022-10-13 19:49:42 +0200 | [diff] [blame] | 1518 | * 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] | 1519 | connections established, |
Aurelien DARRAGON | 53901f4 | 2022-10-13 19:49:42 +0200 | [diff] [blame] | 1520 | * get client information like ip source or destination, |
Thierry FOURNIER | 2e4893c | 2015-03-18 13:37:27 +0100 | [diff] [blame] | 1521 | * deal with stick tables, |
Aurelien DARRAGON | 53901f4 | 2022-10-13 19:49:42 +0200 | [diff] [blame] | 1522 | * fetch established SSL information, |
| 1523 | * fetch HTTP information like headers or method. |
Thierry FOURNIER | 2e4893c | 2015-03-18 13:37:27 +0100 | [diff] [blame] | 1524 | |
| 1525 | .. code-block:: lua |
| 1526 | |
Thierry FOURNIER | dc59500 | 2015-12-21 11:13:52 +0100 | [diff] [blame] | 1527 | function action(txn) |
| 1528 | -- Get source IP |
| 1529 | local clientip = txn.f:src() |
| 1530 | end |
Thierry FOURNIER | 2e4893c | 2015-03-18 13:37:27 +0100 | [diff] [blame] | 1531 | .. |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 1532 | |
Thierry FOURNIER | dc59500 | 2015-12-21 11:13:52 +0100 | [diff] [blame] | 1533 | .. _converters_class: |
| 1534 | |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 1535 | Converters class |
| 1536 | ================ |
| 1537 | |
| 1538 | .. js:class:: Converters |
| 1539 | |
| 1540 | This class contains a lot of internal HAProxy sample converters. See the |
Thierry FOURNIER | 2e4893c | 2015-03-18 13:37:27 +0100 | [diff] [blame] | 1541 | HAProxy documentation "configuration.txt" for more information about her |
| 1542 | usage. Its the chapter 7.3.1. |
| 1543 | |
Thierry FOURNIER | 12a865d | 2016-12-14 19:40:37 +0100 | [diff] [blame] | 1544 | :see: :js:attr:`TXN.c` |
| 1545 | :see: :js:attr:`TXN.sc` |
Thierry FOURNIER | 2e4893c | 2015-03-18 13:37:27 +0100 | [diff] [blame] | 1546 | |
Aurelien DARRAGON | 53901f4 | 2022-10-13 19:49:42 +0200 | [diff] [blame] | 1547 | Converters provides stateful transformation. They are useful to: |
Thierry FOURNIER | 2e4893c | 2015-03-18 13:37:27 +0100 | [diff] [blame] | 1548 | |
Aurelien DARRAGON | 53901f4 | 2022-10-13 19:49:42 +0200 | [diff] [blame] | 1549 | * convert input to base64, |
| 1550 | * apply hash on input string (djb2, crc32, sdbm, wt6), |
Thierry FOURNIER | 2e4893c | 2015-03-18 13:37:27 +0100 | [diff] [blame] | 1551 | * format date, |
| 1552 | * json escape, |
Aurelien DARRAGON | 53901f4 | 2022-10-13 19:49:42 +0200 | [diff] [blame] | 1553 | * extract preferred language comparing two lists, |
Thierry FOURNIER | 2e4893c | 2015-03-18 13:37:27 +0100 | [diff] [blame] | 1554 | * turn to lower or upper chars, |
| 1555 | * deal with stick tables. |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 1556 | |
Thierry FOURNIER | dc59500 | 2015-12-21 11:13:52 +0100 | [diff] [blame] | 1557 | .. _channel_class: |
| 1558 | |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 1559 | Channel class |
| 1560 | ============= |
| 1561 | |
| 1562 | .. js:class:: Channel |
| 1563 | |
Christopher Faulet | 5a2c661 | 2021-08-15 20:35:25 +0200 | [diff] [blame] | 1564 | **context**: action, sample-fetch, convert, filter |
| 1565 | |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 1566 | HAProxy uses two buffers for the processing of the requests. The first one is |
| 1567 | used with the request data (from the client to the server) and the second is |
| 1568 | used for the response data (from the server to the client). |
| 1569 | |
| 1570 | Each buffer contains two types of data. The first type is the incoming data |
| 1571 | waiting for a processing. The second part is the outgoing data already |
| 1572 | processed. Usually, the incoming data is processed, after it is tagged as |
| 1573 | outgoing data, and finally it is sent. The following functions provides tools |
| 1574 | for manipulating these data in a buffer. |
| 1575 | |
| 1576 | The following diagram shows where the channel class function are applied. |
| 1577 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 1578 | .. image:: _static/channel.png |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 1579 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 1580 | .. warning:: |
| 1581 | 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] | 1582 | not possible to read from the request channel in response action. |
Christopher Faulet | 0953039 | 2021-06-14 11:43:18 +0200 | [diff] [blame] | 1583 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 1584 | .. warning:: |
| 1585 | It is forbidden to alter the Channels buffer from HTTP contexts. So only |
| 1586 | :js:func:`Channel.input`, :js:func:`Channel.output`, |
| 1587 | :js:func:`Channel.may_recv`, :js:func:`Channel.is_full` and |
Aurelien DARRAGON | 53901f4 | 2022-10-13 19:49:42 +0200 | [diff] [blame] | 1588 | :js:func:`Channel.is_resp` can be called from a HTTP context. |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 1589 | |
Christopher Faulet | 5a2c661 | 2021-08-15 20:35:25 +0200 | [diff] [blame] | 1590 | All the functions provided by this class are available in the |
| 1591 | **sample-fetches**, **actions** and **filters** contexts. For **filters**, |
| 1592 | incoming data (offset and length) are relative to the filter. Some functions |
Boyang Li | 60cfe8b | 2022-05-10 18:11:00 +0000 | [diff] [blame] | 1593 | may yield, but only for **actions**. Yield is not possible for |
Christopher Faulet | 5a2c661 | 2021-08-15 20:35:25 +0200 | [diff] [blame] | 1594 | **sample-fetches**, **converters** and **filters**. |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 1595 | |
| 1596 | .. js:function:: Channel.append(channel, string) |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 1597 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 1598 | This function copies the string **string** at the end of incoming data of the |
| 1599 | channel buffer. The function returns the copied length on success or -1 if |
| 1600 | data cannot be copied. |
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 | Same that :js:func:`Channel.insert(channel, string, channel:input())`. |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 1603 | |
Thierry FOURNIER | 486f5a0 | 2015-03-16 15:13:03 +0100 | [diff] [blame] | 1604 | :param class_channel channel: The manipulated Channel. |
Aurelien DARRAGON | 53901f4 | 2022-10-13 19:49:42 +0200 | [diff] [blame] | 1605 | :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] | 1606 | :returns: an integer containing the amount of bytes copied or -1. |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 1607 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 1608 | .. js:function:: Channel.data(channel [, offset [, length]]) |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 1609 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 1610 | This function returns **length** bytes of incoming data from the channel |
| 1611 | buffer, starting at the offset **offset**. The data are not removed from the |
| 1612 | buffer. |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 1613 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 1614 | By default, if no length is provided, all incoming data found, starting at the |
| 1615 | 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] | 1616 | retrieve a maximum of data and, if called by an action, it yields if |
| 1617 | 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] | 1618 | 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] | 1619 | 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] | 1620 | 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] | 1621 | |
| 1622 | If there is no incoming data and the channel can't receive more data, a 'nil' |
| 1623 | value is returned. |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 1624 | |
Thierry FOURNIER | 486f5a0 | 2015-03-16 15:13:03 +0100 | [diff] [blame] | 1625 | :param class_channel channel: The manipulated Channel. |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 1626 | :param integer offset: *optional* The offset in incoming data to start to get |
| 1627 | data. 0 by default. May be negative to be relative to |
| 1628 | the end of incoming data. |
| 1629 | :param integer length: *optional* The expected length of data to retrieve. All |
| 1630 | incoming data by default. May be set to -1 to get a |
| 1631 | maximum of data. |
| 1632 | :returns: a string containing the data found or nil. |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 1633 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 1634 | .. js:function:: Channel.forward(channel, length) |
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 | This function forwards **length** bytes of data from the channel buffer. If |
Christopher Faulet | 5a2c661 | 2021-08-15 20:35:25 +0200 | [diff] [blame] | 1637 | the requested length exceeds the available amount of incoming data, and if |
| 1638 | called by an action, the function yields, waiting for more data to forward. It |
| 1639 | returns the amount of data forwarded. |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 1640 | |
| 1641 | :param class_channel channel: The manipulated Channel. |
| 1642 | :param integer int: The amount of data to forward. |
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.input(channel) |
| 1645 | |
Christopher Faulet | 5a2c661 | 2021-08-15 20:35:25 +0200 | [diff] [blame] | 1646 | This function returns the length of incoming data in the channel buffer. When |
| 1647 | called by a filter, this value is relative to the filter. |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 1648 | |
Thierry FOURNIER | 486f5a0 | 2015-03-16 15:13:03 +0100 | [diff] [blame] | 1649 | :param class_channel channel: The manipulated Channel. |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 1650 | :returns: an integer containing the amount of available bytes. |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 1651 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 1652 | .. js:function:: Channel.insert(channel, string [, offset]) |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 1653 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 1654 | This function copies the string **string** at the offset **offset** in |
| 1655 | incoming data of the channel buffer. The function returns the copied length on |
| 1656 | success or -1 if data cannot be copied. |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 1657 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 1658 | 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] | 1659 | 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] | 1660 | of the channel buffer while negative offset is relative to their end. |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 1661 | |
Thierry FOURNIER | 486f5a0 | 2015-03-16 15:13:03 +0100 | [diff] [blame] | 1662 | :param class_channel channel: The manipulated Channel. |
Aurelien DARRAGON | 53901f4 | 2022-10-13 19:49:42 +0200 | [diff] [blame] | 1663 | :param string string: The data to copy into incoming data. |
| 1664 | :param integer offset: *optional* The offset in incoming data where to copy |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 1665 | data. 0 by default. May be negative to be relative to |
| 1666 | the end of incoming data. |
Pieter Baauw | 4d7f766 | 2015-11-08 16:38:08 +0100 | [diff] [blame] | 1667 | :returns: an integer containing the amount of bytes copied or -1. |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 1668 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 1669 | .. js:function:: Channel.is_full(channel) |
| 1670 | |
| 1671 | This function returns true if the channel buffer is full. |
| 1672 | |
Christopher Faulet | 5a2c661 | 2021-08-15 20:35:25 +0200 | [diff] [blame] | 1673 | :param class_channel channel: The manipulated Channel. |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 1674 | :returns: a boolean |
| 1675 | |
| 1676 | .. js:function:: Channel.is_resp(channel) |
| 1677 | |
| 1678 | This function returns true if the channel is the response one. |
| 1679 | |
Christopher Faulet | 5a2c661 | 2021-08-15 20:35:25 +0200 | [diff] [blame] | 1680 | :param class_channel channel: The manipulated Channel. |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 1681 | :returns: a boolean |
| 1682 | |
| 1683 | .. js:function:: Channel.line(channel [, offset [, length]]) |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 1684 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 1685 | This function parses **length** bytes of incoming data of the channel buffer, |
| 1686 | starting at offset **offset**, and returns the first line found, including the |
Christopher Faulet | 5a2c661 | 2021-08-15 20:35:25 +0200 | [diff] [blame] | 1687 | '\\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] | 1688 | are returned. |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 1689 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 1690 | By default, if no length is provided, all incoming data, starting at the given |
| 1691 | 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] | 1692 | retrieve a maximum of data and, if called by an action, yields if |
| 1693 | 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] | 1694 | 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] | 1695 | 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] | 1696 | 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] | 1697 | |
| 1698 | If there is no incoming data and the channel can't receive more data, a 'nil' |
| 1699 | value is returned. |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 1700 | |
Thierry FOURNIER | 486f5a0 | 2015-03-16 15:13:03 +0100 | [diff] [blame] | 1701 | :param class_channel channel: The manipulated Channel. |
Aurelien DARRAGON | 53901f4 | 2022-10-13 19:49:42 +0200 | [diff] [blame] | 1702 | :param integer offset: *optional* The offset in incoming data to start to |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 1703 | parse data. 0 by default. May be negative to be |
| 1704 | relative to the end of incoming data. |
| 1705 | :param integer length: *optional* The length of data to parse. All incoming |
| 1706 | data by default. May be set to -1 to get a maximum of |
| 1707 | data. |
| 1708 | :returns: a string containing the line found or nil. |
| 1709 | |
| 1710 | .. js:function:: Channel.may_recv(channel) |
| 1711 | |
| 1712 | This function returns true if the channel may still receive data. |
| 1713 | |
Christopher Faulet | 5a2c661 | 2021-08-15 20:35:25 +0200 | [diff] [blame] | 1714 | :param class_channel channel: The manipulated Channel. |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 1715 | :returns: a boolean |
| 1716 | |
| 1717 | .. js:function:: Channel.output(channel) |
| 1718 | |
Christopher Faulet | 5a2c661 | 2021-08-15 20:35:25 +0200 | [diff] [blame] | 1719 | This function returns the length of outgoing data of the channel buffer. When |
| 1720 | called by a filter, this value is relative to the filter. |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 1721 | |
| 1722 | :param class_channel channel: The manipulated Channel. |
| 1723 | :returns: an integer containing the amount of available bytes. |
| 1724 | |
| 1725 | .. js:function:: Channel.prepend(channel, string) |
| 1726 | |
| 1727 | This function copies the string **string** in front of incoming data of the |
| 1728 | channel buffer. The function returns the copied length on success or -1 if |
| 1729 | data cannot be copied. |
| 1730 | |
| 1731 | Same that :js:func:`Channel.insert(channel, string, 0)`. |
| 1732 | |
| 1733 | :param class_channel channel: The manipulated Channel. |
Aurelien DARRAGON | 53901f4 | 2022-10-13 19:49:42 +0200 | [diff] [blame] | 1734 | :param string string: The data to copy in front of incoming data. |
Pieter Baauw | 4d7f766 | 2015-11-08 16:38:08 +0100 | [diff] [blame] | 1735 | :returns: an integer containing the amount of bytes copied or -1. |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 1736 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 1737 | .. js:function:: Channel.remove(channel [, offset [, length]]) |
| 1738 | |
| 1739 | This function removes **length** bytes of incoming data of the channel buffer, |
| 1740 | starting at offset **offset**. This function returns number of bytes removed |
| 1741 | on success. |
| 1742 | |
| 1743 | 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] | 1744 | 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] | 1745 | 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] | 1746 | channel buffer while negative offset is relative to the end. |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 1747 | |
| 1748 | :param class_channel channel: The manipulated Channel. |
Aurelien DARRAGON | 53901f4 | 2022-10-13 19:49:42 +0200 | [diff] [blame] | 1749 | :param integer offset: *optional* The offset in incoming data where to start |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 1750 | to remove data. 0 by default. May be negative to |
| 1751 | be relative to the end of incoming data. |
| 1752 | :param integer length: *optional* The length of data to remove. All incoming |
| 1753 | data by default. |
| 1754 | :returns: an integer containing the amount of bytes removed. |
| 1755 | |
Thierry FOURNIER | 486f5a0 | 2015-03-16 15:13:03 +0100 | [diff] [blame] | 1756 | .. js:function:: Channel.send(channel, string) |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 1757 | |
Christopher Faulet | 5a2c661 | 2021-08-15 20:35:25 +0200 | [diff] [blame] | 1758 | This function requires immediate send of the string **string**. It means the |
Ilya Shipitsin | ff0f278 | 2021-08-22 22:18:07 +0500 | [diff] [blame] | 1759 | 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] | 1760 | 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] | 1761 | action, this function yields to copy and forward all the string. |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 1762 | |
Thierry FOURNIER | 486f5a0 | 2015-03-16 15:13:03 +0100 | [diff] [blame] | 1763 | :param class_channel channel: The manipulated Channel. |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 1764 | :param string string: The data to send. |
Pieter Baauw | 4d7f766 | 2015-11-08 16:38:08 +0100 | [diff] [blame] | 1765 | :returns: an integer containing the amount of bytes copied or -1. |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 1766 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 1767 | .. js:function:: Channel.set(channel, string [, offset [, length]]) |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 1768 | |
Aurelien DARRAGON | 53901f4 | 2022-10-13 19:49:42 +0200 | [diff] [blame] | 1769 | This function replaces **length** bytes of incoming data of the channel buffer, |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 1770 | starting at offset **offset**, by the string **string**. The function returns |
| 1771 | the copied length on success or -1 if data cannot be copied. |
| 1772 | |
| 1773 | 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] | 1774 | 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] | 1775 | 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] | 1776 | channel buffer while negative offset is relative to the end. |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 1777 | |
Thierry FOURNIER | 486f5a0 | 2015-03-16 15:13:03 +0100 | [diff] [blame] | 1778 | :param class_channel channel: The manipulated Channel. |
Aurelien DARRAGON | 53901f4 | 2022-10-13 19:49:42 +0200 | [diff] [blame] | 1779 | :param string string: The data to copy into incoming data. |
| 1780 | :param integer offset: *optional* The offset in incoming data where to start |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 1781 | the data replacement. 0 by default. May be negative to |
| 1782 | be relative to the end of incoming data. |
| 1783 | :param integer length: *optional* The length of data to replace. All incoming |
| 1784 | data by default. |
| 1785 | :returns: an integer containing the amount of bytes copied or -1. |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 1786 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 1787 | .. js:function:: Channel.dup(channel) |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 1788 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 1789 | **DEPRECATED** |
| 1790 | |
| 1791 | 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] | 1792 | are not removed from the buffer and can be reprocessed later. |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 1793 | |
| 1794 | If there is no incoming data and the channel can't receive more data, a 'nil' |
| 1795 | value is returned. |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 1796 | |
Thierry FOURNIER | 486f5a0 | 2015-03-16 15:13:03 +0100 | [diff] [blame] | 1797 | :param class_channel channel: The manipulated Channel. |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 1798 | :returns: a string containing all data found or nil. |
| 1799 | |
| 1800 | .. warning:: |
| 1801 | This function is deprecated. :js:func:`Channel.data()` must be used |
| 1802 | instead. |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 1803 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 1804 | .. js:function:: Channel.get(channel) |
| 1805 | |
| 1806 | **DEPRECATED** |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 1807 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 1808 | This function returns all incoming data found in the channel buffer and remove |
| 1809 | them from the buffer. |
| 1810 | |
| 1811 | If there is no incoming data and the channel can't receive more data, a 'nil' |
| 1812 | value is returned. |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 1813 | |
Thierry FOURNIER | 486f5a0 | 2015-03-16 15:13:03 +0100 | [diff] [blame] | 1814 | :param class_channel channel: The manipulated Channel. |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 1815 | :returns: a string containing all the data found or nil. |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 1816 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 1817 | .. warning:: |
| 1818 | This function is deprecated. :js:func:`Channel.data()` must be used to |
| 1819 | retrieve data followed by a call to :js:func:`Channel:remove()` to remove |
| 1820 | data. |
Thierry FOURNIER / OZON.IO | 65192f3 | 2016-11-07 15:28:40 +0100 | [diff] [blame] | 1821 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 1822 | .. code-block:: lua |
Thierry FOURNIER / OZON.IO | 65192f3 | 2016-11-07 15:28:40 +0100 | [diff] [blame] | 1823 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 1824 | local data = chn:data() |
| 1825 | chn:remove(0, data:len()) |
| 1826 | |
| 1827 | .. |
| 1828 | |
| 1829 | .. js:function:: Channel.getline(channel) |
| 1830 | |
| 1831 | **DEPRECATED** |
| 1832 | |
| 1833 | 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] | 1834 | buffer, including the '\\n'. The returned data are removed from the buffer. If |
| 1835 | no line is found, and if called by an action, this function yields to wait for |
| 1836 | more data, except if the channel can't receive more data. In this case all |
| 1837 | data are returned. |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 1838 | |
| 1839 | If there is no incoming data and the channel can't receive more data, a 'nil' |
| 1840 | value is returned. |
| 1841 | |
| 1842 | :param class_channel channel: The manipulated Channel. |
| 1843 | :returns: a string containing the line found or nil. |
| 1844 | |
| 1845 | .. warning:: |
Boyang Li | 60cfe8b | 2022-05-10 18:11:00 +0000 | [diff] [blame] | 1846 | This function is deprecated. :js:func:`Channel.line()` must be used to |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 1847 | retrieve a line followed by a call to :js:func:`Channel:remove()` to remove |
| 1848 | data. |
| 1849 | |
| 1850 | .. code-block:: lua |
| 1851 | |
| 1852 | local line = chn:line(0, -1) |
| 1853 | chn:remove(0, line:len()) |
| 1854 | |
| 1855 | .. |
| 1856 | |
| 1857 | .. js:function:: Channel.get_in_len(channel) |
| 1858 | |
Boyang Li | 60cfe8b | 2022-05-10 18:11:00 +0000 | [diff] [blame] | 1859 | **DEPRECATED** |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 1860 | |
Christopher Faulet | 5a2c661 | 2021-08-15 20:35:25 +0200 | [diff] [blame] | 1861 | This function returns the length of the input part of the buffer. When called |
| 1862 | by a filter, this value is relative to the filter. |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 1863 | |
| 1864 | :param class_channel channel: The manipulated Channel. |
| 1865 | :returns: an integer containing the amount of available bytes. |
| 1866 | |
| 1867 | .. warning:: |
| 1868 | This function is deprecated. :js:func:`Channel.input()` must be used |
| 1869 | instead. |
| 1870 | |
| 1871 | .. js:function:: Channel.get_out_len(channel) |
| 1872 | |
Boyang Li | 60cfe8b | 2022-05-10 18:11:00 +0000 | [diff] [blame] | 1873 | **DEPRECATED** |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 1874 | |
Christopher Faulet | 5a2c661 | 2021-08-15 20:35:25 +0200 | [diff] [blame] | 1875 | This function returns the length of the output part of the buffer. When called |
| 1876 | by a filter, this value is relative to the filter. |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 1877 | |
| 1878 | :param class_channel channel: The manipulated Channel. |
| 1879 | :returns: an integer containing the amount of available bytes. |
| 1880 | |
| 1881 | .. warning:: |
| 1882 | This function is deprecated. :js:func:`Channel.output()` must be used |
| 1883 | instead. |
Thierry FOURNIER | dc59500 | 2015-12-21 11:13:52 +0100 | [diff] [blame] | 1884 | |
| 1885 | .. _http_class: |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 1886 | |
| 1887 | HTTP class |
| 1888 | ========== |
| 1889 | |
| 1890 | .. js:class:: HTTP |
| 1891 | |
| 1892 | This class contain all the HTTP manipulation functions. |
| 1893 | |
Pieter Baauw | 386a127 | 2015-08-16 15:26:24 +0200 | [diff] [blame] | 1894 | .. js:function:: HTTP.req_get_headers(http) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 1895 | |
Patrick Hemmer | c6a1d71 | 2018-05-01 21:30:41 -0400 | [diff] [blame] | 1896 | Returns a table containing all the request headers. |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 1897 | |
| 1898 | :param class_http http: The related http object. |
Patrick Hemmer | c6a1d71 | 2018-05-01 21:30:41 -0400 | [diff] [blame] | 1899 | :returns: table of headers. |
Thierry FOURNIER | 12a865d | 2016-12-14 19:40:37 +0100 | [diff] [blame] | 1900 | :see: :js:func:`HTTP.res_get_headers` |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 1901 | |
Patrick Hemmer | c6a1d71 | 2018-05-01 21:30:41 -0400 | [diff] [blame] | 1902 | This is the form of the returned table: |
Thierry FOURNIER | dc59500 | 2015-12-21 11:13:52 +0100 | [diff] [blame] | 1903 | |
| 1904 | .. code-block:: lua |
| 1905 | |
| 1906 | HTTP:req_get_headers()['<header-name>'][<header-index>] = "<header-value>" |
| 1907 | |
| 1908 | local hdr = HTTP:req_get_headers() |
| 1909 | hdr["host"][0] = "www.test.com" |
| 1910 | hdr["accept"][0] = "audio/basic q=1" |
| 1911 | hdr["accept"][1] = "audio/*, q=0.2" |
| 1912 | hdr["accept"][2] = "*/*, q=0.1" |
| 1913 | .. |
| 1914 | |
Pieter Baauw | 386a127 | 2015-08-16 15:26:24 +0200 | [diff] [blame] | 1915 | .. js:function:: HTTP.res_get_headers(http) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 1916 | |
Patrick Hemmer | c6a1d71 | 2018-05-01 21:30:41 -0400 | [diff] [blame] | 1917 | Returns a table containing all the response headers. |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 1918 | |
| 1919 | :param class_http http: The related http object. |
Patrick Hemmer | c6a1d71 | 2018-05-01 21:30:41 -0400 | [diff] [blame] | 1920 | :returns: table of headers. |
Thierry FOURNIER | 12a865d | 2016-12-14 19:40:37 +0100 | [diff] [blame] | 1921 | :see: :js:func:`HTTP.req_get_headers` |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 1922 | |
Patrick Hemmer | c6a1d71 | 2018-05-01 21:30:41 -0400 | [diff] [blame] | 1923 | This is the form of the returned table: |
Thierry FOURNIER | dc59500 | 2015-12-21 11:13:52 +0100 | [diff] [blame] | 1924 | |
| 1925 | .. code-block:: lua |
| 1926 | |
| 1927 | HTTP:res_get_headers()['<header-name>'][<header-index>] = "<header-value>" |
| 1928 | |
| 1929 | local hdr = HTTP:req_get_headers() |
| 1930 | hdr["host"][0] = "www.test.com" |
| 1931 | hdr["accept"][0] = "audio/basic q=1" |
| 1932 | hdr["accept"][1] = "audio/*, q=0.2" |
| 1933 | hdr["accept"][2] = "*.*, q=0.1" |
| 1934 | .. |
| 1935 | |
Thierry FOURNIER | 486f5a0 | 2015-03-16 15:13:03 +0100 | [diff] [blame] | 1936 | .. js:function:: HTTP.req_add_header(http, name, value) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 1937 | |
Aurelien DARRAGON | 53901f4 | 2022-10-13 19:49:42 +0200 | [diff] [blame] | 1938 | Appends a HTTP header field in the request whose name is |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 1939 | specified in "name" and whose value is defined in "value". |
| 1940 | |
| 1941 | :param class_http http: The related http object. |
| 1942 | :param string name: The header name. |
| 1943 | :param string value: The header value. |
Thierry FOURNIER | 12a865d | 2016-12-14 19:40:37 +0100 | [diff] [blame] | 1944 | :see: :js:func:`HTTP.res_add_header` |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 1945 | |
Thierry FOURNIER | 486f5a0 | 2015-03-16 15:13:03 +0100 | [diff] [blame] | 1946 | .. js:function:: HTTP.res_add_header(http, name, value) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 1947 | |
Aurelien DARRAGON | 53901f4 | 2022-10-13 19:49:42 +0200 | [diff] [blame] | 1948 | Appends a HTTP header field in the response whose name is |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 1949 | specified in "name" and whose value is defined in "value". |
| 1950 | |
| 1951 | :param class_http http: The related http object. |
| 1952 | :param string name: The header name. |
| 1953 | :param string value: The header value. |
Thierry FOURNIER | 12a865d | 2016-12-14 19:40:37 +0100 | [diff] [blame] | 1954 | :see: :js:func:`HTTP.req_add_header` |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 1955 | |
Thierry FOURNIER | 486f5a0 | 2015-03-16 15:13:03 +0100 | [diff] [blame] | 1956 | .. js:function:: HTTP.req_del_header(http, name) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 1957 | |
| 1958 | Removes all HTTP header fields in the request whose name is |
| 1959 | specified in "name". |
| 1960 | |
| 1961 | :param class_http http: The related http object. |
| 1962 | :param string name: The header name. |
Thierry FOURNIER | 12a865d | 2016-12-14 19:40:37 +0100 | [diff] [blame] | 1963 | :see: :js:func:`HTTP.res_del_header` |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 1964 | |
Thierry FOURNIER | 486f5a0 | 2015-03-16 15:13:03 +0100 | [diff] [blame] | 1965 | .. js:function:: HTTP.res_del_header(http, name) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 1966 | |
| 1967 | Removes all HTTP header fields in the response whose name is |
| 1968 | specified in "name". |
| 1969 | |
| 1970 | :param class_http http: The related http object. |
| 1971 | :param string name: The header name. |
Thierry FOURNIER | 12a865d | 2016-12-14 19:40:37 +0100 | [diff] [blame] | 1972 | :see: :js:func:`HTTP.req_del_header` |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 1973 | |
Thierry FOURNIER | 486f5a0 | 2015-03-16 15:13:03 +0100 | [diff] [blame] | 1974 | .. js:function:: HTTP.req_set_header(http, name, value) |
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 variable replace all occurrence of all header "name", by only |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 1977 | one containing the "value". |
| 1978 | |
| 1979 | :param class_http http: The related http object. |
| 1980 | :param string name: The header name. |
| 1981 | :param string value: The header value. |
Thierry FOURNIER | 12a865d | 2016-12-14 19:40:37 +0100 | [diff] [blame] | 1982 | :see: :js:func:`HTTP.res_set_header` |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 1983 | |
Bertrand Jacquin | 874a35c | 2018-09-10 21:26:07 +0100 | [diff] [blame] | 1984 | This function does the same work as the following code: |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 1985 | |
| 1986 | .. code-block:: lua |
| 1987 | |
| 1988 | function fcn(txn) |
Thierry FOURNIER | 486f5a0 | 2015-03-16 15:13:03 +0100 | [diff] [blame] | 1989 | TXN.http:req_del_header("header") |
| 1990 | TXN.http:req_add_header("header", "value") |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 1991 | end |
| 1992 | .. |
| 1993 | |
Thierry FOURNIER | 486f5a0 | 2015-03-16 15:13:03 +0100 | [diff] [blame] | 1994 | .. js:function:: HTTP.res_set_header(http, name, value) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 1995 | |
Aurelien DARRAGON | 53901f4 | 2022-10-13 19:49:42 +0200 | [diff] [blame] | 1996 | This function replaces all occurrence of all header "name", by only |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 1997 | one containing the "value". |
| 1998 | |
| 1999 | :param class_http http: The related http object. |
| 2000 | :param string name: The header name. |
| 2001 | :param string value: The header value. |
Thierry FOURNIER | 12a865d | 2016-12-14 19:40:37 +0100 | [diff] [blame] | 2002 | :see: :js:func:`HTTP.req_rep_header()` |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 2003 | |
Pieter Baauw | 386a127 | 2015-08-16 15:26:24 +0200 | [diff] [blame] | 2004 | .. js:function:: HTTP.req_rep_header(http, name, regex, replace) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 2005 | |
| 2006 | Matches the regular expression in all occurrences of header field "name" |
| 2007 | according to "regex", and replaces them with the "replace" argument. The |
| 2008 | replacement value can contain back references like \1, \2, ... This |
| 2009 | function works with the request. |
| 2010 | |
| 2011 | :param class_http http: The related http object. |
| 2012 | :param string name: The header name. |
| 2013 | :param string regex: The match regular expression. |
| 2014 | :param string replace: The replacement value. |
Thierry FOURNIER | 12a865d | 2016-12-14 19:40:37 +0100 | [diff] [blame] | 2015 | :see: :js:func:`HTTP.res_rep_header()` |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 2016 | |
Pieter Baauw | 386a127 | 2015-08-16 15:26:24 +0200 | [diff] [blame] | 2017 | .. js:function:: HTTP.res_rep_header(http, name, regex, string) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 2018 | |
| 2019 | Matches the regular expression in all occurrences of header field "name" |
| 2020 | according to "regex", and replaces them with the "replace" argument. The |
| 2021 | replacement value can contain back references like \1, \2, ... This |
| 2022 | function works with the request. |
| 2023 | |
| 2024 | :param class_http http: The related http object. |
| 2025 | :param string name: The header name. |
| 2026 | :param string regex: The match regular expression. |
| 2027 | :param string replace: The replacement value. |
Thierry FOURNIER | 12a865d | 2016-12-14 19:40:37 +0100 | [diff] [blame] | 2028 | :see: :js:func:`HTTP.req_rep_header()` |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 2029 | |
Thierry FOURNIER | 486f5a0 | 2015-03-16 15:13:03 +0100 | [diff] [blame] | 2030 | .. js:function:: HTTP.req_set_method(http, method) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 2031 | |
| 2032 | Rewrites the request method with the parameter "method". |
| 2033 | |
| 2034 | :param class_http http: The related http object. |
| 2035 | :param string method: The new method. |
| 2036 | |
Thierry FOURNIER | 486f5a0 | 2015-03-16 15:13:03 +0100 | [diff] [blame] | 2037 | .. js:function:: HTTP.req_set_path(http, path) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 2038 | |
| 2039 | Rewrites the request path with the "path" parameter. |
| 2040 | |
| 2041 | :param class_http http: The related http object. |
| 2042 | :param string path: The new path. |
| 2043 | |
Thierry FOURNIER | 486f5a0 | 2015-03-16 15:13:03 +0100 | [diff] [blame] | 2044 | .. js:function:: HTTP.req_set_query(http, query) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 2045 | |
| 2046 | Rewrites the request's query string which appears after the first question |
| 2047 | mark ("?") with the parameter "query". |
| 2048 | |
| 2049 | :param class_http http: The related http object. |
| 2050 | :param string query: The new query. |
| 2051 | |
Thierry FOURNIER | 0d79cf6 | 2015-08-26 14:20:58 +0200 | [diff] [blame] | 2052 | .. js:function:: HTTP.req_set_uri(http, uri) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 2053 | |
| 2054 | Rewrites the request URI with the parameter "uri". |
| 2055 | |
| 2056 | :param class_http http: The related http object. |
| 2057 | :param string uri: The new uri. |
| 2058 | |
Robin H. Johnson | 52f5db2 | 2017-01-01 13:10:52 -0800 | [diff] [blame] | 2059 | .. js:function:: HTTP.res_set_status(http, status [, reason]) |
Thierry FOURNIER | 35d70ef | 2015-08-26 16:21:56 +0200 | [diff] [blame] | 2060 | |
Robin H. Johnson | 52f5db2 | 2017-01-01 13:10:52 -0800 | [diff] [blame] | 2061 | Rewrites the response status code with the parameter "code". |
| 2062 | |
| 2063 | 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] | 2064 | |
| 2065 | :param class_http http: The related http object. |
| 2066 | :param integer status: The new response status code. |
Robin H. Johnson | 52f5db2 | 2017-01-01 13:10:52 -0800 | [diff] [blame] | 2067 | :param string reason: The new response reason (optional). |
Thierry FOURNIER | 35d70ef | 2015-08-26 16:21:56 +0200 | [diff] [blame] | 2068 | |
William Lallemand | 00a1502 | 2021-11-19 16:02:44 +0100 | [diff] [blame] | 2069 | .. _httpclient_class: |
| 2070 | |
| 2071 | HTTPClient class |
| 2072 | ================ |
| 2073 | |
| 2074 | .. js:class:: HTTPClient |
| 2075 | |
| 2076 | The httpclient class allows issue of outbound HTTP requests through a simple |
| 2077 | API without the knowledge of HAProxy internals. |
| 2078 | |
| 2079 | .. js:function:: HTTPClient.get(httpclient, request) |
| 2080 | .. js:function:: HTTPClient.head(httpclient, request) |
| 2081 | .. js:function:: HTTPClient.put(httpclient, request) |
| 2082 | .. js:function:: HTTPClient.post(httpclient, request) |
| 2083 | .. js:function:: HTTPClient.delete(httpclient, request) |
| 2084 | |
Aurelien DARRAGON | 53901f4 | 2022-10-13 19:49:42 +0200 | [diff] [blame] | 2085 | Send a HTTP request and wait for a response. GET, HEAD PUT, POST and DELETE methods can be used. |
| 2086 | 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] | 2087 | |
William Lallemand | a925619 | 2022-10-21 11:48:24 +0200 | [diff] [blame] | 2088 | The HTTPClient interface is not able to decompress responses, it is not |
| 2089 | recommended to send an Accept-Encoding in the request so the response is |
| 2090 | received uncompressed. |
William Lallemand | 00a1502 | 2021-11-19 16:02:44 +0100 | [diff] [blame] | 2091 | |
| 2092 | :param class httpclient: Is the manipulated HTTPClient. |
| 2093 | :param table request: Is a table containing the parameters of the request that will be send. |
| 2094 | :param string request.url: Is a mandatory parameter for the request that contains the URL. |
| 2095 | :param string request.body: Is an optional parameter for the request that contains the body to send. |
| 2096 | :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] | 2097 | :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] | 2098 | :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] | 2099 | :returns: Lua table containing the response |
| 2100 | |
| 2101 | |
| 2102 | .. code-block:: lua |
| 2103 | |
| 2104 | local httpclient = core.httpclient() |
William Lallemand | 4f4f2b7 | 2022-02-17 20:00:23 +0100 | [diff] [blame] | 2105 | 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] | 2106 | |
| 2107 | .. |
| 2108 | |
| 2109 | .. code-block:: lua |
| 2110 | |
| 2111 | response = { |
| 2112 | status = 400, |
| 2113 | reason = "Bad request", |
| 2114 | headers = { |
| 2115 | ["content-type"] = { "text/html" }, |
| 2116 | ["cache-control"] = { "no-cache", "no-store" }, |
| 2117 | }, |
William Lallemand | 4f4f2b7 | 2022-02-17 20:00:23 +0100 | [diff] [blame] | 2118 | body = "<html><body><h1>invalid request<h1></body></html>", |
William Lallemand | 00a1502 | 2021-11-19 16:02:44 +0100 | [diff] [blame] | 2119 | } |
| 2120 | .. |
| 2121 | |
| 2122 | |
Thierry FOURNIER | dc59500 | 2015-12-21 11:13:52 +0100 | [diff] [blame] | 2123 | .. _txn_class: |
| 2124 | |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 2125 | TXN class |
| 2126 | ========= |
| 2127 | |
| 2128 | .. js:class:: TXN |
| 2129 | |
| 2130 | The txn class contain all the functions relative to the http or tcp |
| 2131 | 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] | 2132 | a HTTP transaction is not the same than a tcp stream). |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 2133 | |
| 2134 | The usage of this class permits to retrieve data from the requests, alter it |
| 2135 | and forward it. |
| 2136 | |
| 2137 | All the functions provided by this class are available in the context |
Christopher Faulet | 5a2c661 | 2021-08-15 20:35:25 +0200 | [diff] [blame] | 2138 | **sample-fetches**, **actions** and **filters**. |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 2139 | |
Thierry FOURNIER | 486f5a0 | 2015-03-16 15:13:03 +0100 | [diff] [blame] | 2140 | .. js:attribute:: TXN.c |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 2141 | |
Thierry FOURNIER | dc59500 | 2015-12-21 11:13:52 +0100 | [diff] [blame] | 2142 | :returns: An :ref:`converters_class`. |
| 2143 | |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 2144 | This attribute contains a Converters class object. |
| 2145 | |
Thierry FOURNIER | 486f5a0 | 2015-03-16 15:13:03 +0100 | [diff] [blame] | 2146 | .. js:attribute:: TXN.sc |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 2147 | |
Thierry FOURNIER | dc59500 | 2015-12-21 11:13:52 +0100 | [diff] [blame] | 2148 | :returns: An :ref:`converters_class`. |
| 2149 | |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 2150 | This attribute contains a Converters class object. The functions of |
| 2151 | this object returns always a string. |
| 2152 | |
Thierry FOURNIER | 486f5a0 | 2015-03-16 15:13:03 +0100 | [diff] [blame] | 2153 | .. js:attribute:: TXN.f |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 2154 | |
Thierry FOURNIER | dc59500 | 2015-12-21 11:13:52 +0100 | [diff] [blame] | 2155 | :returns: An :ref:`fetches_class`. |
| 2156 | |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 2157 | This attribute contains a Fetches class object. |
| 2158 | |
Thierry FOURNIER | 486f5a0 | 2015-03-16 15:13:03 +0100 | [diff] [blame] | 2159 | .. js:attribute:: TXN.sf |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 2160 | |
Thierry FOURNIER | dc59500 | 2015-12-21 11:13:52 +0100 | [diff] [blame] | 2161 | :returns: An :ref:`fetches_class`. |
| 2162 | |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 2163 | This attribute contains a Fetches class object. The functions of |
| 2164 | this object returns always a string. |
| 2165 | |
Thierry FOURNIER | 486f5a0 | 2015-03-16 15:13:03 +0100 | [diff] [blame] | 2166 | .. js:attribute:: TXN.req |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 2167 | |
Thierry FOURNIER | dc59500 | 2015-12-21 11:13:52 +0100 | [diff] [blame] | 2168 | :returns: An :ref:`channel_class`. |
| 2169 | |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 2170 | This attribute contains a channel class object for the request buffer. |
| 2171 | |
Thierry FOURNIER | 486f5a0 | 2015-03-16 15:13:03 +0100 | [diff] [blame] | 2172 | .. js:attribute:: TXN.res |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 2173 | |
Thierry FOURNIER | dc59500 | 2015-12-21 11:13:52 +0100 | [diff] [blame] | 2174 | :returns: An :ref:`channel_class`. |
| 2175 | |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 2176 | This attribute contains a channel class object for the response buffer. |
| 2177 | |
Thierry FOURNIER | 486f5a0 | 2015-03-16 15:13:03 +0100 | [diff] [blame] | 2178 | .. js:attribute:: TXN.http |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 2179 | |
Thierry FOURNIER | dc59500 | 2015-12-21 11:13:52 +0100 | [diff] [blame] | 2180 | :returns: An :ref:`http_class`. |
| 2181 | |
Aurelien DARRAGON | 53901f4 | 2022-10-13 19:49:42 +0200 | [diff] [blame] | 2182 | 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] | 2183 | proxy has the "mode http" enabled. |
| 2184 | |
Christopher Faulet | 5a2c661 | 2021-08-15 20:35:25 +0200 | [diff] [blame] | 2185 | .. js:attribute:: TXN.http_req |
| 2186 | |
| 2187 | :returns: An :ref:`httpmessage_class`. |
| 2188 | |
| 2189 | This attribute contains the request HTTPMessage class object. It is available |
| 2190 | only if the proxy has the "mode http" enabled and only in the **filters** |
| 2191 | context. |
| 2192 | |
| 2193 | .. js:attribute:: TXN.http_res |
| 2194 | |
| 2195 | :returns: An :ref:`httpmessage_class`. |
| 2196 | |
| 2197 | This attribute contains the response HTTPMessage class object. It is available |
| 2198 | only if the proxy has the "mode http" enabled and only in the **filters** |
| 2199 | context. |
| 2200 | |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 2201 | .. js:function:: TXN.log(TXN, loglevel, msg) |
| 2202 | |
| 2203 | This function sends a log. The log is sent, according with the HAProxy |
| 2204 | configuration file, on the default syslog server if it is configured and on |
| 2205 | the stderr if it is allowed. |
| 2206 | |
| 2207 | :param class_txn txn: The class txn object containing the data. |
Bertrand Jacquin | 874a35c | 2018-09-10 21:26:07 +0100 | [diff] [blame] | 2208 | :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] | 2209 | number between 0 and 7. |
| 2210 | :param string msg: The log content. |
Thierry FOURNIER | 12a865d | 2016-12-14 19:40:37 +0100 | [diff] [blame] | 2211 | :see: :js:attr:`core.emerg`, :js:attr:`core.alert`, :js:attr:`core.crit`, |
| 2212 | :js:attr:`core.err`, :js:attr:`core.warning`, :js:attr:`core.notice`, |
| 2213 | :js:attr:`core.info`, :js:attr:`core.debug` (log level definitions) |
| 2214 | :see: :js:func:`TXN.deflog` |
| 2215 | :see: :js:func:`TXN.Debug` |
| 2216 | :see: :js:func:`TXN.Info` |
| 2217 | :see: :js:func:`TXN.Warning` |
| 2218 | :see: :js:func:`TXN.Alert` |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 2219 | |
| 2220 | .. js:function:: TXN.deflog(TXN, msg) |
| 2221 | |
Bertrand Jacquin | 874a35c | 2018-09-10 21:26:07 +0100 | [diff] [blame] | 2222 | 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] | 2223 | transaction. |
| 2224 | |
| 2225 | :param class_txn txn: The class txn object containing the data. |
| 2226 | :param string msg: The log content. |
Aurelien DARRAGON | 53901f4 | 2022-10-13 19:49:42 +0200 | [diff] [blame] | 2227 | :see: :js:func:`TXN.log` |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 2228 | |
| 2229 | .. js:function:: TXN.Debug(txn, msg) |
| 2230 | |
| 2231 | :param class_txn txn: The class txn object containing the data. |
| 2232 | :param string msg: The log content. |
Thierry FOURNIER | 12a865d | 2016-12-14 19:40:37 +0100 | [diff] [blame] | 2233 | :see: :js:func:`TXN.log` |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 2234 | |
Aurelien DARRAGON | 53901f4 | 2022-10-13 19:49:42 +0200 | [diff] [blame] | 2235 | Does the same job as: |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 2236 | |
| 2237 | .. code-block:: lua |
| 2238 | |
Thierry FOURNIER | dc59500 | 2015-12-21 11:13:52 +0100 | [diff] [blame] | 2239 | function Debug(txn, msg) |
| 2240 | TXN.log(txn, core.debug, msg) |
| 2241 | end |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 2242 | .. |
| 2243 | |
| 2244 | .. js:function:: TXN.Info(txn, msg) |
| 2245 | |
| 2246 | :param class_txn txn: The class txn object containing the data. |
| 2247 | :param string msg: The log content. |
Thierry FOURNIER | 12a865d | 2016-12-14 19:40:37 +0100 | [diff] [blame] | 2248 | :see: :js:func:`TXN.log` |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 2249 | |
Aurelien DARRAGON | 53901f4 | 2022-10-13 19:49:42 +0200 | [diff] [blame] | 2250 | Does the same job as: |
| 2251 | |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 2252 | .. code-block:: lua |
| 2253 | |
Aurelien DARRAGON | 53901f4 | 2022-10-13 19:49:42 +0200 | [diff] [blame] | 2254 | function Info(txn, msg) |
Thierry FOURNIER | dc59500 | 2015-12-21 11:13:52 +0100 | [diff] [blame] | 2255 | TXN.log(txn, core.info, msg) |
| 2256 | end |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 2257 | .. |
| 2258 | |
| 2259 | .. js:function:: TXN.Warning(txn, msg) |
| 2260 | |
| 2261 | :param class_txn txn: The class txn object containing the data. |
| 2262 | :param string msg: The log content. |
Thierry FOURNIER | 12a865d | 2016-12-14 19:40:37 +0100 | [diff] [blame] | 2263 | :see: :js:func:`TXN.log` |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 2264 | |
Aurelien DARRAGON | 53901f4 | 2022-10-13 19:49:42 +0200 | [diff] [blame] | 2265 | Does the same job as: |
| 2266 | |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 2267 | .. code-block:: lua |
| 2268 | |
Aurelien DARRAGON | 53901f4 | 2022-10-13 19:49:42 +0200 | [diff] [blame] | 2269 | function Warning(txn, msg) |
Thierry FOURNIER | dc59500 | 2015-12-21 11:13:52 +0100 | [diff] [blame] | 2270 | TXN.log(txn, core.warning, msg) |
| 2271 | end |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 2272 | .. |
| 2273 | |
| 2274 | .. js:function:: TXN.Alert(txn, msg) |
| 2275 | |
| 2276 | :param class_txn txn: The class txn object containing the data. |
| 2277 | :param string msg: The log content. |
Thierry FOURNIER | 12a865d | 2016-12-14 19:40:37 +0100 | [diff] [blame] | 2278 | :see: :js:func:`TXN.log` |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 2279 | |
Aurelien DARRAGON | 53901f4 | 2022-10-13 19:49:42 +0200 | [diff] [blame] | 2280 | Does the same job as: |
| 2281 | |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 2282 | .. code-block:: lua |
| 2283 | |
Aurelien DARRAGON | 53901f4 | 2022-10-13 19:49:42 +0200 | [diff] [blame] | 2284 | function Alert(txn, msg) |
Thierry FOURNIER | dc59500 | 2015-12-21 11:13:52 +0100 | [diff] [blame] | 2285 | TXN.log(txn, core.alert, msg) |
| 2286 | end |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 2287 | .. |
| 2288 | |
Thierry FOURNIER | 486f5a0 | 2015-03-16 15:13:03 +0100 | [diff] [blame] | 2289 | .. js:function:: TXN.get_priv(txn) |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 2290 | |
Thierry FOURNIER | 486f5a0 | 2015-03-16 15:13:03 +0100 | [diff] [blame] | 2291 | 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] | 2292 | function. If no data are stored, it returns a nil value. |
| 2293 | |
| 2294 | :param class_txn txn: The class txn object containing the data. |
Bertrand Jacquin | 874a35c | 2018-09-10 21:26:07 +0100 | [diff] [blame] | 2295 | :returns: the opaque data previously stored, or nil if nothing is |
| 2296 | available. |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 2297 | |
Thierry FOURNIER | 486f5a0 | 2015-03-16 15:13:03 +0100 | [diff] [blame] | 2298 | .. js:function:: TXN.set_priv(txn, data) |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 2299 | |
Aurelien DARRAGON | 53901f4 | 2022-10-13 19:49:42 +0200 | [diff] [blame] | 2300 | Store any data in the current HAProxy transaction. This action replaces the |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 2301 | old stored data. |
| 2302 | |
| 2303 | :param class_txn txn: The class txn object containing the data. |
| 2304 | :param opaque data: The data which is stored in the transaction. |
| 2305 | |
Tim Duesterhus | 4e172c9 | 2020-05-19 13:49:42 +0200 | [diff] [blame] | 2306 | .. js:function:: TXN.set_var(TXN, var, value[, ifexist]) |
Thierry FOURNIER | 053ba8ad | 2015-06-08 13:05:33 +0200 | [diff] [blame] | 2307 | |
David Carlier | 61fdf8b | 2015-10-02 11:59:38 +0100 | [diff] [blame] | 2308 | 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] | 2309 | |
| 2310 | :param class_txn txn: The class txn object containing the data. |
| 2311 | :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] | 2312 | :param type value: The value associated to the variable. The type can be string or |
| 2313 | integer. |
Aurelien DARRAGON | 53901f4 | 2022-10-13 19:49:42 +0200 | [diff] [blame] | 2314 | :param boolean ifexist: If this parameter is set to true the variable |
Tim Duesterhus | 4e172c9 | 2020-05-19 13:49:42 +0200 | [diff] [blame] | 2315 | will only be set if it was defined elsewhere (i.e. used |
Willy Tarreau | 7978c5c | 2021-09-07 14:24:07 +0200 | [diff] [blame] | 2316 | within the configuration). For global variables (using the |
| 2317 | "proc" scope), they will only be updated and never created. |
| 2318 | It is highly recommended to always set this to true. |
Christopher Faulet | 85d79c9 | 2016-11-09 16:54:56 +0100 | [diff] [blame] | 2319 | |
| 2320 | .. js:function:: TXN.unset_var(TXN, var) |
| 2321 | |
| 2322 | Unset the variable <var>. |
| 2323 | |
| 2324 | :param class_txn txn: The class txn object containing the data. |
| 2325 | :param string var: The variable name according with the HAProxy variable syntax. |
Thierry FOURNIER | 053ba8ad | 2015-06-08 13:05:33 +0200 | [diff] [blame] | 2326 | |
| 2327 | .. js:function:: TXN.get_var(TXN, var) |
| 2328 | |
| 2329 | Returns data stored in the variable <var> converter in Lua type. |
| 2330 | |
| 2331 | :param class_txn txn: The class txn object containing the data. |
| 2332 | :param string var: The variable name according with the HAProxy variable syntax. |
| 2333 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 2334 | .. js:function:: TXN.reply([reply]) |
| 2335 | |
| 2336 | Return a new reply object |
| 2337 | |
| 2338 | :param table reply: A table containing info to initialize the reply fields. |
| 2339 | :returns: A :ref:`reply_class` object. |
| 2340 | |
| 2341 | The table used to initialized the reply object may contain following entries : |
| 2342 | |
| 2343 | * status : The reply status code. the code 200 is used by default. |
| 2344 | * reason : The reply reason. The reason corresponding to the status code is |
| 2345 | used by default. |
Aurelien DARRAGON | 53901f4 | 2022-10-13 19:49:42 +0200 | [diff] [blame] | 2346 | * 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] | 2347 | a given name, multiple values are possible, stored in an ordered list. |
| 2348 | * body : The reply body, empty by default. |
| 2349 | |
| 2350 | .. code-block:: lua |
| 2351 | |
| 2352 | local reply = txn:reply{ |
| 2353 | status = 400, |
| 2354 | reason = "Bad request", |
| 2355 | headers = { |
| 2356 | ["content-type"] = { "text/html" }, |
| 2357 | ["cache-control"] = {"no-cache", "no-store" } |
| 2358 | }, |
| 2359 | body = "<html><body><h1>invalid request<h1></body></html>" |
| 2360 | } |
| 2361 | .. |
| 2362 | :see: :js:class:`Reply` |
| 2363 | |
| 2364 | .. js:function:: TXN.done(txn[, reply]) |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 2365 | |
Willy Tarreau | bc183a6 | 2015-08-28 10:39:11 +0200 | [diff] [blame] | 2366 | This function terminates processing of the transaction and the associated |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 2367 | session and optionally reply to the client for HTTP sessions. |
| 2368 | |
| 2369 | :param class_txn txn: The class txn object containing the data. |
| 2370 | :param class_reply reply: The class reply object to return to the client. |
| 2371 | |
| 2372 | 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] | 2373 | 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] | 2374 | To do so, a reply may be provided. This object is optional and may contain a |
| 2375 | 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] | 2376 | optional. When not provided, the default values are used. By default, with an |
| 2377 | empty reply object, an empty HTTP 200 response is returned to the client. If |
| 2378 | no reply object is provided, the transaction is terminated without any |
| 2379 | 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] | 2380 | converted into the internal HTTP representation. Because for now there is no |
Christopher Faulet | 7855b19 | 2021-11-09 18:39:51 +0100 | [diff] [blame] | 2381 | easy way to be sure it fits, it is probably better to keep it reasonably |
| 2382 | small. |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 2383 | |
| 2384 | The reply object may be fully created in lua or the class Reply may be used to |
| 2385 | create it. |
| 2386 | |
| 2387 | .. code-block:: lua |
| 2388 | |
| 2389 | local reply = txn:reply() |
| 2390 | reply:set_status(400, "Bad request") |
| 2391 | reply:add_header("content-type", "text/html") |
| 2392 | reply:add_header("cache-control", "no-cache") |
| 2393 | reply:add_header("cache-control", "no-store") |
| 2394 | reply:set_body("<html><body><h1>invalid request<h1></body></html>") |
| 2395 | txn:done(reply) |
| 2396 | .. |
| 2397 | |
| 2398 | .. code-block:: lua |
| 2399 | |
| 2400 | txn:done{ |
| 2401 | status = 400, |
| 2402 | reason = "Bad request", |
| 2403 | headers = { |
| 2404 | ["content-type"] = { "text/html" }, |
| 2405 | ["cache-control"] = { "no-cache", "no-store" }, |
| 2406 | }, |
| 2407 | body = "<html><body><h1>invalid request<h1></body></html>" |
| 2408 | } |
| 2409 | .. |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 2410 | |
Christopher Faulet | 1e9b1b6 | 2021-08-11 10:14:30 +0200 | [diff] [blame] | 2411 | .. warning:: |
Aurelien DARRAGON | 53901f4 | 2022-10-13 19:49:42 +0200 | [diff] [blame] | 2412 | It does not make sense to call this function from sample-fetches. In this case |
| 2413 | the behavior is the same than core.done(): it finishes the Lua |
Christopher Faulet | 1e9b1b6 | 2021-08-11 10:14:30 +0200 | [diff] [blame] | 2414 | execution. The transaction is really aborted only from an action registered |
| 2415 | function. |
Thierry FOURNIER | ab00df6 | 2016-07-14 11:42:37 +0200 | [diff] [blame] | 2416 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 2417 | :see: :js:func:`TXN.reply`, :js:class:`Reply` |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 2418 | |
Thierry FOURNIER | 486f5a0 | 2015-03-16 15:13:03 +0100 | [diff] [blame] | 2419 | .. js:function:: TXN.set_loglevel(txn, loglevel) |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 2420 | |
| 2421 | Is used to change the log level of the current request. The "loglevel" must |
| 2422 | be an integer between 0 and 7. |
| 2423 | |
| 2424 | :param class_txn txn: The class txn object containing the data. |
| 2425 | :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] | 2426 | :see: :js:attr:`core.emerg`, :js:attr:`core.alert`, :js:attr:`core.crit`, |
| 2427 | :js:attr:`core.err`, :js:attr:`core.warning`, :js:attr:`core.notice`, |
| 2428 | :js:attr:`core.info`, :js:attr:`core.debug` (log level definitions) |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 2429 | |
Thierry FOURNIER | 486f5a0 | 2015-03-16 15:13:03 +0100 | [diff] [blame] | 2430 | .. js:function:: TXN.set_tos(txn, tos) |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 2431 | |
| 2432 | Is used to set the TOS or DSCP field value of packets sent to the client to |
| 2433 | the value passed in "tos" on platforms which support this. |
| 2434 | |
| 2435 | :param class_txn txn: The class txn object containing the data. |
| 2436 | :param integer tos: The new TOS os DSCP. |
| 2437 | |
Thierry FOURNIER | 486f5a0 | 2015-03-16 15:13:03 +0100 | [diff] [blame] | 2438 | .. js:function:: TXN.set_mark(txn, mark) |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 2439 | |
| 2440 | Is used to set the Netfilter MARK on all packets sent to the client to the |
| 2441 | value passed in "mark" on platforms which support it. |
| 2442 | |
| 2443 | :param class_txn txn: The class txn object containing the data. |
| 2444 | :param integer mark: The mark value. |
| 2445 | |
Patrick Hemmer | 268a707 | 2018-05-11 12:52:31 -0400 | [diff] [blame] | 2446 | .. js:function:: TXN.set_priority_class(txn, prio) |
| 2447 | |
| 2448 | This function adjusts the priority class of the transaction. The value should |
| 2449 | be within the range -2047..2047. Values outside this range will be |
| 2450 | truncated. |
| 2451 | |
| 2452 | See the HAProxy configuration.txt file keyword "http-request" action |
| 2453 | "set-priority-class" for details. |
| 2454 | |
| 2455 | .. js:function:: TXN.set_priority_offset(txn, prio) |
| 2456 | |
| 2457 | This function adjusts the priority offset of the transaction. The value |
| 2458 | should be within the range -524287..524287. Values outside this range will be |
| 2459 | truncated. |
| 2460 | |
| 2461 | See the HAProxy configuration.txt file keyword "http-request" action |
| 2462 | "set-priority-offset" for details. |
| 2463 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 2464 | .. _reply_class: |
| 2465 | |
| 2466 | Reply class |
| 2467 | ============ |
| 2468 | |
| 2469 | .. js:class:: Reply |
| 2470 | |
| 2471 | **context**: action |
| 2472 | |
Aurelien DARRAGON | 53901f4 | 2022-10-13 19:49:42 +0200 | [diff] [blame] | 2473 | This class represents a HTTP response message. It provides some methods to |
Christopher Faulet | 7855b19 | 2021-11-09 18:39:51 +0100 | [diff] [blame] | 2474 | enrich it. Once converted into the internal HTTP representation, the response |
| 2475 | message must not exceed the buffer size. Because for now there is no |
| 2476 | easy way to be sure it fits, it is probably better to keep it reasonably |
| 2477 | small. |
| 2478 | |
Aurelien DARRAGON | 53901f4 | 2022-10-13 19:49:42 +0200 | [diff] [blame] | 2479 | See tune.bufsize in the configuration manual for details. |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 2480 | |
| 2481 | .. code-block:: lua |
| 2482 | |
| 2483 | local reply = txn:reply({status = 400}) -- default HTTP 400 reason-phase used |
| 2484 | reply:add_header("content-type", "text/html") |
| 2485 | reply:add_header("cache-control", "no-cache") |
| 2486 | reply:add_header("cache-control", "no-store") |
| 2487 | reply:set_body("<html><body><h1>invalid request<h1></body></html>") |
| 2488 | .. |
| 2489 | |
| 2490 | :see: :js:func:`TXN.reply` |
| 2491 | |
| 2492 | .. js:attribute:: Reply.status |
| 2493 | |
| 2494 | The reply status code. By default, the status code is set to 200. |
| 2495 | |
| 2496 | :returns: integer |
| 2497 | |
| 2498 | .. js:attribute:: Reply.reason |
| 2499 | |
| 2500 | The reason string describing the status code. |
| 2501 | |
| 2502 | :returns: string |
| 2503 | |
| 2504 | .. js:attribute:: Reply.headers |
| 2505 | |
| 2506 | A table indexing all reply headers by name. To each name is associated an |
| 2507 | ordered list of values. |
| 2508 | |
| 2509 | :returns: Lua table |
| 2510 | |
| 2511 | .. code-block:: lua |
| 2512 | |
| 2513 | { |
| 2514 | ["content-type"] = { "text/html" }, |
| 2515 | ["cache-control"] = {"no-cache", "no-store" }, |
| 2516 | x_header_name = { "value1", "value2", ... } |
| 2517 | ... |
| 2518 | } |
| 2519 | .. |
| 2520 | |
| 2521 | .. js:attribute:: Reply.body |
| 2522 | |
| 2523 | The reply payload. |
| 2524 | |
| 2525 | :returns: string |
| 2526 | |
| 2527 | .. js:function:: Reply.set_status(REPLY, status[, reason]) |
| 2528 | |
| 2529 | Set the reply status code and optionally the reason-phrase. If the reason is |
| 2530 | not provided, the default reason corresponding to the status code is used. |
| 2531 | |
| 2532 | :param class_reply reply: The related Reply object. |
| 2533 | :param integer status: The reply status code. |
| 2534 | :param string reason: The reply status reason (optional). |
| 2535 | |
| 2536 | .. js:function:: Reply.add_header(REPLY, name, value) |
| 2537 | |
| 2538 | Add a header to the reply object. If the header does not already exist, a new |
| 2539 | entry is created with its name as index and a one-element list containing its |
| 2540 | value as value. Otherwise, the header value is appended to the ordered list of |
| 2541 | values associated to the header name. |
| 2542 | |
| 2543 | :param class_reply reply: The related Reply object. |
| 2544 | :param string name: The header field name. |
| 2545 | :param string value: The header field value. |
| 2546 | |
| 2547 | .. js:function:: Reply.del_header(REPLY, name) |
| 2548 | |
| 2549 | Remove all occurrences of a header name from the reply object. |
| 2550 | |
| 2551 | :param class_reply reply: The related Reply object. |
| 2552 | :param string name: The header field name. |
| 2553 | |
| 2554 | .. js:function:: Reply.set_body(REPLY, body) |
| 2555 | |
| 2556 | Set the reply payload. |
| 2557 | |
| 2558 | :param class_reply reply: The related Reply object. |
| 2559 | :param string body: The reply payload. |
| 2560 | |
Thierry FOURNIER | dc59500 | 2015-12-21 11:13:52 +0100 | [diff] [blame] | 2561 | .. _socket_class: |
| 2562 | |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 2563 | Socket class |
| 2564 | ============ |
| 2565 | |
| 2566 | .. js:class:: Socket |
| 2567 | |
| 2568 | This class must be compatible with the Lua Socket class. Only the 'client' |
| 2569 | functions are available. See the Lua Socket documentation: |
| 2570 | |
| 2571 | `http://w3.impa.br/~diego/software/luasocket/tcp.html |
| 2572 | <http://w3.impa.br/~diego/software/luasocket/tcp.html>`_ |
| 2573 | |
Thierry FOURNIER | 486f5a0 | 2015-03-16 15:13:03 +0100 | [diff] [blame] | 2574 | .. js:function:: Socket.close(socket) |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 2575 | |
| 2576 | Closes a TCP object. The internal socket used by the object is closed and the |
| 2577 | local address to which the object was bound is made available to other |
| 2578 | applications. No further operations (except for further calls to the close |
Thierry FOURNIER | 486f5a0 | 2015-03-16 15:13:03 +0100 | [diff] [blame] | 2579 | method) are allowed on a closed Socket. |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 2580 | |
Thierry FOURNIER | 486f5a0 | 2015-03-16 15:13:03 +0100 | [diff] [blame] | 2581 | :param class_socket socket: Is the manipulated Socket. |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 2582 | |
| 2583 | Note: It is important to close all used sockets once they are not needed, |
| 2584 | since, in many systems, each socket uses a file descriptor, which are limited |
| 2585 | system resources. Garbage-collected objects are automatically closed before |
| 2586 | destruction, though. |
| 2587 | |
Thierry FOURNIER | a3bc513 | 2015-09-25 21:43:56 +0200 | [diff] [blame] | 2588 | .. js:function:: Socket.connect(socket, address[, port]) |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 2589 | |
| 2590 | Attempts to connect a socket object to a remote host. |
| 2591 | |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 2592 | |
| 2593 | In case of error, the method returns nil followed by a string describing the |
| 2594 | error. In case of success, the method returns 1. |
| 2595 | |
Thierry FOURNIER | 486f5a0 | 2015-03-16 15:13:03 +0100 | [diff] [blame] | 2596 | :param class_socket socket: Is the manipulated Socket. |
Thierry FOURNIER | a3bc513 | 2015-09-25 21:43:56 +0200 | [diff] [blame] | 2597 | :param string address: can be an IP address or a host name. See below for more |
| 2598 | information. |
| 2599 | :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] | 2600 | :returns: 1 or nil. |
| 2601 | |
Bertrand Jacquin | 874a35c | 2018-09-10 21:26:07 +0100 | [diff] [blame] | 2602 | 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] | 2603 | other stream than TCP. The syntax containing a simpleipv4 or ipv6 address is |
| 2604 | the basically expected format. This format requires the port. |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 2605 | |
Thierry FOURNIER | a3bc513 | 2015-09-25 21:43:56 +0200 | [diff] [blame] | 2606 | 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] | 2607 | connect to a socket. Abstract namespaces are supported with the prefix |
Joseph Herlant | 02cedc4 | 2018-11-13 19:45:17 -0800 | [diff] [blame] | 2608 | "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] | 2609 | 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] | 2610 | 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] | 2611 | parameter *port* must not be set. |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 2612 | |
Thierry FOURNIER | 486f5a0 | 2015-03-16 15:13:03 +0100 | [diff] [blame] | 2613 | .. js:function:: Socket.connect_ssl(socket, address, port) |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 2614 | |
| 2615 | Same behavior than the function socket:connect, but uses SSL. |
| 2616 | |
Thierry FOURNIER | 486f5a0 | 2015-03-16 15:13:03 +0100 | [diff] [blame] | 2617 | :param class_socket socket: Is the manipulated Socket. |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 2618 | :returns: 1 or nil. |
| 2619 | |
Thierry FOURNIER | 486f5a0 | 2015-03-16 15:13:03 +0100 | [diff] [blame] | 2620 | .. js:function:: Socket.getpeername(socket) |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 2621 | |
| 2622 | Returns information about the remote side of a connected client object. |
| 2623 | |
| 2624 | Returns a string with the IP address of the peer, followed by the port number |
| 2625 | that peer is using for the connection. In case of error, the method returns |
| 2626 | nil. |
| 2627 | |
Thierry FOURNIER | 486f5a0 | 2015-03-16 15:13:03 +0100 | [diff] [blame] | 2628 | :param class_socket socket: Is the manipulated Socket. |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 2629 | :returns: a string containing the server information. |
| 2630 | |
Thierry FOURNIER | 486f5a0 | 2015-03-16 15:13:03 +0100 | [diff] [blame] | 2631 | .. js:function:: Socket.getsockname(socket) |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 2632 | |
| 2633 | Returns the local address information associated to the object. |
| 2634 | |
| 2635 | The method returns a string with local IP address and a number with the port. |
| 2636 | In case of error, the method returns nil. |
| 2637 | |
Thierry FOURNIER | 486f5a0 | 2015-03-16 15:13:03 +0100 | [diff] [blame] | 2638 | :param class_socket socket: Is the manipulated Socket. |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 2639 | :returns: a string containing the client information. |
| 2640 | |
Thierry FOURNIER | 486f5a0 | 2015-03-16 15:13:03 +0100 | [diff] [blame] | 2641 | .. js:function:: Socket.receive(socket, [pattern [, prefix]]) |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 2642 | |
| 2643 | Reads data from a client object, according to the specified read pattern. |
| 2644 | Patterns follow the Lua file I/O format, and the difference in performance |
| 2645 | between all patterns is negligible. |
| 2646 | |
Thierry FOURNIER | 486f5a0 | 2015-03-16 15:13:03 +0100 | [diff] [blame] | 2647 | :param class_socket socket: Is the manipulated Socket. |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 2648 | :param string|integer pattern: Describe what is required (see below). |
| 2649 | :param string prefix: A string which will be prefix the returned data. |
| 2650 | :returns: a string containing the required data or nil. |
| 2651 | |
| 2652 | Pattern can be any of the following: |
| 2653 | |
| 2654 | * **`*a`**: reads from the socket until the connection is closed. No |
| 2655 | end-of-line translation is performed; |
| 2656 | |
Thierry FOURNIER | 486f5a0 | 2015-03-16 15:13:03 +0100 | [diff] [blame] | 2657 | * **`*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] | 2658 | LF character (ASCII 10), optionally preceded by a CR character |
| 2659 | (ASCII 13). The CR and LF characters are not included in the |
| 2660 | returned line. In fact, all CR characters are ignored by the |
| 2661 | pattern. This is the default pattern. |
| 2662 | |
| 2663 | * **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] | 2664 | Socket. Prefix is an optional string to be concatenated to the |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 2665 | beginning of any received data before return. |
| 2666 | |
Thierry FOURNIER | a3bc513 | 2015-09-25 21:43:56 +0200 | [diff] [blame] | 2667 | * **empty**: If the pattern is left empty, the default option is `*l`. |
| 2668 | |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 2669 | If successful, the method returns the received pattern. In case of error, the |
| 2670 | method returns nil followed by an error message which can be the string |
| 2671 | 'closed' in case the connection was closed before the transmission was |
| 2672 | completed or the string 'timeout' in case there was a timeout during the |
| 2673 | operation. Also, after the error message, the function returns the partial |
| 2674 | result of the transmission. |
| 2675 | |
| 2676 | Important note: This function was changed severely. It used to support |
| 2677 | multiple patterns (but I have never seen this feature used) and now it |
| 2678 | doesn't anymore. Partial results used to be returned in the same way as |
| 2679 | successful results. This last feature violated the idea that all functions |
| 2680 | should return nil on error. Thus it was changed too. |
| 2681 | |
Thierry FOURNIER | 486f5a0 | 2015-03-16 15:13:03 +0100 | [diff] [blame] | 2682 | .. js:function:: Socket.send(socket, data [, start [, end ]]) |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 2683 | |
| 2684 | Sends data through client object. |
| 2685 | |
Thierry FOURNIER | 486f5a0 | 2015-03-16 15:13:03 +0100 | [diff] [blame] | 2686 | :param class_socket socket: Is the manipulated Socket. |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 2687 | :param string data: The data that will be sent. |
| 2688 | :param integer start: The start position in the buffer of the data which will |
| 2689 | be sent. |
| 2690 | :param integer end: The end position in the buffer of the data which will |
| 2691 | be sent. |
| 2692 | :returns: see below. |
| 2693 | |
| 2694 | Data is the string to be sent. The optional arguments i and j work exactly |
| 2695 | like the standard string.sub Lua function to allow the selection of a |
| 2696 | substring to be sent. |
| 2697 | |
| 2698 | If successful, the method returns the index of the last byte within [start, |
| 2699 | end] that has been sent. Notice that, if start is 1 or absent, this is |
| 2700 | effectively the total number of bytes sent. In case of error, the method |
| 2701 | returns nil, followed by an error message, followed by the index of the last |
| 2702 | byte within [start, end] that has been sent. You might want to try again from |
| 2703 | the byte following that. The error message can be 'closed' in case the |
| 2704 | connection was closed before the transmission was completed or the string |
| 2705 | 'timeout' in case there was a timeout during the operation. |
| 2706 | |
| 2707 | Note: Output is not buffered. For small strings, it is always better to |
| 2708 | concatenate them in Lua (with the '..' operator) and send the result in one |
| 2709 | call instead of calling the method several times. |
| 2710 | |
Thierry FOURNIER | 486f5a0 | 2015-03-16 15:13:03 +0100 | [diff] [blame] | 2711 | .. js:function:: Socket.setoption(socket, option [, value]) |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 2712 | |
| 2713 | Just implemented for compatibility, this cal does nothing. |
| 2714 | |
Thierry FOURNIER | 486f5a0 | 2015-03-16 15:13:03 +0100 | [diff] [blame] | 2715 | .. js:function:: Socket.settimeout(socket, value [, mode]) |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 2716 | |
| 2717 | Changes the timeout values for the object. All I/O operations are blocking. |
| 2718 | That is, any call to the methods send, receive, and accept will block |
| 2719 | indefinitely, until the operation completes. The settimeout method defines a |
| 2720 | limit on the amount of time the I/O methods can block. When a timeout time |
| 2721 | has elapsed, the affected methods give up and fail with an error code. |
| 2722 | |
| 2723 | The amount of time to wait is specified as the value parameter, in seconds. |
| 2724 | |
Mark Lakes | 56cc125 | 2018-03-27 09:48:06 +0200 | [diff] [blame] | 2725 | The timeout modes are not implemented, the only settable timeout is the |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 2726 | inactivity time waiting for complete the internal buffer send or waiting for |
| 2727 | receive data. |
| 2728 | |
Thierry FOURNIER | 486f5a0 | 2015-03-16 15:13:03 +0100 | [diff] [blame] | 2729 | :param class_socket socket: Is the manipulated Socket. |
Bertrand Jacquin | 874a35c | 2018-09-10 21:26:07 +0100 | [diff] [blame] | 2730 | :param float value: The timeout value. Use floating point to specify |
Mark Lakes | 56cc125 | 2018-03-27 09:48:06 +0200 | [diff] [blame] | 2731 | milliseconds. |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 2732 | |
Thierry FOURNIER | 3190427 | 2017-10-25 12:59:51 +0200 | [diff] [blame] | 2733 | .. _regex_class: |
| 2734 | |
| 2735 | Regex class |
| 2736 | =========== |
| 2737 | |
| 2738 | .. js:class:: Regex |
| 2739 | |
| 2740 | This class allows the usage of HAProxy regexes because classic lua doesn't |
| 2741 | provides regexes. This class inherits the HAProxy compilation options, so the |
| 2742 | regexes can be libc regex, pcre regex or pcre JIT regex. |
| 2743 | |
| 2744 | The expression matching number is limited to 20 per regex. The only available |
| 2745 | option is case sensitive. |
| 2746 | |
| 2747 | Because regexes compilation is a heavy process, it is better to define all |
| 2748 | your regexes in the **body context** and use it during the runtime. |
| 2749 | |
| 2750 | .. code-block:: lua |
| 2751 | |
| 2752 | -- Create the regex |
| 2753 | st, regex = Regex.new("needle (..) (...)", true); |
| 2754 | |
| 2755 | -- Check compilation errors |
| 2756 | if st == false then |
| 2757 | print "error: " .. regex |
| 2758 | end |
| 2759 | |
| 2760 | -- Match the regexes |
| 2761 | print(regex:exec("Looking for a needle in the haystack")) -- true |
| 2762 | print(regex:exec("Lokking for a cat in the haystack")) -- false |
| 2763 | |
| 2764 | -- Extract words |
| 2765 | st, list = regex:match("Looking for a needle in the haystack") |
| 2766 | print(st) -- true |
| 2767 | print(list[1]) -- needle in the |
| 2768 | print(list[2]) -- in |
| 2769 | print(list[3]) -- the |
| 2770 | |
| 2771 | .. js:function:: Regex.new(regex, case_sensitive) |
| 2772 | |
| 2773 | Create and compile a regex. |
| 2774 | |
| 2775 | :param string regex: The regular expression according with the libc or pcre |
| 2776 | standard |
| 2777 | :param boolean case_sensitive: Match is case sensitive or not. |
| 2778 | :returns: boolean status and :ref:`regex_class` or string containing fail reason. |
| 2779 | |
| 2780 | .. js:function:: Regex.exec(regex, str) |
| 2781 | |
| 2782 | Execute the regex. |
| 2783 | |
| 2784 | :param class_regex regex: 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. |
| 2787 | |
| 2788 | .. js:function:: Regex.match(regex, str) |
| 2789 | |
| 2790 | Execute the regex and return matched expressions. |
| 2791 | |
| 2792 | :param class_map map: A :ref:`regex_class` object. |
| 2793 | :param string str: The input string will be compared with the compiled regex. |
| 2794 | :returns: a boolean status according with the match result, and |
| 2795 | a table containing all the string matched in order of declaration. |
| 2796 | |
Thierry FOURNIER | dc59500 | 2015-12-21 11:13:52 +0100 | [diff] [blame] | 2797 | .. _map_class: |
| 2798 | |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 2799 | Map class |
| 2800 | ========= |
| 2801 | |
| 2802 | .. js:class:: Map |
| 2803 | |
Aurelien DARRAGON | 53901f4 | 2022-10-13 19:49:42 +0200 | [diff] [blame] | 2804 | 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] | 2805 | be modified during the runtime through the HAProxy management socket. |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 2806 | |
| 2807 | .. code-block:: lua |
| 2808 | |
Thierry FOURNIER | dc59500 | 2015-12-21 11:13:52 +0100 | [diff] [blame] | 2809 | default = "usa" |
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 | -- Create and load map |
Thierry FOURNIER | 4dc7197 | 2017-01-28 08:33:08 +0100 | [diff] [blame] | 2812 | geo = Map.new("geo.map", Map._ip); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 2813 | |
Thierry FOURNIER | dc59500 | 2015-12-21 11:13:52 +0100 | [diff] [blame] | 2814 | -- Create new fetch that returns the user country |
| 2815 | core.register_fetches("country", function(txn) |
| 2816 | local src; |
| 2817 | local loc; |
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 | src = txn.f:fhdr("x-forwarded-for"); |
| 2820 | if (src == nil) then |
| 2821 | src = txn.f:src() |
| 2822 | if (src == nil) then |
| 2823 | return default; |
| 2824 | end |
| 2825 | end |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 2826 | |
Thierry FOURNIER | dc59500 | 2015-12-21 11:13:52 +0100 | [diff] [blame] | 2827 | -- Perform lookup |
| 2828 | loc = geo:lookup(src); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 2829 | |
Thierry FOURNIER | dc59500 | 2015-12-21 11:13:52 +0100 | [diff] [blame] | 2830 | if (loc == nil) then |
| 2831 | return default; |
| 2832 | end |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 2833 | |
Thierry FOURNIER | dc59500 | 2015-12-21 11:13:52 +0100 | [diff] [blame] | 2834 | return loc; |
| 2835 | end); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 2836 | |
Thierry FOURNIER | 4dc7197 | 2017-01-28 08:33:08 +0100 | [diff] [blame] | 2837 | .. js:attribute:: Map._int |
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.int` is also available for compatibility. |
| 2844 | |
| 2845 | .. js:attribute:: Map._ip |
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.ip` is also available for compatibility. |
| 2852 | |
| 2853 | .. js:attribute:: Map._str |
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.str` is also available for compatibility. |
| 2860 | |
| 2861 | .. js:attribute:: Map._beg |
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.beg` is also available for compatibility. |
| 2868 | |
| 2869 | .. js:attribute:: Map._sub |
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.sub` is also available for compatibility. |
| 2876 | |
| 2877 | .. js:attribute:: Map._dir |
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.dir` is also available for compatibility. |
| 2884 | |
| 2885 | .. js:attribute:: Map._dom |
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 | Note that :js:attr:`Map.dom` is also available for compatibility. |
| 2892 | |
| 2893 | .. js:attribute:: Map._end |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 2894 | |
| 2895 | See the HAProxy configuration.txt file, chapter "Using ACLs and fetching |
Ilya Shipitsin | 2075ca8 | 2020-03-06 23:22:22 +0500 | [diff] [blame] | 2896 | samples" and subchapter "ACL basics" to understand this pattern matching |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 2897 | method. |
| 2898 | |
Thierry FOURNIER | 4dc7197 | 2017-01-28 08:33:08 +0100 | [diff] [blame] | 2899 | .. js:attribute:: Map._reg |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 2900 | |
| 2901 | See the HAProxy configuration.txt file, chapter "Using ACLs and fetching |
Ilya Shipitsin | 2075ca8 | 2020-03-06 23:22:22 +0500 | [diff] [blame] | 2902 | samples" and subchapter "ACL basics" to understand this pattern matching |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 2903 | method. |
| 2904 | |
Thierry FOURNIER | 4dc7197 | 2017-01-28 08:33:08 +0100 | [diff] [blame] | 2905 | Note that :js:attr:`Map.reg` is also available for compatibility. |
| 2906 | |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 2907 | |
| 2908 | .. js:function:: Map.new(file, method) |
| 2909 | |
| 2910 | Creates and load a map. |
| 2911 | |
| 2912 | :param string file: Is the file containing the map. |
| 2913 | :param integer method: Is the map pattern matching method. See the attributes |
| 2914 | of the Map class. |
| 2915 | :returns: a class Map object. |
Thierry FOURNIER | 4dc7197 | 2017-01-28 08:33:08 +0100 | [diff] [blame] | 2916 | :see: The Map attributes: :js:attr:`Map._int`, :js:attr:`Map._ip`, |
| 2917 | :js:attr:`Map._str`, :js:attr:`Map._beg`, :js:attr:`Map._sub`, |
| 2918 | :js:attr:`Map._dir`, :js:attr:`Map._dom`, :js:attr:`Map._end` and |
| 2919 | :js:attr:`Map._reg`. |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 2920 | |
| 2921 | .. js:function:: Map.lookup(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 nil if no match. |
| 2928 | |
| 2929 | .. js:function:: Map.slookup(map, str) |
| 2930 | |
| 2931 | Perform a lookup in a map. |
| 2932 | |
| 2933 | :param class_map map: Is the class Map object. |
| 2934 | :param string str: Is the string used as key. |
| 2935 | :returns: a string containing the result or empty string if no match. |
| 2936 | |
Thierry FOURNIER | dc59500 | 2015-12-21 11:13:52 +0100 | [diff] [blame] | 2937 | .. _applethttp_class: |
| 2938 | |
Thierry FOURNIER | a3bc513 | 2015-09-25 21:43:56 +0200 | [diff] [blame] | 2939 | AppletHTTP class |
Thierry FOURNIER | dc59500 | 2015-12-21 11:13:52 +0100 | [diff] [blame] | 2940 | ================ |
Thierry FOURNIER | a3bc513 | 2015-09-25 21:43:56 +0200 | [diff] [blame] | 2941 | |
| 2942 | .. js:class:: AppletHTTP |
| 2943 | |
| 2944 | This class is used with applets that requires the 'http' mode. The http applet |
| 2945 | can be registered with the *core.register_service()* function. They are used |
| 2946 | for processing an http request like a server in back of HAProxy. |
| 2947 | |
| 2948 | This is an hello world sample code: |
| 2949 | |
| 2950 | .. code-block:: lua |
Thierry FOURNIER | dc59500 | 2015-12-21 11:13:52 +0100 | [diff] [blame] | 2951 | |
Pieter Baauw | 4d7f766 | 2015-11-08 16:38:08 +0100 | [diff] [blame] | 2952 | core.register_service("hello-world", "http", function(applet) |
Thierry FOURNIER | a3bc513 | 2015-09-25 21:43:56 +0200 | [diff] [blame] | 2953 | local response = "Hello World !" |
| 2954 | applet:set_status(200) |
Pieter Baauw | 2dcb9bc | 2015-10-01 22:47:12 +0200 | [diff] [blame] | 2955 | applet:add_header("content-length", string.len(response)) |
Thierry FOURNIER | a3bc513 | 2015-09-25 21:43:56 +0200 | [diff] [blame] | 2956 | applet:add_header("content-type", "text/plain") |
Pieter Baauw | 2dcb9bc | 2015-10-01 22:47:12 +0200 | [diff] [blame] | 2957 | applet:start_response() |
Thierry FOURNIER | a3bc513 | 2015-09-25 21:43:56 +0200 | [diff] [blame] | 2958 | applet:send(response) |
| 2959 | end) |
| 2960 | |
Thierry FOURNIER | dc59500 | 2015-12-21 11:13:52 +0100 | [diff] [blame] | 2961 | .. js:attribute:: AppletHTTP.c |
| 2962 | |
| 2963 | :returns: A :ref:`converters_class` |
| 2964 | |
| 2965 | This attribute contains a Converters class object. |
| 2966 | |
| 2967 | .. js:attribute:: AppletHTTP.sc |
| 2968 | |
| 2969 | :returns: A :ref:`converters_class` |
| 2970 | |
| 2971 | This attribute contains a Converters class object. The |
Aurelien DARRAGON | 53901f4 | 2022-10-13 19:49:42 +0200 | [diff] [blame] | 2972 | functions of this object always return a string. |
Thierry FOURNIER | dc59500 | 2015-12-21 11:13:52 +0100 | [diff] [blame] | 2973 | |
| 2974 | .. js:attribute:: AppletHTTP.f |
| 2975 | |
| 2976 | :returns: A :ref:`fetches_class` |
| 2977 | |
| 2978 | This attribute contains a Fetches class object. Note that the |
| 2979 | applet execution place cannot access to a valid HAProxy core HTTP |
Ilya Shipitsin | 2075ca8 | 2020-03-06 23:22:22 +0500 | [diff] [blame] | 2980 | transaction, so some sample fetches related to the HTTP dependent |
Thierry FOURNIER | dc59500 | 2015-12-21 11:13:52 +0100 | [diff] [blame] | 2981 | values (hdr, path, ...) are not available. |
| 2982 | |
| 2983 | .. js:attribute:: AppletHTTP.sf |
| 2984 | |
| 2985 | :returns: A :ref:`fetches_class` |
| 2986 | |
| 2987 | This attribute contains a Fetches class object. The functions of |
Aurelien DARRAGON | 53901f4 | 2022-10-13 19:49:42 +0200 | [diff] [blame] | 2988 | this object always return a string. Note that the applet |
Thierry FOURNIER | dc59500 | 2015-12-21 11:13:52 +0100 | [diff] [blame] | 2989 | execution place cannot access to a valid HAProxy core HTTP |
Ilya Shipitsin | 2075ca8 | 2020-03-06 23:22:22 +0500 | [diff] [blame] | 2990 | transaction, so some sample fetches related to the HTTP dependent |
Thierry FOURNIER | dc59500 | 2015-12-21 11:13:52 +0100 | [diff] [blame] | 2991 | values (hdr, path, ...) are not available. |
| 2992 | |
Thierry FOURNIER | e34a78e | 2015-12-25 01:31:35 +0100 | [diff] [blame] | 2993 | .. js:attribute:: AppletHTTP.method |
Thierry FOURNIER | dc59500 | 2015-12-21 11:13:52 +0100 | [diff] [blame] | 2994 | |
| 2995 | :returns: string |
| 2996 | |
| 2997 | The attribute method returns a string containing the HTTP |
| 2998 | method. |
| 2999 | |
| 3000 | .. js:attribute:: AppletHTTP.version |
| 3001 | |
| 3002 | :returns: string |
| 3003 | |
| 3004 | The attribute version, returns a string containing the HTTP |
| 3005 | request version. |
| 3006 | |
| 3007 | .. js:attribute:: AppletHTTP.path |
| 3008 | |
| 3009 | :returns: string |
| 3010 | |
| 3011 | The attribute path returns a string containing the HTTP |
| 3012 | request path. |
| 3013 | |
| 3014 | .. js:attribute:: AppletHTTP.qs |
| 3015 | |
| 3016 | :returns: string |
| 3017 | |
| 3018 | The attribute qs returns a string containing the HTTP |
| 3019 | request query string. |
| 3020 | |
| 3021 | .. js:attribute:: AppletHTTP.length |
| 3022 | |
| 3023 | :returns: integer |
| 3024 | |
| 3025 | The attribute length returns an integer containing the HTTP |
| 3026 | body length. |
Thierry FOURNIER | a3bc513 | 2015-09-25 21:43:56 +0200 | [diff] [blame] | 3027 | |
Thierry FOURNIER | 841475e | 2015-12-11 17:10:09 +0100 | [diff] [blame] | 3028 | .. js:attribute:: AppletHTTP.headers |
| 3029 | |
Patrick Hemmer | c6a1d71 | 2018-05-01 21:30:41 -0400 | [diff] [blame] | 3030 | :returns: table |
Thierry FOURNIER | dc59500 | 2015-12-21 11:13:52 +0100 | [diff] [blame] | 3031 | |
Patrick Hemmer | c6a1d71 | 2018-05-01 21:30:41 -0400 | [diff] [blame] | 3032 | The attribute headers returns a table containing the HTTP |
Thierry FOURNIER | dc59500 | 2015-12-21 11:13:52 +0100 | [diff] [blame] | 3033 | headers. The header names are always in lower case. As the header name can be |
| 3034 | 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] | 3035 | first index value. The table has this form: |
Thierry FOURNIER | dc59500 | 2015-12-21 11:13:52 +0100 | [diff] [blame] | 3036 | |
| 3037 | .. code-block:: lua |
| 3038 | |
| 3039 | AppletHTTP.headers['<header-name>'][<header-index>] = "<header-value>" |
| 3040 | |
| 3041 | AppletHTTP.headers["host"][0] = "www.test.com" |
| 3042 | AppletHTTP.headers["accept"][0] = "audio/basic q=1" |
| 3043 | AppletHTTP.headers["accept"][1] = "audio/*, q=0.2" |
Thierry FOURNIER | e34a78e | 2015-12-25 01:31:35 +0100 | [diff] [blame] | 3044 | AppletHTTP.headers["accept"][2] = "*/*, q=0.1" |
Thierry FOURNIER | dc59500 | 2015-12-21 11:13:52 +0100 | [diff] [blame] | 3045 | .. |
| 3046 | |
Robin H. Johnson | 52f5db2 | 2017-01-01 13:10:52 -0800 | [diff] [blame] | 3047 | .. js:function:: AppletHTTP.set_status(applet, code [, reason]) |
Thierry FOURNIER | a3bc513 | 2015-09-25 21:43:56 +0200 | [diff] [blame] | 3048 | |
| 3049 | This function sets the HTTP status code for the response. The allowed code are |
| 3050 | from 100 to 599. |
| 3051 | |
Thierry FOURNIER | e34a78e | 2015-12-25 01:31:35 +0100 | [diff] [blame] | 3052 | :param class_AppletHTTP applet: An :ref:`applethttp_class` |
Thierry FOURNIER | a3bc513 | 2015-09-25 21:43:56 +0200 | [diff] [blame] | 3053 | :param integer code: the status code returned to the client. |
Robin H. Johnson | 52f5db2 | 2017-01-01 13:10:52 -0800 | [diff] [blame] | 3054 | :param string reason: the status reason returned to the client (optional). |
Thierry FOURNIER | a3bc513 | 2015-09-25 21:43:56 +0200 | [diff] [blame] | 3055 | |
Thierry FOURNIER | e34a78e | 2015-12-25 01:31:35 +0100 | [diff] [blame] | 3056 | .. js:function:: AppletHTTP.add_header(applet, name, value) |
Thierry FOURNIER | a3bc513 | 2015-09-25 21:43:56 +0200 | [diff] [blame] | 3057 | |
Aurelien DARRAGON | 53901f4 | 2022-10-13 19:49:42 +0200 | [diff] [blame] | 3058 | This function adds a header in the response. Duplicated headers are not |
Thierry FOURNIER | a3bc513 | 2015-09-25 21:43:56 +0200 | [diff] [blame] | 3059 | collapsed. The special header *content-length* is used to determinate the |
Aurelien DARRAGON | 53901f4 | 2022-10-13 19:49:42 +0200 | [diff] [blame] | 3060 | 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] | 3061 | all the write from the function *AppletHTTP:send()* become a chunk. |
Thierry FOURNIER | a3bc513 | 2015-09-25 21:43:56 +0200 | [diff] [blame] | 3062 | |
Thierry FOURNIER | e34a78e | 2015-12-25 01:31:35 +0100 | [diff] [blame] | 3063 | :param class_AppletHTTP applet: An :ref:`applethttp_class` |
Thierry FOURNIER | a3bc513 | 2015-09-25 21:43:56 +0200 | [diff] [blame] | 3064 | :param string name: the header name |
| 3065 | :param string value: the header value |
| 3066 | |
Thierry FOURNIER | e34a78e | 2015-12-25 01:31:35 +0100 | [diff] [blame] | 3067 | .. js:function:: AppletHTTP.start_response(applet) |
Thierry FOURNIER | a3bc513 | 2015-09-25 21:43:56 +0200 | [diff] [blame] | 3068 | |
| 3069 | This function indicates to the HTTP engine that it can process and send the |
| 3070 | response headers. After this called we cannot add headers to the response; We |
| 3071 | cannot use the *AppletHTTP:send()* function if the |
| 3072 | *AppletHTTP:start_response()* is not called. |
| 3073 | |
Thierry FOURNIER | e34a78e | 2015-12-25 01:31:35 +0100 | [diff] [blame] | 3074 | :param class_AppletHTTP applet: An :ref:`applethttp_class` |
| 3075 | |
| 3076 | .. js:function:: AppletHTTP.getline(applet) |
Thierry FOURNIER | a3bc513 | 2015-09-25 21:43:56 +0200 | [diff] [blame] | 3077 | |
| 3078 | This function returns a string containing one line from the http body. If the |
| 3079 | data returned doesn't contains a final '\\n' its assumed than its the last |
| 3080 | available data before the end of stream. |
| 3081 | |
Thierry FOURNIER | e34a78e | 2015-12-25 01:31:35 +0100 | [diff] [blame] | 3082 | :param class_AppletHTTP applet: An :ref:`applethttp_class` |
Thierry FOURNIER | a3bc513 | 2015-09-25 21:43:56 +0200 | [diff] [blame] | 3083 | :returns: a string. The string can be empty if we reach the end of the stream. |
| 3084 | |
Thierry FOURNIER | e34a78e | 2015-12-25 01:31:35 +0100 | [diff] [blame] | 3085 | .. js:function:: AppletHTTP.receive(applet, [size]) |
Thierry FOURNIER | a3bc513 | 2015-09-25 21:43:56 +0200 | [diff] [blame] | 3086 | |
| 3087 | Reads data from the HTTP body, according to the specified read *size*. If the |
| 3088 | *size* is missing, the function tries to read all the content of the stream |
| 3089 | 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] | 3090 | amount of data available. |
Thierry FOURNIER | a3bc513 | 2015-09-25 21:43:56 +0200 | [diff] [blame] | 3091 | |
Thierry FOURNIER | e34a78e | 2015-12-25 01:31:35 +0100 | [diff] [blame] | 3092 | :param class_AppletHTTP applet: An :ref:`applethttp_class` |
Thierry FOURNIER | a3bc513 | 2015-09-25 21:43:56 +0200 | [diff] [blame] | 3093 | :param integer size: the required read size. |
Ilya Shipitsin | 11057a3 | 2020-06-21 21:18:27 +0500 | [diff] [blame] | 3094 | :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] | 3095 | closed. |
| 3096 | |
Thierry FOURNIER | e34a78e | 2015-12-25 01:31:35 +0100 | [diff] [blame] | 3097 | .. js:function:: AppletHTTP.send(applet, msg) |
Thierry FOURNIER | a3bc513 | 2015-09-25 21:43:56 +0200 | [diff] [blame] | 3098 | |
| 3099 | Send the message *msg* on the http request body. |
| 3100 | |
Thierry FOURNIER | e34a78e | 2015-12-25 01:31:35 +0100 | [diff] [blame] | 3101 | :param class_AppletHTTP applet: An :ref:`applethttp_class` |
Thierry FOURNIER | a3bc513 | 2015-09-25 21:43:56 +0200 | [diff] [blame] | 3102 | :param string msg: the message to send. |
| 3103 | |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 3104 | .. js:function:: AppletHTTP.get_priv(applet) |
| 3105 | |
Thierry FOURNIER | 12a865d | 2016-12-14 19:40:37 +0100 | [diff] [blame] | 3106 | Return Lua data stored in the current transaction. If no data are stored, |
| 3107 | it returns a nil value. |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 3108 | |
| 3109 | :param class_AppletHTTP applet: An :ref:`applethttp_class` |
Bertrand Jacquin | 874a35c | 2018-09-10 21:26:07 +0100 | [diff] [blame] | 3110 | :returns: the opaque data previously stored, or nil if nothing is |
| 3111 | available. |
Thierry FOURNIER | 12a865d | 2016-12-14 19:40:37 +0100 | [diff] [blame] | 3112 | :see: :js:func:`AppletHTTP.set_priv` |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 3113 | |
| 3114 | .. js:function:: AppletHTTP.set_priv(applet, data) |
| 3115 | |
Aurelien DARRAGON | 53901f4 | 2022-10-13 19:49:42 +0200 | [diff] [blame] | 3116 | Store any data in the current HAProxy transaction. This action replaces the |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 3117 | old stored data. |
| 3118 | |
| 3119 | :param class_AppletHTTP applet: An :ref:`applethttp_class` |
| 3120 | :param opaque data: The data which is stored in the transaction. |
Thierry FOURNIER | 12a865d | 2016-12-14 19:40:37 +0100 | [diff] [blame] | 3121 | :see: :js:func:`AppletHTTP.get_priv` |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 3122 | |
Tim Duesterhus | 4e172c9 | 2020-05-19 13:49:42 +0200 | [diff] [blame] | 3123 | .. js:function:: AppletHTTP.set_var(applet, var, value[, ifexist]) |
Thierry FOURNIER / OZON.IO | c1edafe | 2016-12-12 16:25:30 +0100 | [diff] [blame] | 3124 | |
| 3125 | Converts a Lua type in a HAProxy type and store it in a variable <var>. |
| 3126 | |
| 3127 | :param class_AppletHTTP applet: An :ref:`applethttp_class` |
| 3128 | :param string var: The variable name according with the HAProxy variable syntax. |
| 3129 | :param type value: The value associated to the variable. The type ca be string or |
| 3130 | integer. |
Aurelien DARRAGON | 53901f4 | 2022-10-13 19:49:42 +0200 | [diff] [blame] | 3131 | :param boolean ifexist: If this parameter is set to true the variable |
Tim Duesterhus | 4e172c9 | 2020-05-19 13:49:42 +0200 | [diff] [blame] | 3132 | will only be set if it was defined elsewhere (i.e. used |
Willy Tarreau | 7978c5c | 2021-09-07 14:24:07 +0200 | [diff] [blame] | 3133 | within the configuration). For global variables (using the |
| 3134 | "proc" scope), they will only be updated and never created. |
Aurelien DARRAGON | 21f7ebb | 2023-03-13 19:49:31 +0100 | [diff] [blame] | 3135 | It is highly recommended to always set this to true. |
Aurelien DARRAGON | 53901f4 | 2022-10-13 19:49:42 +0200 | [diff] [blame] | 3136 | |
Thierry FOURNIER | 12a865d | 2016-12-14 19:40:37 +0100 | [diff] [blame] | 3137 | :see: :js:func:`AppletHTTP.unset_var` |
| 3138 | :see: :js:func:`AppletHTTP.get_var` |
Thierry FOURNIER / OZON.IO | c1edafe | 2016-12-12 16:25:30 +0100 | [diff] [blame] | 3139 | |
| 3140 | .. js:function:: AppletHTTP.unset_var(applet, var) |
| 3141 | |
| 3142 | Unset the variable <var>. |
| 3143 | |
| 3144 | :param class_AppletHTTP applet: An :ref:`applethttp_class` |
| 3145 | :param string var: The variable name according with the HAProxy variable syntax. |
Thierry FOURNIER | 12a865d | 2016-12-14 19:40:37 +0100 | [diff] [blame] | 3146 | :see: :js:func:`AppletHTTP.set_var` |
| 3147 | :see: :js:func:`AppletHTTP.get_var` |
Thierry FOURNIER / OZON.IO | c1edafe | 2016-12-12 16:25:30 +0100 | [diff] [blame] | 3148 | |
| 3149 | .. js:function:: AppletHTTP.get_var(applet, var) |
| 3150 | |
| 3151 | Returns data stored in the variable <var> converter in Lua type. |
| 3152 | |
| 3153 | :param class_AppletHTTP applet: An :ref:`applethttp_class` |
| 3154 | :param string var: The variable name according with the HAProxy variable syntax. |
Thierry FOURNIER | 12a865d | 2016-12-14 19:40:37 +0100 | [diff] [blame] | 3155 | :see: :js:func:`AppletHTTP.set_var` |
| 3156 | :see: :js:func:`AppletHTTP.unset_var` |
Thierry FOURNIER / OZON.IO | c1edafe | 2016-12-12 16:25:30 +0100 | [diff] [blame] | 3157 | |
Thierry FOURNIER | dc59500 | 2015-12-21 11:13:52 +0100 | [diff] [blame] | 3158 | .. _applettcp_class: |
| 3159 | |
Thierry FOURNIER | a3bc513 | 2015-09-25 21:43:56 +0200 | [diff] [blame] | 3160 | AppletTCP class |
| 3161 | =============== |
| 3162 | |
| 3163 | .. js:class:: AppletTCP |
| 3164 | |
| 3165 | This class is used with applets that requires the 'tcp' mode. The tcp applet |
| 3166 | can be registered with the *core.register_service()* function. They are used |
| 3167 | for processing a tcp stream like a server in back of HAProxy. |
| 3168 | |
Thierry FOURNIER | dc59500 | 2015-12-21 11:13:52 +0100 | [diff] [blame] | 3169 | .. js:attribute:: AppletTCP.c |
| 3170 | |
| 3171 | :returns: A :ref:`converters_class` |
| 3172 | |
| 3173 | This attribute contains a Converters class object. |
| 3174 | |
| 3175 | .. js:attribute:: AppletTCP.sc |
| 3176 | |
| 3177 | :returns: A :ref:`converters_class` |
| 3178 | |
| 3179 | This attribute contains a Converters class object. The |
Aurelien DARRAGON | 53901f4 | 2022-10-13 19:49:42 +0200 | [diff] [blame] | 3180 | functions of this object always return a string. |
Thierry FOURNIER | dc59500 | 2015-12-21 11:13:52 +0100 | [diff] [blame] | 3181 | |
| 3182 | .. js:attribute:: AppletTCP.f |
| 3183 | |
| 3184 | :returns: A :ref:`fetches_class` |
| 3185 | |
| 3186 | This attribute contains a Fetches class object. |
| 3187 | |
| 3188 | .. js:attribute:: AppletTCP.sf |
| 3189 | |
| 3190 | :returns: A :ref:`fetches_class` |
| 3191 | |
| 3192 | This attribute contains a Fetches class object. |
| 3193 | |
Thierry FOURNIER | e34a78e | 2015-12-25 01:31:35 +0100 | [diff] [blame] | 3194 | .. js:function:: AppletTCP.getline(applet) |
Thierry FOURNIER | a3bc513 | 2015-09-25 21:43:56 +0200 | [diff] [blame] | 3195 | |
| 3196 | This function returns a string containing one line from the stream. If the |
| 3197 | data returned doesn't contains a final '\\n' its assumed than its the last |
| 3198 | available data before the end of stream. |
| 3199 | |
Thierry FOURNIER | e34a78e | 2015-12-25 01:31:35 +0100 | [diff] [blame] | 3200 | :param class_AppletTCP applet: An :ref:`applettcp_class` |
Thierry FOURNIER | a3bc513 | 2015-09-25 21:43:56 +0200 | [diff] [blame] | 3201 | :returns: a string. The string can be empty if we reach the end of the stream. |
| 3202 | |
Thierry FOURNIER | e34a78e | 2015-12-25 01:31:35 +0100 | [diff] [blame] | 3203 | .. js:function:: AppletTCP.receive(applet, [size]) |
Thierry FOURNIER | a3bc513 | 2015-09-25 21:43:56 +0200 | [diff] [blame] | 3204 | |
| 3205 | Reads data from the TCP stream, according to the specified read *size*. If the |
| 3206 | *size* is missing, the function tries to read all the content of the stream |
| 3207 | until the end. |
| 3208 | |
Thierry FOURNIER | e34a78e | 2015-12-25 01:31:35 +0100 | [diff] [blame] | 3209 | :param class_AppletTCP applet: An :ref:`applettcp_class` |
Thierry FOURNIER | a3bc513 | 2015-09-25 21:43:56 +0200 | [diff] [blame] | 3210 | :param integer size: the required read size. |
Aurelien DARRAGON | 53901f4 | 2022-10-13 19:49:42 +0200 | [diff] [blame] | 3211 | :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] | 3212 | closed. |
| 3213 | |
Thierry FOURNIER | e34a78e | 2015-12-25 01:31:35 +0100 | [diff] [blame] | 3214 | .. js:function:: AppletTCP.send(appletmsg) |
Thierry FOURNIER | a3bc513 | 2015-09-25 21:43:56 +0200 | [diff] [blame] | 3215 | |
| 3216 | Send the message on the stream. |
| 3217 | |
Thierry FOURNIER | e34a78e | 2015-12-25 01:31:35 +0100 | [diff] [blame] | 3218 | :param class_AppletTCP applet: An :ref:`applettcp_class` |
Thierry FOURNIER | a3bc513 | 2015-09-25 21:43:56 +0200 | [diff] [blame] | 3219 | :param string msg: the message to send. |
| 3220 | |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 3221 | .. js:function:: AppletTCP.get_priv(applet) |
| 3222 | |
Thierry FOURNIER | 12a865d | 2016-12-14 19:40:37 +0100 | [diff] [blame] | 3223 | Return Lua data stored in the current transaction. If no data are stored, |
| 3224 | it returns a nil value. |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 3225 | |
| 3226 | :param class_AppletTCP applet: An :ref:`applettcp_class` |
Bertrand Jacquin | 874a35c | 2018-09-10 21:26:07 +0100 | [diff] [blame] | 3227 | :returns: the opaque data previously stored, or nil if nothing is |
| 3228 | available. |
Thierry FOURNIER | 12a865d | 2016-12-14 19:40:37 +0100 | [diff] [blame] | 3229 | :see: :js:func:`AppletTCP.set_priv` |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 3230 | |
| 3231 | .. js:function:: AppletTCP.set_priv(applet, data) |
| 3232 | |
Aurelien DARRAGON | 53901f4 | 2022-10-13 19:49:42 +0200 | [diff] [blame] | 3233 | Store any data in the current HAProxy transaction. This action replaces the |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 3234 | old stored data. |
| 3235 | |
| 3236 | :param class_AppletTCP applet: An :ref:`applettcp_class` |
| 3237 | :param opaque data: The data which is stored in the transaction. |
Thierry FOURNIER | 12a865d | 2016-12-14 19:40:37 +0100 | [diff] [blame] | 3238 | :see: :js:func:`AppletTCP.get_priv` |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 3239 | |
Tim Duesterhus | 4e172c9 | 2020-05-19 13:49:42 +0200 | [diff] [blame] | 3240 | .. js:function:: AppletTCP.set_var(applet, var, value[, ifexist]) |
Thierry FOURNIER / OZON.IO | c1edafe | 2016-12-12 16:25:30 +0100 | [diff] [blame] | 3241 | |
| 3242 | Converts a Lua type in a HAProxy type and stores it in a variable <var>. |
| 3243 | |
| 3244 | :param class_AppletTCP applet: An :ref:`applettcp_class` |
| 3245 | :param string var: The variable name according with the HAProxy variable syntax. |
| 3246 | :param type value: The value associated to the variable. The type can be string or |
| 3247 | integer. |
Aurelien DARRAGON | 53901f4 | 2022-10-13 19:49:42 +0200 | [diff] [blame] | 3248 | :param boolean ifexist: If this parameter is set to true the variable |
Tim Duesterhus | 4e172c9 | 2020-05-19 13:49:42 +0200 | [diff] [blame] | 3249 | will only be set if it was defined elsewhere (i.e. used |
Willy Tarreau | 7978c5c | 2021-09-07 14:24:07 +0200 | [diff] [blame] | 3250 | within the configuration). For global variables (using the |
| 3251 | "proc" scope), they will only be updated and never created. |
Aurelien DARRAGON | 21f7ebb | 2023-03-13 19:49:31 +0100 | [diff] [blame] | 3252 | It is highly recommended to always set this to true. |
Aurelien DARRAGON | 53901f4 | 2022-10-13 19:49:42 +0200 | [diff] [blame] | 3253 | |
Thierry FOURNIER | 12a865d | 2016-12-14 19:40:37 +0100 | [diff] [blame] | 3254 | :see: :js:func:`AppletTCP.unset_var` |
| 3255 | :see: :js:func:`AppletTCP.get_var` |
Thierry FOURNIER / OZON.IO | c1edafe | 2016-12-12 16:25:30 +0100 | [diff] [blame] | 3256 | |
| 3257 | .. js:function:: AppletTCP.unset_var(applet, var) |
| 3258 | |
| 3259 | Unsets the variable <var>. |
| 3260 | |
| 3261 | :param class_AppletTCP applet: An :ref:`applettcp_class` |
| 3262 | :param string var: The variable name according with the HAProxy variable syntax. |
Thierry FOURNIER | 12a865d | 2016-12-14 19:40:37 +0100 | [diff] [blame] | 3263 | :see: :js:func:`AppletTCP.unset_var` |
| 3264 | :see: :js:func:`AppletTCP.set_var` |
Thierry FOURNIER / OZON.IO | c1edafe | 2016-12-12 16:25:30 +0100 | [diff] [blame] | 3265 | |
| 3266 | .. js:function:: AppletTCP.get_var(applet, var) |
| 3267 | |
| 3268 | Returns data stored in the variable <var> converter in Lua type. |
| 3269 | |
| 3270 | :param class_AppletTCP applet: An :ref:`applettcp_class` |
| 3271 | :param string var: The variable name according with the HAProxy variable syntax. |
Thierry FOURNIER | 12a865d | 2016-12-14 19:40:37 +0100 | [diff] [blame] | 3272 | :see: :js:func:`AppletTCP.unset_var` |
| 3273 | :see: :js:func:`AppletTCP.set_var` |
Thierry FOURNIER / OZON.IO | c1edafe | 2016-12-12 16:25:30 +0100 | [diff] [blame] | 3274 | |
Adis Nezirovic | 8878f8e | 2018-07-13 12:18:33 +0200 | [diff] [blame] | 3275 | StickTable class |
| 3276 | ================ |
| 3277 | |
| 3278 | .. js:class:: StickTable |
| 3279 | |
| 3280 | **context**: task, action, sample-fetch |
| 3281 | |
| 3282 | This class can be used to access the HAProxy stick tables from Lua. |
| 3283 | |
| 3284 | .. js:function:: StickTable.info() |
| 3285 | |
| 3286 | Returns stick table attributes as a Lua table. See HAProxy documentation for |
Ilya Shipitsin | 2272d8a | 2020-12-21 01:22:40 +0500 | [diff] [blame] | 3287 | "stick-table" for canonical info, or check out example below. |
Adis Nezirovic | 8878f8e | 2018-07-13 12:18:33 +0200 | [diff] [blame] | 3288 | |
| 3289 | :returns: Lua table |
| 3290 | |
| 3291 | Assume our table has IPv4 key and gpc0 and conn_rate "columns": |
| 3292 | |
| 3293 | .. code-block:: lua |
| 3294 | |
| 3295 | { |
| 3296 | expire=<int>, # Value in ms |
| 3297 | size=<int>, # Maximum table size |
| 3298 | used=<int>, # Actual number of entries in table |
| 3299 | data={ # Data columns, with types as key, and periods as values |
| 3300 | (-1 if type is not rate counter) |
| 3301 | conn_rate=<int>, |
| 3302 | gpc0=-1 |
| 3303 | }, |
| 3304 | length=<int>, # max string length for string table keys, key length |
| 3305 | # otherwise |
| 3306 | nopurge=<boolean>, # purge oldest entries when table is full |
| 3307 | type="ip" # can be "ip", "ipv6", "integer", "string", "binary" |
| 3308 | } |
| 3309 | |
| 3310 | .. js:function:: StickTable.lookup(key) |
| 3311 | |
| 3312 | Returns stick table entry for given <key> |
| 3313 | |
| 3314 | :param string key: Stick table key (IP addresses and strings are supported) |
| 3315 | :returns: Lua table |
| 3316 | |
| 3317 | .. js:function:: StickTable.dump([filter]) |
| 3318 | |
| 3319 | Returns all entries in stick table. An optional filter can be used |
| 3320 | to extract entries with specific data values. Filter is a table with valid |
| 3321 | comparison operators as keys followed by data type name and value pairs. |
| 3322 | Check out the HAProxy docs for "show table" for more details. For the |
| 3323 | reference, the supported operators are: |
Aurelien DARRAGON | 21f7ebb | 2023-03-13 19:49:31 +0100 | [diff] [blame] | 3324 | |
Adis Nezirovic | 8878f8e | 2018-07-13 12:18:33 +0200 | [diff] [blame] | 3325 | "eq", "ne", "le", "lt", "ge", "gt" |
| 3326 | |
| 3327 | For large tables, execution of this function can take a long time (for |
| 3328 | HAProxy standards). That's also true when filter is used, so take care and |
| 3329 | measure the impact. |
| 3330 | |
| 3331 | :param table filter: Stick table filter |
| 3332 | :returns: Stick table entries (table) |
| 3333 | |
| 3334 | See below for example filter, which contains 4 entries (or comparisons). |
| 3335 | (Maximum number of filter entries is 4, defined in the source code) |
| 3336 | |
| 3337 | .. code-block:: lua |
| 3338 | |
| 3339 | local filter = { |
| 3340 | {"gpc0", "gt", 30}, {"gpc1", "gt", 20}}, {"conn_rate", "le", 10} |
| 3341 | } |
| 3342 | |
Christopher Faulet | 0f3c890 | 2020-01-31 18:57:12 +0100 | [diff] [blame] | 3343 | .. _action_class: |
| 3344 | |
| 3345 | Action class |
| 3346 | ============= |
| 3347 | |
| 3348 | .. js:class:: Act |
| 3349 | |
| 3350 | **context**: action |
| 3351 | |
| 3352 | This class contains all return codes an action may return. It is the lua |
| 3353 | equivalent to HAProxy "ACT_RET_*" code. |
| 3354 | |
| 3355 | .. code-block:: lua |
| 3356 | |
| 3357 | core.register_action("deny", { "http-req" }, function (txn) |
| 3358 | return act.DENY |
| 3359 | end) |
| 3360 | .. |
| 3361 | .. js:attribute:: act.CONTINUE |
| 3362 | |
| 3363 | This attribute is an integer (0). It instructs HAProxy to continue the current |
| 3364 | ruleset processing on the message. It is the default return code for a lua |
| 3365 | action. |
| 3366 | |
| 3367 | :returns: integer |
| 3368 | |
| 3369 | .. js:attribute:: act.STOP |
| 3370 | |
| 3371 | This attribute is an integer (1). It instructs HAProxy to stop the current |
| 3372 | ruleset processing on the message. |
| 3373 | |
| 3374 | .. js:attribute:: act.YIELD |
| 3375 | |
| 3376 | This attribute is an integer (2). It instructs HAProxy to temporarily pause |
| 3377 | the message processing. It will be resumed later on the same rule. The |
| 3378 | corresponding lua script is re-executed for the start. |
| 3379 | |
| 3380 | .. js:attribute:: act.ERROR |
| 3381 | |
| 3382 | This attribute is an integer (3). It triggers an internal errors The message |
| 3383 | processing is stopped and the transaction is terminated. For HTTP streams, an |
| 3384 | HTTP 500 error is returned to the client. |
| 3385 | |
| 3386 | :returns: integer |
| 3387 | |
| 3388 | .. js:attribute:: act.DONE |
| 3389 | |
| 3390 | This attribute is an integer (4). It instructs HAProxy to stop the message |
| 3391 | processing. |
| 3392 | |
| 3393 | :returns: integer |
| 3394 | |
| 3395 | .. js:attribute:: act.DENY |
| 3396 | |
| 3397 | This attribute is an integer (5). It denies the current message. The message |
| 3398 | processing is stopped and the transaction is terminated. For HTTP streams, an |
| 3399 | 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] | 3400 | request analysis. During the response analysis, a HTTP 502 error is returned |
Christopher Faulet | 0f3c890 | 2020-01-31 18:57:12 +0100 | [diff] [blame] | 3401 | and the server response is discarded. |
| 3402 | |
| 3403 | :returns: integer |
| 3404 | |
| 3405 | .. js:attribute:: act.ABORT |
| 3406 | |
| 3407 | This attribute is an integer (6). It aborts the current message. The message |
| 3408 | processing is stopped and the transaction is terminated. For HTTP streams, |
Willy Tarreau | 714f345 | 2021-05-09 06:47:26 +0200 | [diff] [blame] | 3409 | 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] | 3410 | actions point of view, when this code is used, the transaction is terminated |
| 3411 | with no reply. |
| 3412 | |
| 3413 | :returns: integer |
| 3414 | |
| 3415 | .. js:attribute:: act.INVALID |
| 3416 | |
| 3417 | This attribute is an integer (7). It triggers an internal errors. The message |
| 3418 | processing is stopped and the transaction is terminated. For HTTP streams, an |
| 3419 | 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] | 3420 | request analysis. During the response analysis, a HTTP 502 error is returned |
Christopher Faulet | 0f3c890 | 2020-01-31 18:57:12 +0100 | [diff] [blame] | 3421 | and the server response is discarded. |
| 3422 | |
| 3423 | :returns: integer |
Adis Nezirovic | 8878f8e | 2018-07-13 12:18:33 +0200 | [diff] [blame] | 3424 | |
Christopher Faulet | 2c2c2e3 | 2020-01-31 19:07:52 +0100 | [diff] [blame] | 3425 | .. js:function:: act:wake_time(milliseconds) |
| 3426 | |
| 3427 | **context**: action |
| 3428 | |
| 3429 | Set the script pause timeout to the specified time, defined in |
| 3430 | milliseconds. |
| 3431 | |
| 3432 | :param integer milliseconds: the required milliseconds. |
| 3433 | |
| 3434 | This function may be used when a lua action returns `act.YIELD`, to force its |
| 3435 | wake-up at most after the specified number of milliseconds. |
| 3436 | |
Christopher Faulet | 5a2c661 | 2021-08-15 20:35:25 +0200 | [diff] [blame] | 3437 | .. _filter_class: |
| 3438 | |
| 3439 | Filter class |
| 3440 | ============= |
| 3441 | |
| 3442 | .. js:class:: filter |
| 3443 | |
| 3444 | **context**: filter |
| 3445 | |
| 3446 | This class contains return codes some filter callback functions may return. It |
| 3447 | also contains configuration flags and some helper functions. To understand how |
| 3448 | the filter API works, see `doc/internal/filters.txt` documentation. |
| 3449 | |
| 3450 | .. js:attribute:: filter.CONTINUE |
| 3451 | |
| 3452 | This attribute is an integer (1). It may be returned by some filter callback |
| 3453 | functions to instruct this filtering step is finished for this filter. |
| 3454 | |
| 3455 | .. js:attribute:: filter.WAIT |
| 3456 | |
| 3457 | This attribute is an integer (0). It may be returned by some filter callback |
| 3458 | functions to instruct the filtering must be paused, waiting for more data or |
| 3459 | for an external event depending on this filter. |
| 3460 | |
| 3461 | .. js:attribute:: filter.ERROR |
| 3462 | |
| 3463 | This attribute is an integer (-1). It may be returned by some filter callback |
| 3464 | functions to trigger an error. |
| 3465 | |
| 3466 | .. js:attribute:: filter.FLT_CFG_FL_HTX |
| 3467 | |
| 3468 | This attribute is a flag corresponding to the filter flag FLT_CFG_FL_HTX. When |
| 3469 | it is set for a filter, it means the filter is able to filter HTTP streams. |
| 3470 | |
| 3471 | .. js:function:: filter.register_data_filter(chn) |
| 3472 | |
| 3473 | **context**: filter |
| 3474 | |
| 3475 | 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] | 3476 | 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] | 3477 | analysis. |
| 3478 | |
| 3479 | :param class_Channel chn: A :ref:`channel_class`. |
| 3480 | |
| 3481 | .. js:function:: filter.unregister_data_filter(chn) |
| 3482 | |
| 3483 | **context**: filter |
| 3484 | |
| 3485 | Disable the data filtering on the channel **chn** for the current filter. It |
| 3486 | may be called at any time from any callback functions. |
| 3487 | |
| 3488 | :param class_Channel chn: A :ref:`channel_class`. |
| 3489 | |
| 3490 | .. js:function:: filter.wake_time(milliseconds) |
| 3491 | |
| 3492 | **context**: filter |
| 3493 | |
| 3494 | Set the script pause timeout to the specified time, defined in |
| 3495 | milliseconds. |
| 3496 | |
| 3497 | :param integer milliseconds: the required milliseconds. |
| 3498 | |
| 3499 | This function may be used from any lua filter callback function to force its |
| 3500 | wake-up at most after the specified number of milliseconds. Especially, when |
| 3501 | `filter.CONTINUE` is returned. |
| 3502 | |
| 3503 | |
| 3504 | A filters is declared using :js:func:`core.register_filter()` function. The |
| 3505 | provided class will be used to instantiate filters. It may define following |
| 3506 | attributes: |
| 3507 | |
| 3508 | * id: The filter identifier. It is a string that identifies the filter and is |
| 3509 | optional. |
| 3510 | |
| 3511 | * flags: The filter flags. Only :js:attr:`filter.FLT_CFG_FL_HTX` may be set for now. |
| 3512 | |
| 3513 | Such filter class must also define all required callback functions in the |
| 3514 | 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] | 3515 | filter is ignored. Others are optional. |
Christopher Faulet | 5a2c661 | 2021-08-15 20:35:25 +0200 | [diff] [blame] | 3516 | |
| 3517 | * .. js:function:: FILTER.new() |
| 3518 | |
| 3519 | Called to instantiate a new filter. This function must be defined. |
| 3520 | |
| 3521 | :returns: a Lua object that will be used as filter instance for the current |
| 3522 | stream. |
| 3523 | |
| 3524 | * .. js:function:: FILTER.start_analyze(flt, txn, chn) |
| 3525 | |
| 3526 | Called when the analysis starts on the channel **chn**. |
| 3527 | |
| 3528 | * .. js:function:: FILTER.end_analyze(flt, txn, chn) |
| 3529 | |
| 3530 | Called when the analysis ends on the channel **chn**. |
| 3531 | |
| 3532 | * .. js:function:: FILTER.http_headers(flt, txn, http_msg) |
| 3533 | |
| 3534 | Called just before the HTTP payload analysis and after any processing on the |
| 3535 | HTTP message **http_msg**. This callback functions is only called for HTTP |
| 3536 | streams. |
| 3537 | |
| 3538 | * .. js:function:: FILTER.http_payload(flt, txn, http_msg) |
| 3539 | |
| 3540 | Called during the HTTP payload analysis on the HTTP message **http_msg**. This |
| 3541 | callback functions is only called for HTTP streams. |
| 3542 | |
| 3543 | * .. js:function:: FILTER.http_end(flt, txn, http_msg) |
| 3544 | |
| 3545 | Called after the HTTP payload analysis on the HTTP message **http_msg**. This |
| 3546 | callback functions is only called for HTTP streams. |
| 3547 | |
| 3548 | * .. js:function:: FILTER.tcp_payload(flt, txn, chn) |
| 3549 | |
| 3550 | Called during the TCP payload analysis on the channel **chn**. |
| 3551 | |
Aurelien DARRAGON | 53901f4 | 2022-10-13 19:49:42 +0200 | [diff] [blame] | 3552 | Here is a full example: |
Christopher Faulet | 5a2c661 | 2021-08-15 20:35:25 +0200 | [diff] [blame] | 3553 | |
| 3554 | .. code-block:: lua |
| 3555 | |
| 3556 | Trace = {} |
| 3557 | Trace.id = "Lua trace filter" |
| 3558 | Trace.flags = filter.FLT_CFG_FL_HTX; |
| 3559 | Trace.__index = Trace |
| 3560 | |
| 3561 | function Trace:new() |
| 3562 | local trace = {} |
| 3563 | setmetatable(trace, Trace) |
| 3564 | trace.req_len = 0 |
| 3565 | trace.res_len = 0 |
| 3566 | return trace |
| 3567 | end |
| 3568 | |
| 3569 | function Trace:start_analyze(txn, chn) |
| 3570 | if chn:is_resp() then |
| 3571 | print("Start response analysis") |
| 3572 | else |
| 3573 | print("Start request analysis") |
| 3574 | end |
| 3575 | filter.register_data_filter(self, chn) |
| 3576 | end |
| 3577 | |
| 3578 | function Trace:end_analyze(txn, chn) |
| 3579 | if chn:is_resp() then |
| 3580 | print("End response analysis: "..self.res_len.." bytes filtered") |
| 3581 | else |
| 3582 | print("End request analysis: "..self.req_len.." bytes filtered") |
| 3583 | end |
| 3584 | end |
| 3585 | |
| 3586 | function Trace:http_headers(txn, http_msg) |
| 3587 | stline = http_msg:get_stline() |
| 3588 | if http_msg.channel:is_resp() then |
| 3589 | print("response:") |
| 3590 | print(stline.version.." "..stline.code.." "..stline.reason) |
| 3591 | else |
| 3592 | print("request:") |
| 3593 | print(stline.method.." "..stline.uri.." "..stline.version) |
| 3594 | end |
| 3595 | |
| 3596 | for n, hdrs in pairs(http_msg:get_headers()) do |
| 3597 | for i,v in pairs(hdrs) do |
| 3598 | print(n..": "..v) |
| 3599 | end |
| 3600 | end |
| 3601 | return filter.CONTINUE |
| 3602 | end |
| 3603 | |
| 3604 | function Trace:http_payload(txn, http_msg) |
| 3605 | body = http_msg:body(-20000) |
| 3606 | if http_msg.channel:is_resp() then |
| 3607 | self.res_len = self.res_len + body:len() |
| 3608 | else |
| 3609 | self.req_len = self.req_len + body:len() |
| 3610 | end |
| 3611 | end |
| 3612 | |
| 3613 | core.register_filter("trace", Trace, function(trace, args) |
| 3614 | return trace |
| 3615 | end) |
| 3616 | |
| 3617 | .. |
| 3618 | |
| 3619 | .. _httpmessage_class: |
| 3620 | |
| 3621 | HTTPMessage class |
| 3622 | =================== |
| 3623 | |
| 3624 | .. js:class:: HTTPMessage |
| 3625 | |
| 3626 | **context**: filter |
| 3627 | |
Aurelien DARRAGON | 53901f4 | 2022-10-13 19:49:42 +0200 | [diff] [blame] | 3628 | 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] | 3629 | class is only available from a filter context. |
| 3630 | |
| 3631 | .. js:function:: HTTPMessage.add_header(http_msg, name, value) |
| 3632 | |
Aurelien DARRAGON | 53901f4 | 2022-10-13 19:49:42 +0200 | [diff] [blame] | 3633 | 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] | 3634 | specified in **name** and whose value is defined in **value**. |
| 3635 | |
| 3636 | :param class_httpmessage http_msg: The manipulated HTTP message. |
| 3637 | :param string name: The header name. |
| 3638 | :param string value: The header value. |
| 3639 | |
| 3640 | .. js:function:: HTTPMessage.append(http_msg, string) |
| 3641 | |
| 3642 | This function copies the string **string** at the end of incoming data of the |
| 3643 | HTTP message **http_msg**. The function returns the copied length on success |
| 3644 | or -1 if data cannot be copied. |
| 3645 | |
| 3646 | Same that :js:func:`HTTPMessage.insert(http_msg, string, http_msg:input())`. |
| 3647 | |
| 3648 | :param class_httpmessage http_msg: The manipulated HTTP message. |
Aurelien DARRAGON | 53901f4 | 2022-10-13 19:49:42 +0200 | [diff] [blame] | 3649 | :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] | 3650 | :returns: an integer containing the amount of bytes copied or -1. |
| 3651 | |
| 3652 | .. js:function:: HTTPMessage.body(http_msgl[, offset[, length]]) |
| 3653 | |
| 3654 | This function returns **length** bytes of incoming data from the HTTP message |
| 3655 | **http_msg**, starting at the offset **offset**. The data are not removed from |
| 3656 | the buffer. |
| 3657 | |
| 3658 | By default, if no length is provided, all incoming data found, starting at the |
| 3659 | given offset, are returned. If **length** is set to -1, the function tries to |
| 3660 | 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] | 3661 | 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] | 3662 | positive offset is relative to the beginning of incoming data of the |
Christopher Faulet | 5a2c661 | 2021-08-15 20:35:25 +0200 | [diff] [blame] | 3663 | http_message buffer while negative offset is relative to their end. |
| 3664 | |
| 3665 | If there is no incoming data and the HTTP message can't receive more data, a 'nil' |
| 3666 | value is returned. |
| 3667 | |
| 3668 | :param class_httpmessage http_msg: The manipulated HTTP message. |
| 3669 | :param integer offset: *optional* The offset in incoming data to start to get |
| 3670 | data. 0 by default. May be negative to be relative to |
| 3671 | the end of incoming data. |
| 3672 | :param integer length: *optional* The expected length of data to retrieve. All |
| 3673 | incoming data by default. May be set to -1 to get a |
| 3674 | maximum of data. |
| 3675 | :returns: a string containing the data found or nil. |
| 3676 | |
| 3677 | .. js:function:: HTTPMessage.eom(http_msg) |
| 3678 | |
| 3679 | This function returns true if the end of message is reached for the HTTP |
| 3680 | message **http_msg**. |
| 3681 | |
| 3682 | :param class_httpmessage http_msg: The manipulated HTTP message. |
| 3683 | :returns: an integer containing the amount of available bytes. |
| 3684 | |
| 3685 | .. js:function:: HTTPMessage.del_header(http_msg, name) |
| 3686 | |
| 3687 | Removes all HTTP header fields in the HTTP message **http_msg** whose name is |
| 3688 | specified in **name**. |
| 3689 | |
| 3690 | :param class_httpmessage http_msg: The manipulated http message. |
| 3691 | :param string name: The header name. |
| 3692 | |
| 3693 | .. js:function:: HTTPMessage.get_headers(http_msg) |
| 3694 | |
| 3695 | Returns a table containing all the headers of the HTTP message **http_msg**. |
| 3696 | |
| 3697 | :param class_httpmessage http_msg: The manipulated http message. |
| 3698 | :returns: table of headers. |
| 3699 | |
| 3700 | This is the form of the returned table: |
| 3701 | |
| 3702 | .. code-block:: lua |
| 3703 | |
| 3704 | http_msg:get_headers()['<header-name>'][<header-index>] = "<header-value>" |
| 3705 | |
| 3706 | local hdr = http_msg:get_headers() |
| 3707 | hdr["host"][0] = "www.test.com" |
| 3708 | hdr["accept"][0] = "audio/basic q=1" |
| 3709 | hdr["accept"][1] = "audio/*, q=0.2" |
| 3710 | hdr["accept"][2] = "*.*, q=0.1" |
| 3711 | .. |
| 3712 | |
| 3713 | .. js:function:: HTTPMessage.get_stline(http_msg) |
| 3714 | |
| 3715 | Returns a table containing the start-line of the HTTP message **http_msg**. |
| 3716 | |
| 3717 | :param class_httpmessage http_msg: The manipulated http message. |
| 3718 | :returns: the start-line. |
| 3719 | |
| 3720 | This is the form of the returned table: |
| 3721 | |
| 3722 | .. code-block:: lua |
| 3723 | |
| 3724 | -- for the request : |
| 3725 | {"method" = string, "uri" = string, "version" = string} |
| 3726 | |
| 3727 | -- for the response: |
| 3728 | {"version" = string, "code" = string, "reason" = string} |
| 3729 | .. |
| 3730 | |
| 3731 | .. js:function:: HTTPMessage.forward(http_msg, length) |
| 3732 | |
| 3733 | This function forwards **length** bytes of data from the HTTP message |
Aurelien DARRAGON | 53901f4 | 2022-10-13 19:49:42 +0200 | [diff] [blame] | 3734 | **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] | 3735 | available incoming data may be forwarded, event if the requested length |
| 3736 | exceeds the available amount of incoming data. It returns the amount of data |
| 3737 | forwarded. |
| 3738 | |
| 3739 | :param class_httpmessage http_msg: The manipulated HTTP message. |
| 3740 | :param integer int: The amount of data to forward. |
| 3741 | |
| 3742 | .. js:function:: HTTPMessage.input(http_msg) |
| 3743 | |
| 3744 | This function returns the length of incoming data in the HTTP message |
Aurelien DARRAGON | 53901f4 | 2022-10-13 19:49:42 +0200 | [diff] [blame] | 3745 | **http_msg** from the filter point of view. |
Christopher Faulet | 5a2c661 | 2021-08-15 20:35:25 +0200 | [diff] [blame] | 3746 | |
| 3747 | :param class_httpmessage http_msg: The manipulated HTTP message. |
| 3748 | :returns: an integer containing the amount of available bytes. |
| 3749 | |
| 3750 | .. js:function:: HTTPMessage.insert(http_msg, string[, offset]) |
| 3751 | |
| 3752 | This function copies the string **string** at the offset **offset** in |
| 3753 | incoming data of the HTTP message **http_msg**. The function returns the |
| 3754 | copied length on success or -1 if data cannot be copied. |
| 3755 | |
| 3756 | 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] | 3757 | 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] | 3758 | of the HTTP message while negative offset is relative to their end. |
| 3759 | |
| 3760 | :param class_httpmessage http_msg: The manipulated HTTP message. |
Aurelien DARRAGON | 53901f4 | 2022-10-13 19:49:42 +0200 | [diff] [blame] | 3761 | :param string string: The data to copy into incoming data. |
| 3762 | :param integer offset: *optional* The offset in incoming data where to copy |
Christopher Faulet | 5a2c661 | 2021-08-15 20:35:25 +0200 | [diff] [blame] | 3763 | data. 0 by default. May be negative to be relative to |
| 3764 | the end of incoming data. |
| 3765 | :returns: an integer containing the amount of bytes copied or -1. |
| 3766 | |
| 3767 | .. js:function:: HTTPMessage.is_full(http_msg) |
| 3768 | |
| 3769 | This function returns true if the HTTP message **http_msg** is full. |
| 3770 | |
| 3771 | :param class_httpmessage http_msg: The manipulated HTTP message. |
| 3772 | :returns: a boolean |
| 3773 | |
| 3774 | .. js:function:: HTTPMessage.is_resp(http_msg) |
| 3775 | |
| 3776 | This function returns true if the HTTP message **http_msg** is the response |
| 3777 | one. |
| 3778 | |
| 3779 | :param class_httpmessage http_msg: The manipulated HTTP message. |
| 3780 | :returns: a boolean |
| 3781 | |
| 3782 | .. js:function:: HTTPMessage.may_recv(http_msg) |
| 3783 | |
| 3784 | This function returns true if the HTTP message **http_msg** may still receive |
| 3785 | data. |
| 3786 | |
| 3787 | :param class_httpmessage http_msg: The manipulated HTTP message. |
| 3788 | :returns: a boolean |
| 3789 | |
| 3790 | .. js:function:: HTTPMessage.output(http_msg) |
| 3791 | |
| 3792 | This function returns the length of outgoing data of the HTTP message |
| 3793 | **http_msg**. |
| 3794 | |
| 3795 | :param class_httpmessage http_msg: The manipulated HTTP message. |
| 3796 | :returns: an integer containing the amount of available bytes. |
| 3797 | |
| 3798 | .. js:function:: HTTPMessage.prepend(http_msg, string) |
| 3799 | |
| 3800 | This function copies the string **string** in front of incoming data of the |
| 3801 | HTTP message **http_msg**. The function returns the copied length on success |
| 3802 | or -1 if data cannot be copied. |
| 3803 | |
| 3804 | Same that :js:func:`HTTPMessage.insert(http_msg, string, 0)`. |
| 3805 | |
| 3806 | :param class_httpmessage http_msg: The manipulated HTTP message. |
Aurelien DARRAGON | 53901f4 | 2022-10-13 19:49:42 +0200 | [diff] [blame] | 3807 | :param string string: The data to copy in front of incoming data. |
Christopher Faulet | 5a2c661 | 2021-08-15 20:35:25 +0200 | [diff] [blame] | 3808 | :returns: an integer containing the amount of bytes copied or -1. |
| 3809 | |
| 3810 | .. js:function:: HTTPMessage.remove(http_msg[, offset[, length]]) |
| 3811 | |
| 3812 | This function removes **length** bytes of incoming data of the HTTP message |
| 3813 | **http_msg**, starting at offset **offset**. This function returns number of |
| 3814 | bytes removed on success. |
| 3815 | |
| 3816 | 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] | 3817 | 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] | 3818 | 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] | 3819 | HTTP message while negative offset is relative to the end. |
Christopher Faulet | 5a2c661 | 2021-08-15 20:35:25 +0200 | [diff] [blame] | 3820 | |
| 3821 | :param class_httpmessage http_msg: The manipulated HTTP message. |
Aurelien DARRAGON | 53901f4 | 2022-10-13 19:49:42 +0200 | [diff] [blame] | 3822 | :param integer offset: *optional* The offset in incoming data where to start |
Christopher Faulet | 5a2c661 | 2021-08-15 20:35:25 +0200 | [diff] [blame] | 3823 | to remove data. 0 by default. May be negative to |
| 3824 | be relative to the end of incoming data. |
| 3825 | :param integer length: *optional* The length of data to remove. All incoming |
| 3826 | data by default. |
| 3827 | :returns: an integer containing the amount of bytes removed. |
| 3828 | |
| 3829 | .. js:function:: HTTPMessage.rep_header(http_msg, name, regex, replace) |
| 3830 | |
| 3831 | Matches the regular expression in all occurrences of header field **name** |
| 3832 | according to regex **regex**, and replaces them with the string **replace**. |
| 3833 | The replacement value can contain back references like \1, \2, ... This |
| 3834 | function acts on whole header lines, regardless of the number of values they |
| 3835 | may contain. |
| 3836 | |
| 3837 | :param class_httpmessage http_msg: The manipulated HTTP message. |
| 3838 | :param string name: The header name. |
| 3839 | :param string regex: The match regular expression. |
| 3840 | :param string replace: The replacement value. |
| 3841 | |
| 3842 | .. js:function:: HTTPMessage.rep_value(http_msg, name, regex, replace) |
| 3843 | |
| 3844 | Matches the regular expression on every comma-delimited value of header field |
| 3845 | **name** according to regex **regex**, and replaces them with the string |
| 3846 | **replace**. The replacement value can contain back references like \1, \2, |
| 3847 | ... |
| 3848 | |
| 3849 | :param class_httpmessage http_msg: The manipulated HTTP message. |
| 3850 | :param string name: The header name. |
| 3851 | :param string regex: The match regular expression. |
| 3852 | :param string replace: The replacement value. |
| 3853 | |
| 3854 | .. js:function:: HTTPMessage.send(http_msg, string) |
| 3855 | |
Aurelien DARRAGON | 53901f4 | 2022-10-13 19:49:42 +0200 | [diff] [blame] | 3856 | This function requires immediate send of the string **string**. It means the |
Ilya Shipitsin | ff0f278 | 2021-08-22 22:18:07 +0500 | [diff] [blame] | 3857 | 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] | 3858 | **http_msg** and immediately forwarded. Because it is called in the filter |
Aurelien DARRAGON | 53901f4 | 2022-10-13 19:49:42 +0200 | [diff] [blame] | 3859 | context, it never yields. |
Christopher Faulet | 5a2c661 | 2021-08-15 20:35:25 +0200 | [diff] [blame] | 3860 | |
| 3861 | :param class_httpmessage http_msg: The manipulated HTTP message. |
| 3862 | :param string string: The data to send. |
| 3863 | :returns: an integer containing the amount of bytes copied or -1. |
| 3864 | |
| 3865 | .. js:function:: HTTPMessage.set(http_msg, string[, offset[, length]]) |
| 3866 | |
Aurelien DARRAGON | 53901f4 | 2022-10-13 19:49:42 +0200 | [diff] [blame] | 3867 | This function replaces **length** bytes of incoming data of the HTTP message |
Christopher Faulet | 5a2c661 | 2021-08-15 20:35:25 +0200 | [diff] [blame] | 3868 | **http_msg**, starting at offset **offset**, by the string **string**. The |
| 3869 | function returns the copied length on success or -1 if data cannot be copied. |
| 3870 | |
| 3871 | 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] | 3872 | 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] | 3873 | 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] | 3874 | HTTP message while negative offset is relative to the end. |
Christopher Faulet | 5a2c661 | 2021-08-15 20:35:25 +0200 | [diff] [blame] | 3875 | |
| 3876 | :param class_httpmessage http_msg: The manipulated HTTP message. |
Aurelien DARRAGON | 53901f4 | 2022-10-13 19:49:42 +0200 | [diff] [blame] | 3877 | :param string string: The data to copy into incoming data. |
| 3878 | :param integer offset: *optional* The offset in incoming data where to start |
Christopher Faulet | 5a2c661 | 2021-08-15 20:35:25 +0200 | [diff] [blame] | 3879 | the data replacement. 0 by default. May be negative to |
| 3880 | be relative to the end of incoming data. |
| 3881 | :param integer length: *optional* The length of data to replace. All incoming |
| 3882 | data by default. |
| 3883 | :returns: an integer containing the amount of bytes copied or -1. |
| 3884 | |
| 3885 | .. js:function:: HTTPMessage.set_eom(http_msg) |
| 3886 | |
| 3887 | This function set the end of message for the HTTP message **http_msg**. |
| 3888 | |
| 3889 | :param class_httpmessage http_msg: The manipulated HTTP message. |
| 3890 | |
| 3891 | .. js:function:: HTTPMessage.set_header(http_msg, name, value) |
| 3892 | |
| 3893 | This variable replace all occurrence of all header matching the name **name**, |
| 3894 | by only one containing the value **value**. |
| 3895 | |
| 3896 | :param class_httpmessage http_msg: The manipulated HTTP message. |
| 3897 | :param string name: The header name. |
| 3898 | :param string value: The header value. |
| 3899 | |
| 3900 | This function does the same work as the following code: |
| 3901 | |
| 3902 | .. code-block:: lua |
| 3903 | |
| 3904 | http_msg:del_header("header") |
| 3905 | http_msg:add_header("header", "value") |
| 3906 | .. |
| 3907 | |
| 3908 | .. js:function:: HTTPMessage.set_method(http_msg, method) |
| 3909 | |
| 3910 | Rewrites the request method with the string **method**. 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_path(http_msg, path) |
| 3917 | |
| 3918 | Rewrites the request path with the string **path**. The HTTP message |
| 3919 | **http_msg** must be the request. |
| 3920 | |
| 3921 | :param class_httpmessage http_msg: The manipulated HTTP message. |
| 3922 | :param string method: The new method. |
| 3923 | |
| 3924 | .. js:function:: HTTPMessage.set_query(http_msg, query) |
| 3925 | |
| 3926 | Rewrites the request's query string which appears after the first question |
| 3927 | mark ("?") with the string **query**. The HTTP message **http_msg** must be |
| 3928 | the request. |
| 3929 | |
| 3930 | :param class_httpmessage http_msg: The manipulated HTTP message. |
| 3931 | :param string query: The new query. |
| 3932 | |
| 3933 | .. js:function:: HTTPMessage.set_status(http_msg, status[, reason]) |
| 3934 | |
Ilya Shipitsin | ff0f278 | 2021-08-22 22:18:07 +0500 | [diff] [blame] | 3935 | Rewrites the response status code with the integer **code** and optional the |
Christopher Faulet | 5a2c661 | 2021-08-15 20:35:25 +0200 | [diff] [blame] | 3936 | reason **reason**. If no custom reason is provided, it will be generated from |
| 3937 | the status. The HTTP message **http_msg** must be the response. |
| 3938 | |
| 3939 | :param class_httpmessage http_msg: The manipulated HTTP message. |
| 3940 | :param integer status: The new response status code. |
| 3941 | :param string reason: The new response reason (optional). |
| 3942 | |
| 3943 | .. js:function:: HTTPMessage.set_uri(http_msg, uri) |
| 3944 | |
| 3945 | Rewrites the request URI with the string **uri**. The HTTP message |
| 3946 | **http_msg** must be the request. |
| 3947 | |
| 3948 | :param class_httpmessage http_msg: The manipulated HTTP message. |
| 3949 | :param string uri: The new uri. |
| 3950 | |
| 3951 | .. js:function:: HTTPMessage.unset_eom(http_msg) |
| 3952 | |
| 3953 | This function remove the end of message for the HTTP message **http_msg**. |
| 3954 | |
| 3955 | :param class_httpmessage http_msg: The manipulated HTTP message. |
| 3956 | |
William Lallemand | 10cea5c | 2022-03-30 16:02:43 +0200 | [diff] [blame] | 3957 | .. _CertCache_class: |
| 3958 | |
| 3959 | CertCache class |
| 3960 | ================ |
| 3961 | |
| 3962 | .. js:class:: CertCache |
| 3963 | |
| 3964 | This class allows to update an SSL certificate file in the memory of the |
| 3965 | current HAProxy process. It will do the same as "set ssl cert" + "commit ssl |
| 3966 | cert" over the HAProxy CLI. |
| 3967 | |
| 3968 | .. js:function:: CertCache.set(certificate) |
| 3969 | |
| 3970 | This function updates a certificate in memory. |
| 3971 | |
| 3972 | :param table certificate: A table containing the fields to update. |
| 3973 | :param string certificate.filename: The mandatory filename of the certificate |
| 3974 | to update, it must already exist in memory. |
| 3975 | :param string certificate.crt: A certificate in the PEM format. It can also |
| 3976 | contain a private key. |
| 3977 | :param string certificate.key: A private key in the PEM format. |
| 3978 | :param string certificate.ocsp: An OCSP response in base64. (cf management.txt) |
| 3979 | :param string certificate.issuer: The certificate of the OCSP issuer. |
| 3980 | :param string certificate.sctl: An SCTL file. |
| 3981 | |
| 3982 | .. code-block:: lua |
| 3983 | |
| 3984 | CertCache.set{filename="certs/localhost9994.pem.rsa", crt=crt} |
| 3985 | |
| 3986 | |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 3987 | External Lua libraries |
| 3988 | ====================== |
| 3989 | |
| 3990 | A lot of useful lua libraries can be found here: |
| 3991 | |
Aurelien DARRAGON | 846fc7d | 2022-10-14 08:48:57 +0200 | [diff] [blame] | 3992 | * Lua toolbox has been superseded by `https://luarocks.org/ <https://luarocks.org/>`_ |
| 3993 | 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] | 3994 | |
Ilya Shipitsin | 2075ca8 | 2020-03-06 23:22:22 +0500 | [diff] [blame] | 3995 | Redis client library: |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 3996 | |
| 3997 | * `https://github.com/nrk/redis-lua <https://github.com/nrk/redis-lua>`_ |
| 3998 | |
Aurelien DARRAGON | 53901f4 | 2022-10-13 19:49:42 +0200 | [diff] [blame] | 3999 | This is an example about the usage of the Redis library within HAProxy. Note that |
| 4000 | 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] | 4001 | connection fails. |
| 4002 | |
| 4003 | .. code-block:: lua |
| 4004 | |
| 4005 | -- load the redis library |
| 4006 | local redis = require("redis"); |
| 4007 | |
| 4008 | function do_something(txn) |
| 4009 | |
| 4010 | -- create and connect new tcp socket |
| 4011 | local tcp = core.tcp(); |
| 4012 | tcp:settimeout(1); |
| 4013 | tcp:connect("127.0.0.1", 6379); |
| 4014 | |
| 4015 | -- use the redis library with this new socket |
| 4016 | local client = redis.connect({socket=tcp}); |
| 4017 | client:ping(); |
| 4018 | |
| 4019 | end |
| 4020 | |
| 4021 | OpenSSL: |
| 4022 | |
| 4023 | * `http://mkottman.github.io/luacrypto/index.html |
| 4024 | <http://mkottman.github.io/luacrypto/index.html>`_ |
| 4025 | |
| 4026 | * `https://github.com/brunoos/luasec/wiki |
| 4027 | <https://github.com/brunoos/luasec/wiki>`_ |