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