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 |
| 23 | functions. Lua have 6 execution context. |
| 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 | |
| 58 | **NOTE**: It is possible that this function cannot found the required data |
| 59 | in the original HAProxy sample-fetches, in this case, it cannot return the |
| 60 | result. This case is not yet supported |
| 61 | |
David Carlier | 61fdf8b | 2015-10-02 11:59:38 +0100 | [diff] [blame] | 62 | 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] | 63 | and returns another string as output. These types of function are stateless, |
| 64 | it cannot access to any context. They don't execute any blocking function. |
| 65 | The call prototype is `function string fcn(string)`. This function can be |
| 66 | registered with the Lua function `core.register_converters()`. Each declared |
| 67 | converter is prefixed by the string "lua.". |
| 68 | |
| 69 | HAProxy Lua Hello world |
| 70 | ----------------------- |
| 71 | |
| 72 | HAProxy configuration file (`hello_world.conf`): |
| 73 | |
| 74 | :: |
| 75 | |
| 76 | global |
| 77 | lua-load hello_world.lua |
| 78 | |
| 79 | listen proxy |
| 80 | bind 127.0.0.1:10001 |
Thierry FOURNIER | a2d0225 | 2015-10-01 15:00:42 +0200 | [diff] [blame] | 81 | tcp-request inspect-delay 1s |
| 82 | tcp-request content use-service lua.hello_world |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 83 | |
| 84 | HAProxy Lua file (`hello_world.lua`): |
| 85 | |
| 86 | .. code-block:: lua |
| 87 | |
Thierry FOURNIER | a2d0225 | 2015-10-01 15:00:42 +0200 | [diff] [blame] | 88 | core.register_service("hello_world", "tcp", function(applet) |
| 89 | applet:send("hello world\n") |
| 90 | end) |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 91 | |
| 92 | How to start HAProxy for testing this configuration: |
| 93 | |
| 94 | :: |
| 95 | |
| 96 | ./haproxy -f hello_world.conf |
| 97 | |
| 98 | On other terminal, you can test with telnet: |
| 99 | |
| 100 | :: |
| 101 | |
| 102 | #:~ telnet 127.0.0.1 10001 |
| 103 | hello world |
| 104 | |
| 105 | Core class |
| 106 | ========== |
| 107 | |
| 108 | .. js:class:: core |
| 109 | |
| 110 | The "core" class contains all the HAProxy core functions. These function are |
| 111 | useful for the controlling the execution flow, registering hooks, manipulating |
| 112 | global maps or ACL, ... |
| 113 | |
| 114 | "core" class is basically provided with HAProxy. No `require` line is |
| 115 | required to uses these function. |
| 116 | |
David Carlier | 61fdf8b | 2015-10-02 11:59:38 +0100 | [diff] [blame] | 117 | 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] | 118 | type. |
| 119 | |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 120 | .. js:attribute:: core.emerg |
| 121 | |
Thierry FOURNIER | dc59500 | 2015-12-21 11:13:52 +0100 | [diff] [blame] | 122 | :returns: integer |
| 123 | |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 124 | This attribute is an integer, it contains the value of the loglevel "emergency" (0). |
| 125 | |
| 126 | .. js:attribute:: core.alert |
| 127 | |
Thierry FOURNIER | dc59500 | 2015-12-21 11:13:52 +0100 | [diff] [blame] | 128 | :returns: integer |
| 129 | |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 130 | This attribute is an integer, it contains the value of the loglevel "alert" (1). |
| 131 | |
| 132 | .. js:attribute:: core.crit |
| 133 | |
Thierry FOURNIER | dc59500 | 2015-12-21 11:13:52 +0100 | [diff] [blame] | 134 | :returns: integer |
| 135 | |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 136 | This attribute is an integer, it contains the value of the loglevel "critical" (2). |
| 137 | |
| 138 | .. js:attribute:: core.err |
| 139 | |
Thierry FOURNIER | dc59500 | 2015-12-21 11:13:52 +0100 | [diff] [blame] | 140 | :returns: integer |
| 141 | |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 142 | This attribute is an integer, it contains the value of the loglevel "error" (3). |
| 143 | |
| 144 | .. js:attribute:: core.warning |
| 145 | |
Thierry FOURNIER | dc59500 | 2015-12-21 11:13:52 +0100 | [diff] [blame] | 146 | :returns: integer |
| 147 | |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 148 | This attribute is an integer, it contains the value of the loglevel "warning" (4). |
| 149 | |
| 150 | .. js:attribute:: core.notice |
| 151 | |
Thierry FOURNIER | dc59500 | 2015-12-21 11:13:52 +0100 | [diff] [blame] | 152 | :returns: integer |
| 153 | |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 154 | This attribute is an integer, it contains the value of the loglevel "notice" (5). |
| 155 | |
| 156 | .. js:attribute:: core.info |
| 157 | |
Thierry FOURNIER | dc59500 | 2015-12-21 11:13:52 +0100 | [diff] [blame] | 158 | :returns: integer |
| 159 | |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 160 | This attribute is an integer, it contains the value of the loglevel "info" (6). |
| 161 | |
| 162 | .. js:attribute:: core.debug |
| 163 | |
Thierry FOURNIER | dc59500 | 2015-12-21 11:13:52 +0100 | [diff] [blame] | 164 | :returns: integer |
| 165 | |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 166 | This attribute is an integer, it contains the value of the loglevel "debug" (7). |
| 167 | |
Thierry FOURNIER | 12a865d | 2016-12-14 19:40:37 +0100 | [diff] [blame] | 168 | .. js:attribute:: core.proxies |
| 169 | |
| 170 | **context**: task, action, sample-fetch, converter |
| 171 | |
Patrick Hemmer | c6a1d71 | 2018-05-01 21:30:41 -0400 | [diff] [blame] | 172 | This attribute is a table of declared proxies (frontend and backends). Each |
| 173 | proxy give an access to his list of listeners and servers. The table is |
| 174 | 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] | 175 | |
Thierry FOURNIER | 9b82a58 | 2017-07-24 13:30:43 +0200 | [diff] [blame] | 176 | Warning, if you are declared frontend and backend with the same name, only one |
| 177 | of these are listed. |
| 178 | |
| 179 | :see: :js:attr:`core.backends` |
| 180 | :see: :js:attr:`core.frontends` |
| 181 | |
| 182 | .. js:attribute:: core.backends |
| 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 with backend capability. Each |
| 187 | proxy give an access to his list of listeners and servers. The table is |
| 188 | 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] | 189 | |
| 190 | :see: :js:attr:`core.proxies` |
| 191 | :see: :js:attr:`core.frontends` |
| 192 | |
| 193 | .. js:attribute:: core.frontends |
| 194 | |
| 195 | **context**: task, action, sample-fetch, converter |
| 196 | |
Patrick Hemmer | c6a1d71 | 2018-05-01 21:30:41 -0400 | [diff] [blame] | 197 | This attribute is a table of declared proxies with frontend capability. Each |
| 198 | proxy give an access to his list of listeners and servers. The table is |
| 199 | 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] | 200 | |
| 201 | :see: :js:attr:`core.proxies` |
| 202 | :see: :js:attr:`core.backends` |
| 203 | |
Thierry Fournier | ecb83c2 | 2020-11-28 15:49:44 +0100 | [diff] [blame] | 204 | .. js:attribute:: core.thread |
| 205 | |
| 206 | **context**: task, action, sample-fetch, converter, applet |
| 207 | |
| 208 | This variable contains the executing thread number starting at 1. 0 is a |
| 209 | special case for the common lua context. So, if thread is 0, Lua scope is |
| 210 | shared by all threads, otherwise the scope is dedicated to a single thread. |
| 211 | A program which needs to execute some parts exactly once regardless of the |
| 212 | number of threads can check that core.thread is 0 or 1. |
| 213 | |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 214 | .. js:function:: core.log(loglevel, msg) |
| 215 | |
| 216 | **context**: body, init, task, action, sample-fetch, converter |
| 217 | |
David Carlier | 61fdf8b | 2015-10-02 11:59:38 +0100 | [diff] [blame] | 218 | 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] | 219 | configuration file, on the default syslog server if it is configured and on |
| 220 | the stderr if it is allowed. |
| 221 | |
Bertrand Jacquin | 874a35c | 2018-09-10 21:26:07 +0100 | [diff] [blame] | 222 | :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] | 223 | number between 0 and 7. |
| 224 | :param string msg: The log content. |
Thierry FOURNIER | 12a865d | 2016-12-14 19:40:37 +0100 | [diff] [blame] | 225 | :see: :js:attr:`core.emerg`, :js:attr:`core.alert`, :js:attr:`core.crit`, |
| 226 | :js:attr:`core.err`, :js:attr:`core.warning`, :js:attr:`core.notice`, |
| 227 | :js:attr:`core.info`, :js:attr:`core.debug` (log level definitions) |
| 228 | :see: :js:func:`core.Debug` |
| 229 | :see: :js:func:`core.Info` |
| 230 | :see: :js:func:`core.Warning` |
| 231 | :see: :js:func:`core.Alert` |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 232 | |
| 233 | .. js:function:: core.Debug(msg) |
| 234 | |
| 235 | **context**: body, init, task, action, sample-fetch, converter |
| 236 | |
| 237 | :param string msg: The log content. |
Thierry FOURNIER | 12a865d | 2016-12-14 19:40:37 +0100 | [diff] [blame] | 238 | :see: :js:func:`core.log` |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 239 | |
| 240 | Does the same job than: |
| 241 | |
| 242 | .. code-block:: lua |
| 243 | |
Thierry FOURNIER | dc59500 | 2015-12-21 11:13:52 +0100 | [diff] [blame] | 244 | function Debug(msg) |
| 245 | core.log(core.debug, msg) |
| 246 | end |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 247 | .. |
| 248 | |
| 249 | .. js:function:: core.Info(msg) |
| 250 | |
| 251 | **context**: body, init, task, action, sample-fetch, converter |
| 252 | |
| 253 | :param string msg: The log content. |
Thierry FOURNIER | 12a865d | 2016-12-14 19:40:37 +0100 | [diff] [blame] | 254 | :see: :js:func:`core.log` |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 255 | |
| 256 | .. code-block:: lua |
| 257 | |
Thierry FOURNIER | dc59500 | 2015-12-21 11:13:52 +0100 | [diff] [blame] | 258 | function Info(msg) |
| 259 | core.log(core.info, msg) |
| 260 | end |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 261 | .. |
| 262 | |
| 263 | .. js:function:: core.Warning(msg) |
| 264 | |
| 265 | **context**: body, init, task, action, sample-fetch, converter |
| 266 | |
| 267 | :param string msg: The log content. |
Thierry FOURNIER | 12a865d | 2016-12-14 19:40:37 +0100 | [diff] [blame] | 268 | :see: :js:func:`core.log` |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 269 | |
| 270 | .. code-block:: lua |
| 271 | |
Thierry FOURNIER | dc59500 | 2015-12-21 11:13:52 +0100 | [diff] [blame] | 272 | function Warning(msg) |
| 273 | core.log(core.warning, msg) |
| 274 | end |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 275 | .. |
| 276 | |
| 277 | .. js:function:: core.Alert(msg) |
| 278 | |
| 279 | **context**: body, init, task, action, sample-fetch, converter |
| 280 | |
| 281 | :param string msg: The log content. |
Thierry FOURNIER | 12a865d | 2016-12-14 19:40:37 +0100 | [diff] [blame] | 282 | :see: :js:func:`core.log` |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 283 | |
| 284 | .. code-block:: lua |
| 285 | |
Thierry FOURNIER | dc59500 | 2015-12-21 11:13:52 +0100 | [diff] [blame] | 286 | function Alert(msg) |
| 287 | core.log(core.alert, msg) |
| 288 | end |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 289 | .. |
| 290 | |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 291 | .. js:function:: core.add_acl(filename, key) |
| 292 | |
| 293 | **context**: init, task, action, sample-fetch, converter |
| 294 | |
| 295 | Add the ACL *key* in the ACLs list referenced by the file *filename*. |
| 296 | |
| 297 | :param string filename: the filename that reference the ACL entries. |
| 298 | :param string key: the key which will be added. |
| 299 | |
| 300 | .. js:function:: core.del_acl(filename, key) |
| 301 | |
| 302 | **context**: init, task, action, sample-fetch, converter |
| 303 | |
| 304 | Delete the ACL entry referenced by the key *key* in the list of ACLs |
| 305 | referenced by *filename*. |
| 306 | |
| 307 | :param string filename: the filename that reference the ACL entries. |
| 308 | :param string key: the key which will be deleted. |
| 309 | |
| 310 | .. js:function:: core.del_map(filename, key) |
| 311 | |
| 312 | **context**: init, task, action, sample-fetch, converter |
| 313 | |
| 314 | Delete the map entry indexed with the specified key in the list of maps |
| 315 | referenced by his filename. |
| 316 | |
| 317 | :param string filename: the filename that reference the map entries. |
| 318 | :param string key: the key which will be deleted. |
| 319 | |
Thierry Fournier | eea77c0 | 2016-03-18 08:47:13 +0100 | [diff] [blame] | 320 | .. js:function:: core.get_info() |
| 321 | |
| 322 | **context**: body, init, task, action, sample-fetch, converter |
| 323 | |
Ilya Shipitsin | 2075ca8 | 2020-03-06 23:22:22 +0500 | [diff] [blame] | 324 | Returns HAProxy core information. We can found information like the uptime, |
Thierry Fournier | eea77c0 | 2016-03-18 08:47:13 +0100 | [diff] [blame] | 325 | the pid, memory pool usage, tasks number, ... |
| 326 | |
Bertrand Jacquin | 874a35c | 2018-09-10 21:26:07 +0100 | [diff] [blame] | 327 | These information are also returned by the management socket via the command |
| 328 | "show info". See the management socket documentation for more information |
Thierry Fournier | eea77c0 | 2016-03-18 08:47:13 +0100 | [diff] [blame] | 329 | about the content of these variables. |
| 330 | |
| 331 | :returns: an array of values. |
| 332 | |
Thierry Fournier | b1f4656 | 2016-01-21 09:46:15 +0100 | [diff] [blame] | 333 | .. js:function:: core.now() |
| 334 | |
| 335 | **context**: body, init, task, action |
| 336 | |
| 337 | 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] | 338 | 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] | 339 | call 'gettimeofday' will not be called too. The time is refreshed between each |
| 340 | Lua execution or resume, so two consecutive call to the function "now" will |
| 341 | probably returns the same result. |
| 342 | |
Patrick Hemmer | c6a1d71 | 2018-05-01 21:30:41 -0400 | [diff] [blame] | 343 | :returns: a table which contains two entries "sec" and "usec". "sec" |
Thierry Fournier | b1f4656 | 2016-01-21 09:46:15 +0100 | [diff] [blame] | 344 | contains the current at the epoch format, and "usec" contains the |
| 345 | current microseconds. |
| 346 | |
Thierry FOURNIER | a78f037 | 2016-12-14 19:04:41 +0100 | [diff] [blame] | 347 | .. js:function:: core.http_date(date) |
| 348 | |
| 349 | **context**: body, init, task, action |
| 350 | |
Bertrand Jacquin | 874a35c | 2018-09-10 21:26:07 +0100 | [diff] [blame] | 351 | This function take a string representing http date, and returns an integer |
Thierry FOURNIER | a78f037 | 2016-12-14 19:04:41 +0100 | [diff] [blame] | 352 | containing the corresponding date with a epoch format. A valid http date |
| 353 | me respect the format IMF, RFC850 or ASCTIME. |
| 354 | |
| 355 | :param string date: a date http-date formatted |
| 356 | :returns: integer containing epoch date |
| 357 | :see: :js:func:`core.imf_date`. |
| 358 | :see: :js:func:`core.rfc850_date`. |
| 359 | :see: :js:func:`core.asctime_date`. |
| 360 | :see: https://tools.ietf.org/html/rfc7231#section-7.1.1.1 |
| 361 | |
| 362 | .. js:function:: core.imf_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 IMF 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. |
| 368 | |
| 369 | :param string date: a date IMF formatted |
| 370 | :returns: integer containing epoch date |
| 371 | :see: https://tools.ietf.org/html/rfc7231#section-7.1.1.1 |
| 372 | |
| 373 | The IMF format is like this: |
| 374 | |
| 375 | .. code-block:: text |
| 376 | |
| 377 | Sun, 06 Nov 1994 08:49:37 GMT |
| 378 | .. |
| 379 | |
| 380 | .. js:function:: core.rfc850_date(date) |
| 381 | |
| 382 | **context**: body, init, task, action |
| 383 | |
Bertrand Jacquin | 874a35c | 2018-09-10 21:26:07 +0100 | [diff] [blame] | 384 | This function take a string representing RFC850 date, and returns an integer |
Thierry FOURNIER | a78f037 | 2016-12-14 19:04:41 +0100 | [diff] [blame] | 385 | containing the corresponding date with a epoch format. |
| 386 | |
| 387 | :param string date: a date RFC859 formatted |
| 388 | :returns: integer containing epoch date |
| 389 | :see: https://tools.ietf.org/html/rfc7231#section-7.1.1.1 |
| 390 | |
| 391 | The RFC850 format is like this: |
| 392 | |
| 393 | .. code-block:: text |
| 394 | |
| 395 | Sunday, 06-Nov-94 08:49:37 GMT |
| 396 | .. |
| 397 | |
| 398 | .. js:function:: core.asctime_date(date) |
| 399 | |
| 400 | **context**: body, init, task, action |
| 401 | |
Bertrand Jacquin | 874a35c | 2018-09-10 21:26:07 +0100 | [diff] [blame] | 402 | This function take a string representing ASCTIME date, and returns an integer |
Thierry FOURNIER | a78f037 | 2016-12-14 19:04:41 +0100 | [diff] [blame] | 403 | containing the corresponding date with a epoch format. |
| 404 | |
| 405 | :param string date: a date ASCTIME formatted |
| 406 | :returns: integer containing epoch date |
| 407 | :see: https://tools.ietf.org/html/rfc7231#section-7.1.1.1 |
| 408 | |
| 409 | The ASCTIME format is like this: |
| 410 | |
| 411 | .. code-block:: text |
| 412 | |
| 413 | Sun Nov 6 08:49:37 1994 |
| 414 | .. |
| 415 | |
| 416 | .. js:function:: core.rfc850_date(date) |
| 417 | |
| 418 | **context**: body, init, task, action |
| 419 | |
Bertrand Jacquin | 874a35c | 2018-09-10 21:26:07 +0100 | [diff] [blame] | 420 | This function take a string representing http date, and returns an integer |
Thierry FOURNIER | a78f037 | 2016-12-14 19:04:41 +0100 | [diff] [blame] | 421 | containing the corresponding date with a epoch format. |
| 422 | |
| 423 | :param string date: a date http-date formatted |
| 424 | |
| 425 | .. js:function:: core.asctime_date(date) |
| 426 | |
| 427 | **context**: body, init, task, action |
| 428 | |
Bertrand Jacquin | 874a35c | 2018-09-10 21:26:07 +0100 | [diff] [blame] | 429 | This function take a string representing http date, and returns an integer |
Thierry FOURNIER | a78f037 | 2016-12-14 19:04:41 +0100 | [diff] [blame] | 430 | containing the corresponding date with a epoch format. |
| 431 | |
| 432 | :param string date: a date http-date formatted |
| 433 | |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 434 | .. js:function:: core.msleep(milliseconds) |
| 435 | |
| 436 | **context**: body, init, task, action |
| 437 | |
| 438 | The `core.msleep()` stops the Lua execution between specified milliseconds. |
| 439 | |
| 440 | :param integer milliseconds: the required milliseconds. |
| 441 | |
Thierry Fournier | f61aa63 | 2016-02-19 20:56:00 +0100 | [diff] [blame] | 442 | .. js:attribute:: core.proxies |
| 443 | |
| 444 | **context**: body, init, task, action, sample-fetch, converter |
| 445 | |
Bertrand Jacquin | 874a35c | 2018-09-10 21:26:07 +0100 | [diff] [blame] | 446 | 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] | 447 | configuration file. The table is indexed by the proxy name, and each entry |
| 448 | of the proxies table is an object of type :ref:`proxy_class`. |
| 449 | |
| 450 | Warning, if you have declared a frontend and backend with the same name, only |
| 451 | one of these are listed. |
Thierry Fournier | f61aa63 | 2016-02-19 20:56:00 +0100 | [diff] [blame] | 452 | |
Thierry FOURNIER | c5d11c6 | 2018-02-12 14:46:54 +0100 | [diff] [blame] | 453 | .. js:function:: core.register_action(name, actions, func [, nb_args]) |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 454 | |
| 455 | **context**: body |
| 456 | |
David Carlier | 61fdf8b | 2015-10-02 11:59:38 +0100 | [diff] [blame] | 457 | 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] | 458 | used in HAProxy with the prefix "lua.". An action gets a TXN object class as |
| 459 | input. |
| 460 | |
| 461 | :param string name: is the name of the converter. |
Willy Tarreau | 61add3c | 2015-09-28 15:39:10 +0200 | [diff] [blame] | 462 | :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] | 463 | want to register to. The expected actions are 'tcp-req', |
| 464 | 'tcp-res', 'http-req' or 'http-res'. |
Thierry FOURNIER | c5d11c6 | 2018-02-12 14:46:54 +0100 | [diff] [blame] | 465 | :param integer nb_args: is the expected number of argument for the action. |
| 466 | By default the value is 0. |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 467 | :param function func: is the Lua function called to work as converter. |
| 468 | |
| 469 | The prototype of the Lua function used as argument is: |
| 470 | |
| 471 | .. code-block:: lua |
| 472 | |
Thierry FOURNIER | c5d11c6 | 2018-02-12 14:46:54 +0100 | [diff] [blame] | 473 | function(txn [, arg1 [, arg2]]) |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 474 | .. |
| 475 | |
Thierry FOURNIER | dc59500 | 2015-12-21 11:13:52 +0100 | [diff] [blame] | 476 | * **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] | 477 | current request or TCP stream. |
| 478 | |
Bertrand Jacquin | 874a35c | 2018-09-10 21:26:07 +0100 | [diff] [blame] | 479 | * **argX**: this is argument provided through the HAProxy configuration file. |
Thierry FOURNIER | c5d11c6 | 2018-02-12 14:46:54 +0100 | [diff] [blame] | 480 | |
Bertrand Jacquin | 874a35c | 2018-09-10 21:26:07 +0100 | [diff] [blame] | 481 | 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] | 482 | in the logs. |
| 483 | |
| 484 | .. code-block:: lua |
| 485 | |
| 486 | core.register_action("hello-world", { "tcp-req", "http-req" }, function(txn) |
| 487 | txn:Info("Hello world") |
| 488 | end) |
| 489 | .. |
| 490 | |
| 491 | This example code is used in HAproxy configuration like this: |
| 492 | |
| 493 | :: |
| 494 | |
| 495 | frontend tcp_frt |
| 496 | mode tcp |
| 497 | tcp-request content lua.hello-world |
| 498 | |
| 499 | frontend http_frt |
| 500 | mode http |
| 501 | http-request lua.hello-world |
Thierry FOURNIER | c5d11c6 | 2018-02-12 14:46:54 +0100 | [diff] [blame] | 502 | .. |
| 503 | |
Bertrand Jacquin | 874a35c | 2018-09-10 21:26:07 +0100 | [diff] [blame] | 504 | A second example using arguments |
Thierry FOURNIER | c5d11c6 | 2018-02-12 14:46:54 +0100 | [diff] [blame] | 505 | |
| 506 | .. code-block:: lua |
| 507 | |
| 508 | function hello_world(txn, arg) |
| 509 | txn:Info("Hello world for " .. arg) |
| 510 | end |
| 511 | core.register_action("hello-world", { "tcp-req", "http-req" }, hello_world, 2) |
| 512 | .. |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 513 | |
Thierry FOURNIER | c5d11c6 | 2018-02-12 14:46:54 +0100 | [diff] [blame] | 514 | This example code is used in HAproxy configuration like this: |
| 515 | |
| 516 | :: |
| 517 | |
| 518 | frontend tcp_frt |
| 519 | mode tcp |
| 520 | tcp-request content lua.hello-world everybody |
| 521 | .. |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 522 | .. js:function:: core.register_converters(name, func) |
| 523 | |
| 524 | **context**: body |
| 525 | |
David Carlier | 61fdf8b | 2015-10-02 11:59:38 +0100 | [diff] [blame] | 526 | Register a Lua function executed as converter. All the registered converters |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 527 | can be used in HAProxy with the prefix "lua.". An converter get a string as |
| 528 | input and return a string as output. The registered function can take up to 9 |
| 529 | values as parameter. All the value are strings. |
| 530 | |
| 531 | :param string name: is the name of the converter. |
| 532 | :param function func: is the Lua function called to work as converter. |
| 533 | |
| 534 | The prototype of the Lua function used as argument is: |
| 535 | |
| 536 | .. code-block:: lua |
| 537 | |
| 538 | function(str, [p1 [, p2 [, ... [, p5]]]]) |
| 539 | .. |
| 540 | |
| 541 | * **str** (*string*): this is the input value automatically converted in |
| 542 | string. |
| 543 | * **p1** .. **p5** (*string*): this is a list of string arguments declared in |
Bertrand Jacquin | 874a35c | 2018-09-10 21:26:07 +0100 | [diff] [blame] | 544 | the HAProxy configuration file. The number of arguments doesn't exceed 5. |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 545 | The order and the nature of these is conventionally choose by the |
Bertrand Jacquin | 874a35c | 2018-09-10 21:26:07 +0100 | [diff] [blame] | 546 | developer. |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 547 | |
| 548 | .. js:function:: core.register_fetches(name, func) |
| 549 | |
| 550 | **context**: body |
| 551 | |
David Carlier | 61fdf8b | 2015-10-02 11:59:38 +0100 | [diff] [blame] | 552 | Register a Lua function executed as sample fetch. All the registered sample |
Bertrand Jacquin | 874a35c | 2018-09-10 21:26:07 +0100 | [diff] [blame] | 553 | 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] | 554 | return a string as output. The registered function can take up to 9 values as |
| 555 | parameter. All the value are strings. |
| 556 | |
| 557 | :param string name: is the name of the converter. |
| 558 | :param function func: is the Lua function called to work as sample fetch. |
| 559 | |
| 560 | The prototype of the Lua function used as argument is: |
| 561 | |
| 562 | .. code-block:: lua |
| 563 | |
| 564 | string function(txn, [p1 [, p2 [, ... [, p5]]]]) |
| 565 | .. |
| 566 | |
Thierry FOURNIER | dc59500 | 2015-12-21 11:13:52 +0100 | [diff] [blame] | 567 | * **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] | 568 | request. |
| 569 | * **p1** .. **p5** (*string*): this is a list of string arguments declared in |
Bertrand Jacquin | 874a35c | 2018-09-10 21:26:07 +0100 | [diff] [blame] | 570 | the HAProxy configuration file. The number of arguments doesn't exceed 5. |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 571 | The order and the nature of these is conventionally choose by the |
Bertrand Jacquin | 874a35c | 2018-09-10 21:26:07 +0100 | [diff] [blame] | 572 | developer. |
| 573 | * **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] | 574 | returned now. |
| 575 | |
| 576 | lua example code: |
| 577 | |
| 578 | .. code-block:: lua |
| 579 | |
| 580 | core.register_fetches("hello", function(txn) |
| 581 | return "hello" |
| 582 | end) |
| 583 | .. |
| 584 | |
| 585 | HAProxy example configuration: |
| 586 | |
| 587 | :: |
| 588 | |
| 589 | frontend example |
| 590 | http-request redirect location /%[lua.hello] |
| 591 | |
Thierry FOURNIER | a3bc513 | 2015-09-25 21:43:56 +0200 | [diff] [blame] | 592 | .. js:function:: core.register_service(name, mode, func) |
| 593 | |
| 594 | **context**: body |
| 595 | |
David Carlier | 61fdf8b | 2015-10-02 11:59:38 +0100 | [diff] [blame] | 596 | 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] | 597 | be used in HAProxy with the prefix "lua.". A service gets an object class as |
| 598 | input according with the required mode. |
| 599 | |
| 600 | :param string name: is the name of the converter. |
Willy Tarreau | 61add3c | 2015-09-28 15:39:10 +0200 | [diff] [blame] | 601 | :param string mode: is string describing the required mode. Only 'tcp' or |
Thierry FOURNIER | a3bc513 | 2015-09-25 21:43:56 +0200 | [diff] [blame] | 602 | 'http' are allowed. |
| 603 | :param function func: is the Lua function called to work as converter. |
| 604 | |
| 605 | The prototype of the Lua function used as argument is: |
| 606 | |
| 607 | .. code-block:: lua |
| 608 | |
| 609 | function(applet) |
| 610 | .. |
| 611 | |
Thierry FOURNIER | dc59500 | 2015-12-21 11:13:52 +0100 | [diff] [blame] | 612 | * **applet** *applet* will be a :ref:`applettcp_class` or a |
| 613 | :ref:`applethttp_class`. It depends the type of registered applet. An applet |
| 614 | registered with the 'http' value for the *mode* parameter will gets a |
| 615 | :ref:`applethttp_class`. If the *mode* value is 'tcp', the applet will gets |
| 616 | a :ref:`applettcp_class`. |
| 617 | |
| 618 | **warning**: Applets of type 'http' cannot be called from 'tcp-*' |
| 619 | rulesets. Only the 'http-*' rulesets are authorized, this means |
| 620 | that is not possible to call an HTTP applet from a proxy in tcp |
Bertrand Jacquin | 874a35c | 2018-09-10 21:26:07 +0100 | [diff] [blame] | 621 | mode. Applets of type 'tcp' can be called from anywhere. |
Thierry FOURNIER | a3bc513 | 2015-09-25 21:43:56 +0200 | [diff] [blame] | 622 | |
Bertrand Jacquin | 874a35c | 2018-09-10 21:26:07 +0100 | [diff] [blame] | 623 | 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] | 624 | as an http response. |
| 625 | |
| 626 | .. code-block:: lua |
| 627 | |
Pieter Baauw | 4d7f766 | 2015-11-08 16:38:08 +0100 | [diff] [blame] | 628 | core.register_service("hello-world", "http", function(applet) |
Thierry FOURNIER | a3bc513 | 2015-09-25 21:43:56 +0200 | [diff] [blame] | 629 | local response = "Hello World !" |
| 630 | applet:set_status(200) |
Pieter Baauw | 2dcb9bc | 2015-10-01 22:47:12 +0200 | [diff] [blame] | 631 | applet:add_header("content-length", string.len(response)) |
Thierry FOURNIER | a3bc513 | 2015-09-25 21:43:56 +0200 | [diff] [blame] | 632 | applet:add_header("content-type", "text/plain") |
Pieter Baauw | 2dcb9bc | 2015-10-01 22:47:12 +0200 | [diff] [blame] | 633 | applet:start_response() |
Thierry FOURNIER | a3bc513 | 2015-09-25 21:43:56 +0200 | [diff] [blame] | 634 | applet:send(response) |
| 635 | end) |
| 636 | .. |
| 637 | |
| 638 | This example code is used in HAproxy configuration like this: |
| 639 | |
| 640 | :: |
| 641 | |
| 642 | frontend example |
| 643 | http-request use-service lua.hello-world |
| 644 | |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 645 | .. js:function:: core.register_init(func) |
| 646 | |
| 647 | **context**: body |
| 648 | |
| 649 | Register a function executed after the configuration parsing. This is useful |
| 650 | to check any parameters. |
| 651 | |
Pieter Baauw | 4d7f766 | 2015-11-08 16:38:08 +0100 | [diff] [blame] | 652 | :param function func: is the Lua function called to work as initializer. |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 653 | |
| 654 | The prototype of the Lua function used as argument is: |
| 655 | |
| 656 | .. code-block:: lua |
| 657 | |
| 658 | function() |
| 659 | .. |
| 660 | |
| 661 | It takes no input, and no output is expected. |
| 662 | |
| 663 | .. js:function:: core.register_task(func) |
| 664 | |
| 665 | **context**: body, init, task, action, sample-fetch, converter |
| 666 | |
| 667 | Register and start independent task. The task is started when the HAProxy |
| 668 | 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] | 669 | perform complex health checks. |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 670 | |
Pieter Baauw | 4d7f766 | 2015-11-08 16:38:08 +0100 | [diff] [blame] | 671 | :param function func: is the Lua function called to work as initializer. |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 672 | |
| 673 | The prototype of the Lua function used as argument is: |
| 674 | |
| 675 | .. code-block:: lua |
| 676 | |
| 677 | function() |
| 678 | .. |
| 679 | |
| 680 | It takes no input, and no output is expected. |
| 681 | |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 682 | .. js:function:: core.register_cli([path], usage, func) |
| 683 | |
| 684 | **context**: body |
| 685 | |
| 686 | Register and start independent task. The task is started when the HAProxy |
| 687 | main scheduler starts. For example this type of tasks can be executed to |
| 688 | perform complex health checks. |
| 689 | |
| 690 | :param array path: is the sequence of word for which the cli execute the Lua |
| 691 | binding. |
| 692 | :param string usage: is the usage message displayed in the help. |
| 693 | :param function func: is the Lua function called to handle the CLI commands. |
| 694 | |
| 695 | The prototype of the Lua function used as argument is: |
| 696 | |
| 697 | .. code-block:: lua |
| 698 | |
| 699 | function(AppletTCP, [arg1, [arg2, [...]]]) |
| 700 | .. |
| 701 | |
| 702 | 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] | 703 | 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] | 704 | this: |
| 705 | |
| 706 | .. code-block:: lua |
| 707 | |
| 708 | core.register_cli({"show", "ssl", "stats"}, "Display SSL stats..", function(applet, arg1, arg2, arg3, arg4, arg5) |
| 709 | end) |
| 710 | .. |
| 711 | |
| 712 | And we execute this in the prompt: |
| 713 | |
| 714 | .. code-block:: text |
| 715 | |
| 716 | > prompt |
| 717 | > show ssl stats all |
| 718 | .. |
| 719 | |
Bertrand Jacquin | 874a35c | 2018-09-10 21:26:07 +0100 | [diff] [blame] | 720 | 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] | 721 | arg4 will contain "all". arg5 contains nil. |
| 722 | |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 723 | .. js:function:: core.set_nice(nice) |
| 724 | |
| 725 | **context**: task, action, sample-fetch, converter |
| 726 | |
| 727 | Change the nice of the current task or current session. |
Thierry FOURNIER | 2e4893c | 2015-03-18 13:37:27 +0100 | [diff] [blame] | 728 | |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 729 | :param integer nice: the nice value, it must be between -1024 and 1024. |
| 730 | |
| 731 | .. js:function:: core.set_map(filename, key, value) |
| 732 | |
| 733 | **context**: init, task, action, sample-fetch, converter |
| 734 | |
Bertrand Jacquin | 874a35c | 2018-09-10 21:26:07 +0100 | [diff] [blame] | 735 | 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] | 736 | *filename*. |
| 737 | |
Thierry FOURNIER | dc59500 | 2015-12-21 11:13:52 +0100 | [diff] [blame] | 738 | :param string filename: the Map reference |
| 739 | :param string key: the key to set or replace |
| 740 | :param string value: the associated value |
| 741 | |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 742 | .. js:function:: core.sleep(int seconds) |
| 743 | |
| 744 | **context**: body, init, task, action |
| 745 | |
| 746 | The `core.sleep()` functions stop the Lua execution between specified seconds. |
| 747 | |
| 748 | :param integer seconds: the required seconds. |
| 749 | |
| 750 | .. js:function:: core.tcp() |
| 751 | |
| 752 | **context**: init, task, action |
| 753 | |
| 754 | This function returns a new object of a *socket* class. |
| 755 | |
Thierry FOURNIER | dc59500 | 2015-12-21 11:13:52 +0100 | [diff] [blame] | 756 | :returns: A :ref:`socket_class` object. |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 757 | |
Thierry Fournier | 1de1659 | 2016-01-27 09:49:07 +0100 | [diff] [blame] | 758 | .. js:function:: core.concat() |
| 759 | |
| 760 | **context**: body, init, task, action, sample-fetch, converter |
| 761 | |
Bertrand Jacquin | 874a35c | 2018-09-10 21:26:07 +0100 | [diff] [blame] | 762 | This function returns a new concat object. |
Thierry Fournier | 1de1659 | 2016-01-27 09:49:07 +0100 | [diff] [blame] | 763 | |
| 764 | :returns: A :ref:`concat_class` object. |
| 765 | |
Thierry FOURNIER | 0a99b89 | 2015-08-26 00:14:17 +0200 | [diff] [blame] | 766 | .. js:function:: core.done(data) |
| 767 | |
| 768 | **context**: body, init, task, action, sample-fetch, converter |
| 769 | |
| 770 | :param any data: Return some data for the caller. It is useful with |
| 771 | sample-fetches and sample-converters. |
| 772 | |
| 773 | Immediately stops the current Lua execution and returns to the caller which |
| 774 | 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] | 775 | value (ignored for actions and init). It is used when the LUA process finishes |
| 776 | 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] | 777 | remaining code. It can be seen as a multi-level "return". |
| 778 | |
Thierry FOURNIER | 486f5a0 | 2015-03-16 15:13:03 +0100 | [diff] [blame] | 779 | .. js:function:: core.yield() |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 780 | |
| 781 | **context**: task, action, sample-fetch, converter |
| 782 | |
| 783 | Give back the hand at the HAProxy scheduler. It is used when the LUA |
| 784 | processing consumes a lot of processing time. |
| 785 | |
Thierry FOURNIER / OZON.IO | 62fec75 | 2016-11-10 20:38:11 +0100 | [diff] [blame] | 786 | .. js:function:: core.parse_addr(address) |
| 787 | |
| 788 | **context**: body, init, task, action, sample-fetch, converter |
| 789 | |
| 790 | :param network: is a string describing an ipv4 or ipv6 address and optionally |
| 791 | its network length, like this: "127.0.0.1/8" or "aaaa::1234/32". |
| 792 | :returns: a userdata containing network or nil if an error occurs. |
| 793 | |
Bertrand Jacquin | 874a35c | 2018-09-10 21:26:07 +0100 | [diff] [blame] | 794 | Parse ipv4 or ipv6 addresses and its facultative associated network. |
Thierry FOURNIER / OZON.IO | 62fec75 | 2016-11-10 20:38:11 +0100 | [diff] [blame] | 795 | |
| 796 | .. js:function:: core.match_addr(addr1, addr2) |
| 797 | |
| 798 | **context**: body, init, task, action, sample-fetch, converter |
| 799 | |
| 800 | :param addr1: is an address created with "core.parse_addr". |
| 801 | :param addr2: is an address created with "core.parse_addr". |
Bertrand Jacquin | 874a35c | 2018-09-10 21:26:07 +0100 | [diff] [blame] | 802 | :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] | 803 | false. |
| 804 | |
Ilya Shipitsin | 2075ca8 | 2020-03-06 23:22:22 +0500 | [diff] [blame] | 805 | 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] | 806 | of network is not important. |
| 807 | |
Thierry FOURNIER / OZON.IO | 8a1027a | 2016-11-24 20:48:38 +0100 | [diff] [blame] | 808 | .. js:function:: core.tokenize(str, separators [, noblank]) |
| 809 | |
| 810 | **context**: body, init, task, action, sample-fetch, converter |
| 811 | |
| 812 | This function is useful for tokenizing an entry, or splitting some messages. |
| 813 | :param string str: The string which will be split. |
| 814 | :param string separators: A string containing a list of separators. |
| 815 | :param boolean noblank: Ignore empty entries. |
| 816 | :returns: an array of string. |
| 817 | |
| 818 | For example: |
| 819 | |
| 820 | .. code-block:: lua |
| 821 | |
| 822 | local array = core.tokenize("This function is useful, for tokenizing an entry.", "., ", true) |
| 823 | print_r(array) |
| 824 | .. |
| 825 | |
| 826 | Returns this array: |
| 827 | |
| 828 | .. code-block:: text |
| 829 | |
| 830 | (table) table: 0x21c01e0 [ |
| 831 | 1: (string) "This" |
| 832 | 2: (string) "function" |
| 833 | 3: (string) "is" |
| 834 | 4: (string) "useful" |
| 835 | 5: (string) "for" |
| 836 | 6: (string) "tokenizing" |
| 837 | 7: (string) "an" |
| 838 | 8: (string) "entry" |
| 839 | ] |
| 840 | .. |
| 841 | |
Thierry Fournier | f61aa63 | 2016-02-19 20:56:00 +0100 | [diff] [blame] | 842 | .. _proxy_class: |
| 843 | |
| 844 | Proxy class |
| 845 | ============ |
| 846 | |
| 847 | .. js:class:: Proxy |
| 848 | |
| 849 | This class provides a way for manipulating proxy and retrieving information |
| 850 | like statistics. |
| 851 | |
Thierry FOURNIER | 817e759 | 2017-07-24 14:35:04 +0200 | [diff] [blame] | 852 | .. js:attribute:: Proxy.name |
| 853 | |
| 854 | Contain the name of the proxy. |
| 855 | |
Baptiste Assmann | 46c7255 | 2017-10-26 21:51:58 +0200 | [diff] [blame] | 856 | .. js:attribute:: Proxy.uuid |
| 857 | |
| 858 | Contain the unique identifier of the proxy. |
| 859 | |
Thierry Fournier | f2fdc9d | 2016-02-22 08:21:39 +0100 | [diff] [blame] | 860 | .. js:attribute:: Proxy.servers |
| 861 | |
Patrick Hemmer | c6a1d71 | 2018-05-01 21:30:41 -0400 | [diff] [blame] | 862 | Contain a table with the attached servers. The table is indexed by server |
| 863 | 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] | 864 | |
Adis Nezirovic | 8878f8e | 2018-07-13 12:18:33 +0200 | [diff] [blame] | 865 | .. js:attribute:: Proxy.stktable |
| 866 | |
| 867 | Contains a stick table object attached to the proxy. |
| 868 | |
Thierry Fournier | ff48042 | 2016-02-25 08:36:46 +0100 | [diff] [blame] | 869 | .. js:attribute:: Proxy.listeners |
| 870 | |
Patrick Hemmer | c6a1d71 | 2018-05-01 21:30:41 -0400 | [diff] [blame] | 871 | Contain a table with the attached listeners. The table is indexed by listener |
| 872 | name, and each each listeners entry is an object of type |
| 873 | :ref:`listener_class`. |
Thierry Fournier | ff48042 | 2016-02-25 08:36:46 +0100 | [diff] [blame] | 874 | |
Thierry Fournier | f61aa63 | 2016-02-19 20:56:00 +0100 | [diff] [blame] | 875 | .. js:function:: Proxy.pause(px) |
| 876 | |
| 877 | Pause the proxy. See the management socket documentation for more information. |
| 878 | |
| 879 | :param class_proxy px: A :ref:`proxy_class` which indicates the manipulated |
| 880 | proxy. |
| 881 | |
| 882 | .. js:function:: Proxy.resume(px) |
| 883 | |
| 884 | Resume the proxy. See the management socket documentation for more |
| 885 | information. |
| 886 | |
| 887 | :param class_proxy px: A :ref:`proxy_class` which indicates the manipulated |
| 888 | proxy. |
| 889 | |
| 890 | .. js:function:: Proxy.stop(px) |
| 891 | |
| 892 | Stop the proxy. See the management socket documentation for more information. |
| 893 | |
| 894 | :param class_proxy px: A :ref:`proxy_class` which indicates the manipulated |
| 895 | proxy. |
| 896 | |
| 897 | .. js:function:: Proxy.shut_bcksess(px) |
| 898 | |
| 899 | Kill the session attached to a backup server. See the management socket |
| 900 | documentation for more information. |
| 901 | |
| 902 | :param class_proxy px: A :ref:`proxy_class` which indicates the manipulated |
| 903 | proxy. |
| 904 | |
| 905 | .. js:function:: Proxy.get_cap(px) |
| 906 | |
| 907 | Returns a string describing the capabilities of the proxy. |
| 908 | |
| 909 | :param class_proxy px: A :ref:`proxy_class` which indicates the manipulated |
| 910 | proxy. |
| 911 | :returns: a string "frontend", "backend", "proxy" or "ruleset". |
| 912 | |
| 913 | .. js:function:: Proxy.get_mode(px) |
| 914 | |
| 915 | Returns a string describing the mode of the current proxy. |
| 916 | |
| 917 | :param class_proxy px: A :ref:`proxy_class` which indicates the manipulated |
| 918 | proxy. |
| 919 | :returns: a string "tcp", "http", "health" or "unknown" |
| 920 | |
| 921 | .. js:function:: Proxy.get_stats(px) |
| 922 | |
Bertrand Jacquin | 874a35c | 2018-09-10 21:26:07 +0100 | [diff] [blame] | 923 | Returns a table containing the proxy statistics. The statistics returned are |
Thierry Fournier | f61aa63 | 2016-02-19 20:56:00 +0100 | [diff] [blame] | 924 | not the same if the proxy is frontend or a backend. |
| 925 | |
| 926 | :param class_proxy px: A :ref:`proxy_class` which indicates the manipulated |
| 927 | proxy. |
Patrick Hemmer | c6a1d71 | 2018-05-01 21:30:41 -0400 | [diff] [blame] | 928 | :returns: a key/value table containing stats |
Thierry Fournier | f61aa63 | 2016-02-19 20:56:00 +0100 | [diff] [blame] | 929 | |
Thierry Fournier | f2fdc9d | 2016-02-22 08:21:39 +0100 | [diff] [blame] | 930 | .. _server_class: |
| 931 | |
| 932 | Server class |
| 933 | ============ |
| 934 | |
Patrick Hemmer | c6a1d71 | 2018-05-01 21:30:41 -0400 | [diff] [blame] | 935 | .. js:class:: Server |
| 936 | |
| 937 | This class provides a way for manipulating servers and retrieving information. |
| 938 | |
Patrick Hemmer | a62ae7e | 2018-04-29 14:23:48 -0400 | [diff] [blame] | 939 | .. js:attribute:: Server.name |
| 940 | |
| 941 | Contain the name of the server. |
| 942 | |
| 943 | .. js:attribute:: Server.puid |
| 944 | |
| 945 | Contain the proxy unique identifier of the server. |
| 946 | |
Thierry Fournier | f2fdc9d | 2016-02-22 08:21:39 +0100 | [diff] [blame] | 947 | .. js:function:: Server.is_draining(sv) |
| 948 | |
Patrick Hemmer | c6a1d71 | 2018-05-01 21:30:41 -0400 | [diff] [blame] | 949 | Return true if the server is currently draining sticky connections. |
Thierry Fournier | f2fdc9d | 2016-02-22 08:21:39 +0100 | [diff] [blame] | 950 | |
| 951 | :param class_server sv: A :ref:`server_class` which indicates the manipulated |
| 952 | server. |
| 953 | :returns: a boolean |
| 954 | |
Patrick Hemmer | 32d539f | 2018-04-29 14:25:46 -0400 | [diff] [blame] | 955 | .. js:function:: Server.set_maxconn(sv, weight) |
| 956 | |
| 957 | Dynamically change the maximum connections of the server. See the management |
| 958 | socket documentation for more information about the format of the string. |
| 959 | |
| 960 | :param class_server sv: A :ref:`server_class` which indicates the manipulated |
| 961 | server. |
| 962 | :param string maxconn: A string describing the server maximum connections. |
| 963 | |
| 964 | .. js:function:: Server.get_maxconn(sv, weight) |
| 965 | |
| 966 | This function returns an integer representing the server maximum connections. |
| 967 | |
| 968 | :param class_server sv: A :ref:`server_class` which indicates the manipulated |
| 969 | server. |
| 970 | :returns: an integer. |
| 971 | |
Thierry Fournier | f2fdc9d | 2016-02-22 08:21:39 +0100 | [diff] [blame] | 972 | .. js:function:: Server.set_weight(sv, weight) |
| 973 | |
Patrick Hemmer | c6a1d71 | 2018-05-01 21:30:41 -0400 | [diff] [blame] | 974 | Dynamically change the weight of the server. See the management socket |
Thierry Fournier | f2fdc9d | 2016-02-22 08:21:39 +0100 | [diff] [blame] | 975 | documentation for more information about the format of the string. |
| 976 | |
| 977 | :param class_server sv: A :ref:`server_class` which indicates the manipulated |
| 978 | server. |
| 979 | :param string weight: A string describing the server weight. |
| 980 | |
| 981 | .. js:function:: Server.get_weight(sv) |
| 982 | |
Patrick Hemmer | c6a1d71 | 2018-05-01 21:30:41 -0400 | [diff] [blame] | 983 | This function returns an integer representing the server weight. |
Thierry Fournier | f2fdc9d | 2016-02-22 08:21:39 +0100 | [diff] [blame] | 984 | |
| 985 | :param class_server sv: A :ref:`server_class` which indicates the manipulated |
| 986 | server. |
| 987 | :returns: an integer. |
| 988 | |
Joseph C. Sible | 49bbf52 | 2020-05-04 22:20:32 -0400 | [diff] [blame] | 989 | .. js:function:: Server.set_addr(sv, addr[, port]) |
Thierry Fournier | f2fdc9d | 2016-02-22 08:21:39 +0100 | [diff] [blame] | 990 | |
Patrick Hemmer | c6a1d71 | 2018-05-01 21:30:41 -0400 | [diff] [blame] | 991 | Dynamically change the address of the server. See the management socket |
Thierry Fournier | f2fdc9d | 2016-02-22 08:21:39 +0100 | [diff] [blame] | 992 | documentation for more information about the format of the string. |
| 993 | |
| 994 | :param class_server sv: A :ref:`server_class` which indicates the manipulated |
| 995 | server. |
Patrick Hemmer | c6a1d71 | 2018-05-01 21:30:41 -0400 | [diff] [blame] | 996 | :param string addr: A string describing the server address. |
Thierry Fournier | f2fdc9d | 2016-02-22 08:21:39 +0100 | [diff] [blame] | 997 | |
| 998 | .. js:function:: Server.get_addr(sv) |
| 999 | |
Patrick Hemmer | c6a1d71 | 2018-05-01 21:30:41 -0400 | [diff] [blame] | 1000 | Returns a string describing the address of the server. |
Thierry Fournier | f2fdc9d | 2016-02-22 08:21:39 +0100 | [diff] [blame] | 1001 | |
| 1002 | :param class_server sv: A :ref:`server_class` which indicates the manipulated |
| 1003 | server. |
| 1004 | :returns: A string |
| 1005 | |
| 1006 | .. js:function:: Server.get_stats(sv) |
| 1007 | |
| 1008 | Returns server statistics. |
| 1009 | |
| 1010 | :param class_server sv: A :ref:`server_class` which indicates the manipulated |
| 1011 | server. |
Patrick Hemmer | c6a1d71 | 2018-05-01 21:30:41 -0400 | [diff] [blame] | 1012 | :returns: a key/value table containing stats |
Thierry Fournier | f2fdc9d | 2016-02-22 08:21:39 +0100 | [diff] [blame] | 1013 | |
| 1014 | .. js:function:: Server.shut_sess(sv) |
| 1015 | |
| 1016 | Shutdown all the sessions attached to the server. See the management socket |
| 1017 | documentation for more information about this function. |
| 1018 | |
| 1019 | :param class_server sv: A :ref:`server_class` which indicates the manipulated |
| 1020 | server. |
| 1021 | |
| 1022 | .. js:function:: Server.set_drain(sv) |
| 1023 | |
| 1024 | Drain sticky sessions. See the management socket documentation for more |
| 1025 | information about this function. |
| 1026 | |
| 1027 | :param class_server sv: A :ref:`server_class` which indicates the manipulated |
| 1028 | server. |
| 1029 | |
| 1030 | .. js:function:: Server.set_maint(sv) |
| 1031 | |
| 1032 | Set maintenance mode. See the management socket documentation for more |
| 1033 | information about this function. |
| 1034 | |
| 1035 | :param class_server sv: A :ref:`server_class` which indicates the manipulated |
| 1036 | server. |
| 1037 | |
| 1038 | .. js:function:: Server.set_ready(sv) |
| 1039 | |
| 1040 | Set normal mode. See the management socket documentation for more information |
| 1041 | about this function. |
| 1042 | |
| 1043 | :param class_server sv: A :ref:`server_class` which indicates the manipulated |
| 1044 | server. |
| 1045 | |
| 1046 | .. js:function:: Server.check_enable(sv) |
| 1047 | |
| 1048 | Enable health checks. See the management socket documentation for more |
| 1049 | information about this function. |
| 1050 | |
| 1051 | :param class_server sv: A :ref:`server_class` which indicates the manipulated |
| 1052 | server. |
| 1053 | |
| 1054 | .. js:function:: Server.check_disable(sv) |
| 1055 | |
| 1056 | Disable health checks. See the management socket documentation for more |
| 1057 | information about this function. |
| 1058 | |
| 1059 | :param class_server sv: A :ref:`server_class` which indicates the manipulated |
| 1060 | server. |
| 1061 | |
| 1062 | .. js:function:: Server.check_force_up(sv) |
| 1063 | |
| 1064 | Force health-check up. See the management socket documentation for more |
| 1065 | information about this function. |
| 1066 | |
| 1067 | :param class_server sv: A :ref:`server_class` which indicates the manipulated |
| 1068 | server. |
| 1069 | |
| 1070 | .. js:function:: Server.check_force_nolb(sv) |
| 1071 | |
| 1072 | Force health-check nolb mode. See the management socket documentation for more |
| 1073 | information about this function. |
| 1074 | |
| 1075 | :param class_server sv: A :ref:`server_class` which indicates the manipulated |
| 1076 | server. |
| 1077 | |
| 1078 | .. js:function:: Server.check_force_down(sv) |
| 1079 | |
| 1080 | Force health-check down. See the management socket documentation for more |
| 1081 | information about this function. |
| 1082 | |
| 1083 | :param class_server sv: A :ref:`server_class` which indicates the manipulated |
| 1084 | server. |
| 1085 | |
| 1086 | .. js:function:: Server.agent_enable(sv) |
| 1087 | |
| 1088 | Enable agent check. See the management socket documentation for more |
| 1089 | 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.agent_disable(sv) |
| 1095 | |
| 1096 | Disable agent check. 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.agent_force_up(sv) |
| 1103 | |
| 1104 | Force agent check up. 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.agent_force_down(sv) |
| 1111 | |
| 1112 | Force agent check down. See the management socket documentation for more |
| 1113 | information about this function. |
| 1114 | |
| 1115 | :param class_server sv: A :ref:`server_class` which indicates the manipulated |
| 1116 | server. |
| 1117 | |
Thierry Fournier | ff48042 | 2016-02-25 08:36:46 +0100 | [diff] [blame] | 1118 | .. _listener_class: |
| 1119 | |
| 1120 | Listener class |
| 1121 | ============== |
| 1122 | |
| 1123 | .. js:function:: Listener.get_stats(ls) |
| 1124 | |
| 1125 | Returns server statistics. |
| 1126 | |
| 1127 | :param class_listener ls: A :ref:`listener_class` which indicates the |
| 1128 | manipulated listener. |
Patrick Hemmer | c6a1d71 | 2018-05-01 21:30:41 -0400 | [diff] [blame] | 1129 | :returns: a key/value table containing stats |
Thierry Fournier | ff48042 | 2016-02-25 08:36:46 +0100 | [diff] [blame] | 1130 | |
Thierry Fournier | 1de1659 | 2016-01-27 09:49:07 +0100 | [diff] [blame] | 1131 | .. _concat_class: |
| 1132 | |
| 1133 | Concat class |
| 1134 | ============ |
| 1135 | |
| 1136 | .. js:class:: Concat |
| 1137 | |
| 1138 | This class provides a fast way for string concatenation. The way using native |
| 1139 | Lua concatenation like the code below is slow for some reasons. |
| 1140 | |
| 1141 | .. code-block:: lua |
| 1142 | |
| 1143 | str = "string1" |
| 1144 | str = str .. ", string2" |
| 1145 | str = str .. ", string3" |
| 1146 | .. |
| 1147 | |
| 1148 | For each concatenation, Lua: |
| 1149 | * allocate memory for the result, |
Ilya Shipitsin | 2075ca8 | 2020-03-06 23:22:22 +0500 | [diff] [blame] | 1150 | * catenate the two string copying the strings in the new memory block, |
Bertrand Jacquin | 874a35c | 2018-09-10 21:26:07 +0100 | [diff] [blame] | 1151 | * 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] | 1152 | This process does many memory move, allocation and free. In addition, the |
Bertrand Jacquin | 874a35c | 2018-09-10 21:26:07 +0100 | [diff] [blame] | 1153 | 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] | 1154 | garbage collector. |
| 1155 | |
Bertrand Jacquin | 874a35c | 2018-09-10 21:26:07 +0100 | [diff] [blame] | 1156 | The Concat class provide an alternative way to concatenate strings. It uses |
Thierry Fournier | 1de1659 | 2016-01-27 09:49:07 +0100 | [diff] [blame] | 1157 | the internal Lua mechanism (it does not allocate memory), but it doesn't copy |
| 1158 | the data more than once. |
| 1159 | |
| 1160 | On my computer, the following loops spends 0.2s for the Concat method and |
| 1161 | 18.5s for the pure Lua implementation. So, the Concat class is about 1000x |
| 1162 | faster than the embedded solution. |
| 1163 | |
| 1164 | .. code-block:: lua |
| 1165 | |
| 1166 | for j = 1, 100 do |
| 1167 | c = core.concat() |
| 1168 | for i = 1, 20000 do |
| 1169 | c:add("#####") |
| 1170 | end |
| 1171 | end |
| 1172 | .. |
| 1173 | |
| 1174 | .. code-block:: lua |
| 1175 | |
| 1176 | for j = 1, 100 do |
| 1177 | c = "" |
| 1178 | for i = 1, 20000 do |
| 1179 | c = c .. "#####" |
| 1180 | end |
| 1181 | end |
| 1182 | .. |
| 1183 | |
| 1184 | .. js:function:: Concat.add(concat, string) |
| 1185 | |
| 1186 | This function adds a string to the current concatenated string. |
| 1187 | |
| 1188 | :param class_concat concat: A :ref:`concat_class` which contains the currently |
Ilya Shipitsin | 2075ca8 | 2020-03-06 23:22:22 +0500 | [diff] [blame] | 1189 | built string. |
Bertrand Jacquin | 874a35c | 2018-09-10 21:26:07 +0100 | [diff] [blame] | 1190 | :param string string: A new string to concatenate to the current built |
Thierry Fournier | 1de1659 | 2016-01-27 09:49:07 +0100 | [diff] [blame] | 1191 | string. |
| 1192 | |
| 1193 | .. js:function:: Concat.dump(concat) |
| 1194 | |
Bertrand Jacquin | 874a35c | 2018-09-10 21:26:07 +0100 | [diff] [blame] | 1195 | This function returns the concatenated string. |
Thierry Fournier | 1de1659 | 2016-01-27 09:49:07 +0100 | [diff] [blame] | 1196 | |
| 1197 | :param class_concat concat: A :ref:`concat_class` which contains the currently |
Ilya Shipitsin | 2075ca8 | 2020-03-06 23:22:22 +0500 | [diff] [blame] | 1198 | built string. |
Thierry Fournier | 1de1659 | 2016-01-27 09:49:07 +0100 | [diff] [blame] | 1199 | :returns: the concatenated string |
| 1200 | |
Thierry FOURNIER | dc59500 | 2015-12-21 11:13:52 +0100 | [diff] [blame] | 1201 | .. _fetches_class: |
| 1202 | |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 1203 | Fetches class |
| 1204 | ============= |
| 1205 | |
| 1206 | .. js:class:: Fetches |
| 1207 | |
| 1208 | This class contains a lot of internal HAProxy sample fetches. See the |
Thierry FOURNIER | 2e4893c | 2015-03-18 13:37:27 +0100 | [diff] [blame] | 1209 | HAProxy "configuration.txt" documentation for more information about her |
Bertrand Jacquin | 874a35c | 2018-09-10 21:26:07 +0100 | [diff] [blame] | 1210 | usage. They are the chapters 7.3.2 to 7.3.6. |
Thierry FOURNIER | 2e4893c | 2015-03-18 13:37:27 +0100 | [diff] [blame] | 1211 | |
Thierry FOURNIER | dc59500 | 2015-12-21 11:13:52 +0100 | [diff] [blame] | 1212 | **warning** some sample fetches are not available in some context. These |
Patrick Hemmer | c6a1d71 | 2018-05-01 21:30:41 -0400 | [diff] [blame] | 1213 | limitations are specified in this documentation when they're useful. |
Thierry FOURNIER | dc59500 | 2015-12-21 11:13:52 +0100 | [diff] [blame] | 1214 | |
Thierry FOURNIER | 12a865d | 2016-12-14 19:40:37 +0100 | [diff] [blame] | 1215 | :see: :js:attr:`TXN.f` |
| 1216 | :see: :js:attr:`TXN.sf` |
Thierry FOURNIER | 2e4893c | 2015-03-18 13:37:27 +0100 | [diff] [blame] | 1217 | |
| 1218 | Fetches are useful for: |
| 1219 | |
| 1220 | * get system time, |
| 1221 | * get environment variable, |
| 1222 | * get random numbers, |
| 1223 | * known backend status like the number of users in queue or the number of |
| 1224 | connections established, |
| 1225 | * client information like ip source or destination, |
| 1226 | * deal with stick tables, |
Ilya Shipitsin | 2075ca8 | 2020-03-06 23:22:22 +0500 | [diff] [blame] | 1227 | * Established SSL information, |
Thierry FOURNIER | 2e4893c | 2015-03-18 13:37:27 +0100 | [diff] [blame] | 1228 | * HTTP information like headers or method. |
| 1229 | |
| 1230 | .. code-block:: lua |
| 1231 | |
Thierry FOURNIER | dc59500 | 2015-12-21 11:13:52 +0100 | [diff] [blame] | 1232 | function action(txn) |
| 1233 | -- Get source IP |
| 1234 | local clientip = txn.f:src() |
| 1235 | end |
Thierry FOURNIER | 2e4893c | 2015-03-18 13:37:27 +0100 | [diff] [blame] | 1236 | .. |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 1237 | |
Thierry FOURNIER | dc59500 | 2015-12-21 11:13:52 +0100 | [diff] [blame] | 1238 | .. _converters_class: |
| 1239 | |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 1240 | Converters class |
| 1241 | ================ |
| 1242 | |
| 1243 | .. js:class:: Converters |
| 1244 | |
| 1245 | This class contains a lot of internal HAProxy sample converters. See the |
Thierry FOURNIER | 2e4893c | 2015-03-18 13:37:27 +0100 | [diff] [blame] | 1246 | HAProxy documentation "configuration.txt" for more information about her |
| 1247 | usage. Its the chapter 7.3.1. |
| 1248 | |
Thierry FOURNIER | 12a865d | 2016-12-14 19:40:37 +0100 | [diff] [blame] | 1249 | :see: :js:attr:`TXN.c` |
| 1250 | :see: :js:attr:`TXN.sc` |
Thierry FOURNIER | 2e4893c | 2015-03-18 13:37:27 +0100 | [diff] [blame] | 1251 | |
| 1252 | Converters provides statefull transformation. They are useful for: |
| 1253 | |
| 1254 | * converting input to base64, |
| 1255 | * applying hash on input string (djb2, crc32, sdbm, wt6), |
| 1256 | * format date, |
| 1257 | * json escape, |
Pieter Baauw | 4d7f766 | 2015-11-08 16:38:08 +0100 | [diff] [blame] | 1258 | * extracting preferred language comparing two lists, |
Thierry FOURNIER | 2e4893c | 2015-03-18 13:37:27 +0100 | [diff] [blame] | 1259 | * turn to lower or upper chars, |
| 1260 | * deal with stick tables. |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 1261 | |
Thierry FOURNIER | dc59500 | 2015-12-21 11:13:52 +0100 | [diff] [blame] | 1262 | .. _channel_class: |
| 1263 | |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 1264 | Channel class |
| 1265 | ============= |
| 1266 | |
| 1267 | .. js:class:: Channel |
| 1268 | |
| 1269 | HAProxy uses two buffers for the processing of the requests. The first one is |
| 1270 | used with the request data (from the client to the server) and the second is |
| 1271 | used for the response data (from the server to the client). |
| 1272 | |
| 1273 | Each buffer contains two types of data. The first type is the incoming data |
| 1274 | waiting for a processing. The second part is the outgoing data already |
| 1275 | processed. Usually, the incoming data is processed, after it is tagged as |
| 1276 | outgoing data, and finally it is sent. The following functions provides tools |
| 1277 | for manipulating these data in a buffer. |
| 1278 | |
| 1279 | The following diagram shows where the channel class function are applied. |
| 1280 | |
| 1281 | **Warning**: It is not possible to read from the response in request action, |
| 1282 | and it is not possible to read for the request channel in response action. |
| 1283 | |
Thierry FOURNIER | 2e4893c | 2015-03-18 13:37:27 +0100 | [diff] [blame] | 1284 | .. image:: _static/channel.png |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 1285 | |
Thierry FOURNIER | 486f5a0 | 2015-03-16 15:13:03 +0100 | [diff] [blame] | 1286 | .. js:function:: Channel.dup(channel) |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 1287 | |
| 1288 | This function returns a string that contain the entire buffer. The data is |
| 1289 | not remove from the buffer and can be reprocessed later. |
| 1290 | |
Ilya Shipitsin | 2075ca8 | 2020-03-06 23:22:22 +0500 | [diff] [blame] | 1291 | If the buffer can't receive more data, a 'nil' value is returned. |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 1292 | |
Thierry FOURNIER | 486f5a0 | 2015-03-16 15:13:03 +0100 | [diff] [blame] | 1293 | :param class_channel channel: The manipulated Channel. |
Pieter Baauw | 4d7f766 | 2015-11-08 16:38:08 +0100 | [diff] [blame] | 1294 | :returns: a string containing all the available data or nil. |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 1295 | |
Thierry FOURNIER | 486f5a0 | 2015-03-16 15:13:03 +0100 | [diff] [blame] | 1296 | .. js:function:: Channel.get(channel) |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 1297 | |
| 1298 | This function returns a string that contain the entire buffer. The data is |
| 1299 | consumed from the buffer. |
| 1300 | |
Ilya Shipitsin | 2075ca8 | 2020-03-06 23:22:22 +0500 | [diff] [blame] | 1301 | If the buffer can't receive more data, a 'nil' value is returned. |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 1302 | |
Thierry FOURNIER | 486f5a0 | 2015-03-16 15:13:03 +0100 | [diff] [blame] | 1303 | :param class_channel channel: The manipulated Channel. |
Pieter Baauw | 4d7f766 | 2015-11-08 16:38:08 +0100 | [diff] [blame] | 1304 | :returns: a string containing all the available data or nil. |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 1305 | |
Thierry FOURNIER | a3bc513 | 2015-09-25 21:43:56 +0200 | [diff] [blame] | 1306 | .. js:function:: Channel.getline(channel) |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 1307 | |
| 1308 | This function returns a string that contain the first line of the buffer. The |
| 1309 | data is consumed. If the data returned doesn't contains a final '\n' its |
| 1310 | assumed than its the last available data in the buffer. |
| 1311 | |
Ilya Shipitsin | 2075ca8 | 2020-03-06 23:22:22 +0500 | [diff] [blame] | 1312 | If the buffer can't receive more data, a 'nil' value is returned. |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 1313 | |
Thierry FOURNIER | 486f5a0 | 2015-03-16 15:13:03 +0100 | [diff] [blame] | 1314 | :param class_channel channel: The manipulated Channel. |
Pieter Baauw | 386a127 | 2015-08-16 15:26:24 +0200 | [diff] [blame] | 1315 | :returns: a string containing the available line or nil. |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 1316 | |
Thierry FOURNIER | 486f5a0 | 2015-03-16 15:13:03 +0100 | [diff] [blame] | 1317 | .. js:function:: Channel.set(channel, string) |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 1318 | |
| 1319 | This function replace the content of the buffer by the string. The function |
| 1320 | returns the copied length, otherwise, it returns -1. |
| 1321 | |
| 1322 | The data set with this function are not send. They wait for the end of |
| 1323 | HAProxy processing, so the buffer can be full. |
| 1324 | |
Thierry FOURNIER | 486f5a0 | 2015-03-16 15:13:03 +0100 | [diff] [blame] | 1325 | :param class_channel channel: The manipulated Channel. |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 1326 | :param string string: The data which will sent. |
Pieter Baauw | 4d7f766 | 2015-11-08 16:38:08 +0100 | [diff] [blame] | 1327 | :returns: an integer containing the amount of bytes copied or -1. |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 1328 | |
Thierry FOURNIER | 486f5a0 | 2015-03-16 15:13:03 +0100 | [diff] [blame] | 1329 | .. js:function:: Channel.append(channel, string) |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 1330 | |
| 1331 | This function append the string argument to the content of the buffer. The |
| 1332 | function returns the copied length, otherwise, it returns -1. |
| 1333 | |
| 1334 | The data set with this function are not send. They wait for the end of |
| 1335 | HAProxy processing, so the buffer can be full. |
| 1336 | |
Thierry FOURNIER | 486f5a0 | 2015-03-16 15:13:03 +0100 | [diff] [blame] | 1337 | :param class_channel channel: The manipulated Channel. |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 1338 | :param string string: The data which will sent. |
Pieter Baauw | 4d7f766 | 2015-11-08 16:38:08 +0100 | [diff] [blame] | 1339 | :returns: an integer containing the amount of bytes copied or -1. |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 1340 | |
Thierry FOURNIER | 486f5a0 | 2015-03-16 15:13:03 +0100 | [diff] [blame] | 1341 | .. js:function:: Channel.send(channel, string) |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 1342 | |
| 1343 | This function required immediate send of the data. Unless if the connection |
| 1344 | is close, the buffer is regularly flushed and all the string can be sent. |
| 1345 | |
Thierry FOURNIER | 486f5a0 | 2015-03-16 15:13:03 +0100 | [diff] [blame] | 1346 | :param class_channel channel: The manipulated Channel. |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 1347 | :param string string: The data which will sent. |
Pieter Baauw | 4d7f766 | 2015-11-08 16:38:08 +0100 | [diff] [blame] | 1348 | :returns: an integer containing the amount of bytes copied or -1. |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 1349 | |
Thierry FOURNIER | 486f5a0 | 2015-03-16 15:13:03 +0100 | [diff] [blame] | 1350 | .. js:function:: Channel.get_in_length(channel) |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 1351 | |
| 1352 | This function returns the length of the input part of the buffer. |
| 1353 | |
Thierry FOURNIER | 486f5a0 | 2015-03-16 15:13:03 +0100 | [diff] [blame] | 1354 | :param class_channel channel: The manipulated Channel. |
Pieter Baauw | 4d7f766 | 2015-11-08 16:38:08 +0100 | [diff] [blame] | 1355 | :returns: an integer containing the amount of available bytes. |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 1356 | |
Thierry FOURNIER | 486f5a0 | 2015-03-16 15:13:03 +0100 | [diff] [blame] | 1357 | .. js:function:: Channel.get_out_length(channel) |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 1358 | |
| 1359 | This function returns the length of the output part of the buffer. |
| 1360 | |
Thierry FOURNIER | 486f5a0 | 2015-03-16 15:13:03 +0100 | [diff] [blame] | 1361 | :param class_channel channel: The manipulated Channel. |
Pieter Baauw | 4d7f766 | 2015-11-08 16:38:08 +0100 | [diff] [blame] | 1362 | :returns: an integer containing the amount of available bytes. |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 1363 | |
Thierry FOURNIER | 486f5a0 | 2015-03-16 15:13:03 +0100 | [diff] [blame] | 1364 | .. js:function:: Channel.forward(channel, int) |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 1365 | |
| 1366 | This function transfer bytes from the input part of the buffer to the output |
| 1367 | part. |
| 1368 | |
Thierry FOURNIER | 486f5a0 | 2015-03-16 15:13:03 +0100 | [diff] [blame] | 1369 | :param class_channel channel: The manipulated Channel. |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 1370 | :param integer int: The amount of data which will be forwarded. |
| 1371 | |
Thierry FOURNIER / OZON.IO | 65192f3 | 2016-11-07 15:28:40 +0100 | [diff] [blame] | 1372 | .. js:function:: Channel.is_full(channel) |
| 1373 | |
| 1374 | This function returns true if the buffer channel is full. |
| 1375 | |
| 1376 | :returns: a boolean |
Thierry FOURNIER | dc59500 | 2015-12-21 11:13:52 +0100 | [diff] [blame] | 1377 | |
| 1378 | .. _http_class: |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 1379 | |
| 1380 | HTTP class |
| 1381 | ========== |
| 1382 | |
| 1383 | .. js:class:: HTTP |
| 1384 | |
| 1385 | This class contain all the HTTP manipulation functions. |
| 1386 | |
Pieter Baauw | 386a127 | 2015-08-16 15:26:24 +0200 | [diff] [blame] | 1387 | .. js:function:: HTTP.req_get_headers(http) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 1388 | |
Patrick Hemmer | c6a1d71 | 2018-05-01 21:30:41 -0400 | [diff] [blame] | 1389 | Returns a table containing all the request headers. |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 1390 | |
| 1391 | :param class_http http: The related http object. |
Patrick Hemmer | c6a1d71 | 2018-05-01 21:30:41 -0400 | [diff] [blame] | 1392 | :returns: table of headers. |
Thierry FOURNIER | 12a865d | 2016-12-14 19:40:37 +0100 | [diff] [blame] | 1393 | :see: :js:func:`HTTP.res_get_headers` |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 1394 | |
Patrick Hemmer | c6a1d71 | 2018-05-01 21:30:41 -0400 | [diff] [blame] | 1395 | This is the form of the returned table: |
Thierry FOURNIER | dc59500 | 2015-12-21 11:13:52 +0100 | [diff] [blame] | 1396 | |
| 1397 | .. code-block:: lua |
| 1398 | |
| 1399 | HTTP:req_get_headers()['<header-name>'][<header-index>] = "<header-value>" |
| 1400 | |
| 1401 | local hdr = HTTP:req_get_headers() |
| 1402 | hdr["host"][0] = "www.test.com" |
| 1403 | hdr["accept"][0] = "audio/basic q=1" |
| 1404 | hdr["accept"][1] = "audio/*, q=0.2" |
| 1405 | hdr["accept"][2] = "*/*, q=0.1" |
| 1406 | .. |
| 1407 | |
Pieter Baauw | 386a127 | 2015-08-16 15:26:24 +0200 | [diff] [blame] | 1408 | .. js:function:: HTTP.res_get_headers(http) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 1409 | |
Patrick Hemmer | c6a1d71 | 2018-05-01 21:30:41 -0400 | [diff] [blame] | 1410 | Returns a table containing all the response headers. |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 1411 | |
| 1412 | :param class_http http: The related http object. |
Patrick Hemmer | c6a1d71 | 2018-05-01 21:30:41 -0400 | [diff] [blame] | 1413 | :returns: table of headers. |
Thierry FOURNIER | 12a865d | 2016-12-14 19:40:37 +0100 | [diff] [blame] | 1414 | :see: :js:func:`HTTP.req_get_headers` |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 1415 | |
Patrick Hemmer | c6a1d71 | 2018-05-01 21:30:41 -0400 | [diff] [blame] | 1416 | This is the form of the returned table: |
Thierry FOURNIER | dc59500 | 2015-12-21 11:13:52 +0100 | [diff] [blame] | 1417 | |
| 1418 | .. code-block:: lua |
| 1419 | |
| 1420 | HTTP:res_get_headers()['<header-name>'][<header-index>] = "<header-value>" |
| 1421 | |
| 1422 | local hdr = HTTP:req_get_headers() |
| 1423 | hdr["host"][0] = "www.test.com" |
| 1424 | hdr["accept"][0] = "audio/basic q=1" |
| 1425 | hdr["accept"][1] = "audio/*, q=0.2" |
| 1426 | hdr["accept"][2] = "*.*, q=0.1" |
| 1427 | .. |
| 1428 | |
Thierry FOURNIER | 486f5a0 | 2015-03-16 15:13:03 +0100 | [diff] [blame] | 1429 | .. js:function:: HTTP.req_add_header(http, name, value) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 1430 | |
| 1431 | Appends an HTTP header field in the request whose name is |
| 1432 | specified in "name" and whose value is defined in "value". |
| 1433 | |
| 1434 | :param class_http http: The related http object. |
| 1435 | :param string name: The header name. |
| 1436 | :param string value: The header value. |
Thierry FOURNIER | 12a865d | 2016-12-14 19:40:37 +0100 | [diff] [blame] | 1437 | :see: :js:func:`HTTP.res_add_header` |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 1438 | |
Thierry FOURNIER | 486f5a0 | 2015-03-16 15:13:03 +0100 | [diff] [blame] | 1439 | .. js:function:: HTTP.res_add_header(http, name, value) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 1440 | |
Bertrand Jacquin | 874a35c | 2018-09-10 21:26:07 +0100 | [diff] [blame] | 1441 | Appends an HTTP header field in the response whose name is |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 1442 | specified in "name" and whose value is defined in "value". |
| 1443 | |
| 1444 | :param class_http http: The related http object. |
| 1445 | :param string name: The header name. |
| 1446 | :param string value: The header value. |
Thierry FOURNIER | 12a865d | 2016-12-14 19:40:37 +0100 | [diff] [blame] | 1447 | :see: :js:func:`HTTP.req_add_header` |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 1448 | |
Thierry FOURNIER | 486f5a0 | 2015-03-16 15:13:03 +0100 | [diff] [blame] | 1449 | .. js:function:: HTTP.req_del_header(http, name) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 1450 | |
| 1451 | Removes all HTTP header fields in the request whose name is |
| 1452 | specified in "name". |
| 1453 | |
| 1454 | :param class_http http: The related http object. |
| 1455 | :param string name: The header name. |
Thierry FOURNIER | 12a865d | 2016-12-14 19:40:37 +0100 | [diff] [blame] | 1456 | :see: :js:func:`HTTP.res_del_header` |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 1457 | |
Thierry FOURNIER | 486f5a0 | 2015-03-16 15:13:03 +0100 | [diff] [blame] | 1458 | .. js:function:: HTTP.res_del_header(http, name) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 1459 | |
| 1460 | Removes all HTTP header fields in the response whose name is |
| 1461 | specified in "name". |
| 1462 | |
| 1463 | :param class_http http: The related http object. |
| 1464 | :param string name: The header name. |
Thierry FOURNIER | 12a865d | 2016-12-14 19:40:37 +0100 | [diff] [blame] | 1465 | :see: :js:func:`HTTP.req_del_header` |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 1466 | |
Thierry FOURNIER | 486f5a0 | 2015-03-16 15:13:03 +0100 | [diff] [blame] | 1467 | .. js:function:: HTTP.req_set_header(http, name, value) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 1468 | |
Bertrand Jacquin | 874a35c | 2018-09-10 21:26:07 +0100 | [diff] [blame] | 1469 | This variable replace all occurrence of all header "name", by only |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 1470 | one containing the "value". |
| 1471 | |
| 1472 | :param class_http http: The related http object. |
| 1473 | :param string name: The header name. |
| 1474 | :param string value: The header value. |
Thierry FOURNIER | 12a865d | 2016-12-14 19:40:37 +0100 | [diff] [blame] | 1475 | :see: :js:func:`HTTP.res_set_header` |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 1476 | |
Bertrand Jacquin | 874a35c | 2018-09-10 21:26:07 +0100 | [diff] [blame] | 1477 | This function does the same work as the following code: |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 1478 | |
| 1479 | .. code-block:: lua |
| 1480 | |
| 1481 | function fcn(txn) |
Thierry FOURNIER | 486f5a0 | 2015-03-16 15:13:03 +0100 | [diff] [blame] | 1482 | TXN.http:req_del_header("header") |
| 1483 | TXN.http:req_add_header("header", "value") |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 1484 | end |
| 1485 | .. |
| 1486 | |
Thierry FOURNIER | 486f5a0 | 2015-03-16 15:13:03 +0100 | [diff] [blame] | 1487 | .. js:function:: HTTP.res_set_header(http, name, value) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 1488 | |
Bertrand Jacquin | 874a35c | 2018-09-10 21:26:07 +0100 | [diff] [blame] | 1489 | This variable replace all occurrence of all header "name", by only |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 1490 | one containing the "value". |
| 1491 | |
| 1492 | :param class_http http: The related http object. |
| 1493 | :param string name: The header name. |
| 1494 | :param string value: The header value. |
Thierry FOURNIER | 12a865d | 2016-12-14 19:40:37 +0100 | [diff] [blame] | 1495 | :see: :js:func:`HTTP.req_rep_header()` |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 1496 | |
Pieter Baauw | 386a127 | 2015-08-16 15:26:24 +0200 | [diff] [blame] | 1497 | .. js:function:: HTTP.req_rep_header(http, name, regex, replace) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 1498 | |
| 1499 | Matches the regular expression in all occurrences of header field "name" |
| 1500 | according to "regex", and replaces them with the "replace" argument. The |
| 1501 | replacement value can contain back references like \1, \2, ... This |
| 1502 | function works with the request. |
| 1503 | |
| 1504 | :param class_http http: The related http object. |
| 1505 | :param string name: The header name. |
| 1506 | :param string regex: The match regular expression. |
| 1507 | :param string replace: The replacement value. |
Thierry FOURNIER | 12a865d | 2016-12-14 19:40:37 +0100 | [diff] [blame] | 1508 | :see: :js:func:`HTTP.res_rep_header()` |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 1509 | |
Pieter Baauw | 386a127 | 2015-08-16 15:26:24 +0200 | [diff] [blame] | 1510 | .. js:function:: HTTP.res_rep_header(http, name, regex, string) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 1511 | |
| 1512 | Matches the regular expression in all occurrences of header field "name" |
| 1513 | according to "regex", and replaces them with the "replace" argument. The |
| 1514 | replacement value can contain back references like \1, \2, ... This |
| 1515 | function works with the request. |
| 1516 | |
| 1517 | :param class_http http: The related http object. |
| 1518 | :param string name: The header name. |
| 1519 | :param string regex: The match regular expression. |
| 1520 | :param string replace: The replacement value. |
Thierry FOURNIER | 12a865d | 2016-12-14 19:40:37 +0100 | [diff] [blame] | 1521 | :see: :js:func:`HTTP.req_rep_header()` |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 1522 | |
Thierry FOURNIER | 486f5a0 | 2015-03-16 15:13:03 +0100 | [diff] [blame] | 1523 | .. js:function:: HTTP.req_set_method(http, method) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 1524 | |
| 1525 | Rewrites the request method with the parameter "method". |
| 1526 | |
| 1527 | :param class_http http: The related http object. |
| 1528 | :param string method: The new method. |
| 1529 | |
Thierry FOURNIER | 486f5a0 | 2015-03-16 15:13:03 +0100 | [diff] [blame] | 1530 | .. js:function:: HTTP.req_set_path(http, path) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 1531 | |
| 1532 | Rewrites the request path with the "path" parameter. |
| 1533 | |
| 1534 | :param class_http http: The related http object. |
| 1535 | :param string path: The new path. |
| 1536 | |
Thierry FOURNIER | 486f5a0 | 2015-03-16 15:13:03 +0100 | [diff] [blame] | 1537 | .. js:function:: HTTP.req_set_query(http, query) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 1538 | |
| 1539 | Rewrites the request's query string which appears after the first question |
| 1540 | mark ("?") with the parameter "query". |
| 1541 | |
| 1542 | :param class_http http: The related http object. |
| 1543 | :param string query: The new query. |
| 1544 | |
Thierry FOURNIER | 0d79cf6 | 2015-08-26 14:20:58 +0200 | [diff] [blame] | 1545 | .. js:function:: HTTP.req_set_uri(http, uri) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 1546 | |
| 1547 | Rewrites the request URI with the parameter "uri". |
| 1548 | |
| 1549 | :param class_http http: The related http object. |
| 1550 | :param string uri: The new uri. |
| 1551 | |
Robin H. Johnson | 52f5db2 | 2017-01-01 13:10:52 -0800 | [diff] [blame] | 1552 | .. js:function:: HTTP.res_set_status(http, status [, reason]) |
Thierry FOURNIER | 35d70ef | 2015-08-26 16:21:56 +0200 | [diff] [blame] | 1553 | |
Robin H. Johnson | 52f5db2 | 2017-01-01 13:10:52 -0800 | [diff] [blame] | 1554 | Rewrites the response status code with the parameter "code". |
| 1555 | |
| 1556 | 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] | 1557 | |
| 1558 | :param class_http http: The related http object. |
| 1559 | :param integer status: The new response status code. |
Robin H. Johnson | 52f5db2 | 2017-01-01 13:10:52 -0800 | [diff] [blame] | 1560 | :param string reason: The new response reason (optional). |
Thierry FOURNIER | 35d70ef | 2015-08-26 16:21:56 +0200 | [diff] [blame] | 1561 | |
Thierry FOURNIER | dc59500 | 2015-12-21 11:13:52 +0100 | [diff] [blame] | 1562 | .. _txn_class: |
| 1563 | |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 1564 | TXN class |
| 1565 | ========= |
| 1566 | |
| 1567 | .. js:class:: TXN |
| 1568 | |
| 1569 | The txn class contain all the functions relative to the http or tcp |
| 1570 | transaction (Note than a tcp stream is the same than a tcp transaction, but |
| 1571 | an HTTP transaction is not the same than a tcp stream). |
| 1572 | |
| 1573 | The usage of this class permits to retrieve data from the requests, alter it |
| 1574 | and forward it. |
| 1575 | |
| 1576 | All the functions provided by this class are available in the context |
| 1577 | **sample-fetches** and **actions**. |
| 1578 | |
Thierry FOURNIER | 486f5a0 | 2015-03-16 15:13:03 +0100 | [diff] [blame] | 1579 | .. js:attribute:: TXN.c |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 1580 | |
Thierry FOURNIER | dc59500 | 2015-12-21 11:13:52 +0100 | [diff] [blame] | 1581 | :returns: An :ref:`converters_class`. |
| 1582 | |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 1583 | This attribute contains a Converters class object. |
| 1584 | |
Thierry FOURNIER | 486f5a0 | 2015-03-16 15:13:03 +0100 | [diff] [blame] | 1585 | .. js:attribute:: TXN.sc |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 1586 | |
Thierry FOURNIER | dc59500 | 2015-12-21 11:13:52 +0100 | [diff] [blame] | 1587 | :returns: An :ref:`converters_class`. |
| 1588 | |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 1589 | This attribute contains a Converters class object. The functions of |
| 1590 | this object returns always a string. |
| 1591 | |
Thierry FOURNIER | 486f5a0 | 2015-03-16 15:13:03 +0100 | [diff] [blame] | 1592 | .. js:attribute:: TXN.f |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 1593 | |
Thierry FOURNIER | dc59500 | 2015-12-21 11:13:52 +0100 | [diff] [blame] | 1594 | :returns: An :ref:`fetches_class`. |
| 1595 | |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 1596 | This attribute contains a Fetches class object. |
| 1597 | |
Thierry FOURNIER | 486f5a0 | 2015-03-16 15:13:03 +0100 | [diff] [blame] | 1598 | .. js:attribute:: TXN.sf |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 1599 | |
Thierry FOURNIER | dc59500 | 2015-12-21 11:13:52 +0100 | [diff] [blame] | 1600 | :returns: An :ref:`fetches_class`. |
| 1601 | |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 1602 | This attribute contains a Fetches class object. The functions of |
| 1603 | this object returns always a string. |
| 1604 | |
Thierry FOURNIER | 486f5a0 | 2015-03-16 15:13:03 +0100 | [diff] [blame] | 1605 | .. js:attribute:: TXN.req |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 1606 | |
Thierry FOURNIER | dc59500 | 2015-12-21 11:13:52 +0100 | [diff] [blame] | 1607 | :returns: An :ref:`channel_class`. |
| 1608 | |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 1609 | This attribute contains a channel class object for the request buffer. |
| 1610 | |
Thierry FOURNIER | 486f5a0 | 2015-03-16 15:13:03 +0100 | [diff] [blame] | 1611 | .. js:attribute:: TXN.res |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 1612 | |
Thierry FOURNIER | dc59500 | 2015-12-21 11:13:52 +0100 | [diff] [blame] | 1613 | :returns: An :ref:`channel_class`. |
| 1614 | |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 1615 | This attribute contains a channel class object for the response buffer. |
| 1616 | |
Thierry FOURNIER | 486f5a0 | 2015-03-16 15:13:03 +0100 | [diff] [blame] | 1617 | .. js:attribute:: TXN.http |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 1618 | |
Thierry FOURNIER | dc59500 | 2015-12-21 11:13:52 +0100 | [diff] [blame] | 1619 | :returns: An :ref:`http_class`. |
| 1620 | |
Bertrand Jacquin | 874a35c | 2018-09-10 21:26:07 +0100 | [diff] [blame] | 1621 | 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] | 1622 | proxy has the "mode http" enabled. |
| 1623 | |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 1624 | .. js:function:: TXN.log(TXN, loglevel, msg) |
| 1625 | |
| 1626 | This function sends a log. The log is sent, according with the HAProxy |
| 1627 | configuration file, on the default syslog server if it is configured and on |
| 1628 | the stderr if it is allowed. |
| 1629 | |
| 1630 | :param class_txn txn: The class txn object containing the data. |
Bertrand Jacquin | 874a35c | 2018-09-10 21:26:07 +0100 | [diff] [blame] | 1631 | :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] | 1632 | number between 0 and 7. |
| 1633 | :param string msg: The log content. |
Thierry FOURNIER | 12a865d | 2016-12-14 19:40:37 +0100 | [diff] [blame] | 1634 | :see: :js:attr:`core.emerg`, :js:attr:`core.alert`, :js:attr:`core.crit`, |
| 1635 | :js:attr:`core.err`, :js:attr:`core.warning`, :js:attr:`core.notice`, |
| 1636 | :js:attr:`core.info`, :js:attr:`core.debug` (log level definitions) |
| 1637 | :see: :js:func:`TXN.deflog` |
| 1638 | :see: :js:func:`TXN.Debug` |
| 1639 | :see: :js:func:`TXN.Info` |
| 1640 | :see: :js:func:`TXN.Warning` |
| 1641 | :see: :js:func:`TXN.Alert` |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 1642 | |
| 1643 | .. js:function:: TXN.deflog(TXN, msg) |
| 1644 | |
Bertrand Jacquin | 874a35c | 2018-09-10 21:26:07 +0100 | [diff] [blame] | 1645 | 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] | 1646 | transaction. |
| 1647 | |
| 1648 | :param class_txn txn: The class txn object containing the data. |
| 1649 | :param string msg: The log content. |
Thierry FOURNIER | 12a865d | 2016-12-14 19:40:37 +0100 | [diff] [blame] | 1650 | :see: :js:func:`TXN.log |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 1651 | |
| 1652 | .. js:function:: TXN.Debug(txn, msg) |
| 1653 | |
| 1654 | :param class_txn txn: The class txn object containing the data. |
| 1655 | :param string msg: The log content. |
Thierry FOURNIER | 12a865d | 2016-12-14 19:40:37 +0100 | [diff] [blame] | 1656 | :see: :js:func:`TXN.log` |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 1657 | |
| 1658 | Does the same job than: |
| 1659 | |
| 1660 | .. code-block:: lua |
| 1661 | |
Thierry FOURNIER | dc59500 | 2015-12-21 11:13:52 +0100 | [diff] [blame] | 1662 | function Debug(txn, msg) |
| 1663 | TXN.log(txn, core.debug, msg) |
| 1664 | end |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 1665 | .. |
| 1666 | |
| 1667 | .. js:function:: TXN.Info(txn, msg) |
| 1668 | |
| 1669 | :param class_txn txn: The class txn object containing the data. |
| 1670 | :param string msg: The log content. |
Thierry FOURNIER | 12a865d | 2016-12-14 19:40:37 +0100 | [diff] [blame] | 1671 | :see: :js:func:`TXN.log` |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 1672 | |
| 1673 | .. code-block:: lua |
| 1674 | |
Thierry FOURNIER | dc59500 | 2015-12-21 11:13:52 +0100 | [diff] [blame] | 1675 | function Debug(txn, msg) |
| 1676 | TXN.log(txn, core.info, msg) |
| 1677 | end |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 1678 | .. |
| 1679 | |
| 1680 | .. js:function:: TXN.Warning(txn, msg) |
| 1681 | |
| 1682 | :param class_txn txn: The class txn object containing the data. |
| 1683 | :param string msg: The log content. |
Thierry FOURNIER | 12a865d | 2016-12-14 19:40:37 +0100 | [diff] [blame] | 1684 | :see: :js:func:`TXN.log` |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 1685 | |
| 1686 | .. code-block:: lua |
| 1687 | |
Thierry FOURNIER | dc59500 | 2015-12-21 11:13:52 +0100 | [diff] [blame] | 1688 | function Debug(txn, msg) |
| 1689 | TXN.log(txn, core.warning, msg) |
| 1690 | end |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 1691 | .. |
| 1692 | |
| 1693 | .. js:function:: TXN.Alert(txn, msg) |
| 1694 | |
| 1695 | :param class_txn txn: The class txn object containing the data. |
| 1696 | :param string msg: The log content. |
Thierry FOURNIER | 12a865d | 2016-12-14 19:40:37 +0100 | [diff] [blame] | 1697 | :see: :js:func:`TXN.log` |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 1698 | |
| 1699 | .. code-block:: lua |
| 1700 | |
Thierry FOURNIER | dc59500 | 2015-12-21 11:13:52 +0100 | [diff] [blame] | 1701 | function Debug(txn, msg) |
| 1702 | TXN.log(txn, core.alert, msg) |
| 1703 | end |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 1704 | .. |
| 1705 | |
Thierry FOURNIER | 486f5a0 | 2015-03-16 15:13:03 +0100 | [diff] [blame] | 1706 | .. js:function:: TXN.get_priv(txn) |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 1707 | |
Thierry FOURNIER | 486f5a0 | 2015-03-16 15:13:03 +0100 | [diff] [blame] | 1708 | 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] | 1709 | function. If no data are stored, it returns a nil value. |
| 1710 | |
| 1711 | :param class_txn txn: The class txn object containing the data. |
Bertrand Jacquin | 874a35c | 2018-09-10 21:26:07 +0100 | [diff] [blame] | 1712 | :returns: the opaque data previously stored, or nil if nothing is |
| 1713 | available. |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 1714 | |
Thierry FOURNIER | 486f5a0 | 2015-03-16 15:13:03 +0100 | [diff] [blame] | 1715 | .. js:function:: TXN.set_priv(txn, data) |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 1716 | |
| 1717 | Store any data in the current HAProxy transaction. This action replace the |
| 1718 | old stored data. |
| 1719 | |
| 1720 | :param class_txn txn: The class txn object containing the data. |
| 1721 | :param opaque data: The data which is stored in the transaction. |
| 1722 | |
Tim Duesterhus | 4e172c9 | 2020-05-19 13:49:42 +0200 | [diff] [blame] | 1723 | .. js:function:: TXN.set_var(TXN, var, value[, ifexist]) |
Thierry FOURNIER | 053ba8ad | 2015-06-08 13:05:33 +0200 | [diff] [blame] | 1724 | |
David Carlier | 61fdf8b | 2015-10-02 11:59:38 +0100 | [diff] [blame] | 1725 | 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] | 1726 | |
| 1727 | :param class_txn txn: The class txn object containing the data. |
| 1728 | :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] | 1729 | :param type value: The value associated to the variable. The type can be string or |
| 1730 | integer. |
Tim Duesterhus | 4e172c9 | 2020-05-19 13:49:42 +0200 | [diff] [blame] | 1731 | :param boolean ifexist: If this parameter is set to a truthy value the variable |
| 1732 | will only be set if it was defined elsewhere (i.e. used |
| 1733 | within the configuration). It is highly recommended to |
| 1734 | always set this to true. |
Christopher Faulet | 85d79c9 | 2016-11-09 16:54:56 +0100 | [diff] [blame] | 1735 | |
| 1736 | .. js:function:: TXN.unset_var(TXN, var) |
| 1737 | |
| 1738 | Unset the variable <var>. |
| 1739 | |
| 1740 | :param class_txn txn: The class txn object containing the data. |
| 1741 | :param string var: The variable name according with the HAProxy variable syntax. |
Thierry FOURNIER | 053ba8ad | 2015-06-08 13:05:33 +0200 | [diff] [blame] | 1742 | |
| 1743 | .. js:function:: TXN.get_var(TXN, var) |
| 1744 | |
| 1745 | Returns data stored in the variable <var> converter in Lua type. |
| 1746 | |
| 1747 | :param class_txn txn: The class txn object containing the data. |
| 1748 | :param string var: The variable name according with the HAProxy variable syntax. |
| 1749 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 1750 | .. js:function:: TXN.reply([reply]) |
| 1751 | |
| 1752 | Return a new reply object |
| 1753 | |
| 1754 | :param table reply: A table containing info to initialize the reply fields. |
| 1755 | :returns: A :ref:`reply_class` object. |
| 1756 | |
| 1757 | The table used to initialized the reply object may contain following entries : |
| 1758 | |
| 1759 | * status : The reply status code. the code 200 is used by default. |
| 1760 | * reason : The reply reason. The reason corresponding to the status code is |
| 1761 | used by default. |
| 1762 | * headers : An list of headers, indexed by header name. Empty by default. For |
| 1763 | a given name, multiple values are possible, stored in an ordered list. |
| 1764 | * body : The reply body, empty by default. |
| 1765 | |
| 1766 | .. code-block:: lua |
| 1767 | |
| 1768 | local reply = txn:reply{ |
| 1769 | status = 400, |
| 1770 | reason = "Bad request", |
| 1771 | headers = { |
| 1772 | ["content-type"] = { "text/html" }, |
| 1773 | ["cache-control"] = {"no-cache", "no-store" } |
| 1774 | }, |
| 1775 | body = "<html><body><h1>invalid request<h1></body></html>" |
| 1776 | } |
| 1777 | .. |
| 1778 | :see: :js:class:`Reply` |
| 1779 | |
| 1780 | .. js:function:: TXN.done(txn[, reply]) |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 1781 | |
Willy Tarreau | bc183a6 | 2015-08-28 10:39:11 +0200 | [diff] [blame] | 1782 | This function terminates processing of the transaction and the associated |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 1783 | session and optionally reply to the client for HTTP sessions. |
| 1784 | |
| 1785 | :param class_txn txn: The class txn object containing the data. |
| 1786 | :param class_reply reply: The class reply object to return to the client. |
| 1787 | |
| 1788 | 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] | 1789 | 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] | 1790 | To do so, a reply may be provided. This object is optional and may contain a |
| 1791 | status code, a reason, a header list and a body. All these fields are |
| 1792 | optionnals. When not provided, the default values are used. By default, with |
| 1793 | an empty reply object, an empty HTTP 200 response is returned to the |
| 1794 | client. If no reply object is provided, the transaction is terminated without |
| 1795 | any reply. |
| 1796 | |
| 1797 | The reply object may be fully created in lua or the class Reply may be used to |
| 1798 | create it. |
| 1799 | |
| 1800 | .. code-block:: lua |
| 1801 | |
| 1802 | local reply = txn:reply() |
| 1803 | reply:set_status(400, "Bad request") |
| 1804 | reply:add_header("content-type", "text/html") |
| 1805 | reply:add_header("cache-control", "no-cache") |
| 1806 | reply:add_header("cache-control", "no-store") |
| 1807 | reply:set_body("<html><body><h1>invalid request<h1></body></html>") |
| 1808 | txn:done(reply) |
| 1809 | .. |
| 1810 | |
| 1811 | .. code-block:: lua |
| 1812 | |
| 1813 | txn:done{ |
| 1814 | status = 400, |
| 1815 | reason = "Bad request", |
| 1816 | headers = { |
| 1817 | ["content-type"] = { "text/html" }, |
| 1818 | ["cache-control"] = { "no-cache", "no-store" }, |
| 1819 | }, |
| 1820 | body = "<html><body><h1>invalid request<h1></body></html>" |
| 1821 | } |
| 1822 | .. |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 1823 | |
Thierry FOURNIER | ab00df6 | 2016-07-14 11:42:37 +0200 | [diff] [blame] | 1824 | *Warning*: It not make sense to call this function from sample-fetches. In |
| 1825 | this case the behaviour of this one is the same than core.done(): it quit |
| 1826 | the Lua execution. The transaction is really aborted only from an action |
| 1827 | registered function. |
| 1828 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 1829 | :see: :js:func:`TXN.reply`, :js:class:`Reply` |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 1830 | |
Thierry FOURNIER | 486f5a0 | 2015-03-16 15:13:03 +0100 | [diff] [blame] | 1831 | .. js:function:: TXN.set_loglevel(txn, loglevel) |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 1832 | |
| 1833 | Is used to change the log level of the current request. The "loglevel" must |
| 1834 | be an integer between 0 and 7. |
| 1835 | |
| 1836 | :param class_txn txn: The class txn object containing the data. |
| 1837 | :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] | 1838 | :see: :js:attr:`core.emerg`, :js:attr:`core.alert`, :js:attr:`core.crit`, |
| 1839 | :js:attr:`core.err`, :js:attr:`core.warning`, :js:attr:`core.notice`, |
| 1840 | :js:attr:`core.info`, :js:attr:`core.debug` (log level definitions) |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 1841 | |
Thierry FOURNIER | 486f5a0 | 2015-03-16 15:13:03 +0100 | [diff] [blame] | 1842 | .. js:function:: TXN.set_tos(txn, tos) |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 1843 | |
| 1844 | Is used to set the TOS or DSCP field value of packets sent to the client to |
| 1845 | the value passed in "tos" on platforms which support this. |
| 1846 | |
| 1847 | :param class_txn txn: The class txn object containing the data. |
| 1848 | :param integer tos: The new TOS os DSCP. |
| 1849 | |
Thierry FOURNIER | 486f5a0 | 2015-03-16 15:13:03 +0100 | [diff] [blame] | 1850 | .. js:function:: TXN.set_mark(txn, mark) |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 1851 | |
| 1852 | Is used to set the Netfilter MARK on all packets sent to the client to the |
| 1853 | value passed in "mark" on platforms which support it. |
| 1854 | |
| 1855 | :param class_txn txn: The class txn object containing the data. |
| 1856 | :param integer mark: The mark value. |
| 1857 | |
Patrick Hemmer | 268a707 | 2018-05-11 12:52:31 -0400 | [diff] [blame] | 1858 | .. js:function:: TXN.set_priority_class(txn, prio) |
| 1859 | |
| 1860 | This function adjusts the priority class of the transaction. The value should |
| 1861 | be within the range -2047..2047. Values outside this range will be |
| 1862 | truncated. |
| 1863 | |
| 1864 | See the HAProxy configuration.txt file keyword "http-request" action |
| 1865 | "set-priority-class" for details. |
| 1866 | |
| 1867 | .. js:function:: TXN.set_priority_offset(txn, prio) |
| 1868 | |
| 1869 | This function adjusts the priority offset of the transaction. The value |
| 1870 | should be within the range -524287..524287. Values outside this range will be |
| 1871 | truncated. |
| 1872 | |
| 1873 | See the HAProxy configuration.txt file keyword "http-request" action |
| 1874 | "set-priority-offset" for details. |
| 1875 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 1876 | .. _reply_class: |
| 1877 | |
| 1878 | Reply class |
| 1879 | ============ |
| 1880 | |
| 1881 | .. js:class:: Reply |
| 1882 | |
| 1883 | **context**: action |
| 1884 | |
| 1885 | This class represents an HTTP response message. It provides some methods to |
| 1886 | enrich it. |
| 1887 | |
| 1888 | .. code-block:: lua |
| 1889 | |
| 1890 | local reply = txn:reply({status = 400}) -- default HTTP 400 reason-phase used |
| 1891 | reply:add_header("content-type", "text/html") |
| 1892 | reply:add_header("cache-control", "no-cache") |
| 1893 | reply:add_header("cache-control", "no-store") |
| 1894 | reply:set_body("<html><body><h1>invalid request<h1></body></html>") |
| 1895 | .. |
| 1896 | |
| 1897 | :see: :js:func:`TXN.reply` |
| 1898 | |
| 1899 | .. js:attribute:: Reply.status |
| 1900 | |
| 1901 | The reply status code. By default, the status code is set to 200. |
| 1902 | |
| 1903 | :returns: integer |
| 1904 | |
| 1905 | .. js:attribute:: Reply.reason |
| 1906 | |
| 1907 | The reason string describing the status code. |
| 1908 | |
| 1909 | :returns: string |
| 1910 | |
| 1911 | .. js:attribute:: Reply.headers |
| 1912 | |
| 1913 | A table indexing all reply headers by name. To each name is associated an |
| 1914 | ordered list of values. |
| 1915 | |
| 1916 | :returns: Lua table |
| 1917 | |
| 1918 | .. code-block:: lua |
| 1919 | |
| 1920 | { |
| 1921 | ["content-type"] = { "text/html" }, |
| 1922 | ["cache-control"] = {"no-cache", "no-store" }, |
| 1923 | x_header_name = { "value1", "value2", ... } |
| 1924 | ... |
| 1925 | } |
| 1926 | .. |
| 1927 | |
| 1928 | .. js:attribute:: Reply.body |
| 1929 | |
| 1930 | The reply payload. |
| 1931 | |
| 1932 | :returns: string |
| 1933 | |
| 1934 | .. js:function:: Reply.set_status(REPLY, status[, reason]) |
| 1935 | |
| 1936 | Set the reply status code and optionally the reason-phrase. If the reason is |
| 1937 | not provided, the default reason corresponding to the status code is used. |
| 1938 | |
| 1939 | :param class_reply reply: The related Reply object. |
| 1940 | :param integer status: The reply status code. |
| 1941 | :param string reason: The reply status reason (optional). |
| 1942 | |
| 1943 | .. js:function:: Reply.add_header(REPLY, name, value) |
| 1944 | |
| 1945 | Add a header to the reply object. If the header does not already exist, a new |
| 1946 | entry is created with its name as index and a one-element list containing its |
| 1947 | value as value. Otherwise, the header value is appended to the ordered list of |
| 1948 | values associated to the header name. |
| 1949 | |
| 1950 | :param class_reply reply: The related Reply object. |
| 1951 | :param string name: The header field name. |
| 1952 | :param string value: The header field value. |
| 1953 | |
| 1954 | .. js:function:: Reply.del_header(REPLY, name) |
| 1955 | |
| 1956 | Remove all occurrences of a header name from the reply object. |
| 1957 | |
| 1958 | :param class_reply reply: The related Reply object. |
| 1959 | :param string name: The header field name. |
| 1960 | |
| 1961 | .. js:function:: Reply.set_body(REPLY, body) |
| 1962 | |
| 1963 | Set the reply payload. |
| 1964 | |
| 1965 | :param class_reply reply: The related Reply object. |
| 1966 | :param string body: The reply payload. |
| 1967 | |
Thierry FOURNIER | dc59500 | 2015-12-21 11:13:52 +0100 | [diff] [blame] | 1968 | .. _socket_class: |
| 1969 | |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 1970 | Socket class |
| 1971 | ============ |
| 1972 | |
| 1973 | .. js:class:: Socket |
| 1974 | |
| 1975 | This class must be compatible with the Lua Socket class. Only the 'client' |
| 1976 | functions are available. See the Lua Socket documentation: |
| 1977 | |
| 1978 | `http://w3.impa.br/~diego/software/luasocket/tcp.html |
| 1979 | <http://w3.impa.br/~diego/software/luasocket/tcp.html>`_ |
| 1980 | |
Thierry FOURNIER | 486f5a0 | 2015-03-16 15:13:03 +0100 | [diff] [blame] | 1981 | .. js:function:: Socket.close(socket) |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 1982 | |
| 1983 | Closes a TCP object. The internal socket used by the object is closed and the |
| 1984 | local address to which the object was bound is made available to other |
| 1985 | applications. No further operations (except for further calls to the close |
Thierry FOURNIER | 486f5a0 | 2015-03-16 15:13:03 +0100 | [diff] [blame] | 1986 | method) are allowed on a closed Socket. |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 1987 | |
Thierry FOURNIER | 486f5a0 | 2015-03-16 15:13:03 +0100 | [diff] [blame] | 1988 | :param class_socket socket: Is the manipulated Socket. |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 1989 | |
| 1990 | Note: It is important to close all used sockets once they are not needed, |
| 1991 | since, in many systems, each socket uses a file descriptor, which are limited |
| 1992 | system resources. Garbage-collected objects are automatically closed before |
| 1993 | destruction, though. |
| 1994 | |
Thierry FOURNIER | a3bc513 | 2015-09-25 21:43:56 +0200 | [diff] [blame] | 1995 | .. js:function:: Socket.connect(socket, address[, port]) |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 1996 | |
| 1997 | Attempts to connect a socket object to a remote host. |
| 1998 | |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 1999 | |
| 2000 | In case of error, the method returns nil followed by a string describing the |
| 2001 | error. In case of success, the method returns 1. |
| 2002 | |
Thierry FOURNIER | 486f5a0 | 2015-03-16 15:13:03 +0100 | [diff] [blame] | 2003 | :param class_socket socket: Is the manipulated Socket. |
Thierry FOURNIER | a3bc513 | 2015-09-25 21:43:56 +0200 | [diff] [blame] | 2004 | :param string address: can be an IP address or a host name. See below for more |
| 2005 | information. |
| 2006 | :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] | 2007 | :returns: 1 or nil. |
| 2008 | |
Bertrand Jacquin | 874a35c | 2018-09-10 21:26:07 +0100 | [diff] [blame] | 2009 | 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] | 2010 | other stream than TCP. The syntax containing a simpleipv4 or ipv6 address is |
| 2011 | the basically expected format. This format requires the port. |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 2012 | |
Thierry FOURNIER | a3bc513 | 2015-09-25 21:43:56 +0200 | [diff] [blame] | 2013 | 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] | 2014 | connect to a socket. Abstract namespaces are supported with the prefix |
Joseph Herlant | 02cedc4 | 2018-11-13 19:45:17 -0800 | [diff] [blame] | 2015 | "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] | 2016 | 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] | 2017 | 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] | 2018 | parameter *port* must not be set. |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 2019 | |
Thierry FOURNIER | 486f5a0 | 2015-03-16 15:13:03 +0100 | [diff] [blame] | 2020 | .. js:function:: Socket.connect_ssl(socket, address, port) |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 2021 | |
| 2022 | Same behavior than the function socket:connect, but uses SSL. |
| 2023 | |
Thierry FOURNIER | 486f5a0 | 2015-03-16 15:13:03 +0100 | [diff] [blame] | 2024 | :param class_socket socket: Is the manipulated Socket. |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 2025 | :returns: 1 or nil. |
| 2026 | |
Thierry FOURNIER | 486f5a0 | 2015-03-16 15:13:03 +0100 | [diff] [blame] | 2027 | .. js:function:: Socket.getpeername(socket) |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 2028 | |
| 2029 | Returns information about the remote side of a connected client object. |
| 2030 | |
| 2031 | Returns a string with the IP address of the peer, followed by the port number |
| 2032 | that peer is using for the connection. In case of error, the method returns |
| 2033 | nil. |
| 2034 | |
Thierry FOURNIER | 486f5a0 | 2015-03-16 15:13:03 +0100 | [diff] [blame] | 2035 | :param class_socket socket: Is the manipulated Socket. |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 2036 | :returns: a string containing the server information. |
| 2037 | |
Thierry FOURNIER | 486f5a0 | 2015-03-16 15:13:03 +0100 | [diff] [blame] | 2038 | .. js:function:: Socket.getsockname(socket) |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 2039 | |
| 2040 | Returns the local address information associated to the object. |
| 2041 | |
| 2042 | The method returns a string with local IP address and a number with the port. |
| 2043 | In case of error, the method returns nil. |
| 2044 | |
Thierry FOURNIER | 486f5a0 | 2015-03-16 15:13:03 +0100 | [diff] [blame] | 2045 | :param class_socket socket: Is the manipulated Socket. |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 2046 | :returns: a string containing the client information. |
| 2047 | |
Thierry FOURNIER | 486f5a0 | 2015-03-16 15:13:03 +0100 | [diff] [blame] | 2048 | .. js:function:: Socket.receive(socket, [pattern [, prefix]]) |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 2049 | |
| 2050 | Reads data from a client object, according to the specified read pattern. |
| 2051 | Patterns follow the Lua file I/O format, and the difference in performance |
| 2052 | between all patterns is negligible. |
| 2053 | |
Thierry FOURNIER | 486f5a0 | 2015-03-16 15:13:03 +0100 | [diff] [blame] | 2054 | :param class_socket socket: Is the manipulated Socket. |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 2055 | :param string|integer pattern: Describe what is required (see below). |
| 2056 | :param string prefix: A string which will be prefix the returned data. |
| 2057 | :returns: a string containing the required data or nil. |
| 2058 | |
| 2059 | Pattern can be any of the following: |
| 2060 | |
| 2061 | * **`*a`**: reads from the socket until the connection is closed. No |
| 2062 | end-of-line translation is performed; |
| 2063 | |
Thierry FOURNIER | 486f5a0 | 2015-03-16 15:13:03 +0100 | [diff] [blame] | 2064 | * **`*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] | 2065 | LF character (ASCII 10), optionally preceded by a CR character |
| 2066 | (ASCII 13). The CR and LF characters are not included in the |
| 2067 | returned line. In fact, all CR characters are ignored by the |
| 2068 | pattern. This is the default pattern. |
| 2069 | |
| 2070 | * **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] | 2071 | Socket. Prefix is an optional string to be concatenated to the |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 2072 | beginning of any received data before return. |
| 2073 | |
Thierry FOURNIER | a3bc513 | 2015-09-25 21:43:56 +0200 | [diff] [blame] | 2074 | * **empty**: If the pattern is left empty, the default option is `*l`. |
| 2075 | |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 2076 | If successful, the method returns the received pattern. In case of error, the |
| 2077 | method returns nil followed by an error message which can be the string |
| 2078 | 'closed' in case the connection was closed before the transmission was |
| 2079 | completed or the string 'timeout' in case there was a timeout during the |
| 2080 | operation. Also, after the error message, the function returns the partial |
| 2081 | result of the transmission. |
| 2082 | |
| 2083 | Important note: This function was changed severely. It used to support |
| 2084 | multiple patterns (but I have never seen this feature used) and now it |
| 2085 | doesn't anymore. Partial results used to be returned in the same way as |
| 2086 | successful results. This last feature violated the idea that all functions |
| 2087 | should return nil on error. Thus it was changed too. |
| 2088 | |
Thierry FOURNIER | 486f5a0 | 2015-03-16 15:13:03 +0100 | [diff] [blame] | 2089 | .. js:function:: Socket.send(socket, data [, start [, end ]]) |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 2090 | |
| 2091 | Sends data through client object. |
| 2092 | |
Thierry FOURNIER | 486f5a0 | 2015-03-16 15:13:03 +0100 | [diff] [blame] | 2093 | :param class_socket socket: Is the manipulated Socket. |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 2094 | :param string data: The data that will be sent. |
| 2095 | :param integer start: The start position in the buffer of the data which will |
| 2096 | be sent. |
| 2097 | :param integer end: The end position in the buffer of the data which will |
| 2098 | be sent. |
| 2099 | :returns: see below. |
| 2100 | |
| 2101 | Data is the string to be sent. The optional arguments i and j work exactly |
| 2102 | like the standard string.sub Lua function to allow the selection of a |
| 2103 | substring to be sent. |
| 2104 | |
| 2105 | If successful, the method returns the index of the last byte within [start, |
| 2106 | end] that has been sent. Notice that, if start is 1 or absent, this is |
| 2107 | effectively the total number of bytes sent. In case of error, the method |
| 2108 | returns nil, followed by an error message, followed by the index of the last |
| 2109 | byte within [start, end] that has been sent. You might want to try again from |
| 2110 | the byte following that. The error message can be 'closed' in case the |
| 2111 | connection was closed before the transmission was completed or the string |
| 2112 | 'timeout' in case there was a timeout during the operation. |
| 2113 | |
| 2114 | Note: Output is not buffered. For small strings, it is always better to |
| 2115 | concatenate them in Lua (with the '..' operator) and send the result in one |
| 2116 | call instead of calling the method several times. |
| 2117 | |
Thierry FOURNIER | 486f5a0 | 2015-03-16 15:13:03 +0100 | [diff] [blame] | 2118 | .. js:function:: Socket.setoption(socket, option [, value]) |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 2119 | |
| 2120 | Just implemented for compatibility, this cal does nothing. |
| 2121 | |
Thierry FOURNIER | 486f5a0 | 2015-03-16 15:13:03 +0100 | [diff] [blame] | 2122 | .. js:function:: Socket.settimeout(socket, value [, mode]) |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 2123 | |
| 2124 | Changes the timeout values for the object. All I/O operations are blocking. |
| 2125 | That is, any call to the methods send, receive, and accept will block |
| 2126 | indefinitely, until the operation completes. The settimeout method defines a |
| 2127 | limit on the amount of time the I/O methods can block. When a timeout time |
| 2128 | has elapsed, the affected methods give up and fail with an error code. |
| 2129 | |
| 2130 | The amount of time to wait is specified as the value parameter, in seconds. |
| 2131 | |
Mark Lakes | 56cc125 | 2018-03-27 09:48:06 +0200 | [diff] [blame] | 2132 | The timeout modes are not implemented, the only settable timeout is the |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 2133 | inactivity time waiting for complete the internal buffer send or waiting for |
| 2134 | receive data. |
| 2135 | |
Thierry FOURNIER | 486f5a0 | 2015-03-16 15:13:03 +0100 | [diff] [blame] | 2136 | :param class_socket socket: Is the manipulated Socket. |
Bertrand Jacquin | 874a35c | 2018-09-10 21:26:07 +0100 | [diff] [blame] | 2137 | :param float value: The timeout value. Use floating point to specify |
Mark Lakes | 56cc125 | 2018-03-27 09:48:06 +0200 | [diff] [blame] | 2138 | milliseconds. |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 2139 | |
Thierry FOURNIER | 3190427 | 2017-10-25 12:59:51 +0200 | [diff] [blame] | 2140 | .. _regex_class: |
| 2141 | |
| 2142 | Regex class |
| 2143 | =========== |
| 2144 | |
| 2145 | .. js:class:: Regex |
| 2146 | |
| 2147 | This class allows the usage of HAProxy regexes because classic lua doesn't |
| 2148 | provides regexes. This class inherits the HAProxy compilation options, so the |
| 2149 | regexes can be libc regex, pcre regex or pcre JIT regex. |
| 2150 | |
| 2151 | The expression matching number is limited to 20 per regex. The only available |
| 2152 | option is case sensitive. |
| 2153 | |
| 2154 | Because regexes compilation is a heavy process, it is better to define all |
| 2155 | your regexes in the **body context** and use it during the runtime. |
| 2156 | |
| 2157 | .. code-block:: lua |
| 2158 | |
| 2159 | -- Create the regex |
| 2160 | st, regex = Regex.new("needle (..) (...)", true); |
| 2161 | |
| 2162 | -- Check compilation errors |
| 2163 | if st == false then |
| 2164 | print "error: " .. regex |
| 2165 | end |
| 2166 | |
| 2167 | -- Match the regexes |
| 2168 | print(regex:exec("Looking for a needle in the haystack")) -- true |
| 2169 | print(regex:exec("Lokking for a cat in the haystack")) -- false |
| 2170 | |
| 2171 | -- Extract words |
| 2172 | st, list = regex:match("Looking for a needle in the haystack") |
| 2173 | print(st) -- true |
| 2174 | print(list[1]) -- needle in the |
| 2175 | print(list[2]) -- in |
| 2176 | print(list[3]) -- the |
| 2177 | |
| 2178 | .. js:function:: Regex.new(regex, case_sensitive) |
| 2179 | |
| 2180 | Create and compile a regex. |
| 2181 | |
| 2182 | :param string regex: The regular expression according with the libc or pcre |
| 2183 | standard |
| 2184 | :param boolean case_sensitive: Match is case sensitive or not. |
| 2185 | :returns: boolean status and :ref:`regex_class` or string containing fail reason. |
| 2186 | |
| 2187 | .. js:function:: Regex.exec(regex, str) |
| 2188 | |
| 2189 | Execute the regex. |
| 2190 | |
| 2191 | :param class_regex regex: A :ref:`regex_class` object. |
| 2192 | :param string str: The input string will be compared with the compiled regex. |
| 2193 | :returns: a boolean status according with the match result. |
| 2194 | |
| 2195 | .. js:function:: Regex.match(regex, str) |
| 2196 | |
| 2197 | Execute the regex and return matched expressions. |
| 2198 | |
| 2199 | :param class_map map: A :ref:`regex_class` object. |
| 2200 | :param string str: The input string will be compared with the compiled regex. |
| 2201 | :returns: a boolean status according with the match result, and |
| 2202 | a table containing all the string matched in order of declaration. |
| 2203 | |
Thierry FOURNIER | dc59500 | 2015-12-21 11:13:52 +0100 | [diff] [blame] | 2204 | .. _map_class: |
| 2205 | |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 2206 | Map class |
| 2207 | ========= |
| 2208 | |
| 2209 | .. js:class:: Map |
| 2210 | |
| 2211 | 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] | 2212 | be modified during the runtime through the HAProxy management socket. |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 2213 | |
| 2214 | .. code-block:: lua |
| 2215 | |
Thierry FOURNIER | dc59500 | 2015-12-21 11:13:52 +0100 | [diff] [blame] | 2216 | default = "usa" |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 2217 | |
Thierry FOURNIER | dc59500 | 2015-12-21 11:13:52 +0100 | [diff] [blame] | 2218 | -- Create and load map |
Thierry FOURNIER | 4dc7197 | 2017-01-28 08:33:08 +0100 | [diff] [blame] | 2219 | geo = Map.new("geo.map", Map._ip); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 2220 | |
Thierry FOURNIER | dc59500 | 2015-12-21 11:13:52 +0100 | [diff] [blame] | 2221 | -- Create new fetch that returns the user country |
| 2222 | core.register_fetches("country", function(txn) |
| 2223 | local src; |
| 2224 | local loc; |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 2225 | |
Thierry FOURNIER | dc59500 | 2015-12-21 11:13:52 +0100 | [diff] [blame] | 2226 | src = txn.f:fhdr("x-forwarded-for"); |
| 2227 | if (src == nil) then |
| 2228 | src = txn.f:src() |
| 2229 | if (src == nil) then |
| 2230 | return default; |
| 2231 | end |
| 2232 | end |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 2233 | |
Thierry FOURNIER | dc59500 | 2015-12-21 11:13:52 +0100 | [diff] [blame] | 2234 | -- Perform lookup |
| 2235 | loc = geo:lookup(src); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 2236 | |
Thierry FOURNIER | dc59500 | 2015-12-21 11:13:52 +0100 | [diff] [blame] | 2237 | if (loc == nil) then |
| 2238 | return default; |
| 2239 | end |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 2240 | |
Thierry FOURNIER | dc59500 | 2015-12-21 11:13:52 +0100 | [diff] [blame] | 2241 | return loc; |
| 2242 | end); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 2243 | |
Thierry FOURNIER | 4dc7197 | 2017-01-28 08:33:08 +0100 | [diff] [blame] | 2244 | .. js:attribute:: Map._int |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 2245 | |
| 2246 | See the HAProxy configuration.txt file, chapter "Using ACLs and fetching |
Ilya Shipitsin | 2075ca8 | 2020-03-06 23:22:22 +0500 | [diff] [blame] | 2247 | samples" and subchapter "ACL basics" to understand this pattern matching |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 2248 | method. |
| 2249 | |
Thierry FOURNIER | 4dc7197 | 2017-01-28 08:33:08 +0100 | [diff] [blame] | 2250 | Note that :js:attr:`Map.int` is also available for compatibility. |
| 2251 | |
| 2252 | .. js:attribute:: Map._ip |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 2253 | |
| 2254 | See the HAProxy configuration.txt file, chapter "Using ACLs and fetching |
Ilya Shipitsin | 2075ca8 | 2020-03-06 23:22:22 +0500 | [diff] [blame] | 2255 | samples" and subchapter "ACL basics" to understand this pattern matching |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 2256 | method. |
| 2257 | |
Thierry FOURNIER | 4dc7197 | 2017-01-28 08:33:08 +0100 | [diff] [blame] | 2258 | Note that :js:attr:`Map.ip` is also available for compatibility. |
| 2259 | |
| 2260 | .. js:attribute:: Map._str |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 2261 | |
| 2262 | See the HAProxy configuration.txt file, chapter "Using ACLs and fetching |
Ilya Shipitsin | 2075ca8 | 2020-03-06 23:22:22 +0500 | [diff] [blame] | 2263 | samples" and subchapter "ACL basics" to understand this pattern matching |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 2264 | method. |
| 2265 | |
Thierry FOURNIER | 4dc7197 | 2017-01-28 08:33:08 +0100 | [diff] [blame] | 2266 | Note that :js:attr:`Map.str` is also available for compatibility. |
| 2267 | |
| 2268 | .. js:attribute:: Map._beg |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 2269 | |
| 2270 | See the HAProxy configuration.txt file, chapter "Using ACLs and fetching |
Ilya Shipitsin | 2075ca8 | 2020-03-06 23:22:22 +0500 | [diff] [blame] | 2271 | samples" and subchapter "ACL basics" to understand this pattern matching |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 2272 | method. |
| 2273 | |
Thierry FOURNIER | 4dc7197 | 2017-01-28 08:33:08 +0100 | [diff] [blame] | 2274 | Note that :js:attr:`Map.beg` is also available for compatibility. |
| 2275 | |
| 2276 | .. js:attribute:: Map._sub |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 2277 | |
| 2278 | See the HAProxy configuration.txt file, chapter "Using ACLs and fetching |
Ilya Shipitsin | 2075ca8 | 2020-03-06 23:22:22 +0500 | [diff] [blame] | 2279 | samples" and subchapter "ACL basics" to understand this pattern matching |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 2280 | method. |
| 2281 | |
Thierry FOURNIER | 4dc7197 | 2017-01-28 08:33:08 +0100 | [diff] [blame] | 2282 | Note that :js:attr:`Map.sub` is also available for compatibility. |
| 2283 | |
| 2284 | .. js:attribute:: Map._dir |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 2285 | |
| 2286 | See the HAProxy configuration.txt file, chapter "Using ACLs and fetching |
Ilya Shipitsin | 2075ca8 | 2020-03-06 23:22:22 +0500 | [diff] [blame] | 2287 | samples" and subchapter "ACL basics" to understand this pattern matching |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 2288 | method. |
| 2289 | |
Thierry FOURNIER | 4dc7197 | 2017-01-28 08:33:08 +0100 | [diff] [blame] | 2290 | Note that :js:attr:`Map.dir` is also available for compatibility. |
| 2291 | |
| 2292 | .. js:attribute:: Map._dom |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 2293 | |
| 2294 | See the HAProxy configuration.txt file, chapter "Using ACLs and fetching |
Ilya Shipitsin | 2075ca8 | 2020-03-06 23:22:22 +0500 | [diff] [blame] | 2295 | samples" and subchapter "ACL basics" to understand this pattern matching |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 2296 | method. |
| 2297 | |
Thierry FOURNIER | 4dc7197 | 2017-01-28 08:33:08 +0100 | [diff] [blame] | 2298 | Note that :js:attr:`Map.dom` is also available for compatibility. |
| 2299 | |
| 2300 | .. js:attribute:: Map._end |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 2301 | |
| 2302 | See the HAProxy configuration.txt file, chapter "Using ACLs and fetching |
Ilya Shipitsin | 2075ca8 | 2020-03-06 23:22:22 +0500 | [diff] [blame] | 2303 | samples" and subchapter "ACL basics" to understand this pattern matching |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 2304 | method. |
| 2305 | |
Thierry FOURNIER | 4dc7197 | 2017-01-28 08:33:08 +0100 | [diff] [blame] | 2306 | .. js:attribute:: Map._reg |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 2307 | |
| 2308 | See the HAProxy configuration.txt file, chapter "Using ACLs and fetching |
Ilya Shipitsin | 2075ca8 | 2020-03-06 23:22:22 +0500 | [diff] [blame] | 2309 | samples" and subchapter "ACL basics" to understand this pattern matching |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 2310 | method. |
| 2311 | |
Thierry FOURNIER | 4dc7197 | 2017-01-28 08:33:08 +0100 | [diff] [blame] | 2312 | Note that :js:attr:`Map.reg` is also available for compatibility. |
| 2313 | |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 2314 | |
| 2315 | .. js:function:: Map.new(file, method) |
| 2316 | |
| 2317 | Creates and load a map. |
| 2318 | |
| 2319 | :param string file: Is the file containing the map. |
| 2320 | :param integer method: Is the map pattern matching method. See the attributes |
| 2321 | of the Map class. |
| 2322 | :returns: a class Map object. |
Thierry FOURNIER | 4dc7197 | 2017-01-28 08:33:08 +0100 | [diff] [blame] | 2323 | :see: The Map attributes: :js:attr:`Map._int`, :js:attr:`Map._ip`, |
| 2324 | :js:attr:`Map._str`, :js:attr:`Map._beg`, :js:attr:`Map._sub`, |
| 2325 | :js:attr:`Map._dir`, :js:attr:`Map._dom`, :js:attr:`Map._end` and |
| 2326 | :js:attr:`Map._reg`. |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 2327 | |
| 2328 | .. js:function:: Map.lookup(map, str) |
| 2329 | |
| 2330 | Perform a lookup in a map. |
| 2331 | |
| 2332 | :param class_map map: Is the class Map object. |
| 2333 | :param string str: Is the string used as key. |
| 2334 | :returns: a string containing the result or nil if no match. |
| 2335 | |
| 2336 | .. js:function:: Map.slookup(map, str) |
| 2337 | |
| 2338 | Perform a lookup in a map. |
| 2339 | |
| 2340 | :param class_map map: Is the class Map object. |
| 2341 | :param string str: Is the string used as key. |
| 2342 | :returns: a string containing the result or empty string if no match. |
| 2343 | |
Thierry FOURNIER | dc59500 | 2015-12-21 11:13:52 +0100 | [diff] [blame] | 2344 | .. _applethttp_class: |
| 2345 | |
Thierry FOURNIER | a3bc513 | 2015-09-25 21:43:56 +0200 | [diff] [blame] | 2346 | AppletHTTP class |
Thierry FOURNIER | dc59500 | 2015-12-21 11:13:52 +0100 | [diff] [blame] | 2347 | ================ |
Thierry FOURNIER | a3bc513 | 2015-09-25 21:43:56 +0200 | [diff] [blame] | 2348 | |
| 2349 | .. js:class:: AppletHTTP |
| 2350 | |
| 2351 | This class is used with applets that requires the 'http' mode. The http applet |
| 2352 | can be registered with the *core.register_service()* function. They are used |
| 2353 | for processing an http request like a server in back of HAProxy. |
| 2354 | |
| 2355 | This is an hello world sample code: |
| 2356 | |
| 2357 | .. code-block:: lua |
Thierry FOURNIER | dc59500 | 2015-12-21 11:13:52 +0100 | [diff] [blame] | 2358 | |
Pieter Baauw | 4d7f766 | 2015-11-08 16:38:08 +0100 | [diff] [blame] | 2359 | core.register_service("hello-world", "http", function(applet) |
Thierry FOURNIER | a3bc513 | 2015-09-25 21:43:56 +0200 | [diff] [blame] | 2360 | local response = "Hello World !" |
| 2361 | applet:set_status(200) |
Pieter Baauw | 2dcb9bc | 2015-10-01 22:47:12 +0200 | [diff] [blame] | 2362 | applet:add_header("content-length", string.len(response)) |
Thierry FOURNIER | a3bc513 | 2015-09-25 21:43:56 +0200 | [diff] [blame] | 2363 | applet:add_header("content-type", "text/plain") |
Pieter Baauw | 2dcb9bc | 2015-10-01 22:47:12 +0200 | [diff] [blame] | 2364 | applet:start_response() |
Thierry FOURNIER | a3bc513 | 2015-09-25 21:43:56 +0200 | [diff] [blame] | 2365 | applet:send(response) |
| 2366 | end) |
| 2367 | |
Thierry FOURNIER | dc59500 | 2015-12-21 11:13:52 +0100 | [diff] [blame] | 2368 | .. js:attribute:: AppletHTTP.c |
| 2369 | |
| 2370 | :returns: A :ref:`converters_class` |
| 2371 | |
| 2372 | This attribute contains a Converters class object. |
| 2373 | |
| 2374 | .. js:attribute:: AppletHTTP.sc |
| 2375 | |
| 2376 | :returns: A :ref:`converters_class` |
| 2377 | |
| 2378 | This attribute contains a Converters class object. The |
| 2379 | functions of this object returns always a string. |
| 2380 | |
| 2381 | .. js:attribute:: AppletHTTP.f |
| 2382 | |
| 2383 | :returns: A :ref:`fetches_class` |
| 2384 | |
| 2385 | This attribute contains a Fetches class object. Note that the |
| 2386 | applet execution place cannot access to a valid HAProxy core HTTP |
Ilya Shipitsin | 2075ca8 | 2020-03-06 23:22:22 +0500 | [diff] [blame] | 2387 | transaction, so some sample fetches related to the HTTP dependent |
Thierry FOURNIER | dc59500 | 2015-12-21 11:13:52 +0100 | [diff] [blame] | 2388 | values (hdr, path, ...) are not available. |
| 2389 | |
| 2390 | .. js:attribute:: AppletHTTP.sf |
| 2391 | |
| 2392 | :returns: A :ref:`fetches_class` |
| 2393 | |
| 2394 | This attribute contains a Fetches class object. The functions of |
| 2395 | this object returns always a string. Note that the applet |
| 2396 | execution place cannot access to a valid HAProxy core HTTP |
Ilya Shipitsin | 2075ca8 | 2020-03-06 23:22:22 +0500 | [diff] [blame] | 2397 | transaction, so some sample fetches related to the HTTP dependent |
Thierry FOURNIER | dc59500 | 2015-12-21 11:13:52 +0100 | [diff] [blame] | 2398 | values (hdr, path, ...) are not available. |
| 2399 | |
Thierry FOURNIER | e34a78e | 2015-12-25 01:31:35 +0100 | [diff] [blame] | 2400 | .. js:attribute:: AppletHTTP.method |
Thierry FOURNIER | dc59500 | 2015-12-21 11:13:52 +0100 | [diff] [blame] | 2401 | |
| 2402 | :returns: string |
| 2403 | |
| 2404 | The attribute method returns a string containing the HTTP |
| 2405 | method. |
| 2406 | |
| 2407 | .. js:attribute:: AppletHTTP.version |
| 2408 | |
| 2409 | :returns: string |
| 2410 | |
| 2411 | The attribute version, returns a string containing the HTTP |
| 2412 | request version. |
| 2413 | |
| 2414 | .. js:attribute:: AppletHTTP.path |
| 2415 | |
| 2416 | :returns: string |
| 2417 | |
| 2418 | The attribute path returns a string containing the HTTP |
| 2419 | request path. |
| 2420 | |
| 2421 | .. js:attribute:: AppletHTTP.qs |
| 2422 | |
| 2423 | :returns: string |
| 2424 | |
| 2425 | The attribute qs returns a string containing the HTTP |
| 2426 | request query string. |
| 2427 | |
| 2428 | .. js:attribute:: AppletHTTP.length |
| 2429 | |
| 2430 | :returns: integer |
| 2431 | |
| 2432 | The attribute length returns an integer containing the HTTP |
| 2433 | body length. |
Thierry FOURNIER | a3bc513 | 2015-09-25 21:43:56 +0200 | [diff] [blame] | 2434 | |
Thierry FOURNIER | 841475e | 2015-12-11 17:10:09 +0100 | [diff] [blame] | 2435 | .. js:attribute:: AppletHTTP.headers |
| 2436 | |
Patrick Hemmer | c6a1d71 | 2018-05-01 21:30:41 -0400 | [diff] [blame] | 2437 | :returns: table |
Thierry FOURNIER | dc59500 | 2015-12-21 11:13:52 +0100 | [diff] [blame] | 2438 | |
Patrick Hemmer | c6a1d71 | 2018-05-01 21:30:41 -0400 | [diff] [blame] | 2439 | The attribute headers returns a table containing the HTTP |
Thierry FOURNIER | dc59500 | 2015-12-21 11:13:52 +0100 | [diff] [blame] | 2440 | headers. The header names are always in lower case. As the header name can be |
| 2441 | 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] | 2442 | first index value. The table have this form: |
Thierry FOURNIER | dc59500 | 2015-12-21 11:13:52 +0100 | [diff] [blame] | 2443 | |
| 2444 | .. code-block:: lua |
| 2445 | |
| 2446 | AppletHTTP.headers['<header-name>'][<header-index>] = "<header-value>" |
| 2447 | |
| 2448 | AppletHTTP.headers["host"][0] = "www.test.com" |
| 2449 | AppletHTTP.headers["accept"][0] = "audio/basic q=1" |
| 2450 | AppletHTTP.headers["accept"][1] = "audio/*, q=0.2" |
Thierry FOURNIER | e34a78e | 2015-12-25 01:31:35 +0100 | [diff] [blame] | 2451 | AppletHTTP.headers["accept"][2] = "*/*, q=0.1" |
Thierry FOURNIER | dc59500 | 2015-12-21 11:13:52 +0100 | [diff] [blame] | 2452 | .. |
| 2453 | |
Robin H. Johnson | 52f5db2 | 2017-01-01 13:10:52 -0800 | [diff] [blame] | 2454 | .. js:function:: AppletHTTP.set_status(applet, code [, reason]) |
Thierry FOURNIER | a3bc513 | 2015-09-25 21:43:56 +0200 | [diff] [blame] | 2455 | |
| 2456 | This function sets the HTTP status code for the response. The allowed code are |
| 2457 | from 100 to 599. |
| 2458 | |
Thierry FOURNIER | e34a78e | 2015-12-25 01:31:35 +0100 | [diff] [blame] | 2459 | :param class_AppletHTTP applet: An :ref:`applethttp_class` |
Thierry FOURNIER | a3bc513 | 2015-09-25 21:43:56 +0200 | [diff] [blame] | 2460 | :param integer code: the status code returned to the client. |
Robin H. Johnson | 52f5db2 | 2017-01-01 13:10:52 -0800 | [diff] [blame] | 2461 | :param string reason: the status reason returned to the client (optional). |
Thierry FOURNIER | a3bc513 | 2015-09-25 21:43:56 +0200 | [diff] [blame] | 2462 | |
Thierry FOURNIER | e34a78e | 2015-12-25 01:31:35 +0100 | [diff] [blame] | 2463 | .. js:function:: AppletHTTP.add_header(applet, name, value) |
Thierry FOURNIER | a3bc513 | 2015-09-25 21:43:56 +0200 | [diff] [blame] | 2464 | |
| 2465 | This function add an header in the response. Duplicated headers are not |
| 2466 | collapsed. The special header *content-length* is used to determinate the |
| 2467 | 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] | 2468 | all the write from the function *AppletHTTP:send()* become a chunk. |
Thierry FOURNIER | a3bc513 | 2015-09-25 21:43:56 +0200 | [diff] [blame] | 2469 | |
Thierry FOURNIER | e34a78e | 2015-12-25 01:31:35 +0100 | [diff] [blame] | 2470 | :param class_AppletHTTP applet: An :ref:`applethttp_class` |
Thierry FOURNIER | a3bc513 | 2015-09-25 21:43:56 +0200 | [diff] [blame] | 2471 | :param string name: the header name |
| 2472 | :param string value: the header value |
| 2473 | |
Thierry FOURNIER | e34a78e | 2015-12-25 01:31:35 +0100 | [diff] [blame] | 2474 | .. js:function:: AppletHTTP.start_response(applet) |
Thierry FOURNIER | a3bc513 | 2015-09-25 21:43:56 +0200 | [diff] [blame] | 2475 | |
| 2476 | This function indicates to the HTTP engine that it can process and send the |
| 2477 | response headers. After this called we cannot add headers to the response; We |
| 2478 | cannot use the *AppletHTTP:send()* function if the |
| 2479 | *AppletHTTP:start_response()* is not called. |
| 2480 | |
Thierry FOURNIER | e34a78e | 2015-12-25 01:31:35 +0100 | [diff] [blame] | 2481 | :param class_AppletHTTP applet: An :ref:`applethttp_class` |
| 2482 | |
| 2483 | .. js:function:: AppletHTTP.getline(applet) |
Thierry FOURNIER | a3bc513 | 2015-09-25 21:43:56 +0200 | [diff] [blame] | 2484 | |
| 2485 | This function returns a string containing one line from the http body. If the |
| 2486 | data returned doesn't contains a final '\\n' its assumed than its the last |
| 2487 | available data before the end of stream. |
| 2488 | |
Thierry FOURNIER | e34a78e | 2015-12-25 01:31:35 +0100 | [diff] [blame] | 2489 | :param class_AppletHTTP applet: An :ref:`applethttp_class` |
Thierry FOURNIER | a3bc513 | 2015-09-25 21:43:56 +0200 | [diff] [blame] | 2490 | :returns: a string. The string can be empty if we reach the end of the stream. |
| 2491 | |
Thierry FOURNIER | e34a78e | 2015-12-25 01:31:35 +0100 | [diff] [blame] | 2492 | .. js:function:: AppletHTTP.receive(applet, [size]) |
Thierry FOURNIER | a3bc513 | 2015-09-25 21:43:56 +0200 | [diff] [blame] | 2493 | |
| 2494 | Reads data from the HTTP body, according to the specified read *size*. If the |
| 2495 | *size* is missing, the function tries to read all the content of the stream |
| 2496 | 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] | 2497 | amount of data available. |
Thierry FOURNIER | a3bc513 | 2015-09-25 21:43:56 +0200 | [diff] [blame] | 2498 | |
Thierry FOURNIER | e34a78e | 2015-12-25 01:31:35 +0100 | [diff] [blame] | 2499 | :param class_AppletHTTP applet: An :ref:`applethttp_class` |
Thierry FOURNIER | a3bc513 | 2015-09-25 21:43:56 +0200 | [diff] [blame] | 2500 | :param integer size: the required read size. |
Ilya Shipitsin | 11057a3 | 2020-06-21 21:18:27 +0500 | [diff] [blame] | 2501 | :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] | 2502 | closed. |
| 2503 | |
Thierry FOURNIER | e34a78e | 2015-12-25 01:31:35 +0100 | [diff] [blame] | 2504 | .. js:function:: AppletHTTP.send(applet, msg) |
Thierry FOURNIER | a3bc513 | 2015-09-25 21:43:56 +0200 | [diff] [blame] | 2505 | |
| 2506 | Send the message *msg* on the http request body. |
| 2507 | |
Thierry FOURNIER | e34a78e | 2015-12-25 01:31:35 +0100 | [diff] [blame] | 2508 | :param class_AppletHTTP applet: An :ref:`applethttp_class` |
Thierry FOURNIER | a3bc513 | 2015-09-25 21:43:56 +0200 | [diff] [blame] | 2509 | :param string msg: the message to send. |
| 2510 | |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 2511 | .. js:function:: AppletHTTP.get_priv(applet) |
| 2512 | |
Thierry FOURNIER | 12a865d | 2016-12-14 19:40:37 +0100 | [diff] [blame] | 2513 | Return Lua data stored in the current transaction. If no data are stored, |
| 2514 | it returns a nil value. |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 2515 | |
| 2516 | :param class_AppletHTTP applet: An :ref:`applethttp_class` |
Bertrand Jacquin | 874a35c | 2018-09-10 21:26:07 +0100 | [diff] [blame] | 2517 | :returns: the opaque data previously stored, or nil if nothing is |
| 2518 | available. |
Thierry FOURNIER | 12a865d | 2016-12-14 19:40:37 +0100 | [diff] [blame] | 2519 | :see: :js:func:`AppletHTTP.set_priv` |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 2520 | |
| 2521 | .. js:function:: AppletHTTP.set_priv(applet, data) |
| 2522 | |
| 2523 | Store any data in the current HAProxy transaction. This action replace the |
| 2524 | old stored data. |
| 2525 | |
| 2526 | :param class_AppletHTTP applet: An :ref:`applethttp_class` |
| 2527 | :param opaque data: The data which is stored in the transaction. |
Thierry FOURNIER | 12a865d | 2016-12-14 19:40:37 +0100 | [diff] [blame] | 2528 | :see: :js:func:`AppletHTTP.get_priv` |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 2529 | |
Tim Duesterhus | 4e172c9 | 2020-05-19 13:49:42 +0200 | [diff] [blame] | 2530 | .. js:function:: AppletHTTP.set_var(applet, var, value[, ifexist]) |
Thierry FOURNIER / OZON.IO | c1edafe | 2016-12-12 16:25:30 +0100 | [diff] [blame] | 2531 | |
| 2532 | Converts a Lua type in a HAProxy type and store it in a variable <var>. |
| 2533 | |
| 2534 | :param class_AppletHTTP applet: An :ref:`applethttp_class` |
| 2535 | :param string var: The variable name according with the HAProxy variable syntax. |
| 2536 | :param type value: The value associated to the variable. The type ca be string or |
| 2537 | integer. |
Tim Duesterhus | 4e172c9 | 2020-05-19 13:49:42 +0200 | [diff] [blame] | 2538 | :param boolean ifexist: If this parameter is set to a truthy value the variable |
| 2539 | will only be set if it was defined elsewhere (i.e. used |
| 2540 | within the configuration). It is highly recommended to |
| 2541 | always set this to true. |
Thierry FOURNIER | 12a865d | 2016-12-14 19:40:37 +0100 | [diff] [blame] | 2542 | :see: :js:func:`AppletHTTP.unset_var` |
| 2543 | :see: :js:func:`AppletHTTP.get_var` |
Thierry FOURNIER / OZON.IO | c1edafe | 2016-12-12 16:25:30 +0100 | [diff] [blame] | 2544 | |
| 2545 | .. js:function:: AppletHTTP.unset_var(applet, var) |
| 2546 | |
| 2547 | Unset the variable <var>. |
| 2548 | |
| 2549 | :param class_AppletHTTP applet: An :ref:`applethttp_class` |
| 2550 | :param string var: The variable name according with the HAProxy variable syntax. |
Thierry FOURNIER | 12a865d | 2016-12-14 19:40:37 +0100 | [diff] [blame] | 2551 | :see: :js:func:`AppletHTTP.set_var` |
| 2552 | :see: :js:func:`AppletHTTP.get_var` |
Thierry FOURNIER / OZON.IO | c1edafe | 2016-12-12 16:25:30 +0100 | [diff] [blame] | 2553 | |
| 2554 | .. js:function:: AppletHTTP.get_var(applet, var) |
| 2555 | |
| 2556 | Returns data stored in the variable <var> converter in Lua type. |
| 2557 | |
| 2558 | :param class_AppletHTTP applet: An :ref:`applethttp_class` |
| 2559 | :param string var: The variable name according with the HAProxy variable syntax. |
Thierry FOURNIER | 12a865d | 2016-12-14 19:40:37 +0100 | [diff] [blame] | 2560 | :see: :js:func:`AppletHTTP.set_var` |
| 2561 | :see: :js:func:`AppletHTTP.unset_var` |
Thierry FOURNIER / OZON.IO | c1edafe | 2016-12-12 16:25:30 +0100 | [diff] [blame] | 2562 | |
Thierry FOURNIER | dc59500 | 2015-12-21 11:13:52 +0100 | [diff] [blame] | 2563 | .. _applettcp_class: |
| 2564 | |
Thierry FOURNIER | a3bc513 | 2015-09-25 21:43:56 +0200 | [diff] [blame] | 2565 | AppletTCP class |
| 2566 | =============== |
| 2567 | |
| 2568 | .. js:class:: AppletTCP |
| 2569 | |
| 2570 | This class is used with applets that requires the 'tcp' mode. The tcp applet |
| 2571 | can be registered with the *core.register_service()* function. They are used |
| 2572 | for processing a tcp stream like a server in back of HAProxy. |
| 2573 | |
Thierry FOURNIER | dc59500 | 2015-12-21 11:13:52 +0100 | [diff] [blame] | 2574 | .. js:attribute:: AppletTCP.c |
| 2575 | |
| 2576 | :returns: A :ref:`converters_class` |
| 2577 | |
| 2578 | This attribute contains a Converters class object. |
| 2579 | |
| 2580 | .. js:attribute:: AppletTCP.sc |
| 2581 | |
| 2582 | :returns: A :ref:`converters_class` |
| 2583 | |
| 2584 | This attribute contains a Converters class object. The |
| 2585 | functions of this object returns always a string. |
| 2586 | |
| 2587 | .. js:attribute:: AppletTCP.f |
| 2588 | |
| 2589 | :returns: A :ref:`fetches_class` |
| 2590 | |
| 2591 | This attribute contains a Fetches class object. |
| 2592 | |
| 2593 | .. js:attribute:: AppletTCP.sf |
| 2594 | |
| 2595 | :returns: A :ref:`fetches_class` |
| 2596 | |
| 2597 | This attribute contains a Fetches class object. |
| 2598 | |
Thierry FOURNIER | e34a78e | 2015-12-25 01:31:35 +0100 | [diff] [blame] | 2599 | .. js:function:: AppletTCP.getline(applet) |
Thierry FOURNIER | a3bc513 | 2015-09-25 21:43:56 +0200 | [diff] [blame] | 2600 | |
| 2601 | This function returns a string containing one line from the stream. If the |
| 2602 | data returned doesn't contains a final '\\n' its assumed than its the last |
| 2603 | available data before the end of stream. |
| 2604 | |
Thierry FOURNIER | e34a78e | 2015-12-25 01:31:35 +0100 | [diff] [blame] | 2605 | :param class_AppletTCP applet: An :ref:`applettcp_class` |
Thierry FOURNIER | a3bc513 | 2015-09-25 21:43:56 +0200 | [diff] [blame] | 2606 | :returns: a string. The string can be empty if we reach the end of the stream. |
| 2607 | |
Thierry FOURNIER | e34a78e | 2015-12-25 01:31:35 +0100 | [diff] [blame] | 2608 | .. js:function:: AppletTCP.receive(applet, [size]) |
Thierry FOURNIER | a3bc513 | 2015-09-25 21:43:56 +0200 | [diff] [blame] | 2609 | |
| 2610 | Reads data from the TCP stream, according to the specified read *size*. If the |
| 2611 | *size* is missing, the function tries to read all the content of the stream |
| 2612 | until the end. |
| 2613 | |
Thierry FOURNIER | e34a78e | 2015-12-25 01:31:35 +0100 | [diff] [blame] | 2614 | :param class_AppletTCP applet: An :ref:`applettcp_class` |
Thierry FOURNIER | a3bc513 | 2015-09-25 21:43:56 +0200 | [diff] [blame] | 2615 | :param integer size: the required read size. |
Ilya Shipitsin | 11057a3 | 2020-06-21 21:18:27 +0500 | [diff] [blame] | 2616 | :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] | 2617 | closed. |
| 2618 | |
Thierry FOURNIER | e34a78e | 2015-12-25 01:31:35 +0100 | [diff] [blame] | 2619 | .. js:function:: AppletTCP.send(appletmsg) |
Thierry FOURNIER | a3bc513 | 2015-09-25 21:43:56 +0200 | [diff] [blame] | 2620 | |
| 2621 | Send the message on the stream. |
| 2622 | |
Thierry FOURNIER | e34a78e | 2015-12-25 01:31:35 +0100 | [diff] [blame] | 2623 | :param class_AppletTCP applet: An :ref:`applettcp_class` |
Thierry FOURNIER | a3bc513 | 2015-09-25 21:43:56 +0200 | [diff] [blame] | 2624 | :param string msg: the message to send. |
| 2625 | |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 2626 | .. js:function:: AppletTCP.get_priv(applet) |
| 2627 | |
Thierry FOURNIER | 12a865d | 2016-12-14 19:40:37 +0100 | [diff] [blame] | 2628 | Return Lua data stored in the current transaction. If no data are stored, |
| 2629 | it returns a nil value. |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 2630 | |
| 2631 | :param class_AppletTCP applet: An :ref:`applettcp_class` |
Bertrand Jacquin | 874a35c | 2018-09-10 21:26:07 +0100 | [diff] [blame] | 2632 | :returns: the opaque data previously stored, or nil if nothing is |
| 2633 | available. |
Thierry FOURNIER | 12a865d | 2016-12-14 19:40:37 +0100 | [diff] [blame] | 2634 | :see: :js:func:`AppletTCP.set_priv` |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 2635 | |
| 2636 | .. js:function:: AppletTCP.set_priv(applet, data) |
| 2637 | |
| 2638 | Store any data in the current HAProxy transaction. This action replace the |
| 2639 | old stored data. |
| 2640 | |
| 2641 | :param class_AppletTCP applet: An :ref:`applettcp_class` |
| 2642 | :param opaque data: The data which is stored in the transaction. |
Thierry FOURNIER | 12a865d | 2016-12-14 19:40:37 +0100 | [diff] [blame] | 2643 | :see: :js:func:`AppletTCP.get_priv` |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 2644 | |
Tim Duesterhus | 4e172c9 | 2020-05-19 13:49:42 +0200 | [diff] [blame] | 2645 | .. js:function:: AppletTCP.set_var(applet, var, value[, ifexist]) |
Thierry FOURNIER / OZON.IO | c1edafe | 2016-12-12 16:25:30 +0100 | [diff] [blame] | 2646 | |
| 2647 | Converts a Lua type in a HAProxy type and stores it in a variable <var>. |
| 2648 | |
| 2649 | :param class_AppletTCP applet: An :ref:`applettcp_class` |
| 2650 | :param string var: The variable name according with the HAProxy variable syntax. |
| 2651 | :param type value: The value associated to the variable. The type can be string or |
| 2652 | integer. |
Tim Duesterhus | 4e172c9 | 2020-05-19 13:49:42 +0200 | [diff] [blame] | 2653 | :param boolean ifexist: If this parameter is set to a truthy value the variable |
| 2654 | will only be set if it was defined elsewhere (i.e. used |
| 2655 | within the configuration). It is highly recommended to |
| 2656 | always set this to true. |
Thierry FOURNIER | 12a865d | 2016-12-14 19:40:37 +0100 | [diff] [blame] | 2657 | :see: :js:func:`AppletTCP.unset_var` |
| 2658 | :see: :js:func:`AppletTCP.get_var` |
Thierry FOURNIER / OZON.IO | c1edafe | 2016-12-12 16:25:30 +0100 | [diff] [blame] | 2659 | |
| 2660 | .. js:function:: AppletTCP.unset_var(applet, var) |
| 2661 | |
| 2662 | Unsets the variable <var>. |
| 2663 | |
| 2664 | :param class_AppletTCP applet: An :ref:`applettcp_class` |
| 2665 | :param string var: The variable name according with the HAProxy variable syntax. |
Thierry FOURNIER | 12a865d | 2016-12-14 19:40:37 +0100 | [diff] [blame] | 2666 | :see: :js:func:`AppletTCP.unset_var` |
| 2667 | :see: :js:func:`AppletTCP.set_var` |
Thierry FOURNIER / OZON.IO | c1edafe | 2016-12-12 16:25:30 +0100 | [diff] [blame] | 2668 | |
| 2669 | .. js:function:: AppletTCP.get_var(applet, var) |
| 2670 | |
| 2671 | Returns data stored in the variable <var> converter in Lua type. |
| 2672 | |
| 2673 | :param class_AppletTCP applet: An :ref:`applettcp_class` |
| 2674 | :param string var: The variable name according with the HAProxy variable syntax. |
Thierry FOURNIER | 12a865d | 2016-12-14 19:40:37 +0100 | [diff] [blame] | 2675 | :see: :js:func:`AppletTCP.unset_var` |
| 2676 | :see: :js:func:`AppletTCP.set_var` |
Thierry FOURNIER / OZON.IO | c1edafe | 2016-12-12 16:25:30 +0100 | [diff] [blame] | 2677 | |
Adis Nezirovic | 8878f8e | 2018-07-13 12:18:33 +0200 | [diff] [blame] | 2678 | StickTable class |
| 2679 | ================ |
| 2680 | |
| 2681 | .. js:class:: StickTable |
| 2682 | |
| 2683 | **context**: task, action, sample-fetch |
| 2684 | |
| 2685 | This class can be used to access the HAProxy stick tables from Lua. |
| 2686 | |
| 2687 | .. js:function:: StickTable.info() |
| 2688 | |
| 2689 | Returns stick table attributes as a Lua table. See HAProxy documentation for |
Ilya Shipitsin | 2272d8a | 2020-12-21 01:22:40 +0500 | [diff] [blame] | 2690 | "stick-table" for canonical info, or check out example below. |
Adis Nezirovic | 8878f8e | 2018-07-13 12:18:33 +0200 | [diff] [blame] | 2691 | |
| 2692 | :returns: Lua table |
| 2693 | |
| 2694 | Assume our table has IPv4 key and gpc0 and conn_rate "columns": |
| 2695 | |
| 2696 | .. code-block:: lua |
| 2697 | |
| 2698 | { |
| 2699 | expire=<int>, # Value in ms |
| 2700 | size=<int>, # Maximum table size |
| 2701 | used=<int>, # Actual number of entries in table |
| 2702 | data={ # Data columns, with types as key, and periods as values |
| 2703 | (-1 if type is not rate counter) |
| 2704 | conn_rate=<int>, |
| 2705 | gpc0=-1 |
| 2706 | }, |
| 2707 | length=<int>, # max string length for string table keys, key length |
| 2708 | # otherwise |
| 2709 | nopurge=<boolean>, # purge oldest entries when table is full |
| 2710 | type="ip" # can be "ip", "ipv6", "integer", "string", "binary" |
| 2711 | } |
| 2712 | |
| 2713 | .. js:function:: StickTable.lookup(key) |
| 2714 | |
| 2715 | Returns stick table entry for given <key> |
| 2716 | |
| 2717 | :param string key: Stick table key (IP addresses and strings are supported) |
| 2718 | :returns: Lua table |
| 2719 | |
| 2720 | .. js:function:: StickTable.dump([filter]) |
| 2721 | |
| 2722 | Returns all entries in stick table. An optional filter can be used |
| 2723 | to extract entries with specific data values. Filter is a table with valid |
| 2724 | comparison operators as keys followed by data type name and value pairs. |
| 2725 | Check out the HAProxy docs for "show table" for more details. For the |
| 2726 | reference, the supported operators are: |
| 2727 | "eq", "ne", "le", "lt", "ge", "gt" |
| 2728 | |
| 2729 | For large tables, execution of this function can take a long time (for |
| 2730 | HAProxy standards). That's also true when filter is used, so take care and |
| 2731 | measure the impact. |
| 2732 | |
| 2733 | :param table filter: Stick table filter |
| 2734 | :returns: Stick table entries (table) |
| 2735 | |
| 2736 | See below for example filter, which contains 4 entries (or comparisons). |
| 2737 | (Maximum number of filter entries is 4, defined in the source code) |
| 2738 | |
| 2739 | .. code-block:: lua |
| 2740 | |
| 2741 | local filter = { |
| 2742 | {"gpc0", "gt", 30}, {"gpc1", "gt", 20}}, {"conn_rate", "le", 10} |
| 2743 | } |
| 2744 | |
Christopher Faulet | 0f3c890 | 2020-01-31 18:57:12 +0100 | [diff] [blame] | 2745 | .. _action_class: |
| 2746 | |
| 2747 | Action class |
| 2748 | ============= |
| 2749 | |
| 2750 | .. js:class:: Act |
| 2751 | |
| 2752 | **context**: action |
| 2753 | |
| 2754 | This class contains all return codes an action may return. It is the lua |
| 2755 | equivalent to HAProxy "ACT_RET_*" code. |
| 2756 | |
| 2757 | .. code-block:: lua |
| 2758 | |
| 2759 | core.register_action("deny", { "http-req" }, function (txn) |
| 2760 | return act.DENY |
| 2761 | end) |
| 2762 | .. |
| 2763 | .. js:attribute:: act.CONTINUE |
| 2764 | |
| 2765 | This attribute is an integer (0). It instructs HAProxy to continue the current |
| 2766 | ruleset processing on the message. It is the default return code for a lua |
| 2767 | action. |
| 2768 | |
| 2769 | :returns: integer |
| 2770 | |
| 2771 | .. js:attribute:: act.STOP |
| 2772 | |
| 2773 | This attribute is an integer (1). It instructs HAProxy to stop the current |
| 2774 | ruleset processing on the message. |
| 2775 | |
| 2776 | .. js:attribute:: act.YIELD |
| 2777 | |
| 2778 | This attribute is an integer (2). It instructs HAProxy to temporarily pause |
| 2779 | the message processing. It will be resumed later on the same rule. The |
| 2780 | corresponding lua script is re-executed for the start. |
| 2781 | |
| 2782 | .. js:attribute:: act.ERROR |
| 2783 | |
| 2784 | This attribute is an integer (3). It triggers an internal errors The message |
| 2785 | processing is stopped and the transaction is terminated. For HTTP streams, an |
| 2786 | HTTP 500 error is returned to the client. |
| 2787 | |
| 2788 | :returns: integer |
| 2789 | |
| 2790 | .. js:attribute:: act.DONE |
| 2791 | |
| 2792 | This attribute is an integer (4). It instructs HAProxy to stop the message |
| 2793 | processing. |
| 2794 | |
| 2795 | :returns: integer |
| 2796 | |
| 2797 | .. js:attribute:: act.DENY |
| 2798 | |
| 2799 | This attribute is an integer (5). It denies the current message. The message |
| 2800 | processing is stopped and the transaction is terminated. For HTTP streams, an |
| 2801 | HTTP 403 error is returned to the client if the deny is returned during the |
| 2802 | request analysis. During the response analysis, an HTTP 502 error is returned |
| 2803 | and the server response is discarded. |
| 2804 | |
| 2805 | :returns: integer |
| 2806 | |
| 2807 | .. js:attribute:: act.ABORT |
| 2808 | |
| 2809 | This attribute is an integer (6). It aborts the current message. The message |
| 2810 | processing is stopped and the transaction is terminated. For HTTP streams, |
| 2811 | HAproxy assumes a response was already sent to the client. From the Lua |
| 2812 | actions point of view, when this code is used, the transaction is terminated |
| 2813 | with no reply. |
| 2814 | |
| 2815 | :returns: integer |
| 2816 | |
| 2817 | .. js:attribute:: act.INVALID |
| 2818 | |
| 2819 | This attribute is an integer (7). It triggers an internal errors. The message |
| 2820 | processing is stopped and the transaction is terminated. For HTTP streams, an |
| 2821 | HTTP 400 error is returned to the client if the error is returned during the |
| 2822 | request analysis. During the response analysis, an HTTP 502 error is returned |
| 2823 | and the server response is discarded. |
| 2824 | |
| 2825 | :returns: integer |
Adis Nezirovic | 8878f8e | 2018-07-13 12:18:33 +0200 | [diff] [blame] | 2826 | |
Christopher Faulet | 2c2c2e3 | 2020-01-31 19:07:52 +0100 | [diff] [blame] | 2827 | .. js:function:: act:wake_time(milliseconds) |
| 2828 | |
| 2829 | **context**: action |
| 2830 | |
| 2831 | Set the script pause timeout to the specified time, defined in |
| 2832 | milliseconds. |
| 2833 | |
| 2834 | :param integer milliseconds: the required milliseconds. |
| 2835 | |
| 2836 | This function may be used when a lua action returns `act.YIELD`, to force its |
| 2837 | wake-up at most after the specified number of milliseconds. |
| 2838 | |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 2839 | External Lua libraries |
| 2840 | ====================== |
| 2841 | |
| 2842 | A lot of useful lua libraries can be found here: |
| 2843 | |
| 2844 | * `https://lua-toolbox.com/ <https://lua-toolbox.com/>`_ |
| 2845 | |
Ilya Shipitsin | 2075ca8 | 2020-03-06 23:22:22 +0500 | [diff] [blame] | 2846 | Redis client library: |
Thierry FOURNIER | 17bd152 | 2015-03-11 20:31:00 +0100 | [diff] [blame] | 2847 | |
| 2848 | * `https://github.com/nrk/redis-lua <https://github.com/nrk/redis-lua>`_ |
| 2849 | |
| 2850 | This is an example about the usage of the Redis library with HAProxy. Note that |
| 2851 | each call of any function of this library can throw an error if the socket |
| 2852 | connection fails. |
| 2853 | |
| 2854 | .. code-block:: lua |
| 2855 | |
| 2856 | -- load the redis library |
| 2857 | local redis = require("redis"); |
| 2858 | |
| 2859 | function do_something(txn) |
| 2860 | |
| 2861 | -- create and connect new tcp socket |
| 2862 | local tcp = core.tcp(); |
| 2863 | tcp:settimeout(1); |
| 2864 | tcp:connect("127.0.0.1", 6379); |
| 2865 | |
| 2866 | -- use the redis library with this new socket |
| 2867 | local client = redis.connect({socket=tcp}); |
| 2868 | client:ping(); |
| 2869 | |
| 2870 | end |
| 2871 | |
| 2872 | OpenSSL: |
| 2873 | |
| 2874 | * `http://mkottman.github.io/luacrypto/index.html |
| 2875 | <http://mkottman.github.io/luacrypto/index.html>`_ |
| 2876 | |
| 2877 | * `https://github.com/brunoos/luasec/wiki |
| 2878 | <https://github.com/brunoos/luasec/wiki>`_ |