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