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