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