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