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