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